From 2f38fd36ed3c1ae32e51bdedc6491942b537701d Mon Sep 17 00:00:00 2001
From: Henri-Damien LAURENT <hdlaurent@afi-sa.fr>
Date: Fri, 5 Jul 2019 18:28:12 +0200
Subject: [PATCH] hotline#94066 : Calendar Widget : Allow user to reset date
 filter when calendar is not displayed

---
 VERSIONS_HOTLINE/94066                        |  1 +
 library/Class/Calendar.php                    |  5 ++
 .../ZendAfi/View/Helper/Filters/Element.php   | 11 ++++
 .../View/Helper/Filters/Element/Date.php      |  6 +-
 .../View/Helper/Filters/Strategy/Elements.php |  3 +-
 .../View/Helper/Filters/Strategy/Facet.php    | 13 +++--
 .../CmsControllerCalendarActionTest.php       | 55 ++++++++++++++++++-
 .../View/Helper/Accueil/CalendarTest.php      | 14 +++--
 8 files changed, 92 insertions(+), 16 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/94066

diff --git a/VERSIONS_HOTLINE/94066 b/VERSIONS_HOTLINE/94066
new file mode 100644
index 00000000000..f1da4653656
--- /dev/null
+++ b/VERSIONS_HOTLINE/94066
@@ -0,0 +1 @@
+ - ticket #94066 : Filtres de l'agenda : Ajout de l'élément "Tous" sur le filtre "Mois" pour déselectionner le critère lorsque la configuration d'affichage est mise à "Afficher le calendrier" = "non" 
\ No newline at end of file
diff --git a/library/Class/Calendar.php b/library/Class/Calendar.php
index 44ad270138d..65d307f047a 100644
--- a/library/Class/Calendar.php
+++ b/library/Class/Calendar.php
@@ -291,6 +291,11 @@ class Class_Calendar {
   }
 
 
+  public function isDisplayNoCalendar() {
+    return 0 == $this->preferences['display_calendar'];
+  }
+
+
   public function getActiveFilters() {
     if (empty($filters = $this->getEnabledFilters()))
       return [];
diff --git a/library/ZendAfi/View/Helper/Filters/Element.php b/library/ZendAfi/View/Helper/Filters/Element.php
index 81e91c9da3b..551df2ad2e2 100644
--- a/library/ZendAfi/View/Helper/Filters/Element.php
+++ b/library/ZendAfi/View/Helper/Filters/Element.php
@@ -83,6 +83,17 @@ abstract class ZendAfi_View_Helper_Filters_Element extends ZendAfi_View_Helper_B
   }
 
 
+  /**
+   * @param Class_FilterSettings $settings
+   * @param callable $callback
+   *
+   * Take a chance to decide whether to render any value item or not
+   */
+  public function renderAnyValueItem($settings, $callback) {
+    return call_user_func($callback);
+  }
+
+
   protected function _getFilterStrategy() {
     return ZendAfi_View_Helper_Filters_Strategy_Abstract::newFor($this);
   }
diff --git a/library/ZendAfi/View/Helper/Filters/Element/Date.php b/library/ZendAfi/View/Helper/Filters/Element/Date.php
index d29e5a7222f..343f333e489 100644
--- a/library/ZendAfi/View/Helper/Filters/Element/Date.php
+++ b/library/ZendAfi/View/Helper/Filters/Element/Date.php
@@ -26,8 +26,10 @@ class ZendAfi_View_Helper_Filters_Element_Date extends ZendAfi_View_Helper_Filte
   }
 
 
-  protected function _renderAnyValueItem($base_url, $selected) {
-    return '';
+  public function renderAnyValueItem($settings, $callback) {
+    return ($calendar = $settings->getCalendar()) && $calendar->isDisplayNoCalendar()
+      ? parent::renderAnyValueItem($settings, $callback)
+      : '';
   }
 
 
diff --git a/library/ZendAfi/View/Helper/Filters/Strategy/Elements.php b/library/ZendAfi/View/Helper/Filters/Strategy/Elements.php
index 87845f42671..c5aad13dc27 100644
--- a/library/ZendAfi/View/Helper/Filters/Strategy/Elements.php
+++ b/library/ZendAfi/View/Helper/Filters/Strategy/Elements.php
@@ -20,7 +20,8 @@
  */
 
 
-abstract class ZendAfi_View_Helper_Filters_Strategy_Elements extends ZendAfi_View_Helper_Filters_Strategy_Abstract {
+abstract class ZendAfi_View_Helper_Filters_Strategy_Elements
+  extends ZendAfi_View_Helper_Filters_Strategy_Abstract {
 
 
   protected function _shouldRender() {
diff --git a/library/ZendAfi/View/Helper/Filters/Strategy/Facet.php b/library/ZendAfi/View/Helper/Filters/Strategy/Facet.php
index f6fcc2343fc..c3a19bf8953 100644
--- a/library/ZendAfi/View/Helper/Filters/Strategy/Facet.php
+++ b/library/ZendAfi/View/Helper/Filters/Strategy/Facet.php
@@ -20,7 +20,8 @@
  */
 
 
-class ZendAfi_View_Helper_Filters_Strategy_Facet extends ZendAfi_View_Helper_Filters_Strategy_Elements {
+class ZendAfi_View_Helper_Filters_Strategy_Facet
+  extends ZendAfi_View_Helper_Filters_Strategy_Elements {
 
   public function renderFilterItems($elements) {
     $html = $this->_renderAnyValueItem(!$this->_isActive());
@@ -32,10 +33,12 @@ class ZendAfi_View_Helper_Filters_Strategy_Facet extends ZendAfi_View_Helper_Fil
 
 
   protected function _renderAnyValueItem($selected) {
-    if ($this->_getCustomFieldId() == 'date')
-      return '';
-
-    return $this->_renderFilterItem(null, $this->_('Tous'), $selected);
+    return $this->_filter_elements
+      ->renderAnyValueItem($this->_settings,
+                           function () use($selected)
+                           {
+                             return $this->_renderFilterItem(null, $this->_('Tous'), $selected);
+                           });
   }
 
 
diff --git a/tests/application/modules/opac/controllers/CmsControllerCalendarActionTest.php b/tests/application/modules/opac/controllers/CmsControllerCalendarActionTest.php
index eb14bcff095..f8a9a6c14c3 100644
--- a/tests/application/modules/opac/controllers/CmsControllerCalendarActionTest.php
+++ b/tests/application/modules/opac/controllers/CmsControllerCalendarActionTest.php
@@ -276,7 +276,8 @@ class CmsControllerCalendarActionWithMultipleFiltersTest extends CmsControllerCa
 }
 
 
-abstract class CmsControllerCalendarActionWithFiltersTestCase extends CmsControllerCalendarActionTestCase  {
+abstract class CmsControllerCalendarActionWithFiltersTestCase
+  extends CmsControllerCalendarActionTestCase  {
   protected
     $_opac4,
     $_date,
@@ -309,7 +310,8 @@ abstract class CmsControllerCalendarActionWithFiltersTestCase extends CmsControl
 
 
 
-class CmsControllerCalendarActionWithFiltersDateTest  extends CmsControllerCalendarActionWithFiltersTestCase {
+class CmsControllerCalendarActionWithFiltersDateTest
+  extends CmsControllerCalendarActionWithFiltersTestCase {
   public function setUp() {
     parent::setUp();
 
@@ -348,6 +350,15 @@ class CmsControllerCalendarActionWithFiltersDateTest  extends CmsControllerCalen
   }
 
 
+  /** @test */
+  public function calendarFilterOnMonthShouldNotContainsLinkToResetDate() {
+    $expected = '/custom_field_2/opac';
+    $this->assertNotXPathContentContains('//li[@data-id="date"]//a["' . $expected. '"=substring(@href, string-length(@href)-' . (strlen($expected)-1) .')]',
+                                         'Tous',
+                                         $this->_response->getBody());
+  }
+
+
   /** @test **/
   public function calendarWithCustomFieldFiltersShouldBuildPreferencesForGetArticlesByPreferences() {
     $this->assertXPathContentContains('//a[@class="calendar_event_title"]',
@@ -424,11 +435,13 @@ class CmsControllerCalendarActionWithFiltersDateTest  extends CmsControllerCalen
     $this->assertXPath('//a[@class="calendar_title_month_clickable"][contains(@href,"/day/2014-06")][contains(@href, "/date/2014-06")]');
   }
 
+
   /** @test */
   public function linkCurrentMonthContainsDayParameter() {
     $this->assertXPath('//a[@class="calendar_title_month_clickable"][contains(@href,"/day/2014-07")][contains(@href,"/cms/calendar")]');
   }
 
+
   /** @test */
   public function linkInsideCalendarShouldContainsDayButNoDateParameters() {
     $this->assertXPath('//a[contains(@class,"day_clickable")][contains(@href,"/day/2014-07-17")][not(contains(@href, "/date/"))]',
@@ -438,6 +451,44 @@ class CmsControllerCalendarActionWithFiltersDateTest  extends CmsControllerCalen
 
 
 
+class CmsControllerCalendarActionWithFiltersDateAndNoCalendarTest
+  extends CmsControllerCalendarActionWithFiltersTestCase {
+  public function setUp() {
+    parent::setUp();
+
+    Class_Article::getLoader()
+      ->whenCalled('getArticlesByPreferences')
+      ->with(array_merge($this->_common_preferences, ['event_date' => '2014-07']))
+      ->answers([$this->_opac4])
+
+      ->whenCalled('getArticlesByPreferences')
+      ->with(array_merge($this->_common_preferences, ['event_date' => '',
+                                                      'event_start_after' => '2014-07',
+                                                      'event_end_after' => '',
+                                                      'limit' => 3]))
+      ->answers([])
+      ->beStrict();
+
+    $profil = Class_Profil::find(3);
+    $cfg_accueil = $profil->getCfgAccueilAsArray();
+    $cfg_accueil['modules'][1]['preferences']['display_calendar'] = '0';
+    $profil->setCfgAccueil($cfg_accueil)->assertSave();
+
+    $this->dispatch('/cms/calendar/id_profil/3/id_module/1/date/2014-07/custom_field_2/opac/render/ajax', true);
+  }
+
+
+  /** @test */
+  public function calendarFilterOnMonthShouldContainsLinkToResetDate() {
+    $expected = '/custom_field_2/opac';
+    $this->assertXPathContentContains('//li[@data-id="date"]//a["' . $expected. '"=substring(@href, string-length(@href)-' . (strlen($expected)-1) .')]',
+                                      'Tous',
+                                      $this->_response->getBody());
+  }
+}
+
+
+
 
 class CmsControllerCalendarActionWithFiltersDayTest  extends CmsControllerCalendarActionWithFiltersTestCase {
   public function setUp() {
diff --git a/tests/library/ZendAfi/View/Helper/Accueil/CalendarTest.php b/tests/library/ZendAfi/View/Helper/Accueil/CalendarTest.php
index 3a5e2cf2e35..0a1cbbc0c33 100644
--- a/tests/library/ZendAfi/View/Helper/Accueil/CalendarTest.php
+++ b/tests/library/ZendAfi/View/Helper/Accueil/CalendarTest.php
@@ -950,9 +950,9 @@ abstract class CalendarHelperDisplayModeTestCase extends CalendarViewHelperTestC
                                             'categorie' => '',
                                             'contenu' => 'toto'
                                            ]);
-    Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Article')
-      ->whenCalled('getArticlesByPreferences')
-      ->answers([$this->_article_bokeh]);
+    $this->onLoaderOfModel('Class_Article')
+         ->whenCalled('getArticlesByPreferences')
+         ->answers([$this->_article_bokeh]);
 
     $params = ['type_module' => 'CALENDAR',
                'division' => 2,
@@ -1127,14 +1127,16 @@ class CalendarHelperWithFiltersTest extends CalendarHelperDisplayModeTestCase {
   /** @test */
   public function dateFilterShouldContainsAllMonths() {
     $this->assertXPathCount($this->html,
-                            '//ul[contains(@class, "filters")]/li[@class="date"]/ul/li', 12, $this->html);
+                            '//ul[contains(@class, "filters")]/li[@class="date"]/ul/li',
+                            13,
+                            $this->html);
   }
 
 
   /** @test */
   public function dateFilterShouldContainsJuillet() {
     $this->assertXPathContentContains($this->html,
-                                      '//ul[contains(@class, "filters")]/li[@class="date"]//li[1]/a[contains(@href, "date/2014-07")]', 'Juillet', $this->html);
+                                      '//ul[contains(@class, "filters")]/li[@class="date"]//li[2]/a[contains(@href, "date/2014-07")]', 'Juillet', $this->html);
 
   }
 
@@ -1142,7 +1144,7 @@ class CalendarHelperWithFiltersTest extends CalendarHelperDisplayModeTestCase {
   /** @test */
   public function dateFilterShouldContainsJuin() {
     $this->assertXPathContentContains($this->html,
-                                      '//ul[contains(@class, "filters")]/li[@class="date"]//li[12]/a[contains(@href, "date/2015-06")]', 'Juin');
+                                      '//ul[contains(@class, "filters")]/li[@class="date"]//li[13]/a[contains(@href, "date/2015-06")]', 'Juin');
   }
 
 
-- 
GitLab