From e2e59894953c3fd4836f482f7383d3fad63be3c7 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 8 Aug 2018 11:23:15 -0300 Subject: [PATCH] Bug 21178: (follow-up) Stringify exception correctly This patch adds code to stringify the Password::TooShort exception. To test: - Run: $ kshell k$ prove t/Koha/Exceptions.t => SUCCESS: Tests pass! Signed-off-by: Tomas Cohen Arazi --- Koha/Exceptions/Password.pm | 20 ++++++++++++++++++++ t/Koha/Exceptions.t | 22 +++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Koha/Exceptions/Password.pm b/Koha/Exceptions/Password.pm index 30a239bbb9..bffafdbadd 100644 --- a/Koha/Exceptions/Password.pm +++ b/Koha/Exceptions/Password.pm @@ -41,6 +41,20 @@ use Exception::Class ( } ); +sub full_message { + my $self = shift; + + my $msg = $self->message; + + unless ( $msg) { + if ( $self->isa('Koha::Exceptions::Password::TooShort') ) { + $msg = sprintf("Password length (%s) is shorter than required (%s)", $self->length, $self->min_length ); + } + } + + return $msg; +} + =head1 NAME Koha::Exceptions::Password - Base class for password exceptions @@ -67,6 +81,12 @@ Password is too weak. Password contains trailing spaces, which is forbidden. +=head1 Class methods + +=head2 full_message + +Overloaded method for exception stringifying. + =cut 1; diff --git a/t/Koha/Exceptions.t b/t/Koha/Exceptions.t index c306d31cb9..7ecc115cfd 100644 --- a/t/Koha/Exceptions.t +++ b/t/Koha/Exceptions.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; use Test::Exception; subtest 'Koha::Exceptions::Object::FKConstraint tests' => sub { @@ -41,3 +41,23 @@ subtest 'Koha::Exceptions::Object::FKConstraint tests' => sub { is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' ); }; +subtest 'Koha::Exceptions::Password tests' => sub { + + plan tests => 5; + + use_ok('Koha::Exceptions::Password'); + + throws_ok + { Koha::Exceptions::Password::TooShort->throw( length => 4, min_length => 5 ); } + 'Koha::Exceptions::Password::TooShort', + 'Exception is thrown :-D'; + + # stringify the exception + is( "$@", 'Password length (4) is shorter than required (5)', 'Exception stringified correctly' ); + + throws_ok + { Koha::Exceptions::Password::TooShort->throw( "Manual message exception" ) } + 'Koha::Exceptions::Password::TooShort', + 'Exception is thrown :-D'; + is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' ); +}; -- 2.18.0