From fa111bd7ae54417ef167431443bb155f4a78c47b Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Thu, 1 Sep 2022 08:25:20 +0000 Subject: [PATCH] Bug 31503: Add $patron->consent Content-Type: text/plain; charset=utf-8 Test plan: Run t/db_dependent/Koha/Patron.t Signed-off-by: Marcel de Rooy --- Koha/Patron.pm | 23 +++++++++++++++++++++++ t/db_dependent/Koha/Patron.t | 21 ++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 8031257304..358f9241aa 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -43,6 +43,7 @@ use Koha::CurbsidePickups; use Koha::Old::Checkouts; use Koha::Patron::Attributes; use Koha::Patron::Categories; +use Koha::Patron::Consents; use Koha::Patron::Debarments; use Koha::Patron::HouseboundProfile; use Koha::Patron::HouseboundRole; @@ -2196,6 +2197,28 @@ sub virtualshelves { return Koha::Virtualshelves->_new_from_dbic( scalar $self->_result->virtualshelves ); } +=head3 consent + + my $consent = $patron->consent(TYPE); + + Returns the first consent of type TYPE (there should be only one) or a new instance + of Koha::Patron::Consent. + +=cut + +sub consent { + my ( $self, $type ) = @_; + Koha::Exceptions::MissingParameter->throw('Missing consent type') + if !$type; + my $consents = Koha::Patron::Consents->search({ + borrowernumber => $self->borrowernumber, + type => $type, + }); + return $consents && $consents->count + ? $consents->next + : Koha::Patron::Consent->new({ borrowernumber => $self->borrowernumber, type => $type }); +} + =head2 Internal methods =head3 _type diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index 977ec261d6..1d65f8e919 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 => 18; +use Test::More tests => 19; use Test::Exception; use Test::Warn; @@ -28,6 +28,7 @@ use Koha::Database; use Koha::DateUtils qw(dt_from_string); use Koha::ArticleRequests; use Koha::Patrons; +use Koha::Patron::Consents; use Koha::Patron::Relationships; use t::lib::TestBuilder; @@ -1292,3 +1293,21 @@ subtest 'encode_secret and decoded_secret' => sub { $schema->storage->txn_rollback; }; + +subtest 'test patron_consent' => sub { + plan tests => 4; + $schema->storage->txn_begin; + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + throws_ok { $patron->consent } 'Koha::Exceptions::MissingParameter', 'missing type'; + + my $consent = $patron->consent('GDPR_PROCESSING'); + is( ref $consent, 'Koha::Patron::Consent', 'return type check' ); + $consent->given_on( '2021-02-03' )->store; + undef $consent; + is( $patron->consent('GDPR_PROCESSING')->given_on, '2021-02-03 00:00:00', 'check date' ); + + is( $patron->consent('NOT_EXIST')->refused_on, undef, 'New empty object for new type' ); + + $schema->storage->txn_rollback; +}; -- 2.30.2