From dbb46521053cd9ca114935554cf8751aa1b60f60 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Wed, 26 Oct 2016 16:35:15 +0300 Subject: [PATCH] Bug 17499: Throw an exception on duplicate messaging preference When trying to add a duplicate messaging preference that has the same borrowernumber or category, and message_attribute_id as another preference, throw Koha::Exceptions::DuplicateObject. To test: 1. Run t/db_dependent/Koha/Patron/Message/Preferences.t --- Koha/Patron/Message/Preference.pm | 47 +++++++++++++++++++++++- t/db_dependent/Koha/Patron/Message/Preferences.t | 12 +++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/Koha/Patron/Message/Preference.pm b/Koha/Patron/Message/Preference.pm index 670e296..df0d0ec 100644 --- a/Koha/Patron/Message/Preference.pm +++ b/Koha/Patron/Message/Preference.pm @@ -22,6 +22,7 @@ use Modern::Perl; use Koha::Database; use Koha::Exceptions; use Koha::Patron::Categories; +use Koha::Patron::Message::Preferences; use Koha::Patrons; use base qw(Koha::Object); @@ -36,17 +37,61 @@ Koha::Patron::Message::Preference - Koha Patron Message Preference object class =cut +=head3 new + +my $preference = Koha::Patron::Message::Preference->new({ + borrowernumber => 123, + #categorycode => 'ABC', + message_attribute_id => 4, + wants_digest => 1, + days_in_advance => 7, +}); + +Creates a new messaging preference if no existing duplicates are found. Takes +either borrowernumber or categorycode, but not both. + +Throws Koha::Exceptions::DuplicateObject if trying to create a preference that +already exists for the same message_attribute_id and borrowernumber/categorycode. + +=cut + +sub new { + my ($class, $params) = shift; + + my $self = $class->SUPER::new(@_); + + my $previous = Koha::Patron::Message::Preferences->search({ + '-and' => [ + borrowernumber => $self->borrowernumber, + categorycode => $self->categorycode, + message_attribute_id => $self->message_attribute_id, + ] + }); + if ($previous->count) { + Koha::Exceptions::DuplicateObject->throw( + error => "A preference for this borrower/category and" + ." message_attribute_id already exists", + ); + } + + return $self; +} + =head3 store Makes a validation before actual Koha::Object->store so that proper exceptions can be thrown. See C for documentation about exceptions. +Deletes any previous messaging preferences to avoid duplication. + =cut sub store { my $self = shift; - return $self->validate->SUPER::store(@_); + $self->validate; + + return $self->SUPER::store(@_); } =head3 validate diff --git a/t/db_dependent/Koha/Patron/Message/Preferences.t b/t/db_dependent/Koha/Patron/Message/Preferences.t index 0527d78..413222f 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 => 3; + plan tests => 4; ok($preference = Koha::Patron::Message::Preference->new({ borrowernumber => $patron->{'borrowernumber'}, @@ -105,6 +105,16 @@ 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."); + eval { Koha::Patron::Message::Preference->new({ + borrowernumber => $patron->{'borrowernumber'}, + message_attribute_id => $attribute->message_attribute_id, + wants_digest => 0, + days_in_advance => 1, + })->store }; + is(ref $@, "Koha::Exceptions::DuplicateObject", + "Adding a duplicate preference" + ." => Koha::Exceptions::DuplicateObject"); + subtest 'Attempt to add a messaging preference with invalid parameters' => sub { plan tests => 6; -- 2.7.4