From 412f2aa0bdfc94cf1a1101c6785fcbfea825642d Mon Sep 17 00:00:00 2001 From: Andreas Roussos Date: Mon, 30 Jan 2023 19:30:27 +0100 Subject: [PATCH] Bug 31207: Add a Selenium test for the OPAC Shelf browser This adds a new Selenium test to automatically test the functionality of the OPAC Shelf browser with regards to the display of local cover images. This unit test has a somewhat narrow scope at the moment (i.e. it only checks if everything is working fine when local covers are in use), but can be expanded in the future to include OPAC Shelf browser tests for other cover image providers, etc. Also added is a sample 2x2 pixel image to be used by the test when uploading a local cover image. Test plan: 1) Apply the other patch from this bug report, then run the new Selenium unit test which should pass without failures: prove -v t/db_dependent/selenium/opacshelfbrowser.t --- t/db_dependent/data/2x2-000000ff.png | Bin 0 -> 72 bytes t/db_dependent/selenium/opacshelfbrowser.t | 198 +++++++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 t/db_dependent/data/2x2-000000ff.png create mode 100644 t/db_dependent/selenium/opacshelfbrowser.t diff --git a/t/db_dependent/data/2x2-000000ff.png b/t/db_dependent/data/2x2-000000ff.png new file mode 100644 index 0000000000000000000000000000000000000000..f3a926442f788048d255177f63a76b537c0a5235 GIT binary patch literal 72 zcmeAS@N?(olHy`uVBq!ia0vp^Od!m`0wizjKe`@B@q4;BhE&{2PWf^EzyS^=25vqk V#)g+W_XDLEJYD@<);T3K0RS>u5~BbB literal 0 HcmV?d00001 diff --git a/t/db_dependent/selenium/opacshelfbrowser.t b/t/db_dependent/selenium/opacshelfbrowser.t new file mode 100644 index 0000000000..0afaf0b66b --- /dev/null +++ b/t/db_dependent/selenium/opacshelfbrowser.t @@ -0,0 +1,198 @@ +#!/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 File::Basename qw( dirname ); + +use C4::Biblio qw( AddBiblio DelBiblio ); +use C4::Context; +use Koha::AuthUtils; +use MARC::Record; +use MARC::Field; +use Test::More; + +use t::lib::Mocks; +use t::lib::Selenium; +use t::lib::TestBuilder; + +eval { require Selenium::Remote::Driver; }; +if ( $@ ) { + plan skip_all => "Selenium::Remote::Driver is needed for Selenium tests."; +} else { + plan tests => 3; +} + +my $testdir = dirname( __FILE__ ); + +my @cleanup; + +my $OPACShelfBrowser_value = C4::Context->preference( 'OPACShelfBrowser' ); +my $LocalCoverImages_value = C4::Context->preference( 'LocalCoverImages' ); +my $OPACLocalCoverImages_value = C4::Context->preference( 'OPACLocalCoverImages' ); +my $OpacSeparateHoldings_value = C4::Context->preference( 'OpacSeparateHoldings' ); +my $OpacSeparateHoldingsBranch_value = C4::Context->preference( 'OpacSeparateHoldingsBranch' ); + +# Enable the OPAC shelf browser +C4::Context->set_preference( 'OPACShelfBrowser', '1' ); + +# Enable the display of local cover images in the staff interface and in the OPAC +C4::Context->set_preference( 'LocalCoverImages', '1' ); +C4::Context->set_preference( 'OPACLocalCoverImages', '1' ); + +# Add a test biblio (for attaching local cover images to) +my $biblio = MARC::Record->new(); +if ( C4::Context->preference( 'marcflavour' ) eq 'MARC21' ) { + $biblio->append_fields( + MARC::Field->new( '245', ' ', ' ', a => 'test biblio for MARC21 instance' ), + MARC::Field->new( '100', ' ', ' ', a => 'test author' ) + ); +} elsif ( C4::Context->preference( 'marcflavour' ) eq 'UNIMARC' ) { + $biblio->append_fields( + MARC::Field->new( '200', ' ', ' ', a => 'test biblio for UNIMARC intance' ), + MARC::Field->new( '200', ' ', ' ', f => 'test author' ) + ); +} +my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' ); + +my $builder = t::lib::TestBuilder->new; + +# Add a test item (required in order for the OPAC "Browse shelf" hyperlink to appear) +my $itemnumber1 = $builder->build_sample_item( + { + biblionumber => $biblionumber, + library => 'CPL', + itemcallnumber => '123 A', + itype => 'BK' + } +)->itemnumber; +my $item1 = Koha::Items->find( $itemnumber1 ); + +# Add another test item (required in order for the OPAC "Other holdings" tab to appear) +my $itemnumber2 = $builder->build_sample_item( + { + biblionumber => $biblionumber, + library => 'MPL', + itemcallnumber => '123 B', + itype => 'BK' + } +)->itemnumber; +my $item2 = Koha::Items->find( $itemnumber2 ); + +push @cleanup, $builder, $item1, $item2; + +my $s = t::lib::Selenium->new; +my $driver = $s->driver; +my $opac_base_url = $s->opac_base_url; +my $base_url = $s->base_url; + +# Adjust the height and width of the generated screenshots +#$driver->set_window_size( 2160, 991 ); # Height, then Width + +my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 1, branchcode => 'CPL' } } ); +my $password = Koha::AuthUtils::generate_password( $patron->category ); +t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); +$patron->set_password( { password => $password } ); + +push @cleanup, $patron; + +$s->auth( $patron->userid, $password ); + +subtest 'OPAC Shelf browser - Check the display of biblio-level local cover images (OpacSeparateHoldings set to "Don\'t separate")' => sub { + plan tests => 2; + + C4::Context->set_preference( 'OpacSeparateHoldings', '0' ); + C4::Context->set_preference( 'OpacSeparateHoldingsBranch', 'homebranch' ); + + $driver->get( $base_url . "tools/upload-cover-image.pl?biblionumber=$biblionumber&filetype=image" ); + #$driver->capture_screenshot( 'Selenium_00.png' ); + my $image_upload_elt = $driver->find_element( '//input[@id="fileToUpload"]' ); + my $remote_fname = $driver->upload_file( $testdir . '/../data/2x2-000000ff.png' ); + $image_upload_elt->send_keys( $remote_fname ); + sleep 5; # Wait for the "Upload progress:" progress bar to become full and show 100% + #$driver->capture_screenshot( 'Selenium_01.png' ); + my $process_image_button = $driver->find_element( '//button[@class="btn btn-primary btn-sm save_image"]' ); + $process_image_button->click; + #$driver->capture_screenshot( 'Selenium_02.png' ); + $driver->get( $base_url . "catalogue/detail.pl?biblionumber=$biblionumber" ); + #$driver->capture_screenshot( 'Selenium_03.png' ); + my $local_cover_image_elt = $driver->find_element( '//*[@id="biblio-cover-slider"]/div/a' ); + is( $local_cover_image_elt->get_attribute( 'title', 1 ), 'Local cover image', 'A local cover image has been successfully uploaded at the biblio level' ); + + $driver->get( $opac_base_url . "opac-detail.pl?biblionumber=$biblionumber&shelfbrowse_itemnumber=$itemnumber1#holdings" ); + sleep 5; # Wait for the uploaded local cover image to become visible in the OPAC Shelf browser's "Holdings" tab + #$driver->capture_screenshot( 'Selenium_04.png' ); + my $shelf_browser_thumbnail_elt = $driver->find_element( '//*[@id="holdings"]/div[@id="shelfbrowser"]//div[@id="local-thumbnail-shelf-' . $biblionumber . '"]/img' ); + is( $shelf_browser_thumbnail_elt->get_attribute( 'class' , 1 ), 'thumbnail', 'The biblio-level local cover image is displayed in the OPAC Shelf browser in the "Holdings" tab' ); +}; + +subtest 'OPAC Shelf browser - Check the display of item-level local cover images (OpacSeparateHoldings set to "Separate" and OpacSeparateHoldingsBranch set to "home library")' => sub { + plan tests => 2; + + C4::Context->set_preference( 'OpacSeparateHoldings', '1' ); + C4::Context->set_preference( 'OpacSeparateHoldingsBranch', 'homebranch' ); + + $driver->get( $base_url . "tools/upload-cover-image.pl?itemnumber=$itemnumber2&filetype=image" ); + #$driver->capture_screenshot( 'Selenium_05.png' ); + my $image_upload_elt = $driver->find_element( '//input[@id="fileToUpload"]' ); + my $remote_fname = $driver->upload_file( $testdir . '/../data/2x2-000000ff.png' ); + #my $remote_fname = $driver->upload_file( $testdir . '/../data/aedrian-YD8aZ9Pbb14-unsplash.jpg' ); + $image_upload_elt->send_keys( $remote_fname ); + sleep 5; # Wait for the "Upload progress:" progress bar to become full and show 100% + #$driver->capture_screenshot( 'Selenium_06.png' ); + my $process_image_button = $driver->find_element( '//button[@class="btn btn-primary btn-sm save_image"]' ); + $process_image_button->click; + #$driver->capture_screenshot( 'Selenium_07.png' ); + $driver->get( $base_url . "catalogue/detail.pl?biblionumber=$biblionumber" ); + #$driver->capture_screenshot( 'Selenium_08.png' ); + my $local_cover_image_elt = $driver->find_element( '//*[@id="biblio-cover-slider"]/div/a' ); + is( $local_cover_image_elt->get_attribute( 'title', 1 ), 'Local cover image', 'A local cover image has been successfully uploaded at the item level' ); + + $s->opac_auth( $patron->userid, $password ); + #$driver->capture_screenshot( 'Selenium_09.png' ); + + $driver->get( $opac_base_url . "opac-detail.pl?biblionumber=$biblionumber&shelfbrowse_itemnumber=$itemnumber2#otherholdings" ); + sleep 5; # Wait for the uploaded local cover image to become visible in the OPAC Shelf browser + #$driver->capture_screenshot( 'Selenium_10.png' ); + my $shelf_browser_thumbnail_elt = $driver->find_element( '//*[@id="otherholdings"]/div[@id="shelfbrowser"]//div[@id="local-thumbnail-shelf-' . $biblionumber . '"]/img' ); + is( $shelf_browser_thumbnail_elt->get_attribute( 'class' , 1 ), 'thumbnail', 'The item-level local cover image is displayed in the OPAC Shelf browser in the "Other holdings" tab' ); +}; + +subtest 'OPAC Shelf browser - Check the display of local cover images (OpacSeparateHoldings set to "Separate" and OpacSeparateHoldingsBranch set to "holding library")' => sub { + plan tests => 1; + + C4::Context->set_preference( 'OpacSeparateHoldings', '1' ); + C4::Context->set_preference( 'OpacSeparateHoldingsBranch', 'holdingbranch' ); + + $driver->get( $opac_base_url . "opac-detail.pl?biblionumber=$biblionumber&shelfbrowse_itemnumber=$itemnumber2#otherholdings" ); + sleep 5; # Wait for the uploaded local cover image to become visible in the OPAC Shelf browser + #$driver->capture_screenshot( 'Selenium_11.png' ); + my $shelf_browser_thumbnail_elt = $driver->find_element( '//*[@id="otherholdings"]/div[@id="shelfbrowser"]//div[@id="local-thumbnail-shelf-' . $biblionumber . '"]/img' ); + is( $shelf_browser_thumbnail_elt->get_attribute( 'class' , 1 ), 'thumbnail', 'The item-level local cover image is displayed in the OPAC Shelf browser in the "Other holdings" tab' ); +}; + +# Revert the related System Preferences to their original values, +# delete the test item and then delete the test biblio +END { + C4::Context->set_preference( 'OPACShelfBrowser', $OPACShelfBrowser_value ); + C4::Context->set_preference( 'LocalCoverImages', $LocalCoverImages_value ); + C4::Context->set_preference( 'OPACLocalCoverImages', $OPACLocalCoverImages_value ); + C4::Context->set_preference( 'OpacSeparateHoldings', $OpacSeparateHoldings_value ); + C4::Context->set_preference( 'OpacSeparateHoldingsBranch', $OpacSeparateHoldingsBranch_value ); + $_->delete for @cleanup; + DelBiblio( $biblionumber ); +}; -- 2.20.1