From 9b9242d8ca7d29db1f2a5e07e4dd181c832bc957 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 Test plan: Run t/db_dependent/Koha/Patron.t Signed-off-by: Marcel de Rooy Signed-off-by: David Nind --- Koha/Patron.pm | 23 +++++++++++++++++++++++ t/db_dependent/Koha/Patron.t | 20 +++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 8d2cf1fe44..3cc927d3f5 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; @@ -2247,6 +2248,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 fa035b8c04..3d0e224538 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 => 19; +use Test::More tests => 20; 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; @@ -1345,6 +1346,23 @@ subtest 'notify_library_of_registration()' => sub { $to_address = $sth->fetchrow_array; is( $to_address, 'root@localhost', 'OPAC_REG email queued to go to KohaAdminEmailAddress syspref when EmailPatronRegistration equals KohaAdminEmailAddress' ); $dbh->do(q|DELETE FROM message_queue|); + $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