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>.
24 use C4
::Budgets
qw( AddBudgetPeriod AddBudget );
26 use Koha
::DateUtils
qw( dt_from_string );
28 use DateTime
::Duration
;
29 use Test
::More tests
=> 105;
33 use_ok
('C4::Suggestions');
37 my $dbh = C4
::Context
->dbh;
41 $dbh->{AutoCommit
} = 0;
42 $dbh->{RaiseError
} = 1;
44 # Reset item types to only the default ones
45 $dbh->do(q
|DELETE FROM itemtypes
;|);
47 INSERT INTO itemtypes (itemtype, description, rentalcharge, notforloan, imageurl, summary) VALUES
48 ('BK', 'Books',5,0,'bridge/book.gif',''),
49 ('MX', 'Mixed Materials',5,0,'bridge/kit.gif',''),
50 ('CF', 'Computer Files',5,0,'bridge/computer_file.gif',''),
51 ('MP', 'Maps',5,0,'bridge/map.gif',''),
52 ('VM', 'Visual Materials',5,1,'bridge/dvd.gif',''),
53 ('MU', 'Music',5,0,'bridge/sound.gif',''),
54 ('CR', 'Continuing Resources',5,0,'bridge/periodical.gif',''),
55 ('REF', 'Reference',0,1,'bridge/reference.gif','');";
57 $dbh->do(q
|DELETE FROM suggestions
|);
58 $dbh->do(q
|DELETE FROM issues
|);
59 $dbh->do(q
|DELETE FROM borrowers
|);
60 $dbh->do(q
|DELETE FROM letter
|);
61 $dbh->do(q
|DELETE FROM message_queue
|);
62 $dbh->do(q
|INSERT INTO letter
(module
, code
, content
) VALUES
('suggestions', 'CHECKED', 'my content')|);
65 if (not defined GetBranchDetail
('CPL')) {
66 ModBranch
({add
=> 1, branchcode
=> 'CPL', branchname
=> 'Centerville'});
69 my $sth = $dbh->prepare("SELECT * FROM categories WHERE categorycode='S';");
71 if (!$sth->fetchrow_hashref) {
72 $sql = "INSERT INTO categories
73 (categorycode,description,enrolmentperiod,upperagelimit,
74 dateofbirthrequired,finetype,bulk,enrolmentfee,
75 overduenoticerequired,issuelimit,reservefee,category_type)
78 18,NULL,NULL,'0.000000',
79 0,NULL,'0.000000','S');";
84 firstname
=> 'my firstname',
85 surname
=> 'my surname',
89 my $borrowernumber = AddMember
(%$member);
91 my $biblionumber1 = 1;
94 author
=> 'my author',
95 publishercode
=> 'my publishercode',
96 suggestedby
=> $borrowernumber,
97 biblionumber
=> $biblionumber1,
100 accepteddate
=> dt_from_string
,
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,
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,
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,
126 accepteddate
=> dt_from_string
,
128 budgetid
=> $budget_id,
132 is
( CountSuggestion
(), 0, 'CountSuggestion without the status returns 0' );
133 is
( CountSuggestion
('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
134 is
( CountSuggestion
('CHECKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
135 is
( CountSuggestion
('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
136 is
( CountSuggestion
('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
138 my $my_suggestionid = NewSuggestion
($my_suggestion);
139 isnt
( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
140 my $my_suggestionid_with_budget = NewSuggestion
($my_suggestion_with_budget);
142 is
( GetSuggestion
(), undef, 'GetSuggestion without the suggestion id returns undef' );
143 my $suggestion = GetSuggestion
($my_suggestionid);
144 is
( $suggestion->{title
}, $my_suggestion->{title
}, 'NewSuggestion stores the title correctly' );
145 is
( $suggestion->{author
}, $my_suggestion->{author
}, 'NewSuggestion stores the author correctly' );
146 is
( $suggestion->{publishercode
}, $my_suggestion->{publishercode
}, 'NewSuggestion stores the publishercode correctly' );
147 is
( $suggestion->{suggestedby
}, $my_suggestion->{suggestedby
}, 'NewSuggestion stores the borrower number correctly' );
148 is
( $suggestion->{biblionumber
}, $my_suggestion->{biblionumber
}, 'NewSuggestion stores the biblio number correctly' );
149 is
( $suggestion->{STATUS
}, 'ASKED', 'NewSuggestion stores a suggestion with the status ASKED by default' );
150 is
( $suggestion->{managedby
}, undef, 'NewSuggestion stores empty string as undef for non existent foreign key (integer)' );
151 is
( $suggestion->{manageddate
}, undef, 'NewSuggestion stores empty string as undef for date' );
152 is
( $suggestion->{budgetid
}, undef, 'NewSuggestion should set budgetid to NULL if not given' );
154 is
( CountSuggestion
('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
157 is
( ModSuggestion
(), undef, 'ModSuggestion without the suggestion returns undef' );
158 my $mod_suggestion1 = {
159 title
=> 'my modified title',
160 author
=> 'my modified author',
161 publishercode
=> 'my modified publishercode',
165 my $status = ModSuggestion
($mod_suggestion1);
166 is
( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
168 $mod_suggestion1->{suggestionid
} = $my_suggestionid;
169 $status = ModSuggestion
($mod_suggestion1);
170 is
( $status, 1, 'ModSuggestion modifies one entry' );
171 $suggestion = GetSuggestion
($my_suggestionid);
172 is
( $suggestion->{title
}, $mod_suggestion1->{title
}, 'ModSuggestion modifies the title correctly' );
173 is
( $suggestion->{author
}, $mod_suggestion1->{author
}, 'ModSuggestion modifies the author correctly' );
174 is
( $suggestion->{publishercode
}, $mod_suggestion1->{publishercode
}, 'ModSuggestion modifies the publishercode correctly' );
175 is
( $suggestion->{managedby
}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
176 is
( $suggestion->{manageddate
}, undef, 'ModSuggestion stores empty string as undef for date' );
177 isnt
( $suggestion->{accepteddate
}, undef, 'ModSuggestion does not update a non given date value' );
178 is
( $suggestion->{note
}, 'my note', 'ModSuggestion should not erase data if not given' );
180 my $messages = C4
::Letters
::GetQueuedMessages
({
181 borrowernumber
=> $borrowernumber,
183 is
( @
$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
185 my $mod_suggestion2 = {
187 suggestionid
=> $my_suggestionid,
189 warning_is
{ $status = ModSuggestion
($mod_suggestion2) }
190 "No suggestions STALLED letter transported by email",
191 "ModSuggestion status warning is correct";
192 is
( $status, 1, "ModSuggestion Status OK");
194 my $mod_suggestion3 = {
196 suggestionid
=> $my_suggestionid,
198 $status = ModSuggestion
($mod_suggestion3);
200 is
( $status, 1, 'ModSuggestion modifies one entry' );
201 $suggestion = GetSuggestion
($my_suggestionid);
202 is
( $suggestion->{STATUS
}, $mod_suggestion3->{STATUS
}, 'ModSuggestion modifies the status correctly' );
203 $messages = C4
::Letters
::GetQueuedMessages
({
204 borrowernumber
=> $borrowernumber,
206 is
( @
$messages, 1, 'ModSuggestion sends an email if the status is updated' );
208 is
( CountSuggestion
('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
211 is
( GetSuggestionInfo
(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
212 $suggestion = GetSuggestionInfo
($my_suggestionid);
213 is
( $suggestion->{suggestionid
}, $my_suggestionid, 'GetSuggestionInfo returns the suggestion id correctly' );
214 is
( $suggestion->{title
}, $mod_suggestion1->{title
}, 'GetSuggestionInfo returns the title correctly' );
215 is
( $suggestion->{author
}, $mod_suggestion1->{author
}, 'GetSuggestionInfo returns the author correctly' );
216 is
( $suggestion->{publishercode
}, $mod_suggestion1->{publishercode
}, 'GetSuggestionInfo returns the publisher code correctly' );
217 is
( $suggestion->{suggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfo returns the borrower number correctly' );
218 is
( $suggestion->{biblionumber
}, $my_suggestion->{biblionumber
}, 'GetSuggestionInfo returns the biblio number correctly' );
219 is
( $suggestion->{STATUS
}, $mod_suggestion3->{STATUS
}, 'GetSuggestionInfo returns the status correctly' );
220 is
( $suggestion->{surnamesuggestedby
}, $member->{surname
}, 'GetSuggestionInfo returns the surname correctly' );
221 is
( $suggestion->{firstnamesuggestedby
}, $member->{firstname
}, 'GetSuggestionInfo returns the firstname correctly' );
222 is
( $suggestion->{borrnumsuggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfo returns the borrower number correctly' );
225 is
( GetSuggestionFromBiblionumber
(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
226 is
( GetSuggestionFromBiblionumber
(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
227 is
( GetSuggestionFromBiblionumber
($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
230 is
( GetSuggestionInfoFromBiblionumber
(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
231 is
( GetSuggestionInfoFromBiblionumber
(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
232 $suggestion = GetSuggestionInfoFromBiblionumber
($biblionumber1);
233 is
( $suggestion->{suggestionid
}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
234 is
( $suggestion->{title
}, $mod_suggestion1->{title
}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
235 is
( $suggestion->{author
}, $mod_suggestion1->{author
}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
236 is
( $suggestion->{publishercode
}, $mod_suggestion1->{publishercode
}, 'GetSuggestionInfoFromBiblionumber returns the publisher code correctly' );
237 is
( $suggestion->{suggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfoFromBiblionumber returns the borrower number correctly' );
238 is
( $suggestion->{biblionumber
}, $my_suggestion->{biblionumber
}, 'GetSuggestionInfoFromBiblionumber returns the biblio number correctly' );
239 is
( $suggestion->{STATUS
}, $mod_suggestion3->{STATUS
}, 'GetSuggestionInfoFromBiblionumber returns the status correctly' );
240 is
( $suggestion->{surnamesuggestedby
}, $member->{surname
}, 'GetSuggestionInfoFromBiblionumber returns the surname correctly' );
241 is
( $suggestion->{firstnamesuggestedby
}, $member->{firstname
}, 'GetSuggestionInfoFromBiblionumber returns the firstname correctly' );
242 is
( $suggestion->{borrnumsuggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfoFromBiblionumeber returns the borrower number correctly' );
245 my $suggestions = GetSuggestionByStatus
();
246 is
( @
$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
247 $suggestions = GetSuggestionByStatus
('CHECKED');
248 is
( @
$suggestions, 1, 'GetSuggestionByStatus returns the correct number of suggestions' );
249 is
( $suggestions->[0]->{suggestionid
}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
250 is
( $suggestions->[0]->{title
}, $mod_suggestion1->{title
}, 'GetSuggestionByStatus returns the title correctly' );
251 is
( $suggestions->[0]->{author
}, $mod_suggestion1->{author
}, 'GetSuggestionByStatus returns the author correctly' );
252 is
( $suggestions->[0]->{publishercode
}, $mod_suggestion1->{publishercode
}, 'GetSuggestionByStatus returns the publisher code correctly' );
253 is
( $suggestions->[0]->{suggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionByStatus returns the borrower number correctly' );
254 is
( $suggestions->[0]->{biblionumber
}, $my_suggestion->{biblionumber
}, 'GetSuggestionByStatus returns the biblio number correctly' );
255 is
( $suggestions->[0]->{STATUS
}, $mod_suggestion3->{STATUS
}, 'GetSuggestionByStatus returns the status correctly' );
256 is
( $suggestions->[0]->{surnamesuggestedby
}, $member->{surname
}, 'GetSuggestionByStatus returns the surname correctly' );
257 is
( $suggestions->[0]->{firstnamesuggestedby
}, $member->{firstname
}, 'GetSuggestionByStatus returns the firstname correctly' );
258 is
( $suggestions->[0]->{branchcodesuggestedby
}, $member->{branchcode
}, 'GetSuggestionByStatus returns the branch code correctly' );
259 is
( $suggestions->[0]->{borrnumsuggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionByStatus returns the borrower number correctly' );
260 is
( $suggestions->[0]->{categorycodesuggestedby
}, $member->{categorycode
}, 'GetSuggestionByStatus returns the category code correctly' );
263 is
( ConnectSuggestionAndBiblio
(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
264 my $biblionumber2 = 2;
265 my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio
($my_suggestionid, $biblionumber2);
266 is
( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
267 $suggestion = GetSuggestion
($my_suggestionid);
268 is
( $suggestion->{biblionumber
}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
270 my $search_suggestion = SearchSuggestion
();
271 is
( @
$search_suggestion, 2, 'SearchSuggestion without arguments returns all suggestions' );
273 $search_suggestion = SearchSuggestion
({
274 title
=> $mod_suggestion1->{title
},
276 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
277 $search_suggestion = SearchSuggestion
({
278 title
=> 'another title',
280 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
282 $search_suggestion = SearchSuggestion
({
283 author
=> $mod_suggestion1->{author
},
285 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
286 $search_suggestion = SearchSuggestion
({
287 author
=> 'another author',
289 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
291 $search_suggestion = SearchSuggestion
({
292 publishercode
=> $mod_suggestion1->{publishercode
},
294 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
295 $search_suggestion = SearchSuggestion
({
296 publishercode
=> 'another publishercode',
298 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
300 $search_suggestion = SearchSuggestion
({
301 STATUS
=> $mod_suggestion3->{STATUS
},
303 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
304 $search_suggestion = SearchSuggestion
({
305 STATUS
=> 'REJECTED',
307 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
309 $search_suggestion = SearchSuggestion
({
312 is
( @
$search_suggestion, 2, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' );
313 $search_suggestion = SearchSuggestion
({
314 budgetid
=> $budget_id,
316 is
( @
$search_suggestion, 1, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' );
317 $search_suggestion = SearchSuggestion
({
318 budgetid
=> '__NONE__',
320 is
( @
$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns the correct number of suggestions' );
321 $search_suggestion = SearchSuggestion
({
322 budgetid
=> '__ANY__',
324 is
( @
$search_suggestion, 2, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' );
326 my $del_suggestion = {
327 title
=> 'my deleted title',
329 suggestedby
=> $borrowernumber,
331 my $del_suggestionid = NewSuggestion
($del_suggestion);
333 is
( CountSuggestion
('CHECKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
335 is
( DelSuggestion
(), '0E0', 'DelSuggestion without arguments returns 0E0' );
336 is
( DelSuggestion
($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
337 is
( DelSuggestion
(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
338 $suggestion = DelSuggestion
($borrowernumber, $my_suggestionid);
339 is
( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
341 $suggestions = GetSuggestionByStatus
('CHECKED');
342 is
( @
$suggestions, 1, 'DelSuggestion deletes one suggestion' );
343 is
( $suggestions->[0]->{title
}, $del_suggestion->{title
}, 'DelSuggestion deletes the correct suggestion' );
345 ## Bug 11466, making sure GetSupportList() returns itemtypes, even if AdvancedSearchTypes has multiple values
346 C4
::Context
->set_preference("AdvancedSearchTypes", 'itemtypes|loc|ccode');
347 my $itemtypes1 = C4
::Koha
::GetSupportList
();
348 is
(@
$itemtypes1, 8, "Purchase suggestion itemtypes collected, multiple AdvancedSearchTypes");
350 C4
::Context
->set_preference("AdvancedSearchTypes", 'itemtypes');
351 my $itemtypes2 = C4
::Koha
::GetSupportList
();
352 is
(@
$itemtypes2, 8, "Purchase suggestion itemtypes collected, default AdvancedSearchTypes");
354 is_deeply
($itemtypes1, $itemtypes2, 'same set of purchase suggestion formats retrieved');
357 $my_suggestion->{budgetid
} = ''; # If budgetid == '', NULL should be set in DB
358 my $my_suggestionid_test_budgetid = NewSuggestion
($my_suggestion);
359 $suggestion = GetSuggestion
($my_suggestionid_test_budgetid);
360 is
( $suggestion->{budgetid
}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
362 $my_suggestion->{budgetid
} = ''; # If budgetid == '', NULL should be set in DB
363 ModSuggestion
( $my_suggestion );
364 $suggestion = GetSuggestion
($my_suggestionid_test_budgetid);
365 is
( $suggestion->{budgetid
}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
367 subtest
'GetUnprocessedSuggestions' => sub {
369 $dbh->do(q
|DELETE FROM suggestions
|);
370 my $my_suggestionid = NewSuggestion
($my_suggestion);
371 my $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
372 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return 0 if a suggestion has been processed but not linked to a fund' );
373 my $status = ModSuggestion
($mod_suggestion1);
374 my $suggestion = GetSuggestion
($my_suggestionid);
375 is
( $suggestion->{budgetid
}, undef, 'ModSuggestion should set budgetid to NULL if not given' );
376 ModSuggestion
( { suggestionid
=> $my_suggestionid, budgetid
=> $budget_id } );
377 $suggestion = GetSuggestion
($my_suggestionid);
378 is
( $suggestion->{budgetid
}, $budget_id, 'ModSuggestion should modify budgetid if given' );
380 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
381 is
( scalar(@
$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
383 warning_is
{ ModSuggestion
( { suggestionid
=> $my_suggestionid, STATUS
=> 'REJECTED' } ) }
384 'No suggestions REJECTED letter transported by email',
385 'Warning raised if no REJECTED letter by email';
386 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
387 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
389 warning_is
{ ModSuggestion
( { suggestionid
=> $my_suggestionid, STATUS
=> 'ASKED', suggesteddate
=> dt_from_string
->add_duration( DateTime
::Duration
->new( days
=> -4 ) ) } ); }
390 'No suggestions ASKED letter transported by email',
391 'Warning raised if no ASKED letter by email';
392 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
393 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should use 0 as default value for days' );
394 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
(4);
395 is
( scalar(@
$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion suggested 4 days ago' );
396 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
(3);
397 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 3 days ago' );
398 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
(5);
399 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 5 days ago' );