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 => 92;
25 use t::lib::TestBuilder;
28 use C4::Letters qw( GetQueuedMessages GetMessage );
29 use C4::Budgets qw( AddBudgetPeriod AddBudget GetBudget );
31 use Koha::DateUtils qw( dt_from_string output_pref );
35 use Koha::Suggestions;
38 use_ok('C4::Suggestions', qw( GetSuggestion ModSuggestion GetSuggestionInfo GetSuggestionFromBiblionumber GetSuggestionInfoFromBiblionumber GetSuggestionByStatus ConnectSuggestionAndBiblio DelSuggestion MarcRecordFromNewSuggestion GetUnprocessedSuggestions DelSuggestionsOlderThan ));
41 my $schema = Koha::Database->new->schema;
42 $schema->storage->txn_begin;
43 my $dbh = C4::Context->dbh;
44 my $builder = t::lib::TestBuilder->new;
46 t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", "0");
48 # Reset item types to only the default ones
49 $dbh->do(q|DELETE FROM itemtypes;|);
51 INSERT INTO itemtypes (itemtype, description, rentalcharge, notforloan, imageurl, summary) VALUES
52 ('BK', 'Books',5,0,'bridge/book.png',''),
53 ('MX', 'Mixed Materials',5,0,'bridge/kit.png',''),
54 ('CF', 'Computer Files',5,0,'bridge/computer_file.png',''),
55 ('MP', 'Maps',5,0,'bridge/map.png',''),
56 ('VM', 'Visual Materials',5,1,'bridge/dvd.png',''),
57 ('MU', 'Music',5,0,'bridge/sound.png',''),
58 ('CR', 'Continuing Resources',5,0,'bridge/periodical.png',''),
59 ('REF', 'Reference',0,1,'bridge/reference.png','');|;
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 $dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'NEW_SUGGESTION', 'Content for new suggestion')|);
70 if (not defined Koha::Libraries->find('CPL')) {
71 Koha::Library->new({ branchcode => 'CPL', branchname => 'Centerville' })->store;
74 my $patron_category = $builder->build({ source => 'Category' });
77 firstname => 'my firstname',
78 surname => 'my surname',
79 categorycode => $patron_category->{categorycode},
81 smsalertnumber => 12345,
85 firstname => 'my firstname',
86 surname => 'my surname',
87 categorycode => $patron_category->{categorycode},
89 email => 'to@example.com',
92 my $borrowernumber = Koha::Patron->new($member)->store->borrowernumber;
93 my $borrowernumber2 = Koha::Patron->new($member2)->store->borrowernumber;
95 my $biblio_1 = $builder->build_object({ class => 'Koha::Biblios' });
98 author => 'my author',
99 publishercode => 'my publishercode',
100 suggestedby => $borrowernumber,
101 biblionumber => $biblio_1->biblionumber,
105 accepteddate => dt_from_string,
107 quantity => '', # Insert an empty string into int to catch strict SQL modes errors
110 my $budgetperiod_id = AddBudgetPeriod({
111 budget_period_startdate => '2008-01-01',
112 budget_period_enddate => '2008-12-31',
113 budget_period_description => 'MAPERI',
114 budget_period_active => 1,
117 my $budget_id = AddBudget({
118 budget_code => 'ABCD',
119 budget_amount => '123.132000',
120 budget_name => 'ABCD',
121 budget_notes => 'This is a note',
122 budget_period_id => $budgetperiod_id,
124 my $my_suggestion_with_budget = {
125 title => 'my title 2',
126 author => 'my author 2',
127 publishercode => 'my publishercode 2',
128 suggestedby => $borrowernumber,
129 branchcode => '', # This should not fail be set to undef instead
130 biblionumber => $biblio_1->biblionumber,
133 accepteddate => dt_from_string,
135 budgetid => $budget_id,
137 my $my_suggestion_with_budget2 = {
138 title => 'my title 3',
139 author => 'my author 3',
140 publishercode => 'my publishercode 3',
141 suggestedby => $borrowernumber2,
142 biblionumber => $biblio_1->biblionumber,
145 accepteddate => dt_from_string,
147 budgetid => $budget_id,
149 my $my_suggestion_without_suggestedby = {
151 author => 'my author',
152 publishercode => 'my publishercode',
153 suggestedby => undef,
154 biblionumber => $biblio_1->biblionumber,
158 accepteddate => dt_from_string,
160 quantity => '', # Insert an empty string into int to catch strict SQL modes errors
163 my $my_suggestion_object = Koha::Suggestion->new($my_suggestion)->store;
164 my $my_suggestionid = $my_suggestion_object->id;
165 isnt( $my_suggestionid, 0, 'Suggestion is correctly saved' );
166 my $my_suggestion_with_budget_object = Koha::Suggestion->new($my_suggestion_with_budget)->store;
167 my $my_suggestionid_with_budget = $my_suggestion_with_budget_object->id;
169 is( GetSuggestion(), undef, 'GetSuggestion without the suggestion id returns undef' );
170 my $suggestion = GetSuggestion($my_suggestionid);
171 is( $suggestion->{title}, $my_suggestion->{title}, 'Suggestion stores the title correctly' );
172 is( $suggestion->{author}, $my_suggestion->{author}, 'Suggestion stores the author correctly' );
173 is( $suggestion->{publishercode}, $my_suggestion->{publishercode}, 'Suggestion stores the publishercode correctly' );
174 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'Suggestion stores the borrower number correctly' );
175 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'Suggestion stores the biblio number correctly' );
176 is( $suggestion->{STATUS}, 'ASKED', 'Suggestion stores a suggestion with the status ASKED by default' );
177 is( $suggestion->{managedby}, undef, 'Suggestion stores empty string as undef for non existent foreign key (integer)' );
178 is( $suggestion->{manageddate}, undef, 'Suggestion stores empty string as undef for date' );
179 is( $suggestion->{budgetid}, undef, 'Suggestion should set budgetid to NULL if not given' );
181 is( ModSuggestion(), undef, 'ModSuggestion without the suggestion returns undef' );
182 my $mod_suggestion1 = {
183 title => 'my modified title',
184 author => 'my modified author',
185 publishercode => 'my modified publishercode',
189 my $status = ModSuggestion($mod_suggestion1);
190 is( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
192 $mod_suggestion1->{suggestionid} = $my_suggestionid;
193 $status = ModSuggestion($mod_suggestion1);
194 is( $status, 1, 'ModSuggestion modifies one entry' );
195 $suggestion = GetSuggestion($my_suggestionid);
196 is( $suggestion->{title}, $mod_suggestion1->{title}, 'ModSuggestion modifies the title correctly' );
197 is( $suggestion->{author}, $mod_suggestion1->{author}, 'ModSuggestion modifies the author correctly' );
198 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'ModSuggestion modifies the publishercode correctly' );
199 is( $suggestion->{managedby}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
200 is( $suggestion->{manageddate}, undef, 'ModSuggestion stores empty string as undef for date' );
201 isnt( $suggestion->{accepteddate}, undef, 'ModSuggestion does not update a non given date value' );
202 is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not given' );
204 my $messages = C4::Letters::GetQueuedMessages({
205 borrowernumber => $borrowernumber
207 is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
209 my $authorised_value = Koha::AuthorisedValue->new(
211 category => 'SUGGEST_STATUS',
212 authorised_value => 'STALLED'
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');
243 #Check the message_transport_type when the 'FallbackToSMSIfNoEmail' syspref is enabled and the borrower has a smsalertnumber and no email
244 t::lib::Mocks::mock_preference( 'FallbackToSMSIfNoEmail', 1 );
245 ModSuggestion($mod_suggestion3);
246 $messages = C4::Letters::GetQueuedMessages({
247 borrowernumber => $borrowernumber
249 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');
251 #Make a new suggestion for a borrower with defined email and no smsalertnumber
252 my $my_suggestion_2_object = Koha::Suggestion->new($my_suggestion_with_budget2)->store();
253 my $my_suggestion_2_id = $my_suggestion_2_object->id;
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 $del_suggestion = {
343 title => 'my deleted title',
345 suggestedby => $borrowernumber,
347 my $del_suggestion_object = Koha::Suggestion->new($del_suggestion)->store();
348 my $del_suggestionid = $del_suggestion_object->id;
350 is( DelSuggestion(), '0E0', 'DelSuggestion without arguments returns 0E0' );
351 is( DelSuggestion($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
352 is( DelSuggestion(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
353 $suggestion = DelSuggestion($borrowernumber, $my_suggestionid);
354 is( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
356 $suggestions = GetSuggestionByStatus('CHECKED');
357 is( @$suggestions, 2, 'DelSuggestion deletes one suggestion' );
358 is( $suggestions->[1]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' );
361 $my_suggestion->{budgetid} = ''; # If budgetid == '', NULL should be set in DB
362 my $my_suggestionid_test_budget_object = Koha::Suggestion->new($my_suggestion)->store;
363 my $my_suggestionid_test_budgetid = $my_suggestionid_test_budget_object->id;
364 $suggestion = GetSuggestion($my_suggestionid_test_budgetid);
365 is( $suggestion->{budgetid}, undef, 'Suggestion Should set budgetid to NULL if equals an empty string' );
367 $my_suggestion->{budgetid} = ''; # If budgetid == '', NULL should be set in DB
368 ModSuggestion( $my_suggestion );
369 $suggestion = GetSuggestion($my_suggestionid_test_budgetid);
370 is( $suggestion->{budgetid}, undef, 'Suggestion Should set budgetid to NULL if equals an empty string' );
373 title => "Cuisine d'automne",
374 author => "Catherine",
378 my $record = MarcRecordFromNewSuggestion($suggestion2);
380 is("MARC::Record", ref($record), "MarcRecordFromNewSuggestion should return a MARC::Record object");
382 my ($title_tag, $title_subfield) = C4::Biblio::GetMarcFromKohaField('biblio.title', '');
384 is($record->field( $title_tag )->subfield( $title_subfield ), "Cuisine d'automne", "Record from suggestion title should be 'Cuisine d'automne'");
386 my ($author_tag, $author_subfield) = C4::Biblio::GetMarcFromKohaField('biblio.author', '');
388 is($record->field( $author_tag )->subfield( $author_subfield ), "Catherine", "Record from suggestion author should be 'Catherine'");
390 subtest 'GetUnprocessedSuggestions' => sub {
392 $dbh->do(q|DELETE FROM suggestions|);
393 my $my_suggestionid = Koha::Suggestion->new($my_suggestion)->store->id;
394 my $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
395 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return 0 if a suggestion has been processed but not linked to a fund' );
396 my $status = ModSuggestion($mod_suggestion1);
397 my $suggestion = GetSuggestion($my_suggestionid);
398 is( $suggestion->{budgetid}, undef, 'ModSuggestion should set budgetid to NULL if not given' );
399 ModSuggestion( { suggestionid => $my_suggestionid, budgetid => $budget_id } );
400 $suggestion = GetSuggestion($my_suggestionid);
401 is( $suggestion->{budgetid}, $budget_id, 'ModSuggestion should modify budgetid if given' );
403 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
404 is( scalar(@$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
406 warning_is { ModSuggestion( { suggestionid => $my_suggestionid, STATUS => 'REJECTED' } ) }
407 'No suggestions REJECTED letter transported by email',
408 'Warning raised if no REJECTED letter by email';
409 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
410 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
412 warning_is { ModSuggestion( { suggestionid => $my_suggestionid, STATUS => 'ASKED', suggesteddate => dt_from_string->add_duration( DateTime::Duration->new( days => -4 ) ) } ); }
413 'No suggestions ASKED letter transported by email',
414 'Warning raised if no ASKED letter by email';
415 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
416 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should use 0 as default value for days' );
417 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(4);
418 is( scalar(@$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion suggested 4 days ago' );
419 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(3);
420 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 3 days ago' );
421 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(5);
422 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 5 days ago' );
425 subtest 'DelSuggestionsOlderThan' => sub {
428 Koha::Suggestions->delete;
430 # Add four suggestions; note that STATUS needs uppercase (FIXME)
431 my $d1 = output_pref({ dt => dt_from_string->add(days => -2), dateformat => 'sql' });
432 my $d2 = output_pref({ dt => dt_from_string->add(days => -4), dateformat => 'sql' });
433 my $sugg01 = $builder->build({ source => 'Suggestion', value => { manageddate => $d1, date => $d2, STATUS => 'ASKED' }});
434 my $sugg02 = $builder->build({ source => 'Suggestion', value => { manageddate => $d1, date => $d2, STATUS => 'CHECKED' }});
435 my $sugg03 = $builder->build({ source => 'Suggestion', value => { manageddate => $d2, date => $d2, STATUS => 'ASKED' }});
436 my $sugg04 = $builder->build({ source => 'Suggestion', value => { manageddate => $d2, date => $d2, STATUS => 'ACCEPTED' }});
438 # Test no parameter: should do nothing
439 C4::Suggestions::DelSuggestionsOlderThan();
440 is( Koha::Suggestions->count, 4, 'No suggestions deleted' );
441 # Test zero: should do nothing too
442 C4::Suggestions::DelSuggestionsOlderThan(0);
443 is( Koha::Suggestions->count, 4, 'No suggestions deleted again' );
444 # Test negative value
445 C4::Suggestions::DelSuggestionsOlderThan(-1);
446 is( Koha::Suggestions->count, 4, 'No suggestions deleted for -1' );
448 # Test positive values
449 C4::Suggestions::DelSuggestionsOlderThan(5);
450 is( Koha::Suggestions->count, 4, 'No suggestions>5d deleted' );
451 C4::Suggestions::DelSuggestionsOlderThan(3);
452 is( Koha::Suggestions->count, 3, '1 suggestions>3d deleted' );
453 C4::Suggestions::DelSuggestionsOlderThan(1);
454 is( Koha::Suggestions->count, 2, '1 suggestions>1d deleted' );
457 subtest 'EmailPurchaseSuggestions' => sub {
460 $dbh->do(q|DELETE FROM message_queue|);
462 t::lib::Mocks::mock_preference( "KohaAdminEmailAddress",
463 'noreply@hosting.com' );
465 # EmailPurchaseSuggestions set to disabled
466 t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", "0" );
467 Koha::Suggestion->new($my_suggestion)->store;
468 my $newsuggestions_messages = C4::Letters::GetQueuedMessages(
470 borrowernumber => $borrowernumber
473 is( @$newsuggestions_messages, 0,
474 'New suggestion does not send an email when EmailPurchaseSuggestions disabled' );
476 # EmailPurchaseSuggestions set to BranchEmailAddress
477 t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", "BranchEmailAddress" );
478 t::lib::Mocks::mock_preference( "ReplytoDefault", "" );
479 t::lib::Mocks::mock_preference( "EmailAddressForSuggestions", "" );
480 Koha::Libraries->find('CPL')->update( { branchemail => '', branchreplyto => '' } );
481 Koha::Suggestion->new($my_suggestion)->store;
483 t::lib::Mocks::mock_preference( "ReplytoDefault", 'library@b.c' );
484 Koha::Suggestion->new($my_suggestion)->store;
486 Koha::Libraries->find('CPL')->update( { branchemail => 'branchemail@hosting.com' } );
487 Koha::Suggestion->new($my_suggestion)->store;
489 Koha::Libraries->find('CPL')->update( { branchreplyto => 'branchemail@b.c' } );
490 Koha::Suggestion->new($my_suggestion)->store;
492 $newsuggestions_messages = C4::Letters::GetQueuedMessages(
494 borrowernumber => $borrowernumber
497 isnt( @$newsuggestions_messages, 0, 'New suggestions sends an email wne EmailPurchaseSuggestions enabled' );
499 C4::Letters::GetMessage( $newsuggestions_messages->[0]->{message_id} );
500 is( $message1->{to_address}, 'noreply@hosting.com',
501 'BranchEmailAddress falls back to KohaAdminEmailAddress if branchreplyto, branchemail and ReplytoDefault are not set'
504 C4::Letters::GetMessage( $newsuggestions_messages->[1]->{message_id} );
505 is( $message2->{to_address}, 'library@b.c',
506 'BranchEmailAddress falls back to ReplytoDefault if neither branchreplyto or branchemail are set'
509 C4::Letters::GetMessage( $newsuggestions_messages->[2]->{message_id} );
510 is( $message3->{to_address}, 'branchemail@hosting.com',
511 'BranchEmailAddress uses branchemail if branch_replto is not set'
514 C4::Letters::GetMessage( $newsuggestions_messages->[3]->{message_id} );
515 is( $message4->{to_address}, 'branchemail@b.c',
516 'BranchEmailAddress uses branchreplyto in preference to branchemail when set'
519 # EmailPurchaseSuggestions set to KohaAdminEmailAddress
520 t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions",
521 "KohaAdminEmailAddress" );
523 t::lib::Mocks::mock_preference( "ReplytoDefault", undef );
524 Koha::Suggestion->new($my_suggestion)->store;
526 t::lib::Mocks::mock_preference( "ReplytoDefault", 'library@b.c' );
527 Koha::Suggestion->new($my_suggestion)->store;
529 $newsuggestions_messages = C4::Letters::GetQueuedMessages(
531 borrowernumber => $borrowernumber
535 C4::Letters::GetMessage( $newsuggestions_messages->[4]->{message_id} );
536 is( $message5->{to_address},
537 'noreply@hosting.com', 'KohaAdminEmailAddress uses KohaAdminEmailAddress when ReplytoDefault is not set' );
539 C4::Letters::GetMessage( $newsuggestions_messages->[5]->{message_id} );
540 is( $message6->{to_address},
541 'library@b.c', 'KohaAdminEmailAddress uses ReplytoDefualt when ReplytoDefault is set' );
543 # EmailPurchaseSuggestions set to EmailAddressForSuggestions
544 t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions",
545 "EmailAddressForSuggestions" );
547 t::lib::Mocks::mock_preference( "ReplytoDefault", undef );
548 Koha::Suggestion->new($my_suggestion)->store;
550 t::lib::Mocks::mock_preference( "ReplytoDefault", 'library@b.c' );
551 Koha::Suggestion->new($my_suggestion)->store;
553 t::lib::Mocks::mock_preference( "EmailAddressForSuggestions",
555 Koha::Suggestion->new($my_suggestion)->store;
557 $newsuggestions_messages = C4::Letters::GetQueuedMessages(
559 borrowernumber => $borrowernumber
563 C4::Letters::GetMessage( $newsuggestions_messages->[6]->{message_id} );
564 is( $message7->{to_address},
565 'noreply@hosting.com', 'EmailAddressForSuggestions uses KohaAdminEmailAddress when neither EmailAddressForSuggestions or ReplytoDefault are set' );
568 C4::Letters::GetMessage( $newsuggestions_messages->[7]->{message_id} );
569 is( $message8->{to_address},
570 'library@b.c', 'EmailAddressForSuggestions uses ReplytoDefault when EmailAddressForSuggestions is not set' );
573 C4::Letters::GetMessage( $newsuggestions_messages->[8]->{message_id} );
574 is( $message9->{to_address},
575 'suggestions@b.c', 'EmailAddressForSuggestions uses EmailAddressForSuggestions when set' );
578 subtest 'ModSuggestion should work on suggestions without a suggester' => sub {
581 $dbh->do(q|DELETE FROM suggestions|);
582 my $my_suggestionid = Koha::Suggestion->new($my_suggestion_without_suggestedby)->store()->id;
583 $suggestion = GetSuggestion($my_suggestionid);
584 is( $suggestion->{suggestedby}, undef, "Suggestedby is undef" );
588 suggestionid => $my_suggestionid,
593 $suggestion = GetSuggestion($my_suggestionid);
595 is( $suggestion->{note}, "Test note", "ModSuggestion works on suggestions without a suggester" );
598 subtest 'place_hold tests' => sub {
601 t::lib::Mocks::mock_preference( "PlaceHoldsOnOrdersFromSuggestions", "0" );
603 my $biblio = $builder->build_sample_biblio();
604 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
605 my $suggestion = $builder->build_object(
607 class => 'Koha::Suggestions',
609 branchcode => $patron->branchcode,
610 biblionumber => undef,
611 suggestedby => $patron->id
616 my $hold_id = $suggestion->place_hold();
617 is( $hold_id, undef, "No suggestion placed when preference is disabled" );
619 t::lib::Mocks::mock_preference( "PlaceHoldsOnOrdersFromSuggestions", "1" );
621 $hold_id = $suggestion->place_hold();
624 "No suggestion placed when preference is enabled and suggestion does not have a biblionumber"
627 $suggestion->biblionumber( $biblio->id )->store();
628 $suggestion->discard_changes();
630 $hold_id = $suggestion->place_hold();
631 ok( $hold_id, "Suggestion placed when preference is enabled and suggestion does have a biblionumber" );
633 my $hold = Koha::Holds->find($hold_id);
636 t::lib::Mocks::mock_preference( "PlaceHoldsOnOrdersFromSuggestions", "0" );
638 $hold_id = $suggestion->place_hold();
639 is( $hold_id, undef, "Suggestion not placed when preference is disabled and suggestion does have a biblionumber" );
643 subtest 'Suggestion with ISBN' => sub {
644 my $suggestion_with_isbn = {
645 isbn => '1940997232',
646 title => "The Clouds",
647 author => "Aristophanes",
649 my $record = MarcRecordFromNewSuggestion( $suggestion_with_isbn );
650 is("MARC::Record", ref($record), "MarcRecordFromNewSuggestion should return a MARC::Record object");
652 my ($isbn_tag, $isbn_subfield) = C4::Biblio::GetMarcFromKohaField('biblioitems.isbn', '');
653 is($record->field( $isbn_tag )->subfield( $isbn_subfield ), "1940997232", "ISBN Record from suggestion ISBN should be '1940997232'");
655 my ($issn_tag, $issn_subfield) = C4::Biblio::GetMarcFromKohaField('biblioitems.issn', '');
656 is($record->field( $issn_tag )->subfield( $issn_subfield ), "1940997232", "ISSN Record from suggestion ISBN should also be '1940997232'");
658 my ($title_tag, $title_subfield) = C4::Biblio::GetMarcFromKohaField('biblio.title', '');
659 is($record->field( $title_tag), undef, "Record from suggestion title should be empty");
661 my ($author_tag, $author_subfield) = C4::Biblio::GetMarcFromKohaField('biblio.author', '');
662 is($record->field( $author_tag), undef, "Record from suggestion author should be empty");
665 $schema->storage->txn_rollback;