3 # This file is part of Koha.
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.
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.
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>.
20 use DateTime::Duration;
21 use Test::More tests => 103;
25 use t::lib::TestBuilder;
30 use C4::Budgets qw( AddBudgetPeriod AddBudget );
32 use Koha::DateUtils qw( dt_from_string output_pref );
35 use Koha::Suggestions;
38 use_ok('C4::Suggestions');
41 my $schema = Koha::Database->new->schema;
42 $schema->storage->txn_begin;
43 my $dbh = C4::Context->dbh;
44 my $builder = t::lib::TestBuilder->new;
46 # Reset item types to only the default ones
47 $dbh->do(q|DELETE FROM itemtypes;|);
49 INSERT INTO itemtypes (itemtype, description, rentalcharge, notforloan, imageurl, summary) VALUES
50 ('BK', 'Books',5,0,'bridge/book.gif',''),
51 ('MX', 'Mixed Materials',5,0,'bridge/kit.gif',''),
52 ('CF', 'Computer Files',5,0,'bridge/computer_file.gif',''),
53 ('MP', 'Maps',5,0,'bridge/map.gif',''),
54 ('VM', 'Visual Materials',5,1,'bridge/dvd.gif',''),
55 ('MU', 'Music',5,0,'bridge/sound.gif',''),
56 ('CR', 'Continuing Resources',5,0,'bridge/periodical.gif',''),
57 ('REF', 'Reference',0,1,'bridge/reference.gif','');|;
59 $dbh->do(q|DELETE FROM suggestions|);
60 $dbh->do(q|DELETE FROM issues|);
61 $dbh->do(q|DELETE FROM borrowers|);
62 $dbh->do(q|DELETE FROM letter|);
63 $dbh->do(q|DELETE FROM message_queue|);
64 $dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'CHECKED', 'my content')|);
67 if (not defined Koha::Libraries->find('CPL')) {
68 Koha::Library->new({ branchcode => 'CPL', branchname => 'Centerville' })->store;
71 my $patron_category = $builder->build({ source => 'Category' });
74 firstname => 'my firstname',
75 surname => 'my surname',
76 categorycode => $patron_category->{categorycode},
79 my $borrowernumber = AddMember(%$member);
81 my $biblionumber1 = 1;
84 author => 'my author',
85 publishercode => 'my publishercode',
86 suggestedby => $borrowernumber,
87 biblionumber => $biblionumber1,
90 accepteddate => dt_from_string,
94 my $budgetperiod_id = AddBudgetPeriod({
95 budget_period_startdate => '2008-01-01',
96 budget_period_enddate => '2008-12-31',
97 budget_period_description => 'MAPERI',
98 budget_period_active => 1,
101 my $budget_id = AddBudget({
102 budget_code => 'ABCD',
103 budget_amount => '123.132000',
104 budget_name => 'ABCD',
105 budget_notes => 'This is a note',
106 budget_period_id => $budgetperiod_id,
108 my $my_suggestion_with_budget = {
109 title => 'my title 2',
110 author => 'my author 2',
111 publishercode => 'my publishercode 2',
112 suggestedby => $borrowernumber,
113 biblionumber => $biblionumber1,
116 accepteddate => dt_from_string,
118 budgetid => $budget_id,
122 is( CountSuggestion(), 0, 'CountSuggestion without the status returns 0' );
123 is( CountSuggestion('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
124 is( CountSuggestion('CHECKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
125 is( CountSuggestion('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
126 is( CountSuggestion('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
128 my $my_suggestionid = NewSuggestion($my_suggestion);
129 isnt( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
130 my $my_suggestionid_with_budget = NewSuggestion($my_suggestion_with_budget);
132 is( GetSuggestion(), undef, 'GetSuggestion without the suggestion id returns undef' );
133 my $suggestion = GetSuggestion($my_suggestionid);
134 is( $suggestion->{title}, $my_suggestion->{title}, 'NewSuggestion stores the title correctly' );
135 is( $suggestion->{author}, $my_suggestion->{author}, 'NewSuggestion stores the author correctly' );
136 is( $suggestion->{publishercode}, $my_suggestion->{publishercode}, 'NewSuggestion stores the publishercode correctly' );
137 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'NewSuggestion stores the borrower number correctly' );
138 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'NewSuggestion stores the biblio number correctly' );
139 is( $suggestion->{STATUS}, 'ASKED', 'NewSuggestion stores a suggestion with the status ASKED by default' );
140 is( $suggestion->{managedby}, undef, 'NewSuggestion stores empty string as undef for non existent foreign key (integer)' );
141 is( $suggestion->{manageddate}, undef, 'NewSuggestion stores empty string as undef for date' );
142 is( $suggestion->{budgetid}, undef, 'NewSuggestion should set budgetid to NULL if not given' );
144 is( CountSuggestion('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
147 is( ModSuggestion(), undef, 'ModSuggestion without the suggestion returns undef' );
148 my $mod_suggestion1 = {
149 title => 'my modified title',
150 author => 'my modified author',
151 publishercode => 'my modified publishercode',
155 my $status = ModSuggestion($mod_suggestion1);
156 is( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
158 $mod_suggestion1->{suggestionid} = $my_suggestionid;
159 $status = ModSuggestion($mod_suggestion1);
160 is( $status, 1, 'ModSuggestion modifies one entry' );
161 $suggestion = GetSuggestion($my_suggestionid);
162 is( $suggestion->{title}, $mod_suggestion1->{title}, 'ModSuggestion modifies the title correctly' );
163 is( $suggestion->{author}, $mod_suggestion1->{author}, 'ModSuggestion modifies the author correctly' );
164 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'ModSuggestion modifies the publishercode correctly' );
165 is( $suggestion->{managedby}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
166 is( $suggestion->{manageddate}, undef, 'ModSuggestion stores empty string as undef for date' );
167 isnt( $suggestion->{accepteddate}, undef, 'ModSuggestion does not update a non given date value' );
168 is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not given' );
170 my $messages = C4::Letters::GetQueuedMessages({
171 borrowernumber => $borrowernumber,
173 is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
175 my $mod_suggestion2 = {
177 suggestionid => $my_suggestionid,
179 warning_is { $status = ModSuggestion($mod_suggestion2) }
180 "No suggestions STALLED letter transported by email",
181 "ModSuggestion status warning is correct";
182 is( $status, 1, "ModSuggestion Status OK");
184 my $mod_suggestion3 = {
186 suggestionid => $my_suggestionid,
188 $status = ModSuggestion($mod_suggestion3);
190 is( $status, 1, 'ModSuggestion modifies one entry' );
191 $suggestion = GetSuggestion($my_suggestionid);
192 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' );
193 $messages = C4::Letters::GetQueuedMessages({
194 borrowernumber => $borrowernumber,
196 is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' );
198 is( CountSuggestion('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
201 is( GetSuggestionInfo(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
202 $suggestion = GetSuggestionInfo($my_suggestionid);
203 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfo returns the suggestion id correctly' );
204 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfo returns the title correctly' );
205 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfo returns the author correctly' );
206 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfo returns the publisher code correctly' );
207 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
208 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfo returns the biblio number correctly' );
209 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfo returns the status correctly' );
210 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfo returns the surname correctly' );
211 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfo returns the firstname correctly' );
212 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
215 is( GetSuggestionFromBiblionumber(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
216 is( GetSuggestionFromBiblionumber(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
217 is( GetSuggestionFromBiblionumber($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
220 is( GetSuggestionInfoFromBiblionumber(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
221 is( GetSuggestionInfoFromBiblionumber(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
222 $suggestion = GetSuggestionInfoFromBiblionumber($biblionumber1);
223 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
224 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
225 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
226 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfoFromBiblionumber returns the publisher code correctly' );
227 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumber returns the borrower number correctly' );
228 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfoFromBiblionumber returns the biblio number correctly' );
229 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfoFromBiblionumber returns the status correctly' );
230 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfoFromBiblionumber returns the surname correctly' );
231 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfoFromBiblionumber returns the firstname correctly' );
232 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumeber returns the borrower number correctly' );
235 my $suggestions = GetSuggestionByStatus();
236 is( @$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
237 $suggestions = GetSuggestionByStatus('CHECKED');
238 is( @$suggestions, 1, 'GetSuggestionByStatus returns the correct number of suggestions' );
239 is( $suggestions->[0]->{suggestionid}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
240 is( $suggestions->[0]->{title}, $mod_suggestion1->{title}, 'GetSuggestionByStatus returns the title correctly' );
241 is( $suggestions->[0]->{author}, $mod_suggestion1->{author}, 'GetSuggestionByStatus returns the author correctly' );
242 is( $suggestions->[0]->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionByStatus returns the publisher code correctly' );
243 is( $suggestions->[0]->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
244 is( $suggestions->[0]->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionByStatus returns the biblio number correctly' );
245 is( $suggestions->[0]->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionByStatus returns the status correctly' );
246 is( $suggestions->[0]->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionByStatus returns the surname correctly' );
247 is( $suggestions->[0]->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionByStatus returns the firstname correctly' );
248 is( $suggestions->[0]->{branchcodesuggestedby}, $member->{branchcode}, 'GetSuggestionByStatus returns the branch code correctly' );
249 is( $suggestions->[0]->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
250 is( $suggestions->[0]->{categorycodesuggestedby}, $member->{categorycode}, 'GetSuggestionByStatus returns the category code correctly' );
253 is( ConnectSuggestionAndBiblio(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
254 my $biblionumber2 = 2;
255 my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio($my_suggestionid, $biblionumber2);
256 is( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
257 $suggestion = GetSuggestion($my_suggestionid);
258 is( $suggestion->{biblionumber}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
260 my $search_suggestion = SearchSuggestion();
261 is( @$search_suggestion, 2, 'SearchSuggestion without arguments returns all suggestions' );
263 $search_suggestion = SearchSuggestion({
264 title => $mod_suggestion1->{title},
266 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
267 $search_suggestion = SearchSuggestion({
268 title => 'another title',
270 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
272 $search_suggestion = SearchSuggestion({
273 author => $mod_suggestion1->{author},
275 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
276 $search_suggestion = SearchSuggestion({
277 author => 'another author',
279 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
281 $search_suggestion = SearchSuggestion({
282 publishercode => $mod_suggestion1->{publishercode},
284 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
285 $search_suggestion = SearchSuggestion({
286 publishercode => 'another publishercode',
288 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
290 $search_suggestion = SearchSuggestion({
291 STATUS => $mod_suggestion3->{STATUS},
293 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
295 $search_suggestion = SearchSuggestion({
298 is( @$search_suggestion, 0, 'SearchSuggestion should not return all suggestions if we want the suggestions with a STATUS=""' );
299 $search_suggestion = SearchSuggestion({
300 STATUS => 'REJECTED',
302 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
304 $search_suggestion = SearchSuggestion({
307 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' );
308 $search_suggestion = SearchSuggestion({
309 budgetid => $budget_id,
311 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' );
312 $search_suggestion = SearchSuggestion({
313 budgetid => '__NONE__',
315 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns the correct number of suggestions' );
316 $search_suggestion = SearchSuggestion({
317 budgetid => '__ANY__',
319 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' );
321 my $del_suggestion = {
322 title => 'my deleted title',
324 suggestedby => $borrowernumber,
326 my $del_suggestionid = NewSuggestion($del_suggestion);
328 is( CountSuggestion('CHECKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
330 is( DelSuggestion(), '0E0', 'DelSuggestion without arguments returns 0E0' );
331 is( DelSuggestion($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
332 is( DelSuggestion(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
333 $suggestion = DelSuggestion($borrowernumber, $my_suggestionid);
334 is( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
336 $suggestions = GetSuggestionByStatus('CHECKED');
337 is( @$suggestions, 1, 'DelSuggestion deletes one suggestion' );
338 is( $suggestions->[0]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' );
341 $my_suggestion->{budgetid} = ''; # If budgetid == '', NULL should be set in DB
342 my $my_suggestionid_test_budgetid = NewSuggestion($my_suggestion);
343 $suggestion = GetSuggestion($my_suggestionid_test_budgetid);
344 is( $suggestion->{budgetid}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
346 $my_suggestion->{budgetid} = ''; # If budgetid == '', NULL should be set in DB
347 ModSuggestion( $my_suggestion );
348 $suggestion = GetSuggestion($my_suggestionid_test_budgetid);
349 is( $suggestion->{budgetid}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
351 subtest 'GetUnprocessedSuggestions' => sub {
353 $dbh->do(q|DELETE FROM suggestions|);
354 my $my_suggestionid = NewSuggestion($my_suggestion);
355 my $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
356 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return 0 if a suggestion has been processed but not linked to a fund' );
357 my $status = ModSuggestion($mod_suggestion1);
358 my $suggestion = GetSuggestion($my_suggestionid);
359 is( $suggestion->{budgetid}, undef, 'ModSuggestion should set budgetid to NULL if not given' );
360 ModSuggestion( { suggestionid => $my_suggestionid, budgetid => $budget_id } );
361 $suggestion = GetSuggestion($my_suggestionid);
362 is( $suggestion->{budgetid}, $budget_id, 'ModSuggestion should modify budgetid if given' );
364 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
365 is( scalar(@$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
367 warning_is { ModSuggestion( { suggestionid => $my_suggestionid, STATUS => 'REJECTED' } ) }
368 'No suggestions REJECTED letter transported by email',
369 'Warning raised if no REJECTED letter by email';
370 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
371 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
373 warning_is { ModSuggestion( { suggestionid => $my_suggestionid, STATUS => 'ASKED', suggesteddate => dt_from_string->add_duration( DateTime::Duration->new( days => -4 ) ) } ); }
374 'No suggestions ASKED letter transported by email',
375 'Warning raised if no ASKED letter by email';
376 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
377 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should use 0 as default value for days' );
378 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(4);
379 is( scalar(@$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion suggested 4 days ago' );
380 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(3);
381 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 3 days ago' );
382 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(5);
383 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 5 days ago' );
386 subtest 'DelSuggestionsOlderThan' => sub {
389 Koha::Suggestions->delete;
391 # Add four suggestions; note that STATUS needs uppercase (FIXME)
392 my $d1 = output_pref({ dt => dt_from_string->add(days => -2), dateformat => 'sql' });
393 my $d2 = output_pref({ dt => dt_from_string->add(days => -4), dateformat => 'sql' });
394 my $sugg01 = $builder->build({ source => 'Suggestion', value => { date => $d1, STATUS => 'ASKED' }});
395 my $sugg02 = $builder->build({ source => 'Suggestion', value => { date => $d1, STATUS => 'CHECKED' }});
396 my $sugg03 = $builder->build({ source => 'Suggestion', value => { date => $d2, STATUS => 'ASKED' }});
397 my $sugg04 = $builder->build({ source => 'Suggestion', value => { date => $d2, STATUS => 'ACCEPTED' }});
399 # Test no parameter: should do nothing
400 C4::Suggestions::DelSuggestionsOlderThan();
401 is( Koha::Suggestions->count, 4, 'No suggestions deleted' );
402 # Test zero: should do nothing too
403 C4::Suggestions::DelSuggestionsOlderThan(0);
404 is( Koha::Suggestions->count, 4, 'No suggestions deleted again' );
405 # Test negative value
406 C4::Suggestions::DelSuggestionsOlderThan(-1);
407 is( Koha::Suggestions->count, 4, 'No suggestions deleted for -1' );
409 # Test positive values
410 C4::Suggestions::DelSuggestionsOlderThan(5);
411 is( Koha::Suggestions->count, 4, 'No suggestions>5d deleted' );
412 C4::Suggestions::DelSuggestionsOlderThan(3);
413 is( Koha::Suggestions->count, 3, '1 suggestions>3d deleted' );
414 C4::Suggestions::DelSuggestionsOlderThan(1);
415 is( Koha::Suggestions->count, 2, '1 suggestions>1d deleted' );
418 $schema->storage->txn_rollback;