Bug 15081: (followup) Make test files using TestBuilder handle their transactions
[koha.git] / t / db_dependent / Exporter / Record.t
blob72b8c9f42c6e3a8553edb5cff500ebbea682da81
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 => 3;
21 use t::lib::TestBuilder;
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;
30 use C4::Biblio;
31 use C4::Context;
32 use Koha::Database;
33 use Koha::Exporter::Record;
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
38 my $dbh = C4::Context->dbh;
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),
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),
55 my ($biblionumber_2, $biblioitemnumber_2) = AddBiblio($biblio_2, '');
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 => '',
64 });
65 my $item_1_2 = $builder->build({
66 source => 'Item',
67 value => {
68 biblionumber => $biblionumber_1,
69 more_subfields_xml => '',
71 });
72 my $item_2_1 = $builder->build({
73 source => 'Item',
74 value => {
75 biblionumber => $biblionumber_2,
76 more_subfields_xml => '',
78 });
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';
87 # Get all item infos
88 Koha::Exporter::Record::export(
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,
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}
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" );
105 $generated_csv_file = '/tmp/test_export.csv';
106 # Get only 1 item info
107 Koha::Exporter::Record::export(
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,
117 $expected_csv = <<EOF;
118 Title|Barcode
119 "$biblio_1_title"|$item_1_1->{barcode}
120 "$biblio_2_title"|$item_2_1->{barcode}
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" );
126 subtest 'export xml' => sub {
127 plan tests => 2;
128 my $generated_xml_file = '/tmp/test_export.xml';
129 Koha::Exporter::Record::export(
131 record_type => 'bibs',
132 record_ids => [ $biblionumber_1, $biblionumber_2 ],
133 format => 'xml',
134 output_filepath => $generated_xml_file,
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;
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' );
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(
161 record_type => 'bibs',
162 record_ids => [ $biblionumber_1, $biblionumber_2 ],
163 format => 'iso2709',
164 output_filepath => $generated_mrc_file,
167 my $records = MARC::File::USMARC->in( $generated_mrc_file );
168 my @records;
169 while ( my $record = $records->next ) {
170 push @records, $record;
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' );
179 $schema->storage->txn_rollback;