Bug 10500: Improve ISBN matching when importing records
[koha.git] / t / Koha.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use C4::Context;
6 use Test::More tests => 14;
7 use Test::MockModule;
8 use DBD::Mock;
9
10 use_ok('C4::Koha');
11
12 my $module_context = new Test::MockModule('C4::Context');
13 $module_context->mock(
14     '_new_dbh',
15     sub {
16         my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
17           || die "Cannot create handle: $DBI::errstr\n";
18         return $dbh;
19     }
20 );
21
22 SKIP: {
23
24     skip "DBD::Mock is too old", 3
25         unless $DBD::Mock::VERSION >= 1.45;
26
27     my @loc_results = (['category'],['LOC']);
28     my @empty_results = ([]);
29     my @relterms_results = (['category'],['RELTERMS']);
30
31     my $dbh = C4::Context->dbh();
32
33     $dbh->{mock_add_resultset} = \@loc_results;
34     is ( IsAuthorisedValueCategory('LOC'), 1, 'LOC is a valid authorized value category');
35     $dbh->{mock_add_resultset} = \@empty_results;
36     is ( IsAuthorisedValueCategory('something'), 0, 'something is not a valid authorized value category');
37     $dbh->{mock_add_resultset} = \@relterms_results;
38     is ( IsAuthorisedValueCategory('RELTERMS'), 1, 'RELTERMS is a valid authorized value category');
39
40 } # End SKIP block
41
42 #
43 # test that &slashifyDate returns correct (non-US) date
44 #
45 my $date = "01/01/2002";
46 my $newdate = &slashifyDate("2002-01-01");
47 my $isbn13 = "9780330356473";
48 my $isbn13D = "978-0-330-35647-3";
49 my $isbn10 = "033035647X";
50 my $isbn10D = "0-330-35647-X";
51
52 ok($date eq $newdate, 'slashifyDate');
53
54 my $undef = undef;
55 is(xml_escape($undef), '', 'xml_escape() returns empty string on undef input');
56 my $str = q{'"&<>'};
57 is(xml_escape($str), '&apos;&quot;&amp;&lt;&gt;&apos;', 'xml_escape() works as expected');
58 is($str, q{'"&<>'}, '... and does not change input in place');
59
60 is(C4::Koha::_isbn_cleanup('0-590-35340-3'), '0590353403', '_isbn_cleanup removes hyphens');
61 is(C4::Koha::_isbn_cleanup('0590353403 (pbk.)'), '0590353403', '_isbn_cleanup removes parenthetical');
62 is(C4::Koha::_isbn_cleanup('978-0-321-49694-2'), '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10');
63
64 is(C4::Koha::NormalizeISBN({ isbn => '978-0-321-49694-2 (pbk.)', format => 'ISBN-10', strip_hyphens => 1 }), '0321496949', 'Test NormalizeISBN with all features enabled' );
65
66 my @isbns = qw/ 978-0-321-49694-2 0-321-49694-9 978-0-321-49694-2 0321496949 9780321496942/;
67 is( join('|', @isbns), join('|', GetVariationsOfISBN('978-0-321-49694-2 (pbk.)')), 'GetVariationsOfISBN returns all variations' );
68
69 is( join('|', @isbns), join('|', GetVariationsOfISBNs('978-0-321-49694-2 (pbk.)')), 'GetVariationsOfISBNs returns all variations' );