Bug 18292: Remove return 1 statements in tests
[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 t::lib::Mocks;
21 use C4::Context;
22 use C4::Members;
23 use C4::Letters;
24 use C4::Budgets qw( AddBudgetPeriod AddBudget );
25
26 use Koha::DateUtils qw( dt_from_string );
27 use Koha::Library;
28 use Koha::Libraries;
29
30 use t::lib::TestBuilder;
31
32 use DateTime::Duration;
33 use Test::More tests => 102;
34 use Test::Warn;
35
36 BEGIN {
37     use_ok('C4::Suggestions');
38 }
39
40 my $dbh = C4::Context->dbh;
41 my $sql;
42
43 # Start transaction
44 $dbh->{AutoCommit} = 0;
45 $dbh->{RaiseError} = 1;
46
47 my $builder = t::lib::TestBuilder->new;
48 # Reset item types to only the default ones
49 $dbh->do(q|DELETE FROM itemtypes;|);
50 $sql = "
51 INSERT INTO itemtypes (itemtype, description, rentalcharge, notforloan, imageurl, summary) VALUES
52 ('BK', 'Books',5,0,'bridge/book.gif',''),
53 ('MX', 'Mixed Materials',5,0,'bridge/kit.gif',''),
54 ('CF', 'Computer Files',5,0,'bridge/computer_file.gif',''),
55 ('MP', 'Maps',5,0,'bridge/map.gif',''),
56 ('VM', 'Visual Materials',5,1,'bridge/dvd.gif',''),
57 ('MU', 'Music',5,0,'bridge/sound.gif',''),
58 ('CR', 'Continuing Resources',5,0,'bridge/periodical.gif',''),
59 ('REF', 'Reference',0,1,'bridge/reference.gif','');";
60 $dbh->do($sql);
61 $dbh->do(q|DELETE FROM suggestions|);
62 $dbh->do(q|DELETE FROM issues|);
63 $dbh->do(q|DELETE FROM borrowers|);
64 $dbh->do(q|DELETE FROM letter|);
65 $dbh->do(q|DELETE FROM message_queue|);
66 $dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'CHECKED', 'my content')|);
67
68 # Add CPL if missing.
69 if (not defined Koha::Libraries->find('CPL')) {
70     Koha::Library->new({ branchcode => 'CPL', branchname => 'Centerville' })->store;
71 }
72
73 my $patron_category = $builder->build({ source => 'Category' });
74
75 my $member = {
76     firstname => 'my firstname',
77     surname => 'my surname',
78     categorycode => $patron_category->{categorycode},
79     branchcode => 'CPL',
80 };
81 my $borrowernumber = AddMember(%$member);
82
83 my $biblionumber1 = 1;
84 my $my_suggestion = {
85     title         => 'my title',
86     author        => 'my author',
87     publishercode => 'my publishercode',
88     suggestedby   => $borrowernumber,
89     biblionumber  => $biblionumber1,
90     managedby     => '',
91     manageddate   => '',
92     accepteddate  => dt_from_string,
93     note          => 'my note',
94 };
95
96 my $budgetperiod_id = AddBudgetPeriod({
97     budget_period_startdate   => '2008-01-01',
98     budget_period_enddate     => '2008-12-31',
99     budget_period_description => 'MAPERI',
100     budget_period_active      => 1,
101 });
102
103 my $budget_id = AddBudget({
104     budget_code      => 'ABCD',
105     budget_amount    => '123.132000',
106     budget_name      => 'ABCD',
107     budget_notes     => 'This is a note',
108     budget_period_id => $budgetperiod_id,
109 });
110 my $my_suggestion_with_budget = {
111     title         => 'my title 2',
112     author        => 'my author 2',
113     publishercode => 'my publishercode 2',
114     suggestedby   => $borrowernumber,
115     biblionumber  => $biblionumber1,
116     managedby     => '',
117     manageddate   => '',
118     accepteddate  => dt_from_string,
119     note          => 'my note',
120     budgetid      => $budget_id,
121 };
122
123
124 is( CountSuggestion(), 0, 'CountSuggestion without the status returns 0' );
125 is( CountSuggestion('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
126 is( CountSuggestion('CHECKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
127 is( CountSuggestion('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
128 is( CountSuggestion('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
129
130 my $my_suggestionid = NewSuggestion($my_suggestion);
131 isnt( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
132 my $my_suggestionid_with_budget = NewSuggestion($my_suggestion_with_budget);
133
134 is( GetSuggestion(), undef, 'GetSuggestion without the suggestion id returns undef' );
135 my $suggestion = GetSuggestion($my_suggestionid);
136 is( $suggestion->{title}, $my_suggestion->{title}, 'NewSuggestion stores the title correctly' );
137 is( $suggestion->{author}, $my_suggestion->{author}, 'NewSuggestion stores the author correctly' );
138 is( $suggestion->{publishercode}, $my_suggestion->{publishercode}, 'NewSuggestion stores the publishercode correctly' );
139 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'NewSuggestion stores the borrower number correctly' );
140 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'NewSuggestion stores the biblio number correctly' );
141 is( $suggestion->{STATUS}, 'ASKED', 'NewSuggestion stores a suggestion with the status ASKED by default' );
142 is( $suggestion->{managedby}, undef, 'NewSuggestion stores empty string as undef for non existent foreign key (integer)' );
143 is( $suggestion->{manageddate}, undef, 'NewSuggestion stores empty string as undef for date' );
144 is( $suggestion->{budgetid}, undef, 'NewSuggestion should set budgetid to NULL if not given' );
145
146 is( CountSuggestion('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
147
148
149 is( ModSuggestion(), undef, 'ModSuggestion without the suggestion returns undef' );
150 my $mod_suggestion1 = {
151     title         => 'my modified title',
152     author        => 'my modified author',
153     publishercode => 'my modified publishercode',
154     managedby     => '',
155     manageddate   => '',
156 };
157 my $status = ModSuggestion($mod_suggestion1);
158 is( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
159
160 $mod_suggestion1->{suggestionid} = $my_suggestionid;
161 $status = ModSuggestion($mod_suggestion1);
162 is( $status, 1, 'ModSuggestion modifies one entry' );
163 $suggestion = GetSuggestion($my_suggestionid);
164 is( $suggestion->{title}, $mod_suggestion1->{title}, 'ModSuggestion modifies the title  correctly' );
165 is( $suggestion->{author}, $mod_suggestion1->{author}, 'ModSuggestion modifies the author correctly' );
166 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'ModSuggestion modifies the publishercode correctly' );
167 is( $suggestion->{managedby}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
168 is( $suggestion->{manageddate}, undef, 'ModSuggestion stores empty string as undef for date' );
169 isnt( $suggestion->{accepteddate}, undef, 'ModSuggestion does not update a non given date value' );
170 is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not given' );
171
172 my $messages = C4::Letters::GetQueuedMessages({
173     borrowernumber => $borrowernumber,
174 });
175 is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
176
177 my $mod_suggestion2 = {
178     STATUS       => 'STALLED',
179     suggestionid => $my_suggestionid,
180 };
181 warning_is { $status = ModSuggestion($mod_suggestion2) }
182            "No suggestions STALLED letter transported by email",
183            "ModSuggestion status warning is correct";
184 is( $status, 1, "ModSuggestion Status OK");
185
186 my $mod_suggestion3 = {
187     STATUS       => 'CHECKED',
188     suggestionid => $my_suggestionid,
189 };
190 $status = ModSuggestion($mod_suggestion3);
191
192 is( $status, 1, 'ModSuggestion modifies one entry' );
193 $suggestion = GetSuggestion($my_suggestionid);
194 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' );
195 $messages = C4::Letters::GetQueuedMessages({
196     borrowernumber => $borrowernumber,
197 });
198 is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' );
199
200 is( CountSuggestion('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
201
202
203 is( GetSuggestionInfo(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
204 $suggestion = GetSuggestionInfo($my_suggestionid);
205 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfo returns the suggestion id correctly' );
206 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfo returns the title correctly' );
207 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfo returns the author correctly' );
208 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfo returns the publisher code correctly' );
209 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
210 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfo returns the biblio number correctly' );
211 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfo returns the status correctly' );
212 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfo returns the surname correctly' );
213 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfo returns the firstname correctly' );
214 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
215
216
217 is( GetSuggestionFromBiblionumber(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
218 is( GetSuggestionFromBiblionumber(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
219 is( GetSuggestionFromBiblionumber($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
220
221
222 is( GetSuggestionInfoFromBiblionumber(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
223 is( GetSuggestionInfoFromBiblionumber(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
224 $suggestion = GetSuggestionInfoFromBiblionumber($biblionumber1);
225 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
226 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
227 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
228 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfoFromBiblionumber returns the publisher code correctly' );
229 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumber returns the borrower number correctly' );
230 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfoFromBiblionumber returns the biblio number correctly' );
231 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfoFromBiblionumber returns the status correctly' );
232 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfoFromBiblionumber returns the surname correctly' );
233 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfoFromBiblionumber returns the firstname correctly' );
234 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumeber returns the borrower number correctly' );
235
236
237 my $suggestions = GetSuggestionByStatus();
238 is( @$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
239 $suggestions = GetSuggestionByStatus('CHECKED');
240 is( @$suggestions, 1, 'GetSuggestionByStatus returns the correct number of suggestions' );
241 is( $suggestions->[0]->{suggestionid}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
242 is( $suggestions->[0]->{title}, $mod_suggestion1->{title}, 'GetSuggestionByStatus returns the title correctly' );
243 is( $suggestions->[0]->{author}, $mod_suggestion1->{author}, 'GetSuggestionByStatus returns the author correctly' );
244 is( $suggestions->[0]->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionByStatus returns the publisher code correctly' );
245 is( $suggestions->[0]->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
246 is( $suggestions->[0]->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionByStatus returns the biblio number correctly' );
247 is( $suggestions->[0]->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionByStatus returns the status correctly' );
248 is( $suggestions->[0]->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionByStatus returns the surname correctly' );
249 is( $suggestions->[0]->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionByStatus returns the firstname correctly' );
250 is( $suggestions->[0]->{branchcodesuggestedby}, $member->{branchcode}, 'GetSuggestionByStatus returns the branch code correctly' );
251 is( $suggestions->[0]->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
252 is( $suggestions->[0]->{categorycodesuggestedby}, $member->{categorycode}, 'GetSuggestionByStatus returns the category code correctly' );
253
254
255 is( ConnectSuggestionAndBiblio(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
256 my $biblionumber2 = 2;
257 my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio($my_suggestionid, $biblionumber2);
258 is( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
259 $suggestion = GetSuggestion($my_suggestionid);
260 is( $suggestion->{biblionumber}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
261
262 my $search_suggestion = SearchSuggestion();
263 is( @$search_suggestion, 2, 'SearchSuggestion without arguments returns all suggestions' );
264
265 $search_suggestion = SearchSuggestion({
266     title => $mod_suggestion1->{title},
267 });
268 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
269 $search_suggestion = SearchSuggestion({
270     title => 'another title',
271 });
272 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
273
274 $search_suggestion = SearchSuggestion({
275     author => $mod_suggestion1->{author},
276 });
277 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
278 $search_suggestion = SearchSuggestion({
279     author => 'another author',
280 });
281 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
282
283 $search_suggestion = SearchSuggestion({
284     publishercode => $mod_suggestion1->{publishercode},
285 });
286 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
287 $search_suggestion = SearchSuggestion({
288     publishercode => 'another publishercode',
289 });
290 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
291
292 $search_suggestion = SearchSuggestion({
293     STATUS => $mod_suggestion3->{STATUS},
294 });
295 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
296
297 $search_suggestion = SearchSuggestion({
298     STATUS => q||
299 });
300 is( @$search_suggestion, 0, 'SearchSuggestion should not return all suggestions if we want the suggestions with a STATUS=""' );
301 $search_suggestion = SearchSuggestion({
302     STATUS => 'REJECTED',
303 });
304 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
305
306 $search_suggestion = SearchSuggestion({
307     budgetid => '',
308 });
309 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' );
310 $search_suggestion = SearchSuggestion({
311     budgetid => $budget_id,
312 });
313 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' );
314 $search_suggestion = SearchSuggestion({
315     budgetid => '__NONE__',
316 });
317 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns the correct number of suggestions' );
318 $search_suggestion = SearchSuggestion({
319     budgetid => '__ANY__',
320 });
321 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' );
322
323 my $del_suggestion = {
324     title => 'my deleted title',
325     STATUS => 'CHECKED',
326     suggestedby => $borrowernumber,
327 };
328 my $del_suggestionid = NewSuggestion($del_suggestion);
329
330 is( CountSuggestion('CHECKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
331
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' );
337
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' );
341
342 # Test budgetid fk
343 $my_suggestion->{budgetid} = ''; # If budgetid == '', NULL should be set in DB
344 my $my_suggestionid_test_budgetid = NewSuggestion($my_suggestion);
345 $suggestion = GetSuggestion($my_suggestionid_test_budgetid);
346 is( $suggestion->{budgetid}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
347
348 $my_suggestion->{budgetid} = ''; # If budgetid == '', NULL should be set in DB
349 ModSuggestion( $my_suggestion );
350 $suggestion = GetSuggestion($my_suggestionid_test_budgetid);
351 is( $suggestion->{budgetid}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
352
353 subtest 'GetUnprocessedSuggestions' => sub {
354     plan tests => 11;
355     $dbh->do(q|DELETE FROM suggestions|);
356     my $my_suggestionid         = NewSuggestion($my_suggestion);
357     my $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
358     is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return 0 if a suggestion has been processed but not linked to a fund' );
359     my $status     = ModSuggestion($mod_suggestion1);
360     my $suggestion = GetSuggestion($my_suggestionid);
361     is( $suggestion->{budgetid}, undef, 'ModSuggestion should set budgetid to NULL if not given' );
362     ModSuggestion( { suggestionid => $my_suggestionid, budgetid => $budget_id } );
363     $suggestion = GetSuggestion($my_suggestionid);
364     is( $suggestion->{budgetid}, $budget_id, 'ModSuggestion should modify budgetid if given' );
365
366     $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
367     is( scalar(@$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
368
369     warning_is { ModSuggestion( { suggestionid => $my_suggestionid, STATUS => 'REJECTED' } ) }
370                 'No suggestions REJECTED letter transported by email',
371                 'Warning raised if no REJECTED letter by email';
372     $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
373     is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
374
375     warning_is { ModSuggestion( { suggestionid => $my_suggestionid, STATUS => 'ASKED', suggesteddate => dt_from_string->add_duration( DateTime::Duration->new( days => -4 ) ) } ); }
376                 'No suggestions ASKED letter transported by email',
377                 'Warning raised if no ASKED letter by email';
378     $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
379     is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should use 0 as default value for days' );
380     $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(4);
381     is( scalar(@$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion suggested 4 days ago' );
382     $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(3);
383     is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 3 days ago' );
384     $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(5);
385     is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 5 days ago' );
386 };