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
=> 102;
35 use_ok
('C4::Suggestions');
38 my $dbh = C4
::Context
->dbh;
42 $dbh->{AutoCommit
} = 0;
43 $dbh->{RaiseError
} = 1;
45 # Reset item types to only the default ones
46 $dbh->do(q
|DELETE FROM itemtypes
;|);
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','');";
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')|);
66 if (not defined Koha
::Libraries
->find('CPL')) {
67 Koha
::Library
->new({ branchcode
=> 'CPL', branchname
=> 'Centerville' })->store;
70 my $sth = $dbh->prepare("SELECT * FROM categories WHERE categorycode='S';");
72 if (!$sth->fetchrow_hashref) {
73 $sql = "INSERT INTO categories
74 (categorycode,description,enrolmentperiod,upperagelimit,
75 dateofbirthrequired,finetype,bulk,enrolmentfee,
76 overduenoticerequired,issuelimit,reservefee,category_type)
79 18,NULL,NULL,'0.000000',
80 0,NULL,'0.000000','S');";
85 firstname
=> 'my firstname',
86 surname
=> 'my surname',
90 my $borrowernumber = AddMember
(%$member);
92 my $biblionumber1 = 1;
95 author
=> 'my author',
96 publishercode
=> 'my publishercode',
97 suggestedby
=> $borrowernumber,
98 biblionumber
=> $biblionumber1,
101 accepteddate
=> dt_from_string
,
105 my $budgetperiod_id = AddBudgetPeriod
({
106 budget_period_startdate
=> '2008-01-01',
107 budget_period_enddate
=> '2008-12-31',
108 budget_period_description
=> 'MAPERI',
109 budget_period_active
=> 1,
112 my $budget_id = AddBudget
({
113 budget_code
=> 'ABCD',
114 budget_amount
=> '123.132000',
115 budget_name
=> 'ABCD',
116 budget_notes
=> 'This is a note',
117 budget_period_id
=> $budgetperiod_id,
119 my $my_suggestion_with_budget = {
120 title
=> 'my title 2',
121 author
=> 'my author 2',
122 publishercode
=> 'my publishercode 2',
123 suggestedby
=> $borrowernumber,
124 biblionumber
=> $biblionumber1,
127 accepteddate
=> dt_from_string
,
129 budgetid
=> $budget_id,
133 is
( CountSuggestion
(), 0, 'CountSuggestion without the status returns 0' );
134 is
( CountSuggestion
('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
135 is
( CountSuggestion
('CHECKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
136 is
( CountSuggestion
('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
137 is
( CountSuggestion
('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
139 my $my_suggestionid = NewSuggestion
($my_suggestion);
140 isnt
( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
141 my $my_suggestionid_with_budget = NewSuggestion
($my_suggestion_with_budget);
143 is
( GetSuggestion
(), undef, 'GetSuggestion without the suggestion id returns undef' );
144 my $suggestion = GetSuggestion
($my_suggestionid);
145 is
( $suggestion->{title
}, $my_suggestion->{title
}, 'NewSuggestion stores the title correctly' );
146 is
( $suggestion->{author
}, $my_suggestion->{author
}, 'NewSuggestion stores the author correctly' );
147 is
( $suggestion->{publishercode
}, $my_suggestion->{publishercode
}, 'NewSuggestion stores the publishercode correctly' );
148 is
( $suggestion->{suggestedby
}, $my_suggestion->{suggestedby
}, 'NewSuggestion stores the borrower number correctly' );
149 is
( $suggestion->{biblionumber
}, $my_suggestion->{biblionumber
}, 'NewSuggestion stores the biblio number correctly' );
150 is
( $suggestion->{STATUS
}, 'ASKED', 'NewSuggestion stores a suggestion with the status ASKED by default' );
151 is
( $suggestion->{managedby
}, undef, 'NewSuggestion stores empty string as undef for non existent foreign key (integer)' );
152 is
( $suggestion->{manageddate
}, undef, 'NewSuggestion stores empty string as undef for date' );
153 is
( $suggestion->{budgetid
}, undef, 'NewSuggestion should set budgetid to NULL if not given' );
155 is
( CountSuggestion
('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
158 is
( ModSuggestion
(), undef, 'ModSuggestion without the suggestion returns undef' );
159 my $mod_suggestion1 = {
160 title
=> 'my modified title',
161 author
=> 'my modified author',
162 publishercode
=> 'my modified publishercode',
166 my $status = ModSuggestion
($mod_suggestion1);
167 is
( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
169 $mod_suggestion1->{suggestionid
} = $my_suggestionid;
170 $status = ModSuggestion
($mod_suggestion1);
171 is
( $status, 1, 'ModSuggestion modifies one entry' );
172 $suggestion = GetSuggestion
($my_suggestionid);
173 is
( $suggestion->{title
}, $mod_suggestion1->{title
}, 'ModSuggestion modifies the title correctly' );
174 is
( $suggestion->{author
}, $mod_suggestion1->{author
}, 'ModSuggestion modifies the author correctly' );
175 is
( $suggestion->{publishercode
}, $mod_suggestion1->{publishercode
}, 'ModSuggestion modifies the publishercode correctly' );
176 is
( $suggestion->{managedby
}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
177 is
( $suggestion->{manageddate
}, undef, 'ModSuggestion stores empty string as undef for date' );
178 isnt
( $suggestion->{accepteddate
}, undef, 'ModSuggestion does not update a non given date value' );
179 is
( $suggestion->{note
}, 'my note', 'ModSuggestion should not erase data if not given' );
181 my $messages = C4
::Letters
::GetQueuedMessages
({
182 borrowernumber
=> $borrowernumber,
184 is
( @
$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
186 my $mod_suggestion2 = {
188 suggestionid
=> $my_suggestionid,
190 warning_is
{ $status = ModSuggestion
($mod_suggestion2) }
191 "No suggestions STALLED letter transported by email",
192 "ModSuggestion status warning is correct";
193 is
( $status, 1, "ModSuggestion Status OK");
195 my $mod_suggestion3 = {
197 suggestionid
=> $my_suggestionid,
199 $status = ModSuggestion
($mod_suggestion3);
201 is
( $status, 1, 'ModSuggestion modifies one entry' );
202 $suggestion = GetSuggestion
($my_suggestionid);
203 is
( $suggestion->{STATUS
}, $mod_suggestion3->{STATUS
}, 'ModSuggestion modifies the status correctly' );
204 $messages = C4
::Letters
::GetQueuedMessages
({
205 borrowernumber
=> $borrowernumber,
207 is
( @
$messages, 1, 'ModSuggestion sends an email if the status is updated' );
209 is
( CountSuggestion
('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
212 is
( GetSuggestionInfo
(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
213 $suggestion = GetSuggestionInfo
($my_suggestionid);
214 is
( $suggestion->{suggestionid
}, $my_suggestionid, 'GetSuggestionInfo returns the suggestion id correctly' );
215 is
( $suggestion->{title
}, $mod_suggestion1->{title
}, 'GetSuggestionInfo returns the title correctly' );
216 is
( $suggestion->{author
}, $mod_suggestion1->{author
}, 'GetSuggestionInfo returns the author correctly' );
217 is
( $suggestion->{publishercode
}, $mod_suggestion1->{publishercode
}, 'GetSuggestionInfo returns the publisher code correctly' );
218 is
( $suggestion->{suggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfo returns the borrower number correctly' );
219 is
( $suggestion->{biblionumber
}, $my_suggestion->{biblionumber
}, 'GetSuggestionInfo returns the biblio number correctly' );
220 is
( $suggestion->{STATUS
}, $mod_suggestion3->{STATUS
}, 'GetSuggestionInfo returns the status correctly' );
221 is
( $suggestion->{surnamesuggestedby
}, $member->{surname
}, 'GetSuggestionInfo returns the surname correctly' );
222 is
( $suggestion->{firstnamesuggestedby
}, $member->{firstname
}, 'GetSuggestionInfo returns the firstname correctly' );
223 is
( $suggestion->{borrnumsuggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfo returns the borrower number correctly' );
226 is
( GetSuggestionFromBiblionumber
(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
227 is
( GetSuggestionFromBiblionumber
(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
228 is
( GetSuggestionFromBiblionumber
($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
231 is
( GetSuggestionInfoFromBiblionumber
(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
232 is
( GetSuggestionInfoFromBiblionumber
(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
233 $suggestion = GetSuggestionInfoFromBiblionumber
($biblionumber1);
234 is
( $suggestion->{suggestionid
}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
235 is
( $suggestion->{title
}, $mod_suggestion1->{title
}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
236 is
( $suggestion->{author
}, $mod_suggestion1->{author
}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
237 is
( $suggestion->{publishercode
}, $mod_suggestion1->{publishercode
}, 'GetSuggestionInfoFromBiblionumber returns the publisher code correctly' );
238 is
( $suggestion->{suggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfoFromBiblionumber returns the borrower number correctly' );
239 is
( $suggestion->{biblionumber
}, $my_suggestion->{biblionumber
}, 'GetSuggestionInfoFromBiblionumber returns the biblio number correctly' );
240 is
( $suggestion->{STATUS
}, $mod_suggestion3->{STATUS
}, 'GetSuggestionInfoFromBiblionumber returns the status correctly' );
241 is
( $suggestion->{surnamesuggestedby
}, $member->{surname
}, 'GetSuggestionInfoFromBiblionumber returns the surname correctly' );
242 is
( $suggestion->{firstnamesuggestedby
}, $member->{firstname
}, 'GetSuggestionInfoFromBiblionumber returns the firstname correctly' );
243 is
( $suggestion->{borrnumsuggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfoFromBiblionumeber returns the borrower number correctly' );
246 my $suggestions = GetSuggestionByStatus
();
247 is
( @
$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
248 $suggestions = GetSuggestionByStatus
('CHECKED');
249 is
( @
$suggestions, 1, 'GetSuggestionByStatus returns the correct number of suggestions' );
250 is
( $suggestions->[0]->{suggestionid
}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
251 is
( $suggestions->[0]->{title
}, $mod_suggestion1->{title
}, 'GetSuggestionByStatus returns the title correctly' );
252 is
( $suggestions->[0]->{author
}, $mod_suggestion1->{author
}, 'GetSuggestionByStatus returns the author correctly' );
253 is
( $suggestions->[0]->{publishercode
}, $mod_suggestion1->{publishercode
}, 'GetSuggestionByStatus returns the publisher code correctly' );
254 is
( $suggestions->[0]->{suggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionByStatus returns the borrower number correctly' );
255 is
( $suggestions->[0]->{biblionumber
}, $my_suggestion->{biblionumber
}, 'GetSuggestionByStatus returns the biblio number correctly' );
256 is
( $suggestions->[0]->{STATUS
}, $mod_suggestion3->{STATUS
}, 'GetSuggestionByStatus returns the status correctly' );
257 is
( $suggestions->[0]->{surnamesuggestedby
}, $member->{surname
}, 'GetSuggestionByStatus returns the surname correctly' );
258 is
( $suggestions->[0]->{firstnamesuggestedby
}, $member->{firstname
}, 'GetSuggestionByStatus returns the firstname correctly' );
259 is
( $suggestions->[0]->{branchcodesuggestedby
}, $member->{branchcode
}, 'GetSuggestionByStatus returns the branch code correctly' );
260 is
( $suggestions->[0]->{borrnumsuggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionByStatus returns the borrower number correctly' );
261 is
( $suggestions->[0]->{categorycodesuggestedby
}, $member->{categorycode
}, 'GetSuggestionByStatus returns the category code correctly' );
264 is
( ConnectSuggestionAndBiblio
(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
265 my $biblionumber2 = 2;
266 my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio
($my_suggestionid, $biblionumber2);
267 is
( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
268 $suggestion = GetSuggestion
($my_suggestionid);
269 is
( $suggestion->{biblionumber
}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
271 my $search_suggestion = SearchSuggestion
();
272 is
( @
$search_suggestion, 2, 'SearchSuggestion without arguments returns all suggestions' );
274 $search_suggestion = SearchSuggestion
({
275 title
=> $mod_suggestion1->{title
},
277 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
278 $search_suggestion = SearchSuggestion
({
279 title
=> 'another title',
281 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
283 $search_suggestion = SearchSuggestion
({
284 author
=> $mod_suggestion1->{author
},
286 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
287 $search_suggestion = SearchSuggestion
({
288 author
=> 'another author',
290 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
292 $search_suggestion = SearchSuggestion
({
293 publishercode
=> $mod_suggestion1->{publishercode
},
295 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
296 $search_suggestion = SearchSuggestion
({
297 publishercode
=> 'another publishercode',
299 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
301 $search_suggestion = SearchSuggestion
({
302 STATUS
=> $mod_suggestion3->{STATUS
},
304 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
306 $search_suggestion = SearchSuggestion
({
309 is
( @
$search_suggestion, 0, 'SearchSuggestion should not return all suggestions if we want the suggestions with a STATUS=""' );
310 $search_suggestion = SearchSuggestion
({
311 STATUS
=> 'REJECTED',
313 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
315 $search_suggestion = SearchSuggestion
({
318 is
( @
$search_suggestion, 2, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' );
319 $search_suggestion = SearchSuggestion
({
320 budgetid
=> $budget_id,
322 is
( @
$search_suggestion, 1, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' );
323 $search_suggestion = SearchSuggestion
({
324 budgetid
=> '__NONE__',
326 is
( @
$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns the correct number of suggestions' );
327 $search_suggestion = SearchSuggestion
({
328 budgetid
=> '__ANY__',
330 is
( @
$search_suggestion, 2, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' );
332 my $del_suggestion = {
333 title
=> 'my deleted title',
335 suggestedby
=> $borrowernumber,
337 my $del_suggestionid = NewSuggestion
($del_suggestion);
339 is
( CountSuggestion
('CHECKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
341 is
( DelSuggestion
(), '0E0', 'DelSuggestion without arguments returns 0E0' );
342 is
( DelSuggestion
($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
343 is
( DelSuggestion
(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
344 $suggestion = DelSuggestion
($borrowernumber, $my_suggestionid);
345 is
( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
347 $suggestions = GetSuggestionByStatus
('CHECKED');
348 is
( @
$suggestions, 1, 'DelSuggestion deletes one suggestion' );
349 is
( $suggestions->[0]->{title
}, $del_suggestion->{title
}, 'DelSuggestion deletes the correct suggestion' );
352 $my_suggestion->{budgetid
} = ''; # If budgetid == '', NULL should be set in DB
353 my $my_suggestionid_test_budgetid = NewSuggestion
($my_suggestion);
354 $suggestion = GetSuggestion
($my_suggestionid_test_budgetid);
355 is
( $suggestion->{budgetid
}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
357 $my_suggestion->{budgetid
} = ''; # If budgetid == '', NULL should be set in DB
358 ModSuggestion
( $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 subtest
'GetUnprocessedSuggestions' => sub {
364 $dbh->do(q
|DELETE FROM suggestions
|);
365 my $my_suggestionid = NewSuggestion
($my_suggestion);
366 my $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
367 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return 0 if a suggestion has been processed but not linked to a fund' );
368 my $status = ModSuggestion
($mod_suggestion1);
369 my $suggestion = GetSuggestion
($my_suggestionid);
370 is
( $suggestion->{budgetid
}, undef, 'ModSuggestion should set budgetid to NULL if not given' );
371 ModSuggestion
( { suggestionid
=> $my_suggestionid, budgetid
=> $budget_id } );
372 $suggestion = GetSuggestion
($my_suggestionid);
373 is
( $suggestion->{budgetid
}, $budget_id, 'ModSuggestion should modify budgetid if given' );
375 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
376 is
( scalar(@
$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
378 warning_is
{ ModSuggestion
( { suggestionid
=> $my_suggestionid, STATUS
=> 'REJECTED' } ) }
379 'No suggestions REJECTED letter transported by email',
380 'Warning raised if no REJECTED letter by email';
381 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
382 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
384 warning_is
{ ModSuggestion
( { suggestionid
=> $my_suggestionid, STATUS
=> 'ASKED', suggesteddate
=> dt_from_string
->add_duration( DateTime
::Duration
->new( days
=> -4 ) ) } ); }
385 'No suggestions ASKED letter transported by email',
386 'Warning raised if no ASKED letter by email';
387 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
388 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should use 0 as default value for days' );
389 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
(4);
390 is
( scalar(@
$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion suggested 4 days ago' );
391 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
(3);
392 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 3 days ago' );
393 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
(5);
394 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 5 days ago' );