Lines 18-24
Link Here
|
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
19 |
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
#use Data::Dumper qw/Dumper/; |
|
|
22 |
use Test::More tests => 4; |
21 |
use Test::More tests => 4; |
23 |
use Test::MockModule; |
22 |
use Test::MockModule; |
24 |
use Test::Warn; |
23 |
use Test::Warn; |
Lines 26-31
use Test::Warn;
Link Here
|
26 |
use C4::Context; |
25 |
use C4::Context; |
27 |
use Koha::Database; |
26 |
use Koha::Database; |
28 |
use Koha::DateUtils qw/dt_from_string/; |
27 |
use Koha::DateUtils qw/dt_from_string/; |
|
|
28 |
use Koha::MailDomainLimits; |
29 |
use Koha::Notice::Util; |
29 |
use Koha::Notice::Util; |
30 |
|
30 |
|
31 |
use t::lib::TestBuilder; |
31 |
use t::lib::TestBuilder; |
Lines 33-44
use t::lib::Mocks;
Link Here
|
33 |
|
33 |
|
34 |
my $schema = Koha::Database->new->schema; |
34 |
my $schema = Koha::Database->new->schema; |
35 |
$schema->storage->txn_begin; |
35 |
$schema->storage->txn_begin; |
36 |
|
|
|
37 |
my $builder = t::lib::TestBuilder->new; |
36 |
my $builder = t::lib::TestBuilder->new; |
38 |
|
37 |
|
39 |
subtest 'load_domain_limits' => sub { |
38 |
subtest 'load_domain_limits' => sub { |
40 |
plan tests => 8; |
39 |
plan tests => 11; |
41 |
|
40 |
|
|
|
41 |
# Starting testing without limits in database |
42 |
Koha::MailDomainLimits->delete; |
42 |
my $domain_limits; |
43 |
my $domain_limits; |
43 |
t::lib::Mocks::mock_config( 'message_domain_limits', undef ); |
44 |
t::lib::Mocks::mock_config( 'message_domain_limits', undef ); |
44 |
is( Koha::Notice::Util->load_domain_limits, undef, 'koha-conf does not contain entry' ); |
45 |
is( Koha::Notice::Util->load_domain_limits, undef, 'koha-conf does not contain entry' ); |
Lines 48-61
subtest 'load_domain_limits' => sub {
Link Here
|
48 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
49 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
49 |
is( keys %$domain_limits, 1, 'koha-conf contains one domain' ); |
50 |
is( keys %$domain_limits, 1, 'koha-conf contains one domain' ); |
50 |
is( $domain_limits->{a}->{limit}, 2, 'check limit of first entry' ); |
51 |
is( $domain_limits->{a}->{limit}, 2, 'check limit of first entry' ); |
51 |
is( $domain_limits->{a}->{unit}, '1d', 'check unit of first entry' ); |
52 |
is( $domain_limits->{a}->{units}, 1, 'check unit of first entry' ); |
52 |
t::lib::Mocks::mock_config( 'message_domain_limits', |
53 |
is( $domain_limits->{a}->{unit_type}, 'days', 'check unit of first entry' ); |
53 |
{ domain => [ { name => 'A', limit => 2, unit => '2d' }, { name => 'B', limit => 3, unit => '3h' } ] }, |
54 |
t::lib::Mocks::mock_config( |
|
|
55 |
'message_domain_limits', |
56 |
{ |
57 |
domain => [ |
58 |
{ name => 'A', limit => 2, unit => '2d' }, |
59 |
{ name => 'B', limit => 3, unit => '3h' }, |
60 |
{ name => 'C', belongs_to => 'B' }, |
61 |
], |
62 |
} |
54 |
); |
63 |
); |
55 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
64 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
56 |
is( keys %$domain_limits, 2, 'koha-conf contains two domains' ); |
65 |
is( keys %$domain_limits, 3, 'koha-conf contains two domains' ); |
57 |
is( $domain_limits->{b}->{limit}, 3, 'check limit of second entry' ); |
66 |
is( $domain_limits->{b}->{limit}, 3, 'check limit of second entry' ); |
58 |
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' ); |
69 |
|
70 |
# Test with database |
71 |
my $limit01 = Koha::MailDomainLimit->new({ domain_name => 'A', domain_limit => 2, units => '2', unit_type => 'days' })->store; |
72 |
my $limit02 = Koha::MailDomainLimit->new({ domain_name => 'B', domain_limit => 3, units => '3', unit_type => 'hours' })->store; |
73 |
my $limit03 = Koha::MailDomainLimit->new({ domain_name => 'C', belongs_to => $limit02->id })->store; |
74 |
my $domain_limits_db = Koha::Notice::Util->load_domain_limits; |
75 |
is_deeply( $domain_limits_db, $domain_limits, 'Same results for limits in database' ); |
59 |
}; |
76 |
}; |
60 |
|
77 |
|
61 |
subtest 'counting in exceeds_limit' => sub { |
78 |
subtest 'counting in exceeds_limit' => sub { |
Lines 73-84
subtest 'counting in exceeds_limit' => sub {
Link Here
|
73 |
$builder->build_object({ class => 'Koha::Notice::Messages', |
90 |
$builder->build_object({ class => 'Koha::Notice::Messages', |
74 |
value => { @values, to_address => 'c@A', updated_on => $today->clone->subtract( days => 3 ) }}); |
91 |
value => { @values, to_address => 'c@A', updated_on => $today->clone->subtract( days => 3 ) }}); |
75 |
|
92 |
|
76 |
$domain_limits = Koha::Notice::Util->load_domain_limits; # still using last mocked config A:2/2d |
93 |
$domain_limits = Koha::Notice::Util->load_domain_limits; # still using last config for A: 2/2d |
77 |
Koha::Notice::Util->exceeds_limit({ to => '@A', limits => $domain_limits, incr => 0 }); # force counting |
94 |
Koha::Notice::Util->exceeds_limit({ to => '@A', limits => $domain_limits, incr => 0 }); # force counting |
78 |
is( $domain_limits->{a}->{count}, 1, '1 message to A within unit of 2d' ); |
95 |
is( $domain_limits->{a}->{count}, 1, '1 message to A within unit of 2d' ); |
79 |
t::lib::Mocks::mock_config( 'message_domain_limits', |
96 |
Koha::MailDomainLimits->search({ domain_name => 'A' })->next->set({ domain_limit => 2, units => '50', unit_type => 'hours' })->store; |
80 |
{ domain => [ { name => 'A', limit => 2, unit => '50h' }, { name => 'B', limit => 3, unit => '3h' } ] }, |
|
|
81 |
); |
82 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
97 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
83 |
Koha::Notice::Util->exceeds_limit({ to => 'x@A ', limits => $domain_limits, incr => 0 }); # force counting |
98 |
Koha::Notice::Util->exceeds_limit({ to => 'x@A ', limits => $domain_limits, incr => 0 }); # force counting |
84 |
is( $domain_limits->{a}->{count}, 2, '2 messages to A within unit of 50h' ); |
99 |
is( $domain_limits->{a}->{count}, 2, '2 messages to A within unit of 50h' ); |
Lines 86-102
subtest 'counting in exceeds_limit' => sub {
Link Here
|
86 |
ok( !defined $domain_limits->{b}->{count}, 'Prove that we did not count b if not asked for' ); |
101 |
ok( !defined $domain_limits->{b}->{count}, 'Prove that we did not count b if not asked for' ); |
87 |
}; |
102 |
}; |
88 |
|
103 |
|
89 |
subtest '_convert_unit' => sub { |
104 |
subtest '_calc_startdate' => sub { |
90 |
plan tests => 3; |
105 |
plan tests => 2; |
91 |
|
106 |
|
92 |
# Date subtraction - edge case (start of summer time) |
107 |
# Date subtraction - edge case (start of summer time) |
93 |
my $mock_context = Test::MockModule->new('C4::Context'); |
108 |
my $mock_context = Test::MockModule->new('C4::Context'); |
94 |
$mock_context->mock( 'tz', sub { return DateTime::TimeZone->new( name => 'Europe/Amsterdam' )} ); |
109 |
$mock_context->mock( 'tz', sub { return DateTime::TimeZone->new( name => 'Europe/Amsterdam' )} ); |
95 |
my $dt = dt_from_string( '2023-03-31 02:30:00', 'iso', '+02:00' ); |
110 |
my $dt = dt_from_string( '2023-03-31 02:30:00', 'iso', '+02:00' ); |
96 |
is( Koha::Notice::Util::_convert_unit( $dt, '4d')->stringify, '2023-03-27T02:30:00', '02:30 is fine' ); |
111 |
is( Koha::Notice::Util::_calc_startdate( $dt, 4, 'days')->stringify, '2023-03-27T02:30:00', '02:30 is fine' ); |
97 |
is( Koha::Notice::Util::_convert_unit( $dt, '1d')->stringify, '2023-03-26T01:30:00', 'Moved 02:30 to 01:30' ); |
112 |
is( Koha::Notice::Util::_calc_startdate( $dt, 1, 'days')->stringify, '2023-03-26T01:30:00', 'Moved 02:30 to 01:30' ); |
98 |
# Test bad unit |
|
|
99 |
is( Koha::Notice::Util::_convert_unit( $dt, 'y')->stringify, '2023-03-26T01:30:00', 'No further shift for bad unit' ); |
100 |
$mock_context->unmock('tz'); |
113 |
$mock_context->unmock('tz'); |
101 |
}; |
114 |
}; |
102 |
|
115 |
|
Lines 104-118
subtest 'exceeds_limit with group domains' => sub {
Link Here
|
104 |
plan tests => 12; |
117 |
plan tests => 12; |
105 |
|
118 |
|
106 |
my $domain_limits; |
119 |
my $domain_limits; |
|
|
120 |
Koha::MailDomainLimits->delete; |
107 |
t::lib::Mocks::mock_config( 'message_domain_limits', undef ); |
121 |
t::lib::Mocks::mock_config( 'message_domain_limits', undef ); |
108 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
122 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
109 |
is( Koha::Notice::Util->exceeds_limit({ to => 'marcel@koha.nl', limits => $domain_limits }), 0, 'False when having no limits' ); |
123 |
is( Koha::Notice::Util->exceeds_limit({ to => 'marcel@koha.nl', limits => $domain_limits }), 0, 'False when having no limits' ); |
110 |
|
124 |
|
111 |
t::lib::Mocks::mock_config( 'message_domain_limits', { domain => [ |
125 |
my $limit01 = Koha::MailDomainLimit->new({ domain_name => 'A', domain_limit => 3, units => 5, unit_type => 'minutes' })->store; |
112 |
{ name => 'A', limit => 3, unit => '5m' }, |
126 |
my $limit02 = Koha::MailDomainLimit->new({ domain_name => 'B', domain_limit => 2, units => 5, unit_type => 'minutes' })->store; |
113 |
{ name => 'B', limit => 2, unit => '5m' }, |
127 |
my $limit03 = Koha::MailDomainLimit->new({ domain_name => 'C', belongs_to => $limit01->id })->store; |
114 |
{ name => 'C', belongs_to => 'A' }, |
|
|
115 |
]}); |
116 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
128 |
$domain_limits = Koha::Notice::Util->load_domain_limits; |
117 |
my $result; |
129 |
my $result; |
118 |
is( Koha::Notice::Util->exceeds_limit({ to => '1@A', limits => $domain_limits }), 0, 'First message to A' ); |
130 |
is( Koha::Notice::Util->exceeds_limit({ to => '1@A', limits => $domain_limits }), 0, 'First message to A' ); |
119 |
- |
|
|