Bug 19230: Preventing warn when deleting course
[koha.git] / t / ImportBatch.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 File::Temp qw|tempfile|;
21 use MARC::Field;
22 use MARC::File::XML;
23 use MARC::Record;
24 use Test::More tests => 3;
25 use t::lib::Mocks;
26
27 BEGIN {
28     use_ok('C4::ImportBatch');
29 }
30
31 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
32
33 subtest 'RecordsFromISO2709File' => sub {
34     plan tests => 4;
35
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' );
40
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' );
46
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' );
50
51 };
52
53 subtest 'RecordsFromMARCXMLFile' => sub {
54     plan tests => 3;
55
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' );
60
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' );
64
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' );
68
69 };
70
71 sub create_file {
72     my ( $params ) = @_;
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} ) {
77         print $fh "  ";
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>//;
89             print $fh $str;
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>';
94             print $fh $str;
95         } else {
96             print $fh $rec1->as_formatted, "\x1D", $rec2->as_formatted;
97         }
98     }
99     close $fh;
100     return $name;
101 }