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 => 2;
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' );
60 my @values = ( message_transport_type => 'email', status => 'sent' );
61 my $today = dt_from_string();
62 $builder->build_object({ class => 'Koha::Notice::Messages',
63 value => { @values, to_address => 'a@A', updated_on => $today->clone->subtract( hours => 36 ) }});
64 $builder->build_object({ class => 'Koha::Notice::Messages',
65 value => { @values, to_address => 'b@A', updated_on => $today->clone->subtract( hours => 49 ) }});
66 $builder->build_object({ class => 'Koha::Notice::Messages',
67 value => { @values, to_address => 'c@A', updated_on => $today->clone->subtract( days => 3 ) }});
68 $domain_limits = Koha::Notice::Util->load_domain_limits;
69 is( $domain_limits->{a}->{count}, 1, 'Three messages to A, 1 within unit of 2d' );
70 t::lib::Mocks::mock_config( 'message_domain_limits',
71 { domain => [ { name => 'A', limit => 2, unit => '50h' }, { name => 'B', limit => 3, unit => '3h' } ] },
73 $domain_limits = Koha::Notice::Util->load_domain_limits;
74 is( $domain_limits->{a}->{count}, 2, 'Three messages to A, 2 within unit of 50h' );
76 # Date subtraction - edge case (start of summer time)
77 my $mock_context = Test::MockModule->new('C4::Context');
78 $mock_context->mock( 'tz', sub { return DateTime::TimeZone->new( name => 'Europe/Amsterdam' )} );
79 my $dt = dt_from_string( '2023-03-31 02:30:00', 'iso', '+02:00' );
80 is( Koha::Notice::Util::_convert_unit( $dt, '4d')->stringify, '2023-03-27T02:30:00', '02:30 is fine' );
81 is( Koha::Notice::Util::_convert_unit( $dt, '1d')->stringify, '2023-03-26T01:30:00', 'Moved 02:30 to 01:30' );
83 is( Koha::Notice::Util::_convert_unit( $dt, 'y')->stringify, '2023-03-26T01:30:00', 'No further shift for bad unit' );
84 $mock_context->unmock('tz');
87 subtest 'exceeds_limit' => sub {
92 t::lib::Mocks::mock_config( 'message_domain_limits', undef );
93 $domain_limits = Koha::Notice::Util->load_domain_limits;
94 is( Koha::Notice::Util->exceeds_limit( 'marcel@koha.nl', $domain_limits ), 0, 'False when having no limits' );
96 t::lib::Mocks::mock_config( 'message_domain_limits',
97 { domain => [ { name => 'A', limit => 0, unit => '1d' }, { name => 'B', limit => 1, unit => '5h' } ] },
99 $domain_limits = Koha::Notice::Util->load_domain_limits;
100 is( Koha::Notice::Util->exceeds_limit( '1@A', $domain_limits ), 1, 'Limit for A already reached' );
102 warning_like { $result = Koha::Notice::Util->exceeds_limit( '2@B', $domain_limits ) }
103 qr/Sending messages: domain b reached limit/, 'Check warn for reaching limit';
104 is( $result, 0, 'Limit for B not yet exceeded' );
105 is( Koha::Notice::Util->exceeds_limit( '3@B', $domain_limits ), 1, 'Limit for B already reached' );
106 is( Koha::Notice::Util->exceeds_limit( '4@C', $domain_limits ), 0, 'No limits for C' );
109 $schema->storage->txn_rollback;