Bug 34978: Simplify tests logic

Joining the plugin classes strings and then performing regex checks on
it might be a bit fragile. I had hard time understanding the ands the
conditions for truth.

I rewrote it in a more intuitive way just using `none` and the tests are
easy to understand and pass.

The failure is probably because new plugins were added that make the
resulting string not match the expected structure.

The new new tests design is more readable anyway and not subject to this
errors in future additions.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
(cherry picked from commit 67ba381a29)
Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
This commit is contained in:
Tomas Cohen Arazi 2025-07-29 12:13:12 -03:00 committed by Paul Derscheid
parent 32c8ae2792
commit d6b61b732d
No known key found for this signature in database
GPG Key ID: C25F9FD44CED595B
1 changed files with 28 additions and 23 deletions

View File

@ -22,6 +22,7 @@ use File::Basename;
use File::Spec;
use File::Temp qw( tempdir tempfile );
use FindBin qw($Bin);
use List::MoreUtils qw(none);
use Module::Load::Conditional qw(can_load);
use Test::MockModule;
use Test::NoWarnings;
@ -194,7 +195,7 @@ subtest 'GetPlugins() tests' => sub {
subtest 'InstallPlugins() tests' => sub {
plan tests => 6;
plan tests => 8;
$schema->storage->txn_begin;
@ -204,21 +205,25 @@ subtest 'InstallPlugins() tests' => sub {
# Tests for the exclude parameter
# Test the returned plugins of the InstallPlugins subroutine
my $excluded_plugins = [ "Koha::Plugin::Test", "Koha::Plugin::MarcFieldValues" ];
my $plugins = Koha::Plugins->new( { enable_plugins => 1 } );
my @installed_plugins = $plugins->InstallPlugins( { exclude => [ "Koha::Plugin::Test", "Koha::Plugin::MarcFieldValues" ] } );
my $plugin_classes = join( " ", map { $_->{class} } @installed_plugins );
my @installed_plugins = $plugins->InstallPlugins( { exclude => $excluded_plugins } );
my $result = grep { $plugin_classes !~ $_ } [ ":Test |:Test\$", ":MarcFieldValues |:MarcFieldValues\$" ]
&& $plugin_classes =~ ":TestItemBarcodeTransform |:TestItemBarcodeTransform\$";
ok( $result, "Excluded plugins are not returned" );
foreach my $excluded_plugin ( @{$excluded_plugins} ) {
ok(
none { $_ eq $excluded_plugin } ( map { $_->{class} } @installed_plugins ),
"Excluded plugin not returned ($excluded_plugin)"
);
}
# Test the plugins in the database
my @plugins = $plugins->GetPlugins( { all => 1, error => 1 } );
$plugin_classes = join( " ", map { $_->{class} } @plugins );
$result = grep { $plugin_classes !~ $_ } [ ":Test |:Test\$", ":MarcFieldValues |:MarcFieldValues\$" ]
&& $plugin_classes =~ ":TestItemBarcodeTransform |:TestItemBarcodeTransform\$";
ok( $result, "Excluded plugins are not installed" );
foreach my $excluded_plugin ( @{$excluded_plugins} ) {
ok(
none { $_ eq $excluded_plugin } ( map { $_->{class} } @plugins ),
"Excluded plugin not installed ($excluded_plugin)"
);
}
# Remove installed plugins data
Koha::Plugins::Methods->delete;
@ -226,9 +231,10 @@ subtest 'InstallPlugins() tests' => sub {
# Tests for the include parameter
# Test the returned plugins of the InstallPlugins subroutine
@installed_plugins = $plugins->InstallPlugins( { include => [ "Koha::Plugin::Test", "Koha::Plugin::MarcFieldValues" ] } );
@installed_plugins =
$plugins->InstallPlugins( { include => [ "Koha::Plugin::Test", "Koha::Plugin::MarcFieldValues" ] } );
$result = 1;
my $result = 1;
foreach my $plugin_class ( map { $_->{class} } @installed_plugins ) {
$result = 0 unless ( "$plugin_class" =~ ":Test\$" || "$plugin_class" =~ ":MarcFieldValues\$" );
}
@ -244,17 +250,16 @@ subtest 'InstallPlugins() tests' => sub {
ok( $result, "Only included plugins are installed" );
# Tests when both include and exclude parameter are used simultaneously
throws_ok
{
$plugins->InstallPlugins( { exclude => [ "Koha::Plugin::Test" ], include => [ "Koha::Plugin::Test" ] } );
}
'Koha::Exceptions::BadParameter';
throws_ok {
$plugins->InstallPlugins( { exclude => ["Koha::Plugin::Test"], include => ["Koha::Plugin::Test"] } );
}
'Koha::Exceptions::BadParameter';
# Tests when the plugin to be installled is not found
throws_ok
{
$plugins->InstallPlugins( { include => [ "Koha::Plugin::NotfoundPlugin" ] } );
}
'Koha::Exceptions::BadParameter';
throws_ok {
$plugins->InstallPlugins( { include => ["Koha::Plugin::NotfoundPlugin"] } );
}
'Koha::Exceptions::BadParameter';
$schema->storage->txn_rollback;
};