Bug 9032: (follow-up) add support for bootstrap theme
[koha.git] / t / db_dependent / XISBN.t
blob34f353a4d78e4e46020712c146ad57d0284cb317
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 Test::MockModule;
15 BEGIN {
16 use_ok('C4::XISBN');
19 my $dbh = C4::Context->dbh;
20 $dbh->{RaiseError} = 1;
21 $dbh->{AutoCommit} = 0;
23 my $search_module = new Test::MockModule('C4::Search');
25 $search_module->mock('SimpleSearch', \&Mock_SimpleSearch );
27 my $context = C4::Context->new;
29 my ( $biblionumber_tag, $biblionumber_subfield ) =
30 GetMarcFromKohaField( 'biblio.biblionumber', '' );
31 my ( $isbn_tag, $isbn_subfield ) =
32 GetMarcFromKohaField( 'biblioitems.isbn', '' );
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 Sorcerer's Stone,
39 # 1. Scholastic mass market paperback printing1.
40 my $isbn3 = '043936213X';
42 my $biblionumber1 = _add_biblio_with_isbn($isbn1);
43 my $biblionumber2 = _add_biblio_with_isbn($isbn2);
44 my $biblionumber3 = _add_biblio_with_isbn($isbn3);
46 my $trial = C4::XISBN::get_biblionumber_from_isbn($isbn1);
47 is( $trial->[0]->{biblionumber},
48 $biblionumber1,
49 "It gets the correct biblionumber from the only isbn we have added." );
51 $trial = C4::XISBN::_get_biblio_from_xisbn($isbn1);
52 is( $trial->{biblionumber},
53 $biblionumber1, "Gets biblionumber like the previous test." );
55 $context->set_preference( 'ThingISBN', 1 );
56 $context->set_preference( 'XISBN', 0 );
57 my $results_thingisbn = C4::XISBN::get_xisbns($isbn1);
58 is( $results_thingisbn->[0]->{biblionumber},
59 $biblionumber3,
60 "Gets correct biblionumber from a book with a similar isbn using ThingISBN." );
62 $context->set_preference( 'ThingISBN', 0 );
63 $context->set_preference( 'XISBN', 1 );
64 my $results_xisbn = C4::XISBN::get_xisbns($isbn1);
65 is( $results_xisbn->[0]->{biblionumber},
66 $biblionumber3,
67 "Gets correct biblionumber from a book with a similar isbn using XISBN." );
69 $dbh->rollback;
71 # Util subs
73 # Add new biblio with isbn and return biblionumber
74 sub _add_biblio_with_isbn {
75 my $isbn = shift;
77 my $marc_record = MARC::Record->new;
78 my $field = MARC::Field->new( $isbn_tag, '', '', $isbn_subfield => $isbn );
79 $marc_record->append_fields($field);
80 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
81 return $biblionumber;
84 # Mocked subs
86 # C4::Search::SimpleSearch
87 sub Mock_SimpleSearch {
88 my $query = shift;
89 my @results;
91 $query =~ s/-//g;
92 my $ret_biblionumber;
93 if ( $query =~ /$isbn1/ ) {
94 $ret_biblionumber = $biblionumber1;
96 elsif ( $query =~ /$isbn2/ ) {
97 $ret_biblionumber = $biblionumber2;
99 elsif ( $query =~ /$isbn3/ ) {
100 $ret_biblionumber = $biblionumber3;
103 my $record = MARC::Record->new;
104 $record->leader(' ngm a22 7a 4500');
105 my $biblionumber_field;
106 if ( $biblionumber_tag < 10 ) {
107 $biblionumber_field =
108 MARC::Field->new( $biblionumber_tag, $ret_biblionumber );
110 else {
111 $biblionumber_field = MARC::Field->new( $biblionumber_tag, '', '',
112 $biblionumber_subfield => $ret_biblionumber );
114 $record->append_fields($biblionumber_field);
116 push @results, $record->as_usmarc;
117 return ( undef, \@results, 1 );