From 6ef78caf6fce51d456f73be1d066955733a8c029 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 22 Sep 2020 12:29:26 -0300 Subject: [PATCH] Bug 26509: Add CannotBeDeleted object exception This patch adds a new exception with tests for the stringification. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/Koha/Exceptions.t => SUCCESS: Tests pass 3. Sign off :-D Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- Koha/Exceptions/Object.pm | 8 ++++++++ t/Koha/Exceptions.t | 24 +++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Koha/Exceptions/Object.pm b/Koha/Exceptions/Object.pm index 688a97cd5a..b25745723e 100644 --- a/Koha/Exceptions/Object.pm +++ b/Koha/Exceptions/Object.pm @@ -23,6 +23,11 @@ use Exception::Class ( 'Koha::Exceptions::Object' => { isa => 'Koha::Exceptions::Exception', }, + 'Koha::Exceptions::Object::CannotBeDeleted' => { + isa => 'Koha::Exceptions::Object', + description => 'The object cannot be deleted', + fields => ['object', 'reason'], + }, 'Koha::Exceptions::Object::DuplicateID' => { isa => 'Koha::Exceptions::Object', description => "Duplicate ID passed", @@ -84,6 +89,9 @@ sub full_message { elsif ( $self->isa('Koha::Exceptions::Object::NotInstantiated') ) { $msg = sprintf("Tried to access the '%s' method, but %s is not instantiated", $self->method, $self->class ); } + elsif ( $self->isa('Koha::Exceptions::Object::CannotBeDeleted') ) { + $msg = sprintf("Cannot delete %s object (id=%s). Reason: %s", $self->object->_get_object_class, $self->object->id, $self->reason ); + } } return $msg; diff --git a/t/Koha/Exceptions.t b/t/Koha/Exceptions.t index 13f83049ab..77fbb770c7 100755 --- a/t/Koha/Exceptions.t +++ b/t/Koha/Exceptions.t @@ -17,10 +17,12 @@ use Modern::Perl; -use Test::More tests => 6; +use Test::More tests => 7; use Test::MockObject; use Test::Exception; +use Koha::Items; + subtest 'Koha::Exceptions::Hold tests' => sub { plan tests => 5; @@ -196,3 +198,23 @@ subtest 'Koha::Exceptions::Object::NotInstantiated tests' => sub { 'Exception is thrown :-D'; is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' ); }; + +subtest 'Koha::Exceptions::Object::CannotBeDeleted tests' => sub { + + plan tests => 4; + + throws_ok + { Koha::Exceptions::Object::CannotBeDeleted->throw( + object => Koha::Item->new({ itemnumber => 1234 }), reason => 'Some reason' ); } + 'Koha::Exceptions::Object::CannotBeDeleted', + 'Exception is thrown :-D'; + + # stringify the exception + like( "$@", qr/Cannot delete Koha::Item=HASH\(.*\) object \(id=1234\). Reason: Some reason/ ); + + throws_ok + { Koha::Exceptions::Object::CannotBeDeleted->throw( "Manual message exception" ) } + 'Koha::Exceptions::Object::CannotBeDeleted', + 'Exception is thrown :-D'; + is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' ); +}; -- 2.11.0