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;
31 my $schema = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
34 subtest 'ImportFramework() tests' => sub {
38 subtest 'CSV tests' => sub {
46 subtest 'ODS tests' => sub {
54 subtest 'XML tests' => sub {
65 $schema->storage->txn_begin;
67 my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework.$format";
68 my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
70 my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
71 is( $result, 0, 'Import successful, no tags removed' );
73 my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
74 is( $nb_tags, 4, "4 tags should have been imported" );
76 my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
77 is( $nb_subfields, 12, "12 subfields should have been imported" );
80 my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
81 $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id );
83 is( $result, -1, 'Bad file makes it return -1' );
85 $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count;
86 is( $nb_tags, 0, "0 tags should have been imported" );
88 $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count;
89 is( $nb_subfields, 0, "0 subfields should have been imported" );
92 $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_smaller.$format";
94 $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
95 is( $result, 5, 'Smaller fw import successful, 4 tags removed' );
97 $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
98 is( $nb_tags, 3, "3 tags should have been imported" );
100 $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
101 is( $nb_subfields, 8, "8 subfields should have been imported" );
103 $schema->storage->txn_rollback;
106 sub run_csv_no_quoted {
108 $schema->storage->txn_begin;
110 my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_no_quoted.csv";
111 my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
113 my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
114 is( $result, 0, 'Import successful, no tags removed' );
116 my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
117 is( $nb_tags, 4, "4 tags should have been imported" );
119 my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
120 is( $nb_subfields, 12, "12 subfields should have been imported" );
123 my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
124 $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id );
126 is( $result, -1, 'Bad file makes it return -1' );
128 $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count;
129 is( $nb_tags, 0, "0 tags should have been imported" );
131 $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count;
132 is( $nb_subfields, 0, "0 subfields should have been imported" );
134 $schema->storage->txn_rollback;
141 $schema->storage->txn_begin;
143 my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_new.ods";
144 my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
146 my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
147 is( $result, 0, 'Import successful, no tags removed' );
149 my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
150 is( $nb_tags, 5, "5 tags should have been imported" );
152 my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
153 is( $nb_subfields, 16, "16 subfields should have been imported" );
156 my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
157 $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id );
159 is( $result, -1, 'Bad file makes it return -1' );
161 $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count;
162 is( $nb_tags, 0, "0 tags should have been imported" );
164 $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count;
165 is( $nb_subfields, 0, "0 subfields should have been imported" );
168 $schema->storage->txn_rollback;