Lines 47-55
subtest 'load_domain_limits' => sub {
Link Here
|
47 |
is( Koha::Notice::Util->load_domain_limits, undef, 'koha-conf contains blank entry' ); |
47 |
is( Koha::Notice::Util->load_domain_limits, undef, 'koha-conf contains blank entry' ); |
48 |
t::lib::Mocks::mock_config( 'message_domain_limits', { domain => { name => 'A', limit => 2, unit => '1d' } } ); |
48 |
t::lib::Mocks::mock_config( 'message_domain_limits', { domain => { name => 'A', limit => 2, unit => '1d' } } ); |
49 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
49 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
50 |
is( keys %$domain_limits, 1, 'koha-conf contains one domain' ); |
50 |
is( keys %$domain_limits, 1, 'koha-conf contains one domain' ); |
51 |
is( $domain_limits->{a}->{limit}, 2, 'check limit of first entry' ); |
51 |
is( $domain_limits->{a}->{limit}, 2, 'check limit of first entry' ); |
52 |
is( $domain_limits->{a}->{units}, 1, 'check unit of first entry' ); |
52 |
is( $domain_limits->{a}->{units}, 1, 'check unit of first entry' ); |
53 |
is( $domain_limits->{a}->{unit_type}, 'days', 'check unit of first entry' ); |
53 |
is( $domain_limits->{a}->{unit_type}, 'days', 'check unit of first entry' ); |
54 |
t::lib::Mocks::mock_config( |
54 |
t::lib::Mocks::mock_config( |
55 |
'message_domain_limits', |
55 |
'message_domain_limits', |
Lines 62-76
subtest 'load_domain_limits' => sub {
Link Here
|
62 |
} |
62 |
} |
63 |
); |
63 |
); |
64 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
64 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
65 |
is( keys %$domain_limits, 3, 'koha-conf contains two domains' ); |
65 |
is( keys %$domain_limits, 3, 'koha-conf contains two domains' ); |
66 |
is( $domain_limits->{b}->{limit}, 3, 'check limit of second entry' ); |
66 |
is( $domain_limits->{b}->{limit}, 3, 'check limit of second entry' ); |
67 |
is( $domain_limits->{b}->{count}, undef, 'check if count still undefined' ); |
67 |
is( $domain_limits->{b}->{count}, undef, 'check if count still undefined' ); |
68 |
is( $domain_limits->{c}->{belongs_to}, 'b', 'check group domain of C' ); |
68 |
is( $domain_limits->{c}->{belongs_to}, 'b', 'check group domain of C' ); |
69 |
|
69 |
|
70 |
# Test with database |
70 |
# Test with database |
71 |
my $limit01 = Koha::MailDomainLimit->new({ domain_name => 'A', domain_limit => 2, units => '2', unit_type => 'days' })->store; |
71 |
my $limit01 = |
72 |
my $limit02 = Koha::MailDomainLimit->new({ domain_name => 'B', domain_limit => 3, units => '3', unit_type => 'hours' })->store; |
72 |
Koha::MailDomainLimit->new( { domain_name => 'A', domain_limit => 2, units => '2', unit_type => 'days' } ) |
73 |
my $limit03 = Koha::MailDomainLimit->new({ domain_name => 'C', belongs_to => $limit02->id })->store; |
73 |
->store; |
|
|
74 |
my $limit02 = |
75 |
Koha::MailDomainLimit->new( { domain_name => 'B', domain_limit => 3, units => '3', unit_type => 'hours' } ) |
76 |
->store; |
77 |
my $limit03 = Koha::MailDomainLimit->new( { domain_name => 'C', belongs_to => $limit02->id } )->store; |
74 |
my $domain_limits_db = Koha::Notice::Util->load_domain_limits; |
78 |
my $domain_limits_db = Koha::Notice::Util->load_domain_limits; |
75 |
is_deeply( $domain_limits_db, $domain_limits, 'Same results for limits in database' ); |
79 |
is_deeply( $domain_limits_db, $domain_limits, 'Same results for limits in database' ); |
76 |
}; |
80 |
}; |
Lines 79-102
subtest 'counting in exceeds_limit' => sub {
Link Here
|
79 |
plan tests => 3; |
83 |
plan tests => 3; |
80 |
|
84 |
|
81 |
my $domain_limits; |
85 |
my $domain_limits; |
|
|
86 |
|
82 |
# Check counting |
87 |
# Check counting |
83 |
my @values = ( message_transport_type => 'email', status => 'sent' ); |
88 |
my @values = ( message_transport_type => 'email', status => 'sent' ); |
84 |
my $today = dt_from_string(); |
89 |
my $today = dt_from_string(); |
|
|
90 |
|
85 |
#FIXME Why are the three following build calls so slow? |
91 |
#FIXME Why are the three following build calls so slow? |
86 |
$builder->build_object({ class => 'Koha::Notice::Messages', |
92 |
$builder->build_object( |
87 |
value => { @values, to_address => 'a@A', updated_on => $today->clone->subtract( hours => 36 ) }}); |
93 |
{ |
88 |
$builder->build_object({ class => 'Koha::Notice::Messages', |
94 |
class => 'Koha::Notice::Messages', |
89 |
value => { @values, to_address => 'b@A', updated_on => $today->clone->subtract( hours => 49 ) }}); |
95 |
value => { @values, to_address => 'a@A', updated_on => $today->clone->subtract( hours => 36 ) } |
90 |
$builder->build_object({ class => 'Koha::Notice::Messages', |
96 |
} |
91 |
value => { @values, to_address => 'c@A', updated_on => $today->clone->subtract( days => 3 ) }}); |
97 |
); |
92 |
|
98 |
$builder->build_object( |
93 |
$domain_limits = Koha::Notice::Util->load_domain_limits; # still using last config for A: 2/2d |
99 |
{ |
94 |
Koha::Notice::Util->exceeds_limit({ to => '@A', limits => $domain_limits, incr => 0 }); # force counting |
100 |
class => 'Koha::Notice::Messages', |
|
|
101 |
value => { @values, to_address => 'b@A', updated_on => $today->clone->subtract( hours => 49 ) } |
102 |
} |
103 |
); |
104 |
$builder->build_object( |
105 |
{ |
106 |
class => 'Koha::Notice::Messages', |
107 |
value => { @values, to_address => 'c@A', updated_on => $today->clone->subtract( days => 3 ) } |
108 |
} |
109 |
); |
110 |
|
111 |
$domain_limits = Koha::Notice::Util->load_domain_limits; # still using last config for A: 2/2d |
112 |
Koha::Notice::Util->exceeds_limit( { to => '@A', limits => $domain_limits, incr => 0 } ); # force counting |
95 |
is( $domain_limits->{a}->{count}, 1, '1 message to A within unit of 2d' ); |
113 |
is( $domain_limits->{a}->{count}, 1, '1 message to A within unit of 2d' ); |
96 |
Koha::MailDomainLimits->search({ domain_name => 'A' })->next->set({ domain_limit => 2, units => '50', unit_type => 'hours' })->store; |
114 |
Koha::MailDomainLimits->search( { domain_name => 'A' } ) |
|
|
115 |
->next->set( { domain_limit => 2, units => '50', unit_type => 'hours' } )->store; |
97 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
116 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
98 |
Koha::Notice::Util->exceeds_limit({ to => 'x@A ', limits => $domain_limits, incr => 0 }); # force counting |
117 |
Koha::Notice::Util->exceeds_limit( { to => 'x@A ', limits => $domain_limits, incr => 0 } ); # force counting |
99 |
is( $domain_limits->{a}->{count}, 2, '2 messages to A within unit of 50h' ); |
118 |
is( $domain_limits->{a}->{count}, 2, '2 messages to A within unit of 50h' ); |
|
|
119 |
|
100 |
# Check count for B; if counted, there would be 0 or higher, otherwise undef |
120 |
# Check count for B; if counted, there would be 0 or higher, otherwise undef |
101 |
ok( !defined $domain_limits->{b}->{count}, 'Prove that we did not count b if not asked for' ); |
121 |
ok( !defined $domain_limits->{b}->{count}, 'Prove that we did not count b if not asked for' ); |
102 |
}; |
122 |
}; |
Lines 106-115
subtest '_calc_startdate' => sub {
Link Here
|
106 |
|
126 |
|
107 |
# Date subtraction - edge case (start of summer time) |
127 |
# Date subtraction - edge case (start of summer time) |
108 |
my $mock_context = Test::MockModule->new('C4::Context'); |
128 |
my $mock_context = Test::MockModule->new('C4::Context'); |
109 |
$mock_context->mock( 'tz', sub { return DateTime::TimeZone->new( name => 'Europe/Amsterdam' )} ); |
129 |
$mock_context->mock( 'tz', sub { return DateTime::TimeZone->new( name => 'Europe/Amsterdam' ) } ); |
110 |
my $dt = dt_from_string( '2023-03-31 02:30:00', 'iso', '+02:00' ); |
130 |
my $dt = dt_from_string( '2023-03-31 02:30:00', 'iso', '+02:00' ); |
111 |
is( Koha::Notice::Util::_calc_startdate( $dt, 4, 'days')->stringify, '2023-03-27T02:30:00', '02:30 is fine' ); |
131 |
is( Koha::Notice::Util::_calc_startdate( $dt, 4, 'days' )->stringify, '2023-03-27T02:30:00', '02:30 is fine' ); |
112 |
is( Koha::Notice::Util::_calc_startdate( $dt, 1, 'days')->stringify, '2023-03-26T01:30:00', 'Moved 02:30 to 01:30' ); |
132 |
is( |
|
|
133 |
Koha::Notice::Util::_calc_startdate( $dt, 1, 'days' )->stringify, '2023-03-26T01:30:00', |
134 |
'Moved 02:30 to 01:30' |
135 |
); |
113 |
$mock_context->unmock('tz'); |
136 |
$mock_context->unmock('tz'); |
114 |
}; |
137 |
}; |
115 |
|
138 |
|
Lines 120-144
subtest 'exceeds_limit with group domains' => sub {
Link Here
|
120 |
Koha::MailDomainLimits->delete; |
143 |
Koha::MailDomainLimits->delete; |
121 |
t::lib::Mocks::mock_config( 'message_domain_limits', undef ); |
144 |
t::lib::Mocks::mock_config( 'message_domain_limits', undef ); |
122 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
145 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
123 |
is( Koha::Notice::Util->exceeds_limit({ to => 'marcel@koha.nl', limits => $domain_limits }), 0, 'False when having no limits' ); |
146 |
is( |
|
|
147 |
Koha::Notice::Util->exceeds_limit( { to => 'marcel@koha.nl', limits => $domain_limits } ), 0, |
148 |
'False when having no limits' |
149 |
); |
124 |
|
150 |
|
125 |
my $limit01 = Koha::MailDomainLimit->new({ domain_name => 'A', domain_limit => 3, units => 5, unit_type => 'minutes' })->store; |
151 |
my $limit01 = |
126 |
my $limit02 = Koha::MailDomainLimit->new({ domain_name => 'B', domain_limit => 2, units => 5, unit_type => 'minutes' })->store; |
152 |
Koha::MailDomainLimit->new( { domain_name => 'A', domain_limit => 3, units => 5, unit_type => 'minutes' } ) |
127 |
my $limit03 = Koha::MailDomainLimit->new({ domain_name => 'C', belongs_to => $limit01->id })->store; |
153 |
->store; |
|
|
154 |
my $limit02 = |
155 |
Koha::MailDomainLimit->new( { domain_name => 'B', domain_limit => 2, units => 5, unit_type => 'minutes' } ) |
156 |
->store; |
157 |
my $limit03 = Koha::MailDomainLimit->new( { domain_name => 'C', belongs_to => $limit01->id } )->store; |
128 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
158 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
129 |
my $result; |
159 |
my $result; |
130 |
is( Koha::Notice::Util->exceeds_limit({ to => '1@A', limits => $domain_limits }), 0, 'First message to A' ); |
160 |
is( Koha::Notice::Util->exceeds_limit( { to => '1@A', limits => $domain_limits } ), 0, 'First message to A' ); |
131 |
is( Koha::Notice::Util->exceeds_limit({ to => '2@C', limits => $domain_limits }), 0, 'Second message to A (via C)' ); |
161 |
is( |
|
|
162 |
Koha::Notice::Util->exceeds_limit( { to => '2@C', limits => $domain_limits } ), 0, |
163 |
'Second message to A (via C)' |
164 |
); |
132 |
ok( !exists $domain_limits->{c}->{count}, 'No count exists for grouped domain' ); |
165 |
ok( !exists $domain_limits->{c}->{count}, 'No count exists for grouped domain' ); |
133 |
warning_like { $result = Koha::Notice::Util->exceeds_limit({ to => '3@A', limits => $domain_limits }) } |
166 |
warning_like { $result = Koha::Notice::Util->exceeds_limit( { to => '3@A', limits => $domain_limits } ) } |
134 |
qr/Sending messages: domain a reached limit/, 'Check warn for reaching limit A'; |
167 |
qr/Sending messages: domain a reached limit/, 'Check warn for reaching limit A'; |
135 |
is( $result, 0, 'Limit for A reached, not exceeded' ); |
168 |
is( $result, 0, 'Limit for A reached, not exceeded' ); |
136 |
is( Koha::Notice::Util->exceeds_limit({ to => '4@C', limits => $domain_limits }), 1, 'Limit for A exceeded (via C)' ); |
169 |
is( |
137 |
is( Koha::Notice::Util->exceeds_limit({ to => '5@B', limits => $domain_limits }), 0, 'First message to B' ); |
170 |
Koha::Notice::Util->exceeds_limit( { to => '4@C', limits => $domain_limits } ), 1, |
138 |
is( $domain_limits->{b}->{count}, 1, 'Count B updated' ); |
171 |
'Limit for A exceeded (via C)' |
139 |
is( Koha::Notice::Util->exceeds_limit({ to => '5@B', limits => $domain_limits, incr => 0 }), 0, 'Test incr flag' ); |
172 |
); |
140 |
is( $domain_limits->{b}->{count}, 1, 'Count B still 1' ); |
173 |
is( Koha::Notice::Util->exceeds_limit( { to => '5@B', limits => $domain_limits } ), 0, 'First message to B' ); |
141 |
is( Koha::Notice::Util->exceeds_limit({ to => '6@D', limits => $domain_limits }), 0, 'No limits for D' ); |
174 |
is( $domain_limits->{b}->{count}, 1, 'Count B updated' ); |
|
|
175 |
is( |
176 |
Koha::Notice::Util->exceeds_limit( { to => '5@B', limits => $domain_limits, incr => 0 } ), 0, |
177 |
'Test incr flag' |
178 |
); |
179 |
is( $domain_limits->{b}->{count}, 1, 'Count B still 1' ); |
180 |
is( Koha::Notice::Util->exceeds_limit( { to => '6@D', limits => $domain_limits } ), 0, 'No limits for D' ); |
142 |
}; |
181 |
}; |
143 |
|
182 |
|
144 |
$schema->storage->txn_rollback; |
183 |
$schema->storage->txn_rollback; |