Bug 12627: Fix default values
[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
24 use Koha::DateUtils qw( dt_from_string );
25
26 use Test::More tests => 97;
27 use Test::Warn;
28
29 BEGIN {
30     use_ok('C4::Suggestions');
31     use_ok('C4::Koha');
32 }
33
34 my $dbh = C4::Context->dbh;
35
36 # Start transaction
37 $dbh->{AutoCommit} = 0;
38 $dbh->{RaiseError} = 1;
39
40 $dbh->do(q|DELETE FROM suggestions|);
41 $dbh->do(q|DELETE FROM issues|);
42 $dbh->do(q|DELETE FROM borrowers|);
43 $dbh->do(q|DELETE FROM letter|);
44 $dbh->do(q|DELETE FROM message_queue|);
45 $dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'CHECKED', 'my content')|);
46
47 my $member = {
48     firstname => 'my firstname',
49     surname => 'my surname',
50     categorycode => 'S',
51     branchcode => 'CPL',
52 };
53 my $borrowernumber = AddMember(%$member);
54
55 my $biblionumber1 = 1;
56 my $my_suggestion = {
57     title         => 'my title',
58     author        => 'my author',
59     publishercode => 'my publishercode',
60     suggestedby   => $borrowernumber,
61     biblionumber  => $biblionumber1,
62     managedby     => '',
63     manageddate   => '',
64     accepteddate  => dt_from_string,
65     note          => 'my note',
66 };
67
68
69 is( CountSuggestion(), 0, 'CountSuggestion without the status returns 0' );
70 is( CountSuggestion('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
71 is( CountSuggestion('CHECKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
72 is( CountSuggestion('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
73 is( CountSuggestion('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
74
75 my $my_suggestionid = NewSuggestion($my_suggestion);
76 isnt( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
77
78 is( GetSuggestion(), undef, 'GetSuggestion without the suggestion id returns undef' );
79 my $suggestion = GetSuggestion($my_suggestionid);
80 is( $suggestion->{title}, $my_suggestion->{title}, 'NewSuggestion stores the title correctly' );
81 is( $suggestion->{author}, $my_suggestion->{author}, 'NewSuggestion stores the author correctly' );
82 is( $suggestion->{publishercode}, $my_suggestion->{publishercode}, 'NewSuggestion stores the publishercode correctly' );
83 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'NewSuggestion stores the borrower number correctly' );
84 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'NewSuggestion stores the biblio number correctly' );
85 is( $suggestion->{STATUS}, 'ASKED', 'NewSuggestion stores a suggestion with the status ASKED by default' );
86 is( $suggestion->{managedby}, undef, 'NewSuggestion stores empty string as undef for non existent foreign key (integer)' );
87 is( $suggestion->{manageddate}, undef, 'NewSuggestion stores empty string as undef for date' );
88 is( CountSuggestion('ASKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
89
90
91 is( ModSuggestion(), undef, 'ModSuggestion without the suggestion returns undef' );
92 my $mod_suggestion1 = {
93     title         => 'my modified title',
94     author        => 'my modified author',
95     publishercode => 'my modified publishercode',
96     managedby     => '',
97     manageddate   => '',
98 };
99 my $status = ModSuggestion($mod_suggestion1);
100 is( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
101
102 $mod_suggestion1->{suggestionid} = $my_suggestionid;
103 $status = ModSuggestion($mod_suggestion1);
104 is( $status, 1, 'ModSuggestion modifies one entry' );
105 $suggestion = GetSuggestion($my_suggestionid);
106 is( $suggestion->{title}, $mod_suggestion1->{title}, 'ModSuggestion modifies the title  correctly' );
107 is( $suggestion->{author}, $mod_suggestion1->{author}, 'ModSuggestion modifies the author correctly' );
108 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'ModSuggestion modifies the publishercode correctly' );
109 is( $suggestion->{managedby}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
110 is( $suggestion->{manageddate}, undef, 'ModSuggestion stores empty string as undef for date' );
111 isnt( $suggestion->{accepteddate}, undef, 'ModSuggestion does not update a non given date value' );
112 is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not given' );
113
114 my $messages = C4::Letters::GetQueuedMessages({
115     borrowernumber => $borrowernumber,
116 });
117 is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
118
119 my $mod_suggestion2 = {
120     STATUS       => 'STALLED',
121     suggestionid => $my_suggestionid,
122 };
123 warning_is { $status = ModSuggestion($mod_suggestion2) }
124            "No suggestions STALLED letter transported by email",
125            "ModSuggestion status warning is correct";
126 is( $status, 1, "ModSuggestion Status OK");
127
128 my $mod_suggestion3 = {
129     STATUS       => 'CHECKED',
130     suggestionid => $my_suggestionid,
131 };
132 $status = ModSuggestion($mod_suggestion3);
133
134 is( $status, 1, 'ModSuggestion modifies one entry' );
135 $suggestion = GetSuggestion($my_suggestionid);
136 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' );
137 $messages = C4::Letters::GetQueuedMessages({
138     borrowernumber => $borrowernumber,
139 });
140 is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' );
141
142 is( CountSuggestion('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
143
144
145 is( GetSuggestionInfo(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
146 $suggestion = GetSuggestionInfo($my_suggestionid);
147 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfo returns the suggestion id correctly' );
148 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfo returns the title correctly' );
149 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfo returns the author correctly' );
150 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfo returns the publisher code correctly' );
151 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
152 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfo returns the biblio number correctly' );
153 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfo returns the status correctly' );
154 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfo returns the surname correctly' );
155 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfo returns the firstname correctly' );
156 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
157
158
159 is( GetSuggestionFromBiblionumber(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
160 is( GetSuggestionFromBiblionumber(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
161 is( GetSuggestionFromBiblionumber($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
162
163
164 is( GetSuggestionInfoFromBiblionumber(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
165 is( GetSuggestionInfoFromBiblionumber(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
166 $suggestion = GetSuggestionInfoFromBiblionumber($biblionumber1);
167 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
168 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
169 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
170 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfoFromBiblionumber returns the publisher code correctly' );
171 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumber returns the borrower number correctly' );
172 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfoFromBiblionumber returns the biblio number correctly' );
173 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfoFromBiblionumber returns the status correctly' );
174 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfoFromBiblionumber returns the surname correctly' );
175 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfoFromBiblionumber returns the firstname correctly' );
176 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumeber returns the borrower number correctly' );
177
178
179 my $suggestions = GetSuggestionByStatus();
180 is( @$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
181 $suggestions = GetSuggestionByStatus('CHECKED');
182 is( @$suggestions, 1, 'GetSuggestionByStatus returns the correct number of suggestions' );
183 is( $suggestions->[0]->{suggestionid}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
184 is( $suggestions->[0]->{title}, $mod_suggestion1->{title}, 'GetSuggestionByStatus returns the title correctly' );
185 is( $suggestions->[0]->{author}, $mod_suggestion1->{author}, 'GetSuggestionByStatus returns the author correctly' );
186 is( $suggestions->[0]->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionByStatus returns the publisher code correctly' );
187 is( $suggestions->[0]->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
188 is( $suggestions->[0]->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionByStatus returns the biblio number correctly' );
189 is( $suggestions->[0]->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionByStatus returns the status correctly' );
190 is( $suggestions->[0]->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionByStatus returns the surname correctly' );
191 is( $suggestions->[0]->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionByStatus returns the firstname correctly' );
192 is( $suggestions->[0]->{branchcodesuggestedby}, $member->{branchcode}, 'GetSuggestionByStatus returns the branch code correctly' );
193 is( $suggestions->[0]->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
194 is( $suggestions->[0]->{categorycodesuggestedby}, $member->{categorycode}, 'GetSuggestionByStatus returns the category code correctly' );
195
196
197 is( ConnectSuggestionAndBiblio(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
198 my $biblionumber2 = 2;
199 my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio($my_suggestionid, $biblionumber2);
200 is( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
201 $suggestion = GetSuggestion($my_suggestionid);
202 is( $suggestion->{biblionumber}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
203
204
205 my $search_suggestion = SearchSuggestion();
206 is( @$search_suggestion, 1, 'SearchSuggestion without arguments returns all suggestions' );
207
208 $search_suggestion = SearchSuggestion({
209     title => $mod_suggestion1->{title},
210 });
211 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
212 $search_suggestion = SearchSuggestion({
213     title => 'another title',
214 });
215 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
216
217 $search_suggestion = SearchSuggestion({
218     author => $mod_suggestion1->{author},
219 });
220 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
221 $search_suggestion = SearchSuggestion({
222     author => 'another author',
223 });
224 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
225
226 $search_suggestion = SearchSuggestion({
227     publishercode => $mod_suggestion3->{publishercode},
228 });
229 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
230 $search_suggestion = SearchSuggestion({
231     publishercode => 'another publishercode',
232 });
233 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
234
235 $search_suggestion = SearchSuggestion({
236     STATUS => $mod_suggestion3->{STATUS},
237 });
238 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
239 $search_suggestion = SearchSuggestion({
240     STATUS => 'REJECTED',
241 });
242 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
243
244
245 my $del_suggestion = {
246     title => 'my deleted title',
247     STATUS => 'CHECKED',
248     suggestedby => $borrowernumber,
249 };
250 my $del_suggestionid = NewSuggestion($del_suggestion);
251
252 is( CountSuggestion('CHECKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
253
254 is( DelSuggestion(), '0E0', 'DelSuggestion without arguments returns 0E0' );
255 is( DelSuggestion($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
256 is( DelSuggestion(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
257 $suggestion = DelSuggestion($borrowernumber, $my_suggestionid);
258 is( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
259
260 $suggestions = GetSuggestionByStatus('CHECKED');
261 is( @$suggestions, 1, 'DelSuggestion deletes one suggestion' );
262 is( $suggestions->[0]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' );
263
264 ## Bug 11466, making sure GetSupportList() returns itemtypes, even if AdvancedSearchTypes has multiple values
265 C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes|loc|ccode');
266 my $itemtypes1 = C4::Koha::GetSupportList();
267 is(@$itemtypes1, 8, "Purchase suggestion itemtypes collected, multiple AdvancedSearchTypes");
268
269 C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes');
270 my $itemtypes2 = C4::Koha::GetSupportList();
271 is(@$itemtypes2, 8, "Purchase suggestion itemtypes collected, default AdvancedSearchTypes");
272
273 is_deeply($itemtypes1, $itemtypes2, 'same set of purchase suggestion formats retrieved');
274
275 $dbh->rollback;