Bug 28211: (QA follow-up) Test for hook calls properly

Some warnings, that mean a hook has been called were not tested.
This patch adds those tests, and does some tidy also.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Tomás Cohen Arazi 2021-10-19 09:46:32 -03:00 committed by Jonathan Druart
parent c290ae7e0e
commit 6c4fa7d82d
2 changed files with 38 additions and 17 deletions

View file

@ -40,14 +40,24 @@ my $builder = t::lib::TestBuilder->new;
t::lib::Mocks::mock_config( 'enable_plugins', 1 );
subtest '() hook tests' => sub {
subtest 'patron_barcode_transform() and item_barcode_transform() hook tests' => sub {
plan tests => 4;
plan tests => 6;
$schema->storage->txn_begin;
# Avoid testing useless warnings
my $test_plugin = Test::MockModule->new('Koha::Plugin::Test');
$test_plugin->mock( 'after_item_action', undef );
$test_plugin->mock( 'after_biblio_action', undef );
my $plugins = Koha::Plugins->new;
$plugins->InstallPlugins;
warnings_are
{ $plugins->InstallPlugins; }
[ "Calling 'install' died for plugin Koha::Plugin::BrokenInstall",
"Calling 'upgrade' died for plugin Koha::Plugin::BrokenUpgrade" ];
C4::Context->dbh->do("DELETE FROM plugin_methods WHERE plugin_class LIKE '%TestBarcodes%'");
my $plugin = Koha::Plugin::Test->new->enable;
@ -81,18 +91,16 @@ subtest '() hook tests' => sub {
}
);
# Avoid testing useless warnings
my $test_plugin = Test::MockModule->new('Koha::Plugin::Test');
$test_plugin->mock( 'after_item_action', undef );
$test_plugin->mock( 'after_biblio_action', undef );
my $item;
warning_like { $item = $builder->build_sample_item(); }
qr/Plugin error \(Test Plugin\): item_barcode_transform called with parameter: /,
'Koha::Item->store calls the item_barcode_transform hook';
my $biblio = $builder->build_sample_biblio();
my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
$item_1->barcode('THISISATEST');
$item->barcode('THISISATEST');
warning_like { $item_1->store(); }
qr/item_barcode_transform called with parameter: THISISATEST/,
'AddReserve calls the after_hold_create hook';
warning_is { $item->store(); }
'Plugin error (Test Plugin): item_barcode_transform called with parameter: THISISATEST',
'Koha::Item->store calls the item_barcode_transform hook';
$schema->storage->txn_rollback;
Koha::Plugins::Methods->delete;

View file

@ -88,7 +88,8 @@ subtest 'call() tests' => sub {
};
subtest 'more call() tests' => sub {
plan tests => 3;
plan tests => 6;
$schema->storage->txn_begin;
# Temporarily remove any installed plugins data
@ -96,7 +97,13 @@ subtest 'more call() tests' => sub {
t::lib::Mocks::mock_config('enable_plugins', 1);
my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
my @plugins = $plugins->InstallPlugins;
my @plugins;
warnings_are
{ @plugins = $plugins->InstallPlugins; }
[ "Calling 'install' died for plugin Koha::Plugin::BrokenInstall",
"Calling 'upgrade' died for plugin Koha::Plugin::BrokenUpgrade" ];
foreach my $plugin (@plugins) {
$plugin->enable();
}
@ -104,11 +111,17 @@ subtest 'more call() tests' => sub {
# Barcode is multiplied by 2 by Koha::Plugin::Test, and again by 4 by Koha::Plugin::TestItemBarcodeTransform
# showing that call has passed the same ref to multiple plugins to operate on
my $bc = 1;
Koha::Plugins->call('item_barcode_transform', \$bc);
warnings_are
{ Koha::Plugins->call('item_barcode_transform', \$bc); }
[ 'Plugin error (Test Plugin): item_barcode_transform called with parameter: 1',
'Plugin error (Test Plugin for item_barcode_transform): item_barcode_transform called with parameter: 2' ];
is( $bc, 8, "Got expected response" );
my $cn = 'abcd';
Koha::Plugins->call('item_barcode_transform', \$cn);
warnings_are
{ Koha::Plugins->call('item_barcode_transform', \$bc); }
[ 'Plugin error (Test Plugin): item_barcode_transform called with parameter: 8',
'Plugin error (Test Plugin for item_barcode_transform): item_barcode_transform called with parameter: 16' ];
is( $cn, 'abcd', "Got expected response" );
t::lib::Mocks::mock_config('enable_plugins', 0);