Lines 20-29
Link Here
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
|
21 |
|
22 |
use Test::NoWarnings; |
22 |
use Test::NoWarnings; |
23 |
use Test::More tests => 45; |
23 |
use Test::More tests => 46; |
24 |
use Test::Warn; |
24 |
use Test::Warn; |
25 |
use Test::Exception; |
25 |
use Test::Exception; |
26 |
use Test::MockModule; |
26 |
use Test::MockModule; |
|
|
27 |
use Test::Deep; |
27 |
use Time::Fake; |
28 |
use Time::Fake; |
28 |
use DateTime; |
29 |
use DateTime; |
29 |
use JSON; |
30 |
use JSON; |
Lines 2660-2665
subtest 'anonymize' => sub {
Link Here
|
2660 |
is( $patron2->firstname, undef, 'First name patron2 cleared' ); |
2661 |
is( $patron2->firstname, undef, 'First name patron2 cleared' ); |
2661 |
}; |
2662 |
}; |
2662 |
|
2663 |
|
|
|
2664 |
subtest "search_patrons_to_update_category tests" => sub { |
2665 |
|
2666 |
plan tests => 9; |
2667 |
|
2668 |
$schema->storage->txn_begin(); |
2669 |
|
2670 |
my $current_dt = dt_from_string(); |
2671 |
|
2672 |
# Set up category with age limits |
2673 |
my $category_code = 'TEST_CAT'; |
2674 |
my $category = $builder->build_object( |
2675 |
{ |
2676 |
class => 'Koha::Patron::Categories', |
2677 |
value => { |
2678 |
categorycode => $category_code, |
2679 |
description => 'Test category', |
2680 |
dateofbirthrequired => 13, # Must be 13 or older |
2681 |
upperagelimit => 17, # Must be 17 or younger |
2682 |
} |
2683 |
} |
2684 |
); |
2685 |
|
2686 |
# Create test patrons with various ages and fines |
2687 |
my $patron_12 = $builder->build_object( |
2688 |
{ # Too young |
2689 |
class => 'Koha::Patrons', |
2690 |
value => { |
2691 |
categorycode => $category_code, |
2692 |
dateofbirth => $current_dt->clone->subtract( years => 12 )->ymd, |
2693 |
} |
2694 |
} |
2695 |
); |
2696 |
|
2697 |
my $patron_13 = $builder->build_object( |
2698 |
{ # Minimum age (not too young) |
2699 |
class => 'Koha::Patrons', |
2700 |
value => { |
2701 |
categorycode => $category_code, |
2702 |
dateofbirth => $current_dt->clone->subtract( years => 13 )->ymd, |
2703 |
} |
2704 |
} |
2705 |
); |
2706 |
|
2707 |
my $patron_17 = $builder->build_object( |
2708 |
{ # Maximum age (not too old) |
2709 |
class => 'Koha::Patrons', |
2710 |
value => { |
2711 |
categorycode => $category_code, |
2712 |
dateofbirth => $current_dt->clone->subtract( years => 17 )->ymd, |
2713 |
} |
2714 |
} |
2715 |
); |
2716 |
|
2717 |
my $patron_18 = $builder->build_object( |
2718 |
{ # Too old |
2719 |
class => 'Koha::Patrons', |
2720 |
value => { |
2721 |
categorycode => $category_code, |
2722 |
dateofbirth => $current_dt->clone->subtract( years => 18 )->ymd, |
2723 |
} |
2724 |
} |
2725 |
); |
2726 |
|
2727 |
my $patron_no_dob = $builder->build_object( |
2728 |
{ # No date of birth |
2729 |
class => 'Koha::Patrons', |
2730 |
value => { |
2731 |
categorycode => $category_code, |
2732 |
dateofbirth => undef, |
2733 |
} |
2734 |
} |
2735 |
); |
2736 |
|
2737 |
# Add fines to patrons |
2738 |
add_fines( $patron_12->borrowernumber, 5.00 ); # $5 |
2739 |
add_fines( $patron_13->borrowernumber, 0.00 ); # $0 |
2740 |
add_fines( $patron_17->borrowernumber, 10.00 ); # $10 |
2741 |
add_fines( $patron_18->borrowernumber, 20.00 ); # $20 |
2742 |
add_fines( $patron_no_dob->borrowernumber, 15.00 ); # $15 |
2743 |
|
2744 |
# Helper function to add fines |
2745 |
sub add_fines { |
2746 |
my ( $borrowernumber, $amount ) = @_; |
2747 |
if ( $amount > 0 ) { |
2748 |
$builder->build_object( |
2749 |
{ |
2750 |
class => 'Koha::Account::Lines', |
2751 |
value => { |
2752 |
borrowernumber => $borrowernumber, |
2753 |
amountoutstanding => $amount, |
2754 |
debit_type_code => 'OVERDUE', |
2755 |
} |
2756 |
} |
2757 |
); |
2758 |
} |
2759 |
} |
2760 |
|
2761 |
# Tests for the search_patrons_to_update_category method |
2762 |
subtest 'Basic test - from category only' => sub { |
2763 |
plan tests => 2; |
2764 |
|
2765 |
my $patrons = Koha::Patrons->search_patrons_to_update_category( |
2766 |
{ |
2767 |
from => $category_code, |
2768 |
} |
2769 |
); |
2770 |
|
2771 |
is( $patrons->count, 5, "Returns all patrons in the category" ); |
2772 |
my @expected_patrons = ( |
2773 |
$patron_12->borrowernumber, $patron_13->borrowernumber, |
2774 |
$patron_17->borrowernumber, $patron_18->borrowernumber, |
2775 |
$patron_no_dob->borrowernumber |
2776 |
); |
2777 |
|
2778 |
is_deeply( |
2779 |
[ sort $patrons->get_column('borrowernumber') ], |
2780 |
[ sort @expected_patrons ], |
2781 |
"Returns the correct patrons" |
2782 |
); |
2783 |
}; |
2784 |
|
2785 |
subtest 'Test too_young parameter' => sub { |
2786 |
plan tests => 2; |
2787 |
|
2788 |
my $patrons = Koha::Patrons->search_patrons_to_update_category( |
2789 |
{ |
2790 |
from => $category_code, |
2791 |
too_young => 1, |
2792 |
} |
2793 |
); |
2794 |
|
2795 |
is( $patrons->count, 1, "Returns patrons who are too young" ); |
2796 |
is( $patrons->next->borrowernumber, $patron_12->borrowernumber, "Returns the correct patron" ); |
2797 |
}; |
2798 |
|
2799 |
subtest 'Test too_old parameter' => sub { |
2800 |
plan tests => 2; |
2801 |
|
2802 |
my $patrons = Koha::Patrons->search_patrons_to_update_category( |
2803 |
{ |
2804 |
from => $category_code, |
2805 |
too_old => 1, |
2806 |
} |
2807 |
); |
2808 |
|
2809 |
is( $patrons->count, 1, "Returns patrons who are too old" ); |
2810 |
is( $patrons->next->borrowernumber, $patron_18->borrowernumber, "Returns the correct patron" ); |
2811 |
}; |
2812 |
|
2813 |
subtest 'Test fine_min parameter' => sub { |
2814 |
plan tests => 2; |
2815 |
|
2816 |
my $patrons = Koha::Patrons->search_patrons_to_update_category( |
2817 |
{ |
2818 |
from => $category_code, |
2819 |
fine_min => 10.00, |
2820 |
} |
2821 |
); |
2822 |
|
2823 |
is( $patrons->count, 3, "Returns patrons with fines >= 10.00" ); |
2824 |
cmp_bag( |
2825 |
[ $patrons->get_column('borrowernumber') ], |
2826 |
[ $patron_17->borrowernumber, $patron_18->borrowernumber, $patron_no_dob->borrowernumber ], |
2827 |
"Returns the correct patrons" |
2828 |
); |
2829 |
}; |
2830 |
|
2831 |
subtest 'Test fine_max parameter' => sub { |
2832 |
plan tests => 2; |
2833 |
|
2834 |
my $patrons = Koha::Patrons->search_patrons_to_update_category( |
2835 |
{ |
2836 |
from => $category_code, |
2837 |
fine_max => 10.00, |
2838 |
} |
2839 |
); |
2840 |
|
2841 |
is( $patrons->count, 3, "Returns patrons with fines <= 10.00" ); |
2842 |
cmp_bag( |
2843 |
[ $patrons->get_column('borrowernumber') ], |
2844 |
[ $patron_12->borrowernumber, $patron_13->borrowernumber, $patron_17->borrowernumber ], |
2845 |
"Returns the correct patrons" |
2846 |
); |
2847 |
}; |
2848 |
|
2849 |
subtest 'Test combining age and fine criteria' => sub { |
2850 |
plan tests => 2; |
2851 |
|
2852 |
my $patrons = Koha::Patrons->search_patrons_to_update_category( |
2853 |
{ |
2854 |
from => $category_code, |
2855 |
too_old => 1, |
2856 |
fine_min => 15.00, |
2857 |
} |
2858 |
); |
2859 |
|
2860 |
is( $patrons->count, 1, "Returns too old patrons with fines >= 15.00" ); |
2861 |
is( $patrons->next->borrowernumber, $patron_18->borrowernumber, "Returns the correct patron" ); |
2862 |
}; |
2863 |
|
2864 |
subtest 'Test with empty result' => sub { |
2865 |
plan tests => 1; |
2866 |
|
2867 |
my $patrons = Koha::Patrons->search_patrons_to_update_category( |
2868 |
{ |
2869 |
from => $category_code, |
2870 |
too_young => 1, |
2871 |
fine_max => 1.00, |
2872 |
} |
2873 |
); |
2874 |
|
2875 |
is( $patrons->count, 0, "Returns no patrons when criteria don't match any" ); |
2876 |
}; |
2877 |
|
2878 |
subtest 'Test having both age and fine criteria with multiple matches' => sub { |
2879 |
plan tests => 2; |
2880 |
|
2881 |
# Add another patron who is both too young and has fines |
2882 |
my $another_young = $builder->build_object( |
2883 |
{ |
2884 |
class => 'Koha::Patrons', |
2885 |
value => { |
2886 |
categorycode => $category_code, |
2887 |
dateofbirth => $current_dt->clone->subtract( years => 12 )->subtract( months => 6 )->ymd, |
2888 |
} |
2889 |
} |
2890 |
); |
2891 |
|
2892 |
add_fines( $another_young->borrowernumber, 7.50 ); |
2893 |
|
2894 |
my $patrons = Koha::Patrons->search_patrons_to_update_category( |
2895 |
{ |
2896 |
from => $category_code, |
2897 |
too_young => 1, |
2898 |
fine_min => 5.00, |
2899 |
} |
2900 |
); |
2901 |
|
2902 |
is( $patrons->count, 2, "Returns too young patrons with fines >= 5.00" ); |
2903 |
cmp_bag( |
2904 |
[ $patrons->get_column('borrowernumber') ], |
2905 |
[ $patron_12->borrowernumber, $another_young->borrowernumber ], |
2906 |
"Returns the correct patrons" |
2907 |
); |
2908 |
}; |
2909 |
|
2910 |
subtest 'Test edge cases with date calculations' => sub { |
2911 |
plan tests => 4; |
2912 |
|
2913 |
# Test edge case: Patron exactly at minimum age |
2914 |
my $edge_min = $builder->build_object( |
2915 |
{ |
2916 |
class => 'Koha::Patrons', |
2917 |
value => { |
2918 |
categorycode => $category_code, |
2919 |
dateofbirth => $current_dt->clone->subtract( years => $category->dateofbirthrequired )->ymd, |
2920 |
} |
2921 |
} |
2922 |
); |
2923 |
|
2924 |
# Test edge case: Patron exactly at upper age limit |
2925 |
my $edge_max = $builder->build_object( |
2926 |
{ |
2927 |
class => 'Koha::Patrons', |
2928 |
value => { |
2929 |
categorycode => $category_code, |
2930 |
dateofbirth => $current_dt->clone->subtract( years => $category->upperagelimit )->ymd, |
2931 |
} |
2932 |
} |
2933 |
); |
2934 |
|
2935 |
# Test edge case: Patron one day younger than minimum age |
2936 |
my $edge_too_young = $builder->build_object( |
2937 |
{ |
2938 |
class => 'Koha::Patrons', |
2939 |
value => { |
2940 |
categorycode => $category_code, |
2941 |
dateofbirth => |
2942 |
$current_dt->clone->subtract( years => $category->dateofbirthrequired )->add( days => 1 )->ymd, |
2943 |
} |
2944 |
} |
2945 |
); |
2946 |
|
2947 |
# Test edge case: Patron one day older than upper age limit |
2948 |
my $edge_too_old = $builder->build_object( |
2949 |
{ |
2950 |
class => 'Koha::Patrons', |
2951 |
value => { |
2952 |
categorycode => $category_code, |
2953 |
dateofbirth => |
2954 |
$current_dt->clone->subtract( years => $category->upperagelimit + 1 )->subtract( days => 1 ) |
2955 |
->ymd, |
2956 |
} |
2957 |
} |
2958 |
); |
2959 |
|
2960 |
# Test too_young with edge cases |
2961 |
my $patrons = Koha::Patrons->search_patrons_to_update_category( |
2962 |
{ |
2963 |
from => $category_code, |
2964 |
too_young => 1, |
2965 |
} |
2966 |
); |
2967 |
|
2968 |
my @too_young_patrons = $patrons->get_column('borrowernumber'); |
2969 |
ok( |
2970 |
( grep { $_ eq $edge_too_young->borrowernumber } @too_young_patrons ), |
2971 |
"Patron one day younger than min age is marked as too young" |
2972 |
); |
2973 |
ok( |
2974 |
!( grep { $_ eq $edge_min->borrowernumber } @too_young_patrons ), |
2975 |
"Patron exactly at min age is not marked as too young" |
2976 |
); |
2977 |
|
2978 |
# Test too_old with edge cases |
2979 |
$patrons = Koha::Patrons->search_patrons_to_update_category( |
2980 |
{ |
2981 |
from => $category_code, |
2982 |
too_old => 1, |
2983 |
} |
2984 |
); |
2985 |
|
2986 |
my @too_old_patrons = $patrons->get_column('borrowernumber'); |
2987 |
ok( |
2988 |
( grep { $_ eq $edge_too_old->borrowernumber } @too_old_patrons ), |
2989 |
"Patron just above upper age limit is marked as too old" |
2990 |
); |
2991 |
ok( |
2992 |
!( grep { $_ eq $edge_max->borrowernumber } @too_old_patrons ), |
2993 |
"Patron exactly at upper age limit is not marked as too old" |
2994 |
); |
2995 |
}; |
2996 |
|
2997 |
$schema->storage->txn_rollback(); |
2998 |
}; |
2999 |
|
2663 |
subtest 'queue_notice' => sub { |
3000 |
subtest 'queue_notice' => sub { |
2664 |
plan tests => 11; |
3001 |
plan tests => 11; |
2665 |
|
3002 |
|
2666 |
- |
|
|