From 94e7efdb6139efd1f6f3f21afc4a6087ad546df5 Mon Sep 17 00:00:00 2001
From: Lari Taskula <lari.taskula@hypernova.fi>
Date: Thu, 18 Mar 2021 03:16:11 +0000
Subject: [PATCH] Bug 17499: (follow-up) Add $patron->messaging_preferences
 accessor

To test:
1. prove t/db_dependent/Koha/Patron.t

Sponsored-by: The National Library of Finland
---
 Koha/Patron.pm               | 14 ++++++++++
 t/db_dependent/Koha/Patron.t | 53 +++++++++++++++++++++++++++++++++++-
 2 files changed, 66 insertions(+), 1 deletion(-)

diff --git a/Koha/Patron.pm b/Koha/Patron.pm
index 80c88e9716..16806d4cb0 100644
--- a/Koha/Patron.pm
+++ b/Koha/Patron.pm
@@ -647,6 +647,20 @@ sub merge_with {
 }
 
 
+=head3 messaging_preferences
+
+    my $patron = Koha::Patrons->find($id);
+    $patron->messaging_preferences();
+
+=cut
+
+sub messaging_preferences {
+    my ( $self ) = @_;
+
+    return Koha::Patron::MessagePreferences->search({
+        borrowernumber => $self->borrowernumber,
+    });
+}
 
 =head3 wants_check_for_previous_checkout
 
diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t
index 1b0d24552a..7f3578af73 100755
--- a/t/db_dependent/Koha/Patron.t
+++ b/t/db_dependent/Koha/Patron.t
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 6;
+use Test::More tests => 7;
 use Test::Exception;
 
 use Koha::Database;
@@ -283,6 +283,57 @@ subtest 'add_enrolment_fee_if_needed() tests' => sub {
     };
 };
 
+subtest 'messaging_preferences() tests' => sub {
+    plan tests => 5;
+
+    $schema->storage->txn_begin;
+
+    my $mtt = $builder->build_object({
+        class => 'Koha::Patron::MessagePreference::Transport::Types'
+    });
+    my $attribute = $builder->build_object({
+        class => 'Koha::Patron::MessagePreference::Attributes'
+    });
+    my $branchcode     = $builder->build({
+        source => 'Branch' })->{branchcode};
+    my $letter = $builder->build_object({
+        class => 'Koha::Notice::Templates',
+        value => {
+            branchcode => '',
+            is_html => 0,
+            message_transport_type => $mtt->message_transport_type
+        }
+    });
+
+    Koha::Patron::MessagePreference::Transport->new({
+        message_attribute_id   => $attribute->message_attribute_id,
+        message_transport_type => $mtt->message_transport_type,
+        is_digest              => 0,
+        letter_module          => $letter->module,
+        letter_code            => $letter->code,
+    })->store;
+
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+
+    my $preference = Koha::Patron::MessagePreference->new({
+        borrowernumber => $patron->borrowernumber,
+        message_attribute_id => $attribute->message_attribute_id,
+        wants_digest => 0,
+        days_in_advance => undef,
+    })->store;
+
+    my $messaging_preferences = $patron->messaging_preferences();
+    is($messaging_preferences->count, 1, 'Found one preference');
+
+    my $messaging_preference = $messaging_preferences->next;
+    is($messaging_preference->borrowernumber, $patron->borrowernumber);
+    is($messaging_preference->message_attribute_id, $attribute->message_attribute_id);
+    is($messaging_preference->wants_digest, 0);
+    is($messaging_preference->days_in_advance, undef);
+
+    $schema->storage->txn_rollback;
+};
+
 subtest 'to_api() tests' => sub {
 
     plan tests => 6;
-- 
2.25.1