From 992eb912de188a8f50d72acd9e5be0710bff0948 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 17 Oct 2025 09:07:45 +0200 Subject: [PATCH] Bug 41042: Simplify tests No change here. Test plan: Apply only this patch and confirm that the tests are still passing. The previous code was dynamic but complicated to adjust. The next patch needs more complex and specific comparisons. Signed-off-by: Michaela Sieber Signed-off-by: Martin Renvoize --- t/db_dependent/TablesSettings.t | 396 +++++++++++++++++--------------- 1 file changed, 214 insertions(+), 182 deletions(-) diff --git a/t/db_dependent/TablesSettings.t b/t/db_dependent/TablesSettings.t index f50295a17b8..fc846aaa807 100755 --- a/t/db_dependent/TablesSettings.t +++ b/t/db_dependent/TablesSettings.t @@ -2,7 +2,7 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 4; +use Test::More tests => 2; use Test::MockModule; use C4::Context; @@ -10,198 +10,230 @@ use C4::Utils::DataTables::TablesSettings; use Koha::Database; my $schema = Koha::Database->new->schema; -$schema->storage->txn_begin; -my $dbh = C4::Context->dbh; - -$dbh->do(q|DELETE FROM columns_settings|); +my $yaml; my $module = Test::MockModule->new('C4::Utils::DataTables::TablesSettings'); $module->mock( 'get_yaml', - sub { - { - modules => { - admin => { - currency => { - 'currencies-table' => { - default_display_length => 20, - default_sort_order => 1, - default_save_state => 1, - default_save_state_search => 0, - columns => [ - { - columnname => 'currency', - cannot_be_toggled => '1', - cannot_be_modified => '1' - }, - { - columnname => 'rate', - cannot_be_toggled => '1', - cannot_be_modified => '1' - }, - { columnname => 'symbol' }, - { - is_hidden => '1', - columnname => 'iso_code' - }, - { columnname => 'last_updated' }, - { columnname => 'active' }, - { columnname => 'actions' } - ] - } - } - }, - } - }; - } + sub { return $yaml; } ); -C4::Utils::DataTables::TablesSettings::update_columns( - { - columns => [ - { - module => 'admin', - page => 'currency', - tablename => 'currencies-table', - columnname => 'currency', - cannot_be_toggled => 1, - cannot_be_modified => 1, - }, - { - module => 'admin', - page => 'currency', - tablename => 'currencies-table', - columnname => 'rate', - cannot_be_toggled => 1, - cannot_be_modified => 1, - }, - { - module => 'admin', - page => 'currency', - tablename => 'currencies-table', - columnname => 'symbol', - }, - { - module => 'admin', - page => 'currency', - tablename => 'currencies-table', - columnname => 'iso_code', - is_hidden => 0, - }, - { - module => 'admin', - page => 'currency', - tablename => 'currencies-table', - columnname => 'last_updated', - }, - { - module => 'admin', - page => 'currency', - tablename => 'currencies-table', - columnname => 'active', - is_hidden => 1, - }, - { - module => 'admin', - page => 'currency', - tablename => 'currencies-table', - columnname => 'actions', - cannot_be_toggled => 1, - }, - ] - } -); +subtest 'update_columns, get_modules, get_columns, get_table_settings' => sub { + plan tests => 5; -my $modules = C4::Utils::DataTables::TablesSettings::get_modules(); - -my $modules_expected = { - 'admin' => { - 'currency' => { - 'currencies-table' => { - default_display_length => 20, - default_sort_order => 1, - default_save_state => 1, - default_save_state_search => 0, - columns => [ - { - columnname => 'currency', - cannot_be_toggled => 1, - cannot_be_modified => 1, - is_hidden => 0, - }, - { - columnname => 'rate', - cannot_be_toggled => 1, - cannot_be_modified => 1, - is_hidden => 0, - }, - { - columnname => 'symbol', - cannot_be_toggled => 0, - cannot_be_modified => 0, - is_hidden => 0, - }, - { - columnname => 'iso_code', - cannot_be_toggled => 0, - cannot_be_modified => 0, - is_hidden => 0, - }, - { - columnname => 'last_updated', - cannot_be_toggled => 0, - cannot_be_modified => 0, - is_hidden => 0, - }, - { - columnname => 'active', - cannot_be_toggled => 0, - cannot_be_modified => 0, - is_hidden => 1, - }, - { - columnname => 'actions', - cannot_be_toggled => 1, - cannot_be_modified => 0, - is_hidden => 0, - }, - ] - } + $schema->storage->txn_begin; + my $dbh = C4::Context->dbh; + + $dbh->do(q|DELETE FROM columns_settings|); + $dbh->do(q|DELETE FROM tables_settings|); + + $yaml = { + modules => { + admin => { + currency => { + 'currencies-table' => { + default_display_length => 20, + default_sort_order => 1, + default_save_state => 1, + default_save_state_search => 0, + columns => [ + { + columnname => 'currency', + cannot_be_toggled => '1', + cannot_be_modified => '1' + }, + { + columnname => 'rate', + cannot_be_toggled => '1', + cannot_be_modified => '1' + }, + { columnname => 'symbol' }, + { + is_hidden => '1', + columnname => 'iso_code' + }, + { columnname => 'last_updated' }, + { columnname => 'active' }, + { columnname => 'actions' } + ] + } + } + }, } - } -}; + }; -is_deeply( $modules, $modules_expected, 'get_modules returns all values' ); - -for my $m ( keys %$modules ) { - for my $p ( keys %{ $modules->{$m} } ) { - for my $t ( keys %{ $modules->{$m}{$p} } ) { - my $columns = C4::Utils::DataTables::TablesSettings::get_columns( $m, $p, $t ); - - # We do not store default_save_state and default_save_state_search in the yml file - # Removing them before comparison - delete $_->{default_save_state} for @$columns; - delete $_->{default_save_state_search} for @$columns; - is_deeply( - $columns, - $modules->{$m}{$p}{$t}{columns}, - "columns for $m>$p>$t" - ); - my $table_settings = C4::Utils::DataTables::TablesSettings::get_table_settings( $m, $p, $t ); - is_deeply( + C4::Utils::DataTables::TablesSettings::update_columns( + { + columns => [ + { + module => 'admin', + page => 'currency', + tablename => 'currencies-table', + columnname => 'currency', + cannot_be_toggled => 1, + cannot_be_modified => 1, + }, { - default_display_length => $table_settings->{default_display_length}, - default_sort_order => $table_settings->{default_sort_order}, - default_save_state => $table_settings->{default_save_state}, - default_save_state_search => $table_settings->{default_save_state_search}, + module => 'admin', + page => 'currency', + tablename => 'currencies-table', + columnname => 'rate', + cannot_be_toggled => 1, + cannot_be_modified => 1, }, { - default_display_length => $modules->{$m}{$p}{$t}{default_display_length}, - default_sort_order => $modules->{$m}{$p}{$t}{default_sort_order}, - default_save_state => $modules->{$m}{$p}{$t}{default_save_state}, - default_save_state_search => $modules->{$m}{$p}{$t}{default_save_state_search}, + module => 'admin', + page => 'currency', + tablename => 'currencies-table', + columnname => 'symbol', + }, + { + module => 'admin', + page => 'currency', + tablename => 'currencies-table', + columnname => 'iso_code', + is_hidden => 0, + }, + { + module => 'admin', + page => 'currency', + tablename => 'currencies-table', + columnname => 'last_updated', + }, + { + module => 'admin', + page => 'currency', + tablename => 'currencies-table', + columnname => 'active', + is_hidden => 1, + }, + { + module => 'admin', + page => 'currency', + tablename => 'currencies-table', + columnname => 'actions', + cannot_be_toggled => 1, + }, + ] + } + ); + + my $table_settings = + C4::Utils::DataTables::TablesSettings::get_table_settings( 'admin', 'currency', 'currencies-table' ); + is_deeply( + $table_settings, + { + default_display_length => 20, + default_sort_order => 1, + default_save_state => 1, + default_save_state_search => 0, + } + ); + + # No changes to table settings + C4::Utils::DataTables::TablesSettings::update_table_settings( + { + module => 'admin', + page => 'currency', + tablename => 'currencies-table', + default_display_length => 20, + default_sort_order => 1, + default_save_state => 1, + default_save_state_search => 0, + } + ); + + my $modules_expected = { + 'admin' => { + 'currency' => { + 'currencies-table' => { + default_display_length => 20, + default_sort_order => 1, + default_save_state => 1, + default_save_state_search => 0, + columns => [ + { + columnname => 'currency', + cannot_be_toggled => 1, + cannot_be_modified => 1, + is_hidden => 0, + }, + { + columnname => 'rate', + cannot_be_toggled => 1, + cannot_be_modified => 1, + is_hidden => 0, + }, + { + columnname => 'symbol', + cannot_be_toggled => 0, + cannot_be_modified => 0, + is_hidden => 0, + }, + { + columnname => 'iso_code', + cannot_be_toggled => 0, + cannot_be_modified => 0, + is_hidden => 0, + }, + { + columnname => 'last_updated', + cannot_be_toggled => 0, + cannot_be_modified => 0, + is_hidden => 0, + }, + { + columnname => 'active', + cannot_be_toggled => 0, + cannot_be_modified => 0, + is_hidden => 1, + }, + { + columnname => 'actions', + cannot_be_toggled => 1, + cannot_be_modified => 0, + is_hidden => 0, + }, + ] } - ); + } + } + }; + + my $modules = C4::Utils::DataTables::TablesSettings::get_modules(); + + is_deeply( $modules, $modules_expected, 'get_modules returns all values' ); + + my $columns = C4::Utils::DataTables::TablesSettings::get_columns( 'admin', 'currency', 'currencies-table' ); + is_deeply( $modules->{admin}, $modules_expected->{admin} ); + + $table_settings = + C4::Utils::DataTables::TablesSettings::get_table_settings( 'admin', 'currency', 'currencies-table' ); + is_deeply( + $table_settings, + { + default_display_length => 20, + default_sort_order => 1, + default_save_state => 1, + default_save_state_search => 0, } - } -} + ); + + # Changes to table settings + my $new_table_settings = { + default_display_length => 42, + default_sort_order => 2, + default_save_state => 0, + default_save_state_search => 0, + }; + + C4::Utils::DataTables::TablesSettings::update_table_settings( + { module => 'admin', page => 'currency', tablename => 'currencies-table', %$new_table_settings } ); + + $table_settings = + C4::Utils::DataTables::TablesSettings::get_table_settings( 'admin', 'currency', 'currencies-table' ); + is_deeply( + $table_settings, + $new_table_settings, + ); +}; -- 2.51.0