]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Biblio.t
Bug 15871: Improve PerlCritic level for t/RecordProcessor.t
[koha.git] / t / db_dependent / Biblio.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 5;
21 use Test::MockModule;
22
23 use MARC::Record;
24 use t::lib::Mocks qw( mock_preference );
25
26 BEGIN {
27     use_ok('C4::Biblio');
28 }
29
30 my $dbh = C4::Context->dbh;
31 # Start transaction
32 $dbh->{AutoCommit} = 0;
33 $dbh->{RaiseError} = 1;
34
35 # Mocking variables
36 my $context = new Test::MockModule('C4::Context');
37
38 mock_marcfromkohafield();
39
40 my $currency = new Test::MockModule('Koha::Acquisition::Currencies');
41 $currency->mock(
42     'get_active',
43     sub {
44         return Koha::Acquisition::Currency->new(
45             {   symbol   => '$',
46                 isocode  => 'USD',
47                 currency => 'USD',
48                 active   => 1,
49             }
50         );
51     }
52 );
53
54 sub run_tests {
55
56     # Undef C4::Biblio::inverted_field_map to avoid problems introduced
57     # by caching in TransformMarcToKoha
58     undef $C4::Biblio::inverted_field_map;
59
60     my $marcflavour = shift;
61     t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
62
63     my $isbn = '0590353403';
64     my $title = 'Foundation';
65
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 );
70
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.');
78
79     # Add title
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.');
87
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.');
93
94     my $success = 0;
95     $field = MARC::Field->new(
96             655, ' ', ' ',
97             'a' => 'Auction catalogs',
98             '9' => '1'
99             );
100     eval {
101         $marc_record->append_fields($field);
102         $success = ModBiblio($marc_record,$biblionumber,'');
103     } or do {
104         diag($@);
105         $success = 0;
106     };
107     ok($success, "ModBiblio handles authority-linked 655");
108
109     eval {
110         $field->delete_subfields('a');
111         $marc_record->append_fields($field);
112         $success = ModBiblio($marc_record,$biblionumber,'');
113     } or do {
114         diag($@);
115         $success = 0;
116     };
117     ok($success, "ModBiblio handles 655 with authority link but no heading");
118
119     eval {
120         $field->delete_subfields('9');
121         $marc_record->append_fields($field);
122         $success = ModBiblio($marc_record,$biblionumber,'');
123     } or do {
124         diag($@);
125         $success = 0;
126     };
127     ok($success, "ModBiblio handles 655 with no subfields");
128
129     ## Testing GetMarcISSN
130     my $issns;
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)' );
136     # Add an ISSN field
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);
150     }
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)');
160
161     ## Testing GetMarcControlnumber
162     my $controlnumber;
163     $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
164     is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
165
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' );
170
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' );
175
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');
180
181     # We add one 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');
187
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);
194     }
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));
200     }
201
202
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();
214     } else {
215         $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
216     }
217     is ($newincbiblioitemnumber, $biblioitemnumbertotest);
218 }
219
220 sub mock_marcfromkohafield {
221
222     $context->mock('marcfromkohafield',
223         sub {
224             my ( $self ) = shift;
225
226             if ( C4::Context->preference('marcflavour') eq 'MARC21' ||
227                  C4::Context->preference('marcflavour') eq 'NORMARC' ) {
228
229                 return  {
230                 '' => {
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' ]
236                     }
237                 };
238             } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
239
240                 return {
241                 '' => {
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' ]
247                     }
248                 };
249             }
250         });
251 }
252
253 sub create_title_field {
254     my ( $title, $marcflavour ) = @_;
255
256     my $title_field = ( $marcflavour eq 'UNIMARC' ) ? '200' : '245';
257     my $field = MARC::Field->new( $title_field,'','','a' => $title);
258
259     return $field;
260 }
261
262 sub create_isbn_field {
263     my ( $isbn, $marcflavour ) = @_;
264
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' );
270
271     return $field;
272 }
273
274 sub create_issn_field {
275     my ( $issn, $marcflavour ) = @_;
276
277     my $issn_field = ( $marcflavour eq 'UNIMARC' ) ? '011' : '022';
278     my $field = MARC::Field->new( $issn_field,'','','a' => $issn);
279
280     return $field;
281 }
282
283 subtest 'MARC21' => sub {
284     plan tests => 28;
285     run_tests('MARC21');
286     $dbh->rollback;
287 };
288
289 subtest 'UNIMARC' => sub {
290     plan tests => 28;
291     run_tests('UNIMARC');
292     $dbh->rollback;
293 };
294
295 subtest 'NORMARC' => sub {
296     plan tests => 28;
297     run_tests('NORMARC');
298     $dbh->rollback;
299 };
300
301 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
302     plan tests => 23;
303
304     my @columns = qw(
305         tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
306         authorised_value authtypecode value_builder isurl hidden frameworkcode
307         seealso link defaultvalue maxlength
308     );
309
310     # biblio.biblionumber must be mapped so this should return something
311     my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber', '');
312
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'");
317     }
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");
320
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");
324 };
325
326 1;