Bug 10900 - Follow up, since more has been added to master
[koha.git] / t / db_dependent / Biblio.t
blobf53b6a5c29b9faa04943e1825a9fea1f61b98df3
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 => 4;
21 use Test::MockModule;
23 use MARC::Record;
24 use t::lib::Mocks qw( mock_preference );
26 BEGIN {
27 use_ok('C4::Biblio');
30 my $dbh = C4::Context->dbh;
31 # Start transaction
32 $dbh->{AutoCommit} = 0;
33 $dbh->{RaiseError} = 1;
35 # Mocking variables
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 };
44 });
46 sub run_tests {
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.');
71 # Add title
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.');
86 my $success = 0;
87 $field = MARC::Field->new(
88 655, ' ', ' ',
89 'a' => 'Auction catalogs',
90 '9' => '1'
92 eval {
93 $marc_record->append_fields($field);
94 $success = ModBiblio($marc_record,$biblionumber,'');
95 } or do {
96 diag($@);
97 $success = 0;
99 ok($success, "ModBiblio handles authority-linked 655");
101 eval {
102 $field->delete_subfields('a');
103 $marc_record->append_fields($field);
104 $success = ModBiblio($marc_record,$biblionumber,'');
105 } or do {
106 diag($@);
107 $success = 0;
109 ok($success, "ModBiblio handles 655 with authority link but no heading");
111 eval {
112 $field->delete_subfields('9');
113 $marc_record->append_fields($field);
114 $success = ModBiblio($marc_record,$biblionumber,'');
115 } or do {
116 diag($@);
117 $success = 0;
119 ok($success, "ModBiblio handles 655 with no subfields");
121 ## Testing GetMarcISSN
122 my $issns;
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)' );
128 # Add an ISSN field
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)');
146 # Create an empty ISSN
147 $field = create_issn_field( "", $marcflavour );
148 $marc_record->append_fields($field);
149 $issns = GetMarcISSN( $marc_record, $marcflavour );
150 is( scalar @$issns, 4,
151 'GetMARCISSN skips empty ISSN fields (Bug 12674)');
153 ## Testing GetMarcControlnumber
154 my $controlnumber;
155 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
156 is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
158 $field = MARC::Field->new( '001', '' );
159 $marc_record->append_fields($field);
160 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
161 is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' );
163 $field = $marc_record->field('001');
164 $field->update('123456789X');
165 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
166 is( $controlnumber, '123456789X', 'GetMarcControlnumber handles records with 001' );
168 ## Testing GetMarcISBN
169 my $record_for_isbn = MARC::Record->new();
170 my $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
171 is( scalar @$isbns, 0, '(GetMarcISBN) The record contains no ISBN');
173 # We add one ISBN
174 $isbn_field = create_isbn_field( $isbn, $marcflavour );
175 $record_for_isbn->append_fields( $isbn_field );
176 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
177 is( scalar @$isbns, 1, '(GetMarcISBN) The record contains one ISBN');
178 is( $isbns->[0], $isbn, '(GetMarcISBN) The record contains our ISBN');
180 # We add 3 more ISBNs
181 $record_for_isbn = MARC::Record->new();
182 my @more_isbns = qw/1111111111 2222222222 3333333333 444444444/;
183 foreach (@more_isbns) {
184 $field = create_isbn_field( $_, $marcflavour );
185 $record_for_isbn->append_fields($field);
187 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
188 is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
189 for my $i (0 .. $#more_isbns) {
190 is( $isbns->[$i], $more_isbns[$i],
191 "(GetMarcISBN) Corretly retrieves ISBN #". ($i + 1));
195 is( GetMarcPrice( $record_for_isbn, $marcflavour ), 100,
196 "GetMarcPrice returns the correct value");
199 sub mock_marcfromkohafield {
201 $context->mock('marcfromkohafield',
202 sub {
203 my ( $self ) = shift;
205 if ( C4::Context->preference('marcflavour') eq 'MARC21' ||
206 C4::Context->preference('marcflavour') eq 'NORMARC' ) {
208 return {
209 '' => {
210 'biblio.title' => [ '245', 'a' ],
211 'biblio.biblionumber' => [ '999', 'c' ],
212 'biblioitems.isbn' => [ '020', 'a' ],
213 'biblioitems.issn' => [ '022', 'a' ],
214 'biblioitems.biblioitemnumber' => [ '999', 'd' ]
217 } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
219 return {
220 '' => {
221 'biblio.title' => [ '200', 'a' ],
222 'biblio.biblionumber' => [ '999', 'c' ],
223 'biblioitems.isbn' => [ '010', 'a' ],
224 'biblioitems.issn' => [ '011', 'a' ],
225 'biblioitems.biblioitemnumber' => [ '090', 'a' ]
232 sub create_title_field {
233 my ( $title, $marcflavour ) = @_;
235 my $title_field = ( $marcflavour eq 'UNIMARC' ) ? '200' : '245';
236 my $field = MARC::Field->new( $title_field,'','','a' => $title);
238 return $field;
241 sub create_isbn_field {
242 my ( $isbn, $marcflavour ) = @_;
244 my $isbn_field = ( $marcflavour eq 'UNIMARC' ) ? '010' : '020';
245 my $field = MARC::Field->new( $isbn_field,'','','a' => $isbn);
246 # Add the price subfield
247 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c' ;
248 $field->add_subfields( $price_subfield => '$100' );
250 return $field;
253 sub create_issn_field {
254 my ( $issn, $marcflavour ) = @_;
256 my $issn_field = ( $marcflavour eq 'UNIMARC' ) ? '011' : '022';
257 my $field = MARC::Field->new( $issn_field,'','','a' => $issn);
259 return $field;
262 subtest 'MARC21' => sub {
263 plan tests => 27;
264 run_tests('MARC21');
265 $dbh->rollback;
268 subtest 'UNIMARC' => sub {
269 plan tests => 27;
270 run_tests('UNIMARC');
271 $dbh->rollback;
274 subtest 'NORMARC' => sub {
275 plan tests => 27;
276 run_tests('NORMARC');
277 $dbh->rollback;