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