From 7704f51d5131d3f1104244a705294db614eae76e Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 9 Feb 2022 10:05:12 -0300 Subject: [PATCH] Bug 30057: Move virtualshelf-related exceptions to a separate library This patch moves the not-generic virtualshelf-related exceptions to their own file. Callers are adjusted as required. To test: 1. Run: $ kshell k$ prove t/db_dependent/Virtualshelves.t => SUCCESS: Tests pass 2. Apply this patch 3. Repeat 1 => SUCCESS: Tests pass 4. Check virtualshelves work as expected 5. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Nind Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- Koha/Exceptions.pm | 21 ----- Koha/Exceptions/Virtualshelf.pm | 81 +++++++++++++++++++ Koha/Virtualshelf.pm | 8 +- Koha/Virtualshelfshare.pm | 4 +- .../prog/en/modules/virtualshelves/shelves.tt | 4 +- .../bootstrap/en/modules/opac-shelves.tt | 2 +- t/db_dependent/Virtualshelves.t | 6 +- 7 files changed, 93 insertions(+), 33 deletions(-) create mode 100644 Koha/Exceptions/Virtualshelf.pm diff --git a/Koha/Exceptions.pm b/Koha/Exceptions.pm index 5cb8d5c38b..56ab691df2 100644 --- a/Koha/Exceptions.pm +++ b/Koha/Exceptions.pm @@ -55,27 +55,6 @@ use Exception::Class ( isa => 'Koha::Exception', description => 'Koha is under maintenance.' }, - # Virtualshelves exceptions - 'Koha::Exceptions::Virtualshelves::DuplicateObject' => { - isa => 'Koha::Exceptions::DuplicateObject', - description => "Duplicate shelf object", - }, - 'Koha::Exceptions::Virtualshelves::InvalidInviteKey' => { - isa => 'Koha::Exception', - description => 'Invalid key on accepting the share', - }, - 'Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing' => { - isa => 'Koha::Exception', - description=> 'Invalid key on sharing a shelf', - }, - 'Koha::Exceptions::Virtualshelves::ShareHasExpired' => { - isa => 'Koha::Exception', - description=> 'Cannot share this shelf, the share has expired', - }, - 'Koha::Exceptions::Virtualshelves::UseDbAdminAccount' => { - isa => 'Koha::Exception', - description => "Invalid use of database administrator account", - } ); 1; diff --git a/Koha/Exceptions/Virtualshelf.pm b/Koha/Exceptions/Virtualshelf.pm new file mode 100644 index 0000000000..c3d32133fb --- /dev/null +++ b/Koha/Exceptions/Virtualshelf.pm @@ -0,0 +1,81 @@ +package Koha::Exceptions::Virtualshelf; + +# 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 Koha::Exception; + +use Exception::Class ( + + 'Koha::Exceptions::Virtualshelf' => { + isa => 'Koha::Exception', + }, + 'Koha::Exceptions::Virtualshelf::DuplicateObject' => { + isa => 'Koha::Exceptions::Virtualshelf', + description => "Duplicate shelf object", + }, + 'Koha::Exceptions::Virtualshelf::InvalidInviteKey' => { + isa => 'Koha::Exceptions::Virtualshelf', + description => 'Invalid key on accepting the share', + }, + 'Koha::Exceptions::Virtualshelf::InvalidKeyOnSharing' => { + isa => 'Koha::Exceptions::Virtualshelf', + description=> 'Invalid key on sharing a shelf', + }, + 'Koha::Exceptions::Virtualshelf::ShareHasExpired' => { + isa => 'Koha::Exceptions::Virtualshelf', + description=> 'Cannot share this shelf, the share has expired', + }, + 'Koha::Exceptions::Virtualshelf::UseDbAdminAccount' => { + isa => 'Koha::Exceptions::Virtualshelf', + description => "Invalid use of database administrator account", + } +); + +=head1 NAME + +Koha::Exceptions::Virtualshelf - Base class for virtualshelf exceptions + +=head1 Exceptions + +=head2 Koha::Exceptions::Virtualshelf + +Generic Virtualshelf exception + +=head2 Koha::Exceptions::Virtualshelf::DuplicateObject + +Exception to be used when a similar virtual shelf already exists. + +=head2 Koha::Exceptions::Virtualshelf::InvalidInviteKey + +Exception to be used when an invite key is invalid. + +=head2 Koha::Exceptions::Virtualshelf::InvalidKeyOnSharing + +Exception to be used when the supplied key is invalid on sharing. + +=head2 Koha::Exceptions::Virtualshelf::ShareHasExpired + +Exception to be used when a share has expired. + +=head2 Koha::Exceptions::Virtualshelf::UseDbAdminAccount + +Exception to be used when the owner is not set. + +=cut + +1; diff --git a/Koha/Virtualshelf.pm b/Koha/Virtualshelf.pm index e8f0f14180..029a98b228 100644 --- a/Koha/Virtualshelf.pm +++ b/Koha/Virtualshelf.pm @@ -23,7 +23,7 @@ use C4::Auth qw( haspermission ); use Koha::Patrons; use Koha::Database; use Koha::DateUtils qw( dt_from_string ); -use Koha::Exceptions; +use Koha::Exceptions::Virtualshelf; use Koha::Virtualshelfshare; use Koha::Virtualshelfshares; use Koha::Virtualshelfcontent; @@ -45,11 +45,11 @@ sub store { my ( $self ) = @_; unless ( $self->owner ) { - Koha::Exceptions::Virtualshelves::UseDbAdminAccount->throw; + Koha::Exceptions::Virtualshelf::UseDbAdminAccount->throw; } unless ( $self->is_shelfname_valid ) { - Koha::Exceptions::Virtualshelves::DuplicateObject->throw; + Koha::Exceptions::Virtualshelf::DuplicateObject->throw; } $self->allow_change_from_owner( 1 ) @@ -124,7 +124,7 @@ sub get_contents { sub share { my ( $self, $key ) = @_; unless ( $key ) { - Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw; + Koha::Exceptions::Virtualshelf::InvalidKeyOnSharing->throw; } Koha::Virtualshelfshare->new( { diff --git a/Koha/Virtualshelfshare.pm b/Koha/Virtualshelfshare.pm index 5f61e27a84..64e13bd542 100644 --- a/Koha/Virtualshelfshare.pm +++ b/Koha/Virtualshelfshare.pm @@ -45,10 +45,10 @@ Koha::Virtualshelfshare - Koha Virtualshelfshare Object class sub accept { my ( $self, $invitekey, $borrowernumber ) = @_; if ( $self->has_expired ) { - Koha::Exceptions::Virtualshelves::ShareHasExpired->throw; + Koha::Exceptions::Virtualshelf::ShareHasExpired->throw; } if ( $self->invitekey ne $invitekey ) { - Koha::Exceptions::Virtualshelves::InvalidInviteKey->throw; + Koha::Exceptions::Virtualshelf::InvalidInviteKey->throw; } # If this borrower already has a share, there is no need to accept twice diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tt index b83781c051..98a0c03cda 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tt @@ -171,9 +171,9 @@ You do not have permission to add a record to this list. [% CASE 'no_biblio_removed' %] No record was removed. - [% CASE 'Koha::Exceptions::Virtualshelves::DuplicateObject' %] + [% CASE 'Koha::Exceptions::Virtualshelf::DuplicateObject' %] An error occurred when creating this list. The name [% shelfname | html %] already exists. - [% CASE 'Koha::Exceptions::Virtualshelves::UseDbAdminAccount' %] + [% CASE 'Koha::Exceptions::Virtualshelf::UseDbAdminAccount' %] List could not be created. (Do not use the database administrator account.) [% CASE 'DBIx::Class::Exception' %] [% m.msg | html %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt index 05388eb3e8..195b817e7a 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt @@ -184,7 +184,7 @@ You do not have permission to add a record to this list. [% CASE 'no_biblio_removed' %] No record was removed. - [% CASE 'Koha::Exceptions::Virtualshelves::DuplicateObject' %] + [% CASE 'Koha::Exceptions::Virtualshelf::DuplicateObject' %] An error occurred when creating the list. The name [% shelfname | html %] already exists. [% CASE 'DBIx::Class::Exception' %] [% m.msg | html %] diff --git a/t/db_dependent/Virtualshelves.t b/t/db_dependent/Virtualshelves.t index c23db964ed..e9444f06cd 100755 --- a/t/db_dependent/Virtualshelves.t +++ b/t/db_dependent/Virtualshelves.t @@ -66,7 +66,7 @@ subtest 'CRUD' => sub { } )->store; }; - is( ref($@), 'Koha::Exceptions::Virtualshelves::DuplicateObject', + is( ref($@), 'Koha::Exceptions::Virtualshelf::DuplicateObject', 'Exception on duplicate name' ); $number_of_shelves = Koha::Virtualshelves->search->count; is( $number_of_shelves, 1, 'To be sure the number of shelves is still 1' ); @@ -122,7 +122,7 @@ subtest 'Sharing' => sub { )->store; my $shared_shelf = eval { $shelf_to_share->share }; - is ( ref( $@ ), 'Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing', 'Do not share if no key given' ); + is ( ref( $@ ), 'Koha::Exceptions::Virtualshelf::InvalidKeyOnSharing', 'Do not share if no key given' ); $shared_shelf = eval { $shelf_to_share->share('valid key') }; is( ref( $shared_shelf ), 'Koha::Virtualshelfshare', 'On sharing, the method should return a valid Koha::Virtualshelfshare object' ); @@ -135,7 +135,7 @@ subtest 'Sharing' => sub { $shared_shelf->accept( 'invalid k', $share_with_me->{borrowernumber} ); }; is( $is_accepted, undef, 'The share should have not been accepted if the key is invalid' ); - is( ref( $@ ), 'Koha::Exceptions::Virtualshelves::InvalidInviteKey', 'accept with an invalid key should raise an exception' ); + is( ref( $@ ), 'Koha::Exceptions::Virtualshelf::InvalidInviteKey', 'accept with an invalid key should raise an exception' ); $is_accepted = $shared_shelf->accept( 'valid key', $share_with_me->{borrowernumber} ); ok( defined($is_accepted), 'The share should have been accepted if the key valid' ); -- 2.39.5