Bug 5404: [QA Follow-up] Add test descriptions
[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 => 6;
21 use Test::MockModule;
22
23 use List::MoreUtils qw( uniq );
24 use MARC::Record;
25 use t::lib::Mocks qw( mock_preference );
26
27 BEGIN {
28     use_ok('C4::Biblio');
29 }
30
31 my $dbh = C4::Context->dbh;
32 # Start transaction
33 $dbh->{AutoCommit} = 0;
34 $dbh->{RaiseError} = 1;
35
36 # Mocking variables
37 my $context = new Test::MockModule('C4::Context');
38
39 mock_marcfromkohafield();
40
41 my $currency = new Test::MockModule('Koha::Acquisition::Currencies');
42 $currency->mock(
43     'get_active',
44     sub {
45         return Koha::Acquisition::Currency->new(
46             {   symbol   => '$',
47                 isocode  => 'USD',
48                 currency => 'USD',
49                 active   => 1,
50             }
51         );
52     }
53 );
54
55 sub run_tests {
56
57     # Undef C4::Biblio::inverted_field_map to avoid problems introduced
58     # by caching in TransformMarcToKoha
59     undef $C4::Biblio::inverted_field_map;
60
61     my $marcflavour = shift;
62     t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
63
64     my $isbn = '0590353403';
65     my $title = 'Foundation';
66
67     # Generate a record with just the ISBN
68     my $marc_record = MARC::Record->new;
69     my $isbn_field  = create_isbn_field( $isbn, $marcflavour );
70     $marc_record->append_fields( $isbn_field );
71
72     # Add the record to the DB
73     my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
74     my $data = GetBiblioData( $biblionumber );
75     is( $data->{ isbn }, $isbn,
76         '(GetBiblioData) ISBN correctly retireved.');
77     is( $data->{ title }, undef,
78         '(GetBiblioData) Title field is empty in fresh biblio.');
79
80     # Add title
81     my $field = create_title_field( $title, $marcflavour );
82     $marc_record->append_fields( $field );
83     ModBiblio( $marc_record, $biblionumber ,'' );
84     $data = GetBiblioData( $biblionumber );
85     is( $data->{ title }, $title,
86         'ModBiblio correctly added the title field, and GetBiblioData.');
87     is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
88
89     my $itemdata = GetBiblioItemData( $biblioitemnumber );
90     is( $itemdata->{ title }, $title,
91         'First test of GetBiblioItemData to get same result of previous two GetBiblioData tests.');
92     is( $itemdata->{ isbn }, $isbn,
93         'Second test checking it returns the correct isbn.');
94
95     my $success = 0;
96     $field = MARC::Field->new(
97             655, ' ', ' ',
98             'a' => 'Auction catalogs',
99             '9' => '1'
100             );
101     eval {
102         $marc_record->append_fields($field);
103         $success = ModBiblio($marc_record,$biblionumber,'');
104     } or do {
105         diag($@);
106         $success = 0;
107     };
108     ok($success, "ModBiblio handles authority-linked 655");
109
110     eval {
111         $field->delete_subfields('a');
112         $marc_record->append_fields($field);
113         $success = ModBiblio($marc_record,$biblionumber,'');
114     } or do {
115         diag($@);
116         $success = 0;
117     };
118     ok($success, "ModBiblio handles 655 with authority link but no heading");
119
120     eval {
121         $field->delete_subfields('9');
122         $marc_record->append_fields($field);
123         $success = ModBiblio($marc_record,$biblionumber,'');
124     } or do {
125         diag($@);
126         $success = 0;
127     };
128     ok($success, "ModBiblio handles 655 with no subfields");
129
130     ## Testing GetMarcISSN
131     my $issns;
132     $issns = GetMarcISSN( $marc_record, $marcflavour );
133     is( $issns->[0], undef,
134         'GetMarcISSN handles records without the ISSN field (list is empty)' );
135     is( scalar @$issns, 0,
136         'GetMarcISSN handles records without the ISSN field (count is 0)' );
137     # Add an ISSN field
138     my $issn = '1234-1234';
139     $field = create_issn_field( $issn, $marcflavour );
140     $marc_record->append_fields($field);
141     $issns = GetMarcISSN( $marc_record, $marcflavour );
142     is( $issns->[0], $issn,
143         'GetMarcISSN handles records with a single ISSN field (first element is correct)' );
144     is( scalar @$issns, 1,
145         'GetMARCISSN handles records with a single ISSN field (count is 1)');
146     # Add multiple ISSN field
147     my @more_issns = qw/1111-1111 2222-2222 3333-3333/;
148     foreach (@more_issns) {
149         $field = create_issn_field( $_, $marcflavour );
150         $marc_record->append_fields($field);
151     }
152     $issns = GetMarcISSN( $marc_record, $marcflavour );
153     is( scalar @$issns, 4,
154         'GetMARCISSN handles records with multiple ISSN fields (count correct)');
155     # Create an empty ISSN
156     $field = create_issn_field( "", $marcflavour );
157     $marc_record->append_fields($field);
158     $issns = GetMarcISSN( $marc_record, $marcflavour );
159     is( scalar @$issns, 4,
160         'GetMARCISSN skips empty ISSN fields (Bug 12674)');
161
162     ## Testing GetMarcControlnumber
163     my $controlnumber;
164     $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
165     is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
166
167     $field = MARC::Field->new( '001', '' );
168     $marc_record->append_fields($field);
169     $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
170     is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' );
171
172     $field = $marc_record->field('001');
173     $field->update('123456789X');
174     $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
175     is( $controlnumber, '123456789X', 'GetMarcControlnumber handles records with 001' );
176
177     ## Testing GetMarcISBN
178     my $record_for_isbn = MARC::Record->new();
179     my $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
180     is( scalar @$isbns, 0, '(GetMarcISBN) The record contains no ISBN');
181
182     # We add one ISBN
183     $isbn_field = create_isbn_field( $isbn, $marcflavour );
184     $record_for_isbn->append_fields( $isbn_field );
185     $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
186     is( scalar @$isbns, 1, '(GetMarcISBN) The record contains one ISBN');
187     is( $isbns->[0], $isbn, '(GetMarcISBN) The record contains our ISBN');
188
189     # We add 3 more ISBNs
190     $record_for_isbn = MARC::Record->new();
191     my @more_isbns = qw/1111111111 2222222222 3333333333 444444444/;
192     foreach (@more_isbns) {
193         $field = create_isbn_field( $_, $marcflavour );
194         $record_for_isbn->append_fields($field);
195     }
196     $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
197     is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
198     for my $i (0 .. $#more_isbns) {
199         is( $isbns->[$i], $more_isbns[$i],
200             "(GetMarcISBN) Corretly retrieves ISBN #". ($i + 1));
201     }
202
203
204     is( GetMarcPrice( $record_for_isbn, $marcflavour ), 100,
205         "GetMarcPrice returns the correct value");
206     my $newincbiblioitemnumber=$biblioitemnumber+1;
207     $dbh->do("UPDATE biblioitems SET biblioitemnumber = ? WHERE biblionumber = ?;", undef, $newincbiblioitemnumber, $biblionumber );
208     my $updatedrecord = GetMarcBiblio($biblionumber, 0);
209     my $frameworkcode = GetFrameworkCode($biblionumber);
210     my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
211     die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
212     my $biblioitemnumbertotest;
213     if ( $biblioitem_tag < 10 ) {
214         $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->data();
215     } else {
216         $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
217     }
218     is ($newincbiblioitemnumber, $biblioitemnumbertotest);
219 }
220
221 sub mock_marcfromkohafield {
222
223     $context->mock('marcfromkohafield',
224         sub {
225             my ( $self ) = shift;
226
227             if ( C4::Context->preference('marcflavour') eq 'MARC21' ||
228                  C4::Context->preference('marcflavour') eq 'NORMARC' ) {
229
230                 return  {
231                 '' => {
232                     'biblio.title' => [ '245', 'a' ],
233                     'biblio.biblionumber' => [ '999', 'c' ],
234                     'biblioitems.isbn' => [ '020', 'a' ],
235                     'biblioitems.issn' => [ '022', 'a' ],
236                     'biblioitems.biblioitemnumber' => [ '999', 'd' ]
237                     }
238                 };
239             } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
240
241                 return {
242                 '' => {
243                     'biblio.title' => [ '200', 'a' ],
244                     'biblio.biblionumber' => [ '999', 'c' ],
245                     'biblioitems.isbn' => [ '010', 'a' ],
246                     'biblioitems.issn' => [ '011', 'a' ],
247                     'biblioitems.biblioitemnumber' => [ '090', 'a' ]
248                     }
249                 };
250             }
251         });
252 }
253
254 sub create_title_field {
255     my ( $title, $marcflavour ) = @_;
256
257     my $title_field = ( $marcflavour eq 'UNIMARC' ) ? '200' : '245';
258     my $field = MARC::Field->new( $title_field,'','','a' => $title);
259
260     return $field;
261 }
262
263 sub create_isbn_field {
264     my ( $isbn, $marcflavour ) = @_;
265
266     my $isbn_field = ( $marcflavour eq 'UNIMARC' ) ? '010' : '020';
267     my $field = MARC::Field->new( $isbn_field,'','','a' => $isbn);
268     # Add the price subfield
269     my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c' ;
270     $field->add_subfields( $price_subfield => '$100' );
271
272     return $field;
273 }
274
275 sub create_issn_field {
276     my ( $issn, $marcflavour ) = @_;
277
278     my $issn_field = ( $marcflavour eq 'UNIMARC' ) ? '011' : '022';
279     my $field = MARC::Field->new( $issn_field,'','','a' => $issn);
280
281     return $field;
282 }
283
284 subtest 'MARC21' => sub {
285     plan tests => 28;
286     run_tests('MARC21');
287     $dbh->rollback;
288 };
289
290 subtest 'UNIMARC' => sub {
291     plan tests => 28;
292     run_tests('UNIMARC');
293     $dbh->rollback;
294 };
295
296 subtest 'NORMARC' => sub {
297     plan tests => 28;
298     run_tests('NORMARC');
299     $dbh->rollback;
300 };
301
302 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
303     plan tests => 23;
304
305     my @columns = qw(
306         tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
307         authorised_value authtypecode value_builder isurl hidden frameworkcode
308         seealso link defaultvalue maxlength
309     );
310
311     # biblio.biblionumber must be mapped so this should return something
312     my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber', '');
313
314     ok(defined $marc_subfield_structure, "There is a result");
315     is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
316     foreach my $col (@columns) {
317         ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
318     }
319     is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
320     like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
321
322     # foo.bar does not exist so this should return undef
323     $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar', '');
324     is($marc_subfield_structure, undef, "invalid kohafield returns undef");
325 };
326
327 subtest 'IsMarcStructureInternal' => sub {
328     plan tests => 6;
329     my $tagslib = GetMarcStructure();
330     my @internals;
331     for my $tag ( sort keys %$tagslib ) {
332         next unless $tag;
333         for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
334             push @internals, $subfield if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
335         }
336     }
337     @internals = uniq @internals;
338     is( scalar(@internals), 4, 'expect four internals');
339     is( grep( /^lib$/, @internals ), 1, 'check lib' );
340     is( grep( /^tab$/, @internals ), 1, 'check tab' );
341     is( grep( /^mandatory$/, @internals ), 1, 'check mandatory' );
342     is( grep( /^repeatable$/, @internals ), 1, 'check repeatable' );
343     is( grep( /^a$/, @internals ), 0, 'no subfield a' );
344 };
345
346 1;