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>.
20 use File
::Temp qw
|tempfile
|;
24 use Test
::More tests
=> 3;
28 use_ok
('C4::ImportBatch');
31 t
::lib
::Mocks
::mock_preference
('marcflavour', 'MARC21');
33 subtest
'RecordsFromISO2709File' => sub {
36 my ( $errors, $recs );
37 my $file = create_file
({ whitespace
=> 1, format
=> 'marc' });
38 ( $errors, $recs ) = C4
::ImportBatch
::RecordsFromISO2709File
( $file, 'biblio', 'UTF-8' );
39 is
( @
$recs, 0, 'No records from empty marc file' );
41 $file = create_file
({ garbage
=> 1, format
=> 'marc' });
42 ( $errors, $recs ) = C4
::ImportBatch
::RecordsFromISO2709File
( $file, 'biblio', 'UTF-8' );
43 is
( @
$recs, 1, 'Garbage returns one record' );
44 my @fields = @
$recs?
$recs->[0]->fields: ();
45 is
( @fields, 0, 'That is an empty record' );
47 $file = create_file
({ two
=> 1, format
=> 'marc' });
48 ( $errors, $recs ) = C4
::ImportBatch
::RecordsFromISO2709File
( $file, 'biblio', 'UTF-8' );
49 is
( @
$recs, 2, 'File contains 2 records' );
53 subtest
'RecordsFromMARCXMLFile' => sub {
56 my ( $errors, $recs );
57 my $file = create_file
({ whitespace
=> 1, format
=> 'marcxml' });
58 ( $errors, $recs ) = C4
::ImportBatch
::RecordsFromMARCXMLFile
( $file, 'UTF-8' );
59 is
( @
$recs, 0, 'No records from empty marcxml file' );
61 $file = create_file
({ garbage
=> 1, format
=> 'marcxml' });
62 ( $errors, $recs ) = C4
::ImportBatch
::RecordsFromMARCXMLFile
( $file, 'UTF-8' );
63 is
( @
$recs, 0, 'Garbage returns no records' );
65 $file = create_file
({ two
=> 1, format
=> 'marcxml' });
66 ( $errors, $recs ) = C4
::ImportBatch
::RecordsFromMARCXMLFile
( $file, 'UTF-8' );
67 is
( @
$recs, 2, 'File has two records' );
73 my ( $fh, $name ) = tempfile
( SUFFIX
=> '.' . $params->{format
} );
74 if( $params->{garbage
} ) {
75 print $fh "Just some garbage\n\nAnd another line";
76 } elsif( $params->{whitespace
} ) {
78 } elsif ( $params->{two
} ) {
79 my $rec1 = MARC
::Record
->new;
80 my $rec2 = MARC
::Record
->new;
81 my $fld1 = MARC
::Field
->new('245','','','a','Title1');
82 my $fld2 = MARC
::Field
->new('245','','','a','Title2');
83 $rec1->append_fields( $fld1 );
84 $rec2->append_fields( $fld2 );
85 if( $params->{format
} eq 'marcxml' ) {
86 my $str = $rec1->as_xml;
87 # remove ending collection tag
88 $str =~ s/<\/collection>//;
90 $str = $rec2->as_xml_record; # no collection tag
91 # remove <?xml> line from 2nd record, add collection
92 $str =~ s/<\?xml.*\n//;
93 $str .= '</collection>';
96 print $fh $rec1->as_formatted, "\x1D", $rec2->as_formatted;