Bug 18309: Add UNIMARC field 214 and its subfields
[koha.git] / t / db_dependent / ImportExportFramework.t
blob8189183fc422b6b056238d48c4accbe034831f08
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Test::More tests => 1;
22 use t::lib::TestBuilder;
24 use File::Basename qw( dirname );
26 use Koha::Database;
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 {
36 plan tests => 3;
38 subtest 'CSV tests' => sub {
39 plan tests => 9;
41 run_tests('csv');
44 subtest 'ODS tests' => sub {
45 plan tests => 9;
47 run_tests('ods');
50 subtest 'XML tests' => sub {
51 plan tests => 9;
53 run_tests('xml');
57 sub run_tests {
59 my ($format) = @_;
61 $schema->storage->txn_begin;
63 my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework.$format";
64 my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
66 my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
67 is( $result, 0, 'Import successful, no tags removed' );
69 my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
70 is( $nb_tags, 4, "4 tags should have been imported" );
72 my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
73 is( $nb_subfields, 12, "12 subfields should have been imported" );
75 # bad file tests
76 my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
77 $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id );
79 is( $result, -1, 'Bad file makes it return -1' );
81 $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count;
82 is( $nb_tags, 0, "0 tags should have been imported" );
84 $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count;
85 is( $nb_subfields, 0, "0 subfields should have been imported" );
87 # framework overwrite
88 $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_smaller.$format";
90 $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
91 is( $result, 5, 'Smaller fw import successful, 4 tags removed' );
93 $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
94 is( $nb_tags, 3, "3 tags should have been imported" );
96 $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
97 is( $nb_subfields, 8, "8 subfields should have been imported" );
99 $schema->storage->txn_rollback;