Bug 18811: DBRev 17.05.00.006
[koha.git] / t / db_dependent / Biblio.t
blob08903502636617bfbd0458f4dd80ac38aa8492e7
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Test::More tests => 7;
21 use Test::MockModule;
23 use List::MoreUtils qw( uniq );
24 use MARC::Record;
25 use t::lib::Mocks qw( mock_preference );
27 use Koha::Database;
29 BEGIN {
30 use_ok('C4::Biblio');
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
35 my $dbh = C4::Context->dbh;
37 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
38 plan tests => 23;
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
46 # biblio.biblionumber must be mapped so this should return something
47 my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber', '');
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'");
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");
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");
63 # Mocking variables
64 my $biblio_module = new Test::MockModule('C4::Biblio');
65 $biblio_module->mock(
66 'GetMarcSubfieldStructure',
67 sub {
68 my ($self) = shift;
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();
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 },
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,
102 sub run_tests {
104 my $marcflavour = shift;
105 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
107 my $isbn = '0590353403';
108 my $title = 'Foundation';
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 ) );
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.');
122 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
123 my $marc = GetMarcBiblio({ biblionumber => $biblionumber });
124 is( $marc->subfield( $isbn_field, $isbn_subfield ), $isbn, );
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 => $biblionumber });
135 my ( $title_field, $title_subfield ) = get_title_field();
136 is( $marc->subfield( $title_field, $title_subfield ), $title, );
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.');
144 my $success = 0;
145 $field = MARC::Field->new(
146 655, ' ', ' ',
147 'a' => 'Auction catalogs',
148 '9' => '1'
150 eval {
151 $marc_record->append_fields($field);
152 $success = ModBiblio($marc_record,$biblionumber,'');
153 } or do {
154 diag($@);
155 $success = 0;
157 ok($success, "ModBiblio handles authority-linked 655");
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;
167 ok($success, "ModBiblio handles 655 with authority link but no heading");
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;
177 ok($success, "ModBiblio handles 655 with no subfields");
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);
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)');
211 ## Testing GetMarcControlnumber
212 my $controlnumber;
213 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
214 is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
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' );
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' );
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');
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');
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);
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));
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({
257 biblionumber => $biblionumber,
258 embed_items => 0 });
259 my $frameworkcode = GetFrameworkCode($biblionumber);
260 my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
261 die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
262 my $biblioitemnumbertotest;
263 if ( $biblioitem_tag < 10 ) {
264 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->data();
265 } else {
266 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
268 is ($newincbiblioitemnumber, $biblioitemnumbertotest, 'Check newincbiblioitemnumber');
270 # test for GetMarcNotes
271 my $a1= GetMarcNotes( $marc_record, $marcflavour );
272 my $field2 = MARC::Field->new( $marcflavour eq 'UNIMARC'? 300: 555, 0, '', a=> 'Some text', u=> 'http://url-1.com', u=> 'nohttp://something_else' );
273 $marc_record->append_fields( $field2 );
274 my $a2= GetMarcNotes( $marc_record, $marcflavour );
275 is( ( $marcflavour eq 'UNIMARC' && @$a2 == @$a1 + 1 ) ||
276 ( $marcflavour ne 'UNIMARC' && @$a2 == @$a1 + 3 ), 1,
277 'Check the number of returned notes of GetMarcNotes' );
279 # test for GetMarcUrls
280 $marc_record->append_fields(
281 MARC::Field->new( '856', '', '', u => ' https://koha-community.org ' ),
282 MARC::Field->new( '856', '', '', u => 'koha-community.org' ),
284 my $marcurl = GetMarcUrls( $marc_record, $marcflavour );
285 is( @$marcurl, 2, 'GetMarcUrls returns two URLs' );
286 like( $marcurl->[0]->{MARCURL}, qr/^https/, 'GetMarcUrls did not stumble over a preceding space' );
287 ok( $marcflavour ne 'MARC21' || $marcurl->[1]->{MARCURL} =~ /^http:\/\//,
288 'GetMarcUrls prefixed a MARC21 URL with http://' );
291 sub get_title_field {
292 my $marc_flavour = C4::Context->preference('marcflavour');
293 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'a' ) : ( '245', 'a' );
296 sub get_isbn_field {
297 my $marc_flavour = C4::Context->preference('marcflavour');
298 return ( $marc_flavour eq 'UNIMARC' ) ? ( '010', 'a' ) : ( '020', 'a' );
301 sub get_issn_field {
302 my $marc_flavour = C4::Context->preference('marcflavour');
303 return ( $marc_flavour eq 'UNIMARC' ) ? ( '011', 'a' ) : ( '022', 'a' );
306 sub get_itemnumber_field {
307 my $marc_flavour = C4::Context->preference('marcflavour');
308 return ( $marc_flavour eq 'UNIMARC' ) ? ( '995', '9' ) : ( '952', '9' );
311 sub create_title_field {
312 my ( $title, $marcflavour ) = @_;
314 my ( $title_field, $title_subfield ) = get_title_field();
315 my $field = MARC::Field->new( $title_field, '', '', $title_subfield => $title );
317 return $field;
320 sub create_isbn_field {
321 my ( $isbn, $marcflavour ) = @_;
323 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
324 my $field = MARC::Field->new( $isbn_field, '', '', $isbn_subfield => $isbn );
326 # Add the price subfield
327 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c';
328 $field->add_subfields( $price_subfield => '$100' );
330 return $field;
333 sub create_issn_field {
334 my ( $issn, $marcflavour ) = @_;
336 my ( $issn_field, $issn_subfield ) = get_issn_field();
337 my $field = MARC::Field->new( $issn_field, '', '', $issn_subfield => $issn );
339 return $field;
342 subtest 'MARC21' => sub {
343 plan tests => 34;
344 run_tests('MARC21');
345 $schema->storage->txn_rollback;
346 $schema->storage->txn_begin;
349 subtest 'UNIMARC' => sub {
350 plan tests => 34;
351 run_tests('UNIMARC');
352 $schema->storage->txn_rollback;
353 $schema->storage->txn_begin;
356 subtest 'NORMARC' => sub {
357 plan tests => 34;
358 run_tests('NORMARC');
359 $schema->storage->txn_rollback;
360 $schema->storage->txn_begin;
363 subtest 'IsMarcStructureInternal' => sub {
364 plan tests => 6;
365 my $tagslib = GetMarcStructure();
366 my @internals;
367 for my $tag ( sort keys %$tagslib ) {
368 next unless $tag;
369 for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
370 push @internals, $subfield if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
373 @internals = uniq @internals;
374 is( scalar(@internals), 4, 'expect four internals');
375 is( grep( /^lib$/, @internals ), 1, 'check lib' );
376 is( grep( /^tab$/, @internals ), 1, 'check tab' );
377 is( grep( /^mandatory$/, @internals ), 1, 'check mandatory' );
378 is( grep( /^repeatable$/, @internals ), 1, 'check repeatable' );
379 is( grep( /^a$/, @internals ), 0, 'no subfield a' );
382 subtest 'deletedbiblio_metadata' => sub {
383 plan tests => 2;
385 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
386 my $biblio_metadata = C4::Biblio::GetXmlBiblio( $biblionumber );
387 C4::Biblio::DelBiblio( $biblionumber );
388 my ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio WHERE biblionumber=?|, undef, $biblionumber);
389 is( $moved, $biblionumber, 'Found in deletedbiblio' );
390 ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio_metadata WHERE biblionumber=?|, undef, $biblionumber);
391 is( $moved, $biblionumber, 'Found in deletedbiblio_metadata' );