Bug 18554: Adjust a few typos including responsability
[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 => 7;
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 use Koha::Database;
28
29 BEGIN {
30     use_ok('C4::Biblio');
31 }
32
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
35 my $dbh = C4::Context->dbh;
36
37 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
38     plan tests => 23;
39
40     my @columns = qw(
41         tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
42         authorised_value authtypecode value_builder isurl hidden frameworkcode
43         seealso link defaultvalue maxlength
44     );
45
46     # biblio.biblionumber must be mapped so this should return something
47     my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber', '');
48
49     ok(defined $marc_subfield_structure, "There is a result");
50     is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
51     foreach my $col (@columns) {
52         ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
53     }
54     is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
55     like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
56
57     # foo.bar does not exist so this should return undef
58     $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar', '');
59     is($marc_subfield_structure, undef, "invalid kohafield returns undef");
60 };
61
62
63 # Mocking variables
64 my $biblio_module = new Test::MockModule('C4::Biblio');
65 $biblio_module->mock(
66     'GetMarcSubfieldStructure',
67     sub {
68         my ($self) = shift;
69
70         my ( $title_field,            $title_subfield )            = get_title_field();
71         my ( $isbn_field,             $isbn_subfield )             = get_isbn_field();
72         my ( $issn_field,             $issn_subfield )             = get_issn_field();
73         my ( $biblionumber_field,     $biblionumber_subfield )     = ( '999', 'c' );
74         my ( $biblioitemnumber_field, $biblioitemnumber_subfield ) = ( '999', '9' );
75         my ( $itemnumber_field,       $itemnumber_subfield )       = get_itemnumber_field();
76
77         return {
78             'biblio.title'                 => { tagfield => $title_field,            tagsubfield => $title_subfield },
79             'biblio.biblionumber'          => { tagfield => $biblionumber_field,     tagsubfield => $biblionumber_subfield },
80             'biblioitems.isbn'             => { tagfield => $isbn_field,             tagsubfield => $isbn_subfield },
81             'biblioitems.issn'             => { tagfield => $issn_field,             tagsubfield => $issn_subfield },
82             'biblioitems.biblioitemnumber' => { tagfield => $biblioitemnumber_field, tagsubfield => $biblioitemnumber_subfield },
83             'items.itemnumber'             => { tagfield => $itemnumber_subfield,    tagsubfield => $itemnumber_subfield },
84         };
85       }
86 );
87
88 my $currency = new Test::MockModule('Koha::Acquisition::Currencies');
89 $currency->mock(
90     'get_active',
91     sub {
92         return Koha::Acquisition::Currency->new(
93             {   symbol   => '$',
94                 isocode  => 'USD',
95                 currency => 'USD',
96                 active   => 1,
97             }
98         );
99     }
100 );
101
102 sub run_tests {
103
104     my $marcflavour = shift;
105     t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
106
107     my $isbn = '0590353403';
108     my $title = 'Foundation';
109
110     # Generate a record with just the ISBN
111     my $marc_record = MARC::Record->new;
112     $marc_record->append_fields( create_isbn_field( $isbn, $marcflavour ) );
113
114     # Add the record to the DB
115     my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
116     my $data = GetBiblioData( $biblionumber );
117     is( $data->{ isbn }, $isbn,
118         '(GetBiblioData) ISBN correctly retireved.');
119     is( $data->{ title }, undef,
120         '(GetBiblioData) Title field is empty in fresh biblio.');
121
122     my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
123     my $marc = GetMarcBiblio( $biblionumber );
124     is( $marc->subfield( $isbn_field, $isbn_subfield ), $isbn, );
125
126     # Add title
127     my $field = create_title_field( $title, $marcflavour );
128     $marc_record->append_fields( $field );
129     ModBiblio( $marc_record, $biblionumber ,'' );
130     $data = GetBiblioData( $biblionumber );
131     is( $data->{ title }, $title,
132         'ModBiblio correctly added the title field, and GetBiblioData.');
133     is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
134     $marc = GetMarcBiblio( $biblionumber );
135     my ( $title_field, $title_subfield ) = get_title_field();
136     is( $marc->subfield( $title_field, $title_subfield ), $title, );
137
138     my $itemdata = GetBiblioItemData( $biblioitemnumber );
139     is( $itemdata->{ title }, $title,
140         'First test of GetBiblioItemData to get same result of previous two GetBiblioData tests.');
141     is( $itemdata->{ isbn }, $isbn,
142         'Second test checking it returns the correct isbn.');
143
144     my $success = 0;
145     $field = MARC::Field->new(
146             655, ' ', ' ',
147             'a' => 'Auction catalogs',
148             '9' => '1'
149             );
150     eval {
151         $marc_record->append_fields($field);
152         $success = ModBiblio($marc_record,$biblionumber,'');
153     } or do {
154         diag($@);
155         $success = 0;
156     };
157     ok($success, "ModBiblio handles authority-linked 655");
158
159     eval {
160         $field->delete_subfields('a');
161         $marc_record->append_fields($field);
162         $success = ModBiblio($marc_record,$biblionumber,'');
163     } or do {
164         diag($@);
165         $success = 0;
166     };
167     ok($success, "ModBiblio handles 655 with authority link but no heading");
168
169     eval {
170         $field->delete_subfields('9');
171         $marc_record->append_fields($field);
172         $success = ModBiblio($marc_record,$biblionumber,'');
173     } or do {
174         diag($@);
175         $success = 0;
176     };
177     ok($success, "ModBiblio handles 655 with no subfields");
178
179     ## Testing GetMarcISSN
180     my $issns;
181     $issns = GetMarcISSN( $marc_record, $marcflavour );
182     is( $issns->[0], undef,
183         'GetMarcISSN handles records without the ISSN field (list is empty)' );
184     is( scalar @$issns, 0,
185         'GetMarcISSN handles records without the ISSN field (count is 0)' );
186     # Add an ISSN field
187     my $issn = '1234-1234';
188     $field = create_issn_field( $issn, $marcflavour );
189     $marc_record->append_fields($field);
190     $issns = GetMarcISSN( $marc_record, $marcflavour );
191     is( $issns->[0], $issn,
192         'GetMarcISSN handles records with a single ISSN field (first element is correct)' );
193     is( scalar @$issns, 1,
194         'GetMARCISSN handles records with a single ISSN field (count is 1)');
195     # Add multiple ISSN field
196     my @more_issns = qw/1111-1111 2222-2222 3333-3333/;
197     foreach (@more_issns) {
198         $field = create_issn_field( $_, $marcflavour );
199         $marc_record->append_fields($field);
200     }
201     $issns = GetMarcISSN( $marc_record, $marcflavour );
202     is( scalar @$issns, 4,
203         'GetMARCISSN handles records with multiple ISSN fields (count correct)');
204     # Create an empty ISSN
205     $field = create_issn_field( "", $marcflavour );
206     $marc_record->append_fields($field);
207     $issns = GetMarcISSN( $marc_record, $marcflavour );
208     is( scalar @$issns, 4,
209         'GetMARCISSN skips empty ISSN fields (Bug 12674)');
210
211     ## Testing GetMarcControlnumber
212     my $controlnumber;
213     $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
214     is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
215
216     $field = MARC::Field->new( '001', '' );
217     $marc_record->append_fields($field);
218     $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
219     is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' );
220
221     $field = $marc_record->field('001');
222     $field->update('123456789X');
223     $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
224     is( $controlnumber, '123456789X', 'GetMarcControlnumber handles records with 001' );
225
226     ## Testing GetMarcISBN
227     my $record_for_isbn = MARC::Record->new();
228     my $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
229     is( scalar @$isbns, 0, '(GetMarcISBN) The record contains no ISBN');
230
231     # We add one ISBN
232     $isbn_field = create_isbn_field( $isbn, $marcflavour );
233     $record_for_isbn->append_fields( $isbn_field );
234     $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
235     is( scalar @$isbns, 1, '(GetMarcISBN) The record contains one ISBN');
236     is( $isbns->[0], $isbn, '(GetMarcISBN) The record contains our ISBN');
237
238     # We add 3 more ISBNs
239     $record_for_isbn = MARC::Record->new();
240     my @more_isbns = qw/1111111111 2222222222 3333333333 444444444/;
241     foreach (@more_isbns) {
242         $field = create_isbn_field( $_, $marcflavour );
243         $record_for_isbn->append_fields($field);
244     }
245     $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
246     is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
247     for my $i (0 .. $#more_isbns) {
248         is( $isbns->[$i], $more_isbns[$i],
249             "(GetMarcISBN) Correctly retrieves ISBN #". ($i + 1));
250     }
251
252     is( GetMarcPrice( $record_for_isbn, $marcflavour ), 100,
253         "GetMarcPrice returns the correct value");
254     my $newincbiblioitemnumber=$biblioitemnumber+1;
255     $dbh->do("UPDATE biblioitems SET biblioitemnumber = ? WHERE biblionumber = ?;", undef, $newincbiblioitemnumber, $biblionumber );
256     my $updatedrecord = GetMarcBiblio($biblionumber, 0);
257     my $frameworkcode = GetFrameworkCode($biblionumber);
258     my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
259     die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
260     my $biblioitemnumbertotest;
261     if ( $biblioitem_tag < 10 ) {
262         $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->data();
263     } else {
264         $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
265     }
266     is ($newincbiblioitemnumber, $biblioitemnumbertotest, 'Check newincbiblioitemnumber');
267
268     # test for GetMarcNotes
269     my $a1= GetMarcNotes( $marc_record, $marcflavour );
270     my $field2 = MARC::Field->new( $marcflavour eq 'UNIMARC'? 300: 555, 0, '', a=> 'Some text', u=> 'http://url-1.com', u=> 'nohttp://something_else' );
271     $marc_record->append_fields( $field2 );
272     my $a2= GetMarcNotes( $marc_record, $marcflavour );
273     is( ( $marcflavour eq 'UNIMARC' && @$a2 == @$a1 + 1 ) ||
274         ( $marcflavour ne 'UNIMARC' && @$a2 == @$a1 + 3 ), 1,
275         'Check the number of returned notes of GetMarcNotes' );
276
277     # test for GetMarcUrls
278     $marc_record->append_fields(
279         MARC::Field->new( '856', '', '', u => ' https://koha-community.org ' ),
280         MARC::Field->new( '856', '', '', u => 'koha-community.org' ),
281     );
282     my $marcurl = GetMarcUrls( $marc_record, $marcflavour );
283     is( @$marcurl, 2, 'GetMarcUrls returns two URLs' );
284     like( $marcurl->[0]->{MARCURL}, qr/^https/, 'GetMarcUrls did not stumble over a preceding space' );
285     ok( $marcflavour ne 'MARC21' || $marcurl->[1]->{MARCURL} =~ /^http:\/\//,
286         'GetMarcUrls prefixed a MARC21 URL with http://' );
287 }
288
289 sub get_title_field {
290     my $marc_flavour = C4::Context->preference('marcflavour');
291     return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'a' ) : ( '245', 'a' );
292 }
293
294 sub get_isbn_field {
295     my $marc_flavour = C4::Context->preference('marcflavour');
296     return ( $marc_flavour eq 'UNIMARC' ) ? ( '010', 'a' ) : ( '020', 'a' );
297 }
298
299 sub get_issn_field {
300     my $marc_flavour = C4::Context->preference('marcflavour');
301     return ( $marc_flavour eq 'UNIMARC' ) ? ( '011', 'a' ) : ( '022', 'a' );
302 }
303
304 sub get_itemnumber_field {
305     my $marc_flavour = C4::Context->preference('marcflavour');
306     return ( $marc_flavour eq 'UNIMARC' ) ? ( '995', '9' ) : ( '952', '9' );
307 }
308
309 sub create_title_field {
310     my ( $title, $marcflavour ) = @_;
311
312     my ( $title_field, $title_subfield ) = get_title_field();
313     my $field = MARC::Field->new( $title_field, '', '', $title_subfield => $title );
314
315     return $field;
316 }
317
318 sub create_isbn_field {
319     my ( $isbn, $marcflavour ) = @_;
320
321     my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
322     my $field = MARC::Field->new( $isbn_field, '', '', $isbn_subfield => $isbn );
323
324     # Add the price subfield
325     my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c';
326     $field->add_subfields( $price_subfield => '$100' );
327
328     return $field;
329 }
330
331 sub create_issn_field {
332     my ( $issn, $marcflavour ) = @_;
333
334     my ( $issn_field, $issn_subfield ) = get_issn_field();
335     my $field = MARC::Field->new( $issn_field, '', '', $issn_subfield => $issn );
336
337     return $field;
338 }
339
340 subtest 'MARC21' => sub {
341     plan tests => 34;
342     run_tests('MARC21');
343     $schema->storage->txn_rollback;
344     $schema->storage->txn_begin;
345 };
346
347 subtest 'UNIMARC' => sub {
348     plan tests => 34;
349     run_tests('UNIMARC');
350     $schema->storage->txn_rollback;
351     $schema->storage->txn_begin;
352 };
353
354 subtest 'NORMARC' => sub {
355     plan tests => 34;
356     run_tests('NORMARC');
357     $schema->storage->txn_rollback;
358     $schema->storage->txn_begin;
359 };
360
361 subtest 'IsMarcStructureInternal' => sub {
362     plan tests => 6;
363     my $tagslib = GetMarcStructure();
364     my @internals;
365     for my $tag ( sort keys %$tagslib ) {
366         next unless $tag;
367         for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
368             push @internals, $subfield if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
369         }
370     }
371     @internals = uniq @internals;
372     is( scalar(@internals), 4, 'expect four internals');
373     is( grep( /^lib$/, @internals ), 1, 'check lib' );
374     is( grep( /^tab$/, @internals ), 1, 'check tab' );
375     is( grep( /^mandatory$/, @internals ), 1, 'check mandatory' );
376     is( grep( /^repeatable$/, @internals ), 1, 'check repeatable' );
377     is( grep( /^a$/, @internals ), 0, 'no subfield a' );
378 };
379
380 subtest 'deletedbiblio_metadata' => sub {
381     plan tests => 2;
382
383     my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
384     my $biblio_metadata = C4::Biblio::GetXmlBiblio( $biblionumber );
385     C4::Biblio::DelBiblio( $biblionumber );
386     my ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio WHERE biblionumber=?|, undef, $biblionumber);
387     is( $moved, $biblionumber, 'Found in deletedbiblio' );
388     ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio_metadata WHERE biblionumber=?|, undef, $biblionumber);
389     is( $moved, $biblionumber, 'Found in deletedbiblio_metadata' );
390 };
391
392 1;