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 $biblio = $builder->build({
123 my $biblioitem = $builder->build({
124 source => 'Biblioitem',
126 biblionumber => $biblio->{biblionumber}
129 my $item1 = $builder->build({
132 my $item2 = $builder->build({
135 my $item3 = $builder->build({
138 my $now = dt_from_string();
139 my $tomorrow = $now->add(days => 1)->strftime('%F');
141 my $issue1 = $builder->build({
144 date_due => $tomorrow,
145 itemnumber => $item1->{itemnumber},
146 branchcode => $library2->{branchcode},
147 borrowernumber => $borrower->{borrowernumber},
152 my $issue2 = $builder->build({
155 date_due => $tomorrow,
156 itemnumber => $item2->{itemnumber},
157 branchcode => $library3->{branchcode},
158 borrowernumber => $borrower->{borrowernumber},
162 my $issue3 = $builder->build({
165 date_due => $tomorrow,
166 itemnumber => $item3->{itemnumber},
167 branchcode => $library3->{branchcode},
168 borrowernumber => $borrower->{borrowernumber},
173 C4::Context->set_preference('EnhancedMessagingPreferences', 1);
180 # We simulate script execution by evaluating the script code in the context
183 eval $script; ## no critic (StringyEval)
188 my $scriptContent = '';
189 my $scriptFile = "$scriptDir/../../../misc/cronjobs/advance_notices.pl";
190 open my $scriptfh, "<", $scriptFile or die "Failed to open $scriptFile: $!";
192 while (<$scriptfh>) {
193 $scriptContent .= $_;
197 my $sthmq = $dbh->prepare('SELECT * FROM message_queue WHERE borrowernumber = ?');
199 subtest 'Default behaviour tests' => sub {
203 $schema->storage->txn_begin;
205 build_test_objects();
207 run_script($scriptContent, 'advanced_notices.pl', '-c');
209 $sthmq->execute($borrower->{borrowernumber});
211 my $messages = $sthmq->fetchall_hashref('message_id');
213 is(scalar(keys %$messages), 1, 'There is one message in the queue');
215 for my $message (keys %$messages) {
216 $messages->{$message}->{content} =~ /(\d+) (.*)/;
220 is ($count, '3', 'Issue count is 3');
221 is ($branchname, $library1->{branchname}, 'Branchname is that of borrowers home branch.');
224 $schema->storage->txn_rollback;
227 subtest '--digest-per-branch tests' => sub {
231 $schema->storage->txn_begin;
233 build_test_objects();
235 run_script($scriptContent, 'advanced_notices.pl', '-c', '-digest-per-branch');
237 $sthmq->execute($borrower->{borrowernumber});
239 my $messages = $sthmq->fetchall_hashref('message_id');
241 is(scalar(keys %$messages), 2, 'There are two messages in the queue');
244 $library2->{branchname} => {
247 $library3->{branchname} => {
252 my %expected_branchnames = (
253 $library2->{branchname} => 1,
254 $library3->{branchname} => 1
258 for my $message (keys %$messages) {
259 $messages->{$message}->{content} =~ /(\d+) (.*)/;
263 ok ($expected_branchnames{$branchname}, 'Branchname is that of expected issuing branch.');
265 $expected_branchnames{$branchname} = 0;
267 is ($count, $expected{$branchname}->{count}, 'Issue count is ' . $expected{$branchname}->{count});
272 $schema->storage->txn_rollback;