2 use Test::More tests => 3;
4 use MARC::File::USMARC;
5 use MARC::File::XML;# ( BinaryEncoding => 'utf-8' );
8 use t::lib::TestBuilder;
16 use Koha::Exporter::Record;
18 my $dbh = C4::Context->dbh;
19 #$dbh->{AutoCommit} = 0;
20 #$dbh->{RaiseError} = 1;
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|);
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),
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),
44 my ($biblionumber_2, $biblioitemnumber_2) = AddBiblio($biblio_2, '');
46 my $builder = t::lib::TestBuilder->new;
47 my $item_1_1 = $builder->build({
50 biblionumber => $biblionumber_1,
51 more_subfields_xml => '',
54 my $item_1_2 = $builder->build({
57 biblionumber => $biblionumber_1,
58 more_subfields_xml => '',
61 my $item_2_1 = $builder->build({
64 biblionumber => $biblionumber_2,
65 more_subfields_xml => '',
69 subtest 'export csv' => sub {
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';
77 Koha::Exporter::Record::export(
79 record_type => 'bibs',
80 record_ids => [ $biblionumber_1, $biblionumber_2 ],
82 csv_profile_id => $csv_profile_id,
83 output_filepath => $generated_csv_file,
86 my $expected_csv = <<EOF;
88 "$biblio_1_title"|$item_1_1->{barcode},$item_1_2->{barcode}
89 "$biblio_2_title"|$item_2_1->{barcode}
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" );
94 $generated_csv_file = '/tmp/test_export.csv';
95 # Get only 1 item info
96 Koha::Exporter::Record::export(
98 record_type => 'bibs',
99 record_ids => [ $biblionumber_1, $biblionumber_2 ],
100 itemnumbers => [ $item_1_1->{itemnumber}, $item_2_1->{itemnumber} ],
102 csv_profile_id => $csv_profile_id,
103 output_filepath => $generated_csv_file,
106 $expected_csv = <<EOF;
108 "$biblio_1_title"|$item_1_1->{barcode}
109 "$biblio_2_title"|$item_2_1->{barcode}
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" );
115 subtest 'export xml' => sub {
117 my $generated_xml_file = '/tmp/test_export.xml';
118 Koha::Exporter::Record::export(
120 record_type => 'bibs',
121 record_ids => [ $biblionumber_1, $biblionumber_2 ],
123 output_filepath => $generated_xml_file,
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 );
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.
134 while ( my $record = $records->next ) {
135 push @records, $record;
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' );
144 subtest 'export iso2709' => sub {
146 my $generated_mrc_file = '/tmp/test_export.mrc';
148 Koha::Exporter::Record::export(
150 record_type => 'bibs',
151 record_ids => [ $biblionumber_1, $biblionumber_2 ],
153 output_filepath => $generated_mrc_file,
156 my $records = MARC::File::USMARC->in( $generated_mrc_file );
158 while ( my $record = $records->next ) {
159 push @records, $record;
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' );