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 94-100
my $bad_item = $builder->build({
Link Here
|
94 |
|
110 |
|
95 |
subtest 'export csv' => sub { |
111 |
subtest 'export csv' => sub { |
96 |
plan tests => 3; |
112 |
plan tests => 3; |
97 |
my $csv_content = q{Title=245$a|Barcode=952$p}; |
113 |
my $csv_content = qq{Title=$title_field\$$title_subfield|Barcode=$item_field\$$barcode_subfield}; |
98 |
$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'); |
114 |
$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'); |
99 |
my $csv_profile_id = $dbh->last_insert_id( undef, undef, 'export_format', undef ); |
115 |
my $csv_profile_id = $dbh->last_insert_id( undef, undef, 'export_format', undef ); |
100 |
my $generated_csv_file = '/tmp/test_export_1.csv'; |
116 |
my $generated_csv_file = '/tmp/test_export_1.csv'; |
Lines 167-173
subtest 'export xml' => sub {
Link Here
|
167 |
} |
183 |
} |
168 |
is( scalar( @records ), 2, 'Export XML: 2 records should have been exported' ); |
184 |
is( scalar( @records ), 2, 'Export XML: 2 records should have been exported' ); |
169 |
my $second_record = $records[1]; |
185 |
my $second_record = $records[1]; |
170 |
my $title = $second_record->subfield(245, 'a'); |
186 |
my $title = $second_record->subfield($title_field, $title_subfield); |
171 |
$title = Encode::encode('UTF-8', $title); |
187 |
$title = Encode::encode('UTF-8', $title); |
172 |
is( $title, $biblio_2_title, 'Export XML: The title is correctly encoded' ); |
188 |
is( $title, $biblio_2_title, 'Export XML: The title is correctly encoded' ); |
173 |
}; |
189 |
}; |
Lines 194-200
subtest 'export iso2709' => sub {
Link Here
|
194 |
} |
210 |
} |
195 |
is( scalar( @records ), 2, 'Export ISO2709: 2 records should have been exported' ); |
211 |
is( scalar( @records ), 2, 'Export ISO2709: 2 records should have been exported' ); |
196 |
my $second_record = $records[1]; |
212 |
my $second_record = $records[1]; |
197 |
my $title = $second_record->subfield(245, 'a'); |
213 |
my $title = $second_record->subfield($title_field, $title_subfield); |
198 |
$title = Encode::encode('UTF-8', $title); |
214 |
$title = Encode::encode('UTF-8', $title); |
199 |
is( $title, $biblio_2_title, 'Export ISO2709: The title is correctly encoded' ); |
215 |
is( $title, $biblio_2_title, 'Export ISO2709: The title is correctly encoded' ); |
200 |
}; |
216 |
}; |
Lines 216-225
subtest '_get_biblio_for_export' => sub {
Link Here
|
216 |
|
232 |
|
217 |
my $biblio = MARC::Record->new(); |
233 |
my $biblio = MARC::Record->new(); |
218 |
$biblio->leader('00266nam a22001097a 4500'); |
234 |
$biblio->leader('00266nam a22001097a 4500'); |
219 |
$biblio->append_fields( |
235 |
if (C4::Context->preference('marcflavour') eq 'UNIMARC') { |
220 |
MARC::Field->new( '100', ' ', ' ', a => 'Thurber, James' ), |
236 |
$biblio->append_fields( |
221 |
MARC::Field->new( '245', ' ', ' ', a => "The 13 Clocks" ), |
237 |
MARC::Field->new( '200', ' ', ' ', a => "The 13 Clocks", f => 'Thurber, James' ), |
222 |
); |
238 |
); |
|
|
239 |
} else { |
240 |
$biblio->append_fields( |
241 |
MARC::Field->new( '100', ' ', ' ', a => 'Thurber, James' ), |
242 |
MARC::Field->new( '245', ' ', ' ', a => "The 13 Clocks" ), |
243 |
); |
244 |
} |
223 |
my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' ); |
245 |
my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' ); |
224 |
my $branch_a = $builder->build({source => 'Branch',}); |
246 |
my $branch_a = $builder->build({source => 'Branch',}); |
225 |
my $branch_b = $builder->build({source => 'Branch',}); |
247 |
my $branch_b = $builder->build({source => 'Branch',}); |
Lines 251-257
subtest '_get_biblio_for_export' => sub {
Link Here
|
251 |
only_export_items_for_branches => undef |
273 |
only_export_items_for_branches => undef |
252 |
} |
274 |
} |
253 |
); |
275 |
); |
254 |
my @items = $record->field('952'); |
276 |
my @items = $record->field($item_field); |
255 |
is( scalar @items, 2, "We should retrieve all items if we don't pass specific branches and request items" ); |
277 |
is( scalar @items, 2, "We should retrieve all items if we don't pass specific branches and request items" ); |
256 |
|
278 |
|
257 |
$record = Koha::Exporter::Record::_get_biblio_for_export( |
279 |
$record = Koha::Exporter::Record::_get_biblio_for_export( |
Lines 261-270
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, 1, "We should retrieve only item for branch_b item if we request items and pass branch" ); |
287 |
is( scalar @items, 1, "We should retrieve only item for branch_b item if we request items and pass branch" ); |
266 |
is( |
288 |
is( |
267 |
$items[0]->subfield('a'), |
289 |
$items[0]->subfield($homebranch_subfield), |
268 |
$branch_b->{branchcode}, |
290 |
$branch_b->{branchcode}, |
269 |
"And the homebranch for that item should be branch_b branchcode" |
291 |
"And the homebranch for that item should be branch_b branchcode" |
270 |
); |
292 |
); |
Lines 276-282
subtest '_get_biblio_for_export' => sub {
Link Here
|
276 |
only_export_items_for_branches => [ $branch_b->{branchcode} ] |
298 |
only_export_items_for_branches => [ $branch_b->{branchcode} ] |
277 |
} |
299 |
} |
278 |
); |
300 |
); |
279 |
@items = $record->field('952'); |
301 |
@items = $record->field($item_field); |
280 |
is( scalar @items, 0, "We should not have any items if we don't request items and pass a branch"); |
302 |
is( scalar @items, 0, "We should not have any items if we don't request items and pass a branch"); |
281 |
|
303 |
|
282 |
}; |
304 |
}; |
Lines 287-294
subtest '_get_record_for_export MARC field conditions' => sub {
Link Here
|
287 |
my $biblio = MARC::Record->new(); |
309 |
my $biblio = MARC::Record->new(); |
288 |
$biblio->leader('00266nam a22001097a 4500'); |
310 |
$biblio->leader('00266nam a22001097a 4500'); |
289 |
$biblio->append_fields( |
311 |
$biblio->append_fields( |
290 |
MARC::Field->new( '100', ' ', ' ', a => 'Thurber, James' ), |
312 |
MARC::Field->new( '200', ' ', ' ', f => 'Thurber, James' ), |
291 |
MARC::Field->new( '245', ' ', ' ', a => 'The 13 Clocks' ), |
313 |
MARC::Field->new( '200', ' ', ' ', a => 'The 13 Clocks' ), |
292 |
MARC::Field->new( '080', ' ', ' ', a => '12345' ), |
314 |
MARC::Field->new( '080', ' ', ' ', a => '12345' ), |
293 |
MARC::Field->new( '035', ' ', ' ', a => '(TEST)123' ), |
315 |
MARC::Field->new( '035', ' ', ' ', a => '(TEST)123' ), |
294 |
MARC::Field->new( '035', ' ', ' ', a => '(TEST)1234' ), |
316 |
MARC::Field->new( '035', ' ', ' ', a => '(TEST)1234' ), |
Lines 406-409
subtest '_get_record_for_export MARC field conditions' => sub {
Link Here
|
406 |
is( $record, undef, "Record condition \"not_exists(035a)\" should not match" ); |
428 |
is( $record, undef, "Record condition \"not_exists(035a)\" should not match" ); |
407 |
}; |
429 |
}; |
408 |
|
430 |
|
|
|
431 |
subtest 'export csv with marc modification template' => sub { |
432 |
plan tests => 1; |
433 |
my $csv_content = qq{Title=$title_field\$$title_subfield|Test=999\$9}; |
434 |
$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'); |
435 |
my $csv_profile_id = $dbh->last_insert_id( undef, undef, 'export_format', undef ); |
436 |
|
437 |
my $template_id = C4::MarcModificationTemplates::AddModificationTemplate('test exporter'); |
438 |
C4::MarcModificationTemplates::AddModificationTemplateAction($template_id, 'add_field', 0, '999', '9', 'Foobar'); |
439 |
|
440 |
my $generated_csv_file = '/tmp/test_export_1.csv'; |
441 |
Koha::Exporter::Record::export({ |
442 |
record_type => 'bibs', |
443 |
record_ids => [ $biblionumber_1 ], |
444 |
format => 'csv', |
445 |
csv_profile_id => $csv_profile_id, |
446 |
marc_modification_template_id => $template_id, |
447 |
output_filepath => $generated_csv_file, |
448 |
}); |
449 |
|
450 |
my $expected_csv = <<EOF; |
451 |
Title|Test |
452 |
"$biblio_1_title"|Foobar |
453 |
EOF |
454 |
my $generated_csv_content = read_file( $generated_csv_file ); |
455 |
is( $generated_csv_content, $expected_csv, "Export CSV: Modification template should have been applied" ); |
456 |
}; |
457 |
|
458 |
subtest 'export xml with marc modification template' => sub { |
459 |
plan tests => 2; |
460 |
|
461 |
my $template_id = C4::MarcModificationTemplates::AddModificationTemplate('test exporter'); |
462 |
C4::MarcModificationTemplates::AddModificationTemplateAction($template_id, 'add_field', 0, '999', '9', 'Foobar'); |
463 |
|
464 |
my $generated_xml_file = '/tmp/test_export.xml'; |
465 |
Koha::Exporter::Record::export({ |
466 |
record_type => 'bibs', |
467 |
record_ids => [ $biblionumber_1 ], |
468 |
format => 'xml', |
469 |
marc_modification_template_id => $template_id, |
470 |
output_filepath => $generated_xml_file, |
471 |
}); |
472 |
|
473 |
my $generated_xml_content = read_file( $generated_xml_file ); |
474 |
$MARC::File::XML::_load_args{BinaryEncoding} = 'utf-8'; |
475 |
open my $fh, '<', $generated_xml_file; |
476 |
my $records = MARC::Batch->new( 'XML', $fh ); |
477 |
my @records; |
478 |
while ( my $record = $records->next ) { |
479 |
push @records, $record; |
480 |
} |
481 |
is( scalar( @records ), 1, 'Export XML: 1 record should have been exported' ); |
482 |
my $record = $records[0]; |
483 |
is( $record->subfield('999', '9'), 'Foobar', 'Export XML: marc modification template should have added 999$9' ); |
484 |
}; |
485 |
|
486 |
subtest 'export iso2709 with marc modification template' => sub { |
487 |
plan tests => 2; |
488 |
|
489 |
my $template_id = C4::MarcModificationTemplates::AddModificationTemplate('test exporter'); |
490 |
C4::MarcModificationTemplates::AddModificationTemplateAction($template_id, 'add_field', 0, '999', '9', 'Foobar'); |
491 |
|
492 |
my $generated_mrc_file = '/tmp/test_export.mrc'; |
493 |
Koha::Exporter::Record::export({ |
494 |
record_type => 'bibs', |
495 |
record_ids => [ $biblionumber_1 ], |
496 |
format => 'iso2709', |
497 |
marc_modification_template_id => $template_id, |
498 |
output_filepath => $generated_mrc_file, |
499 |
}); |
500 |
|
501 |
my $records = MARC::File::USMARC->in( $generated_mrc_file ); |
502 |
my @records; |
503 |
while ( my $record = $records->next ) { |
504 |
push @records, $record; |
505 |
} |
506 |
is( scalar( @records ), 1, 'Export ISO2709: 1 record should have been exported' ); |
507 |
my $record = $records[0]; |
508 |
is( $record->subfield('999', '9'), 'Foobar', 'Export ISO2709: marc modification template should have added 999$9' ); |
509 |
}; |
510 |
|
409 |
$schema->storage->txn_rollback; |
511 |
$schema->storage->txn_rollback; |