X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=t%2Fdb_dependent%2FSuggestions.t;h=0349353d02e85fb7d7956d8850c2a43f29b3b9dc;hb=7d8b96803f664d86762a6afb966051f7d565c40e;hp=63f390e8296f73af2f9c85adbb633871ac9aec99;hpb=a9d3eb233bffab3b974e51f34adf502548341c3c;p=koha-ffzg.git diff --git a/t/db_dependent/Suggestions.t b/t/db_dependent/Suggestions.t index 63f390e829..0349353d02 100644 --- a/t/db_dependent/Suggestions.t +++ b/t/db_dependent/Suggestions.t @@ -17,34 +17,36 @@ use Modern::Perl; +use DateTime::Duration; +use Test::More tests => 113; +use Test::Warn; + use t::lib::Mocks; +use t::lib::TestBuilder; + use C4::Context; -use C4::Members; use C4::Letters; -use C4::Budgets qw( AddBudgetPeriod AddBudget ); - -use Koha::DateUtils qw( dt_from_string ); -use Koha::Library; +use C4::Budgets qw( AddBudgetPeriod AddBudget GetBudget ); +use Koha::Database; +use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Libraries; - -use DateTime::Duration; -use Test::More tests => 102; -use Test::Warn; +use Koha::Patrons; +use Koha::Suggestions; BEGIN { use_ok('C4::Suggestions'); } +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; my $dbh = C4::Context->dbh; -my $sql; +my $builder = t::lib::TestBuilder->new; -# Start transaction -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; +t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", "0"); # Reset item types to only the default ones $dbh->do(q|DELETE FROM itemtypes;|); -$sql = " +my $sql = qq| INSERT INTO itemtypes (itemtype, description, rentalcharge, notforloan, imageurl, summary) VALUES ('BK', 'Books',5,0,'bridge/book.gif',''), ('MX', 'Mixed Materials',5,0,'bridge/kit.gif',''), @@ -53,7 +55,7 @@ INSERT INTO itemtypes (itemtype, description, rentalcharge, notforloan, imageurl ('VM', 'Visual Materials',5,1,'bridge/dvd.gif',''), ('MU', 'Music',5,0,'bridge/sound.gif',''), ('CR', 'Continuing Resources',5,0,'bridge/periodical.gif',''), -('REF', 'Reference',0,1,'bridge/reference.gif','');"; +('REF', 'Reference',0,1,'bridge/reference.gif','');|; $dbh->do($sql); $dbh->do(q|DELETE FROM suggestions|); $dbh->do(q|DELETE FROM issues|); @@ -61,45 +63,47 @@ $dbh->do(q|DELETE FROM borrowers|); $dbh->do(q|DELETE FROM letter|); $dbh->do(q|DELETE FROM message_queue|); $dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'CHECKED', 'my content')|); +$dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'NEW_SUGGESTION', 'Content for new suggestion')|); # Add CPL if missing. if (not defined Koha::Libraries->find('CPL')) { Koha::Library->new({ branchcode => 'CPL', branchname => 'Centerville' })->store; } -my $sth = $dbh->prepare("SELECT * FROM categories WHERE categorycode='S';"); -$sth->execute(); -if (!$sth->fetchrow_hashref) { - $sql = "INSERT INTO categories - (categorycode,description,enrolmentperiod,upperagelimit, - dateofbirthrequired,finetype,bulk,enrolmentfee, - overduenoticerequired,issuelimit,reservefee,category_type) - VALUES - ('S','Staff',99,999, - 18,NULL,NULL,'0.000000', - 0,NULL,'0.000000','S');"; - $dbh->do($sql); -} +my $patron_category = $builder->build({ source => 'Category' }); my $member = { firstname => 'my firstname', surname => 'my surname', - categorycode => 'S', + categorycode => $patron_category->{categorycode}, + branchcode => 'CPL', + smsalertnumber => 12345, +}; + +my $member2 = { + firstname => 'my firstname', + surname => 'my surname', + categorycode => $patron_category->{categorycode}, branchcode => 'CPL', + email => 'to@example.com', }; -my $borrowernumber = AddMember(%$member); -my $biblionumber1 = 1; +my $borrowernumber = Koha::Patron->new($member)->store->borrowernumber; +my $borrowernumber2 = Koha::Patron->new($member2)->store->borrowernumber; + +my $biblio_1 = $builder->build_object({ class => 'Koha::Biblios' }); my $my_suggestion = { title => 'my title', author => 'my author', publishercode => 'my publishercode', suggestedby => $borrowernumber, - biblionumber => $biblionumber1, + biblionumber => $biblio_1->biblionumber, + branchcode => 'CPL', managedby => '', manageddate => '', accepteddate => dt_from_string, note => 'my note', + quantity => '', # Insert an empty string into int to catch strict SQL modes errors }; my $budgetperiod_id = AddBudgetPeriod({ @@ -121,14 +125,26 @@ my $my_suggestion_with_budget = { author => 'my author 2', publishercode => 'my publishercode 2', suggestedby => $borrowernumber, - biblionumber => $biblionumber1, + branchcode => '', # This should not fail be set to undef instead + biblionumber => $biblio_1->biblionumber, + managedby => '', + manageddate => '', + accepteddate => dt_from_string, + note => 'my note', + budgetid => $budget_id, +}; +my $my_suggestion_with_budget2 = { + title => 'my title 3', + author => 'my author 3', + publishercode => 'my publishercode 3', + suggestedby => $borrowernumber2, + biblionumber => $biblio_1->biblionumber, managedby => '', manageddate => '', accepteddate => dt_from_string, note => 'my note', budgetid => $budget_id, }; - is( CountSuggestion(), 0, 'CountSuggestion without the status returns 0' ); is( CountSuggestion('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' ); @@ -179,7 +195,7 @@ isnt( $suggestion->{accepteddate}, undef, 'ModSuggestion does not update a non g is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not given' ); my $messages = C4::Letters::GetQueuedMessages({ - borrowernumber => $borrowernumber, + borrowernumber => $borrowernumber }); is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' ); @@ -196,18 +212,60 @@ my $mod_suggestion3 = { STATUS => 'CHECKED', suggestionid => $my_suggestionid, }; -$status = ModSuggestion($mod_suggestion3); +#Test the message_transport_type of suggestion notices + +#Check the message_transport_type when the 'FallbackToSMSIfNoEmail' syspref is disabled +t::lib::Mocks::mock_preference( 'FallbackToSMSIfNoEmail', 0 ); +$status = ModSuggestion($mod_suggestion3); is( $status, 1, 'ModSuggestion modifies one entry' ); $suggestion = GetSuggestion($my_suggestionid); is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' ); $messages = C4::Letters::GetQueuedMessages({ - borrowernumber => $borrowernumber, + borrowernumber => $borrowernumber }); is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' ); - +is ($messages->[0]->{message_transport_type}, 'email', 'When FallbackToSMSIfNoEmail syspref is disabled the suggestion message_transport_type is always email'); is( CountSuggestion('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' ); +#Check the message_transport_type when the 'FallbackToSMSIfNoEmail' syspref is enabled and the borrower has a smsalertnumber and no email +t::lib::Mocks::mock_preference( 'FallbackToSMSIfNoEmail', 1 ); +ModSuggestion($mod_suggestion3); +$messages = C4::Letters::GetQueuedMessages({ + borrowernumber => $borrowernumber +}); +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'); + +#Make a new suggestion for a borrower with defined email and no smsalertnumber +my $my_suggestion_2_id = NewSuggestion($my_suggestion_with_budget2); + +#Check the message_transport_type when the 'FallbackToSMSIfNoEmail' syspref is enabled and the borrower has a defined email and no smsalertnumber +t::lib::Mocks::mock_preference( 'FallbackToSMSIfNoEmail', 1 ); +my $mod_suggestion4 = { + STATUS => 'CHECKED', + suggestionid => $my_suggestion_2_id, +}; +ModSuggestion($mod_suggestion4); +$messages = C4::Letters::GetQueuedMessages({ + borrowernumber => $borrowernumber2 +}); +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'); + +{ + # Hiding the expected warning displayed by DBI + # DBD::mysql::st execute failed: Incorrect date value: 'invalid date!' for column 'manageddate' + local *STDERR; + open STDERR, '>', '/dev/null'; + + $mod_suggestion4->{manageddate} = 'invalid date!'; + ModSuggestion($mod_suggestion4); + $messages = C4::Letters::GetQueuedMessages({ + borrowernumber => $borrowernumber2 + }); + + close STDERR; + is (scalar(@$messages), 1, 'No new letter should have been generated if the update raised an error'); +} is( GetSuggestionInfo(), undef, 'GetSuggestionInfo without the suggestion id returns undef' ); $suggestion = GetSuggestionInfo($my_suggestionid); @@ -225,12 +283,12 @@ is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSugges is( GetSuggestionFromBiblionumber(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' ); is( GetSuggestionFromBiblionumber(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' ); -is( GetSuggestionFromBiblionumber($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' ); +is( GetSuggestionFromBiblionumber($biblio_1->biblionumber), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' ); is( GetSuggestionInfoFromBiblionumber(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' ); is( GetSuggestionInfoFromBiblionumber(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' ); -$suggestion = GetSuggestionInfoFromBiblionumber($biblionumber1); +$suggestion = GetSuggestionInfoFromBiblionumber($biblio_1->biblionumber); is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' ); is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' ); is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' ); @@ -246,7 +304,7 @@ is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSugges my $suggestions = GetSuggestionByStatus(); is( @$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' ); $suggestions = GetSuggestionByStatus('CHECKED'); -is( @$suggestions, 1, 'GetSuggestionByStatus returns the correct number of suggestions' ); +is( @$suggestions, 2, 'GetSuggestionByStatus returns the correct number of suggestions' ); is( $suggestions->[0]->{suggestionid}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' ); is( $suggestions->[0]->{title}, $mod_suggestion1->{title}, 'GetSuggestionByStatus returns the title correctly' ); is( $suggestions->[0]->{author}, $mod_suggestion1->{author}, 'GetSuggestionByStatus returns the author correctly' ); @@ -262,14 +320,14 @@ is( $suggestions->[0]->{categorycodesuggestedby}, $member->{categorycode}, 'GetS is( ConnectSuggestionAndBiblio(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' ); -my $biblionumber2 = 2; -my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio($my_suggestionid, $biblionumber2); +my $biblio_2 = $builder->build_object({ class => 'Koha::Biblios' }); +my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio($my_suggestionid, $biblio_2->biblionumber); is( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' ); $suggestion = GetSuggestion($my_suggestionid); -is( $suggestion->{biblionumber}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' ); +is( $suggestion->{biblionumber}, $biblio_2->biblionumber, 'ConnectSuggestionAndBiblio updates the biblio number correctly' ); my $search_suggestion = SearchSuggestion(); -is( @$search_suggestion, 2, 'SearchSuggestion without arguments returns all suggestions' ); +is( @$search_suggestion, 3, 'SearchSuggestion without arguments returns all suggestions' ); $search_suggestion = SearchSuggestion({ title => $mod_suggestion1->{title}, @@ -301,7 +359,7 @@ is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of sugg $search_suggestion = SearchSuggestion({ STATUS => $mod_suggestion3->{STATUS}, }); -is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' ); +is( @$search_suggestion, 2, 'SearchSuggestion returns the correct number of suggestions' ); $search_suggestion = SearchSuggestion({ STATUS => q|| @@ -315,11 +373,11 @@ is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of sugg $search_suggestion = SearchSuggestion({ budgetid => '', }); -is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' ); +is( @$search_suggestion, 3, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' ); $search_suggestion = SearchSuggestion({ budgetid => $budget_id, }); -is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' ); +is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' ); $search_suggestion = SearchSuggestion({ budgetid => '__NONE__', }); @@ -327,7 +385,13 @@ is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns th $search_suggestion = SearchSuggestion({ budgetid => '__ANY__', }); -is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' ); +is( @$search_suggestion, 3, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' ); + +$search_suggestion = SearchSuggestion({ budgetid => $budget_id }); +is( @$search_suggestion[0]->{budget_name}, GetBudget($budget_id)->{budget_name}, 'SearchSuggestion returns the correct budget name'); +$search_suggestion = SearchSuggestion({ budgetid => "__NONE__" }); +is( @$search_suggestion[0]->{budget_name}, undef, 'SearchSuggestion returns the correct budget name'); + my $del_suggestion = { title => 'my deleted title', @@ -336,7 +400,7 @@ my $del_suggestion = { }; my $del_suggestionid = NewSuggestion($del_suggestion); -is( CountSuggestion('CHECKED'), 2, 'CountSuggestion returns the correct number of suggestions' ); +is( CountSuggestion('CHECKED'), 3, 'CountSuggestion returns the correct number of suggestions' ); is( DelSuggestion(), '0E0', 'DelSuggestion without arguments returns 0E0' ); is( DelSuggestion($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' ); @@ -345,8 +409,8 @@ $suggestion = DelSuggestion($borrowernumber, $my_suggestionid); is( $suggestion, 1, 'DelSuggestion deletes one suggestion' ); $suggestions = GetSuggestionByStatus('CHECKED'); -is( @$suggestions, 1, 'DelSuggestion deletes one suggestion' ); -is( $suggestions->[0]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' ); +is( @$suggestions, 2, 'DelSuggestion deletes one suggestion' ); +is( $suggestions->[1]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' ); # Test budgetid fk $my_suggestion->{budgetid} = ''; # If budgetid == '', NULL should be set in DB @@ -359,6 +423,24 @@ ModSuggestion( $my_suggestion ); $suggestion = GetSuggestion($my_suggestionid_test_budgetid); is( $suggestion->{budgetid}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' ); +my $suggestion2 = { + title => "Cuisine d'automne", + author => "Catherine", + itemtype => "LIV" +}; + +my $record = MarcRecordFromNewSuggestion($suggestion2); + +is("MARC::Record", ref($record), "MarcRecordFromNewSuggestion should return a MARC::Record object"); + +my ($title_tag, $title_subfield) = C4::Biblio::GetMarcFromKohaField('biblio.title', ''); + +is($record->field( $title_tag )->subfield( $title_subfield ), "Cuisine d'automne", "Record from suggestion title should be 'Cuisine d'automne'"); + +my ($author_tag, $author_subfield) = C4::Biblio::GetMarcFromKohaField('biblio.author', ''); + +is($record->field( $author_tag )->subfield( $author_subfield ), "Catherine", "Record from suggestion author should be 'Catherine'"); + subtest 'GetUnprocessedSuggestions' => sub { plan tests => 11; $dbh->do(q|DELETE FROM suggestions|); @@ -393,3 +475,156 @@ subtest 'GetUnprocessedSuggestions' => sub { $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(5); is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 5 days ago' ); }; + +subtest 'DelSuggestionsOlderThan' => sub { + plan tests => 6; + + Koha::Suggestions->delete; + + # Add four suggestions; note that STATUS needs uppercase (FIXME) + my $d1 = output_pref({ dt => dt_from_string->add(days => -2), dateformat => 'sql' }); + my $d2 = output_pref({ dt => dt_from_string->add(days => -4), dateformat => 'sql' }); + my $sugg01 = $builder->build({ source => 'Suggestion', value => { date => $d1, STATUS => 'ASKED' }}); + my $sugg02 = $builder->build({ source => 'Suggestion', value => { date => $d1, STATUS => 'CHECKED' }}); + my $sugg03 = $builder->build({ source => 'Suggestion', value => { date => $d2, STATUS => 'ASKED' }}); + my $sugg04 = $builder->build({ source => 'Suggestion', value => { date => $d2, STATUS => 'ACCEPTED' }}); + + # Test no parameter: should do nothing + C4::Suggestions::DelSuggestionsOlderThan(); + is( Koha::Suggestions->count, 4, 'No suggestions deleted' ); + # Test zero: should do nothing too + C4::Suggestions::DelSuggestionsOlderThan(0); + is( Koha::Suggestions->count, 4, 'No suggestions deleted again' ); + # Test negative value + C4::Suggestions::DelSuggestionsOlderThan(-1); + is( Koha::Suggestions->count, 4, 'No suggestions deleted for -1' ); + + # Test positive values + C4::Suggestions::DelSuggestionsOlderThan(5); + is( Koha::Suggestions->count, 4, 'No suggestions>5d deleted' ); + C4::Suggestions::DelSuggestionsOlderThan(3); + is( Koha::Suggestions->count, 3, '1 suggestions>3d deleted' ); + C4::Suggestions::DelSuggestionsOlderThan(1); + is( Koha::Suggestions->count, 2, '1 suggestions>1d deleted' ); +}; + +subtest 'EmailPurchaseSuggestions' => sub { + plan tests => 11; + + $dbh->do(q|DELETE FROM message_queue|); + + t::lib::Mocks::mock_preference( "KohaAdminEmailAddress", + 'noreply@hosting.com' ); + + # EmailPurchaseSuggestions set to disabled + t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", "0" ); + NewSuggestion($my_suggestion); + my $newsuggestions_messages = C4::Letters::GetQueuedMessages( + { + borrowernumber => $borrowernumber + } + ); + is( @$newsuggestions_messages, 0, + 'NewSuggestions does not send an email when disabled' ); + + # EmailPurchaseSuggestions set to BranchEmailAddress + t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", + "BranchEmailAddress" ); + NewSuggestion($my_suggestion); + + t::lib::Mocks::mock_preference( "ReplytoDefault", 'library@b.c' ); + NewSuggestion($my_suggestion); + + Koha::Libraries->find('CPL')->update( { branchemail => 'branchemail@hosting.com' } ); + NewSuggestion($my_suggestion); + + Koha::Libraries->find('CPL')->update( { branchreplyto => 'branchemail@b.c' } ); + NewSuggestion($my_suggestion); + + $newsuggestions_messages = C4::Letters::GetQueuedMessages( + { + borrowernumber => $borrowernumber + } + ); + isnt( @$newsuggestions_messages, 0, 'NewSuggestions sends an email' ); + my $message1 = + C4::Letters::GetMessage( $newsuggestions_messages->[0]->{message_id} ); + is( $message1->{to_address}, 'noreply@hosting.com', +'BranchEmailAddress falls back to KohaAdminEmailAddress if branchreplyto, branchemail and ReplytoDefault are not set' + ); + my $message2 = + C4::Letters::GetMessage( $newsuggestions_messages->[1]->{message_id} ); + is( $message2->{to_address}, 'library@b.c', +'BranchEmailAddress falls back to ReplytoDefault if neither branchreplyto or branchemail are set' + ); + my $message3 = + C4::Letters::GetMessage( $newsuggestions_messages->[2]->{message_id} ); + is( $message3->{to_address}, 'branchemail@hosting.com', +'BranchEmailAddress uses branchemail if branch_replto is not set' + ); + my $message4 = + C4::Letters::GetMessage( $newsuggestions_messages->[3]->{message_id} ); + is( $message4->{to_address}, 'branchemail@b.c', +'BranchEmailAddress uses branchreplyto in preference to branchemail when set' + ); + + # EmailPurchaseSuggestions set to KohaAdminEmailAddress + t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", + "KohaAdminEmailAddress" ); + + t::lib::Mocks::mock_preference( "ReplytoDefault", undef ); + NewSuggestion($my_suggestion); + + t::lib::Mocks::mock_preference( "ReplytoDefault", 'library@b.c' ); + NewSuggestion($my_suggestion); + + $newsuggestions_messages = C4::Letters::GetQueuedMessages( + { + borrowernumber => $borrowernumber + } + ); + my $message5 = + C4::Letters::GetMessage( $newsuggestions_messages->[4]->{message_id} ); + is( $message5->{to_address}, + 'noreply@hosting.com', 'KohaAdminEmailAddress uses KohaAdminEmailAddress when ReplytoDefault is not set' ); + my $message6 = + C4::Letters::GetMessage( $newsuggestions_messages->[5]->{message_id} ); + is( $message6->{to_address}, + 'library@b.c', 'KohaAdminEmailAddress uses ReplytoDefualt when ReplytoDefault is set' ); + + # EmailPurchaseSuggestions set to EmailAddressForSuggestions + t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", + "EmailAddressForSuggestions" ); + + t::lib::Mocks::mock_preference( "ReplytoDefault", undef ); + NewSuggestion($my_suggestion); + + t::lib::Mocks::mock_preference( "ReplytoDefault", 'library@b.c' ); + NewSuggestion($my_suggestion); + + t::lib::Mocks::mock_preference( "EmailAddressForSuggestions", + 'suggestions@b.c' ); + NewSuggestion($my_suggestion); + + $newsuggestions_messages = C4::Letters::GetQueuedMessages( + { + borrowernumber => $borrowernumber + } + ); + my $message7 = + C4::Letters::GetMessage( $newsuggestions_messages->[6]->{message_id} ); + is( $message7->{to_address}, + 'noreply@hosting.com', 'EmailAddressForSuggestions uses KohaAdminEmailAddress when neither EmailAddressForSuggestions or ReplytoDefault are set' ); + + my $message8 = + C4::Letters::GetMessage( $newsuggestions_messages->[7]->{message_id} ); + is( $message8->{to_address}, + 'library@b.c', 'EmailAddressForSuggestions uses ReplytoDefault when EmailAddressForSuggestions is not set' ); + + my $message9 = + C4::Letters::GetMessage( $newsuggestions_messages->[8]->{message_id} ); + is( $message9->{to_address}, + 'suggestions@b.c', 'EmailAddressForSuggestions uses EmailAddressForSuggestions when set' ); +}; + +$schema->storage->txn_rollback;