Bug 11439: UT: Improve XISBN.t
[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::More tests => 5;
9 use MARC::Record;
10 use C4::Biblio;
11 use C4::XISBN;
12 use C4::Context;
13 use Test::MockModule;
14
15 BEGIN {
16     use_ok('C4::XISBN');
17 }
18
19 my $dbh = C4::Context->dbh;
20 $dbh->{RaiseError} = 1;
21 $dbh->{AutoCommit} = 0;
22
23 my $search_module = new Test::MockModule('C4::Search');
24
25 $search_module->mock('SimpleSearch', \&Mock_SimpleSearch );
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     $biblionumber3,
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 # Util subs
69
70 # Add new biblio with isbn and return biblionumber
71 sub _add_biblio_with_isbn {
72     my $isbn = shift;
73
74     my $marc_record = MARC::Record->new;
75     my $field = MARC::Field->new( $isbn_tag, '', '', $isbn_subfield => $isbn );
76     $marc_record->append_fields($field);
77     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
78     return $biblionumber;
79 }
80
81 # Mocked subs
82
83 # C4::Search::SimpleSearch
84 sub Mock_SimpleSearch {
85     my $query = shift;
86     my @results;
87
88     $query =~ s/-//g;
89     my $ret_biblionumber;
90     if ( $query =~ /$isbn1/ ) {
91         $ret_biblionumber = $biblionumber1;
92     }
93     elsif ( $query =~ /$isbn2/ ) {
94         $ret_biblionumber = $biblionumber2;
95     }
96     elsif ( $query =~ /$isbn3/ ) {
97         $ret_biblionumber = $biblionumber3;
98     }
99
100     my $record = MARC::Record->new;
101     $record->leader('     ngm a22     7a 4500');
102     my $biblionumber_field;
103     if ( $biblionumber_tag < 10 ) {
104         $biblionumber_field =
105           MARC::Field->new( $biblionumber_tag, $ret_biblionumber );
106     }
107     else {
108         $biblionumber_field = MARC::Field->new( $biblionumber_tag, '', '',
109             $biblionumber_subfield => $ret_biblionumber );
110     }
111     $record->append_fields($biblionumber_field);
112
113     push @results, $record->as_usmarc;
114     return ( undef, \@results, 1 );
115 }