From 1af7c1693b2c0416f12a3e4e7677415851cc678d Mon Sep 17 00:00:00 2001
From: Henri-Damien LAURENT <hdlaurent@afi-sa.fr>
Date: Wed, 1 Mar 2023 23:04:51 +0100
Subject: [PATCH] hotline#173260 : newletters : fixing wrong error message

---
 VERSIONS_HOTLINE/173260                                   | 1 +
 .../modules/admin/controllers/NewsletterController.php    | 7 +++++--
 library/Class/Batch/SendNewsletters.php                   | 5 +++++
 library/Class/Newsletter.php                              | 7 ++++---
 .../admin/controllers/NewsletterControllerTest.php        | 8 +++++++-
 5 files changed, 22 insertions(+), 6 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/173260

diff --git a/VERSIONS_HOTLINE/173260 b/VERSIONS_HOTLINE/173260
new file mode 100644
index 00000000000..5ea50e42ee0
--- /dev/null
+++ b/VERSIONS_HOTLINE/173260
@@ -0,0 +1 @@
+  - hotline#173260 : Newsletter : Le message à la fin de l'envoi des lettres d'information présentait une fausse erreur au lancement de la commande
\ No newline at end of file
diff --git a/application/modules/admin/controllers/NewsletterController.php b/application/modules/admin/controllers/NewsletterController.php
index a1de4d39dfb..15f68bcc35d 100644
--- a/application/modules/admin/controllers/NewsletterController.php
+++ b/application/modules/admin/controllers/NewsletterController.php
@@ -35,9 +35,12 @@ class Admin_NewsletterController extends ZendAfi_Controller_Action {
       return $this->_redirectToIndex();
     }
 
-    if (!$newsletter->send())
-      $this->_helper->notify($this->_('Envoi impossible : erreur à la création de la commande d\'envoi'));
+    if ($error = $newsletter->send()){
+      $this->_helper->notify($this->_("Envoi impossible : erreur à la création de la commande d'envoi : %s", $error));
+      return $this->_redirectToIndex();
+    }
 
+    $this->_helper->notify($this->_("Lettre d'information %s envoyée", $newsletter->getTitre()));
     $this->_redirectToIndex();
   }
 
diff --git a/library/Class/Batch/SendNewsletters.php b/library/Class/Batch/SendNewsletters.php
index ff0b536ed77..65a74cc8b8e 100644
--- a/library/Class/Batch/SendNewsletters.php
+++ b/library/Class/Batch/SendNewsletters.php
@@ -84,6 +84,11 @@ class Class_Batch_SendNewsletters extends Class_Batch_Abstract {
   }
 
 
+  public function getError() : string {
+    return $this->_dispatch->getErrorMessage() ?? '';
+  }
+
+
   public function sendOne(string $email) : void {
     if (!$this->_dispatch)
       return;
diff --git a/library/Class/Newsletter.php b/library/Class/Newsletter.php
index cd709843e62..bc6abac77f6 100644
--- a/library/Class/Newsletter.php
+++ b/library/Class/Newsletter.php
@@ -149,9 +149,10 @@ class Class_Newsletter extends Storm_Model_Abstract {
   }
 
 
-  public function send() {
-    return (new Class_Batch_SendNewsletters($this->_getOrCreateDispatchToRun()))
-      ->run();
+  public function send() :string {
+    $batch = new Class_Batch_SendNewsletters($this->_getOrCreateDispatchToRun());
+    $batch->run();
+    return $batch->getError();
   }
 
 
diff --git a/tests/application/modules/admin/controllers/NewsletterControllerTest.php b/tests/application/modules/admin/controllers/NewsletterControllerTest.php
index 7d27958da7c..a27e28b74c8 100644
--- a/tests/application/modules/admin/controllers/NewsletterControllerTest.php
+++ b/tests/application/modules/admin/controllers/NewsletterControllerTest.php
@@ -817,6 +817,12 @@ Lien pour se désinscrire de cette lettre d\'information : http://localhost'. BA
   public function shouldRedirectToNewsletterIndex() {
     $this->assertRedirectTo('/admin/newsletter/index');
   }
+
+
+  /** @test */
+  public function shouldNotifyCommand() {
+    $this->assertFlashMessengerContentContains('Lettre d\'information Animations envoyée');
+  }
 }
 
 
@@ -850,7 +856,7 @@ class Admin_NewsletterControllerSendActionWithCommandFailureTest extends Admin_N
 
   /** @test */
   public function shouldNotifyCommandError() {
-    $this->assertFlashMessengerContentContains('Envoi impossible : erreur à la création de la commande d\'envoi');
+    $this->assertFlashMessengerContentContains("Envoi impossible : erreur à la création de la commande d'envoi : Unable to run php -f");
   }
 
 
-- 
GitLab