From 5816b312a7e16b55c53086e4ad4801eefc83bb42 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Wed, 26 Oct 2016 14:54:40 +0300 Subject: [PATCH] Bug 17499: Add basic validation for messaging preference This patch adds simple validation for messaging preferences. The validation includes - check that only either borrowernumber or categorycode is given, but not both - throw exception if patron for the given borrowernumber is not found - throw exception if category for the given categorycode is not found To test: 1. Run t/db_dependent/Koha/Patron/Message/Preferences.t --- Koha/Patron/Message/Preference.pm | 62 ++++++++++++++++++++++++ t/db_dependent/Koha/Patron/Message/Preferences.t | 33 ++++++++++++- 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/Koha/Patron/Message/Preference.pm b/Koha/Patron/Message/Preference.pm index daeae5c..670e296 100644 --- a/Koha/Patron/Message/Preference.pm +++ b/Koha/Patron/Message/Preference.pm @@ -20,6 +20,9 @@ package Koha::Patron::Message::Preference; use Modern::Perl; use Koha::Database; +use Koha::Exceptions; +use Koha::Patron::Categories; +use Koha::Patrons; use base qw(Koha::Object); @@ -33,6 +36,65 @@ Koha::Patron::Message::Preference - Koha Patron Message Preference object class =cut +=head3 store + +Makes a validation before actual Koha::Object->store so that proper exceptions +can be thrown. See C for documentation about exceptions. + +=cut + +sub store { + my $self = shift; + + return $self->validate->SUPER::store(@_); +} + +=head3 validate + +Makes a basic validation for object. + +Throws following exceptions regarding parameters. +- Koha::Exceptions::MissingParameter +- Koha::Exceptions::TooManyParameters +- Koha::Exceptions::BadParameter + +See $_->parameter to identify the parameter causing the exception. + +Returns Koha::Patron::Message::Preference object. + +=cut + +sub validate { + my ($self) = @_; + + if ($self->borrowernumber && $self->categorycode) { + Koha::Exceptions::TooManyParameters->throw( + error => "Both borrowernumber and category given, only one accepted", + parameter => ['borrowernumber', 'categorycode'], + ); + } + if (!$self->borrowernumber && !$self->categorycode) { + Koha::Exceptions::MissingParameter->throw( + error => "borrowernumber or category required, none given", + parameter => ['borrowernumber', 'categorycode'], + ); + } + if ($self->borrowernumber) { + Koha::Exceptions::BadParameter->throw( + error => "Patron not found.", + parameter => 'borrowernumber', + ) unless Koha::Patrons->find($self->borrowernumber); + } + if ($self->categorycode) { + Koha::Exceptions::BadParameter->throw( + error => "Category not found.", + parameter => 'categorycode', + ) unless Koha::Patron::Categories->find($self->categorycode); + } + + return $self; +} + =head3 type =cut diff --git a/t/db_dependent/Koha/Patron/Message/Preferences.t b/t/db_dependent/Koha/Patron/Message/Preferences.t index 3d87943..0527d78 100644 --- a/t/db_dependent/Koha/Patron/Message/Preferences.t +++ b/t/db_dependent/Koha/Patron/Message/Preferences.t @@ -94,7 +94,7 @@ subtest 'Add a test messaging transport' => sub { }; subtest 'Add a messaging preference to patron' => sub { - plan tests => 2; + plan tests => 3; ok($preference = Koha::Patron::Message::Preference->new({ borrowernumber => $patron->{'borrowernumber'}, @@ -105,6 +105,37 @@ subtest 'Add a messaging preference to patron' => sub { is(Koha::Patron::Message::Preferences->find({ borrowernumber => $patron->{'borrowernumber'} })->message_attribute_id, $preference->message_attribute_id, "Found test messaging preference from database."); + subtest 'Attempt to add a messaging preference with invalid parameters' => sub { + plan tests => 6; + + eval { Koha::Patron::Message::Preference->new->store }; + is (ref $@, "Koha::Exceptions::MissingParameter", + "Adding a message preference without parameters" + ." => Koha::Exceptions::MissingParameter"); + eval { Koha::Patron::Message::Preference->new({ + borrowernumber => $patron->{'borrowernumber'}, + categorycode => $patron->{'categorycode'}, + })->store }; + is (ref $@, "Koha::Exceptions::TooManyParameters", + "Adding a message preference with both borrowernumber and categorycode" + ." => Koha::Exceptions::TooManyParameters"); + eval { Koha::Patron::Message::Preference->new({ + borrowernumber => -999, + })->store }; + is (ref $@, "Koha::Exceptions::BadParameter", + "Adding a message preference with invalid borrowernumber" + ." => Koha::Exceptions::BadParameter"); + is ($@->parameter, "borrowernumber", "The previous exception tells us it" + ." was the borrowernumber."); + eval { Koha::Patron::Message::Preference->new({ + categorycode => "nonexistent", + })->store }; + is (ref $@, "Koha::Exceptions::BadParameter", + "Adding a message preference with invalid categorycode" + ." => Koha::Exceptions::BadParameter"); + is ($@->parameter, "categorycode", "The previous exception tells us it" + ." was the categorycode."); + }; }; subtest 'Add a messaging transport preference to patron' => sub { -- 2.7.4