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