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 Test::More tests => 1;
22 use t::lib::TestBuilder;
24 use File::Basename qw( dirname );
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;
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'ImportFramework() tests' => sub {
40 subtest 'CSV tests for biblio frameworks' => sub {
43 run_biblio_tests('csv');
48 subtest 'ODS tests for biblio frameworks' => sub {
51 run_biblio_tests('ods');
56 subtest 'XML tests for biblio frameworks' => sub {
59 run_biblio_tests('xml');
62 subtest 'CSV tests for auth frameworks' => sub {
65 run_auth_tests('csv');
68 subtest 'ODS tests for auth frameworks' => sub {
71 run_auth_tests('ods');
74 subtest 'XML tests for auth frameworks' => sub {
77 run_auth_tests('xml');
82 sub run_biblio_tests {
87 $schema->storage->txn_begin;
89 my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework.$format";
90 my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
92 my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id, 0, $type );
93 is( $result, 0, 'Import successful, no tags removed' );
95 my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
96 is( $nb_tags, 4, "4 tags should have been imported" );
98 my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
99 is( $nb_subfields, 12, "12 subfields should have been imported" );
102 my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
103 $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id, 0, $type );
105 is( $result, -1, 'Bad file makes it return -1' );
107 $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count;
108 is( $nb_tags, 0, "0 tags should have been imported" );
110 $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count;
111 is( $nb_subfields, 0, "0 subfields should have been imported" );
113 # framework overwrite
114 $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_smaller.$format";
116 $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id, 0, $type );
117 is( $result, 5, 'Smaller fw import successful, 4 tags removed' );
119 $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
120 is( $nb_tags, 3, "3 tags should have been imported" );
122 $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
123 is( $nb_subfields, 8, "8 subfields should have been imported" );
125 $schema->storage->txn_rollback;
128 sub run_csv_no_quoted {
130 $schema->storage->txn_begin;
132 my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_no_quoted.csv";
133 my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
135 my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
136 is( $result, 0, 'Import successful, no tags removed' );
138 my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
139 is( $nb_tags, 4, "4 tags should have been imported" );
141 my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
142 is( $nb_subfields, 12, "12 subfields should have been imported" );
145 my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
146 $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id );
148 is( $result, -1, 'Bad file makes it return -1' );
150 $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count;
151 is( $nb_tags, 0, "0 tags should have been imported" );
153 $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count;
154 is( $nb_subfields, 0, "0 subfields should have been imported" );
156 $schema->storage->txn_rollback;
163 $schema->storage->txn_begin;
165 my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_new.ods";
166 my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
168 my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
169 is( $result, 0, 'Import successful, no tags removed' );
171 my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
172 is( $nb_tags, 5, "5 tags should have been imported" );
174 my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
175 is( $nb_subfields, 16, "16 subfields should have been imported" );
178 my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
179 $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id );
181 is( $result, -1, 'Bad file makes it return -1' );
183 $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count;
184 is( $nb_tags, 0, "0 tags should have been imported" );
186 $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count;
187 is( $nb_subfields, 0, "0 subfields should have been imported" );
190 $schema->storage->txn_rollback;
196 my $type = "authority";
198 $schema->storage->txn_begin;
200 my $data_filepath = dirname(__FILE__) . "/data/frameworks/auth_type.$format";
201 my $fw_1 = $builder->build_object({ class => 'Koha::Authority::Types' });
203 my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id, '', $type );
204 is( $result, 0, 'Import successful, no tags removed' );
206 my $nb_tags = $schema->resultset('AuthTagStructure')->search({ authtypecode => $fw_1->id })->count;
207 is( $nb_tags, 6, "6 tags should have been imported" );
209 my $nb_subfields = Koha::Authority::Subfields->search({ authtypecode => $fw_1->id })->count;
210 is( $nb_subfields, 8, "8 subfields should have been imported" );
213 my $fw_2 = $builder->build_object({ class => 'Koha::Authority::Types' });
214 $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id, '', $type );
216 is( $result, -1, 'Bad file makes it return -1' );
218 $nb_tags = $schema->resultset('AuthTagStructure')->search({ authtypecode => $fw_2->id })->count;
219 is( $nb_tags, 0, "0 tags should have been imported" );
221 $nb_subfields = Koha::Authority::Subfields->search({ authtypecode => $fw_2->id })->count;
222 is( $nb_subfields, 0, "0 subfields should have been imported" );
224 # framework overwrite
225 $data_filepath = dirname(__FILE__) . "/data/frameworks/auth_type_smaller.$format";
227 $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id, '', $type );
228 is( $result, 8, 'Smaller fw import successful, 6 tags removed' );
230 $nb_tags = $schema->resultset('AuthTagStructure')->search({ authtypecode => $fw_1->id })->count;
231 is( $nb_tags, 3, "3 tags should have been imported" );
233 $nb_subfields = Koha::Authority::Subfields->search({ authtypecode => $fw_1->id })->count;
234 is( $nb_subfields, 3, "3 subfields should have been imported" );
236 $schema->storage->txn_rollback;