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