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));
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();
215 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
217 is ($newincbiblioitemnumber, $biblioitemnumbertotest, 'Check newincbiblioitemnumber');
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 is( ( $marcflavour eq 'UNIMARC' && @$a2 == @$a1 + 1 ) ||
225 ( $marcflavour ne 'UNIMARC' && @$a2 == @$a1 + 3 ), 1,
226 'Check the number of returned notes of GetMarcNotes' );
229 sub mock_marcfromkohafield {
231 $context->mock('marcfromkohafield',
233 my ( $self ) = shift;
235 if ( C4::Context->preference('marcflavour') eq 'MARC21' ||
236 C4::Context->preference('marcflavour') eq 'NORMARC' ) {
240 'biblio.title' => [ '245', 'a' ],
241 'biblio.biblionumber' => [ '999', 'c' ],
242 'biblioitems.isbn' => [ '020', 'a' ],
243 'biblioitems.issn' => [ '022', 'a' ],
244 'biblioitems.biblioitemnumber' => [ '999', 'd' ]
247 } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
251 'biblio.title' => [ '200', 'a' ],
252 'biblio.biblionumber' => [ '999', 'c' ],
253 'biblioitems.isbn' => [ '010', 'a' ],
254 'biblioitems.issn' => [ '011', 'a' ],
255 'biblioitems.biblioitemnumber' => [ '090', 'a' ]
262 sub create_title_field {
263 my ( $title, $marcflavour ) = @_;
265 my $title_field = ( $marcflavour eq 'UNIMARC' ) ? '200' : '245';
266 my $field = MARC::Field->new( $title_field,'','','a' => $title);
271 sub create_isbn_field {
272 my ( $isbn, $marcflavour ) = @_;
274 my $isbn_field = ( $marcflavour eq 'UNIMARC' ) ? '010' : '020';
275 my $field = MARC::Field->new( $isbn_field,'','','a' => $isbn);
276 # Add the price subfield
277 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c' ;
278 $field->add_subfields( $price_subfield => '$100' );
283 sub create_issn_field {
284 my ( $issn, $marcflavour ) = @_;
286 my $issn_field = ( $marcflavour eq 'UNIMARC' ) ? '011' : '022';
287 my $field = MARC::Field->new( $issn_field,'','','a' => $issn);
292 subtest 'MARC21' => sub {
298 subtest 'UNIMARC' => sub {
300 run_tests('UNIMARC');
304 subtest 'NORMARC' => sub {
306 run_tests('NORMARC');
310 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
314 tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
315 authorised_value authtypecode value_builder isurl hidden frameworkcode
316 seealso link defaultvalue maxlength
319 # biblio.biblionumber must be mapped so this should return something
320 my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber', '');
322 ok(defined $marc_subfield_structure, "There is a result");
323 is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
324 foreach my $col (@columns) {
325 ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
327 is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
328 like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
330 # foo.bar does not exist so this should return undef
331 $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar', '');
332 is($marc_subfield_structure, undef, "invalid kohafield returns undef");
335 subtest 'IsMarcStructureInternal' => sub {
337 my $tagslib = GetMarcStructure();
339 for my $tag ( sort keys %$tagslib ) {
341 for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
342 push @internals, $subfield if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
345 @internals = uniq @internals;
346 is( scalar(@internals), 4, 'expect four internals');
347 is( grep( /^lib$/, @internals ), 1, 'check lib' );
348 is( grep( /^tab$/, @internals ), 1, 'check tab' );
349 is( grep( /^mandatory$/, @internals ), 1, 'check mandatory' );
350 is( grep( /^repeatable$/, @internals ), 1, 'check repeatable' );
351 is( grep( /^a$/, @internals ), 0, 'no subfield a' );