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 );
30 use DateTime
::Duration
;
31 use Test
::More tests
=> 106;
35 use_ok
('C4::Suggestions');
39 my $dbh = C4
::Context
->dbh;
43 $dbh->{AutoCommit
} = 0;
44 $dbh->{RaiseError
} = 1;
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 $sth = $dbh->prepare("SELECT * FROM categories WHERE categorycode='S';");
73 if (!$sth->fetchrow_hashref) {
74 $sql = "INSERT INTO categories
75 (categorycode,description,enrolmentperiod,upperagelimit,
76 dateofbirthrequired,finetype,bulk,enrolmentfee,
77 overduenoticerequired,issuelimit,reservefee,category_type)
80 18,NULL,NULL,'0.000000',
81 0,NULL,'0.000000','S');";
86 firstname
=> 'my firstname',
87 surname
=> 'my surname',
91 my $borrowernumber = AddMember
(%$member);
93 my $biblionumber1 = 1;
96 author
=> 'my author',
97 publishercode
=> 'my publishercode',
98 suggestedby
=> $borrowernumber,
99 biblionumber
=> $biblionumber1,
102 accepteddate
=> dt_from_string
,
106 my $budgetperiod_id = AddBudgetPeriod
({
107 budget_period_startdate
=> '2008-01-01',
108 budget_period_enddate
=> '2008-12-31',
109 budget_period_description
=> 'MAPERI',
110 budget_period_active
=> 1,
113 my $budget_id = AddBudget
({
114 budget_code
=> 'ABCD',
115 budget_amount
=> '123.132000',
116 budget_name
=> 'ABCD',
117 budget_notes
=> 'This is a note',
118 budget_period_id
=> $budgetperiod_id,
120 my $my_suggestion_with_budget = {
121 title
=> 'my title 2',
122 author
=> 'my author 2',
123 publishercode
=> 'my publishercode 2',
124 suggestedby
=> $borrowernumber,
125 biblionumber
=> $biblionumber1,
128 accepteddate
=> dt_from_string
,
130 budgetid
=> $budget_id,
134 is
( CountSuggestion
(), 0, 'CountSuggestion without the status returns 0' );
135 is
( CountSuggestion
('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
136 is
( CountSuggestion
('CHECKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
137 is
( CountSuggestion
('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
138 is
( CountSuggestion
('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
140 my $my_suggestionid = NewSuggestion
($my_suggestion);
141 isnt
( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
142 my $my_suggestionid_with_budget = NewSuggestion
($my_suggestion_with_budget);
144 is
( GetSuggestion
(), undef, 'GetSuggestion without the suggestion id returns undef' );
145 my $suggestion = GetSuggestion
($my_suggestionid);
146 is
( $suggestion->{title
}, $my_suggestion->{title
}, 'NewSuggestion stores the title correctly' );
147 is
( $suggestion->{author
}, $my_suggestion->{author
}, 'NewSuggestion stores the author correctly' );
148 is
( $suggestion->{publishercode
}, $my_suggestion->{publishercode
}, 'NewSuggestion stores the publishercode correctly' );
149 is
( $suggestion->{suggestedby
}, $my_suggestion->{suggestedby
}, 'NewSuggestion stores the borrower number correctly' );
150 is
( $suggestion->{biblionumber
}, $my_suggestion->{biblionumber
}, 'NewSuggestion stores the biblio number correctly' );
151 is
( $suggestion->{STATUS
}, 'ASKED', 'NewSuggestion stores a suggestion with the status ASKED by default' );
152 is
( $suggestion->{managedby
}, undef, 'NewSuggestion stores empty string as undef for non existent foreign key (integer)' );
153 is
( $suggestion->{manageddate
}, undef, 'NewSuggestion stores empty string as undef for date' );
154 is
( $suggestion->{budgetid
}, undef, 'NewSuggestion should set budgetid to NULL if not given' );
156 is
( CountSuggestion
('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
159 is
( ModSuggestion
(), undef, 'ModSuggestion without the suggestion returns undef' );
160 my $mod_suggestion1 = {
161 title
=> 'my modified title',
162 author
=> 'my modified author',
163 publishercode
=> 'my modified publishercode',
167 my $status = ModSuggestion
($mod_suggestion1);
168 is
( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
170 $mod_suggestion1->{suggestionid
} = $my_suggestionid;
171 $status = ModSuggestion
($mod_suggestion1);
172 is
( $status, 1, 'ModSuggestion modifies one entry' );
173 $suggestion = GetSuggestion
($my_suggestionid);
174 is
( $suggestion->{title
}, $mod_suggestion1->{title
}, 'ModSuggestion modifies the title correctly' );
175 is
( $suggestion->{author
}, $mod_suggestion1->{author
}, 'ModSuggestion modifies the author correctly' );
176 is
( $suggestion->{publishercode
}, $mod_suggestion1->{publishercode
}, 'ModSuggestion modifies the publishercode correctly' );
177 is
( $suggestion->{managedby
}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
178 is
( $suggestion->{manageddate
}, undef, 'ModSuggestion stores empty string as undef for date' );
179 isnt
( $suggestion->{accepteddate
}, undef, 'ModSuggestion does not update a non given date value' );
180 is
( $suggestion->{note
}, 'my note', 'ModSuggestion should not erase data if not given' );
182 my $messages = C4
::Letters
::GetQueuedMessages
({
183 borrowernumber
=> $borrowernumber,
185 is
( @
$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
187 my $mod_suggestion2 = {
189 suggestionid
=> $my_suggestionid,
191 warning_is
{ $status = ModSuggestion
($mod_suggestion2) }
192 "No suggestions STALLED letter transported by email",
193 "ModSuggestion status warning is correct";
194 is
( $status, 1, "ModSuggestion Status OK");
196 my $mod_suggestion3 = {
198 suggestionid
=> $my_suggestionid,
200 $status = ModSuggestion
($mod_suggestion3);
202 is
( $status, 1, 'ModSuggestion modifies one entry' );
203 $suggestion = GetSuggestion
($my_suggestionid);
204 is
( $suggestion->{STATUS
}, $mod_suggestion3->{STATUS
}, 'ModSuggestion modifies the status correctly' );
205 $messages = C4
::Letters
::GetQueuedMessages
({
206 borrowernumber
=> $borrowernumber,
208 is
( @
$messages, 1, 'ModSuggestion sends an email if the status is updated' );
210 is
( CountSuggestion
('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
213 is
( GetSuggestionInfo
(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
214 $suggestion = GetSuggestionInfo
($my_suggestionid);
215 is
( $suggestion->{suggestionid
}, $my_suggestionid, 'GetSuggestionInfo returns the suggestion id correctly' );
216 is
( $suggestion->{title
}, $mod_suggestion1->{title
}, 'GetSuggestionInfo returns the title correctly' );
217 is
( $suggestion->{author
}, $mod_suggestion1->{author
}, 'GetSuggestionInfo returns the author correctly' );
218 is
( $suggestion->{publishercode
}, $mod_suggestion1->{publishercode
}, 'GetSuggestionInfo returns the publisher code correctly' );
219 is
( $suggestion->{suggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfo returns the borrower number correctly' );
220 is
( $suggestion->{biblionumber
}, $my_suggestion->{biblionumber
}, 'GetSuggestionInfo returns the biblio number correctly' );
221 is
( $suggestion->{STATUS
}, $mod_suggestion3->{STATUS
}, 'GetSuggestionInfo returns the status correctly' );
222 is
( $suggestion->{surnamesuggestedby
}, $member->{surname
}, 'GetSuggestionInfo returns the surname correctly' );
223 is
( $suggestion->{firstnamesuggestedby
}, $member->{firstname
}, 'GetSuggestionInfo returns the firstname correctly' );
224 is
( $suggestion->{borrnumsuggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfo returns the borrower number correctly' );
227 is
( GetSuggestionFromBiblionumber
(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
228 is
( GetSuggestionFromBiblionumber
(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
229 is
( GetSuggestionFromBiblionumber
($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
232 is
( GetSuggestionInfoFromBiblionumber
(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
233 is
( GetSuggestionInfoFromBiblionumber
(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
234 $suggestion = GetSuggestionInfoFromBiblionumber
($biblionumber1);
235 is
( $suggestion->{suggestionid
}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
236 is
( $suggestion->{title
}, $mod_suggestion1->{title
}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
237 is
( $suggestion->{author
}, $mod_suggestion1->{author
}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
238 is
( $suggestion->{publishercode
}, $mod_suggestion1->{publishercode
}, 'GetSuggestionInfoFromBiblionumber returns the publisher code correctly' );
239 is
( $suggestion->{suggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfoFromBiblionumber returns the borrower number correctly' );
240 is
( $suggestion->{biblionumber
}, $my_suggestion->{biblionumber
}, 'GetSuggestionInfoFromBiblionumber returns the biblio number correctly' );
241 is
( $suggestion->{STATUS
}, $mod_suggestion3->{STATUS
}, 'GetSuggestionInfoFromBiblionumber returns the status correctly' );
242 is
( $suggestion->{surnamesuggestedby
}, $member->{surname
}, 'GetSuggestionInfoFromBiblionumber returns the surname correctly' );
243 is
( $suggestion->{firstnamesuggestedby
}, $member->{firstname
}, 'GetSuggestionInfoFromBiblionumber returns the firstname correctly' );
244 is
( $suggestion->{borrnumsuggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfoFromBiblionumeber returns the borrower number correctly' );
247 my $suggestions = GetSuggestionByStatus
();
248 is
( @
$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
249 $suggestions = GetSuggestionByStatus
('CHECKED');
250 is
( @
$suggestions, 1, 'GetSuggestionByStatus returns the correct number of suggestions' );
251 is
( $suggestions->[0]->{suggestionid
}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
252 is
( $suggestions->[0]->{title
}, $mod_suggestion1->{title
}, 'GetSuggestionByStatus returns the title correctly' );
253 is
( $suggestions->[0]->{author
}, $mod_suggestion1->{author
}, 'GetSuggestionByStatus returns the author correctly' );
254 is
( $suggestions->[0]->{publishercode
}, $mod_suggestion1->{publishercode
}, 'GetSuggestionByStatus returns the publisher code correctly' );
255 is
( $suggestions->[0]->{suggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionByStatus returns the borrower number correctly' );
256 is
( $suggestions->[0]->{biblionumber
}, $my_suggestion->{biblionumber
}, 'GetSuggestionByStatus returns the biblio number correctly' );
257 is
( $suggestions->[0]->{STATUS
}, $mod_suggestion3->{STATUS
}, 'GetSuggestionByStatus returns the status correctly' );
258 is
( $suggestions->[0]->{surnamesuggestedby
}, $member->{surname
}, 'GetSuggestionByStatus returns the surname correctly' );
259 is
( $suggestions->[0]->{firstnamesuggestedby
}, $member->{firstname
}, 'GetSuggestionByStatus returns the firstname correctly' );
260 is
( $suggestions->[0]->{branchcodesuggestedby
}, $member->{branchcode
}, 'GetSuggestionByStatus returns the branch code correctly' );
261 is
( $suggestions->[0]->{borrnumsuggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionByStatus returns the borrower number correctly' );
262 is
( $suggestions->[0]->{categorycodesuggestedby
}, $member->{categorycode
}, 'GetSuggestionByStatus returns the category code correctly' );
265 is
( ConnectSuggestionAndBiblio
(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
266 my $biblionumber2 = 2;
267 my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio
($my_suggestionid, $biblionumber2);
268 is
( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
269 $suggestion = GetSuggestion
($my_suggestionid);
270 is
( $suggestion->{biblionumber
}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
272 my $search_suggestion = SearchSuggestion
();
273 is
( @
$search_suggestion, 2, 'SearchSuggestion without arguments returns all suggestions' );
275 $search_suggestion = SearchSuggestion
({
276 title
=> $mod_suggestion1->{title
},
278 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
279 $search_suggestion = SearchSuggestion
({
280 title
=> 'another title',
282 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
284 $search_suggestion = SearchSuggestion
({
285 author
=> $mod_suggestion1->{author
},
287 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
288 $search_suggestion = SearchSuggestion
({
289 author
=> 'another author',
291 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
293 $search_suggestion = SearchSuggestion
({
294 publishercode
=> $mod_suggestion1->{publishercode
},
296 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
297 $search_suggestion = SearchSuggestion
({
298 publishercode
=> 'another publishercode',
300 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
302 $search_suggestion = SearchSuggestion
({
303 STATUS
=> $mod_suggestion3->{STATUS
},
305 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
307 $search_suggestion = SearchSuggestion
({
310 is
( @
$search_suggestion, 0, 'SearchSuggestion should not return all suggestions if we want the suggestions with a STATUS=""' );
311 $search_suggestion = SearchSuggestion
({
312 STATUS
=> 'REJECTED',
314 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
316 $search_suggestion = SearchSuggestion
({
319 is
( @
$search_suggestion, 2, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' );
320 $search_suggestion = SearchSuggestion
({
321 budgetid
=> $budget_id,
323 is
( @
$search_suggestion, 1, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' );
324 $search_suggestion = SearchSuggestion
({
325 budgetid
=> '__NONE__',
327 is
( @
$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns the correct number of suggestions' );
328 $search_suggestion = SearchSuggestion
({
329 budgetid
=> '__ANY__',
331 is
( @
$search_suggestion, 2, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' );
333 my $del_suggestion = {
334 title
=> 'my deleted title',
336 suggestedby
=> $borrowernumber,
338 my $del_suggestionid = NewSuggestion
($del_suggestion);
340 is
( CountSuggestion
('CHECKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
342 is
( DelSuggestion
(), '0E0', 'DelSuggestion without arguments returns 0E0' );
343 is
( DelSuggestion
($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
344 is
( DelSuggestion
(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
345 $suggestion = DelSuggestion
($borrowernumber, $my_suggestionid);
346 is
( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
348 $suggestions = GetSuggestionByStatus
('CHECKED');
349 is
( @
$suggestions, 1, 'DelSuggestion deletes one suggestion' );
350 is
( $suggestions->[0]->{title
}, $del_suggestion->{title
}, 'DelSuggestion deletes the correct suggestion' );
352 ## Bug 11466, making sure GetSupportList() returns itemtypes, even if AdvancedSearchTypes has multiple values
353 t
::lib
::Mocks
::mock_preference
("AdvancedSearchTypes", 'itemtypes|loc|ccode');
354 my $itemtypes1 = C4
::Koha
::GetSupportList
();
355 is
(@
$itemtypes1, 8, "Purchase suggestion itemtypes collected, multiple AdvancedSearchTypes");
357 t
::lib
::Mocks
::mock_preference
("AdvancedSearchTypes", 'itemtypes');
358 my $itemtypes2 = C4
::Koha
::GetSupportList
();
359 is
(@
$itemtypes2, 8, "Purchase suggestion itemtypes collected, default AdvancedSearchTypes");
361 is_deeply
($itemtypes1, $itemtypes2, 'same set of purchase suggestion formats retrieved');
364 $my_suggestion->{budgetid
} = ''; # If budgetid == '', NULL should be set in DB
365 my $my_suggestionid_test_budgetid = NewSuggestion
($my_suggestion);
366 $suggestion = GetSuggestion
($my_suggestionid_test_budgetid);
367 is
( $suggestion->{budgetid
}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
369 $my_suggestion->{budgetid
} = ''; # If budgetid == '', NULL should be set in DB
370 ModSuggestion
( $my_suggestion );
371 $suggestion = GetSuggestion
($my_suggestionid_test_budgetid);
372 is
( $suggestion->{budgetid
}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
374 subtest
'GetUnprocessedSuggestions' => sub {
376 $dbh->do(q
|DELETE FROM suggestions
|);
377 my $my_suggestionid = NewSuggestion
($my_suggestion);
378 my $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
379 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return 0 if a suggestion has been processed but not linked to a fund' );
380 my $status = ModSuggestion
($mod_suggestion1);
381 my $suggestion = GetSuggestion
($my_suggestionid);
382 is
( $suggestion->{budgetid
}, undef, 'ModSuggestion should set budgetid to NULL if not given' );
383 ModSuggestion
( { suggestionid
=> $my_suggestionid, budgetid
=> $budget_id } );
384 $suggestion = GetSuggestion
($my_suggestionid);
385 is
( $suggestion->{budgetid
}, $budget_id, 'ModSuggestion should modify budgetid if given' );
387 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
388 is
( scalar(@
$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
390 warning_is
{ ModSuggestion
( { suggestionid
=> $my_suggestionid, STATUS
=> 'REJECTED' } ) }
391 'No suggestions REJECTED letter transported by email',
392 'Warning raised if no REJECTED letter by email';
393 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
394 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
396 warning_is
{ ModSuggestion
( { suggestionid
=> $my_suggestionid, STATUS
=> 'ASKED', suggesteddate
=> dt_from_string
->add_duration( DateTime
::Duration
->new( days
=> -4 ) ) } ); }
397 'No suggestions ASKED letter transported by email',
398 'Warning raised if no ASKED letter by email';
399 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
400 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should use 0 as default value for days' );
401 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
(4);
402 is
( scalar(@
$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion suggested 4 days ago' );
403 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
(3);
404 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 3 days ago' );
405 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
(5);
406 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 5 days ago' );