From c3a9bc3ff38bdca503e75436726ea2aa9d22d77f Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Thu, 13 Feb 2025 10:07:24 -0100 Subject: [PATCH] Bug 37893: Add data migration tests This test file utilizes the same SIPconfig.xml as the atomicupdate. To test different SIPConfig.xml configurations using k-t-d, edit the contents of /etc/koha/sites/kohadev/SIPConfig.xml and run the test file again. All tests should always pass. [IMPORTANT]: The atomicupdate file used in this test (bug_37893.pl) needs to be updated to the respective DBrev file when it's pushed. prove t/db_dependent/Koha/SIP2/SIP2ModuleMigration.t Test plan for the whole module: 1) Checkout the branch and reset_all 2) Visit the administration page: /cgi-bin/koha/admin/admin-home.pl 3) Notice the new 'Self-service circulation (SIP2)' item under 'Patrons and circulation'. Click it. 4) Notice you are now on the new SIP2 module. Create some institutions, accounts, listeners, etc. Play around with the forms. 5) Notice everytime you add/edit a new SIP2 'resource', a prompt asking to restart the SIP2 server is shown. If you confirm it, it will restart the SIP server in order for the new data to take effect. 6) Run 'updatedatabase'. Notice the contents from /etc/koha/sites/kohadev/SIPConfig.xml have been migrated to the database are should be accessible in the UI. 7) Confirm all the data has been migrated correctly, and is editable in the UI as expected. 8) You can repeat this test plan but edit the contents of /etc/koha/sites/kohadev/SIPConfig.xml with a more complex and thorough configuration (something resembling a real SIPConfig.xml) 9) After updating the contents of SIPConfig.xml, restart SIP: koha-sip --restart 10) Test the module again, verify the contents have been migrated as expected. 11) Run the migration test again to ensure that SIP2 is being served the exact same configuration before and after the migration: prove t/db_dependent/Koha/SIP2/SIP2ModuleMigration.t Signed-off-by: Andrew Fuerste Henry Signed-off-by: Lisette Scheer Signed-off-by: Martin Renvoize Signed-off-by: Lucas Gass --- .../Koha/SIP2/SIP2ModuleMigration.t | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 t/db_dependent/Koha/SIP2/SIP2ModuleMigration.t diff --git a/t/db_dependent/Koha/SIP2/SIP2ModuleMigration.t b/t/db_dependent/Koha/SIP2/SIP2ModuleMigration.t new file mode 100644 index 0000000000..558e5960bd --- /dev/null +++ b/t/db_dependent/Koha/SIP2/SIP2ModuleMigration.t @@ -0,0 +1,80 @@ +#!/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 => 1; + +use C4::Installer; +use C4::SIP::Sip::Configuration; +use XML::Simple; + +use Koha::SIP2::Institutions; +use Koha::SIP2::Accounts; +use Koha::SIP2::Listeners; +use Koha::SIP2::ServerParams; +use Koha::SIP2::SystemPreferenceOverrides; + +subtest 'Config from XML matches config from database' => sub { + + plan tests => 4; + + my $schema = Koha::Database->new->schema; + $schema->storage->txn_begin; + + Koha::SIP2::Institutions->delete; + Koha::SIP2::Accounts->delete; + Koha::SIP2::Listeners->delete; + Koha::SIP2::ServerParams->delete; + Koha::SIP2::SystemPreferenceOverrides->delete; + + my $koha_instance = $ENV{KOHA_CONF} =~ m!^.+/sites/([^/]+)/koha-conf\.xml$! ? $1 : undef; + my $SIPconfigXMLFile = "/etc/koha/sites/$koha_instance/SIPconfig.xml"; + my $fileSIPconfig = C4::SIP::Sip::Configuration->new($SIPconfigXMLFile); + + is( scalar( keys %{ $fileSIPconfig->{accounts} } ), 5, 'fileSIPconfig->{accounts} has 5 members' ); + is( Koha::SIP2::Accounts->search()->count, 0, 'Database does not yet contain any accounts' ); + + run_atomic_updates( ['bug_37893.pl'] ); #TODO: Update this to the DBREV when pushed + + is( Koha::SIP2::Accounts->search()->count, 5, 'Database now contains 5 accounts' ); + + my $databaseSIPconfig = C4::SIP::Sip::Configuration->new($SIPconfigXMLFile); + + # Remove 'parms' out of each institution. This is not used and was not migrated + foreach my $key ( keys %{ $fileSIPconfig->{institutions} } ) { + delete $fileSIPconfig->{institutions}->{$key}{'parms'}; + } + + # Remove 'register_id' out of each account if it's empty. This can no longer be returned as an empty string as is now an FK constraint + foreach my $key ( keys %{ $fileSIPconfig->{accounts} } ) { + if ( defined $fileSIPconfig->{accounts}->{$key}{'register_id'} + && $fileSIPconfig->{accounts}->{$key}{'register_id'} eq "" ) + { + delete $fileSIPconfig->{accounts}->{$key}{'register_id'}; + } + } + + #Test accounts + is_deeply( + $fileSIPconfig, + $databaseSIPconfig, + 'config from XML file matches config from database' + ); + + $schema->storage->txn_rollback; +};