Bug 12554: (regression test) C4::Biblio::GetMarcPrice does not handle NORMARC
[koha.git] / t / db_dependent / Biblio.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 4;
21 use Test::MockModule;
22
23 use MARC::Record;
24 use t::lib::Mocks qw( mock_preference );
25
26 BEGIN {
27     use_ok('C4::Biblio');
28 }
29
30 my $dbh = C4::Context->dbh;
31 # Start transaction
32 $dbh->{AutoCommit} = 0;
33 $dbh->{RaiseError} = 1;
34
35 # Mocking variables
36 my $context = new Test::MockModule('C4::Context');
37
38 mock_marcfromkohafield();
39
40 sub run_tests {
41
42     # Undef C4::Biblio::inverted_field_map to avoid problems introduced
43     # by caching in TransformMarcToKoha
44     undef $C4::Biblio::inverted_field_map;
45
46     my $marcflavour = shift;
47     t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
48
49     my $isbn = '0590353403';
50     my $title = 'Foundation';
51
52     # Generate a record with just the ISBN
53     my $marc_record = MARC::Record->new;
54     my $isbn_field  = create_isbn_field( $isbn, $marcflavour );
55     $marc_record->append_fields( $isbn_field );
56
57     # Add the record to the DB
58     my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
59     my $data = GetBiblioData( $biblionumber );
60     is( $data->{ isbn }, $isbn,
61         '(GetBiblioData) ISBN correctly retireved.');
62     is( $data->{ title }, undef,
63         '(GetBiblioData) Title field is empty in fresh biblio.');
64
65     # Add title
66     my $field = create_title_field( $title, $marcflavour );
67     $marc_record->append_fields( $field );
68     ModBiblio( $marc_record, $biblionumber ,'' );
69     $data = GetBiblioData( $biblionumber );
70     is( $data->{ title }, $title,
71         'ModBiblio correctly added the title field, and GetBiblioData.');
72     is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
73
74     my $itemdata = GetBiblioItemData( $biblioitemnumber );
75     is( $itemdata->{ title }, $title,
76         'First test of GetBiblioItemData to get same result of previous two GetBiblioData tests.');
77     is( $itemdata->{ isbn }, $isbn,
78         'Second test checking it returns the correct isbn.');
79
80     my $success = 0;
81     $field = MARC::Field->new(
82             655, ' ', ' ',
83             'a' => 'Auction catalogs',
84             '9' => '1'
85             );
86     eval {
87         $marc_record->append_fields($field);
88         $success = ModBiblio($marc_record,$biblionumber,'');
89     } or do {
90         diag($@);
91         $success = 0;
92     };
93     ok($success, "ModBiblio handles authority-linked 655");
94
95     eval {
96         $field->delete_subfields('a');
97         $marc_record->append_fields($field);
98         $success = ModBiblio($marc_record,$biblionumber,'');
99     } or do {
100         diag($@);
101         $success = 0;
102     };
103     ok($success, "ModBiblio handles 655 with authority link but no heading");
104
105     eval {
106         $field->delete_subfields('9');
107         $marc_record->append_fields($field);
108         $success = ModBiblio($marc_record,$biblionumber,'');
109     } or do {
110         diag($@);
111         $success = 0;
112     };
113     ok($success, "ModBiblio handles 655 with no subfields");
114
115     ## Testing GetMarcISSN
116     my $issns;
117     $issns = GetMarcISSN( $marc_record, $marcflavour );
118     is( $issns->[0], undef,
119         'GetMarcISSN handles records without the ISSN field (list is empty)' );
120     is( scalar @$issns, 0,
121         'GetMarcISSN handles records without the ISSN field (count is 0)' );
122     # Add an ISSN field
123     my $issn = '1234-1234';
124     $field = create_issn_field( $issn, $marcflavour );
125     $marc_record->append_fields($field);
126     $issns = GetMarcISSN( $marc_record, $marcflavour );
127     is( $issns->[0], $issn,
128         'GetMarcISSN handles records with a single ISSN field (first element is correct)' );
129     is( scalar @$issns, 1,
130         'GetMARCISSN handles records with a single ISSN field (count is 1)');
131     # Add multiple ISSN field
132     my @more_issns = qw/1111-1111 2222-2222 3333-3333/;
133     foreach (@more_issns) {
134         $field = create_issn_field( $_, $marcflavour );
135         $marc_record->append_fields($field);
136     }
137     $issns = GetMarcISSN( $marc_record, $marcflavour );
138     is( scalar @$issns, 4,
139         'GetMARCISSN handles records with multiple ISSN fields (count correct)');
140
141     ## Testing GetMarcControlnumber
142     my $controlnumber;
143     $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
144     is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
145
146     $field = MARC::Field->new( '001', '' );
147     $marc_record->append_fields($field);
148     $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
149     is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' );
150
151     $field = $marc_record->field('001');
152     $field->update('123456789X');
153     $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
154     is( $controlnumber, '123456789X', 'GetMarcControlnumber handles records with 001' );
155
156     ## Testing GetMarcISBN
157     my $record_for_isbn = MARC::Record->new();
158     my $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
159     is( scalar @$isbns, 0, '(GetMarcISBN) The record contains no ISBN');
160
161     # We add one ISBN
162     $isbn_field = create_isbn_field( $isbn, $marcflavour );
163     $record_for_isbn->append_fields( $isbn_field );
164     $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
165     is( scalar @$isbns, 1, '(GetMarcISBN) The record contains one ISBN');
166     is( $isbns->[0], $isbn, '(GetMarcISBN) The record contains our ISBN');
167
168     # We add 3 more ISBNs
169     $record_for_isbn = MARC::Record->new();
170     my @more_isbns = qw/1111111111 2222222222 3333333333 444444444/;
171     foreach (@more_isbns) {
172         $field = create_isbn_field( $_, $marcflavour );
173         $record_for_isbn->append_fields($field);
174     }
175     $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
176     is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
177     for my $i (0 .. $#more_isbns) {
178         is( $isbns->[$i], $more_isbns[$i],
179             "(GetMarcISBN) Corretly retrieves ISBN #". ($i + 1));
180     }
181     is( GetMarcPrice( $record_for_isbn, $marcflavour ), 100,
182         "GetMarcPrice returns the correct value");
183 }
184
185 sub mock_marcfromkohafield {
186
187     $context->mock('marcfromkohafield',
188         sub {
189             my ( $self ) = shift;
190
191             if ( C4::Context->preference('marcflavour') eq 'MARC21' ||
192                  C4::Context->preference('marcflavour') eq 'NORMARC' ) {
193
194                 return  {
195                 '' => {
196                     'biblio.title' => [ '245', 'a' ],
197                     'biblio.biblionumber' => [ '999', 'c' ],
198                     'biblioitems.isbn' => [ '020', 'a' ],
199                     'biblioitems.issn' => [ '022', 'a' ],
200                     'biblioitems.biblioitemnumber' => [ '999', 'd' ]
201                     }
202                 };
203             } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
204
205                 return {
206                 '' => {
207                     'biblio.title' => [ '200', 'a' ],
208                     'biblio.biblionumber' => [ '999', 'c' ],
209                     'biblioitems.isbn' => [ '010', 'a' ],
210                     'biblioitems.issn' => [ '011', 'a' ],
211                     'biblioitems.biblioitemnumber' => [ '090', 'a' ]
212                     }
213                 };
214             }
215         });
216 }
217
218 sub create_title_field {
219     my ( $title, $marcflavour ) = @_;
220
221     my $title_field = ( $marcflavour eq 'UNIMARC' ) ? '200' : '245';
222     my $field = MARC::Field->new( $title_field,'','','a' => $title);
223
224     return $field;
225 }
226
227 sub create_isbn_field {
228     my ( $isbn, $marcflavour ) = @_;
229
230     my $isbn_field = ( $marcflavour eq 'UNIMARC' ) ? '010' : '020';
231     my $field = MARC::Field->new( $isbn_field,'','','a' => $isbn);
232     # Add the price subfield
233     my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c' ;
234     $field->add_subfields( $price_subfield => '$100' );
235
236     return $field;
237 }
238
239 sub create_issn_field {
240     my ( $issn, $marcflavour ) = @_;
241
242     my $issn_field = ( $marcflavour eq 'UNIMARC' ) ? '011' : '022';
243     my $field = MARC::Field->new( $issn_field,'','','a' => $issn);
244
245     return $field;
246 }
247
248 subtest 'MARC21' => sub {
249     plan tests => 26;
250     run_tests('MARC21');
251     $dbh->rollback;
252 };
253
254 subtest 'UNIMARC' => sub {
255     plan tests => 26;
256     run_tests('UNIMARC');
257     $dbh->rollback;
258 };
259
260 subtest 'NORMARC' => sub {
261     plan tests => 26;
262     run_tests('NORMARC');
263     $dbh->rollback;
264 };
265
266
267 1;