Bug 9165: (Followup) Clear existing sync
[koha.git] / t / db_dependent / XISBN.t
blob9486274e78159555fd51c7f9d1ad95d90a470348
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 Test::MockModule;
16 BEGIN {
17 use_ok('C4::XISBN');
20 my $dbh = C4::Context->dbh;
21 $dbh->{RaiseError} = 1;
22 $dbh->{AutoCommit} = 0;
24 my $search_module = new Test::MockModule('C4::Search');
26 $search_module->mock('SimpleSearch', \&Mock_SimpleSearch );
28 my $context = C4::Context->new;
30 my ( $biblionumber_tag, $biblionumber_subfield ) =
31 GetMarcFromKohaField( 'biblio.biblionumber', '' );
32 my ( $isbn_tag, $isbn_subfield ) =
33 GetMarcFromKohaField( 'biblioitems.isbn', '' );
35 # Harry Potter and the Sorcerer's Stone, 1st American ed. 1997
36 my $isbn1 = '0590353403';
37 # ThingISBN match : Silent Wing, First Edition 1998
38 my $isbn2 = '0684843897';
39 # XISBN match : Harry Potter and the Sorcerer's Stone,
40 # 1. Scholastic mass market paperback printing1.
41 my $isbn3 = '043936213X';
43 my $biblionumber1 = _add_biblio_with_isbn($isbn1);
44 my $biblionumber2 = _add_biblio_with_isbn($isbn2);
45 my $biblionumber3 = _add_biblio_with_isbn($isbn3);
47 my $trial = C4::XISBN::get_biblionumber_from_isbn($isbn1);
48 is( $trial->[0]->{biblionumber},
49 $biblionumber1,
50 "It gets the correct biblionumber from the only isbn we have added." );
52 $trial = C4::XISBN::_get_biblio_from_xisbn($isbn1);
53 is( $trial->{biblionumber},
54 $biblionumber1, "Gets biblionumber like the previous test." );
56 ## Test ThingISBN
57 $context->set_preference( 'ThingISBN', 1 );
58 $context->set_preference( 'XISBN', 0 );
60 my $results_thingisbn;
61 eval { $results_thingisbn = C4::XISBN::get_xisbns($isbn1); };
62 SKIP: {
63 skip "Problem retrieving ThingISBN", 1
64 unless $@ eq '';
65 is( $results_thingisbn->[0]->{biblionumber},
66 $biblionumber3,
67 "Gets correct biblionumber from a book with a similar isbn using ThingISBN." );
70 ## Test XISBN
71 $context->set_preference( 'ThingISBN', 0 );
72 $context->set_preference( 'XISBN', 1 );
74 my $results_xisbn;
75 eval { $results_xisbn = C4::XISBN::get_xisbns($isbn1); };
76 SKIP: {
77 skip "Problem retrieving XISBN", 1
78 unless $@ eq '';
79 is( $results_xisbn->[0]->{biblionumber},
80 $biblionumber3,
81 "Gets correct biblionumber from a book with a similar isbn using XISBN." );
84 $dbh->rollback;
86 # Util subs
88 # Add new biblio with isbn and return biblionumber
89 sub _add_biblio_with_isbn {
90 my $isbn = shift;
92 my $marc_record = MARC::Record->new;
93 my $field = MARC::Field->new( $isbn_tag, '', '', $isbn_subfield => $isbn );
94 $marc_record->append_fields($field);
95 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
96 return $biblionumber;
99 # Mocked subs
101 # C4::Search::SimpleSearch
102 sub Mock_SimpleSearch {
103 my $query = shift;
104 my @results;
106 $query =~ s/-//g;
107 my $ret_biblionumber;
108 if ( $query =~ /$isbn1/ ) {
109 $ret_biblionumber = $biblionumber1;
111 elsif ( $query =~ /$isbn2/ ) {
112 $ret_biblionumber = $biblionumber2;
114 elsif ( $query =~ /$isbn3/ ) {
115 $ret_biblionumber = $biblionumber3;
118 my $record = MARC::Record->new;
119 $record->leader(' ngm a22 7a 4500');
120 my $biblionumber_field;
121 if ( $biblionumber_tag < 10 ) {
122 $biblionumber_field =
123 MARC::Field->new( $biblionumber_tag, $ret_biblionumber );
125 else {
126 $biblionumber_field = MARC::Field->new( $biblionumber_tag, '', '',
127 $biblionumber_subfield => $ret_biblionumber );
129 $record->append_fields($biblionumber_field);
131 my $indexing_mode = C4::Context->config('zebra_bib_index_mode') // 'dom';
132 push @results, ( $indexing_mode eq 'dom' )
133 ? $record->as_xml()
134 : $record->as_usmarc() ;
136 return ( undef, \@results, 1 );