|
Lines 1-258
Link Here
|
| 1 |
#!/usr/bin/perl |
|
|
| 2 |
# |
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Copyright (C) 2018 Andreas Jonsson <andreas.jonsson@kreablo.se> |
| 6 |
# |
| 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. |
| 11 |
# |
| 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. |
| 16 |
# |
| 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>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More tests => 2; |
| 23 |
use t::lib::TestBuilder; |
| 24 |
use t::lib::Mocks; |
| 25 |
use File::Spec; |
| 26 |
use File::Basename; |
| 27 |
|
| 28 |
use Koha::DateUtils; |
| 29 |
|
| 30 |
my $scriptDir = dirname(File::Spec->rel2abs( __FILE__ )); |
| 31 |
|
| 32 |
my $schema = Koha::Database->new->schema; |
| 33 |
my $dbh = C4::Context->dbh; |
| 34 |
|
| 35 |
my $library1; |
| 36 |
my $library2; |
| 37 |
my $library3; |
| 38 |
my $borrower; |
| 39 |
|
| 40 |
sub build_test_objects { |
| 41 |
|
| 42 |
# Set only to avoid exception. |
| 43 |
t::lib::Mocks::mock_preference('dateformat', 'metric'); |
| 44 |
|
| 45 |
my $builder = t::lib::TestBuilder->new; |
| 46 |
|
| 47 |
$library1 = $builder->build({ |
| 48 |
source => 'Branch', |
| 49 |
}); |
| 50 |
$library2 = $builder->build({ |
| 51 |
source => 'Branch', |
| 52 |
}); |
| 53 |
$library3 = $builder->build({ |
| 54 |
source => 'Branch', |
| 55 |
}); |
| 56 |
$borrower = $builder->build({ |
| 57 |
source => 'Borrower', |
| 58 |
value => { |
| 59 |
branchcode => $library1->{branchcode}, |
| 60 |
} |
| 61 |
}); |
| 62 |
$dbh->do(<<DELETESQL); |
| 63 |
DELETE FROM letter |
| 64 |
WHERE module='circulation' |
| 65 |
AND code = 'PREDUEDGST' |
| 66 |
AND message_transport_type='email' |
| 67 |
AND branchcode='' |
| 68 |
DELETESQL |
| 69 |
|
| 70 |
$dbh->do(<<DELETESQL); |
| 71 |
DELETE FROM message_attributes WHERE message_name = 'Advance_Notice' |
| 72 |
DELETESQL |
| 73 |
|
| 74 |
my $message_attribute = $builder->build({ |
| 75 |
source => 'MessageAttribute', |
| 76 |
value => { |
| 77 |
message_name => 'Advance_Notice' |
| 78 |
} |
| 79 |
}); |
| 80 |
|
| 81 |
my $letter = $builder->build({ |
| 82 |
source => 'Letter', |
| 83 |
value => { |
| 84 |
module => 'circulation', |
| 85 |
code => 'PREDUEDGST', |
| 86 |
branchcode => '', |
| 87 |
message_transport_type => 'email', |
| 88 |
lang => 'default', |
| 89 |
is_html => 0, |
| 90 |
content => '<<count>> <<branches.branchname>>' |
| 91 |
} |
| 92 |
}); |
| 93 |
my $borrower_message_preference = $builder->build({ |
| 94 |
source => 'BorrowerMessagePreference', |
| 95 |
value => { |
| 96 |
borrowernumber => $borrower->{borrowernumber}, |
| 97 |
wants_digest => 1, |
| 98 |
days_in_advance => 1, |
| 99 |
message_attribute_id => $message_attribute->{message_attribute_id} |
| 100 |
} |
| 101 |
}); |
| 102 |
|
| 103 |
my $borrower_message_transport_preference = $builder->build({ |
| 104 |
source => 'BorrowerMessageTransportPreference', |
| 105 |
value => { |
| 106 |
borrower_message_preference_id => $borrower_message_preference->{borrower_message_preference_id}, |
| 107 |
message_transport_type => 'email' |
| 108 |
} |
| 109 |
}); |
| 110 |
|
| 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', |
| 114 |
value => { |
| 115 |
borrower_message_preference_id => $borrower_message_preference->{borrower_message_preference_id}, |
| 116 |
message_transport_type => 'phone' |
| 117 |
} |
| 118 |
}); |
| 119 |
|
| 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'); |
| 125 |
|
| 126 |
my $issue1 = $builder->build({ |
| 127 |
source => 'Issue', |
| 128 |
value => { |
| 129 |
date_due => $tomorrow, |
| 130 |
itemnumber => $item1->itemnumber, |
| 131 |
branchcode => $library2->{branchcode}, |
| 132 |
borrowernumber => $borrower->{borrowernumber}, |
| 133 |
returndate => undef |
| 134 |
} |
| 135 |
}); |
| 136 |
|
| 137 |
my $issue2 = $builder->build({ |
| 138 |
source => 'Issue', |
| 139 |
value => { |
| 140 |
date_due => $tomorrow, |
| 141 |
itemnumber => $item2->itemnumber, |
| 142 |
branchcode => $library3->{branchcode}, |
| 143 |
borrowernumber => $borrower->{borrowernumber}, |
| 144 |
returndate => undef |
| 145 |
} |
| 146 |
}); |
| 147 |
my $issue3 = $builder->build({ |
| 148 |
source => 'Issue', |
| 149 |
value => { |
| 150 |
date_due => $tomorrow, |
| 151 |
itemnumber => $item3->itemnumber, |
| 152 |
branchcode => $library3->{branchcode}, |
| 153 |
borrowernumber => $borrower->{borrowernumber}, |
| 154 |
returndate => undef |
| 155 |
} |
| 156 |
}); |
| 157 |
|
| 158 |
C4::Context->set_preference('EnhancedMessagingPreferences', 1); |
| 159 |
} |
| 160 |
|
| 161 |
sub run_script { |
| 162 |
my $script = shift; |
| 163 |
local @ARGV = @_; |
| 164 |
|
| 165 |
# We simulate script execution by evaluating the script code in the context |
| 166 |
# of this unit test. |
| 167 |
|
| 168 |
eval $script; ## no critic (StringyEval) |
| 169 |
|
| 170 |
die $@ if $@; |
| 171 |
} |
| 172 |
|
| 173 |
my $scriptContent = ''; |
| 174 |
my $scriptFile = "$scriptDir/../../../misc/cronjobs/advance_notices.pl"; |
| 175 |
open my $scriptfh, "<", $scriptFile or die "Failed to open $scriptFile: $!"; |
| 176 |
|
| 177 |
while (<$scriptfh>) { |
| 178 |
$scriptContent .= $_; |
| 179 |
} |
| 180 |
close $scriptfh; |
| 181 |
|
| 182 |
my $sthmq = $dbh->prepare('SELECT * FROM message_queue WHERE borrowernumber = ?'); |
| 183 |
|
| 184 |
subtest 'Default behaviour tests' => sub { |
| 185 |
|
| 186 |
plan tests => 3; |
| 187 |
|
| 188 |
$schema->storage->txn_begin; |
| 189 |
|
| 190 |
build_test_objects(); |
| 191 |
|
| 192 |
run_script($scriptContent, 'advanced_notices.pl', '-c'); |
| 193 |
|
| 194 |
$sthmq->execute($borrower->{borrowernumber}); |
| 195 |
|
| 196 |
my $messages = $sthmq->fetchall_hashref('message_id'); |
| 197 |
|
| 198 |
is(scalar(keys %$messages), 1, 'There is one message in the queue'); |
| 199 |
|
| 200 |
for my $message (keys %$messages) { |
| 201 |
$messages->{$message}->{content} =~ /(\d+) (.*)/; |
| 202 |
my $count = $1; |
| 203 |
my $branchname = $2; |
| 204 |
|
| 205 |
is ($count, '3', 'Issue count is 3'); |
| 206 |
is ($branchname, $library1->{branchname}, 'Branchname is that of borrowers home branch.'); |
| 207 |
} |
| 208 |
|
| 209 |
$schema->storage->txn_rollback; |
| 210 |
}; |
| 211 |
|
| 212 |
subtest '--digest-per-branch tests' => sub { |
| 213 |
|
| 214 |
plan tests => 5; |
| 215 |
|
| 216 |
$schema->storage->txn_begin; |
| 217 |
|
| 218 |
build_test_objects(); |
| 219 |
|
| 220 |
run_script($scriptContent, 'advanced_notices.pl', '-c', '-digest-per-branch'); |
| 221 |
|
| 222 |
$sthmq->execute($borrower->{borrowernumber}); |
| 223 |
|
| 224 |
my $messages = $sthmq->fetchall_hashref('message_id'); |
| 225 |
|
| 226 |
is(scalar(keys %$messages), 2, 'There are two messages in the queue'); |
| 227 |
|
| 228 |
my %expected = ( |
| 229 |
$library2->{branchname} => { |
| 230 |
count => 1, |
| 231 |
}, |
| 232 |
$library3->{branchname} => { |
| 233 |
count => 2, |
| 234 |
} |
| 235 |
); |
| 236 |
|
| 237 |
my %expected_branchnames = ( |
| 238 |
$library2->{branchname} => 1, |
| 239 |
$library3->{branchname} => 1 |
| 240 |
); |
| 241 |
|
| 242 |
my $i = 0; |
| 243 |
for my $message (keys %$messages) { |
| 244 |
$messages->{$message}->{content} =~ /(\d+) (.*)/; |
| 245 |
my $count = $1; |
| 246 |
my $branchname = $2; |
| 247 |
|
| 248 |
ok ($expected_branchnames{$branchname}, 'Branchname is that of expected issuing branch.'); |
| 249 |
|
| 250 |
$expected_branchnames{$branchname} = 0; |
| 251 |
|
| 252 |
is ($count, $expected{$branchname}->{count}, 'Issue count is ' . $expected{$branchname}->{count}); |
| 253 |
|
| 254 |
$i++; |
| 255 |
} |
| 256 |
|
| 257 |
$schema->storage->txn_rollback; |
| 258 |
}; |
| 259 |
- |