Bug 14778: Make 3 tests pass
[koha.git] / t / db_dependent / Biblio.t
blob0413b909cd4486622eff1e786e2d28c6f879cabe
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");
197 my $newincbiblioitemnumber=$biblioitemnumber+1;
198 $dbh->do("UPDATE biblioitems SET biblioitemnumber = ? WHERE biblionumber = ?;", undef, $newincbiblioitemnumber, $biblionumber );
199 my $updatedrecord = GetMarcBiblio($biblionumber, 0);
200 my $frameworkcode = GetFrameworkCode($biblionumber);
201 my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
202 die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
203 my $biblioitemnumbertotest;
204 if ( $biblioitem_tag < 10 ) {
205 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->data();
206 } else {
207 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
209 is ($newincbiblioitemnumber, $biblioitemnumbertotest);
212 sub mock_marcfromkohafield {
214 $context->mock('marcfromkohafield',
215 sub {
216 my ( $self ) = shift;
218 if ( C4::Context->preference('marcflavour') eq 'MARC21' ||
219 C4::Context->preference('marcflavour') eq 'NORMARC' ) {
221 return {
222 '' => {
223 'biblio.title' => [ '245', 'a' ],
224 'biblio.biblionumber' => [ '999', 'c' ],
225 'biblioitems.isbn' => [ '020', 'a' ],
226 'biblioitems.issn' => [ '022', 'a' ],
227 'biblioitems.biblioitemnumber' => [ '999', 'd' ]
230 } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
232 return {
233 '' => {
234 'biblio.title' => [ '200', 'a' ],
235 'biblio.biblionumber' => [ '999', 'c' ],
236 'biblioitems.isbn' => [ '010', 'a' ],
237 'biblioitems.issn' => [ '011', 'a' ],
238 'biblioitems.biblioitemnumber' => [ '090', 'a' ]
245 sub create_title_field {
246 my ( $title, $marcflavour ) = @_;
248 my $title_field = ( $marcflavour eq 'UNIMARC' ) ? '200' : '245';
249 my $field = MARC::Field->new( $title_field,'','','a' => $title);
251 return $field;
254 sub create_isbn_field {
255 my ( $isbn, $marcflavour ) = @_;
257 my $isbn_field = ( $marcflavour eq 'UNIMARC' ) ? '010' : '020';
258 my $field = MARC::Field->new( $isbn_field,'','','a' => $isbn);
259 # Add the price subfield
260 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c' ;
261 $field->add_subfields( $price_subfield => '$100' );
263 return $field;
266 sub create_issn_field {
267 my ( $issn, $marcflavour ) = @_;
269 my $issn_field = ( $marcflavour eq 'UNIMARC' ) ? '011' : '022';
270 my $field = MARC::Field->new( $issn_field,'','','a' => $issn);
272 return $field;
275 subtest 'MARC21' => sub {
276 plan tests => 28;
277 run_tests('MARC21');
278 $dbh->rollback;
281 subtest 'UNIMARC' => sub {
282 plan tests => 28;
283 run_tests('UNIMARC');
284 $dbh->rollback;
287 subtest 'NORMARC' => sub {
288 plan tests => 28;
289 run_tests('NORMARC');
290 $dbh->rollback;