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 $dbh->do(q|DELETE FROM suggestions|);
42 $dbh->do(q|DELETE FROM issues|);
43 $dbh->do(q|DELETE FROM borrowers|);
44 $dbh->do(q|DELETE FROM letter|);
45 $dbh->do(q|DELETE FROM message_queue|);
46 $dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'CHECKED', 'my content')|);
49 firstname => 'my firstname',
50 surname => 'my surname',
54 my $borrowernumber = AddMember(%$member);
56 my $biblionumber1 = 1;
59 author => 'my author',
60 publishercode => 'my publishercode',
61 suggestedby => $borrowernumber,
62 biblionumber => $biblionumber1,
65 accepteddate => dt_from_string,
69 my $budgetperiod_id = AddBudgetPeriod({
70 budget_period_startdate => '2008-01-01',
71 budget_period_enddate => '2008-12-31',
72 budget_period_description => 'MAPERI',
73 budget_period_active => 1,
76 my $budget_id = AddBudget({
77 budget_code => 'ABCD',
78 budget_amount => '123.132000',
79 budget_name => 'ABCD',
80 budget_notes => 'This is a note',
81 budget_period_id => $budgetperiod_id,
83 my $my_suggestion_with_budget = {
84 title => 'my title 2',
85 author => 'my author 2',
86 publishercode => 'my publishercode 2',
87 suggestedby => $borrowernumber,
88 biblionumber => $biblionumber1,
91 accepteddate => dt_from_string,
93 budgetid => $budget_id,
97 is( CountSuggestion(), 0, 'CountSuggestion without the status returns 0' );
98 is( CountSuggestion('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
99 is( CountSuggestion('CHECKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
100 is( CountSuggestion('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
101 is( CountSuggestion('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
103 my $my_suggestionid = NewSuggestion($my_suggestion);
104 isnt( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
105 my $my_suggestionid_with_budget = NewSuggestion($my_suggestion_with_budget);
107 is( GetSuggestion(), undef, 'GetSuggestion without the suggestion id returns undef' );
108 my $suggestion = GetSuggestion($my_suggestionid);
109 is( $suggestion->{title}, $my_suggestion->{title}, 'NewSuggestion stores the title correctly' );
110 is( $suggestion->{author}, $my_suggestion->{author}, 'NewSuggestion stores the author correctly' );
111 is( $suggestion->{publishercode}, $my_suggestion->{publishercode}, 'NewSuggestion stores the publishercode correctly' );
112 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'NewSuggestion stores the borrower number correctly' );
113 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'NewSuggestion stores the biblio number correctly' );
114 is( $suggestion->{STATUS}, 'ASKED', 'NewSuggestion stores a suggestion with the status ASKED by default' );
115 is( $suggestion->{managedby}, undef, 'NewSuggestion stores empty string as undef for non existent foreign key (integer)' );
116 is( $suggestion->{manageddate}, undef, 'NewSuggestion stores empty string as undef for date' );
117 is( CountSuggestion('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
120 is( ModSuggestion(), undef, 'ModSuggestion without the suggestion returns undef' );
121 my $mod_suggestion1 = {
122 title => 'my modified title',
123 author => 'my modified author',
124 publishercode => 'my modified publishercode',
128 my $status = ModSuggestion($mod_suggestion1);
129 is( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
131 $mod_suggestion1->{suggestionid} = $my_suggestionid;
132 $status = ModSuggestion($mod_suggestion1);
133 is( $status, 1, 'ModSuggestion modifies one entry' );
134 $suggestion = GetSuggestion($my_suggestionid);
135 is( $suggestion->{title}, $mod_suggestion1->{title}, 'ModSuggestion modifies the title correctly' );
136 is( $suggestion->{author}, $mod_suggestion1->{author}, 'ModSuggestion modifies the author correctly' );
137 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'ModSuggestion modifies the publishercode correctly' );
138 is( $suggestion->{managedby}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
139 is( $suggestion->{manageddate}, undef, 'ModSuggestion stores empty string as undef for date' );
140 isnt( $suggestion->{accepteddate}, undef, 'ModSuggestion does not update a non given date value' );
141 is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not given' );
143 my $messages = C4::Letters::GetQueuedMessages({
144 borrowernumber => $borrowernumber,
146 is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
148 my $mod_suggestion2 = {
150 suggestionid => $my_suggestionid,
152 warning_is { $status = ModSuggestion($mod_suggestion2) }
153 "No suggestions STALLED letter transported by email",
154 "ModSuggestion status warning is correct";
155 is( $status, 1, "ModSuggestion Status OK");
157 my $mod_suggestion3 = {
159 suggestionid => $my_suggestionid,
161 $status = ModSuggestion($mod_suggestion3);
163 is( $status, 1, 'ModSuggestion modifies one entry' );
164 $suggestion = GetSuggestion($my_suggestionid);
165 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' );
166 $messages = C4::Letters::GetQueuedMessages({
167 borrowernumber => $borrowernumber,
169 is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' );
171 is( CountSuggestion('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
174 is( GetSuggestionInfo(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
175 $suggestion = GetSuggestionInfo($my_suggestionid);
176 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfo returns the suggestion id correctly' );
177 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfo returns the title correctly' );
178 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfo returns the author correctly' );
179 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfo returns the publisher code correctly' );
180 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
181 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfo returns the biblio number correctly' );
182 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfo returns the status correctly' );
183 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfo returns the surname correctly' );
184 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfo returns the firstname correctly' );
185 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
188 is( GetSuggestionFromBiblionumber(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
189 is( GetSuggestionFromBiblionumber(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
190 is( GetSuggestionFromBiblionumber($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
193 is( GetSuggestionInfoFromBiblionumber(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
194 is( GetSuggestionInfoFromBiblionumber(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
195 $suggestion = GetSuggestionInfoFromBiblionumber($biblionumber1);
196 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
197 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
198 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
199 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfoFromBiblionumber returns the publisher code correctly' );
200 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumber returns the borrower number correctly' );
201 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfoFromBiblionumber returns the biblio number correctly' );
202 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfoFromBiblionumber returns the status correctly' );
203 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfoFromBiblionumber returns the surname correctly' );
204 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfoFromBiblionumber returns the firstname correctly' );
205 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumeber returns the borrower number correctly' );
208 my $suggestions = GetSuggestionByStatus();
209 is( @$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
210 $suggestions = GetSuggestionByStatus('CHECKED');
211 is( @$suggestions, 1, 'GetSuggestionByStatus returns the correct number of suggestions' );
212 is( $suggestions->[0]->{suggestionid}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
213 is( $suggestions->[0]->{title}, $mod_suggestion1->{title}, 'GetSuggestionByStatus returns the title correctly' );
214 is( $suggestions->[0]->{author}, $mod_suggestion1->{author}, 'GetSuggestionByStatus returns the author correctly' );
215 is( $suggestions->[0]->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionByStatus returns the publisher code correctly' );
216 is( $suggestions->[0]->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
217 is( $suggestions->[0]->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionByStatus returns the biblio number correctly' );
218 is( $suggestions->[0]->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionByStatus returns the status correctly' );
219 is( $suggestions->[0]->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionByStatus returns the surname correctly' );
220 is( $suggestions->[0]->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionByStatus returns the firstname correctly' );
221 is( $suggestions->[0]->{branchcodesuggestedby}, $member->{branchcode}, 'GetSuggestionByStatus returns the branch code correctly' );
222 is( $suggestions->[0]->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
223 is( $suggestions->[0]->{categorycodesuggestedby}, $member->{categorycode}, 'GetSuggestionByStatus returns the category code correctly' );
226 is( ConnectSuggestionAndBiblio(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
227 my $biblionumber2 = 2;
228 my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio($my_suggestionid, $biblionumber2);
229 is( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
230 $suggestion = GetSuggestion($my_suggestionid);
231 is( $suggestion->{biblionumber}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
233 my $search_suggestion = SearchSuggestion();
234 is( @$search_suggestion, 2, 'SearchSuggestion without arguments returns all suggestions' );
236 $search_suggestion = SearchSuggestion({
237 title => $mod_suggestion1->{title},
239 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
240 $search_suggestion = SearchSuggestion({
241 title => 'another title',
243 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
245 $search_suggestion = SearchSuggestion({
246 author => $mod_suggestion1->{author},
248 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
249 $search_suggestion = SearchSuggestion({
250 author => 'another author',
252 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
254 $search_suggestion = SearchSuggestion({
255 publishercode => $mod_suggestion1->{publishercode},
257 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
258 $search_suggestion = SearchSuggestion({
259 publishercode => 'another publishercode',
261 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
263 $search_suggestion = SearchSuggestion({
264 STATUS => $mod_suggestion3->{STATUS},
266 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
267 $search_suggestion = SearchSuggestion({
268 STATUS => 'REJECTED',
270 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
272 $search_suggestion = SearchSuggestion({
275 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' );
276 $search_suggestion = SearchSuggestion({
277 budgetid => $budget_id,
279 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' );
280 $search_suggestion = SearchSuggestion({
281 budgetid => '__NONE__',
283 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns the correct number of suggestions' );
284 $search_suggestion = SearchSuggestion({
285 budgetid => '__ANY__',
287 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' );
289 my $del_suggestion = {
290 title => 'my deleted title',
292 suggestedby => $borrowernumber,
294 my $del_suggestionid = NewSuggestion($del_suggestion);
296 is( CountSuggestion('CHECKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
298 is( DelSuggestion(), '0E0', 'DelSuggestion without arguments returns 0E0' );
299 is( DelSuggestion($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
300 is( DelSuggestion(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
301 $suggestion = DelSuggestion($borrowernumber, $my_suggestionid);
302 is( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
304 $suggestions = GetSuggestionByStatus('CHECKED');
305 is( @$suggestions, 1, 'DelSuggestion deletes one suggestion' );
306 is( $suggestions->[0]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' );
308 ## Bug 11466, making sure GetSupportList() returns itemtypes, even if AdvancedSearchTypes has multiple values
309 C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes|loc|ccode');
310 my $itemtypes1 = C4::Koha::GetSupportList();
311 is(@$itemtypes1, 8, "Purchase suggestion itemtypes collected, multiple AdvancedSearchTypes");
313 C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes');
314 my $itemtypes2 = C4::Koha::GetSupportList();
315 is(@$itemtypes2, 8, "Purchase suggestion itemtypes collected, default AdvancedSearchTypes");
317 is_deeply($itemtypes1, $itemtypes2, 'same set of purchase suggestion formats retrieved');