From 08a4fca166c20b28871bdb04c891fb96c9d5903f Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 28 May 2024 12:28:18 +0200 Subject: [PATCH] Bug 36820: Adjust and add tests --- t/db_dependent/Koha/Suggestions.t | 38 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/t/db_dependent/Koha/Suggestions.t b/t/db_dependent/Koha/Suggestions.t index 1254248e43d..784aa7355de 100755 --- a/t/db_dependent/Koha/Suggestions.t +++ b/t/db_dependent/Koha/Suggestions.t @@ -50,7 +50,7 @@ my $new_suggestion_2 = Koha::Suggestion->new( )->store; subtest 'store' => sub { - plan tests => 8; + plan tests => 9; my $suggestion = Koha::Suggestion->new( { suggestedby => $patron->{borrowernumber}, biblionumber => $biblio_1->biblionumber, @@ -69,37 +69,35 @@ subtest 'store' => sub { t::lib::Mocks::mock_preference( 'EmailPurchaseSuggestions', 0 ); Koha::Notice::Messages->search->delete; + $suggestion->STATUS('REJECTED')->store; $suggestion->STATUS('ASKED')->store; - my $last_message = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } )->single; - if ( !defined $last_message ) { - $last_message = "No message was sent"; - } + my $messages_sent = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } ); is( - $last_message, 'No message was sent', + $messages_sent->count, 1, 'If EmailPurchaseSuggestions is not enabled, a message should not be sent' ); t::lib::Mocks::mock_preference( 'EmailPurchaseSuggestions', 'EmailAddressForSuggestions' ); Koha::Notice::Messages->search->delete; + $suggestion->STATUS('REJECTED')->store; $suggestion->STATUS('ASKED')->store; - $last_message = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } )->single; - if ( !defined $last_message ) { - fail('No message was sent'); - } else { - is( - $last_message->letter_code, 'NEW_SUGGESTION', - 'If EmailPurchaseSuggestions is enabled and the status of suggestion is set to ASKED, a message should be sent' - ); - } + $messages_sent = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } ); + is( $messages_sent->count, 1, 'If EmailPurchaseSuggestions is enabled and the status of suggestion is set to ASKED, a message should be sent' ); + is( + $messages_sent->next->letter_code, 'NEW_SUGGESTION', + 'If EmailPurchaseSuggestions is enabled and the status of suggestion is set to ASKED, a message should be sent' + ); + + Koha::Notice::Messages->search->delete; + $suggestion->set( { note => 'a note' } )->store; + $messages_sent = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } ); + is( $messages_sent->count, 1, 'NEW_SUGGESTION should not be sent again if status has not changed' ); Koha::Notice::Messages->search->delete; $suggestion->STATUS('ACCEPTED')->store; - $last_message = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } )->single; - if ( !defined $last_message ) { - $last_message = "No message was sent"; - } + $messages_sent = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } ); is( - $last_message, 'No message was sent', + $messages_sent->count, 0, 'If the status of suggestion is not set to ASKED, a message should not be sent' ); -- 2.34.1