|
Lines 1-113
Link Here
|
| 1 |
#!/usr/bin/perl |
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. |
| 2 |
# |
9 |
# |
| 3 |
# This Koha test module is a stub! |
10 |
# Koha is distributed in the hope that it will be useful, but |
| 4 |
# Add more tests here!!! |
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; |
| 5 |
|
23 |
|
| 6 |
use strict; |
|
|
| 7 |
use warnings; |
| 8 |
use Test::More tests => 17; |
| 9 |
use MARC::Record; |
24 |
use MARC::Record; |
| 10 |
use C4::Biblio; |
|
|
| 11 |
|
25 |
|
| 12 |
BEGIN { |
26 |
BEGIN { |
| 13 |
use_ok('C4::Biblio'); |
27 |
use_ok('C4::Biblio'); |
| 14 |
} |
28 |
} |
| 15 |
|
29 |
|
| 16 |
my $isbn = '0590353403'; |
30 |
my $dbh = C4::Context->dbh; |
| 17 |
my $title = 'Foundation'; |
31 |
# Start transaction |
| 18 |
|
32 |
$dbh->{AutoCommit} = 0; |
| 19 |
my $marc_record=MARC::Record->new; |
33 |
$dbh->{RaiseError} = 1; |
| 20 |
my $field = MARC::Field->new('020','','','a' => $isbn); |
34 |
|
| 21 |
$marc_record->append_fields($field); |
35 |
my $global_marcflavour; |
| 22 |
my($biblionumber,$biblioitemnumber) = AddBiblio($marc_record,''); |
36 |
# Mocking variables |
| 23 |
my $data = &GetBiblioData($biblionumber); |
37 |
my $original_preference = C4::Context->can( 'preference' ); |
| 24 |
is($data->{Title},undef,'Makes sure title field in biblio is empty.'); |
38 |
my $context = new Test::MockModule('C4::Context'); |
| 25 |
|
39 |
|
| 26 |
$field = MARC::Field->new('245','','','a' => $title); |
40 |
mock_preference(); |
| 27 |
$marc_record->append_fields($field); |
41 |
mock_marcfromkohafield(); |
| 28 |
ModBiblio($marc_record,$biblionumber,''); |
42 |
|
| 29 |
$data = &GetBiblioData($biblionumber); |
43 |
sub run_tests { |
| 30 |
is($data->{title},$title,'uses ModBiblio to add a title to the previously created record and checks that its there.'); |
44 |
|
| 31 |
is($data->{isbn},$isbn,'Makes sure the isbn is still there after using ModBiblio.'); |
45 |
# Undef C4::Biblio::inverted_field_map to avoid problems introduced |
| 32 |
|
46 |
# by caching in TransformMarcToKoha |
| 33 |
my $itemdata = &GetBiblioItemData($biblioitemnumber); |
47 |
undef $C4::Biblio::inverted_field_map; |
| 34 |
is($itemdata->{title},$title,'First test of GetBiblioItemData to get same result of previous two GetBiblioData tests.'); |
48 |
|
| 35 |
is($itemdata->{isbn},$isbn,'Second test checking it returns the correct isbn.'); |
49 |
my $marcflavour = shift; |
| 36 |
|
50 |
$global_marcflavour = $marcflavour; |
| 37 |
my $success = 0; |
51 |
|
| 38 |
$field = MARC::Field->new( |
52 |
my $isbn = '0590353403'; |
| 39 |
655, ' ', ' ', |
53 |
my $title = 'Foundation'; |
| 40 |
'a' => 'Auction catalogs', |
54 |
|
| 41 |
'9' => '1' |
55 |
# Generate a record with just the ISBN |
| 42 |
); |
56 |
my $marc_record = MARC::Record->new; |
| 43 |
eval { |
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 ); |
| 44 |
$marc_record->append_fields($field); |
128 |
$marc_record->append_fields($field); |
| 45 |
$success = ModBiblio($marc_record,$biblionumber,''); |
129 |
$issns = GetMarcISSN( $marc_record, $marcflavour ); |
| 46 |
} or do { |
130 |
is( $issns->[0], $issn, |
| 47 |
diag($@); |
131 |
'GetMarcISSN handles records with a single ISSN field (first element is correct)' ); |
| 48 |
$success = 0; |
132 |
is( scalar @$issns, 1, |
| 49 |
}; |
133 |
'GetMARCISSN handles records with a single ISSN field (count is 1)'); |
| 50 |
ok($success, "ModBiblio handles authority-linked 655"); |
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' ); |
| 51 |
|
148 |
|
| 52 |
eval { |
149 |
$field = MARC::Field->new( '001', '' ); |
| 53 |
$field->delete_subfields('a'); |
|
|
| 54 |
$marc_record->append_fields($field); |
150 |
$marc_record->append_fields($field); |
| 55 |
$success = ModBiblio($marc_record,$biblionumber,''); |
151 |
$controlnumber = GetMarcControlnumber( $marc_record, $marcflavour ); |
| 56 |
} or do { |
152 |
is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' ); |
| 57 |
diag($@); |
153 |
|
| 58 |
$success = 0; |
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; |
| 59 |
}; |
263 |
}; |
| 60 |
ok($success, "ModBiblio handles 655 with authority link but no heading"); |
|
|
| 61 |
|
264 |
|
| 62 |
eval { |
265 |
subtest 'UNIMARC' => sub { |
| 63 |
$field->delete_subfields('9'); |
266 |
plan tests => 25; |
| 64 |
$marc_record->append_fields($field); |
267 |
run_tests('UNIMARC'); |
| 65 |
$success = ModBiblio($marc_record,$biblionumber,''); |
268 |
$dbh->rollback; |
| 66 |
} or do { |
|
|
| 67 |
diag($@); |
| 68 |
$success = 0; |
| 69 |
}; |
269 |
}; |
| 70 |
ok($success, "ModBiblio handles 655 with no subfields"); |
270 |
|
| 71 |
|
271 |
|
| 72 |
# Testing GetMarcISSN |
272 |
1; |
| 73 |
my $issns; |
|
|
| 74 |
$issns = GetMarcISSN( $marc_record, 'MARC21' ); |
| 75 |
is( $issns->[0], undef, |
| 76 |
'GetMarcISSN handles records without 022 (list is empty)' ); |
| 77 |
is( scalar @$issns, 0, 'GetMarcISSN handles records without 022 (number of elements correct)' ); |
| 78 |
|
| 79 |
my $issn = '1234-1234'; |
| 80 |
$field = MARC::Field->new( '022', '', '', 'a', => $issn ); |
| 81 |
$marc_record->append_fields($field); |
| 82 |
$issns = GetMarcISSN( $marc_record, 'MARC21' ); |
| 83 |
is( $issns->[0], $issn, |
| 84 |
'GetMarcISSN handles records with single 022 (first element is correct)' ); |
| 85 |
is( scalar @$issns, 1, 'GetMARCISSN handles records with single 022 (number of elements correct)' |
| 86 |
); |
| 87 |
|
| 88 |
my @more_issns = qw/1111-1111 2222-2222 3333-3333/; |
| 89 |
foreach (@more_issns) { |
| 90 |
$field = MARC::Field->new( '022', '', '', 'a', => $_ ); |
| 91 |
$marc_record->append_fields($field); |
| 92 |
} |
| 93 |
$issns = GetMarcISSN( $marc_record, 'MARC21' ); |
| 94 |
is( scalar @$issns, 4, 'GetMARCISSN handles records with multiple 022 (number of elements correct)' |
| 95 |
); |
| 96 |
|
| 97 |
# Testing GetMarcControlnumber |
| 98 |
my $controlnumber; |
| 99 |
$controlnumber = GetMarcControlnumber( $marc_record, 'MARC21' ); |
| 100 |
is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' ); |
| 101 |
|
| 102 |
$field = MARC::Field->new( '001', '' ); |
| 103 |
$marc_record->append_fields($field); |
| 104 |
$controlnumber = GetMarcControlnumber( $marc_record, 'MARC21' ); |
| 105 |
is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' ); |
| 106 |
|
| 107 |
$field = $marc_record->field('001'); |
| 108 |
$field->update('123456789X'); |
| 109 |
$controlnumber = GetMarcControlnumber( $marc_record, 'MARC21' ); |
| 110 |
is( $controlnumber, '123456789X', 'GetMarcControlnumber handles records with 001' ); |
| 111 |
|
| 112 |
# clean up after ourselves |
| 113 |
DelBiblio($biblionumber); |
| 114 |
- |
|
|