From 933346b5388cf28e0fa305daa807f1f908af81be Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 9 Jun 2025 15:03:54 +0100 Subject: [PATCH] Bug 40095: Add context to intranet_js plugin hook The get_plugins_intranet_js method now passes the current script name (controller) to plugins via a 'page' parameter, allowing plugins to determine which page/controller is being displayed. This will allow plugin authors to only return JS that's relevant to the current page when they wish to do so. Test plan: 1. Create a plugin with an intranet_js method that logs the received parameters 2. Navigate to different pages in the staff interface 3. Verify that the plugin receives the page parameter containing the script name 4. Confirm that the script name reflects the actual controller (e.g., '/cgi-bin/koha/admin/admin-home.pl' when on the admin home page) 5. Run the test: prove t/db_dependent/Koha/Template/Plugin/KohaPlugins.t 6. Verify that the new test passes, confirming plugins receive the page parameter Signed-off-by: Roman Dolny --- Koha/Template/Plugin/KohaPlugins.pm | 10 ++++++- .../Koha/Template/Plugin/KohaPlugins.t | 28 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Koha/Template/Plugin/KohaPlugins.pm b/Koha/Template/Plugin/KohaPlugins.pm index 63bbc2c6ff..e8a4cfa851 100644 --- a/Koha/Template/Plugin/KohaPlugins.pm +++ b/Koha/Template/Plugin/KohaPlugins.pm @@ -24,6 +24,7 @@ use base qw( Template::Plugin ); use Try::Tiny qw( catch try ); use Koha::Plugins; +use Koha::AuthUtils qw( get_script_name ); =head1 NAME @@ -151,9 +152,13 @@ sub get_plugins_intranet_head { This method collects the output of all plugins with an intranet_js method to output to the javascript section of at the bottom of intranet pages. +The method now passes the current script name (controller) to the plugin's intranet_js +method as a parameter: { page => 'script_name' } + =cut sub get_plugins_intranet_js { + my ($self) = @_; return q{} unless C4::Context->config("enable_plugins"); my $p = Koha::Plugins->new(); @@ -166,10 +171,13 @@ sub get_plugins_intranet_js { } ); + # Get script name (controller) instead of template name + my $script_name = get_script_name() || ''; + my @data = (); foreach my $plugin (@plugins) { try { - my $datum = $plugin->intranet_js || q{}; + my $datum = $plugin->intranet_js( { page => $script_name } ) || q{}; push( @data, $datum ); } catch { warn "Error calling 'intranet_js' on the " . $plugin->{class} . "plugin ($_)"; diff --git a/t/db_dependent/Koha/Template/Plugin/KohaPlugins.t b/t/db_dependent/Koha/Template/Plugin/KohaPlugins.t index d8aa7b658e..0b98f126dd 100755 --- a/t/db_dependent/Koha/Template/Plugin/KohaPlugins.t +++ b/t/db_dependent/Koha/Template/Plugin/KohaPlugins.t @@ -3,7 +3,7 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 19; +use Test::More tests => 20; use Test::Warn; use CGI; use File::Basename; @@ -92,4 +92,30 @@ is( $plugin->get_plugins_opac_head, q{}, 'Test plugin opac_head return value is( $plugin->get_plugins_intranet_js, q{}, 'Test plugin intranet_js return value is empty' ); is( $plugin->get_plugins_intranet_head, q{}, 'Test plugin intranet_head return value is empty' ); +# Test intranet_js page parameter passing +t::lib::Mocks::mock_config( 'enable_plugins', 1 ); + +# Reset the mock to capture arguments +my $captured_args; +$mock_plugin->mock( + 'intranet_js', + sub { + my ( $self, $args ) = @_; + $captured_args = $args; + return "test"; + } +); + +# Create plugin with context (minimal) +my $plugin_with_context = bless {}, 'Koha::Template::Plugin::KohaPlugins'; + +# Call get_plugins_intranet_js +my $result = $plugin_with_context->get_plugins_intranet_js(); + +# Verify that the plugin was called with page parameter containing script name +ok( + defined $captured_args && ref($captured_args) eq 'HASH' && exists $captured_args->{page}, + 'Plugin intranet_js method receives page parameter with script name' +); + $schema->storage->txn_rollback; -- 2.39.5