Bug 31183: Unit 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 qw( ImportFramework ExportFramework );
30 use Koha::Authority::Types;
31 use Koha::Authority::Subfields;
32
33 my $schema  = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'ImportFramework() tests' => sub {
37
38     plan tests => 6;
39
40     subtest 'CSV tests for biblio frameworks' => sub {
41         plan tests => 15;
42
43         run_biblio_tests('csv');
44
45         run_csv_no_quoted();
46     };
47
48     subtest 'ODS tests for biblio frameworks' => sub {
49         plan tests => 15;
50
51         run_biblio_tests('ods');
52
53         run_ods_new();
54     };
55
56     subtest 'XML tests for biblio frameworks' => sub {
57         plan tests => 9;
58
59         run_biblio_tests('xml');
60     };
61
62     subtest 'CSV tests for auth frameworks' => sub {
63         plan tests => 9;
64
65         run_auth_tests('csv');
66     };
67
68     subtest 'ODS tests for auth frameworks' => sub {
69         plan tests => 9;
70
71         run_auth_tests('ods');
72     };
73
74     subtest 'XML tests for auth frameworks' => sub {
75         plan tests => 9;
76
77         run_auth_tests('xml');
78     };
79
80 };
81
82 sub run_biblio_tests {
83
84     my ($format) = @_;
85     my $type = "biblio";
86
87     $schema->storage->txn_begin;
88
89     my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework.$format";
90     my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
91
92     my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id, 0, $type );
93     is( $result, 0, 'Import successful, no tags removed' );
94
95     my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
96     is( $nb_tags, 4, "4 tags should have been imported" );
97
98     my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
99     is( $nb_subfields, 12, "12 subfields should have been imported" );
100
101     # bad file tests
102     my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
103     $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id, 0, $type );
104
105     is( $result, -1, 'Bad file makes it return -1' );
106
107     $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count;
108     is( $nb_tags, 0, "0 tags should have been imported" );
109
110     $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count;
111     is( $nb_subfields, 0, "0 subfields should have been imported" );
112
113     # framework overwrite
114     $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_smaller.$format";
115
116     $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id, 0, $type );
117     is( $result, 5, 'Smaller fw import successful, 4 tags removed' );
118
119     $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
120     is( $nb_tags, 3, "3 tags should have been imported" );
121
122     $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
123     is( $nb_subfields, 8, "8 subfields should have been imported" );
124
125     $schema->storage->txn_rollback;
126 };
127
128 sub run_csv_no_quoted {
129
130     $schema->storage->txn_begin;
131
132     my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_no_quoted.csv";
133     my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
134
135     my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
136     is( $result, 0, 'Import successful, no tags removed' );
137
138     my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
139     is( $nb_tags, 4, "4 tags should have been imported" );
140
141     my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
142     is( $nb_subfields, 12, "12 subfields should have been imported" );
143
144     # bad file tests
145     my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
146     $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id );
147
148     is( $result, -1, 'Bad file makes it return -1' );
149
150     $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count;
151     is( $nb_tags, 0, "0 tags should have been imported" );
152
153     $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count;
154     is( $nb_subfields, 0, "0 subfields should have been imported" );
155
156     $schema->storage->txn_rollback;
157 };
158
159 sub run_ods_new {
160
161     my ($format) = @_;
162
163     $schema->storage->txn_begin;
164
165     my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_new.ods";
166     my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
167
168     my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
169     is( $result, 0, 'Import successful, no tags removed' );
170
171     my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
172     is( $nb_tags, 5, "5 tags should have been imported" );
173
174     my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
175     is( $nb_subfields, 16, "16 subfields should have been imported" );
176
177     # bad file tests
178     my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
179     $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id );
180
181     is( $result, -1, 'Bad file makes it return -1' );
182
183     $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count;
184     is( $nb_tags, 0, "0 tags should have been imported" );
185
186     $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count;
187     is( $nb_subfields, 0, "0 subfields should have been imported" );
188
189
190     $schema->storage->txn_rollback;
191 };
192
193 sub run_auth_tests {
194
195     my ($format) = @_;
196     my $type = "authority";
197
198     $schema->storage->txn_begin;
199
200     my $data_filepath = dirname(__FILE__) . "/data/frameworks/auth_type.$format";
201     my $fw_1 = $builder->build_object({ class => 'Koha::Authority::Types' });
202
203     my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id, '', $type );
204     is( $result, 0, 'Import successful, no tags removed' );
205
206     my $nb_tags = $schema->resultset('AuthTagStructure')->search({ authtypecode => $fw_1->id })->count;
207     is( $nb_tags, 6, "6 tags should have been imported" );
208
209     my $nb_subfields = Koha::Authority::Subfields->search({ authtypecode => $fw_1->id })->count;
210     is( $nb_subfields, 8, "8 subfields should have been imported" );
211
212     # bad file tests
213     my $fw_2 = $builder->build_object({ class => 'Koha::Authority::Types' });
214     $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id, '', $type );
215
216     is( $result, -1, 'Bad file makes it return -1' );
217
218     $nb_tags = $schema->resultset('AuthTagStructure')->search({ authtypecode => $fw_2->id })->count;
219     is( $nb_tags, 0, "0 tags should have been imported" );
220
221     $nb_subfields = Koha::Authority::Subfields->search({ authtypecode => $fw_2->id })->count;
222     is( $nb_subfields, 0, "0 subfields should have been imported" );
223
224     # framework overwrite
225     $data_filepath = dirname(__FILE__) . "/data/frameworks/auth_type_smaller.$format";
226
227     $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id, '', $type );
228     is( $result, 8, 'Smaller fw import successful, 6 tags removed' );
229
230     $nb_tags = $schema->resultset('AuthTagStructure')->search({ authtypecode => $fw_1->id })->count;
231     is( $nb_tags, 3, "3 tags should have been imported" );
232
233     $nb_subfields = Koha::Authority::Subfields->search({ authtypecode => $fw_1->id })->count;
234     is( $nb_subfields, 3, "3 subfields should have been imported" );
235
236     $schema->storage->txn_rollback;
237 };