Lines 17-23
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 6; |
20 |
use Test::More tests => 9; |
21 |
use Test::Warn; |
21 |
use Test::Warn; |
22 |
use t::lib::TestBuilder; |
22 |
use t::lib::TestBuilder; |
23 |
|
23 |
|
Lines 45-61
my $biblio_1_title = 'Silence in the library';
Link Here
|
45 |
my $biblio_2_title = 'The art of computer programming ກ ຂ ຄ ງ ຈ ຊ ຍ é'; |
45 |
my $biblio_2_title = 'The art of computer programming ກ ຂ ຄ ງ ຈ ຊ ຍ é'; |
46 |
my $biblio_1 = MARC::Record->new(); |
46 |
my $biblio_1 = MARC::Record->new(); |
47 |
$biblio_1->leader('00266nam a22001097a 4500'); |
47 |
$biblio_1->leader('00266nam a22001097a 4500'); |
48 |
$biblio_1->append_fields( |
|
|
49 |
MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), |
50 |
MARC::Field->new('245', ' ', ' ', a => $biblio_1_title), |
51 |
); |
52 |
my ($biblionumber_1, $biblioitemnumber_1) = AddBiblio($biblio_1, ''); |
53 |
my $biblio_2 = MARC::Record->new(); |
48 |
my $biblio_2 = MARC::Record->new(); |
54 |
$biblio_2->leader('00266nam a22001097a 4500'); |
49 |
$biblio_2->leader('00266nam a22001097a 4500'); |
55 |
$biblio_2->append_fields( |
50 |
|
56 |
MARC::Field->new('100', ' ', ' ', a => 'Knuth, Donald Ervin'), |
51 |
my ($title_field, $title_subfield, $item_field, $barcode_subfield, $homebranch_subfield); |
57 |
MARC::Field->new('245', ' ', ' ', a => $biblio_2_title), |
52 |
if (C4::Context->preference('marcflavour') eq 'UNIMARC') { |
58 |
); |
53 |
$biblio_1->append_fields( |
|
|
54 |
MARC::Field->new('200', ' ', ' ', a => $biblio_1_title, f => 'Moffat, Steven'), |
55 |
); |
56 |
$biblio_2->append_fields( |
57 |
MARC::Field->new('200', ' ', ' ', a => $biblio_2_title, f => 'Knuth, Donald Ervin'), |
58 |
); |
59 |
($title_field, $title_subfield) = ('200', 'a'); |
60 |
($item_field, $barcode_subfield, $homebranch_subfield) = ('995', 'f', 'b'); |
61 |
} else { |
62 |
$biblio_1->append_fields( |
63 |
MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), |
64 |
MARC::Field->new('245', ' ', ' ', a => $biblio_1_title), |
65 |
); |
66 |
$biblio_2->append_fields( |
67 |
MARC::Field->new('100', ' ', ' ', a => 'Knuth, Donald Ervin'), |
68 |
MARC::Field->new('245', ' ', ' ', a => $biblio_2_title), |
69 |
); |
70 |
($title_field, $title_subfield) = ('245', 'a'); |
71 |
($item_field, $barcode_subfield, $homebranch_subfield) = ('952', 'p', 'a'); |
72 |
} |
73 |
|
74 |
my ($biblionumber_1, $biblioitemnumber_1) = AddBiblio($biblio_1, ''); |
59 |
my ($biblionumber_2, $biblioitemnumber_2) = AddBiblio($biblio_2, ''); |
75 |
my ($biblionumber_2, $biblioitemnumber_2) = AddBiblio($biblio_2, ''); |
60 |
|
76 |
|
61 |
my $bad_biblio = Koha::Biblio->new()->store(); |
77 |
my $bad_biblio = Koha::Biblio->new()->store(); |
Lines 87-93
my $bad_item = $builder->build({ # Cannot call build_sample_item, we want incons
Link Here
|
87 |
|
103 |
|
88 |
subtest 'export csv' => sub { |
104 |
subtest 'export csv' => sub { |
89 |
plan tests => 3; |
105 |
plan tests => 3; |
90 |
my $csv_content = q{Title=245$a|Barcode=952$p}; |
106 |
my $csv_content = qq{Title=$title_field\$$title_subfield|Barcode=$item_field\$$barcode_subfield}; |
91 |
$dbh->do(q|INSERT INTO export_format(profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)|, {}, "TEST_PROFILE_Records.t", "my useless desc", $csv_content, '|', ';', ',', 'utf8', 'marc'); |
107 |
$dbh->do(q|INSERT INTO export_format(profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)|, {}, "TEST_PROFILE_Records.t", "my useless desc", $csv_content, '|', ';', ',', 'utf8', 'marc'); |
92 |
my $csv_profile_id = $dbh->last_insert_id( undef, undef, 'export_format', undef ); |
108 |
my $csv_profile_id = $dbh->last_insert_id( undef, undef, 'export_format', undef ); |
93 |
my $generated_csv_file = '/tmp/test_export_1.csv'; |
109 |
my $generated_csv_file = '/tmp/test_export_1.csv'; |
Lines 160-166
subtest 'export xml' => sub {
Link Here
|
160 |
} |
176 |
} |
161 |
is( scalar( @records ), 2, 'Export XML: 2 records should have been exported' ); |
177 |
is( scalar( @records ), 2, 'Export XML: 2 records should have been exported' ); |
162 |
my $second_record = $records[1]; |
178 |
my $second_record = $records[1]; |
163 |
my $title = $second_record->subfield(245, 'a'); |
179 |
my $title = $second_record->subfield($title_field, $title_subfield); |
164 |
$title = Encode::encode('UTF-8', $title); |
180 |
$title = Encode::encode('UTF-8', $title); |
165 |
is( $title, $biblio_2_title, 'Export XML: The title is correctly encoded' ); |
181 |
is( $title, $biblio_2_title, 'Export XML: The title is correctly encoded' ); |
166 |
}; |
182 |
}; |
Lines 187-193
subtest 'export iso2709' => sub {
Link Here
|
187 |
} |
203 |
} |
188 |
is( scalar( @records ), 2, 'Export ISO2709: 2 records should have been exported' ); |
204 |
is( scalar( @records ), 2, 'Export ISO2709: 2 records should have been exported' ); |
189 |
my $second_record = $records[1]; |
205 |
my $second_record = $records[1]; |
190 |
my $title = $second_record->subfield(245, 'a'); |
206 |
my $title = $second_record->subfield($title_field, $title_subfield); |
191 |
$title = Encode::encode('UTF-8', $title); |
207 |
$title = Encode::encode('UTF-8', $title); |
192 |
is( $title, $biblio_2_title, 'Export ISO2709: The title is correctly encoded' ); |
208 |
is( $title, $biblio_2_title, 'Export ISO2709: The title is correctly encoded' ); |
193 |
}; |
209 |
}; |
Lines 209-218
subtest '_get_biblio_for_export' => sub {
Link Here
|
209 |
|
225 |
|
210 |
my $biblio = MARC::Record->new(); |
226 |
my $biblio = MARC::Record->new(); |
211 |
$biblio->leader('00266nam a22001097a 4500'); |
227 |
$biblio->leader('00266nam a22001097a 4500'); |
212 |
$biblio->append_fields( |
228 |
if (C4::Context->preference('marcflavour') eq 'UNIMARC') { |
213 |
MARC::Field->new( '100', ' ', ' ', a => 'Thurber, James' ), |
229 |
$biblio->append_fields( |
214 |
MARC::Field->new( '245', ' ', ' ', a => "The 13 Clocks" ), |
230 |
MARC::Field->new( '200', ' ', ' ', a => "The 13 Clocks", f => 'Thurber, James' ), |
215 |
); |
231 |
); |
|
|
232 |
} else { |
233 |
$biblio->append_fields( |
234 |
MARC::Field->new( '100', ' ', ' ', a => 'Thurber, James' ), |
235 |
MARC::Field->new( '245', ' ', ' ', a => "The 13 Clocks" ), |
236 |
); |
237 |
} |
216 |
my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' ); |
238 |
my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' ); |
217 |
my $branch_a = $builder->build({source => 'Branch',}); |
239 |
my $branch_a = $builder->build({source => 'Branch',}); |
218 |
my $branch_b = $builder->build({source => 'Branch',}); |
240 |
my $branch_b = $builder->build({source => 'Branch',}); |
Lines 236-242
subtest '_get_biblio_for_export' => sub {
Link Here
|
236 |
only_export_items_for_branches => undef |
258 |
only_export_items_for_branches => undef |
237 |
} |
259 |
} |
238 |
); |
260 |
); |
239 |
my @items = $record->field('952'); |
261 |
my @items = $record->field($item_field); |
240 |
is( scalar @items, 2, "We should retrieve all items if we don't pass specific branches and request items" ); |
262 |
is( scalar @items, 2, "We should retrieve all items if we don't pass specific branches and request items" ); |
241 |
|
263 |
|
242 |
$record = Koha::Exporter::Record::_get_biblio_for_export( |
264 |
$record = Koha::Exporter::Record::_get_biblio_for_export( |
Lines 246-255
subtest '_get_biblio_for_export' => sub {
Link Here
|
246 |
only_export_items_for_branches => [ $branch_b->{branchcode} ] |
268 |
only_export_items_for_branches => [ $branch_b->{branchcode} ] |
247 |
} |
269 |
} |
248 |
); |
270 |
); |
249 |
@items = $record->field('952'); |
271 |
@items = $record->field($item_field); |
250 |
is( scalar @items, 1, "We should retrieve only item for branch_b item if we request items and pass branch" ); |
272 |
is( scalar @items, 1, "We should retrieve only item for branch_b item if we request items and pass branch" ); |
251 |
is( |
273 |
is( |
252 |
$items[0]->subfield('a'), |
274 |
$items[0]->subfield($homebranch_subfield), |
253 |
$branch_b->{branchcode}, |
275 |
$branch_b->{branchcode}, |
254 |
"And the homebranch for that item should be branch_b branchcode" |
276 |
"And the homebranch for that item should be branch_b branchcode" |
255 |
); |
277 |
); |
Lines 261-267
subtest '_get_biblio_for_export' => sub {
Link Here
|
261 |
only_export_items_for_branches => [ $branch_b->{branchcode} ] |
283 |
only_export_items_for_branches => [ $branch_b->{branchcode} ] |
262 |
} |
284 |
} |
263 |
); |
285 |
); |
264 |
@items = $record->field('952'); |
286 |
@items = $record->field($item_field); |
265 |
is( scalar @items, 0, "We should not have any items if we don't request items and pass a branch"); |
287 |
is( scalar @items, 0, "We should not have any items if we don't request items and pass a branch"); |
266 |
|
288 |
|
267 |
}; |
289 |
}; |
Lines 272-279
subtest '_get_record_for_export MARC field conditions' => sub {
Link Here
|
272 |
my $biblio = MARC::Record->new(); |
294 |
my $biblio = MARC::Record->new(); |
273 |
$biblio->leader('00266nam a22001097a 4500'); |
295 |
$biblio->leader('00266nam a22001097a 4500'); |
274 |
$biblio->append_fields( |
296 |
$biblio->append_fields( |
275 |
MARC::Field->new( '100', ' ', ' ', a => 'Thurber, James' ), |
297 |
MARC::Field->new( '200', ' ', ' ', f => 'Thurber, James' ), |
276 |
MARC::Field->new( '245', ' ', ' ', a => 'The 13 Clocks' ), |
298 |
MARC::Field->new( '200', ' ', ' ', a => 'The 13 Clocks' ), |
277 |
MARC::Field->new( '080', ' ', ' ', a => '12345' ), |
299 |
MARC::Field->new( '080', ' ', ' ', a => '12345' ), |
278 |
MARC::Field->new( '035', ' ', ' ', a => '(TEST)123' ), |
300 |
MARC::Field->new( '035', ' ', ' ', a => '(TEST)123' ), |
279 |
MARC::Field->new( '035', ' ', ' ', a => '(TEST)1234' ), |
301 |
MARC::Field->new( '035', ' ', ' ', a => '(TEST)1234' ), |
Lines 391-394
subtest '_get_record_for_export MARC field conditions' => sub {
Link Here
|
391 |
is( $record, undef, "Record condition \"not_exists(035a)\" should not match" ); |
413 |
is( $record, undef, "Record condition \"not_exists(035a)\" should not match" ); |
392 |
}; |
414 |
}; |
393 |
|
415 |
|
|
|
416 |
subtest 'export csv with marc modification template' => sub { |
417 |
plan tests => 1; |
418 |
my $csv_content = qq{Title=$title_field\$$title_subfield|Test=999\$9}; |
419 |
$dbh->do(q|INSERT INTO export_format(profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)|, {}, "TEST_PROFILE_Records.t", "my useless desc", $csv_content, '|', ';', ',', 'utf8', 'marc'); |
420 |
my $csv_profile_id = $dbh->last_insert_id( undef, undef, 'export_format', undef ); |
421 |
|
422 |
my $template_id = C4::MarcModificationTemplates::AddModificationTemplate('test exporter'); |
423 |
C4::MarcModificationTemplates::AddModificationTemplateAction($template_id, 'add_field', 0, '999', '9', 'Foobar'); |
424 |
|
425 |
my $generated_csv_file = '/tmp/test_export_1.csv'; |
426 |
Koha::Exporter::Record::export({ |
427 |
record_type => 'bibs', |
428 |
record_ids => [ $biblionumber_1 ], |
429 |
format => 'csv', |
430 |
csv_profile_id => $csv_profile_id, |
431 |
marc_modification_template_id => $template_id, |
432 |
output_filepath => $generated_csv_file, |
433 |
}); |
434 |
|
435 |
my $expected_csv = <<EOF; |
436 |
Title|Test |
437 |
"$biblio_1_title"|Foobar |
438 |
EOF |
439 |
my $generated_csv_content = read_file( $generated_csv_file ); |
440 |
is( $generated_csv_content, $expected_csv, "Export CSV: Modification template should have been applied" ); |
441 |
}; |
442 |
|
443 |
subtest 'export xml with marc modification template' => sub { |
444 |
plan tests => 2; |
445 |
|
446 |
my $template_id = C4::MarcModificationTemplates::AddModificationTemplate('test exporter'); |
447 |
C4::MarcModificationTemplates::AddModificationTemplateAction($template_id, 'add_field', 0, '999', '9', 'Foobar'); |
448 |
|
449 |
my $generated_xml_file = '/tmp/test_export.xml'; |
450 |
Koha::Exporter::Record::export({ |
451 |
record_type => 'bibs', |
452 |
record_ids => [ $biblionumber_1 ], |
453 |
format => 'xml', |
454 |
marc_modification_template_id => $template_id, |
455 |
output_filepath => $generated_xml_file, |
456 |
}); |
457 |
|
458 |
my $generated_xml_content = read_file( $generated_xml_file ); |
459 |
$MARC::File::XML::_load_args{BinaryEncoding} = 'utf-8'; |
460 |
open my $fh, '<', $generated_xml_file; |
461 |
my $records = MARC::Batch->new( 'XML', $fh ); |
462 |
my @records; |
463 |
while ( my $record = $records->next ) { |
464 |
push @records, $record; |
465 |
} |
466 |
is( scalar( @records ), 1, 'Export XML: 1 record should have been exported' ); |
467 |
my $record = $records[0]; |
468 |
is( $record->subfield('999', '9'), 'Foobar', 'Export XML: marc modification template should have added 999$9' ); |
469 |
}; |
470 |
|
471 |
subtest 'export iso2709 with marc modification template' => sub { |
472 |
plan tests => 2; |
473 |
|
474 |
my $template_id = C4::MarcModificationTemplates::AddModificationTemplate('test exporter'); |
475 |
C4::MarcModificationTemplates::AddModificationTemplateAction($template_id, 'add_field', 0, '999', '9', 'Foobar'); |
476 |
|
477 |
my $generated_mrc_file = '/tmp/test_export.mrc'; |
478 |
Koha::Exporter::Record::export({ |
479 |
record_type => 'bibs', |
480 |
record_ids => [ $biblionumber_1 ], |
481 |
format => 'iso2709', |
482 |
marc_modification_template_id => $template_id, |
483 |
output_filepath => $generated_mrc_file, |
484 |
}); |
485 |
|
486 |
my $records = MARC::File::USMARC->in( $generated_mrc_file ); |
487 |
my @records; |
488 |
while ( my $record = $records->next ) { |
489 |
push @records, $record; |
490 |
} |
491 |
is( scalar( @records ), 1, 'Export ISO2709: 1 record should have been exported' ); |
492 |
my $record = $records[0]; |
493 |
is( $record->subfield('999', '9'), 'Foobar', 'Export ISO2709: marc modification template should have added 999$9' ); |
494 |
}; |
495 |
|
394 |
$schema->storage->txn_rollback; |
496 |
$schema->storage->txn_rollback; |