From 086890be5d00a0c1f6b3bebd557a2f8bf0b83acd 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 | 48 ++++++++++++++++++++++++ t/db_dependent/Koha/Patron/Message/Preferences.t | 43 ++++++++++++++++++++- 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/Koha/Patron/Message/Preference.pm b/Koha/Patron/Message/Preference.pm index ce055d7..447919e 100644 --- a/Koha/Patron/Message/Preference.pm +++ b/Koha/Patron/Message/Preference.pm @@ -38,6 +38,38 @@ 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, +}); + +Takes either borrowernumber or categorycode, but not both. + +days_in_advance may not be available. See message_attributes table for takes_days +configuration. + +wants_digest may not be available. See message_transports table for is_digest +configuration. + +You can instantiate a new object without custom validation errors, but when +storing, validation may throw exceptions. See C for more +documentation. + +=cut + +sub new { + my ($class, $params) = shift; + + my $self = $class->SUPER::new(@_); + + return $self; +} + =head3 store Makes a validation before actual Koha::Object->store so that proper exceptions @@ -62,6 +94,8 @@ Throws following exceptions regarding parameters. See $_->parameter to identify the parameter causing the exception. +Throws Koha::Exceptions::DuplicateObject if this preference already exists. + Returns Koha::Patron::Message::Preference object. =cut @@ -94,6 +128,20 @@ sub validate { ) unless Koha::Patron::Categories->find($self->categorycode); } + if (!$self->in_storage) { + my $previous = Koha::Patron::Message::Preferences->search({ + 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', + ); + } + } + my $attr; if ($self->days_in_advance || $self->wants_digest) { $attr = Koha::Patron::Message::Attributes->find( diff --git a/t/db_dependent/Koha/Patron/Message/Preferences.t b/t/db_dependent/Koha/Patron/Message/Preferences.t index b608981..7fa3688 100644 --- a/t/db_dependent/Koha/Patron/Message/Preferences.t +++ b/t/db_dependent/Koha/Patron/Message/Preferences.t @@ -49,7 +49,7 @@ subtest 'Test Koha::Patron::Message::Preferences' => sub { my $attribute = build_a_test_attribute(); subtest 'Test for a patron' => sub { - plan tests => 2; + plan tests => 3; my $patron = build_a_test_patron(); Koha::Patron::Message::Preference->new({ @@ -66,6 +66,17 @@ subtest 'Test Koha::Patron::Message::Preferences' => sub { ok($preference->borrower_message_preference_id > 0, 'Added a new messaging preference for patron.'); + subtest 'Test set not throwing an exception on duplicate object' => sub { + plan tests => 1; + + Koha::Patron::Message::Attributes->find({ + message_attribute_id => $attribute->message_attribute_id + })->set({ takes_days => 1 })->store; + $preference->set({ days_in_advance => 1 })->store; + is(ref($preference), 'Koha::Patron::Message::Preference', + 'Updating the preference does not cause duplicate object exception'); + }; + $preference->delete; is(Koha::Patron::Message::Preferences->search({ borrowernumber => $patron->borrowernumber, @@ -100,7 +111,7 @@ subtest 'Test Koha::Patron::Message::Preferences' => sub { }; subtest 'Test adding a new preference with invalid parameters' => sub { - plan tests => 3; + plan tests => 4; subtest 'Missing parameters' => sub { plan tests => 1; @@ -178,6 +189,34 @@ subtest 'Test adding a new preference with invalid parameters' => sub { $schema->storage->txn_rollback; }; + + subtest 'Duplicate object' => sub { + plan tests => 2; + + $schema->storage->txn_begin; + + my $attribute = build_a_test_attribute(); + my $patron = build_a_test_patron(); + my $preference = Koha::Patron::Message::Preference->new({ + borrowernumber => $patron->borrowernumber, + message_attribute_id => $attribute->message_attribute_id, + wants_digest => 0, + days_in_advance => undef, + })->store; + ok($preference->borrower_message_preference_id, + 'Added a new messaging preference for patron.'); + eval { Koha::Patron::Message::Preference->new({ + borrowernumber => $patron->borrowernumber, + message_attribute_id => $attribute->message_attribute_id, + wants_digest => 0, + days_in_advance => undef, + })->store }; + is(ref $@, 'Koha::Exceptions::DuplicateObject', + 'Adding a duplicate preference' + .' => Koha::Exceptions::DuplicateObject'); + + $schema->storage->txn_rollback; + }; }; sub build_a_test_attribute { -- 2.7.4