3 # This file is part of Koha.
5 # Copyright (C) 2018 Andreas Jonsson <andreas.jonsson@kreablo.se>
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>.
22 use Test::More tests => 2;
23 use t::lib::TestBuilder;
30 my $scriptDir = dirname(File::Spec->rel2abs( __FILE__ ));
32 my $schema = Koha::Database->new->schema;
33 my $dbh = C4::Context->dbh;
40 sub build_test_objects {
42 # Set only to avoid exception.
43 t::lib::Mocks::mock_preference('dateformat', 'metric');
45 my $builder = t::lib::TestBuilder->new;
47 $library1 = $builder->build({
50 $library2 = $builder->build({
53 $library3 = $builder->build({
56 $borrower = $builder->build({
59 branchcode => $library1->{branchcode},
62 $dbh->do(<<DELETESQL);
64 WHERE module='circulation'
65 AND code = 'PREDUEDGST'
66 AND message_transport_type='email'
70 $dbh->do(<<DELETESQL);
71 DELETE FROM message_attributes WHERE message_name = 'Advance_Notice'
74 my $message_attribute = $builder->build({
75 source => 'MessageAttribute',
77 message_name => 'Advance_Notice'
81 my $letter = $builder->build({
84 module => 'circulation',
87 message_transport_type => 'email',
90 content => '<<count>> <<branches.branchname>>'
93 my $borrower_message_preference = $builder->build({
94 source => 'BorrowerMessagePreference',
96 borrowernumber => $borrower->{borrowernumber},
99 message_attribute_id => $message_attribute->{message_attribute_id}
103 my $borrower_message_transport_preference = $builder->build({
104 source => 'BorrowerMessageTransportPreference',
106 borrower_message_preference_id => $borrower_message_preference->{borrower_message_preference_id},
107 message_transport_type => 'email'
111 #Adding a second preference for a notice that isn't defined, should just be skipped
112 my $borrower_message_transport_preference_1 = $builder->build({
113 source => 'BorrowerMessageTransportPreference',
115 borrower_message_preference_id => $borrower_message_preference->{borrower_message_preference_id},
116 message_transport_type => 'phone'
120 my $item1 = $builder->build_sample_item;
121 my $item2 = $builder->build_sample_item;
122 my $item3 = $builder->build_sample_item;
123 my $now = dt_from_string();
124 my $tomorrow = $now->add(days => 1)->strftime('%F');
126 my $issue1 = $builder->build({
129 date_due => $tomorrow,
130 itemnumber => $item1->itemnumber,
131 branchcode => $library2->{branchcode},
132 borrowernumber => $borrower->{borrowernumber},
137 my $issue2 = $builder->build({
140 date_due => $tomorrow,
141 itemnumber => $item2->itemnumber,
142 branchcode => $library3->{branchcode},
143 borrowernumber => $borrower->{borrowernumber},
147 my $issue3 = $builder->build({
150 date_due => $tomorrow,
151 itemnumber => $item3->itemnumber,
152 branchcode => $library3->{branchcode},
153 borrowernumber => $borrower->{borrowernumber},
158 C4::Context->set_preference('EnhancedMessagingPreferences', 1);
165 # We simulate script execution by evaluating the script code in the context
168 eval $script; ## no critic (StringyEval)
173 my $scriptContent = '';
174 my $scriptFile = "$scriptDir/../../../misc/cronjobs/advance_notices.pl";
175 open my $scriptfh, "<", $scriptFile or die "Failed to open $scriptFile: $!";
177 while (<$scriptfh>) {
178 $scriptContent .= $_;
182 my $sthmq = $dbh->prepare('SELECT * FROM message_queue WHERE borrowernumber = ?');
184 subtest 'Default behaviour tests' => sub {
188 $schema->storage->txn_begin;
190 build_test_objects();
192 run_script($scriptContent, 'advanced_notices.pl', '-c');
194 $sthmq->execute($borrower->{borrowernumber});
196 my $messages = $sthmq->fetchall_hashref('message_id');
198 is(scalar(keys %$messages), 1, 'There is one message in the queue');
200 for my $message (keys %$messages) {
201 $messages->{$message}->{content} =~ /(\d+) (.*)/;
205 is ($count, '3', 'Issue count is 3');
206 is ($branchname, $library1->{branchname}, 'Branchname is that of borrowers home branch.');
209 $schema->storage->txn_rollback;
212 subtest '--digest-per-branch tests' => sub {
216 $schema->storage->txn_begin;
218 build_test_objects();
220 run_script($scriptContent, 'advanced_notices.pl', '-c', '-digest-per-branch');
222 $sthmq->execute($borrower->{borrowernumber});
224 my $messages = $sthmq->fetchall_hashref('message_id');
226 is(scalar(keys %$messages), 2, 'There are two messages in the queue');
229 $library2->{branchname} => {
232 $library3->{branchname} => {
237 my %expected_branchnames = (
238 $library2->{branchname} => 1,
239 $library3->{branchname} => 1
243 for my $message (keys %$messages) {
244 $messages->{$message}->{content} =~ /(\d+) (.*)/;
248 ok ($expected_branchnames{$branchname}, 'Branchname is that of expected issuing branch.');
250 $expected_branchnames{$branchname} = 0;
252 is ($count, $expected{$branchname}->{count}, 'Issue count is ' . $expected{$branchname}->{count});
257 $schema->storage->txn_rollback;