From e0cfaf636039fefa38335571c227e1eaf3eec3c6 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Tue, 8 Nov 2016 13:29:07 +0200 Subject: [PATCH] [SIGNED-OFF] Bug 17499: Simplify searching by message_name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch adds optional search & find parameter message_name for Koha::Patron::Message::Preferences->search and ->find. This simplifies object usage by allowing us to skip joins or finding the attribute id ourselves. prove -v t/db_dependent/Koha/Patron/Message/Preferences.t turns green Signed-off-by: Marc VĂ©ron Signed-off-by: Josef Moravec --- Koha/Patron/Message/Preference.pm | 27 +++++++++++- Koha/Patron/Message/Preferences.pm | 55 ++++++++++++++++++++++++ t/db_dependent/Koha/Patron/Message/Preferences.t | 23 +++++++++- 3 files changed, 101 insertions(+), 4 deletions(-) diff --git a/Koha/Patron/Message/Preference.pm b/Koha/Patron/Message/Preference.pm index c7e2b95..1df44af 100644 --- a/Koha/Patron/Message/Preference.pm +++ b/Koha/Patron/Message/Preference.pm @@ -156,6 +156,28 @@ sub new_from_default { return $self; } +=head3 message_name + +$preference->message_name + +Gets message_name for this messaging preference. + +Setter not implemented. + +=cut + +sub message_name { + my ($self) = @_; + + if ($self->{'_message_name'}) { + return $self->{'_message_name'}; + } + $self->{'_message_name'} = Koha::Patron::Message::Attributes->find({ + message_attribute_id => $self->message_attribute_id, + })->message_name; + return $self->{'_message_name'}; +} + =head3 message_transport_types $preference->message_transport_types @@ -385,8 +407,9 @@ sub _validate_message_transport_types { is_digest => $tmp->{'wants_digest'}, })) { Koha::Exceptions::BadParameter->throw( - error => "Message transport option for '$type' (". - ($tmp->{'wants_digest'} ? 'digest':'no digest').") does not exist", + error => "Message transport option '$type' (". + ($tmp->{'wants_digest'} ? 'digest':'no digest').") for ". + $self->message_name . 'does not exist', parameter => 'message_transport_type', ); } diff --git a/Koha/Patron/Message/Preferences.pm b/Koha/Patron/Message/Preferences.pm index bf3d14c..34af5e0 100644 --- a/Koha/Patron/Message/Preferences.pm +++ b/Koha/Patron/Message/Preferences.pm @@ -20,6 +20,7 @@ package Koha::Patron::Message::Preferences; use Modern::Perl; use Koha::Database; +use Koha::Patron::Message::Attributes; use Koha::Patron::Message::Preference; use Koha::Patron::Message::Transports; @@ -35,6 +36,32 @@ Koha::Patron::Message::Preferences - Koha Patron Message Preferences object clas =cut +=head3 find_with_message_name + +Koha::Patron::Message::Preferences->find_with_message_name({ + borrowernumber => 123, + message_name => 'Hold_Filled', +}); + +Converts C into C and continues find. + +=cut + +sub find_with_message_name { + my ($self, $id) = @_; + + if (ref($id) eq "HASH" && $id->{'message_name'}) { + my $attr = Koha::Patron::Message::Attributes->find({ + message_name => $id->{'message_name'}, + }); + $id->{'message_attribute_id'} = ($attr) ? + $attr->message_attribute_id : undef; + delete $id->{'message_name'}; + } + + return $self->SUPER::find($id); +} + =head3 get_options my $messaging_options = Koha::Patron::Message::Preferences->get_options @@ -69,6 +96,34 @@ sub get_options { return \@return; } +=head3 search + +Koha::Patron::Message::Preferences->search_with_message_name({ + borrowernumber => 123, + message_name => 'Hold_Filled', +}); + +Converts C into C and continues search. Use +Koha::Patron::Message::Preferences->search with a proper join for more complicated +searches. + +=cut + +sub search_with_message_name { + my ($self, $params, $attributes) = @_; + + if (ref($params) eq "HASH" && $params->{'message_name'}) { + my $attr = Koha::Patron::Message::Attributes->find({ + message_name => $params->{'message_name'}, + }); + $params->{'message_attribute_id'} = ($attr) ? + $attr->message_attribute_id : undef; + delete $params->{'message_name'}; + } + + return $self->SUPER::search($params, $attributes); +} + =head3 type =cut diff --git a/t/db_dependent/Koha/Patron/Message/Preferences.t b/t/db_dependent/Koha/Patron/Message/Preferences.t index 0c98222..8f5626a 100644 --- a/t/db_dependent/Koha/Patron/Message/Preferences.t +++ b/t/db_dependent/Koha/Patron/Message/Preferences.t @@ -19,8 +19,7 @@ use Modern::Perl; - -use Test::More tests => 6; +use Test::More tests => 7; use t::lib::Mocks; use t::lib::TestBuilder; @@ -320,6 +319,26 @@ subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub }; }; +subtest 'Test Koha::Patron::Message::Preference->message_name' => sub { + plan tests => 1; + + $schema->storage->txn_begin; + + my $patron = build_a_test_patron(); + my $attribute = build_a_test_attribute(); + my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({ + patron => $patron, + attr => $attribute, + }); + my $message_name_pref = Koha::Patron::Message::Preferences->search_with_message_name({ + borrowernumber => $patron->{'borrowernumber'}, + message_name => $attribute->message_name, + })->next; + is($message_name_pref->message_name, $attribute->message_name, "Found preference with message_name"); + + $schema->storage->txn_rollback; +}; + subtest 'Test adding a new preference with invalid parameters' => sub { plan tests => 4; -- 2.1.4