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>.
25 use Koha::DateUtils qw( dt_from_string );
27 use Test::More tests => 101;
31 use_ok('C4::Suggestions');
35 my $dbh = C4::Context->dbh;
38 $dbh->{AutoCommit} = 0;
39 $dbh->{RaiseError} = 1;
41 # Reset item types to only the default ones
42 $dbh->do(q|DELETE FROM itemtypes;|);
44 INSERT INTO itemtypes (itemtype, description, rentalcharge, notforloan, imageurl, summary) VALUES
45 ('BK', 'Books',5,0,'bridge/book.gif',''),
46 ('MX', 'Mixed Materials',5,0,'bridge/kit.gif',''),
47 ('CF', 'Computer Files',5,0,'bridge/computer_file.gif',''),
48 ('MP', 'Maps',5,0,'bridge/map.gif',''),
49 ('VM', 'Visual Materials',5,1,'bridge/dvd.gif',''),
50 ('MU', 'Music',5,0,'bridge/sound.gif',''),
51 ('CR', 'Continuing Resources',5,0,'bridge/periodical.gif',''),
52 ('REF', 'Reference',0,1,'bridge/reference.gif','');";
54 $dbh->do(q|DELETE FROM suggestions|);
55 $dbh->do(q|DELETE FROM issues|);
56 $dbh->do(q|DELETE FROM borrowers|);
57 $dbh->do(q|DELETE FROM letter|);
58 $dbh->do(q|DELETE FROM message_queue|);
59 $dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'CHECKED', 'my content')|);
62 firstname => 'my firstname',
63 surname => 'my surname',
67 my $borrowernumber = AddMember(%$member);
69 my $biblionumber1 = 1;
72 author => 'my author',
73 publishercode => 'my publishercode',
74 suggestedby => $borrowernumber,
75 biblionumber => $biblionumber1,
78 accepteddate => dt_from_string,
82 my $budgetperiod_id = AddBudgetPeriod({
83 budget_period_startdate => '2008-01-01',
84 budget_period_enddate => '2008-12-31',
85 budget_period_description => 'MAPERI',
86 budget_period_active => 1,
89 my $budget_id = AddBudget({
90 budget_code => 'ABCD',
91 budget_amount => '123.132000',
92 budget_name => 'ABCD',
93 budget_notes => 'This is a note',
94 budget_period_id => $budgetperiod_id,
96 my $my_suggestion_with_budget = {
97 title => 'my title 2',
98 author => 'my author 2',
99 publishercode => 'my publishercode 2',
100 suggestedby => $borrowernumber,
101 biblionumber => $biblionumber1,
104 accepteddate => dt_from_string,
106 budgetid => $budget_id,
110 is( CountSuggestion(), 0, 'CountSuggestion without the status returns 0' );
111 is( CountSuggestion('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
112 is( CountSuggestion('CHECKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
113 is( CountSuggestion('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
114 is( CountSuggestion('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
116 my $my_suggestionid = NewSuggestion($my_suggestion);
117 isnt( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
118 my $my_suggestionid_with_budget = NewSuggestion($my_suggestion_with_budget);
120 is( GetSuggestion(), undef, 'GetSuggestion without the suggestion id returns undef' );
121 my $suggestion = GetSuggestion($my_suggestionid);
122 is( $suggestion->{title}, $my_suggestion->{title}, 'NewSuggestion stores the title correctly' );
123 is( $suggestion->{author}, $my_suggestion->{author}, 'NewSuggestion stores the author correctly' );
124 is( $suggestion->{publishercode}, $my_suggestion->{publishercode}, 'NewSuggestion stores the publishercode correctly' );
125 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'NewSuggestion stores the borrower number correctly' );
126 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'NewSuggestion stores the biblio number correctly' );
127 is( $suggestion->{STATUS}, 'ASKED', 'NewSuggestion stores a suggestion with the status ASKED by default' );
128 is( $suggestion->{managedby}, undef, 'NewSuggestion stores empty string as undef for non existent foreign key (integer)' );
129 is( $suggestion->{manageddate}, undef, 'NewSuggestion stores empty string as undef for date' );
130 is( CountSuggestion('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
133 is( ModSuggestion(), undef, 'ModSuggestion without the suggestion returns undef' );
134 my $mod_suggestion1 = {
135 title => 'my modified title',
136 author => 'my modified author',
137 publishercode => 'my modified publishercode',
141 my $status = ModSuggestion($mod_suggestion1);
142 is( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
144 $mod_suggestion1->{suggestionid} = $my_suggestionid;
145 $status = ModSuggestion($mod_suggestion1);
146 is( $status, 1, 'ModSuggestion modifies one entry' );
147 $suggestion = GetSuggestion($my_suggestionid);
148 is( $suggestion->{title}, $mod_suggestion1->{title}, 'ModSuggestion modifies the title correctly' );
149 is( $suggestion->{author}, $mod_suggestion1->{author}, 'ModSuggestion modifies the author correctly' );
150 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'ModSuggestion modifies the publishercode correctly' );
151 is( $suggestion->{managedby}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
152 is( $suggestion->{manageddate}, undef, 'ModSuggestion stores empty string as undef for date' );
153 isnt( $suggestion->{accepteddate}, undef, 'ModSuggestion does not update a non given date value' );
154 is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not given' );
156 my $messages = C4::Letters::GetQueuedMessages({
157 borrowernumber => $borrowernumber,
159 is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
161 my $mod_suggestion2 = {
163 suggestionid => $my_suggestionid,
165 warning_is { $status = ModSuggestion($mod_suggestion2) }
166 "No suggestions STALLED letter transported by email",
167 "ModSuggestion status warning is correct";
168 is( $status, 1, "ModSuggestion Status OK");
170 my $mod_suggestion3 = {
172 suggestionid => $my_suggestionid,
174 $status = ModSuggestion($mod_suggestion3);
176 is( $status, 1, 'ModSuggestion modifies one entry' );
177 $suggestion = GetSuggestion($my_suggestionid);
178 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' );
179 $messages = C4::Letters::GetQueuedMessages({
180 borrowernumber => $borrowernumber,
182 is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' );
184 is( CountSuggestion('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
187 is( GetSuggestionInfo(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
188 $suggestion = GetSuggestionInfo($my_suggestionid);
189 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfo returns the suggestion id correctly' );
190 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfo returns the title correctly' );
191 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfo returns the author correctly' );
192 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfo returns the publisher code correctly' );
193 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
194 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfo returns the biblio number correctly' );
195 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfo returns the status correctly' );
196 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfo returns the surname correctly' );
197 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfo returns the firstname correctly' );
198 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
201 is( GetSuggestionFromBiblionumber(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
202 is( GetSuggestionFromBiblionumber(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
203 is( GetSuggestionFromBiblionumber($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
206 is( GetSuggestionInfoFromBiblionumber(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
207 is( GetSuggestionInfoFromBiblionumber(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
208 $suggestion = GetSuggestionInfoFromBiblionumber($biblionumber1);
209 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
210 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
211 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
212 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfoFromBiblionumber returns the publisher code correctly' );
213 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumber returns the borrower number correctly' );
214 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfoFromBiblionumber returns the biblio number correctly' );
215 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfoFromBiblionumber returns the status correctly' );
216 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfoFromBiblionumber returns the surname correctly' );
217 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfoFromBiblionumber returns the firstname correctly' );
218 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumeber returns the borrower number correctly' );
221 my $suggestions = GetSuggestionByStatus();
222 is( @$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
223 $suggestions = GetSuggestionByStatus('CHECKED');
224 is( @$suggestions, 1, 'GetSuggestionByStatus returns the correct number of suggestions' );
225 is( $suggestions->[0]->{suggestionid}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
226 is( $suggestions->[0]->{title}, $mod_suggestion1->{title}, 'GetSuggestionByStatus returns the title correctly' );
227 is( $suggestions->[0]->{author}, $mod_suggestion1->{author}, 'GetSuggestionByStatus returns the author correctly' );
228 is( $suggestions->[0]->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionByStatus returns the publisher code correctly' );
229 is( $suggestions->[0]->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
230 is( $suggestions->[0]->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionByStatus returns the biblio number correctly' );
231 is( $suggestions->[0]->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionByStatus returns the status correctly' );
232 is( $suggestions->[0]->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionByStatus returns the surname correctly' );
233 is( $suggestions->[0]->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionByStatus returns the firstname correctly' );
234 is( $suggestions->[0]->{branchcodesuggestedby}, $member->{branchcode}, 'GetSuggestionByStatus returns the branch code correctly' );
235 is( $suggestions->[0]->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
236 is( $suggestions->[0]->{categorycodesuggestedby}, $member->{categorycode}, 'GetSuggestionByStatus returns the category code correctly' );
239 is( ConnectSuggestionAndBiblio(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
240 my $biblionumber2 = 2;
241 my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio($my_suggestionid, $biblionumber2);
242 is( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
243 $suggestion = GetSuggestion($my_suggestionid);
244 is( $suggestion->{biblionumber}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
246 my $search_suggestion = SearchSuggestion();
247 is( @$search_suggestion, 2, 'SearchSuggestion without arguments returns all suggestions' );
249 $search_suggestion = SearchSuggestion({
250 title => $mod_suggestion1->{title},
252 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
253 $search_suggestion = SearchSuggestion({
254 title => 'another title',
256 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
258 $search_suggestion = SearchSuggestion({
259 author => $mod_suggestion1->{author},
261 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
262 $search_suggestion = SearchSuggestion({
263 author => 'another author',
265 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
267 $search_suggestion = SearchSuggestion({
268 publishercode => $mod_suggestion1->{publishercode},
270 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
271 $search_suggestion = SearchSuggestion({
272 publishercode => 'another publishercode',
274 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
276 $search_suggestion = SearchSuggestion({
277 STATUS => $mod_suggestion3->{STATUS},
279 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
280 $search_suggestion = SearchSuggestion({
281 STATUS => 'REJECTED',
283 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
285 $search_suggestion = SearchSuggestion({
288 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' );
289 $search_suggestion = SearchSuggestion({
290 budgetid => $budget_id,
292 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' );
293 $search_suggestion = SearchSuggestion({
294 budgetid => '__NONE__',
296 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns the correct number of suggestions' );
297 $search_suggestion = SearchSuggestion({
298 budgetid => '__ANY__',
300 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' );
302 my $del_suggestion = {
303 title => 'my deleted title',
305 suggestedby => $borrowernumber,
307 my $del_suggestionid = NewSuggestion($del_suggestion);
309 is( CountSuggestion('CHECKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
311 is( DelSuggestion(), '0E0', 'DelSuggestion without arguments returns 0E0' );
312 is( DelSuggestion($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
313 is( DelSuggestion(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
314 $suggestion = DelSuggestion($borrowernumber, $my_suggestionid);
315 is( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
317 $suggestions = GetSuggestionByStatus('CHECKED');
318 is( @$suggestions, 1, 'DelSuggestion deletes one suggestion' );
319 is( $suggestions->[0]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' );
321 ## Bug 11466, making sure GetSupportList() returns itemtypes, even if AdvancedSearchTypes has multiple values
322 C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes|loc|ccode');
323 my $itemtypes1 = C4::Koha::GetSupportList();
324 is(@$itemtypes1, 8, "Purchase suggestion itemtypes collected, multiple AdvancedSearchTypes");
326 C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes');
327 my $itemtypes2 = C4::Koha::GetSupportList();
328 is(@$itemtypes2, 8, "Purchase suggestion itemtypes collected, default AdvancedSearchTypes");
330 is_deeply($itemtypes1, $itemtypes2, 'same set of purchase suggestion formats retrieved');