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>.
20 use DateTime
::Duration
;
21 use Test
::More tests
=> 114;
25 use t
::lib
::TestBuilder
;
29 use C4
::Budgets
qw( AddBudgetPeriod AddBudget GetBudget );
31 use Koha
::DateUtils
qw( dt_from_string output_pref );
34 use Koha
::Suggestions
;
37 use_ok
('C4::Suggestions');
40 my $schema = Koha
::Database
->new->schema;
41 $schema->storage->txn_begin;
42 my $dbh = C4
::Context
->dbh;
43 my $builder = t
::lib
::TestBuilder
->new;
45 t
::lib
::Mocks
::mock_preference
( "EmailPurchaseSuggestions", "0");
47 # Reset item types to only the default ones
48 $dbh->do(q
|DELETE FROM itemtypes
;|);
50 INSERT INTO itemtypes
(itemtype
, description
, rentalcharge
, notforloan
, imageurl
, summary
) VALUES
51 ('BK', 'Books',5,0,'bridge/book.gif',''),
52 ('MX', 'Mixed Materials',5,0,'bridge/kit.gif',''),
53 ('CF', 'Computer Files',5,0,'bridge/computer_file.gif',''),
54 ('MP', 'Maps',5,0,'bridge/map.gif',''),
55 ('VM', 'Visual Materials',5,1,'bridge/dvd.gif',''),
56 ('MU', 'Music',5,0,'bridge/sound.gif',''),
57 ('CR', 'Continuing Resources',5,0,'bridge/periodical.gif',''),
58 ('REF', 'Reference',0,1,'bridge/reference.gif','');|;
60 $dbh->do(q
|DELETE FROM suggestions
|);
61 $dbh->do(q
|DELETE FROM issues
|);
62 $dbh->do(q
|DELETE FROM borrowers
|);
63 $dbh->do(q
|DELETE FROM letter
|);
64 $dbh->do(q
|DELETE FROM message_queue
|);
65 $dbh->do(q
|INSERT INTO letter
(module
, code
, content
) VALUES
('suggestions', 'CHECKED', 'my content')|);
66 $dbh->do(q
|INSERT INTO letter
(module
, code
, content
) VALUES
('suggestions', 'NEW_SUGGESTION', 'Content for new suggestion')|);
69 if (not defined Koha
::Libraries
->find('CPL')) {
70 Koha
::Library
->new({ branchcode
=> 'CPL', branchname
=> 'Centerville' })->store;
73 my $patron_category = $builder->build({ source
=> 'Category' });
76 firstname
=> 'my firstname',
77 surname
=> 'my surname',
78 categorycode
=> $patron_category->{categorycode
},
80 smsalertnumber
=> 12345,
84 firstname
=> 'my firstname',
85 surname
=> 'my surname',
86 categorycode
=> $patron_category->{categorycode
},
88 email
=> 'to@example.com',
91 my $borrowernumber = Koha
::Patron
->new($member)->store->borrowernumber;
92 my $borrowernumber2 = Koha
::Patron
->new($member2)->store->borrowernumber;
94 my $biblio_1 = $builder->build_object({ class => 'Koha::Biblios' });
97 author
=> 'my author',
98 publishercode
=> 'my publishercode',
99 suggestedby
=> $borrowernumber,
100 biblionumber
=> $biblio_1->biblionumber,
104 accepteddate
=> dt_from_string
,
106 quantity
=> '', # Insert an empty string into int to catch strict SQL modes errors
109 my $budgetperiod_id = AddBudgetPeriod
({
110 budget_period_startdate
=> '2008-01-01',
111 budget_period_enddate
=> '2008-12-31',
112 budget_period_description
=> 'MAPERI',
113 budget_period_active
=> 1,
116 my $budget_id = AddBudget
({
117 budget_code
=> 'ABCD',
118 budget_amount
=> '123.132000',
119 budget_name
=> 'ABCD',
120 budget_notes
=> 'This is a note',
121 budget_period_id
=> $budgetperiod_id,
123 my $my_suggestion_with_budget = {
124 title
=> 'my title 2',
125 author
=> 'my author 2',
126 publishercode
=> 'my publishercode 2',
127 suggestedby
=> $borrowernumber,
128 branchcode
=> '', # This should not fail be set to undef instead
129 biblionumber
=> $biblio_1->biblionumber,
132 accepteddate
=> dt_from_string
,
134 budgetid
=> $budget_id,
136 my $my_suggestion_with_budget2 = {
137 title
=> 'my title 3',
138 author
=> 'my author 3',
139 publishercode
=> 'my publishercode 3',
140 suggestedby
=> $borrowernumber2,
141 biblionumber
=> $biblio_1->biblionumber,
144 accepteddate
=> dt_from_string
,
146 budgetid
=> $budget_id,
148 my $my_suggestion_without_suggestedby = {
150 author
=> 'my author',
151 publishercode
=> 'my publishercode',
152 suggestedby
=> undef,
153 biblionumber
=> $biblio_1->biblionumber,
157 accepteddate
=> dt_from_string
,
159 quantity
=> '', # Insert an empty string into int to catch strict SQL modes errors
162 is
( CountSuggestion
(), 0, 'CountSuggestion without the status returns 0' );
163 is
( CountSuggestion
('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
164 is
( CountSuggestion
('CHECKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
165 is
( CountSuggestion
('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
166 is
( CountSuggestion
('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
168 my $my_suggestionid = NewSuggestion
($my_suggestion);
169 isnt
( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
170 my $my_suggestionid_with_budget = NewSuggestion
($my_suggestion_with_budget);
172 is
( GetSuggestion
(), undef, 'GetSuggestion without the suggestion id returns undef' );
173 my $suggestion = GetSuggestion
($my_suggestionid);
174 is
( $suggestion->{title
}, $my_suggestion->{title
}, 'NewSuggestion stores the title correctly' );
175 is
( $suggestion->{author
}, $my_suggestion->{author
}, 'NewSuggestion stores the author correctly' );
176 is
( $suggestion->{publishercode
}, $my_suggestion->{publishercode
}, 'NewSuggestion stores the publishercode correctly' );
177 is
( $suggestion->{suggestedby
}, $my_suggestion->{suggestedby
}, 'NewSuggestion stores the borrower number correctly' );
178 is
( $suggestion->{biblionumber
}, $my_suggestion->{biblionumber
}, 'NewSuggestion stores the biblio number correctly' );
179 is
( $suggestion->{STATUS
}, 'ASKED', 'NewSuggestion stores a suggestion with the status ASKED by default' );
180 is
( $suggestion->{managedby
}, undef, 'NewSuggestion stores empty string as undef for non existent foreign key (integer)' );
181 is
( $suggestion->{manageddate
}, undef, 'NewSuggestion stores empty string as undef for date' );
182 is
( $suggestion->{budgetid
}, undef, 'NewSuggestion should set budgetid to NULL if not given' );
184 is
( CountSuggestion
('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
187 is
( ModSuggestion
(), undef, 'ModSuggestion without the suggestion returns undef' );
188 my $mod_suggestion1 = {
189 title
=> 'my modified title',
190 author
=> 'my modified author',
191 publishercode
=> 'my modified publishercode',
195 my $status = ModSuggestion
($mod_suggestion1);
196 is
( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
198 $mod_suggestion1->{suggestionid
} = $my_suggestionid;
199 $status = ModSuggestion
($mod_suggestion1);
200 is
( $status, 1, 'ModSuggestion modifies one entry' );
201 $suggestion = GetSuggestion
($my_suggestionid);
202 is
( $suggestion->{title
}, $mod_suggestion1->{title
}, 'ModSuggestion modifies the title correctly' );
203 is
( $suggestion->{author
}, $mod_suggestion1->{author
}, 'ModSuggestion modifies the author correctly' );
204 is
( $suggestion->{publishercode
}, $mod_suggestion1->{publishercode
}, 'ModSuggestion modifies the publishercode correctly' );
205 is
( $suggestion->{managedby
}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
206 is
( $suggestion->{manageddate
}, undef, 'ModSuggestion stores empty string as undef for date' );
207 isnt
( $suggestion->{accepteddate
}, undef, 'ModSuggestion does not update a non given date value' );
208 is
( $suggestion->{note
}, 'my note', 'ModSuggestion should not erase data if not given' );
210 my $messages = C4
::Letters
::GetQueuedMessages
({
211 borrowernumber
=> $borrowernumber
213 is
( @
$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
215 my $mod_suggestion2 = {
217 suggestionid
=> $my_suggestionid,
219 warning_is
{ $status = ModSuggestion
($mod_suggestion2) }
220 "No suggestions STALLED letter transported by email",
221 "ModSuggestion status warning is correct";
222 is
( $status, 1, "ModSuggestion Status OK");
224 my $mod_suggestion3 = {
226 suggestionid
=> $my_suggestionid,
229 #Test the message_transport_type of suggestion notices
231 #Check the message_transport_type when the 'FallbackToSMSIfNoEmail' syspref is disabled
232 t
::lib
::Mocks
::mock_preference
( 'FallbackToSMSIfNoEmail', 0 );
233 $status = ModSuggestion
($mod_suggestion3);
234 is
( $status, 1, 'ModSuggestion modifies one entry' );
235 $suggestion = GetSuggestion
($my_suggestionid);
236 is
( $suggestion->{STATUS
}, $mod_suggestion3->{STATUS
}, 'ModSuggestion modifies the status correctly' );
237 $messages = C4
::Letters
::GetQueuedMessages
({
238 borrowernumber
=> $borrowernumber
240 is
( @
$messages, 1, 'ModSuggestion sends an email if the status is updated' );
241 is
($messages->[0]->{message_transport_type
}, 'email', 'When FallbackToSMSIfNoEmail syspref is disabled the suggestion message_transport_type is always email');
242 is
( CountSuggestion
('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
244 #Check the message_transport_type when the 'FallbackToSMSIfNoEmail' syspref is enabled and the borrower has a smsalertnumber and no email
245 t
::lib
::Mocks
::mock_preference
( 'FallbackToSMSIfNoEmail', 1 );
246 ModSuggestion
($mod_suggestion3);
247 $messages = C4
::Letters
::GetQueuedMessages
({
248 borrowernumber
=> $borrowernumber
250 is
($messages->[1]->{message_transport_type
}, 'sms', 'When FallbackToSMSIfNoEmail syspref is enabled the suggestion message_transport_type is sms if the borrower has no email');
252 #Make a new suggestion for a borrower with defined email and no smsalertnumber
253 my $my_suggestion_2_id = NewSuggestion
($my_suggestion_with_budget2);
255 #Check the message_transport_type when the 'FallbackToSMSIfNoEmail' syspref is enabled and the borrower has a defined email and no smsalertnumber
256 t
::lib
::Mocks
::mock_preference
( 'FallbackToSMSIfNoEmail', 1 );
257 my $mod_suggestion4 = {
259 suggestionid
=> $my_suggestion_2_id,
261 ModSuggestion
($mod_suggestion4);
262 $messages = C4
::Letters
::GetQueuedMessages
({
263 borrowernumber
=> $borrowernumber2
265 is
($messages->[0]->{message_transport_type
}, 'email', 'When FallbackToSMSIfNoEmail syspref is enabled the suggestion message_transport_type is email if the borrower has an email');
268 # Hiding the expected warning displayed by DBI
269 # DBD::mysql::st execute failed: Incorrect date value: 'invalid date!' for column 'manageddate'
271 open STDERR
, '>', '/dev/null';
273 $mod_suggestion4->{manageddate
} = 'invalid date!';
274 ModSuggestion
($mod_suggestion4);
275 $messages = C4
::Letters
::GetQueuedMessages
({
276 borrowernumber
=> $borrowernumber2
280 is
(scalar(@
$messages), 1, 'No new letter should have been generated if the update raised an error');
283 is
( GetSuggestionInfo
(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
284 $suggestion = GetSuggestionInfo
($my_suggestionid);
285 is
( $suggestion->{suggestionid
}, $my_suggestionid, 'GetSuggestionInfo returns the suggestion id correctly' );
286 is
( $suggestion->{title
}, $mod_suggestion1->{title
}, 'GetSuggestionInfo returns the title correctly' );
287 is
( $suggestion->{author
}, $mod_suggestion1->{author
}, 'GetSuggestionInfo returns the author correctly' );
288 is
( $suggestion->{publishercode
}, $mod_suggestion1->{publishercode
}, 'GetSuggestionInfo returns the publisher code correctly' );
289 is
( $suggestion->{suggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfo returns the borrower number correctly' );
290 is
( $suggestion->{biblionumber
}, $my_suggestion->{biblionumber
}, 'GetSuggestionInfo returns the biblio number correctly' );
291 is
( $suggestion->{STATUS
}, $mod_suggestion3->{STATUS
}, 'GetSuggestionInfo returns the status correctly' );
292 is
( $suggestion->{surnamesuggestedby
}, $member->{surname
}, 'GetSuggestionInfo returns the surname correctly' );
293 is
( $suggestion->{firstnamesuggestedby
}, $member->{firstname
}, 'GetSuggestionInfo returns the firstname correctly' );
294 is
( $suggestion->{borrnumsuggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfo returns the borrower number correctly' );
297 is
( GetSuggestionFromBiblionumber
(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
298 is
( GetSuggestionFromBiblionumber
(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
299 is
( GetSuggestionFromBiblionumber
($biblio_1->biblionumber), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
302 is
( GetSuggestionInfoFromBiblionumber
(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
303 is
( GetSuggestionInfoFromBiblionumber
(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
304 $suggestion = GetSuggestionInfoFromBiblionumber
($biblio_1->biblionumber);
305 is
( $suggestion->{suggestionid
}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
306 is
( $suggestion->{title
}, $mod_suggestion1->{title
}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
307 is
( $suggestion->{author
}, $mod_suggestion1->{author
}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
308 is
( $suggestion->{publishercode
}, $mod_suggestion1->{publishercode
}, 'GetSuggestionInfoFromBiblionumber returns the publisher code correctly' );
309 is
( $suggestion->{suggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfoFromBiblionumber returns the borrower number correctly' );
310 is
( $suggestion->{biblionumber
}, $my_suggestion->{biblionumber
}, 'GetSuggestionInfoFromBiblionumber returns the biblio number correctly' );
311 is
( $suggestion->{STATUS
}, $mod_suggestion3->{STATUS
}, 'GetSuggestionInfoFromBiblionumber returns the status correctly' );
312 is
( $suggestion->{surnamesuggestedby
}, $member->{surname
}, 'GetSuggestionInfoFromBiblionumber returns the surname correctly' );
313 is
( $suggestion->{firstnamesuggestedby
}, $member->{firstname
}, 'GetSuggestionInfoFromBiblionumber returns the firstname correctly' );
314 is
( $suggestion->{borrnumsuggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionInfoFromBiblionumeber returns the borrower number correctly' );
317 my $suggestions = GetSuggestionByStatus
();
318 is
( @
$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
319 $suggestions = GetSuggestionByStatus
('CHECKED');
320 is
( @
$suggestions, 2, 'GetSuggestionByStatus returns the correct number of suggestions' );
321 is
( $suggestions->[0]->{suggestionid
}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
322 is
( $suggestions->[0]->{title
}, $mod_suggestion1->{title
}, 'GetSuggestionByStatus returns the title correctly' );
323 is
( $suggestions->[0]->{author
}, $mod_suggestion1->{author
}, 'GetSuggestionByStatus returns the author correctly' );
324 is
( $suggestions->[0]->{publishercode
}, $mod_suggestion1->{publishercode
}, 'GetSuggestionByStatus returns the publisher code correctly' );
325 is
( $suggestions->[0]->{suggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionByStatus returns the borrower number correctly' );
326 is
( $suggestions->[0]->{biblionumber
}, $my_suggestion->{biblionumber
}, 'GetSuggestionByStatus returns the biblio number correctly' );
327 is
( $suggestions->[0]->{STATUS
}, $mod_suggestion3->{STATUS
}, 'GetSuggestionByStatus returns the status correctly' );
328 is
( $suggestions->[0]->{surnamesuggestedby
}, $member->{surname
}, 'GetSuggestionByStatus returns the surname correctly' );
329 is
( $suggestions->[0]->{firstnamesuggestedby
}, $member->{firstname
}, 'GetSuggestionByStatus returns the firstname correctly' );
330 is
( $suggestions->[0]->{branchcodesuggestedby
}, $member->{branchcode
}, 'GetSuggestionByStatus returns the branch code correctly' );
331 is
( $suggestions->[0]->{borrnumsuggestedby
}, $my_suggestion->{suggestedby
}, 'GetSuggestionByStatus returns the borrower number correctly' );
332 is
( $suggestions->[0]->{categorycodesuggestedby
}, $member->{categorycode
}, 'GetSuggestionByStatus returns the category code correctly' );
335 is
( ConnectSuggestionAndBiblio
(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
336 my $biblio_2 = $builder->build_object({ class => 'Koha::Biblios' });
337 my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio
($my_suggestionid, $biblio_2->biblionumber);
338 is
( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
339 $suggestion = GetSuggestion
($my_suggestionid);
340 is
( $suggestion->{biblionumber
}, $biblio_2->biblionumber, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
342 my $search_suggestion = SearchSuggestion
();
343 is
( @
$search_suggestion, 3, 'SearchSuggestion without arguments returns all suggestions' );
345 $search_suggestion = SearchSuggestion
({
346 title
=> $mod_suggestion1->{title
},
348 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
349 $search_suggestion = SearchSuggestion
({
350 title
=> 'another title',
352 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
354 $search_suggestion = SearchSuggestion
({
355 author
=> $mod_suggestion1->{author
},
357 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
358 $search_suggestion = SearchSuggestion
({
359 author
=> 'another author',
361 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
363 $search_suggestion = SearchSuggestion
({
364 publishercode
=> $mod_suggestion1->{publishercode
},
366 is
( @
$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
367 $search_suggestion = SearchSuggestion
({
368 publishercode
=> 'another publishercode',
370 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
372 $search_suggestion = SearchSuggestion
({
373 STATUS
=> $mod_suggestion3->{STATUS
},
375 is
( @
$search_suggestion, 2, 'SearchSuggestion returns the correct number of suggestions' );
377 $search_suggestion = SearchSuggestion
({
380 is
( @
$search_suggestion, 0, 'SearchSuggestion should not return all suggestions if we want the suggestions with a STATUS=""' );
381 $search_suggestion = SearchSuggestion
({
382 STATUS
=> 'REJECTED',
384 is
( @
$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
386 $search_suggestion = SearchSuggestion
({
389 is
( @
$search_suggestion, 3, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' );
390 $search_suggestion = SearchSuggestion
({
391 budgetid
=> $budget_id,
393 is
( @
$search_suggestion, 2, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' );
394 $search_suggestion = SearchSuggestion
({
395 budgetid
=> '__NONE__',
397 is
( @
$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns the correct number of suggestions' );
398 $search_suggestion = SearchSuggestion
({
399 budgetid
=> '__ANY__',
401 is
( @
$search_suggestion, 3, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' );
403 $search_suggestion = SearchSuggestion
({ budgetid
=> $budget_id });
404 is
( @
$search_suggestion[0]->{budget_name
}, GetBudget
($budget_id)->{budget_name
}, 'SearchSuggestion returns the correct budget name');
405 $search_suggestion = SearchSuggestion
({ budgetid
=> "__NONE__" });
406 is
( @
$search_suggestion[0]->{budget_name
}, undef, 'SearchSuggestion returns the correct budget name');
409 my $del_suggestion = {
410 title
=> 'my deleted title',
412 suggestedby
=> $borrowernumber,
414 my $del_suggestionid = NewSuggestion
($del_suggestion);
416 is
( CountSuggestion
('CHECKED'), 3, 'CountSuggestion returns the correct number of suggestions' );
418 is
( DelSuggestion
(), '0E0', 'DelSuggestion without arguments returns 0E0' );
419 is
( DelSuggestion
($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
420 is
( DelSuggestion
(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
421 $suggestion = DelSuggestion
($borrowernumber, $my_suggestionid);
422 is
( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
424 $suggestions = GetSuggestionByStatus
('CHECKED');
425 is
( @
$suggestions, 2, 'DelSuggestion deletes one suggestion' );
426 is
( $suggestions->[1]->{title
}, $del_suggestion->{title
}, 'DelSuggestion deletes the correct suggestion' );
429 $my_suggestion->{budgetid
} = ''; # If budgetid == '', NULL should be set in DB
430 my $my_suggestionid_test_budgetid = NewSuggestion
($my_suggestion);
431 $suggestion = GetSuggestion
($my_suggestionid_test_budgetid);
432 is
( $suggestion->{budgetid
}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
434 $my_suggestion->{budgetid
} = ''; # If budgetid == '', NULL should be set in DB
435 ModSuggestion
( $my_suggestion );
436 $suggestion = GetSuggestion
($my_suggestionid_test_budgetid);
437 is
( $suggestion->{budgetid
}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
440 title
=> "Cuisine d'automne",
441 author
=> "Catherine",
445 my $record = MarcRecordFromNewSuggestion
($suggestion2);
447 is
("MARC::Record", ref($record), "MarcRecordFromNewSuggestion should return a MARC::Record object");
449 my ($title_tag, $title_subfield) = C4
::Biblio
::GetMarcFromKohaField
('biblio.title', '');
451 is
($record->field( $title_tag )->subfield( $title_subfield ), "Cuisine d'automne", "Record from suggestion title should be 'Cuisine d'automne'");
453 my ($author_tag, $author_subfield) = C4
::Biblio
::GetMarcFromKohaField
('biblio.author', '');
455 is
($record->field( $author_tag )->subfield( $author_subfield ), "Catherine", "Record from suggestion author should be 'Catherine'");
457 subtest
'GetUnprocessedSuggestions' => sub {
459 $dbh->do(q
|DELETE FROM suggestions
|);
460 my $my_suggestionid = NewSuggestion
($my_suggestion);
461 my $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
462 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return 0 if a suggestion has been processed but not linked to a fund' );
463 my $status = ModSuggestion
($mod_suggestion1);
464 my $suggestion = GetSuggestion
($my_suggestionid);
465 is
( $suggestion->{budgetid
}, undef, 'ModSuggestion should set budgetid to NULL if not given' );
466 ModSuggestion
( { suggestionid
=> $my_suggestionid, budgetid
=> $budget_id } );
467 $suggestion = GetSuggestion
($my_suggestionid);
468 is
( $suggestion->{budgetid
}, $budget_id, 'ModSuggestion should modify budgetid if given' );
470 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
471 is
( scalar(@
$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
473 warning_is
{ ModSuggestion
( { suggestionid
=> $my_suggestionid, STATUS
=> 'REJECTED' } ) }
474 'No suggestions REJECTED letter transported by email',
475 'Warning raised if no REJECTED letter by email';
476 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
477 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
479 warning_is
{ ModSuggestion
( { suggestionid
=> $my_suggestionid, STATUS
=> 'ASKED', suggesteddate
=> dt_from_string
->add_duration( DateTime
::Duration
->new( days
=> -4 ) ) } ); }
480 'No suggestions ASKED letter transported by email',
481 'Warning raised if no ASKED letter by email';
482 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
;
483 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should use 0 as default value for days' );
484 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
(4);
485 is
( scalar(@
$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion suggested 4 days ago' );
486 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
(3);
487 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 3 days ago' );
488 $unprocessed_suggestions = C4
::Suggestions
::GetUnprocessedSuggestions
(5);
489 is
( scalar(@
$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 5 days ago' );
492 subtest
'DelSuggestionsOlderThan' => sub {
495 Koha
::Suggestions
->delete;
497 # Add four suggestions; note that STATUS needs uppercase (FIXME)
498 my $d1 = output_pref
({ dt
=> dt_from_string
->add(days
=> -2), dateformat
=> 'sql' });
499 my $d2 = output_pref
({ dt
=> dt_from_string
->add(days
=> -4), dateformat
=> 'sql' });
500 my $sugg01 = $builder->build({ source
=> 'Suggestion', value
=> { date
=> $d1, STATUS
=> 'ASKED' }});
501 my $sugg02 = $builder->build({ source
=> 'Suggestion', value
=> { date
=> $d1, STATUS
=> 'CHECKED' }});
502 my $sugg03 = $builder->build({ source
=> 'Suggestion', value
=> { date
=> $d2, STATUS
=> 'ASKED' }});
503 my $sugg04 = $builder->build({ source
=> 'Suggestion', value
=> { date
=> $d2, STATUS
=> 'ACCEPTED' }});
505 # Test no parameter: should do nothing
506 C4
::Suggestions
::DelSuggestionsOlderThan
();
507 is
( Koha
::Suggestions
->count, 4, 'No suggestions deleted' );
508 # Test zero: should do nothing too
509 C4
::Suggestions
::DelSuggestionsOlderThan
(0);
510 is
( Koha
::Suggestions
->count, 4, 'No suggestions deleted again' );
511 # Test negative value
512 C4
::Suggestions
::DelSuggestionsOlderThan
(-1);
513 is
( Koha
::Suggestions
->count, 4, 'No suggestions deleted for -1' );
515 # Test positive values
516 C4
::Suggestions
::DelSuggestionsOlderThan
(5);
517 is
( Koha
::Suggestions
->count, 4, 'No suggestions>5d deleted' );
518 C4
::Suggestions
::DelSuggestionsOlderThan
(3);
519 is
( Koha
::Suggestions
->count, 3, '1 suggestions>3d deleted' );
520 C4
::Suggestions
::DelSuggestionsOlderThan
(1);
521 is
( Koha
::Suggestions
->count, 2, '1 suggestions>1d deleted' );
524 subtest
'EmailPurchaseSuggestions' => sub {
527 $dbh->do(q
|DELETE FROM message_queue
|);
529 t
::lib
::Mocks
::mock_preference
( "KohaAdminEmailAddress",
530 'noreply@hosting.com' );
532 # EmailPurchaseSuggestions set to disabled
533 t
::lib
::Mocks
::mock_preference
( "EmailPurchaseSuggestions", "0" );
534 NewSuggestion
($my_suggestion);
535 my $newsuggestions_messages = C4
::Letters
::GetQueuedMessages
(
537 borrowernumber
=> $borrowernumber
540 is
( @
$newsuggestions_messages, 0,
541 'NewSuggestions does not send an email when disabled' );
543 # EmailPurchaseSuggestions set to BranchEmailAddress
544 t
::lib
::Mocks
::mock_preference
( "EmailPurchaseSuggestions",
545 "BranchEmailAddress" );
546 NewSuggestion
($my_suggestion);
548 t
::lib
::Mocks
::mock_preference
( "ReplytoDefault", 'library@b.c' );
549 NewSuggestion
($my_suggestion);
551 Koha
::Libraries
->find('CPL')->update( { branchemail
=> 'branchemail@hosting.com' } );
552 NewSuggestion
($my_suggestion);
554 Koha
::Libraries
->find('CPL')->update( { branchreplyto
=> 'branchemail@b.c' } );
555 NewSuggestion
($my_suggestion);
557 $newsuggestions_messages = C4
::Letters
::GetQueuedMessages
(
559 borrowernumber
=> $borrowernumber
562 isnt
( @
$newsuggestions_messages, 0, 'NewSuggestions sends an email' );
564 C4
::Letters
::GetMessage
( $newsuggestions_messages->[0]->{message_id
} );
565 is
( $message1->{to_address
}, 'noreply@hosting.com',
566 'BranchEmailAddress falls back to KohaAdminEmailAddress if branchreplyto, branchemail and ReplytoDefault are not set'
569 C4
::Letters
::GetMessage
( $newsuggestions_messages->[1]->{message_id
} );
570 is
( $message2->{to_address
}, 'library@b.c',
571 'BranchEmailAddress falls back to ReplytoDefault if neither branchreplyto or branchemail are set'
574 C4
::Letters
::GetMessage
( $newsuggestions_messages->[2]->{message_id
} );
575 is
( $message3->{to_address
}, 'branchemail@hosting.com',
576 'BranchEmailAddress uses branchemail if branch_replto is not set'
579 C4
::Letters
::GetMessage
( $newsuggestions_messages->[3]->{message_id
} );
580 is
( $message4->{to_address
}, 'branchemail@b.c',
581 'BranchEmailAddress uses branchreplyto in preference to branchemail when set'
584 # EmailPurchaseSuggestions set to KohaAdminEmailAddress
585 t
::lib
::Mocks
::mock_preference
( "EmailPurchaseSuggestions",
586 "KohaAdminEmailAddress" );
588 t
::lib
::Mocks
::mock_preference
( "ReplytoDefault", undef );
589 NewSuggestion
($my_suggestion);
591 t
::lib
::Mocks
::mock_preference
( "ReplytoDefault", 'library@b.c' );
592 NewSuggestion
($my_suggestion);
594 $newsuggestions_messages = C4
::Letters
::GetQueuedMessages
(
596 borrowernumber
=> $borrowernumber
600 C4
::Letters
::GetMessage
( $newsuggestions_messages->[4]->{message_id
} );
601 is
( $message5->{to_address
},
602 'noreply@hosting.com', 'KohaAdminEmailAddress uses KohaAdminEmailAddress when ReplytoDefault is not set' );
604 C4
::Letters
::GetMessage
( $newsuggestions_messages->[5]->{message_id
} );
605 is
( $message6->{to_address
},
606 'library@b.c', 'KohaAdminEmailAddress uses ReplytoDefualt when ReplytoDefault is set' );
608 # EmailPurchaseSuggestions set to EmailAddressForSuggestions
609 t
::lib
::Mocks
::mock_preference
( "EmailPurchaseSuggestions",
610 "EmailAddressForSuggestions" );
612 t
::lib
::Mocks
::mock_preference
( "ReplytoDefault", undef );
613 NewSuggestion
($my_suggestion);
615 t
::lib
::Mocks
::mock_preference
( "ReplytoDefault", 'library@b.c' );
616 NewSuggestion
($my_suggestion);
618 t
::lib
::Mocks
::mock_preference
( "EmailAddressForSuggestions",
620 NewSuggestion
($my_suggestion);
622 $newsuggestions_messages = C4
::Letters
::GetQueuedMessages
(
624 borrowernumber
=> $borrowernumber
628 C4
::Letters
::GetMessage
( $newsuggestions_messages->[6]->{message_id
} );
629 is
( $message7->{to_address
},
630 'noreply@hosting.com', 'EmailAddressForSuggestions uses KohaAdminEmailAddress when neither EmailAddressForSuggestions or ReplytoDefault are set' );
633 C4
::Letters
::GetMessage
( $newsuggestions_messages->[7]->{message_id
} );
634 is
( $message8->{to_address
},
635 'library@b.c', 'EmailAddressForSuggestions uses ReplytoDefault when EmailAddressForSuggestions is not set' );
638 C4
::Letters
::GetMessage
( $newsuggestions_messages->[8]->{message_id
} );
639 is
( $message9->{to_address
},
640 'suggestions@b.c', 'EmailAddressForSuggestions uses EmailAddressForSuggestions when set' );
643 subtest
'ModSuggestion should work on suggestions without a suggester' => sub {
646 $dbh->do(q
|DELETE FROM suggestions
|);
647 my $my_suggestionid = NewSuggestion
($my_suggestion_without_suggestedby);
648 $suggestion = GetSuggestion
($my_suggestionid);
649 is
( $suggestion->{suggestedby
}, undef, "Suggestedby is undef" );
653 suggestionid
=> $my_suggestionid,
658 $suggestion = GetSuggestion
($my_suggestionid);
660 is
( $suggestion->{note
}, "Test note", "ModSuggestion works on suggestions without a suggester" );
663 $schema->storage->txn_rollback;