Bug 11912: (regression tests) GetMarcISBN wrongly prepends a space to ISBNs
[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 => 3;
21 use Test::MockModule;
22 use Data::Dumper;
23
24 use MARC::Record;
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 my $global_marcflavour;
36 # Mocking variables
37 my $original_preference = C4::Context->can( 'preference' );
38 my $context             = new Test::MockModule('C4::Context');
39
40 mock_preference();
41 mock_marcfromkohafield();
42
43 sub run_tests {
44
45     # Undef C4::Biblio::inverted_field_map to avoid problems introduced
46     # by caching in TransformMarcToKoha
47     undef $C4::Biblio::inverted_field_map;
48
49     my $marcflavour = shift;
50     $global_marcflavour = $marcflavour;
51
52     my $isbn = '0590353403';
53     my $title = 'Foundation';
54
55     # Generate a record with just the ISBN
56     my $marc_record = MARC::Record->new;
57     my $isbn_field  = create_isbn_field( $isbn, $marcflavour );
58     $marc_record->append_fields( $isbn_field );
59
60     # Add the record to the DB
61     my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
62     my $data = GetBiblioData( $biblionumber );
63     is( $data->{ isbn }, $isbn,
64         '(GetBiblioData) ISBN correctly retireved.');
65     is( $data->{ title }, undef,
66         '(GetBiblioData) Title field is empty in fresh biblio.');
67
68     # Add title
69     my $field = create_title_field( $title, $marcflavour );
70     $marc_record->append_fields( $field );
71     ModBiblio( $marc_record, $biblionumber ,'' );
72     $data = GetBiblioData( $biblionumber );
73     is( $data->{ title }, $title,
74         'ModBiblio correctly added the title field, and GetBiblioData.');
75     is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
76
77     my $itemdata = GetBiblioItemData( $biblioitemnumber );
78     is( $itemdata->{ title }, $title,
79         'First test of GetBiblioItemData to get same result of previous two GetBiblioData tests.');
80     is( $itemdata->{ isbn }, $isbn,
81         'Second test checking it returns the correct isbn.');
82
83     my $success = 0;
84     $field = MARC::Field->new(
85             655, ' ', ' ',
86             'a' => 'Auction catalogs',
87             '9' => '1'
88             );
89     eval {
90         $marc_record->append_fields($field);
91         $success = ModBiblio($marc_record,$biblionumber,'');
92     } or do {
93         diag($@);
94         $success = 0;
95     };
96     ok($success, "ModBiblio handles authority-linked 655");
97
98     eval {
99         $field->delete_subfields('a');
100         $marc_record->append_fields($field);
101         $success = ModBiblio($marc_record,$biblionumber,'');
102     } or do {
103         diag($@);
104         $success = 0;
105     };
106     ok($success, "ModBiblio handles 655 with authority link but no heading");
107
108     eval {
109         $field->delete_subfields('9');
110         $marc_record->append_fields($field);
111         $success = ModBiblio($marc_record,$biblionumber,'');
112     } or do {
113         diag($@);
114         $success = 0;
115     };
116     ok($success, "ModBiblio handles 655 with no subfields");
117
118     ## Testing GetMarcISSN
119     my $issns;
120     $issns = GetMarcISSN( $marc_record, $marcflavour );
121     is( $issns->[0], undef,
122         'GetMarcISSN handles records without the ISSN field (list is empty)' );
123     is( scalar @$issns, 0,
124         'GetMarcISSN handles records without the ISSN field (count is 0)' );
125     # Add an ISSN field
126     my $issn = '1234-1234';
127     $field = create_issn_field( $issn, $marcflavour );
128     $marc_record->append_fields($field);
129     $issns = GetMarcISSN( $marc_record, $marcflavour );
130     is( $issns->[0], $issn,
131         'GetMarcISSN handles records with a single ISSN field (first element is correct)' );
132     is( scalar @$issns, 1,
133         'GetMARCISSN handles records with a single ISSN field (count is 1)');
134     # Add multiple ISSN field
135     my @more_issns = qw/1111-1111 2222-2222 3333-3333/;
136     foreach (@more_issns) {
137         $field = create_issn_field( $_, $marcflavour );
138         $marc_record->append_fields($field);
139     }
140     $issns = GetMarcISSN( $marc_record, $marcflavour );
141     is( scalar @$issns, 4,
142         'GetMARCISSN handles records with multiple ISSN fields (count correct)');
143
144     ## Testing GetMarcControlnumber
145     my $controlnumber;
146     $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
147     is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
148
149     $field = MARC::Field->new( '001', '' );
150     $marc_record->append_fields($field);
151     $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
152     is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' );
153
154     $field = $marc_record->field('001');
155     $field->update('123456789X');
156     $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
157     is( $controlnumber, '123456789X', 'GetMarcControlnumber handles records with 001' );
158
159     ## Testing GetMarcISBN
160     my $record_for_isbn = MARC::Record->new();
161     my $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
162     is( scalar @$isbns, 0, '(GetMarcISBN) The record contains no ISBN');
163
164     # We add one ISBN
165     $isbn_field = create_isbn_field( $isbn, $marcflavour );
166     $record_for_isbn->append_fields( $isbn_field );
167     $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
168     is( scalar @$isbns, 1, '(GetMarcISBN) The record contains one ISBN');
169     is( $isbns->[0]->{ marcisbn }, $isbn, '(GetMarcISBN) The record contains our ISBN');
170
171     # We add 3 more ISBNs
172     $record_for_isbn = MARC::Record->new();
173     my @more_isbns = qw/1111111111 2222222222 3333333333 444444444/;
174     foreach (@more_isbns) {
175         $field = create_isbn_field( $_, $marcflavour );
176         $record_for_isbn->append_fields($field);
177     }
178     $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
179     is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
180     for my $i (0 .. $#more_isbns) {
181         is( $isbns->[$i]->{ marcisbn }, $more_isbns[$i],
182             "(GetMarcISBN) Corretly retrieves ISBN #". ($i + 1));
183     }
184
185 }
186
187 sub mock_preference {
188
189     $context->mock( 'preference', sub {
190         my ( $self, $pref ) = @_;
191         if ( $pref eq 'marcflavour' ) {
192             return $global_marcflavour;
193         } else {
194             &$original_preference(@_);
195         }
196     });
197
198 }
199
200 sub mock_marcfromkohafield {
201
202     $context->mock('marcfromkohafield',
203         sub {
204             my ( $self ) = shift;
205
206             if ( $global_marcflavour eq 'MARC21' ) {
207
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' ]
215                     }
216                 };
217             } elsif ( $global_marcflavour eq 'UNIMARC' ) {
218
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' ]
226                     }
227                 };
228             }
229         });
230 }
231
232 sub create_title_field {
233     my ( $title, $marcflavour ) = @_;
234
235     my $title_field = ( $marcflavour eq 'UNIMARC' ) ? '200' : '245';
236     my $field = MARC::Field->new( $title_field,'','','a' => $title);
237
238     return $field;
239 }
240
241 sub create_isbn_field {
242     my ( $isbn, $marcflavour ) = @_;
243
244     my $isbn_field = ( $marcflavour eq 'UNIMARC' ) ? '010' : '020';
245     my $field = MARC::Field->new( $isbn_field,'','','a' => $isbn);
246
247     return $field;
248 }
249
250 sub create_issn_field {
251     my ( $issn, $marcflavour ) = @_;
252
253     my $issn_field = ( $marcflavour eq 'UNIMARC' ) ? '011' : '022';
254     my $field = MARC::Field->new( $issn_field,'','','a' => $issn);
255
256     return $field;
257 }
258
259 subtest 'MARC21' => sub {
260     plan tests => 25;
261     run_tests('MARC21');
262     $dbh->rollback;
263 };
264
265 subtest 'UNIMARC' => sub {
266     plan tests => 25;
267     run_tests('UNIMARC');
268     $dbh->rollback;
269 };
270
271
272 1;