View | Details | Raw Unified | Return to bug 24757
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Patrons.t (-19 / +46 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 40;
22
use Test::More tests => 4;
23
use Test::Warn;
23
use Test::Warn;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 708-737 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 $today = dt_from_string;
711
    my $dt = dt_from_string('2020-02-28');
712
712
713
    Time::Fake->offset( $dt->epoch );
713
    $patron->dateofbirth( undef );
714
    $patron->dateofbirth( undef );
714
    is( $patron->get_age, undef, 'get_age should return undef if no dateofbirth is defined' );
715
    is( $patron->get_age, undef, 'get_age should return undef if no dateofbirth is defined' );
715
    $patron->dateofbirth( $today->clone->add( years => -12, months => -6, days => -1, end_of_month => 'limit'  ) );
716
717
    my $add_m12_m6_m1 = '2007-08-27';
718
    $patron->dateofbirth( dt_from_string($add_m12_m6_m1, 'iso') );
716
    is( $patron->get_age, 12, 'Patron should be 12' );
719
    is( $patron->get_age, 12, 'Patron should be 12' );
717
    $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 1, end_of_month => 'limit'  ) );
720
721
    my $add_m18_0_p1 = '2002-02-28';
722
    $patron->dateofbirth( dt_from_string($add_m18_0_p1, 'iso'));
718
    is( $patron->get_age, 17, 'Patron should be 17, happy birthday tomorrow!' );
723
    is( $patron->get_age, 17, 'Patron should be 17, happy birthday tomorrow!' );
719
    $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 0, end_of_month => 'limit'  ) );
724
725
    my $add_m18_0_0 = '2002-02-28';
726
    $patron->dateofbirth( dt_from_string($add_m18_0_0, 'iso'));
720
    is( $patron->get_age, 18, 'Patron should be 18' );
727
    is( $patron->get_age, 18, 'Patron should be 18' );
721
    $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -31, end_of_month => 'limit'  ) );
728
729
    my $add_m18_m12_m31 = '2001-01-28';
730
    $patron->dateofbirth( dt_from_string($add_m18_m12_m31, 'iso'));
722
    is( $patron->get_age, 19, 'Patron should be 19' );
731
    is( $patron->get_age, 19, 'Patron should be 19' );
723
    $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -30, end_of_month => 'limit'  ) );
732
733
    my $add_m18_m12_m30 = '2001-01-29';
734
    $patron->dateofbirth( dt_from_string($add_m18_m12_m30, 'iso' ));
724
    is( $patron->get_age, 19, 'Patron should be 19 again' );
735
    is( $patron->get_age, 19, 'Patron should be 19 again' );
725
    $patron->dateofbirth( $today->clone->add( years => 0,   months => -1, days => -1, end_of_month => 'limit'  ) );
736
737
    my $add_0_m1_m1 = '2020-01-27';
738
    $patron->dateofbirth( dt_from_string($add_0_m1_m1, 'iso' ));
726
    is( $patron->get_age, 0, 'Patron is a newborn child' );
739
    is( $patron->get_age, 0, 'Patron is a newborn child' );
727
740
741
    Time::Fake->reset;
742
728
    $patron->delete;
743
    $patron->delete;
729
};
744
};
730
745
731
subtest 'is_valid_age' => sub {
746
subtest 'is_valid_age' => sub {
732
    plan tests => 10;
747
    plan tests => 10;
733
748
734
    my $today = dt_from_string;
749
    my $dt = dt_from_string('2020-02-28');
750
751
    Time::Fake->offset( $dt->epoch );
735
752
736
    my $category = $builder->build({
753
    my $category = $builder->build({
737
        source => 'Category',
754
        source => 'Category',
Lines 755-787 subtest 'is_valid_age' => sub { Link Here
755
    $patron->dateofbirth( undef );
772
    $patron->dateofbirth( undef );
756
    is( $patron->is_valid_age, 1, 'Patron with no dateofbirth is always valid for any category');
773
    is( $patron->is_valid_age, 1, 'Patron with no dateofbirth is always valid for any category');
757
774
758
    $patron->dateofbirth( $today->clone->add( years => -12, months => -6, days => -1 ) );
775
    my $add_m12_m6_m1 = '2007-08-27';
776
    $patron->dateofbirth( dt_from_string($add_m12_m6_m1, 'iso') );
759
    is( $patron->is_valid_age, 0, 'Patron is 12, so the age is above allowed range 5-10 years');
777
    is( $patron->is_valid_age, 0, 'Patron is 12, so the age is above allowed range 5-10 years');
760
778
761
    $patron->dateofbirth( $today->clone->add( years => -3, months => -6, days => -1 ) );
779
    my $add_m3_m6_m1 = '2016-08-27';
780
    $patron->dateofbirth( dt_from_string($add_m3_m6_m1, 'iso'));
762
    is( $patron->is_valid_age, 0, 'Patron is 3, so the age is below allowed range 5-10 years');
781
    is( $patron->is_valid_age, 0, 'Patron is 3, so the age is below allowed range 5-10 years');
763
782
764
    $patron->dateofbirth( $today->clone->add( years => -7, months => -6, days => -1 ) );
783
    my $add_m7_m6_m1 = '2015-02-28';
784
    $patron->dateofbirth( dt_from_string($add_m7_m6_m1, 'iso'));
765
    is( $patron->is_valid_age, 1, 'Patron is 7, so the age perfectly suits allowed range 5-10 years');
785
    is( $patron->is_valid_age, 1, 'Patron is 7, so the age perfectly suits allowed range 5-10 years');
766
786
767
    $patron->dateofbirth( $today->clone->add( years => -5, months => 0, days => 0 ) );
787
    my $add_m5_0_1 = '2015-02-28';
788
    $patron->dateofbirth( dt_from_string($add_m5_0_1, 'iso' ));
768
    is( $patron->is_valid_age, 1, 'Patron celebrates the 5th birthday today, so the age is allowed for this category');
789
    is( $patron->is_valid_age, 1, 'Patron celebrates the 5th birthday today, so the age is allowed for this category');
769
790
770
    $patron->dateofbirth( $today->clone->add( years => -5, months => 0, days => 1 ) );
791
    my $add_m5_0_p1 = '2015-02-28';
792
    $patron->dateofbirth( dt_from_string($add_m5_0_p1, 'iso'));
771
    is( $patron->is_valid_age, 0, 'Patron will celebrate the 5th birthday tomorrow, so the age is NOT allowed for this category');
793
    is( $patron->is_valid_age, 0, 'Patron will celebrate the 5th birthday tomorrow, so the age is NOT allowed for this category');
772
794
773
    $patron->dateofbirth( $today->clone->add( years => -5, months => 0, days => -1 ) );
795
    my $add_m5_0_m1 = '2015-02-27';
796
    $patron->dateofbirth( dt_from_string($add_m5_0_m1, 'iso'));
774
    is( $patron->is_valid_age, 1, 'Patron celebrated the 5th birthday yesterday, so the age is allowed for this category');
797
    is( $patron->is_valid_age, 1, 'Patron celebrated the 5th birthday yesterday, so the age is allowed for this category');
775
798
776
    $patron->dateofbirth( $today->clone->add( years => -11, months => 0, days => 0 ) );
799
    my $add_m11_0_p0 = '2009-02-28';
800
    $patron->dateofbirth( dt_from_string($add_m11_0_p0, 'iso'));
777
    is( $patron->is_valid_age, 0, 'Patron celebrate the 11th birthday today, so the age is NOT allowed for this category');
801
    is( $patron->is_valid_age, 0, 'Patron celebrate the 11th birthday today, so the age is NOT allowed for this category');
778
802
779
    $patron->dateofbirth( $today->clone->add( years => -11, months => 0, days => 1 ) );
803
    my $add_m11_0_p1 = '2009-02-28';
804
    $patron->dateofbirth( dt_from_string($add_m11_0_p1, 'iso'));
780
    is( $patron->is_valid_age, 1, 'Patron will celebrate the 11th birthday tomorrow, so the age is allowed for this category');
805
    is( $patron->is_valid_age, 1, 'Patron will celebrate the 11th birthday tomorrow, so the age is allowed for this category');
781
806
782
    $patron->dateofbirth( $today->clone->add( years => -11, months => 0, days => -1 ) );
807
    my $add_m11_0_m1 = '2009-02-27';
808
    $patron->dateofbirth( dt_from_string($add_m11_0_m1, 'iso' ));
783
    is( $patron->is_valid_age, 0, 'Patron celebrated the 11th birthday yesterday, so the age is NOT allowed for this category');
809
    is( $patron->is_valid_age, 0, 'Patron celebrated the 11th birthday yesterday, so the age is NOT allowed for this category');
784
810
811
    Time::Fake->reset;
812
785
    $patron->delete;
813
    $patron->delete;
786
    $category->delete;
814
    $category->delete;
787
};
815
};
788
- 

Return to bug 24757