Bug 10601: (follow-up) unit tests for new ->set() and ->get()
[koha.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 my $context = C4::Context->new;
25
26 my ( $biblionumber_tag, $biblionumber_subfield ) =
27   GetMarcFromKohaField( 'biblio.biblionumber', '' );
28 my ( $isbn_tag, $isbn_subfield ) =
29   GetMarcFromKohaField( 'biblioitems.isbn', '' );
30
31 # Harry Potter and the Sorcerer's Stone, 1st American ed. 1997
32 my $isbn1 = '0590353403';
33 # ThingISBN match : Silent Wing, First Edition 1998
34 my $isbn2 = '0684843897';
35 # XISBN match : Harry Potter and the Philosopher's Stone, Magic ed. 2000
36 my $isbn3 = '1551923963';
37
38 my $biblionumber1 = _add_biblio_with_isbn($isbn1);
39 my $biblionumber2 = _add_biblio_with_isbn($isbn2);
40 my $biblionumber3 = _add_biblio_with_isbn($isbn3);
41
42 my $trial = C4::XISBN::get_biblionumber_from_isbn($isbn1);
43 is( $trial->[0]->{biblionumber},
44     $biblionumber1,
45     "It gets the correct biblionumber from the only isbn we have added." );
46
47 $trial = C4::XISBN::_get_biblio_from_xisbn($isbn1);
48 is( $trial->{biblionumber},
49     $biblionumber1, "Gets biblionumber like the previous test." );
50
51 $context->set_preference( 'ThingISBN', 1 );
52 $context->set_preference( 'XISBN', 0 );
53 my $results_thingisbn = C4::XISBN::get_xisbns($isbn1);
54 is( $results_thingisbn->[0]->{biblionumber},
55     $biblionumber2,
56     "Gets correct biblionumber from a book with a similar isbn using ThingISBN." );
57
58 $context->set_preference( 'ThingISBN', 0 );
59 $context->set_preference( 'XISBN', 1 );
60 my $results_xisbn = C4::XISBN::get_xisbns($isbn1);
61 is( $results_xisbn->[0]->{biblionumber},
62     $biblionumber3,
63     "Gets correct biblionumber from a book with a similar isbn using XISBN." );
64
65 # clean up after ourselves
66 DelBiblio($biblionumber1);
67 DelBiblio($biblionumber2);
68 DelBiblio($biblionumber3);
69
70 # Util subs
71
72 # Add new biblio with isbn and return biblionumber
73 sub _add_biblio_with_isbn {
74     my $isbn = shift;
75
76     my $marc_record = MARC::Record->new;
77     my $field = MARC::Field->new( $isbn_tag, '', '', $isbn_subfield => $isbn );
78     $marc_record->append_fields($field);
79     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
80     return $biblionumber;
81 }
82
83 # Mocked subs
84
85 # C4::Search::SimpleSearch
86 sub Mock_SimpleSearch {
87     my $query = shift;
88     my @results;
89
90     $query =~ s/-//g;
91     my $ret_biblionumber;
92     if ( $query =~ /$isbn1/ ) {
93         $ret_biblionumber = $biblionumber1;
94     }
95     elsif ( $query =~ /$isbn2/ ) {
96         $ret_biblionumber = $biblionumber2;
97     }
98     elsif ( $query =~ /$isbn3/ ) {
99         $ret_biblionumber = $biblionumber3;
100     }
101
102     my $record = MARC::Record->new;
103     $record->leader('     ngm a22     7a 4500');
104     my $biblionumber_field;
105     if ( $biblionumber_tag < 10 ) {
106         $biblionumber_field =
107           MARC::Field->new( $biblionumber_tag, $ret_biblionumber );
108     }
109     else {
110         $biblionumber_field = MARC::Field->new( $biblionumber_tag, '', '',
111             $biblionumber_subfield => $ret_biblionumber );
112     }
113     $record->append_fields($biblionumber_field);
114
115     push @results, $record->as_usmarc;
116     return ( undef, \@results, 1 );
117 }