From 2f43667e1eeb1c84a07631477b384a79b7fb49b1 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 12 Aug 2019 16:13:25 -0300 Subject: [PATCH] Bug 14570: Add error handling to Koha::Patron->add_guarantor This patch adds checks on the values for the 'relationship'. This is done to avoid future problems when migrating relationships from the plain text syspref into (why not) a proper table. And to preserve consistency. Tests are added for both the new exceptions and the changes to Koha::Patron. To test: - Apply this patches - Run: $ kshell k$ prove t/Koha/Exceptions.t \ t/db_dependent/Koha/Patron.t => SUCCESS: Tests pass! Signed-off-by: Tomas Cohen Arazi --- Koha/Exceptions/Patron/Relationship.pm | 73 ++++++++++++++++++++++++++ Koha/Patron.pm | 11 ++++ t/Koha/Exceptions.t | 31 ++++++++++- t/db_dependent/Koha/Patron.t | 67 +++++++++++++++++++++++ 4 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 Koha/Exceptions/Patron/Relationship.pm create mode 100644 t/db_dependent/Koha/Patron.t diff --git a/Koha/Exceptions/Patron/Relationship.pm b/Koha/Exceptions/Patron/Relationship.pm new file mode 100644 index 0000000000..4e5ba7c210 --- /dev/null +++ b/Koha/Exceptions/Patron/Relationship.pm @@ -0,0 +1,73 @@ +package Koha::Exceptions::Patron::Relationship; + +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Exception::Class ( + + 'Koha::Exceptions::Patron::Relationship' => { + description => 'Something went wrong!', + }, + 'Koha::Exceptions::Patron::Relationship::InvalidRelationship' => { + isa => 'Koha::Exceptions::Patron::Relationship', + description => 'The specified relationship is invalid', + fields => ['relationship','no_relationship'] + } +); + +sub full_message { + my $self = shift; + + my $msg = $self->message; + + unless ( $msg) { + if ( $self->isa('Koha::Exceptions::Patron::Relationship') ) { + if ( $self->no_relationship ) { + $msg = sprintf( "No relationship passed." ); + } + else { + $msg = sprintf("Invalid relationship passed, '%s' is not defined.", $self->relationship ); + } + } + } + + return $msg; +} + +=head1 NAME + +Koha::Exceptions::Patron::Relationship - Base class for patron relatioship exceptions + +=head1 Exceptions + +=head2 Koha::Exceptions::Patron::Relationship + +Generic Patron exception + +=head2 Koha::Exceptions::Patron::Relationship::InvalidRelationship + +Exception to be used when an invalid relationship is passed. + +=head1 Class methods + +=head2 full_message + +Overloaded method for exception stringifying. + +=cut + +1; diff --git a/Koha/Patron.pm b/Koha/Patron.pm index c3c2d909f1..9e04ba846d 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -34,6 +34,7 @@ use Koha::Club::Enrollments; use Koha::Database; use Koha::DateUtils; use Koha::Exceptions::Password; +use Koha::Exceptions::Patron::Relationship; use Koha::Holds; use Koha::Old::Checkouts; use Koha::Patron::Attributes; @@ -1462,6 +1463,16 @@ sub add_guarantor { my $guarantor_id = $params->{guarantor_id}; my $relationship = $params->{relationship}; + my @valid_relationships = split /,|\|/, C4::Context->preference('borrowerRelationship'); + + Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( + no_relationship => 1 ) + unless defined $relationship; + + Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( + relationship => $relationship ) + unless any { $_ eq $relationship } @valid_relationships; + return Koha::Patron::Relationship->new( { guarantee_id => $self->id, diff --git a/t/Koha/Exceptions.t b/t/Koha/Exceptions.t index a5efae2791..cece81316c 100644 --- a/t/Koha/Exceptions.t +++ b/t/Koha/Exceptions.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 4; +use Test::More tests => 5; use Test::MockObject; use Test::Exception; @@ -128,3 +128,32 @@ subtest 'Koha::Exceptions::Metadata tests' => sub { 'Exception is thrown :-D'; is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' ); }; + +subtest 'Koha::Exceptions::Patron::Relationship tests' => sub { + + plan tests => 7; + + use_ok('Koha::Exceptions::Patron::Relationship'); + + throws_ok + { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( no_relationship => 1 ); } + 'Koha::Exceptions::Patron::Relationship::InvalidRelationship', + 'Exception is thrown :-D'; + + # stringify the exception + is( "$@", 'No relationship passed.', 'Exception stringified correctly' ); + + throws_ok + { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( relationship => 'some' ); } + 'Koha::Exceptions::Patron::Relationship::InvalidRelationship', + 'Exception is thrown :-D'; + + # stringify the exception + is( "$@", "Invalid relationship passed, 'some' is not defined.", 'Exception stringified correctly' ); + + throws_ok + { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( "Manual message exception" ) } + 'Koha::Exceptions::Patron::Relationship::InvalidRelationship', + 'Exception is thrown :-D'; + is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' ); +}; diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t new file mode 100644 index 0000000000..bbfc362a89 --- /dev/null +++ b/t/db_dependent/Koha/Patron.t @@ -0,0 +1,67 @@ +#!/usr/bin/perl + +# Copyright 2019 Koha Development team +# +# 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 Test::More tests => 1; +use Test::Exception; + +use Koha::Database; +use Koha::Patrons; +use Koha::Patron::Relationships; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'add_guarantor() tests' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + t::lib::Mocks::mock_preference( 'borrowerRelationship', 'father1|father2' ); + + my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); + my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); + + throws_ok + { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber }); } + 'Koha::Exceptions::Patron::Relationship::InvalidRelationship', + 'Exception is thrown as no relationship passed'; + + is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' ); + + throws_ok + { $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father' }); } + 'Koha::Exceptions::Patron::Relationship::InvalidRelationship', + 'Exception is thrown as a wrong relationship was passed'; + + is( $patron_1->guarantee_relationships->count, 0, 'No guarantors added' ); + + $patron_1->add_guarantor({ guarantor_id => $patron_2->borrowernumber, relationship => 'father1' }); + + my $guarantors = $patron_1->guarantor_relationships; + + is( $guarantors->count, 1, 'No guarantors added' ); + + $schema->storage->txn_rollback; +}; -- 2.22.0