Lines 708-744
subtest 'get_age' => sub {
Link Here
|
708 |
my $patron = $builder->build( { source => 'Borrower' } ); |
708 |
my $patron = $builder->build( { source => 'Borrower' } ); |
709 |
$patron = Koha::Patrons->find( $patron->{borrowernumber} ); |
709 |
$patron = Koha::Patrons->find( $patron->{borrowernumber} ); |
710 |
|
710 |
|
711 |
my $dt = dt_from_string('2020-02-28'); |
711 |
my @dates = ( |
|
|
712 |
{ |
713 |
today => '2020-02-28', |
714 |
add_m12_m6_m1 => { date => '2007-08-27', expected_age => 12 }, |
715 |
add_m18_0_p1 => { date => '2002-02-28', expected_age => 17 }, |
716 |
add_m18_0_0 => { date => '2002-02-28', expected_age => 18 }, |
717 |
add_m18_m12_m31 => { date => '2001-01-28', expected_age => 19 }, |
718 |
add_m18_m12_m30 => { date => '2001-01-29', expected_age => 19 } , |
719 |
add_0_m1_m1 => { date => '2020-01-27', expected_age => 0 }, |
720 |
}, |
721 |
); |
712 |
|
722 |
|
713 |
Time::Fake->offset( $dt->epoch ); |
|
|
714 |
$patron->dateofbirth( undef ); |
723 |
$patron->dateofbirth( undef ); |
715 |
is( $patron->get_age, undef, 'get_age should return undef if no dateofbirth is defined' ); |
724 |
is( $patron->get_age, undef, 'get_age should return undef if no dateofbirth is defined' ); |
716 |
|
725 |
|
717 |
my $add_m12_m6_m1 = '2007-08-27'; |
726 |
for my $date ( @dates ) { |
718 |
$patron->dateofbirth( dt_from_string($add_m12_m6_m1, 'iso') ); |
|
|
719 |
is( $patron->get_age, 12, 'Patron should be 12' ); |
720 |
|
727 |
|
721 |
my $add_m18_0_p1 = '2002-02-28'; |
728 |
my $dt = dt_from_string($date->{today}); |
722 |
$patron->dateofbirth( dt_from_string($add_m18_0_p1, 'iso')); |
|
|
723 |
is( $patron->get_age, 17, 'Patron should be 17, happy birthday tomorrow!' ); |
724 |
|
729 |
|
725 |
my $add_m18_0_0 = '2002-02-28'; |
730 |
Time::Fake->offset( $dt->epoch ); |
726 |
$patron->dateofbirth( dt_from_string($add_m18_0_0, 'iso')); |
|
|
727 |
is( $patron->get_age, 18, 'Patron should be 18' ); |
728 |
|
729 |
my $add_m18_m12_m31 = '2001-01-28'; |
730 |
$patron->dateofbirth( dt_from_string($add_m18_m12_m31, 'iso')); |
731 |
is( $patron->get_age, 19, 'Patron should be 19' ); |
732 |
|
731 |
|
733 |
my $add_m18_m12_m30 = '2001-01-29'; |
732 |
for my $k ( keys %$date ) { |
734 |
$patron->dateofbirth( dt_from_string($add_m18_m12_m30, 'iso' )); |
733 |
next if $k eq 'today'; |
735 |
is( $patron->get_age, 19, 'Patron should be 19 again' ); |
734 |
|
|
|
735 |
my $dob = $date->{$k}; |
736 |
$patron->dateofbirth( dt_from_string( $dob->{date}, 'iso' ) ); |
737 |
is( |
738 |
$patron->get_age, |
739 |
$dob->{expected_age}, |
740 |
sprintf( |
741 |
"Today=%s, dob=%s, should be %d", |
742 |
$date->{today}, $dob->{date}, $dob->{expected_age} |
743 |
) |
744 |
); |
745 |
} |
736 |
|
746 |
|
737 |
my $add_0_m1_m1 = '2020-01-27'; |
747 |
Time::Fake->reset; |
738 |
$patron->dateofbirth( dt_from_string($add_0_m1_m1, 'iso' )); |
|
|
739 |
is( $patron->get_age, 0, 'Patron is a newborn child' ); |
740 |
|
748 |
|
741 |
Time::Fake->reset; |
749 |
} |
742 |
|
750 |
|
743 |
$patron->delete; |
751 |
$patron->delete; |
744 |
}; |
752 |
}; |
Lines 772-814
subtest 'is_valid_age' => sub {
Link Here
|
772 |
$patron->dateofbirth( undef ); |
780 |
$patron->dateofbirth( undef ); |
773 |
is( $patron->is_valid_age, 1, 'Patron with no dateofbirth is always valid for any category'); |
781 |
is( $patron->is_valid_age, 1, 'Patron with no dateofbirth is always valid for any category'); |
774 |
|
782 |
|
775 |
my $add_m12_m6_m1 = '2007-08-27'; |
783 |
my @dates = ( |
776 |
$patron->dateofbirth( dt_from_string($add_m12_m6_m1, 'iso') ); |
784 |
{ |
777 |
is( $patron->is_valid_age, 0, 'Patron is 12, so the age is above allowed range 5-10 years'); |
785 |
today => '2020-02-28', |
778 |
|
786 |
add_m12_m6_m1 => |
779 |
my $add_m3_m6_m1 = '2016-08-27'; |
787 |
{ date => '2007-08-27', expected_age => 12, valid => 0 }, |
780 |
$patron->dateofbirth( dt_from_string($add_m3_m6_m1, 'iso')); |
788 |
add_m3_m6_m1 => |
781 |
is( $patron->is_valid_age, 0, 'Patron is 3, so the age is below allowed range 5-10 years'); |
789 |
{ date => '2016-08-27', expected_age => 3, valid => 0 }, |
782 |
|
790 |
add_m7_m6_m1 => |
783 |
my $add_m7_m6_m1 = '2015-02-28'; |
791 |
{ date => '2015-02-28', expected_age => 7, valid => 1 }, |
784 |
$patron->dateofbirth( dt_from_string($add_m7_m6_m1, 'iso')); |
792 |
add_m5_0_0 => |
785 |
is( $patron->is_valid_age, 1, 'Patron is 7, so the age perfectly suits allowed range 5-10 years'); |
793 |
{ date => '2015-02-28', expected_age => 5, valid => 1 }, |
786 |
|
794 |
add_m5_0_p1 => |
787 |
my $add_m5_0_1 = '2015-02-28'; |
795 |
{ date => '2015-02-28', expected_age => 5, valid => 0 }, |
788 |
$patron->dateofbirth( dt_from_string($add_m5_0_1, 'iso' )); |
796 |
add_m5_0_m1 => |
789 |
is( $patron->is_valid_age, 1, 'Patron celebrates the 5th birthday today, so the age is allowed for this category'); |
797 |
{ date => '2015-02-27', expected_age => 5, valid => 1 }, |
|
|
798 |
add_m11_0_0 => |
799 |
{ date => '2009-02-28', expected_age => 11, valid => 0 }, |
800 |
add_m11_0_p1 => |
801 |
{ date => '2009-02-28', expected_age => 11, valid => 1 }, |
802 |
add_m11_0_m1 => |
803 |
{ date => '2009-02-27', expected_age => 11, valid => 0 }, |
804 |
}, |
805 |
); |
790 |
|
806 |
|
791 |
my $add_m5_0_p1 = '2015-02-28'; |
807 |
for my $date ( @dates ) { |
792 |
$patron->dateofbirth( dt_from_string($add_m5_0_p1, 'iso')); |
|
|
793 |
is( $patron->is_valid_age, 0, 'Patron will celebrate the 5th birthday tomorrow, so the age is NOT allowed for this category'); |
794 |
|
808 |
|
795 |
my $add_m5_0_m1 = '2015-02-27'; |
809 |
my $dt = dt_from_string($date->{today}); |
796 |
$patron->dateofbirth( dt_from_string($add_m5_0_m1, 'iso')); |
|
|
797 |
is( $patron->is_valid_age, 1, 'Patron celebrated the 5th birthday yesterday, so the age is allowed for this category'); |
798 |
|
810 |
|
799 |
my $add_m11_0_p0 = '2009-02-28'; |
811 |
Time::Fake->offset( $dt->epoch ); |
800 |
$patron->dateofbirth( dt_from_string($add_m11_0_p0, 'iso')); |
|
|
801 |
is( $patron->is_valid_age, 0, 'Patron celebrate the 11th birthday today, so the age is NOT allowed for this category'); |
802 |
|
812 |
|
803 |
my $add_m11_0_p1 = '2009-02-28'; |
813 |
for my $k ( keys %$date ) { |
804 |
$patron->dateofbirth( dt_from_string($add_m11_0_p1, 'iso')); |
814 |
next if $k eq 'today'; |
805 |
is( $patron->is_valid_age, 1, 'Patron will celebrate the 11th birthday tomorrow, so the age is allowed for this category'); |
815 |
|
|
|
816 |
my $dob = $date->{$k}; |
817 |
$patron->dateofbirth( dt_from_string( $dob->{date}, 'iso' ) ); |
818 |
is( |
819 |
$patron->is_valid_age, |
820 |
$dob->{valid}, |
821 |
sprintf( |
822 |
"Today=%s, dob=%s, is %s, should be valid=%s", |
823 |
$date->{today}, $dob->{date}, $dob->{expected_age}, $dob->{valid} |
824 |
) |
825 |
); |
826 |
} |
806 |
|
827 |
|
807 |
my $add_m11_0_m1 = '2009-02-27'; |
828 |
Time::Fake->reset; |
808 |
$patron->dateofbirth( dt_from_string($add_m11_0_m1, 'iso' )); |
|
|
809 |
is( $patron->is_valid_age, 0, 'Patron celebrated the 11th birthday yesterday, so the age is NOT allowed for this category'); |
810 |
|
829 |
|
811 |
Time::Fake->reset; |
830 |
} |
812 |
|
831 |
|
813 |
$patron->delete; |
832 |
$patron->delete; |
814 |
$category->delete; |
833 |
$category->delete; |
815 |
- |
|
|