Bug 17709 - Article request broken - 'internal server error'
[koha.git] / t / db_dependent / Biblio.t
blob951288c78ee320c058f042b535981a3ac656b32e
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 => 6;
21 use Test::MockModule;
23 use List::MoreUtils qw( uniq );
24 use MARC::Record;
25 use t::lib::Mocks qw( mock_preference );
27 BEGIN {
28 use_ok('C4::Biblio');
31 my $dbh = C4::Context->dbh;
32 # Start transaction
33 $dbh->{AutoCommit} = 0;
34 $dbh->{RaiseError} = 1;
36 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
37 plan tests => 23;
39 my @columns = qw(
40 tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
41 authorised_value authtypecode value_builder isurl hidden frameworkcode
42 seealso link defaultvalue maxlength
45 # biblio.biblionumber must be mapped so this should return something
46 my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber', '');
48 ok(defined $marc_subfield_structure, "There is a result");
49 is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
50 foreach my $col (@columns) {
51 ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
53 is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
54 like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
56 # foo.bar does not exist so this should return undef
57 $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar', '');
58 is($marc_subfield_structure, undef, "invalid kohafield returns undef");
62 # Mocking variables
63 my $biblio_module = new Test::MockModule('C4::Biblio');
64 $biblio_module->mock(
65 'GetMarcSubfieldStructure',
66 sub {
67 my ($self) = shift;
69 if ( C4::Context->preference('marcflavour') eq 'MARC21'
70 || C4::Context->preference('marcflavour') eq 'NORMARC' ) {
72 return {
73 'biblio.title' => { tagfield => '245', tagsubfield => 'a' },
74 'biblio.biblionumber' => { tagfield => '999', tagsubfield => 'c' },
75 'biblioitems.isbn' => { tagfield => '020', tagsubfield => 'a' },
76 'biblioitems.issn' => { tagfield => '022', tagsubfield => 'a' },
77 'biblioitems.biblioitemnumber' => { tagfield => '999', tagsubfield => 'd' },
78 'items.itemnumber' => { tagfield => '952', tagsubfield => '9' },
80 } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
82 return {
83 'biblio.title' => { tagfield => '200', tagsubfield => 'a' },
84 'biblio.biblionumber' => { tagfield => '999', tagsubfield => 'c' },
85 'biblioitems.isbn' => { tagfield => '010', tagsubfield => 'a' },
86 'biblioitems.issn' => { tagfield => '011', tagsubfield => 'a' },
87 'biblioitems.biblioitemnumber' => { tagfield => '090', tagsubfield => 'a' },
88 'items.itemnumber' => { tagfield => '995', tagsubfield => '9' },
94 my $currency = new Test::MockModule('Koha::Acquisition::Currencies');
95 $currency->mock(
96 'get_active',
97 sub {
98 return Koha::Acquisition::Currency->new(
99 { symbol => '$',
100 isocode => 'USD',
101 currency => 'USD',
102 active => 1,
108 sub run_tests {
110 my $marcflavour = shift;
111 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
113 my $isbn = '0590353403';
114 my $title = 'Foundation';
116 # Generate a record with just the ISBN
117 my $marc_record = MARC::Record->new;
118 my $isbn_field = create_isbn_field( $isbn, $marcflavour );
119 $marc_record->append_fields( $isbn_field );
121 # Add the record to the DB
122 my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
123 my $data = GetBiblioData( $biblionumber );
124 is( $data->{ isbn }, $isbn,
125 '(GetBiblioData) ISBN correctly retireved.');
126 is( $data->{ title }, undef,
127 '(GetBiblioData) Title field is empty in fresh biblio.');
129 # Add title
130 my $field = create_title_field( $title, $marcflavour );
131 $marc_record->append_fields( $field );
132 ModBiblio( $marc_record, $biblionumber ,'' );
133 $data = GetBiblioData( $biblionumber );
134 is( $data->{ title }, $title,
135 'ModBiblio correctly added the title field, and GetBiblioData.');
136 is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
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) Corretly 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($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);
266 is ($newincbiblioitemnumber, $biblioitemnumbertotest, 'Check newincbiblioitemnumber');
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' );
278 sub create_title_field {
279 my ( $title, $marcflavour ) = @_;
281 my $title_field = ( $marcflavour eq 'UNIMARC' ) ? '200' : '245';
282 my $field = MARC::Field->new( $title_field,'','','a' => $title);
284 return $field;
287 sub create_isbn_field {
288 my ( $isbn, $marcflavour ) = @_;
290 my $isbn_field = ( $marcflavour eq 'UNIMARC' ) ? '010' : '020';
291 my $field = MARC::Field->new( $isbn_field,'','','a' => $isbn);
292 # Add the price subfield
293 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c' ;
294 $field->add_subfields( $price_subfield => '$100' );
296 return $field;
299 sub create_issn_field {
300 my ( $issn, $marcflavour ) = @_;
302 my $issn_field = ( $marcflavour eq 'UNIMARC' ) ? '011' : '022';
303 my $field = MARC::Field->new( $issn_field,'','','a' => $issn);
305 return $field;
308 subtest 'MARC21' => sub {
309 plan tests => 29;
310 run_tests('MARC21');
311 $dbh->rollback;
314 subtest 'UNIMARC' => sub {
315 plan tests => 29;
316 run_tests('UNIMARC');
317 $dbh->rollback;
320 subtest 'NORMARC' => sub {
321 plan tests => 29;
322 run_tests('NORMARC');
323 $dbh->rollback;
326 subtest 'IsMarcStructureInternal' => sub {
327 plan tests => 6;
328 my $tagslib = GetMarcStructure();
329 my @internals;
330 for my $tag ( sort keys %$tagslib ) {
331 next unless $tag;
332 for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
333 push @internals, $subfield if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
336 @internals = uniq @internals;
337 is( scalar(@internals), 4, 'expect four internals');
338 is( grep( /^lib$/, @internals ), 1, 'check lib' );
339 is( grep( /^tab$/, @internals ), 1, 'check tab' );
340 is( grep( /^mandatory$/, @internals ), 1, 'check mandatory' );
341 is( grep( /^repeatable$/, @internals ), 1, 'check repeatable' );
342 is( grep( /^a$/, @internals ), 0, 'no subfield a' );