Bug 21241: (follow-up) Syspref to control fallback to SMS when no email is defined
[koha.git] / t / db_dependent / Suggestions.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use DateTime::Duration;
21 use Test::More tests => 106;
22 use Test::Warn;
23
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use C4::Context;
28 use C4::Letters;
29 use C4::Budgets qw( AddBudgetPeriod AddBudget );
30 use Koha::Database;
31 use Koha::DateUtils qw( dt_from_string output_pref );
32 use Koha::Libraries;
33 use Koha::Patrons;
34 use Koha::Suggestions;
35
36 BEGIN {
37     use_ok('C4::Suggestions');
38 }
39
40 my $schema  = Koha::Database->new->schema;
41 $schema->storage->txn_begin;
42 my $dbh = C4::Context->dbh;
43 my $builder = t::lib::TestBuilder->new;
44
45 # Reset item types to only the default ones
46 $dbh->do(q|DELETE FROM itemtypes;|);
47 my $sql = qq|
48 INSERT INTO itemtypes (itemtype, description, rentalcharge, notforloan, imageurl, summary) VALUES
49 ('BK', 'Books',5,0,'bridge/book.gif',''),
50 ('MX', 'Mixed Materials',5,0,'bridge/kit.gif',''),
51 ('CF', 'Computer Files',5,0,'bridge/computer_file.gif',''),
52 ('MP', 'Maps',5,0,'bridge/map.gif',''),
53 ('VM', 'Visual Materials',5,1,'bridge/dvd.gif',''),
54 ('MU', 'Music',5,0,'bridge/sound.gif',''),
55 ('CR', 'Continuing Resources',5,0,'bridge/periodical.gif',''),
56 ('REF', 'Reference',0,1,'bridge/reference.gif','');|;
57 $dbh->do($sql);
58 $dbh->do(q|DELETE FROM suggestions|);
59 $dbh->do(q|DELETE FROM issues|);
60 $dbh->do(q|DELETE FROM borrowers|);
61 $dbh->do(q|DELETE FROM letter|);
62 $dbh->do(q|DELETE FROM message_queue|);
63 $dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'CHECKED', 'my content')|);
64
65 # Add CPL if missing.
66 if (not defined Koha::Libraries->find('CPL')) {
67     Koha::Library->new({ branchcode => 'CPL', branchname => 'Centerville' })->store;
68 }
69
70 my $patron_category = $builder->build({ source => 'Category' });
71
72 my $member = {
73     firstname => 'my firstname',
74     surname => 'my surname',
75     categorycode => $patron_category->{categorycode},
76     branchcode => 'CPL',
77     smsalertnumber => 12345,
78 };
79
80 my $member2 = {
81     firstname => 'my firstname',
82     surname => 'my surname',
83     categorycode => $patron_category->{categorycode},
84     branchcode => 'CPL',
85     email => 'to@example.com',
86 };
87
88 my $borrowernumber = Koha::Patron->new($member)->store->borrowernumber;
89 my $borrowernumber2 = Koha::Patron->new($member2)->store->borrowernumber;
90
91 my $biblionumber1 = 1;
92 my $my_suggestion = {
93     title         => 'my title',
94     author        => 'my author',
95     publishercode => 'my publishercode',
96     suggestedby   => $borrowernumber,
97     biblionumber  => $biblionumber1,
98     managedby     => '',
99     manageddate   => '',
100     accepteddate  => dt_from_string,
101     note          => 'my note',
102 };
103
104 my $budgetperiod_id = AddBudgetPeriod({
105     budget_period_startdate   => '2008-01-01',
106     budget_period_enddate     => '2008-12-31',
107     budget_period_description => 'MAPERI',
108     budget_period_active      => 1,
109 });
110
111 my $budget_id = AddBudget({
112     budget_code      => 'ABCD',
113     budget_amount    => '123.132000',
114     budget_name      => 'ABCD',
115     budget_notes     => 'This is a note',
116     budget_period_id => $budgetperiod_id,
117 });
118 my $my_suggestion_with_budget = {
119     title         => 'my title 2',
120     author        => 'my author 2',
121     publishercode => 'my publishercode 2',
122     suggestedby   => $borrowernumber,
123     biblionumber  => $biblionumber1,
124     managedby     => '',
125     manageddate   => '',
126     accepteddate  => dt_from_string,
127     note          => 'my note',
128     budgetid      => $budget_id,
129 };
130 my $my_suggestion_with_budget2 = {
131     title         => 'my title 3',
132     author        => 'my author 3',
133     publishercode => 'my publishercode 3',
134     suggestedby   => $borrowernumber2,
135     biblionumber  => $biblionumber1,
136     managedby     => '',
137     manageddate   => '',
138     accepteddate  => dt_from_string,
139     note          => 'my note',
140     budgetid      => $budget_id,
141 };
142
143 is( CountSuggestion(), 0, 'CountSuggestion without the status returns 0' );
144 is( CountSuggestion('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
145 is( CountSuggestion('CHECKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
146 is( CountSuggestion('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
147 is( CountSuggestion('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
148
149 my $my_suggestionid = NewSuggestion($my_suggestion);
150 isnt( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
151 my $my_suggestionid_with_budget = NewSuggestion($my_suggestion_with_budget);
152
153 is( GetSuggestion(), undef, 'GetSuggestion without the suggestion id returns undef' );
154 my $suggestion = GetSuggestion($my_suggestionid);
155 is( $suggestion->{title}, $my_suggestion->{title}, 'NewSuggestion stores the title correctly' );
156 is( $suggestion->{author}, $my_suggestion->{author}, 'NewSuggestion stores the author correctly' );
157 is( $suggestion->{publishercode}, $my_suggestion->{publishercode}, 'NewSuggestion stores the publishercode correctly' );
158 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'NewSuggestion stores the borrower number correctly' );
159 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'NewSuggestion stores the biblio number correctly' );
160 is( $suggestion->{STATUS}, 'ASKED', 'NewSuggestion stores a suggestion with the status ASKED by default' );
161 is( $suggestion->{managedby}, undef, 'NewSuggestion stores empty string as undef for non existent foreign key (integer)' );
162 is( $suggestion->{manageddate}, undef, 'NewSuggestion stores empty string as undef for date' );
163 is( $suggestion->{budgetid}, undef, 'NewSuggestion should set budgetid to NULL if not given' );
164
165 is( CountSuggestion('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
166
167
168 is( ModSuggestion(), undef, 'ModSuggestion without the suggestion returns undef' );
169 my $mod_suggestion1 = {
170     title         => 'my modified title',
171     author        => 'my modified author',
172     publishercode => 'my modified publishercode',
173     managedby     => '',
174     manageddate   => '',
175 };
176 my $status = ModSuggestion($mod_suggestion1);
177 is( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
178
179 $mod_suggestion1->{suggestionid} = $my_suggestionid;
180 $status = ModSuggestion($mod_suggestion1);
181 is( $status, 1, 'ModSuggestion modifies one entry' );
182 $suggestion = GetSuggestion($my_suggestionid);
183 is( $suggestion->{title}, $mod_suggestion1->{title}, 'ModSuggestion modifies the title  correctly' );
184 is( $suggestion->{author}, $mod_suggestion1->{author}, 'ModSuggestion modifies the author correctly' );
185 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'ModSuggestion modifies the publishercode correctly' );
186 is( $suggestion->{managedby}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
187 is( $suggestion->{manageddate}, undef, 'ModSuggestion stores empty string as undef for date' );
188 isnt( $suggestion->{accepteddate}, undef, 'ModSuggestion does not update a non given date value' );
189 is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not given' );
190
191 my $messages = C4::Letters::GetQueuedMessages({
192     borrowernumber => $borrowernumber,
193 });
194 is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
195
196 my $mod_suggestion2 = {
197     STATUS       => 'STALLED',
198     suggestionid => $my_suggestionid,
199 };
200 warning_is { $status = ModSuggestion($mod_suggestion2) }
201            "No suggestions STALLED letter transported by email",
202            "ModSuggestion status warning is correct";
203 is( $status, 1, "ModSuggestion Status OK");
204
205 my $mod_suggestion3 = {
206     STATUS       => 'CHECKED',
207     suggestionid => $my_suggestionid,
208 };
209
210 #Test the message_transport_type of suggestion notices
211
212 #Check the message_transport_type when the 'FallbackToSMSIfNoEmail' syspref is disabled
213 t::lib::Mocks::mock_preference( 'FallbackToSMSIfNoEmail', 0 );
214 $status = ModSuggestion($mod_suggestion3);
215 is( $status, 1, 'ModSuggestion modifies one entry' );
216 $suggestion = GetSuggestion($my_suggestionid);
217 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' );
218 $messages = C4::Letters::GetQueuedMessages({
219     borrowernumber => $borrowernumber,
220 });
221 is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' );
222 is ($messages->[0]->{message_transport_type}, 'email', 'When FallbackToSMSIfNoEmail syspref is disabled the suggestion message_transport_type is always email');
223 is( CountSuggestion('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
224
225 #Check the message_transport_type when the 'FallbackToSMSIfNoEmail' syspref is enabled and the borrower has a smsalertnumber and no email
226 t::lib::Mocks::mock_preference( 'FallbackToSMSIfNoEmail', 1 );
227 ModSuggestion($mod_suggestion3);
228 $messages = C4::Letters::GetQueuedMessages({
229     borrowernumber => $borrowernumber,
230 });
231 is ($messages->[1]->{message_transport_type}, 'sms', 'When FallbackToSMSIfNoEmail syspref is enabled the suggestion message_transport_type is sms if the borrower has no email');
232
233 #Make a new suggestion for a borrower with defined email and no smsalertnumber
234 my $my_suggestion_2_id = NewSuggestion($my_suggestion_with_budget2);
235
236 #Check the message_transport_type when the 'FallbackToSMSIfNoEmail' syspref is enabled and the borrower has a defined email and no smsalertnumber
237 t::lib::Mocks::mock_preference( 'FallbackToSMSIfNoEmail', 1 );
238 my $mod_suggestion4 = {
239     STATUS       => 'CHECKED',
240     suggestionid => $my_suggestion_2_id,
241 };
242 ModSuggestion($mod_suggestion4);
243 $messages = C4::Letters::GetQueuedMessages({
244     borrowernumber => $borrowernumber2,
245 });
246 is ($messages->[0]->{message_transport_type}, 'email', 'When FallbackToSMSIfNoEmail syspref is enabled the suggestion message_transport_type is email if the borrower has an email');
247
248 is( GetSuggestionInfo(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
249 $suggestion = GetSuggestionInfo($my_suggestionid);
250 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfo returns the suggestion id correctly' );
251 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfo returns the title correctly' );
252 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfo returns the author correctly' );
253 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfo returns the publisher code correctly' );
254 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
255 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfo returns the biblio number correctly' );
256 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfo returns the status correctly' );
257 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfo returns the surname correctly' );
258 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfo returns the firstname correctly' );
259 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
260
261
262 is( GetSuggestionFromBiblionumber(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
263 is( GetSuggestionFromBiblionumber(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
264 is( GetSuggestionFromBiblionumber($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
265
266
267 is( GetSuggestionInfoFromBiblionumber(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
268 is( GetSuggestionInfoFromBiblionumber(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
269 $suggestion = GetSuggestionInfoFromBiblionumber($biblionumber1);
270 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
271 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
272 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
273 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfoFromBiblionumber returns the publisher code correctly' );
274 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumber returns the borrower number correctly' );
275 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfoFromBiblionumber returns the biblio number correctly' );
276 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfoFromBiblionumber returns the status correctly' );
277 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfoFromBiblionumber returns the surname correctly' );
278 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfoFromBiblionumber returns the firstname correctly' );
279 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumeber returns the borrower number correctly' );
280
281
282 my $suggestions = GetSuggestionByStatus();
283 is( @$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
284 $suggestions = GetSuggestionByStatus('CHECKED');
285 is( @$suggestions, 2, 'GetSuggestionByStatus returns the correct number of suggestions' );
286 is( $suggestions->[0]->{suggestionid}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
287 is( $suggestions->[0]->{title}, $mod_suggestion1->{title}, 'GetSuggestionByStatus returns the title correctly' );
288 is( $suggestions->[0]->{author}, $mod_suggestion1->{author}, 'GetSuggestionByStatus returns the author correctly' );
289 is( $suggestions->[0]->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionByStatus returns the publisher code correctly' );
290 is( $suggestions->[0]->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
291 is( $suggestions->[0]->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionByStatus returns the biblio number correctly' );
292 is( $suggestions->[0]->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionByStatus returns the status correctly' );
293 is( $suggestions->[0]->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionByStatus returns the surname correctly' );
294 is( $suggestions->[0]->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionByStatus returns the firstname correctly' );
295 is( $suggestions->[0]->{branchcodesuggestedby}, $member->{branchcode}, 'GetSuggestionByStatus returns the branch code correctly' );
296 is( $suggestions->[0]->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
297 is( $suggestions->[0]->{categorycodesuggestedby}, $member->{categorycode}, 'GetSuggestionByStatus returns the category code correctly' );
298
299
300 is( ConnectSuggestionAndBiblio(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
301 my $biblionumber2 = 2;
302 my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio($my_suggestionid, $biblionumber2);
303 is( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
304 $suggestion = GetSuggestion($my_suggestionid);
305 is( $suggestion->{biblionumber}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
306
307 my $search_suggestion = SearchSuggestion();
308 is( @$search_suggestion, 3, 'SearchSuggestion without arguments returns all suggestions' );
309
310 $search_suggestion = SearchSuggestion({
311     title => $mod_suggestion1->{title},
312 });
313 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
314 $search_suggestion = SearchSuggestion({
315     title => 'another title',
316 });
317 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
318
319 $search_suggestion = SearchSuggestion({
320     author => $mod_suggestion1->{author},
321 });
322 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
323 $search_suggestion = SearchSuggestion({
324     author => 'another author',
325 });
326 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
327
328 $search_suggestion = SearchSuggestion({
329     publishercode => $mod_suggestion1->{publishercode},
330 });
331 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
332 $search_suggestion = SearchSuggestion({
333     publishercode => 'another publishercode',
334 });
335 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
336
337 $search_suggestion = SearchSuggestion({
338     STATUS => $mod_suggestion3->{STATUS},
339 });
340 is( @$search_suggestion, 2, 'SearchSuggestion returns the correct number of suggestions' );
341
342 $search_suggestion = SearchSuggestion({
343     STATUS => q||
344 });
345 is( @$search_suggestion, 0, 'SearchSuggestion should not return all suggestions if we want the suggestions with a STATUS=""' );
346 $search_suggestion = SearchSuggestion({
347     STATUS => 'REJECTED',
348 });
349 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
350
351 $search_suggestion = SearchSuggestion({
352     budgetid => '',
353 });
354 is( @$search_suggestion, 3, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' );
355 $search_suggestion = SearchSuggestion({
356     budgetid => $budget_id,
357 });
358 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' );
359 $search_suggestion = SearchSuggestion({
360     budgetid => '__NONE__',
361 });
362 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns the correct number of suggestions' );
363 $search_suggestion = SearchSuggestion({
364     budgetid => '__ANY__',
365 });
366 is( @$search_suggestion, 3, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' );
367
368 my $del_suggestion = {
369     title => 'my deleted title',
370     STATUS => 'CHECKED',
371     suggestedby => $borrowernumber,
372 };
373 my $del_suggestionid = NewSuggestion($del_suggestion);
374
375 is( CountSuggestion('CHECKED'), 3, 'CountSuggestion returns the correct number of suggestions' );
376
377 is( DelSuggestion(), '0E0', 'DelSuggestion without arguments returns 0E0' );
378 is( DelSuggestion($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
379 is( DelSuggestion(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
380 $suggestion = DelSuggestion($borrowernumber, $my_suggestionid);
381 is( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
382
383 $suggestions = GetSuggestionByStatus('CHECKED');
384 is( @$suggestions, 2, 'DelSuggestion deletes one suggestion' );
385 is( $suggestions->[1]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' );
386
387 # Test budgetid fk
388 $my_suggestion->{budgetid} = ''; # If budgetid == '', NULL should be set in DB
389 my $my_suggestionid_test_budgetid = NewSuggestion($my_suggestion);
390 $suggestion = GetSuggestion($my_suggestionid_test_budgetid);
391 is( $suggestion->{budgetid}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
392
393 $my_suggestion->{budgetid} = ''; # If budgetid == '', NULL should be set in DB
394 ModSuggestion( $my_suggestion );
395 $suggestion = GetSuggestion($my_suggestionid_test_budgetid);
396 is( $suggestion->{budgetid}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
397
398 subtest 'GetUnprocessedSuggestions' => sub {
399     plan tests => 11;
400     $dbh->do(q|DELETE FROM suggestions|);
401     my $my_suggestionid         = NewSuggestion($my_suggestion);
402     my $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
403     is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return 0 if a suggestion has been processed but not linked to a fund' );
404     my $status     = ModSuggestion($mod_suggestion1);
405     my $suggestion = GetSuggestion($my_suggestionid);
406     is( $suggestion->{budgetid}, undef, 'ModSuggestion should set budgetid to NULL if not given' );
407     ModSuggestion( { suggestionid => $my_suggestionid, budgetid => $budget_id } );
408     $suggestion = GetSuggestion($my_suggestionid);
409     is( $suggestion->{budgetid}, $budget_id, 'ModSuggestion should modify budgetid if given' );
410
411     $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
412     is( scalar(@$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
413
414     warning_is { ModSuggestion( { suggestionid => $my_suggestionid, STATUS => 'REJECTED' } ) }
415                 'No suggestions REJECTED letter transported by email',
416                 'Warning raised if no REJECTED letter by email';
417     $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
418     is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
419
420     warning_is { ModSuggestion( { suggestionid => $my_suggestionid, STATUS => 'ASKED', suggesteddate => dt_from_string->add_duration( DateTime::Duration->new( days => -4 ) ) } ); }
421                 'No suggestions ASKED letter transported by email',
422                 'Warning raised if no ASKED letter by email';
423     $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
424     is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should use 0 as default value for days' );
425     $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(4);
426     is( scalar(@$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion suggested 4 days ago' );
427     $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(3);
428     is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 3 days ago' );
429     $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(5);
430     is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 5 days ago' );
431 };
432
433 subtest 'DelSuggestionsOlderThan' => sub {
434     plan tests => 6;
435
436     Koha::Suggestions->delete;
437
438     # Add four suggestions; note that STATUS needs uppercase (FIXME)
439     my $d1 = output_pref({ dt => dt_from_string->add(days => -2), dateformat => 'sql' });
440     my $d2 = output_pref({ dt => dt_from_string->add(days => -4), dateformat => 'sql' });
441     my $sugg01 = $builder->build({ source => 'Suggestion', value => { date => $d1, STATUS => 'ASKED' }});
442     my $sugg02 = $builder->build({ source => 'Suggestion', value => { date => $d1, STATUS => 'CHECKED' }});
443     my $sugg03 = $builder->build({ source => 'Suggestion', value => { date => $d2, STATUS => 'ASKED' }});
444     my $sugg04 = $builder->build({ source => 'Suggestion', value => { date => $d2, STATUS => 'ACCEPTED' }});
445
446     # Test no parameter: should do nothing
447     C4::Suggestions::DelSuggestionsOlderThan();
448     is( Koha::Suggestions->count, 4, 'No suggestions deleted' );
449     # Test zero: should do nothing too
450     C4::Suggestions::DelSuggestionsOlderThan(0);
451     is( Koha::Suggestions->count, 4, 'No suggestions deleted again' );
452     # Test negative value
453     C4::Suggestions::DelSuggestionsOlderThan(-1);
454     is( Koha::Suggestions->count, 4, 'No suggestions deleted for -1' );
455
456     # Test positive values
457     C4::Suggestions::DelSuggestionsOlderThan(5);
458     is( Koha::Suggestions->count, 4, 'No suggestions>5d deleted' );
459     C4::Suggestions::DelSuggestionsOlderThan(3);
460     is( Koha::Suggestions->count, 3, '1 suggestions>3d deleted' );
461     C4::Suggestions::DelSuggestionsOlderThan(1);
462     is( Koha::Suggestions->count, 2, '1 suggestions>1d deleted' );
463 };
464
465 $schema->storage->txn_rollback;