Bug 13007: Add a foreign key for suggestions.budgetid
[koha.git] / t / db_dependent / Suggestions.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use C4::Context;
21 use C4::Members;
22 use C4::Letters;
23 use C4::Branch;
24 use C4::Budgets;
25
26 use Koha::DateUtils qw( dt_from_string );
27
28 use Test::More tests => 102;
29 use Test::Warn;
30
31 BEGIN {
32     use_ok('C4::Suggestions');
33     use_ok('C4::Koha');
34 }
35
36 my $dbh = C4::Context->dbh;
37 my $sql;
38
39 # Start transaction
40 $dbh->{AutoCommit} = 0;
41 $dbh->{RaiseError} = 1;
42
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')|);
62
63 # Add CPL if missing.
64 if (not defined GetBranchDetail('CPL')) {
65     ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
66 }
67
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);
80 }
81
82 my $member = {
83     firstname => 'my firstname',
84     surname => 'my surname',
85     categorycode => 'S',
86     branchcode => 'CPL',
87 };
88 my $borrowernumber = AddMember(%$member);
89
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',
101 };
102
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,
108 });
109
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,
116 });
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,
128 };
129
130
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' );
136
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);
140
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( $suggestion->{budgetid}, undef, 'NewSuggestion should set budgetid to NULL if not given' );
152
153 is( CountSuggestion('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
154
155
156 is( ModSuggestion(), undef, 'ModSuggestion without the suggestion returns undef' );
157 my $mod_suggestion1 = {
158     title         => 'my modified title',
159     author        => 'my modified author',
160     publishercode => 'my modified publishercode',
161     managedby     => '',
162     manageddate   => '',
163 };
164 my $status = ModSuggestion($mod_suggestion1);
165 is( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
166
167 $mod_suggestion1->{suggestionid} = $my_suggestionid;
168 $status = ModSuggestion($mod_suggestion1);
169 is( $status, 1, 'ModSuggestion modifies one entry' );
170 $suggestion = GetSuggestion($my_suggestionid);
171 is( $suggestion->{title}, $mod_suggestion1->{title}, 'ModSuggestion modifies the title  correctly' );
172 is( $suggestion->{author}, $mod_suggestion1->{author}, 'ModSuggestion modifies the author correctly' );
173 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'ModSuggestion modifies the publishercode correctly' );
174 is( $suggestion->{managedby}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
175 is( $suggestion->{manageddate}, undef, 'ModSuggestion stores empty string as undef for date' );
176 isnt( $suggestion->{accepteddate}, undef, 'ModSuggestion does not update a non given date value' );
177 is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not given' );
178
179 my $messages = C4::Letters::GetQueuedMessages({
180     borrowernumber => $borrowernumber,
181 });
182 is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
183
184 my $mod_suggestion2 = {
185     STATUS       => 'STALLED',
186     suggestionid => $my_suggestionid,
187 };
188 warning_is { $status = ModSuggestion($mod_suggestion2) }
189            "No suggestions STALLED letter transported by email",
190            "ModSuggestion status warning is correct";
191 is( $status, 1, "ModSuggestion Status OK");
192
193 my $mod_suggestion3 = {
194     STATUS       => 'CHECKED',
195     suggestionid => $my_suggestionid,
196 };
197 $status = ModSuggestion($mod_suggestion3);
198
199 is( $status, 1, 'ModSuggestion modifies one entry' );
200 $suggestion = GetSuggestion($my_suggestionid);
201 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' );
202 $messages = C4::Letters::GetQueuedMessages({
203     borrowernumber => $borrowernumber,
204 });
205 is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' );
206
207 is( CountSuggestion('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
208
209
210 is( GetSuggestionInfo(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
211 $suggestion = GetSuggestionInfo($my_suggestionid);
212 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfo returns the suggestion id correctly' );
213 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfo returns the title correctly' );
214 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfo returns the author correctly' );
215 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfo returns the publisher code correctly' );
216 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
217 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfo returns the biblio number correctly' );
218 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfo returns the status correctly' );
219 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfo returns the surname correctly' );
220 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfo returns the firstname correctly' );
221 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
222
223
224 is( GetSuggestionFromBiblionumber(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
225 is( GetSuggestionFromBiblionumber(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
226 is( GetSuggestionFromBiblionumber($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
227
228
229 is( GetSuggestionInfoFromBiblionumber(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
230 is( GetSuggestionInfoFromBiblionumber(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
231 $suggestion = GetSuggestionInfoFromBiblionumber($biblionumber1);
232 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
233 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
234 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
235 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfoFromBiblionumber returns the publisher code correctly' );
236 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumber returns the borrower number correctly' );
237 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfoFromBiblionumber returns the biblio number correctly' );
238 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfoFromBiblionumber returns the status correctly' );
239 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfoFromBiblionumber returns the surname correctly' );
240 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfoFromBiblionumber returns the firstname correctly' );
241 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumeber returns the borrower number correctly' );
242
243
244 my $suggestions = GetSuggestionByStatus();
245 is( @$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
246 $suggestions = GetSuggestionByStatus('CHECKED');
247 is( @$suggestions, 1, 'GetSuggestionByStatus returns the correct number of suggestions' );
248 is( $suggestions->[0]->{suggestionid}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
249 is( $suggestions->[0]->{title}, $mod_suggestion1->{title}, 'GetSuggestionByStatus returns the title correctly' );
250 is( $suggestions->[0]->{author}, $mod_suggestion1->{author}, 'GetSuggestionByStatus returns the author correctly' );
251 is( $suggestions->[0]->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionByStatus returns the publisher code correctly' );
252 is( $suggestions->[0]->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
253 is( $suggestions->[0]->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionByStatus returns the biblio number correctly' );
254 is( $suggestions->[0]->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionByStatus returns the status correctly' );
255 is( $suggestions->[0]->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionByStatus returns the surname correctly' );
256 is( $suggestions->[0]->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionByStatus returns the firstname correctly' );
257 is( $suggestions->[0]->{branchcodesuggestedby}, $member->{branchcode}, 'GetSuggestionByStatus returns the branch code correctly' );
258 is( $suggestions->[0]->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
259 is( $suggestions->[0]->{categorycodesuggestedby}, $member->{categorycode}, 'GetSuggestionByStatus returns the category code correctly' );
260
261
262 is( ConnectSuggestionAndBiblio(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
263 my $biblionumber2 = 2;
264 my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio($my_suggestionid, $biblionumber2);
265 is( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
266 $suggestion = GetSuggestion($my_suggestionid);
267 is( $suggestion->{biblionumber}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
268
269 my $search_suggestion = SearchSuggestion();
270 is( @$search_suggestion, 2, 'SearchSuggestion without arguments returns all suggestions' );
271
272 $search_suggestion = SearchSuggestion({
273     title => $mod_suggestion1->{title},
274 });
275 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
276 $search_suggestion = SearchSuggestion({
277     title => 'another title',
278 });
279 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
280
281 $search_suggestion = SearchSuggestion({
282     author => $mod_suggestion1->{author},
283 });
284 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
285 $search_suggestion = SearchSuggestion({
286     author => 'another author',
287 });
288 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
289
290 $search_suggestion = SearchSuggestion({
291     publishercode => $mod_suggestion1->{publishercode},
292 });
293 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
294 $search_suggestion = SearchSuggestion({
295     publishercode => 'another publishercode',
296 });
297 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
298
299 $search_suggestion = SearchSuggestion({
300     STATUS => $mod_suggestion3->{STATUS},
301 });
302 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
303 $search_suggestion = SearchSuggestion({
304     STATUS => 'REJECTED',
305 });
306 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
307
308 $search_suggestion = SearchSuggestion({
309     budgetid => '',
310 });
311 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' );
312 $search_suggestion = SearchSuggestion({
313     budgetid => $budget_id,
314 });
315 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' );
316 $search_suggestion = SearchSuggestion({
317     budgetid => '__NONE__',
318 });
319 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns the correct number of suggestions' );
320 $search_suggestion = SearchSuggestion({
321     budgetid => '__ANY__',
322 });
323 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' );
324
325 my $del_suggestion = {
326     title => 'my deleted title',
327     STATUS => 'CHECKED',
328     suggestedby => $borrowernumber,
329 };
330 my $del_suggestionid = NewSuggestion($del_suggestion);
331
332 is( CountSuggestion('CHECKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
333
334 is( DelSuggestion(), '0E0', 'DelSuggestion without arguments returns 0E0' );
335 is( DelSuggestion($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
336 is( DelSuggestion(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
337 $suggestion = DelSuggestion($borrowernumber, $my_suggestionid);
338 is( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
339
340 $suggestions = GetSuggestionByStatus('CHECKED');
341 is( @$suggestions, 1, 'DelSuggestion deletes one suggestion' );
342 is( $suggestions->[0]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' );
343
344 ## Bug 11466, making sure GetSupportList() returns itemtypes, even if AdvancedSearchTypes has multiple values
345 C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes|loc|ccode');
346 my $itemtypes1 = C4::Koha::GetSupportList();
347 is(@$itemtypes1, 8, "Purchase suggestion itemtypes collected, multiple AdvancedSearchTypes");
348
349 C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes');
350 my $itemtypes2 = C4::Koha::GetSupportList();
351 is(@$itemtypes2, 8, "Purchase suggestion itemtypes collected, default AdvancedSearchTypes");
352
353 is_deeply($itemtypes1, $itemtypes2, 'same set of purchase suggestion formats retrieved');
354
355 $dbh->rollback;
356
357 done_testing;