From 16f7204e085b1e445e07dab717c3794e69f7e34b Mon Sep 17 00:00:00 2001
From: Matthias Le Gac <matthias.le-gac@inlibro.com>
Date: Fri, 8 Mar 2024 16:44:51 -0500
Subject: [PATCH] Bug 36122: Add unit test

Signed-off-by: Roman Dolny <roman.dolny@jezuici.pl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 t/db_dependent/Koha/Suggestions.t | 42 ++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/Koha/Suggestions.t b/t/db_dependent/Koha/Suggestions.t
index 7c2e443458e..467e207a920 100755
--- a/t/db_dependent/Koha/Suggestions.t
+++ b/t/db_dependent/Koha/Suggestions.t
@@ -23,6 +23,8 @@ use Test::More tests => 11;
 use Test::Exception;
 
 use Koha::Suggestions;
+use Koha::Config::SysPrefs;
+use Koha::Notice::Messages;
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string output_pref );
 
@@ -49,7 +51,7 @@ my $new_suggestion_2 = Koha::Suggestion->new(
 )->store;
 
 subtest 'store' => sub {
-    plan tests => 5;
+    plan tests => 8;
     my $suggestion  = Koha::Suggestion->new(
         {   suggestedby  => $patron->{borrowernumber},
             biblionumber => $biblio_1->biblionumber,
@@ -66,6 +68,44 @@ subtest 'store' => sub {
     $suggestion = Koha::Suggestions->find( $suggestion->suggestionid );
     is( $suggestion->suggesteddate, $two_days_ago_sql, 'If suggestion id modified, suggesteddate should not be modified' );
 
+    my $syspref = Koha::Config::SysPrefs->find('EmailPurchaseSuggestions');
+    $syspref->value(0)->store;
+    Koha::Notice::Messages->search->delete;
+    $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";
+    }
+    is(
+        $last_message, 'No message was sent',
+        'If EmailPurchaseSuggestions is not enabled, a message should not be sent'
+    );
+
+    $syspref = Koha::Config::SysPrefs->find('EmailPurchaseSuggestions');
+    $syspref->value('EmailAddressForSuggestions')->store;
+    Koha::Notice::Messages->search->delete;
+    $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'
+        );
+    }
+
+    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";
+    }
+    is(
+        $last_message, 'No message was sent',
+        'If the status of suggestion is not set to ASKED, a message should not be sent'
+    );
+
     throws_ok {
         $suggestion->STATUS('UNKNOWN')->store;
     }
-- 
2.44.0