Bug 13457 - Followup for CPL and S codes
[koha.git] / t / db_dependent / Suggestions.t
bloba2377f9f94e31aaa7b64995478e39643dbcbec08
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use C4::Context;
21 use C4::Members;
22 use C4::Letters;
23 use C4::Branch;
24 use C4::Budgets;
26 use Koha::DateUtils qw( dt_from_string );
28 use Test::More tests => 101;
29 use Test::Warn;
31 BEGIN {
32 use_ok('C4::Suggestions');
33 use_ok('C4::Koha');
36 my $dbh = C4::Context->dbh;
37 my $sql;
39 # Start transaction
40 $dbh->{AutoCommit} = 0;
41 $dbh->{RaiseError} = 1;
43 # Reset item types to only the default ones
44 $dbh->do(q|DELETE FROM itemtypes;|);
45 $sql = "
46 INSERT INTO itemtypes (itemtype, description, rentalcharge, notforloan, imageurl, summary) VALUES
47 ('BK', 'Books',5,0,'bridge/book.gif',''),
48 ('MX', 'Mixed Materials',5,0,'bridge/kit.gif',''),
49 ('CF', 'Computer Files',5,0,'bridge/computer_file.gif',''),
50 ('MP', 'Maps',5,0,'bridge/map.gif',''),
51 ('VM', 'Visual Materials',5,1,'bridge/dvd.gif',''),
52 ('MU', 'Music',5,0,'bridge/sound.gif',''),
53 ('CR', 'Continuing Resources',5,0,'bridge/periodical.gif',''),
54 ('REF', 'Reference',0,1,'bridge/reference.gif','');";
55 $dbh->do($sql);
56 $dbh->do(q|DELETE FROM suggestions|);
57 $dbh->do(q|DELETE FROM issues|);
58 $dbh->do(q|DELETE FROM borrowers|);
59 $dbh->do(q|DELETE FROM letter|);
60 $dbh->do(q|DELETE FROM message_queue|);
61 $dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'CHECKED', 'my content')|);
63 # Add CPL if missing.
64 if (not defined GetBranchDetail('CPL')) {
65 ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
68 my $sth = $dbh->prepare("SELECT * FROM categories WHERE categorycode='S';");
69 $sth->execute();
70 if (!$sth->fetchrow_hashref) {
71 $sql = "INSERT INTO categories
72 (categorycode,description,enrolmentperiod,upperagelimit,
73 dateofbirthrequired,finetype,bulk,enrolmentfee,
74 overduenoticerequired,issuelimit,reservefee,category_type)
75 VALUES
76 ('S','Staff',99,999,
77 18,NULL,NULL,'0.000000',
78 0,NULL,'0.000000','S');";
79 $dbh->do($sql);
82 my $member = {
83 firstname => 'my firstname',
84 surname => 'my surname',
85 categorycode => 'S',
86 branchcode => 'CPL',
88 my $borrowernumber = AddMember(%$member);
90 my $biblionumber1 = 1;
91 my $my_suggestion = {
92 title => 'my title',
93 author => 'my author',
94 publishercode => 'my publishercode',
95 suggestedby => $borrowernumber,
96 biblionumber => $biblionumber1,
97 managedby => '',
98 manageddate => '',
99 accepteddate => dt_from_string,
100 note => 'my note',
103 my $budgetperiod_id = AddBudgetPeriod({
104 budget_period_startdate => '2008-01-01',
105 budget_period_enddate => '2008-12-31',
106 budget_period_description => 'MAPERI',
107 budget_period_active => 1,
110 my $budget_id = AddBudget({
111 budget_code => 'ABCD',
112 budget_amount => '123.132000',
113 budget_name => 'ABCD',
114 budget_notes => 'This is a note',
115 budget_period_id => $budgetperiod_id,
117 my $my_suggestion_with_budget = {
118 title => 'my title 2',
119 author => 'my author 2',
120 publishercode => 'my publishercode 2',
121 suggestedby => $borrowernumber,
122 biblionumber => $biblionumber1,
123 managedby => '',
124 manageddate => '',
125 accepteddate => dt_from_string,
126 note => 'my note',
127 budgetid => $budget_id,
131 is( CountSuggestion(), 0, 'CountSuggestion without the status returns 0' );
132 is( CountSuggestion('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
133 is( CountSuggestion('CHECKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
134 is( CountSuggestion('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
135 is( CountSuggestion('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
137 my $my_suggestionid = NewSuggestion($my_suggestion);
138 isnt( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
139 my $my_suggestionid_with_budget = NewSuggestion($my_suggestion_with_budget);
141 is( GetSuggestion(), undef, 'GetSuggestion without the suggestion id returns undef' );
142 my $suggestion = GetSuggestion($my_suggestionid);
143 is( $suggestion->{title}, $my_suggestion->{title}, 'NewSuggestion stores the title correctly' );
144 is( $suggestion->{author}, $my_suggestion->{author}, 'NewSuggestion stores the author correctly' );
145 is( $suggestion->{publishercode}, $my_suggestion->{publishercode}, 'NewSuggestion stores the publishercode correctly' );
146 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'NewSuggestion stores the borrower number correctly' );
147 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'NewSuggestion stores the biblio number correctly' );
148 is( $suggestion->{STATUS}, 'ASKED', 'NewSuggestion stores a suggestion with the status ASKED by default' );
149 is( $suggestion->{managedby}, undef, 'NewSuggestion stores empty string as undef for non existent foreign key (integer)' );
150 is( $suggestion->{manageddate}, undef, 'NewSuggestion stores empty string as undef for date' );
151 is( CountSuggestion('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
154 is( ModSuggestion(), undef, 'ModSuggestion without the suggestion returns undef' );
155 my $mod_suggestion1 = {
156 title => 'my modified title',
157 author => 'my modified author',
158 publishercode => 'my modified publishercode',
159 managedby => '',
160 manageddate => '',
162 my $status = ModSuggestion($mod_suggestion1);
163 is( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
165 $mod_suggestion1->{suggestionid} = $my_suggestionid;
166 $status = ModSuggestion($mod_suggestion1);
167 is( $status, 1, 'ModSuggestion modifies one entry' );
168 $suggestion = GetSuggestion($my_suggestionid);
169 is( $suggestion->{title}, $mod_suggestion1->{title}, 'ModSuggestion modifies the title correctly' );
170 is( $suggestion->{author}, $mod_suggestion1->{author}, 'ModSuggestion modifies the author correctly' );
171 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'ModSuggestion modifies the publishercode correctly' );
172 is( $suggestion->{managedby}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
173 is( $suggestion->{manageddate}, undef, 'ModSuggestion stores empty string as undef for date' );
174 isnt( $suggestion->{accepteddate}, undef, 'ModSuggestion does not update a non given date value' );
175 is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not given' );
177 my $messages = C4::Letters::GetQueuedMessages({
178 borrowernumber => $borrowernumber,
180 is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
182 my $mod_suggestion2 = {
183 STATUS => 'STALLED',
184 suggestionid => $my_suggestionid,
186 warning_is { $status = ModSuggestion($mod_suggestion2) }
187 "No suggestions STALLED letter transported by email",
188 "ModSuggestion status warning is correct";
189 is( $status, 1, "ModSuggestion Status OK");
191 my $mod_suggestion3 = {
192 STATUS => 'CHECKED',
193 suggestionid => $my_suggestionid,
195 $status = ModSuggestion($mod_suggestion3);
197 is( $status, 1, 'ModSuggestion modifies one entry' );
198 $suggestion = GetSuggestion($my_suggestionid);
199 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' );
200 $messages = C4::Letters::GetQueuedMessages({
201 borrowernumber => $borrowernumber,
203 is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' );
205 is( CountSuggestion('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
208 is( GetSuggestionInfo(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
209 $suggestion = GetSuggestionInfo($my_suggestionid);
210 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfo returns the suggestion id correctly' );
211 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfo returns the title correctly' );
212 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfo returns the author correctly' );
213 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfo returns the publisher code correctly' );
214 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
215 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfo returns the biblio number correctly' );
216 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfo returns the status correctly' );
217 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfo returns the surname correctly' );
218 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfo returns the firstname correctly' );
219 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
222 is( GetSuggestionFromBiblionumber(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
223 is( GetSuggestionFromBiblionumber(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
224 is( GetSuggestionFromBiblionumber($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
227 is( GetSuggestionInfoFromBiblionumber(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
228 is( GetSuggestionInfoFromBiblionumber(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
229 $suggestion = GetSuggestionInfoFromBiblionumber($biblionumber1);
230 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
231 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
232 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
233 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfoFromBiblionumber returns the publisher code correctly' );
234 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumber returns the borrower number correctly' );
235 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfoFromBiblionumber returns the biblio number correctly' );
236 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfoFromBiblionumber returns the status correctly' );
237 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfoFromBiblionumber returns the surname correctly' );
238 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfoFromBiblionumber returns the firstname correctly' );
239 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumeber returns the borrower number correctly' );
242 my $suggestions = GetSuggestionByStatus();
243 is( @$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
244 $suggestions = GetSuggestionByStatus('CHECKED');
245 is( @$suggestions, 1, 'GetSuggestionByStatus returns the correct number of suggestions' );
246 is( $suggestions->[0]->{suggestionid}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
247 is( $suggestions->[0]->{title}, $mod_suggestion1->{title}, 'GetSuggestionByStatus returns the title correctly' );
248 is( $suggestions->[0]->{author}, $mod_suggestion1->{author}, 'GetSuggestionByStatus returns the author correctly' );
249 is( $suggestions->[0]->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionByStatus returns the publisher code correctly' );
250 is( $suggestions->[0]->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
251 is( $suggestions->[0]->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionByStatus returns the biblio number correctly' );
252 is( $suggestions->[0]->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionByStatus returns the status correctly' );
253 is( $suggestions->[0]->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionByStatus returns the surname correctly' );
254 is( $suggestions->[0]->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionByStatus returns the firstname correctly' );
255 is( $suggestions->[0]->{branchcodesuggestedby}, $member->{branchcode}, 'GetSuggestionByStatus returns the branch code correctly' );
256 is( $suggestions->[0]->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
257 is( $suggestions->[0]->{categorycodesuggestedby}, $member->{categorycode}, 'GetSuggestionByStatus returns the category code correctly' );
260 is( ConnectSuggestionAndBiblio(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
261 my $biblionumber2 = 2;
262 my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio($my_suggestionid, $biblionumber2);
263 is( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
264 $suggestion = GetSuggestion($my_suggestionid);
265 is( $suggestion->{biblionumber}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
267 my $search_suggestion = SearchSuggestion();
268 is( @$search_suggestion, 2, 'SearchSuggestion without arguments returns all suggestions' );
270 $search_suggestion = SearchSuggestion({
271 title => $mod_suggestion1->{title},
273 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
274 $search_suggestion = SearchSuggestion({
275 title => 'another title',
277 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
279 $search_suggestion = SearchSuggestion({
280 author => $mod_suggestion1->{author},
282 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
283 $search_suggestion = SearchSuggestion({
284 author => 'another author',
286 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
288 $search_suggestion = SearchSuggestion({
289 publishercode => $mod_suggestion1->{publishercode},
291 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
292 $search_suggestion = SearchSuggestion({
293 publishercode => 'another publishercode',
295 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
297 $search_suggestion = SearchSuggestion({
298 STATUS => $mod_suggestion3->{STATUS},
300 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
301 $search_suggestion = SearchSuggestion({
302 STATUS => 'REJECTED',
304 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
306 $search_suggestion = SearchSuggestion({
307 budgetid => '',
309 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' );
310 $search_suggestion = SearchSuggestion({
311 budgetid => $budget_id,
313 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' );
314 $search_suggestion = SearchSuggestion({
315 budgetid => '__NONE__',
317 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns the correct number of suggestions' );
318 $search_suggestion = SearchSuggestion({
319 budgetid => '__ANY__',
321 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' );
323 my $del_suggestion = {
324 title => 'my deleted title',
325 STATUS => 'CHECKED',
326 suggestedby => $borrowernumber,
328 my $del_suggestionid = NewSuggestion($del_suggestion);
330 is( CountSuggestion('CHECKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
332 is( DelSuggestion(), '0E0', 'DelSuggestion without arguments returns 0E0' );
333 is( DelSuggestion($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
334 is( DelSuggestion(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
335 $suggestion = DelSuggestion($borrowernumber, $my_suggestionid);
336 is( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
338 $suggestions = GetSuggestionByStatus('CHECKED');
339 is( @$suggestions, 1, 'DelSuggestion deletes one suggestion' );
340 is( $suggestions->[0]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' );
342 ## Bug 11466, making sure GetSupportList() returns itemtypes, even if AdvancedSearchTypes has multiple values
343 C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes|loc|ccode');
344 my $itemtypes1 = C4::Koha::GetSupportList();
345 is(@$itemtypes1, 8, "Purchase suggestion itemtypes collected, multiple AdvancedSearchTypes");
347 C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes');
348 my $itemtypes2 = C4::Koha::GetSupportList();
349 is(@$itemtypes2, 8, "Purchase suggestion itemtypes collected, default AdvancedSearchTypes");
351 is_deeply($itemtypes1, $itemtypes2, 'same set of purchase suggestion formats retrieved');
353 $dbh->rollback;
355 done_testing;