From 48b38fbfb4ede4008ea721d6bbd916ea16917b2a Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Fri, 19 Apr 2024 13:28:34 +0000 Subject: [PATCH] Bug 36197: Add tests 0) If k-t-d, run updatedatabase (ignore this if testing on a sandbox) 1) Enable ILLModule syspref: {staff_url}/cgi-bin/koha/admin/preferences.pl?tab=&op=search&searchfield=ILLModule 2) No need to install a backend as 35570 is in the tree 3) On a different browser session, without being lgoged in, access the create ILL request OPAC page directly: {opac_url}/cgi-bin/koha/opac-illrequests.pl?method=create&backend=Standard 4) Notice you're presented with the login page, as expected 5) Enable ILLOpacUnauthenticatedRequest: {staff_url}/cgi-bin/koha/admin/preferences.pl?tab=&op=search&searchfield=ILLOpacUnauthenticatedRequest 6) Repeat step 3) Notice you now get the create ILL request form, with some additional inputs at the top specific to an unauthenticated request (first name, last name, e-mail address) 6.5) (optional) Confirm the 'Make an Interlibrary loan request' link exists at the bottom of the search results page, while unauthenticated on OPAC: {opac_url}/cgi-bin/koha/opac-search.pl?idx=&q=music&weight_search=1 This should only show for authenticated OPAC users if ILLOpacUnauthenticatedRequest is off. 7) Test the form, try submitting with some blank fields, change type and library after and before inputing data in the input fields, etc. 8) Submit the form successfully. Notice you're presented with the unauthenticated request submission confirmation screen with a message and request details. 9) Go back to Staff UI, logged in, access ILL requests and confirm the unauthenticated request submitted through the OPAC is there: {staff_url}/cgi-bin/koha/ill/ill-requests.pl 10) Test that you can add a patron afterwards, if needed, by clicking 'Edit request' on the toolbar of the manage request page, and entering a patron ID 11) Test that you're able to 'confirm' the request by clicking 'Confirm request', as if it was a regular 'NEW' request. Additional testing: Make sure existing ILL functionality interacts with this new optional workflow behavior. For this, we're using ILLModuleDisclaimerByType. ILLCheckAvailability or AutoILLBackendPriority can also be used but require more setup (plugins, credentials, etc) 1) Configure ILLModuleDisclaimerByType, visit {staff_url}/cgi-bin/koha/admin/preferences.pl?tab=&op=search&searchfield=ILLModuleDisclaimerByType 2) Click the 'edit' link at the bottom of the syspref and copy the example code from the description into the text box. Save. 3) Repeat the original test plan from above, but make sure the request type is not 'article' (as that type is bypassed). Use 'journal', for example. Submit the request making sure you fill in all mandatory fields. 4) Notice you're now presented with the ILL request disclaimer screen, hit Submit. Notice the request is created as before. 5) Return to Staff UI, logged in, confirm this 2nd unauthenticated request has been created and the type disclaimer info is stored and presented when viewing the request. Run tests (k-t-d only): prove t/db_dependent/Koha/ILL/Backends.t prove t/db_dependent/selenium/opac_ill_requests.t Sponsored-by: NHS England (National Health Service England) Signed-off-by: David Nind --- t/db_dependent/Koha/ILL/Backends.t | 69 +++++++++++- t/db_dependent/selenium/opac_ill_requests.t | 119 ++++++++++++++++++++ t/lib/plugins/Koha/Plugin/ILL/TestPlugin.pm | 53 +++++++++ t/lib/plugins/Koha/Plugin/Test.pm | 7 ++ 4 files changed, 245 insertions(+), 3 deletions(-) create mode 100755 t/db_dependent/selenium/opac_ill_requests.t create mode 100644 t/lib/plugins/Koha/Plugin/ILL/TestPlugin.pm diff --git a/t/db_dependent/Koha/ILL/Backends.t b/t/db_dependent/Koha/ILL/Backends.t index d83cba1c91..f9c1e887e1 100755 --- a/t/db_dependent/Koha/ILL/Backends.t +++ b/t/db_dependent/Koha/ILL/Backends.t @@ -22,7 +22,7 @@ use File::Basename; use File::Path qw(make_path remove_tree); use Test::MockModule; -use Test::More tests => 4; +use Test::More tests => 5; use Koha::ILL::Backends; @@ -54,8 +54,7 @@ subtest 'installed_backends() tests' => sub { $schema->storage->txn_begin; # Install a plugin_backend - my $plugins = Koha::Plugins->new; - $plugins->InstallPlugins; + _install_plugin_backend(); is_deeply( Koha::ILL::Backends->installed_backends, ['Test Plugin'], 'Only one backend installed, happens to be a plugin' @@ -82,3 +81,67 @@ subtest 'installed_backends() tests' => sub { Koha::Plugins::Methods->delete; $schema->storage->txn_rollback; }; + +subtest 'opac_available_backends() tests' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + my $logged_in_user = $builder->build_object( + { + class => 'Koha::Patrons' + } + ); + _install_plugin_backend(); + + # ILLOpacUnauthenticatedRequest = 0 + t::lib::Mocks::mock_preference( 'ILLOpacUnauthenticatedRequest', 0 ); + + t::lib::Mocks::mock_preference( 'ILLOpacbackends', '' ); + is_deeply( + Koha::ILL::Backends->opac_available_backends($logged_in_user), [ 'Test Plugin', 'Standard' ], + 'Two backends are available for OPAC, one plugin and the core Standard backend' + ); + + t::lib::Mocks::mock_preference( 'ILLOpacbackends', 'gibberish' ); + is_deeply( + Koha::ILL::Backends->opac_available_backends($logged_in_user), [], + 'ILLOpacbackends contains a non-existing backend' + ); + + t::lib::Mocks::mock_preference( 'ILLOpacbackends', 'Standard' ); + is_deeply( + Koha::ILL::Backends->opac_available_backends($logged_in_user), ['Standard'], + 'ILLOpacbackends contains Standard. Only the Standard backend should be returned' + ); + + t::lib::Mocks::mock_preference( 'ILLOpacbackends', 'Standard|Test Plugin' ); + is_deeply( + Koha::ILL::Backends->opac_available_backends($logged_in_user), [ 'Test Plugin', 'Standard' ], + 'ILLOpacbackends contains both. Both backends should be returned' + ); + + # ILLOpacUnauthenticatedRequest = 1 + t::lib::Mocks::mock_preference( 'ILLOpacUnauthenticatedRequest', 1 ); + t::lib::Mocks::mock_preference( 'ILLOpacbackends', '' ); + is_deeply( + Koha::ILL::Backends->opac_available_backends(undef), [ 'Standard' ], + 'Only the standard backend is available for OPAC, the installed plugin does not support unauthenticated requests' + ); + + t::lib::Mocks::mock_preference( 'ILLOpacbackends', 'Test Plugin' ); + is_deeply( + Koha::ILL::Backends->opac_available_backends(undef), [], + 'The only backend for the OPAC is Test Plugin but it does not support unauthenticated requests' + ); + + Koha::Plugins::Methods->delete; + $schema->storage->txn_rollback; +}; + + +sub _install_plugin_backend { + my $plugins = Koha::Plugins->new; + $plugins->InstallPlugins; +} \ No newline at end of file diff --git a/t/db_dependent/selenium/opac_ill_requests.t b/t/db_dependent/selenium/opac_ill_requests.t new file mode 100755 index 0000000000..40caabb05c --- /dev/null +++ b/t/db_dependent/selenium/opac_ill_requests.t @@ -0,0 +1,119 @@ +#!/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::Biblio qw(DelBiblio); + +use C4::Context; +use Koha::AuthUtils; +use t::lib::Mocks; +use t::lib::Selenium; +use t::lib::TestBuilder; + +our $builder = t::lib::TestBuilder->new; +our $ILLOpacUnauthenticatedRequest_value = C4::Context->preference('ILLOpacUnauthenticatedRequest'); +our $ILLModule_value = C4::Context->preference('ILLModule'); + +SKIP: { + eval { require Selenium::Remote::Driver; }; + skip "Selenium::Remote::Driver is needed for selenium tests.", 3 if $@; + + our $s = t::lib::Selenium->new; + our $driver = $s->driver; + + subtest 'unauthenticated ill request' => sub { + plan tests => 2; + + subtest 'ILLOpacUnauthenticatedRequest enabled' => sub { + plan tests => 4; + + C4::Context->set_preference( 'ILLOpacUnauthenticatedRequest', 1 ); + C4::Context->set_preference( 'ILLModule', 1 ); + + $driver->get( $s->opac_base_url . "opac-search.pl?q=music" ); + + like( + $driver->get_title, qr(Results of search for 'music'), + 'Correctly in search results page' + ); + + is( + $driver->find_element('//div[@class="suggestion"]/ul/li')->get_text, + 'Make an Interlibrary loan request', + 'Placing an ILL request through the OPAC is allowed', + ); + + # Clicking on the search results page link works + $driver->find_element('//div[@class="suggestion"]/ul/li/a')->click; + is( + $driver->find_element('(//nav[@id="breadcrumbs"]/ol/li)[last()]')->get_text, + 'New interlibrary loan request', + 'Correctly on the create new request OPAC page' + ); + + # Visiting the create request page directly works + $driver->get( $s->opac_base_url . "opac-illrequests.pl?op=create" ); + is( + $driver->find_element('(//nav[@id="breadcrumbs"]/ol/li)[last()]')->get_text, + 'New interlibrary loan request', + 'Correctly on the create new request OPAC page' + ); + + }; + + subtest 'ILLOpacUnauthenticatedRequest disabled' => sub { + plan tests => 3; + + C4::Context->set_preference( 'ILLOpacUnauthenticatedRequest', 0 ); + C4::Context->set_preference( 'ILLModule', 1 ); + + $driver->get( $s->opac_base_url . "opac-search.pl?q=music" ); + + like( + $driver->get_title, qr(Results of search for 'music'), + 'Correctly in search results page' + ); + + my $link_exists = + $driver->find_elements( '//div[@class="suggestion"]/ul/li' ); + + is( + scalar @{$link_exists}, + 0, + 'Search page - Place ILL request link should be absent. ' + ); + + # Visiting the create request page directly does not work + $driver->get( $s->opac_base_url . "opac-illrequests.pl?op=create" ); + is( + $driver->find_element('(//nav[@id="breadcrumbs"]/ol/li)[last()]')->get_text, + 'Log in to your account', + 'Correctly on the log in page' + ); + + }; + }; + + $driver->quit(); +} + +END { + C4::Context->set_preference( 'ILLOpacUnauthenticatedRequest', $ILLOpacUnauthenticatedRequest_value ); + C4::Context->set_preference( 'ILLModule', $ILLModule_value ); +} diff --git a/t/lib/plugins/Koha/Plugin/ILL/TestPlugin.pm b/t/lib/plugins/Koha/Plugin/ILL/TestPlugin.pm new file mode 100644 index 0000000000..411c8fffd7 --- /dev/null +++ b/t/lib/plugins/Koha/Plugin/ILL/TestPlugin.pm @@ -0,0 +1,53 @@ +package Koha::Plugin::ILL::TestPlugin; + +use Modern::Perl; + +use base qw(Koha::Plugins::Base); +use Koha::DateUtils qw( dt_from_string ); + +use File::Basename qw( dirname ); +use C4::Installer; +use Cwd qw(abs_path); +use CGI; +use JSON qw( encode_json decode_json ); + +use JSON qw( to_json from_json ); +use File::Basename qw( dirname ); + +use Koha::Libraries; +use Koha::Patrons; + +our $VERSION = "2.0.4"; + +our $metadata = { + name => 'TestPlugin', + author => 'PTFS-Europe', + date_authored => '2023-10-30', + date_updated => '2024-03-28', + minimum_version => '24.05.00.000', + maximum_version => undef, + version => $VERSION, + description => 'This plugin is an ILL backend plugin example' +}; + +sub new { + my ( $self, $params ) = @_; + + my $backend = { + _logger => $params->{logger}, + _config => $params->{config}, + _plugin => $self, + }; + + bless $backend, $self; + return $backend; +} + +sub capabilities { + my ( $self, $name ) = @_; + my $capabilities = { + opac_unauthenticated_ill_requests => sub { return 0; } + }; + + return $capabilities->{$name}; +} diff --git a/t/lib/plugins/Koha/Plugin/Test.pm b/t/lib/plugins/Koha/Plugin/Test.pm index 2db404d627..42f8ce6587 100644 --- a/t/lib/plugins/Koha/Plugin/Test.pm +++ b/t/lib/plugins/Koha/Plugin/Test.pm @@ -426,6 +426,13 @@ sub ill_backend { return 'Test Plugin'; } +sub new_ill_backend { + my ( $self, $params ) = @_; + + require Koha::Plugin::ILL::TestPlugin; + return Koha::Plugin::ILL::TestPlugin->new($params); +} + sub auth_client_get_user { my ( $self, $params ) = @_; -- 2.39.2