From 877381695cff3f977fee1204361118f1a655248a Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 7 Nov 2016 17:12:58 +0000 Subject: [PATCH] Bug 17579: Add the Koha::Patron->is_expired This new method will be handy for further uses Test plan: prove t/db_dependent/Koha/Patrons.t should return green --- Koha/Patron.pm | 16 ++++++++++++++++ t/db_dependent/Koha/Patrons.t | 20 +++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 9a2fd18..04a472e 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -266,6 +266,22 @@ sub is_debarred { return; } +=head2 is_expired + +my $is_expired = $patron->is_expired; + +Returns 1 if the patron is expired or 0; + +=cut + +sub is_expired { + my ($self) = @_; + return 0 unless $self->dateexpiry; + return 0 if $self->dateexpiry eq '0000-00-00'; + return 1 if dt_from_string( $self->dateexpiry ) < dt_from_string; + return 0; +} + =head2 update_password my $updated = $patron->update_password( $userid, $password ); diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index 6b85dcc..60b113a 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 11; +use Test::More tests => 12; use Test::Warn; use C4::Members; @@ -165,6 +165,24 @@ subtest 'update_password' => sub { is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' ); }; +subtest 'is_expired' => sub { + plan tests => 5; + my $patron = $builder->build({ source => 'Borrower' }); + $patron = Koha::Patrons->find( $patron->{borrowernumber} ); + $patron->dateexpiry( undef )->store; + is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not set'); + $patron->dateexpiry( '0000-00-00' )->store; + is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not 0000-00-00'); + $patron->dateexpiry( dt_from_string )->store; + is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is today'); + $patron->dateexpiry( dt_from_string->add( days => 1 ) )->store; + is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is tomorrow'); + $patron->dateexpiry( dt_from_string->add( days => -1 ) )->store; + is( $patron->is_expired, 1, 'Patron should be considered expired if dateexpiry is yesterday'); + + $patron->delete; +}; + subtest 'renew_account' => sub { plan tests => 10; my $a_month_ago = dt_from_string->add( months => -1 )->truncate( to => 'day' ); -- 2.1.4