Bug 11439: (follow up) add missing rollback call
[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 $dbh->rollback;
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 }