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 <tomascohen@theke.io>

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Tomas Cohen Arazi 2022-02-09 10:05:12 -03:00
parent 3f784ea613
commit 7704f51d51
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
7 changed files with 93 additions and 33 deletions

View File

@ -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;

View File

@ -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 <http://www.gnu.org/licenses>.
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;

View File

@ -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(
{

View File

@ -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

View File

@ -171,9 +171,9 @@
<span>You do not have permission to add a record to this list.</span>
[% CASE 'no_biblio_removed' %]
<span>No record was removed.</span>
[% CASE 'Koha::Exceptions::Virtualshelves::DuplicateObject' %]
[% CASE 'Koha::Exceptions::Virtualshelf::DuplicateObject' %]
<span>An error occurred when creating this list. The name [% shelfname | html %] already exists.</span>
[% CASE 'Koha::Exceptions::Virtualshelves::UseDbAdminAccount' %]
[% CASE 'Koha::Exceptions::Virtualshelf::UseDbAdminAccount' %]
<span>List could not be created. (Do not use the database administrator account.)</span>
[% CASE 'DBIx::Class::Exception' %]
[% m.msg | html %]

View File

@ -184,7 +184,7 @@
<span>You do not have permission to add a record to this list.</span>
[% CASE 'no_biblio_removed' %]
<span>No record was removed.</span>
[% CASE 'Koha::Exceptions::Virtualshelves::DuplicateObject' %]
[% CASE 'Koha::Exceptions::Virtualshelf::DuplicateObject' %]
<span>An error occurred when creating the list. The name [% shelfname | html %] already exists.</span>
[% CASE 'DBIx::Class::Exception' %]
[% m.msg | html %]

View File

@ -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' );