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>.
20 use Test::More tests => 6;
22 use t::lib::TestBuilder;
25 use MARC::File::USMARC;
36 use Koha::Exporter::Record;
37 use Koha::Biblio::Metadata;
39 my $schema = Koha::Database->new->schema;
40 $schema->storage->txn_begin;
42 my $dbh = C4::Context->dbh;
44 my $biblio_1_title = 'Silence in the library';
45 my $biblio_2_title = 'The art of computer programming ກ ຂ ຄ ງ ຈ ຊ ຍ é';
46 my $biblio_1 = MARC::Record->new();
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),
52 my ($biblionumber_1, $biblioitemnumber_1) = AddBiblio($biblio_1, '');
53 my $biblio_2 = MARC::Record->new();
54 $biblio_2->leader('00266nam a22001097a 4500');
55 $biblio_2->append_fields(
56 MARC::Field->new('100', ' ', ' ', a => 'Knuth, Donald Ervin'),
57 MARC::Field->new('245', ' ', ' ', a => $biblio_2_title),
59 my ($biblionumber_2, $biblioitemnumber_2) = AddBiblio($biblio_2, '');
61 my $bad_biblio = Koha::Biblio->new()->store();
62 Koha::Biblio::Metadata->new( { biblionumber => $bad_biblio->id, format => 'marcxml', metadata => 'something wrong', marcflavour => C4::Context->preference('marcflavour') } )->store();
63 my $bad_biblionumber = $bad_biblio->id;
65 my $builder = t::lib::TestBuilder->new;
66 my $item_1_1 = $builder->build({
69 biblionumber => $biblionumber_1,
70 more_subfields_xml => '',
73 my $item_1_2 = $builder->build({
76 biblionumber => $biblionumber_1,
77 more_subfields_xml => '',
80 my $item_2_1 = $builder->build({
83 biblionumber => $biblionumber_2,
84 more_subfields_xml => '',
87 my $bad_item = $builder->build({
90 biblionumber => $bad_biblionumber,
91 more_subfields_xml => '',
95 subtest 'export csv' => sub {
97 my $csv_content = q{Title=245$a|Barcode=952$p};
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');
99 my $csv_profile_id = $dbh->last_insert_id( undef, undef, 'export_format', undef );
100 my $generated_csv_file = '/tmp/test_export_1.csv';
104 Koha::Exporter::Record::export(
105 { record_type => 'bibs',
106 record_ids => [ $biblionumber_1, $bad_biblionumber, $biblionumber_2 ],
108 csv_profile_id => $csv_profile_id,
109 output_filepath => $generated_csv_file,
113 qr|.*Start tag expected.*|, "Export csv with wrong marcxml should raise a warning";
114 my $expected_csv = <<EOF;
116 "$biblio_1_title"|$item_1_1->{barcode},$item_1_2->{barcode}
117 "$biblio_2_title"|$item_2_1->{barcode}
119 my $generated_csv_content = read_file( $generated_csv_file );
120 is( $generated_csv_content, $expected_csv, "Export CSV: All item's infos should have been retrieved" );
122 $generated_csv_file = '/tmp/test_export.csv';
123 # Get only 1 item info
124 Koha::Exporter::Record::export(
126 record_type => 'bibs',
127 record_ids => [ $biblionumber_1, $biblionumber_2 ],
128 itemnumbers => [ $item_1_1->{itemnumber}, $item_2_1->{itemnumber} ],
130 csv_profile_id => $csv_profile_id,
131 output_filepath => $generated_csv_file,
134 $expected_csv = <<EOF;
136 "$biblio_1_title"|$item_1_1->{barcode}
137 "$biblio_2_title"|$item_2_1->{barcode}
139 $generated_csv_content = read_file( $generated_csv_file );
140 is( $generated_csv_content, $expected_csv, "Export CSV: Only 1 item info should have been retrieved" );
143 subtest 'export xml' => sub {
145 my $generated_xml_file = '/tmp/test_export.xml';
147 Koha::Exporter::Record::export(
148 { record_type => 'bibs',
149 record_ids => [ $biblionumber_1, $bad_biblionumber, $biblionumber_2 ],
151 output_filepath => $generated_xml_file,
155 qr|.*Start tag expected.*|, "Export xml with wrong marcxml should raise a warning";
157 my $generated_xml_content = read_file( $generated_xml_file );
158 $MARC::File::XML::_load_args{BinaryEncoding} = 'utf-8';
159 open my $fh, '<', $generated_xml_file;
160 my $records = MARC::Batch->new( 'XML', $fh );
162 # The following statement produces
163 # Use of uninitialized value in concatenation (.) or string at /usr/share/perl5/MARC/File/XML.pm line 398, <$fh> chunk 5.
165 while ( my $record = $records->next ) {
166 push @records, $record;
168 is( scalar( @records ), 2, 'Export XML: 2 records should have been exported' );
169 my $second_record = $records[1];
170 my $title = $second_record->subfield(245, 'a');
171 $title = Encode::encode('UTF-8', $title);
172 is( $title, $biblio_2_title, 'Export XML: The title is correctly encoded' );
175 subtest 'export iso2709' => sub {
177 my $generated_mrc_file = '/tmp/test_export.mrc';
180 Koha::Exporter::Record::export(
181 { record_type => 'bibs',
182 record_ids => [ $biblionumber_1, $bad_biblionumber, $biblionumber_2 ],
184 output_filepath => $generated_mrc_file,
188 qr|.*Start tag expected.*|, "Export iso2709 with wrong marcxml should raise a warning";
190 my $records = MARC::File::USMARC->in( $generated_mrc_file );
192 while ( my $record = $records->next ) {
193 push @records, $record;
195 is( scalar( @records ), 2, 'Export ISO2709: 2 records should have been exported' );
196 my $second_record = $records[1];
197 my $title = $second_record->subfield(245, 'a');
198 $title = Encode::encode('UTF-8', $title);
199 is( $title, $biblio_2_title, 'Export ISO2709: The title is correctly encoded' );
202 subtest 'export without record_type' => sub {
205 my $rv = Koha::Exporter::Record::export({
206 record_ids => [ $biblionumber_1, $biblionumber_2 ],
208 output_filepath => 'does_not_matter_here',
210 is( $rv, undef, 'export returns undef' );
211 #Depending on your logger config, you might have a warn in your logs
214 subtest '_get_biblio_for_export' => sub {
217 my $biblio = MARC::Record->new();
218 $biblio->leader('00266nam a22001097a 4500');
219 $biblio->append_fields(
220 MARC::Field->new( '100', ' ', ' ', a => 'Thurber, James' ),
221 MARC::Field->new( '245', ' ', ' ', a => "The 13 Clocks" ),
223 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
224 my $branch_a = $builder->build({source => 'Branch',});
225 my $branch_b = $builder->build({source => 'Branch',});
226 my $item_branch_a = $builder->build(
230 biblionumber => $biblionumber,
231 homebranch => $branch_a->{branchcode},
232 more_subfields_xml => '',
236 my $item_branch_b = $builder->build(
240 biblionumber => $biblionumber,
241 homebranch => $branch_b->{branchcode},
242 more_subfields_xml => '',
247 my $record = Koha::Exporter::Record::_get_biblio_for_export(
249 biblionumber => $biblionumber,
251 only_export_items_for_branches => undef
254 my @items = $record->field('952');
255 is( scalar @items, 2, "We should retrieve all items if we don't pass specific branches and request items" );
257 $record = Koha::Exporter::Record::_get_biblio_for_export(
259 biblionumber => $biblionumber,
261 only_export_items_for_branches => [ $branch_b->{branchcode} ]
264 @items = $record->field('952');
265 is( scalar @items, 1, "We should retrieve only item for branch_b item if we request items and pass branch" );
267 $items[0]->subfield('a'),
268 $branch_b->{branchcode},
269 "And the homebranch for that item should be branch_b branchcode"
272 $record = Koha::Exporter::Record::_get_biblio_for_export(
274 biblionumber => $biblionumber,
276 only_export_items_for_branches => [ $branch_b->{branchcode} ]
279 @items = $record->field('952');
280 is( scalar @items, 0, "We should not have any items if we don't request items and pass a branch");
284 subtest '_get_record_for_export MARC field conditions' => sub {
287 my $biblio = MARC::Record->new();
288 $biblio->leader('00266nam a22001097a 4500');
289 $biblio->append_fields(
290 MARC::Field->new( '100', ' ', ' ', a => 'Thurber, James' ),
291 MARC::Field->new( '245', ' ', ' ', a => 'The 13 Clocks' ),
292 MARC::Field->new( '080', ' ', ' ', a => '12345' ),
293 MARC::Field->new( '035', ' ', ' ', a => '(TEST)123' ),
294 MARC::Field->new( '035', ' ', ' ', a => '(TEST)1234' ),
296 my ( $biblionumber ) = AddBiblio( $biblio, '' );
299 $record = Koha::Exporter::Record::_get_record_for_export(
301 record_id => $biblionumber,
302 record_conditions => [['080', 'a', '=', '12345']],
303 record_type => 'bibs',
306 ok( $record, "Record condition \"080a=12345\" should match" );
308 $record = Koha::Exporter::Record::_get_record_for_export(
310 record_id => $biblionumber,
311 record_conditions => [['080', 'a', '!=', '12345']],
312 record_type => 'bibs',
315 is( $record, undef, "Record condition \"080a!=12345\" should not match" );
317 $record = Koha::Exporter::Record::_get_record_for_export(
319 record_id => $biblionumber,
320 record_conditions => [['080', 'a', '>', '1234']],
321 record_type => 'bibs',
324 ok( $record, "Record condition \"080a>1234\" should match" );
326 $record = Koha::Exporter::Record::_get_record_for_export(
328 record_id => $biblionumber,
329 record_conditions => [['080', 'a', '<', '123456']],
330 record_type => 'bibs',
333 ok( $record, "Record condition \"080a<123456\" should match" );
335 $record = Koha::Exporter::Record::_get_record_for_export(
337 record_id => $biblionumber,
338 record_conditions => [['080', 'a', '>', '123456']],
339 record_type => 'bibs',
342 is( $record, undef, "Record condition \"080a>123456\" should not match" );
345 ## Multiple subfields
347 $record = Koha::Exporter::Record::_get_record_for_export(
349 record_id => $biblionumber,
350 record_conditions => [['035', 'a', '!=', 'TEST(12345)']],
351 record_type => 'bibs',
354 ok( $record, "Record condition \"035a!=TEST(12345)\" should match" );
356 $record = Koha::Exporter::Record::_get_record_for_export(
358 record_id => $biblionumber,
359 record_conditions => [['035', 'a', '=', 'TEST(1234)']],
360 record_type => 'bibs',
363 is( $record, undef, "Record condition \"035a=TEST(1234)\" should not match" ); # Since matching all subfields required
366 ## Multiple conditions
368 $record = Koha::Exporter::Record::_get_record_for_export(
370 record_id => $biblionumber,
371 record_conditions => [['035', 'a', '!=', 'TEST(12345)'], ['080', 'a', '>', '1234']],
372 record_type => 'bibs',
375 ok( $record, "Record condition \"035a!=TEST(12345),080a>1234\" should match" );
377 $record = Koha::Exporter::Record::_get_record_for_export(
379 record_id => $biblionumber,
380 record_conditions => [['035', 'a', '!=', 'TEST(12345)'], ['080', 'a', '<', '1234']],
381 record_type => 'bibs',
384 is( $record, undef, "Record condition \"035a!=TEST(12345),080a<1234\" should not match" );
389 $record = Koha::Exporter::Record::_get_record_for_export(
391 record_id => $biblionumber,
392 record_conditions => [['035', 'a', '?']],
393 record_type => 'bibs',
396 ok( $record, "Record condition \"exists(035a)\" should match" );
398 $record = Koha::Exporter::Record::_get_record_for_export(
400 record_id => $biblionumber,
401 record_conditions => [['035', 'a', '!?']],
402 record_type => 'bibs',
403 record_type => 'bibs',
406 is( $record, undef, "Record condition \"not_exists(035a)\" should not match" );
409 $schema->storage->txn_rollback;