diff --git a/t/db_dependent/ImportExportFramework.t b/t/db_dependent/ImportExportFramework.t index de096cc2a6..8189183fc4 100644 --- a/t/db_dependent/ImportExportFramework.t +++ b/t/db_dependent/ImportExportFramework.t @@ -1,34 +1,100 @@ #!/usr/bin/perl +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + use Modern::Perl; -use Test::More tests => 3; + +use Test::More tests => 1; + +use t::lib::TestBuilder; + use File::Basename qw( dirname ); use Koha::Database; use Koha::BiblioFrameworks; use Koha::MarcSubfieldStructures; -use t::lib::TestBuilder; use C4::ImportExportFramework; -my $schema = Koha::Database->new->schema; -$schema->storage->txn_begin; +my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; -my $data_filepath = dirname(__FILE__) . '/data/biblio_framework.csv'; +subtest 'ImportFramework() tests' => sub { -my $frameworkcode = '4T'; -C4::ImportExportFramework::ImportFramework($data_filepath, $frameworkcode); + plan tests => 3; -my $dbh = C4::Context->dbh; + subtest 'CSV tests' => sub { + plan tests => 9; -# FIXME Import does not create the biblio framework -#my $biblio_framework = Koha::BiblioFrameworks->find($frameworkcode); -#ok( $biblio_framework ); + run_tests('csv'); + }; -my $nb_tags = $dbh->selectrow_array(q|SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode="4T"|); -is( $nb_tags, 4, "4 tags should have been imported" ); + subtest 'ODS tests' => sub { + plan tests => 9; -my $nb_subfields = - Koha::MarcSubfieldStructures->search( { frameworkcode => $frameworkcode } ) - ->count; -is( $nb_subfields, 12, "12 subfields should have been imported" ); + run_tests('ods'); + }; + + subtest 'XML tests' => sub { + plan tests => 9; + + run_tests('xml'); + }; +}; + +sub run_tests { + + my ($format) = @_; + + $schema->storage->txn_begin; + + my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework.$format"; + my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' }); + + my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id ); + is( $result, 0, 'Import successful, no tags removed' ); + + my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count; + is( $nb_tags, 4, "4 tags should have been imported" ); + + my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count; + is( $nb_subfields, 12, "12 subfields should have been imported" ); + + # bad file tests + my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' }); + $result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id ); + + is( $result, -1, 'Bad file makes it return -1' ); + + $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count; + is( $nb_tags, 0, "0 tags should have been imported" ); + + $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count; + is( $nb_subfields, 0, "0 subfields should have been imported" ); + + # framework overwrite + $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_smaller.$format"; + + $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id ); + is( $result, 5, 'Smaller fw import successful, 4 tags removed' ); + + $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count; + is( $nb_tags, 3, "3 tags should have been imported" ); + + $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count; + is( $nb_subfields, 8, "8 subfields should have been imported" ); + + $schema->storage->txn_rollback; +} diff --git a/t/db_dependent/data/frameworks/biblio_framework.ods b/t/db_dependent/data/frameworks/biblio_framework.ods new file mode 100644 index 0000000000..de157bef65 Binary files /dev/null and b/t/db_dependent/data/frameworks/biblio_framework.ods differ diff --git a/t/db_dependent/data/frameworks/biblio_framework.xml b/t/db_dependent/data/frameworks/biblio_framework.xml new file mode 100644 index 0000000000..b658aca9f5 --- /dev/null +++ b/t/db_dependent/data/frameworks/biblio_framework.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + +tagfieldliblibrarianlibopacrepeatablemandatoryauthorised_valueframeworkcode000LEADERLEADER01#T4001CONTROL NUMBERCONTROL NUMBER00#T4003CONTROL NUMBER IDENTIFIERCONTROL NUMBER IDENTIFIER01#T4010LIBRARY OF CONGRESS CONTROL NUMBERLIBRARY OF CONGRESS CONTROL NUMBER00#T4tagfieldtagsubfieldliblibrarianlibopacrepeatablemandatorykohafieldtabauthorised_valueauthtypecodevalue_builderisurlhiddenframeworkcodeseealsolinkdefaultvaluemaxlength000@fixed length control fieldfixed length control field01#0##marc21_leader.pl00T4###24001@control fieldcontrol field00#0###00T4###9999003@control fieldcontrol field01#0##marc21_orgcode.pl00T4###9999005@control fieldcontrol field01#0##marc21_field_005.pl00T4###9999006@fixed length control fieldfixed length control field00#0##marc21_field_006.pl0-1T4###9999007@fixed length control fieldfixed length control field00#0##marc21_field_007.pl00T4###9999008@fixed length control fieldfixed length control field01#0##marc21_field_008.pl00T4###40009@fixed length control fieldfixed length control field00#0###0-6T4###99990108Field link and sequence numberField link and sequence number10#0###0-6T4###9999010aLC control numberLC control number00biblioitems.lccn0###00T4###9999010bNUCMC control numberNUCMC control number10#0###0-1T4###9999010zCanceled/invalid LC control numberCanceled/invalid LC control number10#0###0-1T4###9999 diff --git a/t/db_dependent/data/frameworks/biblio_framework_smaller.csv b/t/db_dependent/data/frameworks/biblio_framework_smaller.csv new file mode 100644 index 0000000000..876410abae --- /dev/null +++ b/t/db_dependent/data/frameworks/biblio_framework_smaller.csv @@ -0,0 +1,18 @@ +"tagfield","liblibrarian","libopac","repeatable","mandatory","authorised_value","frameworkcode" +"000","LEADER","LEADER","0","1","","T5" +"001","CONTROL NUMBER","CONTROL NUMBER","0","0","","T5" +"003","CONTROL NUMBER IDENTIFIER","CONTROL NUMBER IDENTIFIER","0","1","","T5" + +"#-#","#-#","#-#","#-#","#-#","#-#","#-#" + +"tagfield","tagsubfield","liblibrarian","libopac","repeatable","mandatory","kohafield","tab","authorised_value","authtypecode","value_builder","isurl","hidden","frameworkcode","seealso","link","defaultvalue","maxlength" +"000","@","fixed length control field","fixed length control field","0","1","","0","","","marc21_leader.pl","0","0","T5","","","","24" +"001","@","control field","control field","0","0","","0","","","","0","0","T5","","","","9999" +"003","@","control field","control field","0","1","","0","","","marc21_orgcode.pl","0","0","T5","","","","9999" +"005","@","control field","control field","0","1","","0","","","marc21_field_005.pl","0","0","T5","","","","9999" +"007","@","fixed length control field","fixed length control field","0","0","","0","","","marc21_field_007.pl","0","0","T5","","","","9999" +"008","@","fixed length control field","fixed length control field","0","1","","0","","","marc21_field_008.pl","0","0","T5","","","","40" +"010","8","Field link and sequence number","Field link and sequence number","1","0","","0","","","","0","-6","T5","","","","9999" +"010","b","NUCMC control number","NUCMC control number","1","0","","0","","","","0","-1","T5","","","","9999" + +"#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#" diff --git a/t/db_dependent/data/frameworks/biblio_framework_smaller.ods b/t/db_dependent/data/frameworks/biblio_framework_smaller.ods new file mode 100644 index 0000000000..99c5642bf6 Binary files /dev/null and b/t/db_dependent/data/frameworks/biblio_framework_smaller.ods differ diff --git a/t/db_dependent/data/frameworks/biblio_framework_smaller.xml b/t/db_dependent/data/frameworks/biblio_framework_smaller.xml new file mode 100644 index 0000000000..cff72b8026 --- /dev/null +++ b/t/db_dependent/data/frameworks/biblio_framework_smaller.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + +tagfieldliblibrarianlibopacrepeatablemandatoryauthorised_valueframeworkcode000LEADERLEADER01#T5001CONTROL NUMBERCONTROL NUMBER00#T5003CONTROL NUMBER IDENTIFIERCONTROL NUMBER IDENTIFIER01#T5tagfieldtagsubfieldliblibrarianlibopacrepeatablemandatorykohafieldtabauthorised_valueauthtypecodevalue_builderisurlhiddenframeworkcodeseealsolinkdefaultvaluemaxlength000@fixed length control fieldfixed length control field01#0##marc21_leader.pl00T5###24001@control fieldcontrol field00#0###00T5###9999003@control fieldcontrol field01#0##marc21_orgcode.pl00T5###9999005@control fieldcontrol field01#0##marc21_field_005.pl00T5###9999007@fixed length control fieldfixed length control field00#0##marc21_field_007.pl00T5###9999008@fixed length control fieldfixed length control field01#0##marc21_field_008.pl00T5###400108Field link and sequence numberField link and sequence number10#0###0-6T5###9999010bNUCMC control numberNUCMC control number10#0###0-1T5###9999