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 => 4;
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('C4::Budgets');
41 $currency->mock( 'GetCurrency', sub {
42 return { symbol => '$', isocode => 'USD',
43 currency => 'USD', active => 1 };
48 # Undef C4::Biblio::inverted_field_map to avoid problems introduced
49 # by caching in TransformMarcToKoha
50 undef $C4::Biblio::inverted_field_map;
52 my $marcflavour = shift;
53 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
55 my $isbn = '0590353403';
56 my $title = 'Foundation';
58 # Generate a record with just the ISBN
59 my $marc_record = MARC::Record->new;
60 my $isbn_field = create_isbn_field( $isbn, $marcflavour );
61 $marc_record->append_fields( $isbn_field );
63 # Add the record to the DB
64 my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
65 my $data = GetBiblioData( $biblionumber );
66 is( $data->{ isbn }, $isbn,
67 '(GetBiblioData) ISBN correctly retireved.');
68 is( $data->{ title }, undef,
69 '(GetBiblioData) Title field is empty in fresh biblio.');
72 my $field = create_title_field( $title, $marcflavour );
73 $marc_record->append_fields( $field );
74 ModBiblio( $marc_record, $biblionumber ,'' );
75 $data = GetBiblioData( $biblionumber );
76 is( $data->{ title }, $title,
77 'ModBiblio correctly added the title field, and GetBiblioData.');
78 is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
80 my $itemdata = GetBiblioItemData( $biblioitemnumber );
81 is( $itemdata->{ title }, $title,
82 'First test of GetBiblioItemData to get same result of previous two GetBiblioData tests.');
83 is( $itemdata->{ isbn }, $isbn,
84 'Second test checking it returns the correct isbn.');
87 $field = MARC::Field->new(
89 'a' => 'Auction catalogs',
93 $marc_record->append_fields($field);
94 $success = ModBiblio($marc_record,$biblionumber,'');
99 ok($success, "ModBiblio handles authority-linked 655");
102 $field->delete_subfields('a');
103 $marc_record->append_fields($field);
104 $success = ModBiblio($marc_record,$biblionumber,'');
109 ok($success, "ModBiblio handles 655 with authority link but no heading");
112 $field->delete_subfields('9');
113 $marc_record->append_fields($field);
114 $success = ModBiblio($marc_record,$biblionumber,'');
119 ok($success, "ModBiblio handles 655 with no subfields");
121 ## Testing GetMarcISSN
123 $issns = GetMarcISSN( $marc_record, $marcflavour );
124 is( $issns->[0], undef,
125 'GetMarcISSN handles records without the ISSN field (list is empty)' );
126 is( scalar @$issns, 0,
127 'GetMarcISSN handles records without the ISSN field (count is 0)' );
129 my $issn = '1234-1234';
130 $field = create_issn_field( $issn, $marcflavour );
131 $marc_record->append_fields($field);
132 $issns = GetMarcISSN( $marc_record, $marcflavour );
133 is( $issns->[0], $issn,
134 'GetMarcISSN handles records with a single ISSN field (first element is correct)' );
135 is( scalar @$issns, 1,
136 'GetMARCISSN handles records with a single ISSN field (count is 1)');
137 # Add multiple ISSN field
138 my @more_issns = qw/1111-1111 2222-2222 3333-3333/;
139 foreach (@more_issns) {
140 $field = create_issn_field( $_, $marcflavour );
141 $marc_record->append_fields($field);
143 $issns = GetMarcISSN( $marc_record, $marcflavour );
144 is( scalar @$issns, 4,
145 'GetMARCISSN handles records with multiple ISSN fields (count correct)');
147 ## Testing GetMarcControlnumber
149 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
150 is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
152 $field = MARC::Field->new( '001', '' );
153 $marc_record->append_fields($field);
154 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
155 is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' );
157 $field = $marc_record->field('001');
158 $field->update('123456789X');
159 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
160 is( $controlnumber, '123456789X', 'GetMarcControlnumber handles records with 001' );
162 ## Testing GetMarcISBN
163 my $record_for_isbn = MARC::Record->new();
164 my $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
165 is( scalar @$isbns, 0, '(GetMarcISBN) The record contains no ISBN');
168 $isbn_field = create_isbn_field( $isbn, $marcflavour );
169 $record_for_isbn->append_fields( $isbn_field );
170 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
171 is( scalar @$isbns, 1, '(GetMarcISBN) The record contains one ISBN');
172 is( $isbns->[0], $isbn, '(GetMarcISBN) The record contains our ISBN');
174 # We add 3 more ISBNs
175 $record_for_isbn = MARC::Record->new();
176 my @more_isbns = qw/1111111111 2222222222 3333333333 444444444/;
177 foreach (@more_isbns) {
178 $field = create_isbn_field( $_, $marcflavour );
179 $record_for_isbn->append_fields($field);
181 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
182 is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
183 for my $i (0 .. $#more_isbns) {
184 is( $isbns->[$i], $more_isbns[$i],
185 "(GetMarcISBN) Corretly retrieves ISBN #". ($i + 1));
189 is( GetMarcPrice( $record_for_isbn, $marcflavour ), 100,
190 "GetMarcPrice returns the correct value");
193 sub mock_marcfromkohafield {
195 $context->mock('marcfromkohafield',
197 my ( $self ) = shift;
199 if ( C4::Context->preference('marcflavour') eq 'MARC21' ||
200 C4::Context->preference('marcflavour') eq 'NORMARC' ) {
204 'biblio.title' => [ '245', 'a' ],
205 'biblio.biblionumber' => [ '999', 'c' ],
206 'biblioitems.isbn' => [ '020', 'a' ],
207 'biblioitems.issn' => [ '022', 'a' ],
208 'biblioitems.biblioitemnumber' => [ '999', 'd' ]
211 } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
215 'biblio.title' => [ '200', 'a' ],
216 'biblio.biblionumber' => [ '999', 'c' ],
217 'biblioitems.isbn' => [ '010', 'a' ],
218 'biblioitems.issn' => [ '011', 'a' ],
219 'biblioitems.biblioitemnumber' => [ '090', 'a' ]
226 sub create_title_field {
227 my ( $title, $marcflavour ) = @_;
229 my $title_field = ( $marcflavour eq 'UNIMARC' ) ? '200' : '245';
230 my $field = MARC::Field->new( $title_field,'','','a' => $title);
235 sub create_isbn_field {
236 my ( $isbn, $marcflavour ) = @_;
238 my $isbn_field = ( $marcflavour eq 'UNIMARC' ) ? '010' : '020';
239 my $field = MARC::Field->new( $isbn_field,'','','a' => $isbn);
240 # Add the price subfield
241 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c' ;
242 $field->add_subfields( $price_subfield => '$100' );
247 sub create_issn_field {
248 my ( $issn, $marcflavour ) = @_;
250 my $issn_field = ( $marcflavour eq 'UNIMARC' ) ? '011' : '022';
251 my $field = MARC::Field->new( $issn_field,'','','a' => $issn);
256 subtest 'MARC21' => sub {
262 subtest 'UNIMARC' => sub {
264 run_tests('UNIMARC');
268 subtest 'NORMARC' => sub {
270 run_tests('NORMARC');