3 # Copyright 2023 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 #use Data::Dumper qw/Dumper/;
22 use Test::More tests => 4;
28 use Koha::DateUtils qw/dt_from_string/;
29 use Koha::Notice::Util;
31 use t::lib::TestBuilder;
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
37 my $builder = t::lib::TestBuilder->new;
39 subtest 'load_domain_limits' => sub {
43 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 t::lib::Mocks::mock_config( 'message_domain_limits', q{} );
46 is( Koha::Notice::Util->load_domain_limits, undef, 'koha-conf contains blank entry' );
47 t::lib::Mocks::mock_config( 'message_domain_limits', { domain => { name => 'A', limit => 2, unit => '1d' } } );
48 $domain_limits = Koha::Notice::Util->load_domain_limits;
49 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}->{unit}, '1d', 'check unit of first entry' );
52 t::lib::Mocks::mock_config( 'message_domain_limits',
53 { domain => [ { name => 'A', limit => 2, unit => '2d' }, { name => 'B', limit => 3, unit => '3h' } ] },
55 $domain_limits = Koha::Notice::Util->load_domain_limits;
56 is( keys %$domain_limits, 2, 'koha-conf contains two domains' );
57 is( $domain_limits->{b}->{limit}, 3, 'check limit of second entry' );
58 is( $domain_limits->{b}->{count}, undef, 'check if count still undefined' );
61 subtest 'counting in exceeds_limit' => sub {
66 my @values = ( message_transport_type => 'email', status => 'sent' );
67 my $today = dt_from_string();
68 #FIXME Why are the three following build calls so slow?
69 $builder->build_object({ class => 'Koha::Notice::Messages',
70 value => { @values, to_address => 'a@A', updated_on => $today->clone->subtract( hours => 36 ) }});
71 $builder->build_object({ class => 'Koha::Notice::Messages',
72 value => { @values, to_address => 'b@A', updated_on => $today->clone->subtract( hours => 49 ) }});
73 $builder->build_object({ class => 'Koha::Notice::Messages',
74 value => { @values, to_address => 'c@A', updated_on => $today->clone->subtract( days => 3 ) }});
76 $domain_limits = Koha::Notice::Util->load_domain_limits; # still using last mocked config A:2/2d
77 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' );
79 t::lib::Mocks::mock_config( 'message_domain_limits',
80 { domain => [ { name => 'A', limit => 2, unit => '50h' }, { name => 'B', limit => 3, unit => '3h' } ] },
82 $domain_limits = Koha::Notice::Util->load_domain_limits;
83 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' );
85 # Check count for B; if counted, there would be 0 or higher, otherwise undef
86 ok( !defined $domain_limits->{b}->{count}, 'Prove that we did not count b if not asked for' );
89 subtest '_convert_unit' => sub {
92 # Date subtraction - edge case (start of summer time)
93 my $mock_context = Test::MockModule->new('C4::Context');
94 $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' );
96 is( Koha::Notice::Util::_convert_unit( $dt, '4d')->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' );
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');
103 subtest 'exceeds_limit with group domains' => sub {
107 t::lib::Mocks::mock_config( 'message_domain_limits', undef );
108 $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' );
111 t::lib::Mocks::mock_config( 'message_domain_limits', { domain => [
112 { name => 'A', limit => 3, unit => '5m' },
113 { name => 'B', limit => 2, unit => '5m' },
114 { name => 'C', belongs_to => 'A' },
116 $domain_limits = Koha::Notice::Util->load_domain_limits;
118 is( Koha::Notice::Util->exceeds_limit({ to => '1@A', limits => $domain_limits }), 0, 'First message to A' );
119 is( Koha::Notice::Util->exceeds_limit({ to => '2@C', limits => $domain_limits }), 0, 'Second message to A (via C)' );
120 ok( !exists $domain_limits->{c}->{count}, 'No count exists for grouped domain' );
121 warning_like { $result = Koha::Notice::Util->exceeds_limit({ to => '3@A', limits => $domain_limits }) }
122 qr/Sending messages: domain a reached limit/, 'Check warn for reaching limit A';
123 is( $result, 0, 'Limit for A reached, not exceeded' );
124 is( Koha::Notice::Util->exceeds_limit({ to => '4@C', limits => $domain_limits }), 1, 'Limit for A exceeded (via C)' );
125 is( Koha::Notice::Util->exceeds_limit({ to => '5@B', limits => $domain_limits }), 0, 'First message to B' );
126 is( $domain_limits->{b}->{count}, 1, 'Count B updated' );
127 is( Koha::Notice::Util->exceeds_limit({ to => '5@B', limits => $domain_limits, incr => 0 }), 0, 'Test incr flag' );
128 is( $domain_limits->{b}->{count}, 1, 'Count B still 1' );
129 is( Koha::Notice::Util->exceeds_limit({ to => '6@D', limits => $domain_limits }), 0, 'No limits for D' );
132 $schema->storage->txn_rollback;