Bug 27569: Add tests
[koha.git] / t / db_dependent / ImportExportFramework.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 Test::More tests => 1;
21
22 use t::lib::TestBuilder;
23
24 use File::Basename qw( dirname );
25
26 use Koha::Database;
27 use Koha::BiblioFrameworks;
28 use Koha::MarcSubfieldStructures;
29 use C4::ImportExportFramework;
30
31 my $schema  = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
33
34 subtest 'ImportFramework() tests' => sub {
35
36     plan tests => 3;
37
38     subtest 'CSV tests' => sub {
39         plan tests => 15;
40
41         run_tests('csv');
42
43         run_csv_no_quoted();
44     };
45
46     subtest 'ODS tests' => sub {
47         plan tests => 9;
48
49         run_tests('ods');
50     };
51
52     subtest 'XML tests' => sub {
53         plan tests => 9;
54
55         run_tests('xml');
56     };
57 };
58
59 sub run_tests {
60
61     my ($format) = @_;
62
63     $schema->storage->txn_begin;
64
65     my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework.$format";
66     my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
67
68     my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
69     is( $result, 0, 'Import successful, no tags removed' );
70
71     my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
72     is( $nb_tags, 4, "4 tags should have been imported" );
73
74     my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
75     is( $nb_subfields, 12, "12 subfields should have been imported" );
76
77     # bad file tests
78     my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
79     $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id );
80
81     is( $result, -1, 'Bad file makes it return -1' );
82
83     $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count;
84     is( $nb_tags, 0, "0 tags should have been imported" );
85
86     $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count;
87     is( $nb_subfields, 0, "0 subfields should have been imported" );
88
89     # framework overwrite
90     $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_smaller.$format";
91
92     $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
93     is( $result, 5, 'Smaller fw import successful, 4 tags removed' );
94
95     $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
96     is( $nb_tags, 3, "3 tags should have been imported" );
97
98     $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
99     is( $nb_subfields, 8, "8 subfields should have been imported" );
100
101     $schema->storage->txn_rollback;
102 };
103
104 sub run_csv_no_quoted {
105
106     $schema->storage->txn_begin;
107
108     my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_no_quoted.csv";
109     my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
110
111     my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
112     is( $result, 0, 'Import successful, no tags removed' );
113
114     my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
115     is( $nb_tags, 4, "4 tags should have been imported" );
116
117     my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
118     is( $nb_subfields, 12, "12 subfields should have been imported" );
119
120     # bad file tests
121     my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
122     $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id );
123
124     is( $result, -1, 'Bad file makes it return -1' );
125
126     $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count;
127     is( $nb_tags, 0, "0 tags should have been imported" );
128
129     $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count;
130     is( $nb_subfields, 0, "0 subfields should have been imported" );
131
132     $schema->storage->txn_rollback;
133 };