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>.
20 use Test::More tests => 6;
23 use List::MoreUtils qw( uniq );
25 use t::lib::Mocks qw( mock_preference );
31 my $dbh = C4::Context->dbh;
33 $dbh->{AutoCommit} = 0;
34 $dbh->{RaiseError} = 1;
37 my $context = new Test::MockModule('C4::Context');
39 mock_marcfromkohafield();
41 my $currency = new Test::MockModule('Koha::Acquisition::Currencies');
45 return Koha::Acquisition::Currency->new(
57 # Undef C4::Biblio::inverted_field_map to avoid problems introduced
58 # by caching in TransformMarcToKoha
59 undef $C4::Biblio::inverted_field_map;
61 my $marcflavour = shift;
62 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
64 my $isbn = '0590353403';
65 my $title = 'Foundation';
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 );
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.');
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.');
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.');
96 $field = MARC::Field->new(
98 'a' => 'Auction catalogs',
102 $marc_record->append_fields($field);
103 $success = ModBiblio($marc_record,$biblionumber,'');
108 ok($success, "ModBiblio handles authority-linked 655");
111 $field->delete_subfields('a');
112 $marc_record->append_fields($field);
113 $success = ModBiblio($marc_record,$biblionumber,'');
118 ok($success, "ModBiblio handles 655 with authority link but no heading");
121 $field->delete_subfields('9');
122 $marc_record->append_fields($field);
123 $success = ModBiblio($marc_record,$biblionumber,'');
128 ok($success, "ModBiblio handles 655 with no subfields");
130 ## Testing GetMarcISSN
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)' );
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);
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)');
162 ## Testing GetMarcControlnumber
164 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
165 is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
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' );
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' );
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');
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');
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);
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));
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();
216 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
218 is ($newincbiblioitemnumber, $biblioitemnumbertotest);
221 sub mock_marcfromkohafield {
223 $context->mock('marcfromkohafield',
225 my ( $self ) = shift;
227 if ( C4::Context->preference('marcflavour') eq 'MARC21' ||
228 C4::Context->preference('marcflavour') eq 'NORMARC' ) {
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' ]
239 } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
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' ]
254 sub create_title_field {
255 my ( $title, $marcflavour ) = @_;
257 my $title_field = ( $marcflavour eq 'UNIMARC' ) ? '200' : '245';
258 my $field = MARC::Field->new( $title_field,'','','a' => $title);
263 sub create_isbn_field {
264 my ( $isbn, $marcflavour ) = @_;
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' );
275 sub create_issn_field {
276 my ( $issn, $marcflavour ) = @_;
278 my $issn_field = ( $marcflavour eq 'UNIMARC' ) ? '011' : '022';
279 my $field = MARC::Field->new( $issn_field,'','','a' => $issn);
284 subtest 'MARC21' => sub {
290 subtest 'UNIMARC' => sub {
292 run_tests('UNIMARC');
296 subtest 'NORMARC' => sub {
298 run_tests('NORMARC');
302 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
306 tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
307 authorised_value authtypecode value_builder isurl hidden frameworkcode
308 seealso link defaultvalue maxlength
311 # biblio.biblionumber must be mapped so this should return something
312 my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber', '');
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'");
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");
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");
327 subtest 'IsMarcStructureInternal' => sub {
329 my $tagslib = GetMarcStructure();
331 for my $tag ( sort keys %$tagslib ) {
333 for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
334 push @internals, $subfield if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
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' );