Bug 14306: Follow-up for URLs in 555$u
[koha.git] / t / db_dependent / Exporter / Record.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 3;
21 use t::lib::TestBuilder;
22
23 use MARC::Record;
24 use MARC::File::USMARC;
25 use MARC::File::XML;
26 use MARC::Batch;
27 use File::Slurp;
28 use Encode;
29
30 use C4::Biblio;
31 use C4::Context;
32 use Koha::Database;
33 use Koha::Exporter::Record;
34
35 my $schema  = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37
38 my $dbh = C4::Context->dbh;
39
40 my $biblio_1_title = 'Silence in the library';
41 my $biblio_2_title = 'The art of computer programming ກ ຂ ຄ ງ ຈ ຊ ຍ é';
42 my $biblio_1 = MARC::Record->new();
43 $biblio_1->leader('00266nam a22001097a 4500');
44 $biblio_1->append_fields(
45     MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
46     MARC::Field->new('245', ' ', ' ', a => $biblio_1_title),
47 );
48 my ($biblionumber_1, $biblioitemnumber_1) = AddBiblio($biblio_1, '');
49 my $biblio_2 = MARC::Record->new();
50 $biblio_2->leader('00266nam a22001097a 4500');
51 $biblio_2->append_fields(
52     MARC::Field->new('100', ' ', ' ', a => 'Knuth, Donald Ervin'),
53     MARC::Field->new('245', ' ', ' ', a => $biblio_2_title),
54 );
55 my ($biblionumber_2, $biblioitemnumber_2) = AddBiblio($biblio_2, '');
56
57 my $builder = t::lib::TestBuilder->new;
58 my $item_1_1 = $builder->build({
59     source => 'Item',
60     value => {
61         biblionumber => $biblionumber_1,
62         more_subfields_xml => '',
63     }
64 });
65 my $item_1_2 = $builder->build({
66     source => 'Item',
67     value => {
68         biblionumber => $biblionumber_1,
69         more_subfields_xml => '',
70     }
71 });
72 my $item_2_1 = $builder->build({
73     source => 'Item',
74     value => {
75         biblionumber => $biblionumber_2,
76         more_subfields_xml => '',
77     }
78 });
79
80 subtest 'export csv' => sub {
81     plan tests => 2;
82     my $csv_content = q{Title=245$a|Barcode=952$p};
83     $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');
84     my $csv_profile_id = $dbh->last_insert_id( undef, undef, 'export_format', undef );
85     my $generated_csv_file = '/tmp/test_export_1.csv';
86
87     # Get all item infos
88     Koha::Exporter::Record::export(
89         {
90             record_type => 'bibs',
91             record_ids => [ $biblionumber_1, $biblionumber_2 ],
92             format => 'csv',
93             csv_profile_id => $csv_profile_id,
94             output_filepath => $generated_csv_file,
95         }
96     );
97     my $expected_csv = <<EOF;
98 Title|Barcode
99 "$biblio_1_title"|$item_1_1->{barcode},$item_1_2->{barcode}
100 "$biblio_2_title"|$item_2_1->{barcode}
101 EOF
102     my $generated_csv_content = read_file( $generated_csv_file );
103     is( $generated_csv_content, $expected_csv, "Export CSV: All item's infos should have been retrieved" );
104
105     $generated_csv_file = '/tmp/test_export.csv';
106     # Get only 1 item info
107     Koha::Exporter::Record::export(
108         {
109             record_type => 'bibs',
110             record_ids => [ $biblionumber_1, $biblionumber_2 ],
111             itemnumbers => [ $item_1_1->{itemnumber}, $item_2_1->{itemnumber} ],
112             format => 'csv',
113             csv_profile_id => $csv_profile_id,
114             output_filepath => $generated_csv_file,
115         }
116     );
117     $expected_csv = <<EOF;
118 Title|Barcode
119 "$biblio_1_title"|$item_1_1->{barcode}
120 "$biblio_2_title"|$item_2_1->{barcode}
121 EOF
122     $generated_csv_content = read_file( $generated_csv_file );
123     is( $generated_csv_content, $expected_csv, "Export CSV: Only 1 item info should have been retrieved" );
124 };
125
126 subtest 'export xml' => sub {
127     plan tests => 2;
128     my $generated_xml_file = '/tmp/test_export.xml';
129     Koha::Exporter::Record::export(
130         {
131             record_type => 'bibs',
132             record_ids => [ $biblionumber_1, $biblionumber_2 ],
133             format => 'xml',
134             output_filepath => $generated_xml_file,
135         }
136     );
137     my $generated_xml_content = read_file( $generated_xml_file );
138     $MARC::File::XML::_load_args{BinaryEncoding} = 'utf-8';
139     open my $fh, '<', $generated_xml_file;
140     my $records = MARC::Batch->new( 'XML', $fh );
141     my @records;
142     # The following statement produces
143     # Use of uninitialized value in concatenation (.) or string at /usr/share/perl5/MARC/File/XML.pm line 398, <$fh> chunk 5.
144     # Why?
145     while ( my $record = $records->next ) {
146         push @records, $record;
147     }
148     is( scalar( @records ), 2, 'Export XML: 2 records should have been exported' );
149     my $second_record = $records[1];
150     my $title = $second_record->subfield(245, 'a');
151     $title = Encode::encode('UTF-8', $title);
152     is( $title, $biblio_2_title, 'Export XML: The title is correctly encoded' );
153 };
154
155 subtest 'export iso2709' => sub {
156     plan tests => 2;
157     my $generated_mrc_file = '/tmp/test_export.mrc';
158     # Get all item infos
159     Koha::Exporter::Record::export(
160         {
161             record_type => 'bibs',
162             record_ids => [ $biblionumber_1, $biblionumber_2 ],
163             format => 'iso2709',
164             output_filepath => $generated_mrc_file,
165         }
166     );
167     my $records = MARC::File::USMARC->in( $generated_mrc_file );
168     my @records;
169     while ( my $record = $records->next ) {
170         push @records, $record;
171     }
172     is( scalar( @records ), 2, 'Export ISO2709: 2 records should have been exported' );
173     my $second_record = $records[1];
174     my $title = $second_record->subfield(245, 'a');
175     $title = Encode::encode('UTF-8', $title);
176     is( $title, $biblio_2_title, 'Export ISO2709: The title is correctly encoded' );
177 };
178
179 $schema->storage->txn_rollback;
180
181 1;