[Koha-patches] [PATCH 2/3] Bug 17933: Add test and return unless defined dob

Dobrica Pavlinusic dpavlin at rot13.org
Wed Feb 1 13:50:09 CET 2017


From: Jonathan Druart <jonathan.druart at bugs.koha-community.org>

Without this patch get_age return actually 0 instead of

Signed-off-by: Dobrica Pavlinusic <dpavlin at rot13.org>
---
 Koha/Patron.pm                | 4 ++--
 t/db_dependent/Koha/Patrons.t | 4 +++-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/Koha/Patron.pm b/Koha/Patron.pm
index 1b9ece4..28289c5 100644
--- a/Koha/Patron.pm
+++ b/Koha/Patron.pm
@@ -544,8 +544,8 @@ Return the age of the patron
 sub get_age {
     my ($self)    = @_;
     my $today_str = dt_from_string->strftime("%Y-%m-%d");
-    my $dob_str   = dt_from_string( $self->dateofbirth ) || return;
-    $dob_str      = $dob_str->strftime("%Y-%m-%d");
+    return unless $self->dateofbirth;
+    my $dob_str   = dt_from_string( $self->dateofbirth )->strftime("%Y-%m-%d");
 
     my ( $dob_y,   $dob_m,   $dob_d )   = split /-/, $dob_str;
     my ( $today_y, $today_m, $today_d ) = split /-/, $today_str;
diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t
index e5fcba8..8012568 100644
--- a/t/db_dependent/Koha/Patrons.t
+++ b/t/db_dependent/Koha/Patrons.t
@@ -487,13 +487,15 @@ subtest 'checkouts + get_overdues' => sub {
 };
 
 subtest 'get_age' => sub {
-    plan tests => 6;
+    plan tests => 7;
 
     my $patron = $builder->build( { source => 'Borrower' } );
     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
 
     my $today = dt_from_string;
 
+    $patron->dateofbirth( undef );
+    is( $patron->get_age, undef, 'get_age should return undef if no dateofbirth is defined' );
     $patron->dateofbirth( $today->clone->add( years => -12, months => -6, days => -1 ) );
     is( $patron->get_age, 12, 'Patron should be 12' );
     $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 1 ) );
-- 
2.1.4



More information about the Koha-patches mailing list