| Lines 16-117
          
      
      
        Link Here | 
        
          | 16 | # along with Koha; if not, see <http://www.gnu.org/licenses>. | 16 | # along with Koha; if not, see <http://www.gnu.org/licenses>. | 
        
          | 17 |  | 17 |  | 
        
          | 18 | use Modern::Perl; | 18 | use Modern::Perl; | 
          
            
              | 19 |  | 19 | use Test::More tests => 4; | 
            
              | 20 | use C4::Context; |  |  | 
            
              | 21 | use Test::More; | 
        
          | 22 | use Test::MockModule; | 20 | use Test::MockModule; | 
        
          | 23 |  | 21 |  | 
          
            
              | 24 | use Module::Load::Conditional qw/check_install/; | 22 | use t::lib::TestBuilder; | 
            
              | 25 |  |  |  | 
            
              | 26 | BEGIN { | 
            
              | 27 |     if ( check_install( module => 'Test::DBIx::Class' ) ) { | 
            
              | 28 |         plan tests => 39; | 
            
              | 29 |     } else { | 
            
              | 30 |         plan skip_all => "Need Test::DBIx::Class" | 
            
              | 31 |     } | 
            
              | 32 | } | 
        
          | 33 |  | 23 |  | 
        
          | 34 | use_ok('C4::Koha', qw( xml_escape GetVariationsOfISBN GetVariationsOfISBNs GetVariationsOfISSN GetVariationsOfISSNs)); | 24 | use_ok('C4::Koha', qw( xml_escape GetVariationsOfISBN GetVariationsOfISBNs GetVariationsOfISSN GetVariationsOfISSNs)); | 
        
          | 35 |  | 25 |  | 
          
            
              | 36 | use Test::DBIx::Class; | 26 | use C4::Context; | 
            
              | 37 |  | 27 | use Koha::Libraries; | 
            
              | 38 | sub fixtures { | 28 |  | 
            
              | 39 |     my ( $libraries ) = @_; | 29 | subtest 'isbn stuff' => sub { | 
            
              | 40 |     fixtures_ok [ | 30 |     plan tests => 29; | 
            
              | 41 |         Branch => [ | 31 |  | 
            
              | 42 |             ['branchcode', 'branchname'], | 32 |     my $isbn13 = "9780330356473"; | 
            
              | 43 |             @$libraries, | 33 |     my $isbn13D = "978-0-330-35647-3"; | 
            
              | 44 |         ] | 34 |     my $isbn10 = "033035647X"; | 
            
              | 45 |     ], 'add fixtures'; | 35 |     my $isbn10D = "0-330-35647-X"; | 
            
              | 46 | } | 36 |  | 
            
              | 47 |  | 37 |     my $undef = undef; | 
            
              | 48 | my $db = Test::MockModule->new('Koha::Database'); | 38 |     is(xml_escape($undef), '', 'xml_escape() returns empty string on undef input'); | 
            
              | 49 | $db->mock( _new_schema => sub { return Schema(); } ); | 39 |     my $str = q{'"&<>'}; | 
            
              | 50 | Koha::Database::flush_schema_cache(); | 40 |     is(xml_escape($str), ''"&<>'', 'xml_escape() works as expected'); | 
            
              | 51 |  | 41 |     is($str, q{'"&<>'}, '... and does not change input in place'); | 
            
              | 52 | my $libraries = [ | 42 |  | 
            
              | 53 |     ['XXX_test', 'my branchname XXX'], | 43 |     is(C4::Koha::_isbn_cleanup('0-590-35340-3'), '0590353403', '_isbn_cleanup removes hyphens'); | 
            
              | 54 | ]; | 44 |     is(C4::Koha::_isbn_cleanup('0590353403 (pbk.)'), '0590353403', '_isbn_cleanup removes parenthetical'); | 
            
              | 55 | fixtures($libraries); | 45 |     is(C4::Koha::_isbn_cleanup('978-0-321-49694-2'), '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10'); | 
            
              | 56 |  | 46 |  | 
            
              | 57 | my $isbn13 = "9780330356473"; | 47 |     is(C4::Koha::NormalizeISBN({ isbn => '978-0-321-49694-2 (pbk.)', format => 'ISBN-10', strip_hyphens => 1 }), '0321496949', 'Test NormalizeISBN with all features enabled' ); | 
            
              | 58 | my $isbn13D = "978-0-330-35647-3"; | 48 |  | 
            
              | 59 | my $isbn10 = "033035647X"; | 49 |     my @isbns = qw/ 978-0-321-49694-2 0-321-49694-9 978-0-321-49694-2 0321496949 9780321496942/; | 
            
              | 60 | my $isbn10D = "0-330-35647-X"; | 50 |     is( join('|', @isbns), join('|', GetVariationsOfISBN('978-0-321-49694-2 (pbk.)')), 'GetVariationsOfISBN returns all variations' ); | 
            
              | 61 |  | 51 |  | 
            
              | 62 | my $undef = undef; | 52 |     is( join('|', @isbns), join('|', GetVariationsOfISBNs('978-0-321-49694-2 (pbk.)')), 'GetVariationsOfISBNs returns all variations' ); | 
            
              | 63 | is(xml_escape($undef), '', 'xml_escape() returns empty string on undef input'); | 53 |  | 
            
              | 64 | my $str = q{'"&<>'}; | 54 |     my $isbn; | 
            
              | 65 | is(xml_escape($str), ''"&<>'', 'xml_escape() works as expected'); | 55 |     eval { | 
            
              | 66 | is($str, q{'"&<>'}, '... and does not change input in place'); | 56 |         $isbn = C4::Koha::NormalizeISBN({ isbn => '0788893777 (2 DVD 45th ed)', format => 'ISBN-10', strip_hyphens => 1 }); | 
            
              | 67 |  | 57 |     }; | 
            
              | 68 | is(C4::Koha::_isbn_cleanup('0-590-35340-3'), '0590353403', '_isbn_cleanup removes hyphens'); | 58 |     ok($@ eq '', 'NormalizeISBN does not throw exception when parsing invalid ISBN (bug 12243)'); | 
            
              | 69 | is(C4::Koha::_isbn_cleanup('0590353403 (pbk.)'), '0590353403', '_isbn_cleanup removes parenthetical'); | 59 |     $isbn = C4::Koha::NormalizeISBN({ isbn => '0788893777 (2 DVD 45th ed)', format => 'ISBN-10', strip_hyphens => 1, return_invalid =>1 }); | 
            
              | 70 | is(C4::Koha::_isbn_cleanup('978-0-321-49694-2'), '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10'); | 60 |     is($isbn, '0788893777 (2 DVD 45th ed)', 'NormalizeISBN returns original string when converting to ISBN10 an ISBN starting with 979 (bug 13167)'); | 
            
              |  |  | 61 |  | 
            
              | 62 |     eval { | 
            
              | 63 |         $isbn = C4::Koha::NormalizeISBN({ isbn => '979-10-90085-00-8', format => 'ISBN-10', strip_hyphens => 1 }); | 
            
              | 64 |     }; | 
            
              | 65 |     ok($@ eq '', 'NormalizeISBN does not throw exception when converting to ISBN10 an ISBN starting with 979 (bug 13167)'); | 
            
              | 66 |     ok(!defined $isbn, 'NormalizeISBN returns undef when converting to ISBN10 an ISBN starting with 979 (bug 13167)'); | 
            
              | 67 |  | 
            
              | 68 |     @isbns = GetVariationsOfISBNs('abc'); | 
            
              | 69 |     is(@isbns == 1 && $isbns[0] eq 'abc', 1, 'The unaltered version should be returned if invalid'); | 
            
              | 70 |  | 
            
              | 71 |     is( C4::Koha::GetNormalizedISBN('9780062059994 (hardcover bdg.) | 0062059998 (hardcover bdg.)'), '0062059998', 'Test GetNormalizedISBN' ); | 
            
              | 72 |     is( C4::Koha::GetNormalizedISBN('9780385753067 (trade) | 0385753063 (trade) | 9780385753074 (lib. bdg.) | 0385753071 (lib. bdg.)'), '0385753063', 'Test GetNormalizedISBN' ); | 
            
              | 73 |     is( C4::Koha::GetNormalizedISBN('9781432829162 (hardcover) | 1432829165 (hardcover)'), '1432829165', 'Test GetNormalizedISBN' ); | 
            
              | 74 |     is( C4::Koha::GetNormalizedISBN('9780062063625 (hardcover) | 9780062063632 | 0062063634'), '0062063626', 'Test GetNormalizedISBN' ); | 
            
              | 75 |     is( C4::Koha::GetNormalizedISBN('9780062059932 (hardback)'), '0062059939', 'Test GetNormalizedISBN' ); | 
            
              | 76 |     is( C4::Koha::GetNormalizedISBN('9780316370318 (hardback) | 9780316376266 (special edition hardcover) | 9780316405454 (international paperback edition)'), '0316370312', 'Test GetNormalizedISBN' ); | 
            
              | 77 |     is( C4::Koha::GetNormalizedISBN('9781595148032 (hbk.) | 1595148035 (hbk.)') , '1595148035', 'Test GetNormalizedISBN' ); | 
            
              | 78 |     is( C4::Koha::GetNormalizedISBN('9780062349859 | 0062349856 | 9780062391308 | 0062391305'), '0062349856', 'Test GetNormalizedISBN' ); | 
            
              | 79 |     is( C4::Koha::GetNormalizedISBN('9781250075345 (hardcover) | 1250075343 (hardcover) | 9781250049872 (trade pbk.) | 1250049873 (trade pbk.)'), '1250075343', 'Test GetNormalizedISBN' ); | 
            
              | 80 |     is( C4::Koha::GetNormalizedISBN('9781250067128 | 125006712X'), '125006712X', 'Test GetNormalizedISBN' ); | 
            
              | 81 |     is( C4::Koha::GetNormalizedISBN('9780373211463 | 0373211465'), '0373211465', 'Test GetNormalizedISBN' ); | 
            
              | 82 |  | 
            
              | 83 |     is( C4::Koha::GetNormalizedUPC(), undef, 'GetNormalizedUPC should return undef if no record is passed' ); | 
            
              | 84 |     is( C4::Koha::GetNormalizedISBN(), undef, 'GetNormalizedISBN should return undef if no record and no isbn are passed' ); | 
            
              | 85 |     is( C4::Koha::GetNormalizedEAN(), undef, 'GetNormalizedEAN should return undef if no record and no isbn are passed' ); | 
            
              | 86 |     is( C4::Koha::GetNormalizedOCLCNumber(), undef, 'GetNormalizedOCLCNumber should return undef if no record and no isbn are passed' ); | 
            
              | 87 | }; | 
        
          | 71 |  | 88 |  | 
          
            
              | 72 | is(C4::Koha::NormalizeISBN({ isbn => '978-0-321-49694-2 (pbk.)', format => 'ISBN-10', strip_hyphens => 1 }), '0321496949', 'Test NormalizeISBN with all features enabled' ); | 89 | subtest 'issn stuff' => sub { | 
            
              |  |  | 90 |     plan tests => 7; | 
        
          | 73 |  | 91 |  | 
          
            
              | 74 | my @isbns = qw/ 978-0-321-49694-2 0-321-49694-9 978-0-321-49694-2 0321496949 9780321496942/; | 92 |     is(C4::Koha::NormalizeISSN({ issn => '0024-9319', strip_hyphen => 1 }), '00249319', 'Test NormalizeISSN with all features enabled' ); | 
            
              | 75 | is( join('|', @isbns), join('|', GetVariationsOfISBN('978-0-321-49694-2 (pbk.)')), 'GetVariationsOfISBN returns all variations' ); | 93 |     is(C4::Koha::NormalizeISSN({ issn => '0024-9319', strip_hyphen => 0 }), '0024-9319', 'Test NormalizeISSN with all features enabled' ); | 
        
          | 76 |  | 94 |  | 
          
            
              | 77 | is( join('|', @isbns), join('|', GetVariationsOfISBNs('978-0-321-49694-2 (pbk.)')), 'GetVariationsOfISBNs returns all variations' ); | 95 |     my @issns = qw/ 0024-9319 00249319 /; | 
            
              |  |  | 96 |     is( join('|', @issns), join('|', GetVariationsOfISSN('0024-9319')), 'GetVariationsOfISSN returns all variations' ); | 
            
              | 97 |     is( join('|', @issns), join('|', GetVariationsOfISSNs('0024-9319')), 'GetVariationsOfISSNs returns all variations' ); | 
        
          | 78 |  | 98 |  | 
          
            
              | 79 | my $isbn; | 99 |     my $issn; | 
            
              | 80 | eval { | 100 |     eval { | 
            
              | 81 |     $isbn = C4::Koha::NormalizeISBN({ isbn => '0788893777 (2 DVD 45th ed)', format => 'ISBN-10', strip_hyphens => 1 }); | 101 |         $issn = C4::Koha::NormalizeISSN({ issn => '1234-5678', strip_hyphen => 1 }); | 
            
              | 82 | }; | 102 |     }; | 
            
              | 83 | ok($@ eq '', 'NormalizeISBN does not throw exception when parsing invalid ISBN (bug 12243)'); | 103 |     ok($@ eq '', 'NormalizeISSN does not throw exception when parsing invalid ISSN'); | 
            
              | 84 | $isbn = C4::Koha::NormalizeISBN({ isbn => '0788893777 (2 DVD 45th ed)', format => 'ISBN-10', strip_hyphens => 1, return_invalid =>1 }); |  |  | 
            
              | 85 | is($isbn, '0788893777 (2 DVD 45th ed)', 'NormalizeISBN returns original string when converting to ISBN10 an ISBN starting with 979 (bug 13167)'); | 
        
          | 86 |  | 104 |  | 
          
            
              | 87 | eval { | 105 |     @issns = GetVariationsOfISSNs('abc'); | 
            
              | 88 |     $isbn = C4::Koha::NormalizeISBN({ isbn => '979-10-90085-00-8', format => 'ISBN-10', strip_hyphens => 1 }); | 106 |     is($issns[0], 'abc', 'Original ISSN passed through even if invalid'); | 
            
              |  |  | 107 |     is(scalar(@issns), 1, 'zero additional variations returned of invalid ISSN'); | 
        
          | 89 | }; | 108 | }; | 
            
              | 90 | ok($@ eq '', 'NormalizeISBN does not throw exception when converting to ISBN10 an ISBN starting with 979 (bug 13167)'); |  |  | 
            
              | 91 | ok(!defined $isbn, 'NormalizeISBN returns undef when converting to ISBN10 an ISBN starting with 979 (bug 13167)'); | 
            
              | 92 |  | 
            
              | 93 | @isbns = GetVariationsOfISBNs('abc'); | 
            
              | 94 | is(@isbns == 1 && $isbns[0] eq 'abc', 1, 'The unaltered version should be returned if invalid'); | 
            
              | 95 |  | 
            
              | 96 | is( C4::Koha::GetNormalizedISBN('9780062059994 (hardcover bdg.) | 0062059998 (hardcover bdg.)'), '0062059998', 'Test GetNormalizedISBN' ); | 
            
              | 97 | is( C4::Koha::GetNormalizedISBN('9780385753067 (trade) | 0385753063 (trade) | 9780385753074 (lib. bdg.) | 0385753071 (lib. bdg.)'), '0385753063', 'Test GetNormalizedISBN' ); | 
            
              | 98 | is( C4::Koha::GetNormalizedISBN('9781432829162 (hardcover) | 1432829165 (hardcover)'), '1432829165', 'Test GetNormalizedISBN' ); | 
            
              | 99 | is( C4::Koha::GetNormalizedISBN('9780062063625 (hardcover) | 9780062063632 | 0062063634'), '0062063626', 'Test GetNormalizedISBN' ); | 
            
              | 100 | is( C4::Koha::GetNormalizedISBN('9780062059932 (hardback)'), '0062059939', 'Test GetNormalizedISBN' ); | 
            
              | 101 | is( C4::Koha::GetNormalizedISBN('9780316370318 (hardback) | 9780316376266 (special edition hardcover) | 9780316405454 (international paperback edition)'), '0316370312', 'Test GetNormalizedISBN' ); | 
            
              | 102 | is( C4::Koha::GetNormalizedISBN('9781595148032 (hbk.) | 1595148035 (hbk.)') , '1595148035', 'Test GetNormalizedISBN' ); | 
            
              | 103 | is( C4::Koha::GetNormalizedISBN('9780062349859 | 0062349856 | 9780062391308 | 0062391305'), '0062349856', 'Test GetNormalizedISBN' ); | 
            
              | 104 | is( C4::Koha::GetNormalizedISBN('9781250075345 (hardcover) | 1250075343 (hardcover) | 9781250049872 (trade pbk.) | 1250049873 (trade pbk.)'), '1250075343', 'Test GetNormalizedISBN' ); | 
            
              | 105 | is( C4::Koha::GetNormalizedISBN('9781250067128 | 125006712X'), '125006712X', 'Test GetNormalizedISBN' ); | 
            
              | 106 | is( C4::Koha::GetNormalizedISBN('9780373211463 | 0373211465'), '0373211465', 'Test GetNormalizedISBN' ); | 
            
              | 107 |  | 
            
              | 108 | is( C4::Koha::GetNormalizedUPC(), undef, 'GetNormalizedUPC should return undef if no record is passed' ); | 
            
              | 109 | is( C4::Koha::GetNormalizedISBN(), undef, 'GetNormalizedISBN should return undef if no record and no isbn are passed' ); | 
            
              | 110 | is( C4::Koha::GetNormalizedEAN(), undef, 'GetNormalizedEAN should return undef if no record and no isbn are passed' ); | 
            
              | 111 | is( C4::Koha::GetNormalizedOCLCNumber(), undef, 'GetNormalizedOCLCNumber should return undef if no record and no isbn are passed' ); | 
        
          | 112 |  | 109 |  | 
        
          | 113 | subtest 'getFacets() tests' => sub { | 110 | subtest 'getFacets() tests' => sub { | 
          
            
              | 114 |     plan tests => 5; | 111 |     plan tests => 4; | 
            
              |  |  | 112 |  | 
            
              | 113 |     my $count = 1; | 
            
              | 114 |     my $library_module = Test::MockModule->new('Koha::Libraries'); | 
            
              | 115 |     $library_module->mock( 'count', sub { return $count } ); | 
        
          | 115 |  | 116 |  | 
        
          | 116 |     is ( Koha::Libraries->search->count, 1, 'There should be only 1 library (singleBranchMode on)' ); | 117 |     is ( Koha::Libraries->search->count, 1, 'There should be only 1 library (singleBranchMode on)' ); | 
        
          | 117 |     my $facets = C4::Koha::getFacets(); | 118 |     my $facets = C4::Koha::getFacets(); | 
  
    | Lines 121-132
          subtest 'getFacets() tests' => sub {
      
      
        Link Here | 
        
          | 121 |         'location facet present with singleBranchMode on (bug 10078)' | 122 |         'location facet present with singleBranchMode on (bug 10078)' | 
        
          | 122 |     ); | 123 |     ); | 
        
          | 123 |  | 124 |  | 
          
            
              | 124 |     $libraries = [ | 125 |     $count = 3;    # more libraries.. | 
            
              | 125 |         ['YYY_test', 'my branchname YYY'], | 126 |     is ( Koha::Libraries->search->count, 3, 'There should be more than 1 library (singleBranchMode off)' ); | 
            
              | 126 |         ['ZZZ_test', 'my branchname XXX'], |  |  | 
            
              | 127 |     ]; | 
            
              | 128 |     fixtures($libraries); | 
            
              | 129 |     is ( Koha::Libraries->search->count, 3, 'There should be only more than 1 library (singleBranchMode off)' ); | 
        
          | 130 |  | 127 |  | 
        
          | 131 |     $facets = C4::Koha::getFacets(); | 128 |     $facets = C4::Koha::getFacets(); | 
        
          | 132 |     is( | 129 |     is( | 
  
    | Lines 135-155
          subtest 'getFacets() tests' => sub {
      
      
        Link Here | 
        
          | 135 |         'location facet present with singleBranchMode off (bug 10078)' | 132 |         'location facet present with singleBranchMode off (bug 10078)' | 
        
          | 136 |     ); | 133 |     ); | 
        
          | 137 | }; | 134 | }; | 
            
              | 138 |  |  |  | 
            
              | 139 | is(C4::Koha::NormalizeISSN({ issn => '0024-9319', strip_hyphen => 1 }), '00249319', 'Test NormalizeISSN with all features enabled' ); | 
            
              | 140 | is(C4::Koha::NormalizeISSN({ issn => '0024-9319', strip_hyphen => 0 }), '0024-9319', 'Test NormalizeISSN with all features enabled' ); | 
            
              | 141 |  | 
            
              | 142 | my @issns = qw/ 0024-9319 00249319 /; | 
            
              | 143 | is( join('|', @issns), join('|', GetVariationsOfISSN('0024-9319')), 'GetVariationsOfISSN returns all variations' ); | 
            
              | 144 | is( join('|', @issns), join('|', GetVariationsOfISSNs('0024-9319')), 'GetVariationsOfISSNs returns all variations' ); | 
            
              | 145 |  | 
            
              | 146 | my $issn; | 
            
              | 147 | eval { | 
            
              | 148 |     $issn = C4::Koha::NormalizeISSN({ issn => '1234-5678', strip_hyphen => 1 }); | 
            
              | 149 | }; | 
            
              | 150 | ok($@ eq '', 'NormalizeISSN does not throw exception when parsing invalid ISSN'); | 
            
              | 151 |  | 
            
              | 152 | @issns = GetVariationsOfISSNs('abc'); | 
            
              | 153 | is($issns[0], 'abc', 'Original ISSN passed through even if invalid'); | 
            
              | 154 | is(scalar(@issns), 1, 'zero additional variations returned of invalid ISSN'); | 
            
              | 155 |  | 
            
              | 156 | -  |