Bug 18089 - All XSLT testing singleBranchMode = 0 fails to show even if install has...
[koha.git] / t / db_dependent / XISBN.t
blob2f6d63b5b8794e110e27c5e8ffc9500c1581b379
1 #!/usr/bin/perl
3 # This Koha test module is a stub!
4 # Add more tests here!!!
6 use Modern::Perl;
8 use Test::More tests => 5;
9 use MARC::Record;
10 use C4::Biblio;
11 use C4::XISBN;
12 use C4::Context;
13 use C4::Search;
14 use t::lib::Mocks;
15 use Test::MockModule;
17 BEGIN {
18 use_ok('C4::XISBN');
21 my $dbh = C4::Context->dbh;
22 $dbh->{RaiseError} = 1;
23 $dbh->{AutoCommit} = 0;
25 my $search_module = new Test::MockModule('C4::Search');
27 $search_module->mock('SimpleSearch', \&Mock_SimpleSearch );
29 my $context = C4::Context->new;
31 my ( $biblionumber_tag, $biblionumber_subfield ) =
32 GetMarcFromKohaField( 'biblio.biblionumber', '' );
33 my ( $isbn_tag, $isbn_subfield ) =
34 GetMarcFromKohaField( 'biblioitems.isbn', '' );
36 # Harry Potter and the Sorcerer's Stone, 1st American ed. 1997
37 my $isbn1 = '0590353403';
38 # ThingISBN match : Silent Wing, First Edition 1998
39 my $isbn2 = '0684843897';
40 # XISBN match : Harry Potter and the Sorcerer's Stone,
41 # 1. Scholastic mass market paperback printing1.
42 my $isbn3 = '043936213X';
44 my $biblionumber1 = _add_biblio_with_isbn($isbn1);
45 my $biblionumber2 = _add_biblio_with_isbn($isbn2);
46 my $biblionumber3 = _add_biblio_with_isbn($isbn3);
48 my $trial = C4::XISBN::get_biblionumber_from_isbn($isbn1);
49 is( $trial->[0]->{biblionumber},
50 $biblionumber1,
51 "It gets the correct biblionumber from the only isbn we have added." );
53 $trial = C4::XISBN::_get_biblio_from_xisbn($isbn1);
54 is( $trial->{biblionumber},
55 $biblionumber1, "Gets biblionumber like the previous test." );
57 ## Test ThingISBN
58 t::lib::Mocks::mock_preference( 'ThingISBN', 1 );
59 t::lib::Mocks::mock_preference( 'XISBN', 0 );
61 my $results_thingisbn;
62 eval { $results_thingisbn = C4::XISBN::get_xisbns($isbn1); };
63 SKIP: {
64 skip "Problem retrieving ThingISBN", 1
65 unless $@ eq '';
66 is( $results_thingisbn->[0]->{biblionumber},
67 $biblionumber3,
68 "Gets correct biblionumber from a book with a similar isbn using ThingISBN." );
71 ## Test XISBN
72 t::lib::Mocks::mock_preference( 'ThingISBN', 0 );
73 t::lib::Mocks::mock_preference( 'XISBN', 1 );
75 my $results_xisbn;
76 eval { $results_xisbn = C4::XISBN::get_xisbns($isbn1); };
77 SKIP: {
78 skip "Problem retrieving XISBN", 1
79 unless $@ eq '';
80 is( $results_xisbn->[0]->{biblionumber},
81 $biblionumber3,
82 "Gets correct biblionumber from a book with a similar isbn using XISBN." );
85 $dbh->rollback;
87 # Util subs
89 # Add new biblio with isbn and return biblionumber
90 sub _add_biblio_with_isbn {
91 my $isbn = shift;
93 my $marc_record = MARC::Record->new;
94 my $field = MARC::Field->new( $isbn_tag, '', '', $isbn_subfield => $isbn );
95 $marc_record->append_fields($field);
96 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
97 return $biblionumber;
100 # Mocked subs
102 # C4::Search::SimpleSearch
103 sub Mock_SimpleSearch {
104 my $query = shift;
105 my @results;
107 $query =~ s/-//g;
108 my $ret_biblionumber;
109 if ( $query =~ /$isbn1/ ) {
110 $ret_biblionumber = $biblionumber1;
112 elsif ( $query =~ /$isbn2/ ) {
113 $ret_biblionumber = $biblionumber2;
115 elsif ( $query =~ /$isbn3/ ) {
116 $ret_biblionumber = $biblionumber3;
119 my $record = MARC::Record->new;
120 $record->leader(' ngm a22 7a 4500');
121 my $biblionumber_field;
122 if ( $biblionumber_tag < 10 ) {
123 $biblionumber_field =
124 MARC::Field->new( $biblionumber_tag, $ret_biblionumber );
126 else {
127 $biblionumber_field = MARC::Field->new( $biblionumber_tag, '', '',
128 $biblionumber_subfield => $ret_biblionumber );
130 $record->append_fields($biblionumber_field);
132 my $indexing_mode = C4::Context->config('zebra_bib_index_mode') // 'dom';
133 push @results, ( $indexing_mode eq 'dom' )
134 ? $record->as_xml()
135 : $record->as_usmarc() ;
137 return ( undef, \@results, 1 );