@@ -, +, @@ --- Koha/Patron/Message/Preferences.pm | 35 ++++++++++++++++++++++++ t/db_dependent/Koha/Patron/Message/Preferences.t | 34 ++++++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) --- a/Koha/Patron/Message/Preferences.pm +++ a/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 --- a/t/db_dependent/Koha/Patron/Message/Preferences.t +++ a/t/db_dependent/Koha/Patron/Message/Preferences.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 15; +use Test::More tests => 16; use t::lib::Mocks; use t::lib::TestBuilder; @@ -56,6 +56,38 @@ my $preference; my $transport_preference; my $transport_type; +subtest 'Test Koha::Patron::Message::Preferences->get_options' => sub { + plan tests => 5; + + 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 HASHref"); + my $message_attributes = Koha::Patron::Message::Attributes->search; + is(@$options, $message_attributes->count, "There are equal amount of" + ." options and message attributes"); + + subtest 'Make sure options are correct' => sub { + 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"); + } + } + }; +}; + subtest 'Add a test messaging transport type' => sub { plan tests => 2; --