Bug 17088: Add tests to cover the changes
[koha.git] / t / db_dependent / Exporter / Record.t
blobcefd7d2b0dbf87b6dcda435b2c522d82ce08a972
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Test::More tests => 4;
21 use Test::Warn;
22 use t::lib::TestBuilder;
24 use MARC::Record;
25 use MARC::File::USMARC;
26 use MARC::File::XML;
27 use MARC::Batch;
28 use File::Slurp;
29 use Encode;
31 use C4::Biblio;
32 use C4::Context;
33 use Koha::Database;
34 use Koha::Exporter::Record;
36 my $schema = Koha::Database->new->schema;
37 $schema->storage->txn_begin;
39 my $dbh = C4::Context->dbh;
41 my $biblio_1_title = 'Silence in the library';
42 my $biblio_2_title = 'The art of computer programming ກ ຂ ຄ ງ ຈ ຊ ຍ é';
43 my $biblio_1 = MARC::Record->new();
44 $biblio_1->leader('00266nam a22001097a 4500');
45 $biblio_1->append_fields(
46 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
47 MARC::Field->new('245', ' ', ' ', a => $biblio_1_title),
49 my ($biblionumber_1, $biblioitemnumber_1) = AddBiblio($biblio_1, '');
50 my $biblio_2 = MARC::Record->new();
51 $biblio_2->leader('00266nam a22001097a 4500');
52 $biblio_2->append_fields(
53 MARC::Field->new('100', ' ', ' ', a => 'Knuth, Donald Ervin'),
54 MARC::Field->new('245', ' ', ' ', a => $biblio_2_title),
56 my ($biblionumber_2, $biblioitemnumber_2) = AddBiblio($biblio_2, '');
58 my ($bad_biblionumber, $bad_biblioitemnumber) = AddBiblio($biblio_1, '');
59 Koha::Biblioitems->find( $bad_biblionumber )->marcxml("something wrong")->store;
61 my $builder = t::lib::TestBuilder->new;
62 my $item_1_1 = $builder->build({
63 source => 'Item',
64 value => {
65 biblionumber => $biblionumber_1,
66 more_subfields_xml => '',
68 });
69 my $item_1_2 = $builder->build({
70 source => 'Item',
71 value => {
72 biblionumber => $biblionumber_1,
73 more_subfields_xml => '',
75 });
76 my $item_2_1 = $builder->build({
77 source => 'Item',
78 value => {
79 biblionumber => $biblionumber_2,
80 more_subfields_xml => '',
82 });
83 my $bad_item = $builder->build({
84 source => 'Item',
85 value => {
86 biblionumber => $bad_biblionumber,
87 more_subfields_xml => '',
89 });
91 subtest 'export csv' => sub {
92 plan tests => 3;
93 my $csv_content = q{Title=245$a|Barcode=952$p};
94 $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');
95 my $csv_profile_id = $dbh->last_insert_id( undef, undef, 'export_format', undef );
96 my $generated_csv_file = '/tmp/test_export_1.csv';
98 # Get all item infos
99 warning_like {
100 Koha::Exporter::Record::export(
101 { record_type => 'bibs',
102 record_ids => [ $biblionumber_1, $bad_biblionumber, $biblionumber_2 ],
103 format => 'csv',
104 csv_profile_id => $csv_profile_id,
105 output_filepath => $generated_csv_file,
109 qr|.*Start tag expected.*|, "Export csv with wrong marcxml should raise a warning";
110 my $expected_csv = <<EOF;
111 Title|Barcode
112 "$biblio_1_title"|$item_1_1->{barcode},$item_1_2->{barcode}
113 "$biblio_2_title"|$item_2_1->{barcode}
115 my $generated_csv_content = read_file( $generated_csv_file );
116 is( $generated_csv_content, $expected_csv, "Export CSV: All item's infos should have been retrieved" );
118 $generated_csv_file = '/tmp/test_export.csv';
119 # Get only 1 item info
120 Koha::Exporter::Record::export(
122 record_type => 'bibs',
123 record_ids => [ $biblionumber_1, $biblionumber_2 ],
124 itemnumbers => [ $item_1_1->{itemnumber}, $item_2_1->{itemnumber} ],
125 format => 'csv',
126 csv_profile_id => $csv_profile_id,
127 output_filepath => $generated_csv_file,
130 $expected_csv = <<EOF;
131 Title|Barcode
132 "$biblio_1_title"|$item_1_1->{barcode}
133 "$biblio_2_title"|$item_2_1->{barcode}
135 $generated_csv_content = read_file( $generated_csv_file );
136 is( $generated_csv_content, $expected_csv, "Export CSV: Only 1 item info should have been retrieved" );
139 subtest 'export xml' => sub {
140 plan tests => 3;
141 my $generated_xml_file = '/tmp/test_export.xml';
142 warning_like {
143 Koha::Exporter::Record::export(
144 { record_type => 'bibs',
145 record_ids => [ $biblionumber_1, $bad_biblionumber, $biblionumber_2 ],
146 format => 'xml',
147 output_filepath => $generated_xml_file,
151 qr|.*Start tag expected.*|, "Export xml with wrong marcxml should raise a warning";
153 my $generated_xml_content = read_file( $generated_xml_file );
154 $MARC::File::XML::_load_args{BinaryEncoding} = 'utf-8';
155 open my $fh, '<', $generated_xml_file;
156 my $records = MARC::Batch->new( 'XML', $fh );
157 my @records;
158 # The following statement produces
159 # Use of uninitialized value in concatenation (.) or string at /usr/share/perl5/MARC/File/XML.pm line 398, <$fh> chunk 5.
160 # Why?
161 while ( my $record = $records->next ) {
162 push @records, $record;
164 is( scalar( @records ), 2, 'Export XML: 2 records should have been exported' );
165 my $second_record = $records[1];
166 my $title = $second_record->subfield(245, 'a');
167 $title = Encode::encode('UTF-8', $title);
168 is( $title, $biblio_2_title, 'Export XML: The title is correctly encoded' );
171 subtest 'export iso2709' => sub {
172 plan tests => 3;
173 my $generated_mrc_file = '/tmp/test_export.mrc';
174 # Get all item infos
175 warning_like {
176 Koha::Exporter::Record::export(
177 { record_type => 'bibs',
178 record_ids => [ $biblionumber_1, $bad_biblionumber, $biblionumber_2 ],
179 format => 'iso2709',
180 output_filepath => $generated_mrc_file,
184 qr|.*Start tag expected.*|, "Export iso2709 with wrong marcxml should raise a warning";
186 my $records = MARC::File::USMARC->in( $generated_mrc_file );
187 my @records;
188 while ( my $record = $records->next ) {
189 push @records, $record;
191 is( scalar( @records ), 2, 'Export ISO2709: 2 records should have been exported' );
192 my $second_record = $records[1];
193 my $title = $second_record->subfield(245, 'a');
194 $title = Encode::encode('UTF-8', $title);
195 is( $title, $biblio_2_title, 'Export ISO2709: The title is correctly encoded' );
198 subtest 'export without record_type' => sub {
199 plan tests => 1;
201 my $rv = Koha::Exporter::Record::export({
202 record_ids => [ $biblionumber_1, $biblionumber_2 ],
203 format => 'iso2709',
204 output_filepath => 'does_not_matter_here',
206 is( $rv, undef, 'export returns undef' );
207 #Depending on your logger config, you might have a warn in your logs
210 $schema->storage->txn_rollback;