Bug 14306: Show URL from MARC21 field 555$u under Title Notes/Descriptions
[koha-equinox.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     is( GetMarcPrice( $record_for_isbn, $marcflavour ), 100,
204         "GetMarcPrice returns the correct value");
205     my $newincbiblioitemnumber=$biblioitemnumber+1;
206     $dbh->do("UPDATE biblioitems SET biblioitemnumber = ? WHERE biblionumber = ?;", undef, $newincbiblioitemnumber, $biblionumber );
207     my $updatedrecord = GetMarcBiblio($biblionumber, 0);
208     my $frameworkcode = GetFrameworkCode($biblionumber);
209     my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
210     die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
211     my $biblioitemnumbertotest;
212     if ( $biblioitem_tag < 10 ) {
213         $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->data();
214     } else {
215         $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
216     }
217     is ($newincbiblioitemnumber, $biblioitemnumbertotest);
218
219     # test for GetMarcNotes
220     my $a1= GetMarcNotes( $marc_record, $marcflavour );
221     my $field2 = MARC::Field->new( $marcflavour eq 'UNIMARC'? 300: 555, 0, '', a=> 'Some text', u=> 'http://url-1.com', u=> 'nohttp://something_else' );
222     $marc_record->append_fields( $field2 );
223     my $a2= GetMarcNotes( $marc_record, $marcflavour );
224     my $last= @$a2? $a2->[@$a2-1]->{marcnote}: '';
225     is( @$a2 == @$a1 + 1 && (
226         ( $marcflavour eq 'UNIMARC' && $last eq $field2->as_string() ) ||
227         ( $marcflavour ne 'UNIMARC' && $last =~ /\<a href=/ )),
228         1, 'Test for GetMarcNotes' );
229 }
230
231 sub mock_marcfromkohafield {
232
233     $context->mock('marcfromkohafield',
234         sub {
235             my ( $self ) = shift;
236
237             if ( C4::Context->preference('marcflavour') eq 'MARC21' ||
238                  C4::Context->preference('marcflavour') eq 'NORMARC' ) {
239
240                 return  {
241                 '' => {
242                     'biblio.title' => [ '245', 'a' ],
243                     'biblio.biblionumber' => [ '999', 'c' ],
244                     'biblioitems.isbn' => [ '020', 'a' ],
245                     'biblioitems.issn' => [ '022', 'a' ],
246                     'biblioitems.biblioitemnumber' => [ '999', 'd' ]
247                     }
248                 };
249             } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
250
251                 return {
252                 '' => {
253                     'biblio.title' => [ '200', 'a' ],
254                     'biblio.biblionumber' => [ '999', 'c' ],
255                     'biblioitems.isbn' => [ '010', 'a' ],
256                     'biblioitems.issn' => [ '011', 'a' ],
257                     'biblioitems.biblioitemnumber' => [ '090', 'a' ]
258                     }
259                 };
260             }
261         });
262 }
263
264 sub create_title_field {
265     my ( $title, $marcflavour ) = @_;
266
267     my $title_field = ( $marcflavour eq 'UNIMARC' ) ? '200' : '245';
268     my $field = MARC::Field->new( $title_field,'','','a' => $title);
269
270     return $field;
271 }
272
273 sub create_isbn_field {
274     my ( $isbn, $marcflavour ) = @_;
275
276     my $isbn_field = ( $marcflavour eq 'UNIMARC' ) ? '010' : '020';
277     my $field = MARC::Field->new( $isbn_field,'','','a' => $isbn);
278     # Add the price subfield
279     my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c' ;
280     $field->add_subfields( $price_subfield => '$100' );
281
282     return $field;
283 }
284
285 sub create_issn_field {
286     my ( $issn, $marcflavour ) = @_;
287
288     my $issn_field = ( $marcflavour eq 'UNIMARC' ) ? '011' : '022';
289     my $field = MARC::Field->new( $issn_field,'','','a' => $issn);
290
291     return $field;
292 }
293
294 subtest 'MARC21' => sub {
295     plan tests => 29;
296     run_tests('MARC21');
297     $dbh->rollback;
298 };
299
300 subtest 'UNIMARC' => sub {
301     plan tests => 29;
302     run_tests('UNIMARC');
303     $dbh->rollback;
304 };
305
306 subtest 'NORMARC' => sub {
307     plan tests => 29;
308     run_tests('NORMARC');
309     $dbh->rollback;
310 };
311
312 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
313     plan tests => 23;
314
315     my @columns = qw(
316         tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
317         authorised_value authtypecode value_builder isurl hidden frameworkcode
318         seealso link defaultvalue maxlength
319     );
320
321     # biblio.biblionumber must be mapped so this should return something
322     my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber', '');
323
324     ok(defined $marc_subfield_structure, "There is a result");
325     is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
326     foreach my $col (@columns) {
327         ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
328     }
329     is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
330     like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
331
332     # foo.bar does not exist so this should return undef
333     $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar', '');
334     is($marc_subfield_structure, undef, "invalid kohafield returns undef");
335 };
336
337 subtest 'IsMarcStructureInternal' => sub {
338     plan tests => 6;
339     my $tagslib = GetMarcStructure();
340     my @internals;
341     for my $tag ( sort keys %$tagslib ) {
342         next unless $tag;
343         for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
344             push @internals, $subfield if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
345         }
346     }
347     @internals = uniq @internals;
348     is( scalar(@internals), 4, 'expect four internals');
349     is( grep( /^lib$/, @internals ), 1, 'check lib' );
350     is( grep( /^tab$/, @internals ), 1, 'check tab' );
351     is( grep( /^mandatory$/, @internals ), 1, 'check mandatory' );
352     is( grep( /^repeatable$/, @internals ), 1, 'check repeatable' );
353     is( grep( /^a$/, @internals ), 0, 'no subfield a' );
354 };
355
356 1;