From cc221289507f43a9e252282d81e1a494784419df Mon Sep 17 00:00:00 2001
From: Henri-Damien LAURENT <hdlaurent@afi-sa.net>
Date: Mon, 25 Sep 2023 07:30:32 +0000
Subject: [PATCH] dev#160166 : Widget always send results Should be empty when
 no data selected

---
 library/Class/Article/MailRenderer.php        |  2 +
 library/Class/Notice/MailRenderer.php         |  2 +-
 library/ZendAfi/Form/Admin/Newsletter.php     | 55 +++++++++---------
 .../controllers/NewsletterControllerTest.php  | 58 +++++++++++++------
 4 files changed, 72 insertions(+), 45 deletions(-)

diff --git a/library/Class/Article/MailRenderer.php b/library/Class/Article/MailRenderer.php
index d03963c5040..d31c1a14803 100644
--- a/library/Class/Article/MailRenderer.php
+++ b/library/Class/Article/MailRenderer.php
@@ -46,6 +46,8 @@ class Class_Article_MailRenderer {
 
 
   public function renderWithWidget(Class_Newsletter $newsletter) :string {
+    if (($newsletter->getArticlesIds() == 0) && ($newsletter->getArticlesCategoriesIds() ==0))
+      return '';
 
     $view = (new ZendAfi_Controller_Action_Helper_View)->init();
     $widget = (new Class_Systeme_Widget_Widget)
diff --git a/library/Class/Notice/MailRenderer.php b/library/Class/Notice/MailRenderer.php
index fb0ea78ed09..6b6315d1390 100644
--- a/library/Class/Notice/MailRenderer.php
+++ b/library/Class/Notice/MailRenderer.php
@@ -51,7 +51,7 @@ class Class_Notice_MailRenderer {
 
 
   public function renderWithWidget(Class_Newsletter $newsletter) :string {
-    if (! ((bool)$newsletter->getIdCatalogue() && (bool)$newsletter->getIdPanier()))
+    if (($newsletter->getIdCatalogue() == 0) && ($newsletter->getIdPanier() ==0))
       return '';
 
     $view = (new ZendAfi_Controller_Action_Helper_View)->init();
diff --git a/library/ZendAfi/Form/Admin/Newsletter.php b/library/ZendAfi/Form/Admin/Newsletter.php
index 49b54c2290f..c7b14114585 100644
--- a/library/ZendAfi/Form/Admin/Newsletter.php
+++ b/library/ZendAfi/Form/Admin/Newsletter.php
@@ -46,6 +46,32 @@ class ZendAfi_Form_Admin_Newsletter extends ZendAfi_Form {
          ->addElement('checkbox', 'draft',
                       ['label' => $this->_('Brouillon ?')])
 
+         ->addElement('radio',
+                      'display_full_article',
+                      ['label' => $this->_('Afficher articles en intégralité'),
+                       'value' => $datas['display_full_article'] ?? 0,
+                       'separator' => '',
+                       'multiOptions' => [ '1' => $this->_('Oui'),
+                                           '0' => $this->_('Non')
+                       ]
+                      ])
+         ->addElement('radio',
+                      'render_with_widget',
+                      ['label' => $this->_('Afficher comme sur le site'),
+                       'value' => $datas['render_with_widget'] ?? 0,
+                       'separator' => '',
+                       'multiOptions' => [ '1' => $this->_('Oui'),
+                                           '0' => $this->_('Non')
+                       ]
+
+                      ])
+
+         ->addElement('userfile',
+                      'custom_css',
+                      ['label' => $this->_('Css personnalisée'),
+                       'value' => $datas['custom_css'] ?? '',
+                      ])
+
          ->addElement('ckeditor', 'contenu',
                       ['required' => true,
                        'allowEmpty' => false])
@@ -69,7 +95,7 @@ class ZendAfi_Form_Admin_Newsletter extends ZendAfi_Form {
                        'validators' => [new Zend_Validate_Int()]])
 
 
-         ->addDisplayGroup(['titre', 'mail_subject', 'expediteur', 'draft'],
+         ->addDisplayGroup(['titre', 'mail_subject', 'expediteur', 'draft', 'render_with_widget','display_full_article', 'custom_css'],
                            'letter',
                            ['legend' => $this->_('Lettre')])
 
@@ -87,31 +113,6 @@ class ZendAfi_Form_Admin_Newsletter extends ZendAfi_Form {
     parent::populate($datas);
 
     $this
-      ->addElement('radio',
-                   'display_full_article',
-                   ['label' => $this->_('Afficher articles en intégralité'),
-                    'value' => $datas['display_full_article'] ?? 0,
-                    'separator' => '',
-                    'multiOptions' => [ '1' => $this->_('Oui'),
-                                        '0' => $this->_('Non')
-                    ]
-                   ])
-      ->addElement('radio',
-                   'render_with_widget',
-                   ['label' => $this->_('Afficher comme sur le site'),
-                    'value' => $datas['render_with_widget'] ?? 0,
-                    'separator' => '',
-                    'multiOptions' => [ '1' => $this->_('Oui'),
-                                        '0' => $this->_('Non')
-                    ]
-
-                   ])
-
-      ->addElement('userfile',
-                   'custom_css',
-                   ['label' => $this->_('Css personnalisée'),
-                    'value' => $datas['custom_css'] ?? '',
-                   ])
 
       ->addElement('treeSelect',
                    'articles_selector',
@@ -137,7 +138,7 @@ class ZendAfi_Form_Admin_Newsletter extends ZendAfi_Form {
 
                     ]);
 
-    $this->addDisplayGroup(['render_with_widget','display_full_article','custom_css','articles_selector'],
+    $this->addDisplayGroup(['articles_selector'],
                            'articles',
                            ['legend' => $this->_('Articles')]);
     return $this;
diff --git a/tests/application/modules/admin/controllers/NewsletterControllerTest.php b/tests/application/modules/admin/controllers/NewsletterControllerTest.php
index 3de5b87ad51..147fab42c6a 100644
--- a/tests/application/modules/admin/controllers/NewsletterControllerTest.php
+++ b/tests/application/modules/admin/controllers/NewsletterControllerTest.php
@@ -1029,6 +1029,18 @@ abstract class Admin_NewsletterControllerPreviewActionPlaceHoldersTestCase exten
 
   protected function _customizeNewsletter(Class_Newsletter $newsletter){
   }
+
+
+  /** @test */
+  public function placeholderNewsletterRecordsShouldHaveBeenRemoved() {
+    $this->assertNotXPathContentContains('//body', '[newsletter_records]');
+  }
+
+
+  /** @test */
+  public function placeholderNewsletterArticlesShouldHaveBeenRemoved() {
+    $this->assertNotXPathContentContains('//body', '[newsletter_articles]');
+  }
 }
 
 
@@ -1049,61 +1061,72 @@ class Admin_NewsletterControllerPreviewActionWithArticlesSelectionAndPlaceHolder
 
 
   /** @test */
-  public function placeholderNewsletterRecordsShouldHaveBeenRemoved() {
-    $this->assertNotXPathContentContains('//body', '[newsletter_records]');
+  public function contentTextShouldBeAsExpected() {
+    $this->assertContains( $this->_expectedText(), $this->_response->getBody());
   }
 
 
   /** @test */
-  public function placeholderNewsletterArticlesShouldHaveBeenRemoved() {
-    $this->assertNotXPathContentContains('//body', '[newsletter_articles]');
+  public function contentHtmlShouldBeAsExpected() {
+    $this->assertContains( $this->_expectedHtml(), $this->_response->getBody());
   }
+}
 
 
-  /** @test */
-  public function contentTextShouldBeAsExpected() {
-    $this->assertContains( utf8_encode($this->_expectedText()), $this->_response->getBody());
+
+class Admin_NewsletterControllerPreviewActionWithPlaceHoldersWithoutCatalogueTest
+  extends Admin_NewsletterControllerPreviewActionPlaceHoldersTestCase {
+
+  protected function _customizeNewsletter(Class_Newsletter $newsletter) {
+    $newsletter
+      ->setIdCatalogue(0);
+  }
+
+
+  protected function _expectedText() {
+    return file_get_contents(__DIR__.'/newsletter_placeholder_content_without_catalogue_expected.txt');
   }
 
 
   /** @test */
-  public function contentHtmlShouldBeAsExpected() {
-    $this->assertContains( utf8_encode($this->_expectedHtml()), $this->_response->getBody());
+  public function pageShouldContainsExpectedText() {
+    $this->assertContains($this->_expectedText(), $this->_response->getBody());
   }
 }
 
 
 
-class Admin_NewsletterControllerPreviewActionWithArticlesSelectionAndPlaceHoldersWithoutCatalogueTest
+
+class Admin_NewsletterControllerPreviewActionWithPlaceholdersWithoutArticlesTest
   extends Admin_NewsletterControllerPreviewActionPlaceHoldersTestCase {
 
   protected function _customizeNewsletter(Class_Newsletter $newsletter) {
     $newsletter
-      ->setIdCatalogue(0);
+      ->setArticlesIds(0)
+      ->setArticlesCategoriesIds(0);
   }
 
 
   protected function _expectedText() {
-    return file_get_contents(__DIR__.'/newsletter_placeholder_content_without_catalogue_expected.txt');
+    return file_get_contents(__DIR__.'/newsletter_placeholder_content_without_articles.txt');
   }
 
 
   /** @test */
   public function pageShouldContainsExpectedText() {
-    $this->assertContains(utf8_encode($this->_expectedText()), $this->_response->getBody());
+    $this->assertContains($this->_expectedText(), $this->_response->getBody());
   }
 }
 
 
 
 
-class Admin_NewsletterControllerPreviewActionWithArticlesSelectionAndPlaceHoldersWithoutArticlesTes
+class Admin_NewsletterControllerPreviewActionWithPlaceHoldersAndRenderWidgetTest
   extends Admin_NewsletterControllerPreviewActionPlaceHoldersTestCase {
 
   protected function _customizeNewsletter(Class_Newsletter $newsletter) {
     $newsletter
-      ->setArticlesIds(0)
-      ->setArticlesCategoriesIds(0);
+      ->setRenderWithWidget(1);
   }
 
 
@@ -1114,12 +1137,13 @@ class Admin_NewsletterControllerPreviewActionWithArticlesSelectionAndPlaceHolder
 
   /** @test */
   public function pageShouldContainsExpectedText() {
-    $this->assertContains(utf8_encode($this->_expectedText()), $this->_response->getBody());
+    $this->assertContains($this->_expectedText(), $this->_response->getBody());
   }
 }
 
 
 
+
 class Admin_NewsletterControllerPreviewActionWithArticlesSelectionTest
   extends Admin_NewsletterControllerPreviewActionTestCase {
 
-- 
GitLab