a9524842a3a7c3ec5b569e35224a9511801c6dc9
[koha-equinox.git] / t / db_dependent / XISBN.t
1 #!/usr/bin/perl
2 #
3 # This Koha test module is a stub!
4 # Add more tests here!!!
5
6 use Modern::Perl;
7
8 # use Test::Class::Load qw ( t/db_dependent/ );
9 use Test::More tests => 5;
10 use MARC::Record;
11 use C4::Biblio;
12 use C4::XISBN;
13 use Data::Dumper;
14 use C4::Context;
15
16 BEGIN {
17     use_ok('C4::XISBN');
18 }
19
20 # Avoid "redefined subroutine" warnings
21 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
22 *C4::Search::SimpleSearch = \&Mock_SimpleSearch;
23
24 # KohaTest::clear_test_database();
25 # KohaTest::create_test_database();
26
27 my $context = C4::Context->new;
28
29 my ( $biblionumber_tag, $biblionumber_subfield ) =
30   GetMarcFromKohaField( 'biblio.biblionumber', '' );
31 my ( $isbn_tag, $isbn_subfield ) =
32   GetMarcFromKohaField( 'biblioitems.isbn', '' );
33
34 # Harry Potter and the Sorcerer's Stone, 1st American ed. 1997
35 my $isbn1 = '0590353403';
36 # ThingISBN match : Silent Wing, First Edition 1998
37 my $isbn2 = '0684843897';
38 # XISBN match : Harry Potter and the Philosopher's Stone, Magic ed. 2000
39 my $isbn3 = '1551923963';
40
41 my $biblionumber1 = _add_biblio_with_isbn($isbn1);
42 my $biblionumber2 = _add_biblio_with_isbn($isbn2);
43 my $biblionumber3 = _add_biblio_with_isbn($isbn3);
44
45 my $trial = C4::XISBN::get_biblionumber_from_isbn($isbn1);
46 is( $trial->[0]->{biblionumber},
47     $biblionumber1,
48     "It gets the correct biblionumber from the only isbn we have added." );
49
50 $trial = C4::XISBN::_get_biblio_from_xisbn($isbn1);
51 is( $trial->{biblionumber},
52     $biblionumber1, "Gets biblionumber like the previous test." );
53
54 $context->set_preference( 'ThingISBN', 1 );
55 $context->set_preference( 'XISBN', 0 );
56 my $results_thingisbn = C4::XISBN::get_xisbns($isbn1);
57 is( $results_thingisbn->[0]->{biblionumber},
58     $biblionumber2,
59     "Gets correct biblionumber from a book with a similar isbn using ThingISBN." );
60
61 $context->set_preference( 'ThingISBN', 0 );
62 $context->set_preference( 'XISBN', 1 );
63 my $results_xisbn = C4::XISBN::get_xisbns($isbn1);
64 is( $results_xisbn->[0]->{biblionumber},
65     $biblionumber3,
66     "Gets correct biblionumber from a book with a similar isbn using XISBN." );
67
68 # clean up after ourselves
69 DelBiblio($biblionumber1);
70 DelBiblio($biblionumber2);
71 DelBiblio($biblionumber3);
72
73 # Util subs
74
75 # Add new biblio with isbn and return biblionumber
76 sub _add_biblio_with_isbn {
77     my $isbn = shift;
78
79     my $marc_record = MARC::Record->new;
80     my $field = MARC::Field->new( $isbn_tag, '', '', $isbn_subfield => $isbn );
81     $marc_record->append_fields($field);
82     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
83     return $biblionumber;
84 }
85
86 # Mocked subs
87
88 # C4::Search::SimpleSearch
89 sub Mock_SimpleSearch {
90     my $query = shift;
91     my @results;
92
93     $query =~ s/-//g;
94     my $ret_biblionumber;
95     if ( $query =~ /$isbn1/ ) {
96         $ret_biblionumber = $biblionumber1;
97     }
98     elsif ( $query =~ /$isbn2/ ) {
99         $ret_biblionumber = $biblionumber2;
100     }
101     elsif ( $query =~ /$isbn3/ ) {
102         $ret_biblionumber = $biblionumber3;
103     }
104
105     my $record = MARC::Record->new;
106     $record->leader('     ngm a22     7a 4500');
107     my $biblionumber_field;
108     if ( $biblionumber_tag < 10 ) {
109         $biblionumber_field =
110           MARC::Field->new( $biblionumber_tag, $ret_biblionumber );
111     }
112     else {
113         $biblionumber_field = MARC::Field->new( $biblionumber_tag, '', '',
114             $biblionumber_subfield => $ret_biblionumber );
115     }
116     $record->append_fields($biblionumber_field);
117
118     push @results, $record->as_usmarc;
119     return ( undef, \@results, 1 );
120 }