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 => 5;
24 use t::lib::Mocks qw( mock_preference );
30 my $dbh = C4::Context->dbh;
32 $dbh->{AutoCommit} = 0;
33 $dbh->{RaiseError} = 1;
36 my $context = new Test::MockModule('C4::Context');
38 mock_marcfromkohafield();
40 my $currency = new Test::MockModule('Koha::Acquisition::Currencies');
44 return Koha::Acquisition::Currency->new(
56 # Undef C4::Biblio::inverted_field_map to avoid problems introduced
57 # by caching in TransformMarcToKoha
58 undef $C4::Biblio::inverted_field_map;
60 my $marcflavour = shift;
61 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
63 my $isbn = '0590353403';
64 my $title = 'Foundation';
66 # Generate a record with just the ISBN
67 my $marc_record = MARC::Record->new;
68 my $isbn_field = create_isbn_field( $isbn, $marcflavour );
69 $marc_record->append_fields( $isbn_field );
71 # Add the record to the DB
72 my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
73 my $data = GetBiblioData( $biblionumber );
74 is( $data->{ isbn }, $isbn,
75 '(GetBiblioData) ISBN correctly retireved.');
76 is( $data->{ title }, undef,
77 '(GetBiblioData) Title field is empty in fresh biblio.');
80 my $field = create_title_field( $title, $marcflavour );
81 $marc_record->append_fields( $field );
82 ModBiblio( $marc_record, $biblionumber ,'' );
83 $data = GetBiblioData( $biblionumber );
84 is( $data->{ title }, $title,
85 'ModBiblio correctly added the title field, and GetBiblioData.');
86 is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
88 my $itemdata = GetBiblioItemData( $biblioitemnumber );
89 is( $itemdata->{ title }, $title,
90 'First test of GetBiblioItemData to get same result of previous two GetBiblioData tests.');
91 is( $itemdata->{ isbn }, $isbn,
92 'Second test checking it returns the correct isbn.');
95 $field = MARC::Field->new(
97 'a' => 'Auction catalogs',
101 $marc_record->append_fields($field);
102 $success = ModBiblio($marc_record,$biblionumber,'');
107 ok($success, "ModBiblio handles authority-linked 655");
110 $field->delete_subfields('a');
111 $marc_record->append_fields($field);
112 $success = ModBiblio($marc_record,$biblionumber,'');
117 ok($success, "ModBiblio handles 655 with authority link but no heading");
120 $field->delete_subfields('9');
121 $marc_record->append_fields($field);
122 $success = ModBiblio($marc_record,$biblionumber,'');
127 ok($success, "ModBiblio handles 655 with no subfields");
129 ## Testing GetMarcISSN
131 $issns = GetMarcISSN( $marc_record, $marcflavour );
132 is( $issns->[0], undef,
133 'GetMarcISSN handles records without the ISSN field (list is empty)' );
134 is( scalar @$issns, 0,
135 'GetMarcISSN handles records without the ISSN field (count is 0)' );
137 my $issn = '1234-1234';
138 $field = create_issn_field( $issn, $marcflavour );
139 $marc_record->append_fields($field);
140 $issns = GetMarcISSN( $marc_record, $marcflavour );
141 is( $issns->[0], $issn,
142 'GetMarcISSN handles records with a single ISSN field (first element is correct)' );
143 is( scalar @$issns, 1,
144 'GetMARCISSN handles records with a single ISSN field (count is 1)');
145 # Add multiple ISSN field
146 my @more_issns = qw/1111-1111 2222-2222 3333-3333/;
147 foreach (@more_issns) {
148 $field = create_issn_field( $_, $marcflavour );
149 $marc_record->append_fields($field);
151 $issns = GetMarcISSN( $marc_record, $marcflavour );
152 is( scalar @$issns, 4,
153 'GetMARCISSN handles records with multiple ISSN fields (count correct)');
154 # Create an empty ISSN
155 $field = create_issn_field( "", $marcflavour );
156 $marc_record->append_fields($field);
157 $issns = GetMarcISSN( $marc_record, $marcflavour );
158 is( scalar @$issns, 4,
159 'GetMARCISSN skips empty ISSN fields (Bug 12674)');
161 ## Testing GetMarcControlnumber
163 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
164 is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
166 $field = MARC::Field->new( '001', '' );
167 $marc_record->append_fields($field);
168 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
169 is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' );
171 $field = $marc_record->field('001');
172 $field->update('123456789X');
173 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
174 is( $controlnumber, '123456789X', 'GetMarcControlnumber handles records with 001' );
176 ## Testing GetMarcISBN
177 my $record_for_isbn = MARC::Record->new();
178 my $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
179 is( scalar @$isbns, 0, '(GetMarcISBN) The record contains no ISBN');
182 $isbn_field = create_isbn_field( $isbn, $marcflavour );
183 $record_for_isbn->append_fields( $isbn_field );
184 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
185 is( scalar @$isbns, 1, '(GetMarcISBN) The record contains one ISBN');
186 is( $isbns->[0], $isbn, '(GetMarcISBN) The record contains our ISBN');
188 # We add 3 more ISBNs
189 $record_for_isbn = MARC::Record->new();
190 my @more_isbns = qw/1111111111 2222222222 3333333333 444444444/;
191 foreach (@more_isbns) {
192 $field = create_isbn_field( $_, $marcflavour );
193 $record_for_isbn->append_fields($field);
195 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
196 is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
197 for my $i (0 .. $#more_isbns) {
198 is( $isbns->[$i], $more_isbns[$i],
199 "(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);
220 sub mock_marcfromkohafield {
222 $context->mock('marcfromkohafield',
224 my ( $self ) = shift;
226 if ( C4::Context->preference('marcflavour') eq 'MARC21' ||
227 C4::Context->preference('marcflavour') eq 'NORMARC' ) {
231 'biblio.title' => [ '245', 'a' ],
232 'biblio.biblionumber' => [ '999', 'c' ],
233 'biblioitems.isbn' => [ '020', 'a' ],
234 'biblioitems.issn' => [ '022', 'a' ],
235 'biblioitems.biblioitemnumber' => [ '999', 'd' ]
238 } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
242 'biblio.title' => [ '200', 'a' ],
243 'biblio.biblionumber' => [ '999', 'c' ],
244 'biblioitems.isbn' => [ '010', 'a' ],
245 'biblioitems.issn' => [ '011', 'a' ],
246 'biblioitems.biblioitemnumber' => [ '090', 'a' ]
253 sub create_title_field {
254 my ( $title, $marcflavour ) = @_;
256 my $title_field = ( $marcflavour eq 'UNIMARC' ) ? '200' : '245';
257 my $field = MARC::Field->new( $title_field,'','','a' => $title);
262 sub create_isbn_field {
263 my ( $isbn, $marcflavour ) = @_;
265 my $isbn_field = ( $marcflavour eq 'UNIMARC' ) ? '010' : '020';
266 my $field = MARC::Field->new( $isbn_field,'','','a' => $isbn);
267 # Add the price subfield
268 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c' ;
269 $field->add_subfields( $price_subfield => '$100' );
274 sub create_issn_field {
275 my ( $issn, $marcflavour ) = @_;
277 my $issn_field = ( $marcflavour eq 'UNIMARC' ) ? '011' : '022';
278 my $field = MARC::Field->new( $issn_field,'','','a' => $issn);
283 subtest 'MARC21' => sub {
289 subtest 'UNIMARC' => sub {
291 run_tests('UNIMARC');
295 subtest 'NORMARC' => sub {
297 run_tests('NORMARC');
301 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
305 tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
306 authorised_value authtypecode value_builder isurl hidden frameworkcode
307 seealso link defaultvalue maxlength
310 # biblio.biblionumber must be mapped so this should return something
311 my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber', '');
313 ok(defined $marc_subfield_structure, "There is a result");
314 is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
315 foreach my $col (@columns) {
316 ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
318 is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
319 like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
321 # foo.bar does not exist so this should return undef
322 $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar', '');
323 is($marc_subfield_structure, undef, "invalid kohafield returns undef");