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
=> 4;
22 use t
::lib
::TestBuilder
;
25 use MARC
::File
::USMARC
;
36 use Koha
::Exporter
::Record
;
37 use Koha
::Biblio
::Metadatas
;
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 $schema->storage->txn_rollback;