From 864f0ecead71e992a78ee5315da52251ef3aabc3 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Thu, 27 Oct 2016 14:24:34 +0300 Subject: [PATCH] Bug 17499: Add a method for getting messaging options This patch adds a method for getting available messaging options. To test: 1. Run t/db_dependent/Koha/Patron/Message/Preferences.t --- Koha/Patron/Message/Preferences.pm | 35 +++++++++++++++++++++ t/db_dependent/Koha/Patron/Message/Preferences.t | 40 +++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/Koha/Patron/Message/Preferences.pm b/Koha/Patron/Message/Preferences.pm index 4639a6b..bf3d14c 100644 --- a/Koha/Patron/Message/Preferences.pm +++ b/Koha/Patron/Message/Preferences.pm @@ -21,6 +21,7 @@ use Modern::Perl; use Koha::Database; use Koha::Patron::Message::Preference; +use Koha::Patron::Message::Transports; use base qw(Koha::Objects); @@ -34,6 +35,40 @@ Koha::Patron::Message::Preferences - Koha Patron Message Preferences object clas =cut +=head3 get_options + +my $messaging_options = Koha::Patron::Message::Preferences->get_options + +Returns an ARRAYref of HASHrefs on available messaging options. + +=cut + +sub get_options { + my ($self) = @_; + + my $transports = Koha::Patron::Message::Transports->search(undef, + { + join => ['message_attribute'], + '+select' => ['message_attribute.message_name', 'message_attribute.takes_days'], + '+as' => ['message_name', 'takes_days'], + }); + + my $choices; + while (my $transport = $transports->next) { + my $name = $transport->get_column('message_name'); + $choices->{$name}->{'message_attribute_id'} = $transport->message_attribute_id; + $choices->{$name}->{'message_name'} = $name; + $choices->{$name}->{'takes_days'} = $transport->get_column('takes_days'); + $choices->{$name}->{'has_digest'} = 1 if $transport->is_digest; + $choices->{$name}->{'transport_'.$transport->get_column('message_transport_type')} = ' '; + } + + my @return = values %$choices; + @return = sort { $a->{message_attribute_id} <=> $b->{message_attribute_id} } @return; + + return \@return; +} + =head3 type =cut diff --git a/t/db_dependent/Koha/Patron/Message/Preferences.t b/t/db_dependent/Koha/Patron/Message/Preferences.t index 7fa3688..2f20039 100644 --- a/t/db_dependent/Koha/Patron/Message/Preferences.t +++ b/t/db_dependent/Koha/Patron/Message/Preferences.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 4; use t::lib::Mocks; use t::lib::TestBuilder; @@ -110,6 +110,44 @@ subtest 'Test Koha::Patron::Message::Preferences' => sub { $schema->storage->txn_rollback; }; +subtest 'Test Koha::Patron::Message::Preferences->get_options' => sub { + plan tests => 2; + + subtest 'Test method availability and return value' => sub { + plan tests => 3; + + ok(Koha::Patron::Message::Preferences->can('get_options'), + 'Method get_options is available.'); + ok(my $options = Koha::Patron::Message::Preferences->get_options, + 'Called get_options successfully.'); + is(ref($options), 'ARRAY', 'get_options returns a ARRAYref'); + }; + + subtest 'Make sure options are correct' => sub { + $schema->storage->txn_begin; + my $options = Koha::Patron::Message::Preferences->get_options; + + foreach my $option (@$options) { + my $n = $option->{'message_name'}; + my $attr = Koha::Patron::Message::Attributes->find($option->{'message_attribute_id'}); + is($option->{'message_attribute_id'}, $attr->message_attribute_id, + '$n: message_attribute_id is set'); + is($option->{'message_name'}, $attr->message_name, '$n: message_name is set'); + is($option->{'takes_days'}, $attr->takes_days, '$n: takes_days is set'); + my $transports = Koha::Patron::Message::Transports->search({ + message_attribute_id => $option->{'message_attribute_id'}, + is_digest => $option->{'has_digest'} || 0, + }); + while (my $trnzport = $transports->next) { + is($option->{'has_digest'} || 0, $trnzport->is_digest, '$n: has_digest is set for '.$trnzport->message_transport_type); + is($option->{'transport_'.$trnzport->message_transport_type}, ' ', '$n: transport_'.$trnzport->message_transport_type.' is set'); + } + } + + $schema->storage->txn_rollback; + }; +}; + subtest 'Test adding a new preference with invalid parameters' => sub { plan tests => 4; -- 2.7.4