From b55ec3d4cb5a88784e327a16d32d5c9bb496b4f9 Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Thu, 16 Sep 2021 18:02:51 +0200
Subject: [PATCH] hotline MT #140645 menu cache behavior is related to children
 cache behavior.

---
 VERSIONS_HOTLINE/140645                       |  1 +
 library/Class/Systeme/ModulesAccueil/Null.php |  9 +++
 library/Class/Systeme/Widget/Menu.php         | 36 +++++++++++-
 library/ZendAfi/View/Helper/Accueil/Base.php  |  5 +-
 .../Library/Widget/Carousel/Menu/View.php     | 37 ++++++++++--
 .../Intonation/View/RenderMenuEntry.php       | 22 +++----
 .../scenarios/Templates/TemplatesMenuTest.php | 41 ++++++++++++-
 .../Templates/TemplatesWidgetInMenusTest.php  | 58 +++++++++++++++++++
 8 files changed, 183 insertions(+), 26 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/140645

diff --git a/VERSIONS_HOTLINE/140645 b/VERSIONS_HOTLINE/140645
new file mode 100644
index 00000000000..5d5f98984b0
--- /dev/null
+++ b/VERSIONS_HOTLINE/140645
@@ -0,0 +1 @@
+ - ticket #140645 : Magasin de thèmes : Correction du comportement du cache des menus avec des boites.
\ No newline at end of file
diff --git a/library/Class/Systeme/ModulesAccueil/Null.php b/library/Class/Systeme/ModulesAccueil/Null.php
index 0d49821cbd8..07c222e568a 100644
--- a/library/Class/Systeme/ModulesAccueil/Null.php
+++ b/library/Class/Systeme/ModulesAccueil/Null.php
@@ -229,4 +229,13 @@ class Class_Systeme_ModulesAccueil_Null {
 
     return $this->_widget_context = new Intonation_Library_Widget_Carousel_NullContext;
   }
+
+
+  public function shouldCacheContent() {
+    return ($widget = ZendAfi_View_Helper_Accueil_Base::getWidget($this->getWidgetContext()->getId(),
+                                                                  $this->getWidgetContext()->forForm(),
+                                                                  null))
+      ? $widget->shouldCacheContent()
+      : true;
+  }
 }
\ No newline at end of file
diff --git a/library/Class/Systeme/Widget/Menu.php b/library/Class/Systeme/Widget/Menu.php
index 5947f79f478..4d4bb454e28 100644
--- a/library/Class/Systeme/Widget/Menu.php
+++ b/library/Class/Systeme/Widget/Menu.php
@@ -164,8 +164,6 @@ class Class_Systeme_Widget_Menu extends Class_Systeme_Widget_Abstract {
   }
 
 
-
-
   protected function _update() {
     $datas = (new Class_Systeme_Widget_Menu_Datas)
       ->clean(array_merge($this->getLocalSettings(),
@@ -353,4 +351,38 @@ class Class_Systeme_Widget_Menu extends Class_Systeme_Widget_Abstract {
   public function getSubMenus() {
     return $this->_sub_menus;
   }
+
+
+  public function shouldCacheContent() {
+    if ( $this->isWidget())
+      return $this->_getWidgetResources()->shouldCacheContent();
+
+    if ( ! $this->isMenu())
+      return true;
+
+    foreach( $this->getChildrenAsMenu() as $menu )
+      if ( ! $menu->shouldCacheContent())
+        return false;
+
+    return true;
+  }
+
+
+  public function getChildrenAsMenu() {
+    if ( ! $children = $this->getChildren())
+      return [];
+
+    return
+      array_filter(
+                   array_map(function($child)
+                             {
+                               return ($menu_entry_child = (new Class_Systeme_Widget_Menu)
+                                       ->setId($child->getId())
+                                       ->setProfileId($this->getProfileId())
+                                       ->setParent($this->getParent())
+                                       ->load())
+                                 ? $menu_entry_child
+                                 : null;
+                             }, $children));
+  }
 }
diff --git a/library/ZendAfi/View/Helper/Accueil/Base.php b/library/ZendAfi/View/Helper/Accueil/Base.php
index 9f81e3bb40e..1a8f9e7de0b 100644
--- a/library/ZendAfi/View/Helper/Accueil/Base.php
+++ b/library/ZendAfi/View/Helper/Accueil/Base.php
@@ -373,7 +373,10 @@ class ZendAfi_View_Helper_Accueil_Base extends ZendAfi_View_Helper_ModuleAbstrac
       : self::getModuleHelperClass($type_module);
 
     $helper = new $classname($id_module, $module_params);
-    $helper->setView($view);
+
+    if ($view)
+      $helper->setView($view);
+
     return $helper;
   }
 
diff --git a/library/templates/Intonation/Library/Widget/Carousel/Menu/View.php b/library/templates/Intonation/Library/Widget/Carousel/Menu/View.php
index 691b097017a..40dc4ccbe0b 100644
--- a/library/templates/Intonation/Library/Widget/Carousel/Menu/View.php
+++ b/library/templates/Intonation/Library/Widget/Carousel/Menu/View.php
@@ -24,17 +24,25 @@ class Intonation_Library_Widget_Carousel_Menu_View extends Intonation_Library_Wi
 
 
   protected
-    $_menu,
+    $_menu_cache,
     $_use_menu_layout;
 
 
   protected function _findElements() {
-    return ( $this->_menu = Class_Systeme_Widget_Menu::loadFromWidgetPreferences($this->preferences))
-      ? $this->_menu->getSubMenus()
+    return ($menu = $this->_getMenu())
+      ? $menu->getSubMenus()
       : [];
   }
 
 
+  protected function _getMenu() {
+    if ( $this->_menu_cache)
+      return $this->_menu_cache;
+
+    return $this->_menu_cache = Class_Systeme_Widget_Menu::loadFromWidgetPreferences($this->preferences);
+  }
+
+
   protected function _getWrapper() {
     return Intonation_Library_View_Wrapper_MenuEntry::class;
   }
@@ -104,12 +112,31 @@ class Intonation_Library_Widget_Carousel_Menu_View extends Intonation_Library_Wi
 
 
   protected function _extendedActions() {
-    if ( ! $this->_menu)
+    if ( ! $this->_getMenu())
       return [];
 
     return [function()
             {
-              return $this->view->tagEditMenu($this->_menu->getId(), $this->_menu->getProfileId());
+              return $this->view->tagEditMenu($this->_getMenu()->getId(), $this->_getMenu()->getProfileId());
             }];
   }
+
+
+  public function shouldCacheContent() {
+    return $this->_useNotCachedWidget()
+      ? false
+      : parent::shouldCacheContent();
+  }
+
+
+  protected function _useNotCachedWidget() {
+    if ( ! $menu = $this->_getMenu())
+      return false;
+
+    foreach ( $menu->getSubMenus() as $sub_menu )
+      if ( ! $sub_menu->shouldCacheContent())
+        return true;
+
+    return false;
+  }
 }
diff --git a/library/templates/Intonation/View/RenderMenuEntry.php b/library/templates/Intonation/View/RenderMenuEntry.php
index f2b26acf977..290d5c4b9c1 100644
--- a/library/templates/Intonation/View/RenderMenuEntry.php
+++ b/library/templates/Intonation/View/RenderMenuEntry.php
@@ -62,18 +62,9 @@ class Intonation_View_RenderMenuEntry extends ZendAfi_View_Helper_BaseHelper {
 
 
   protected function _renderChildren($menu_entry, $id, $use_menu_layout) {
-    if ( ! $children = $menu_entry->getChildren())
-      return '';
-
     $html = [];
-    foreach($children as $child) {
-      if ( ! $menu_entry_child = (new Class_Systeme_Widget_Menu)
-          ->setId($child->getId())
-          ->setProfileId($menu_entry->getProfileId())
-          ->setParent($menu_entry->getParent())
-          ->load())
-        continue;
 
+    foreach($menu_entry->getChildrenAsMenu() as $menu_entry_child)
       $html[] = $this->_tag('li',
                             $this->_renderMenuEntry($menu_entry_child, $use_menu_layout),
 
@@ -83,12 +74,13 @@ class Intonation_View_RenderMenuEntry extends ZendAfi_View_Helper_BaseHelper {
                                                 $menu_entry_child->getId()) . (($menu_entry_child->isMenu())
                              ? ' dropdown'
                                                       : '')]);
-    }
 
-    return $this->_tag('ul',
-                       implode($html),
-                       ['class' => 'dropdown-menu',
-                        'aria-labelledby' => $id]);
+    return $html
+      ?  $this->_tag('ul',
+                     implode($html),
+                     ['class' => 'dropdown-menu',
+                      'aria-labelledby' => $id])
+      : '';
   }
 
 
diff --git a/tests/scenarios/Templates/TemplatesMenuTest.php b/tests/scenarios/Templates/TemplatesMenuTest.php
index a22adfc48fa..eb5f8fc5f30 100644
--- a/tests/scenarios/Templates/TemplatesMenuTest.php
+++ b/tests/scenarios/Templates/TemplatesMenuTest.php
@@ -203,25 +203,55 @@ class TemplatesMenuDisplayTwoTimesTest extends Admin_AbstractControllerTestCase
   public function setUp() {
     parent::setUp();
 
+    Class_AdminVar::set('MENU_BOITE', 1);
+    Class_AdminVar::set('CACHE_ACTIF', 1);
+
+    Storm_Cache::beVolatile();
+
+    $user =
+      $this->fixture(Class_Users::class,
+                     ['id' => 2323,
+                      'login' => 'Tor',
+                      'nom' => 'Tor',
+                      'prenom' => 'Termina',
+                      'password' => '2000']);
+
+    ZendAfi_Auth::getInstance()->logUser($user);
+
     $profile = $this->_buildTemplateProfil(['id' => 1]);
 
     (new Class_Profil_Import($profile))
       ->import(__DIR__ . '/137985_profile_with_menu_with_widget_with_menu.json');
-
-
-    $this->dispatch('/index');
   }
 
 
   /** @test */
   public function linkToRenderMenuEntryIWantToSubscribeShouldBeParentMenu6SubMenu3MenuProfil1() {
+    $this->dispatch('/index');
     $this->assertXPathContentContains('//a[@href="/widget/render-menu-entry/parent/6/menu_profil/1/menu/3/id_profil/55"]', 'Je veux m\'inscrire');
   }
 
+
   /** @test */
   public function linkToRenderMenuEntryIWantToSubscribeShouldBePresentTwoTime() {
+    ZendAfi_Auth::getInstance()->clearIdentity();
+    $this->dispatch('/index');
     $this->assertXPathCount('//a[@href="/widget/render-menu-entry/parent/6/menu_profil/1/menu/3/id_profil/55"]', 2);
   }
+
+
+
+  /** @test */
+  public function loginWidgetInMenuShouldNotBeCached() {
+    $this->dispatch('/index');
+
+    ZendAfi_Auth::getInstance()->clearIdentity();
+    $this->_response->setBody('');
+
+    $this->dispatch('/index');
+    $this->assertNotXPathContentContains('//div[contains(@class, "login")]', 'Tor');
+    $this->assertXPath('//div[contains(@class, "login")]//form[contains(@class, "login")]//input[@id="username"]');
+  }
 }
 
 
@@ -320,6 +350,11 @@ class TemplatesMenuDefaultSettingsTest extends Admin_AbstractControllerTestCase
   public function setUp() {
     parent::setUp();
 
+    Class_AdminVar::set('MENU_BOITE', 1);
+    Class_AdminVar::set('CACHE_ACTIF', 1);
+
+    Storm_Cache::beVolatile();
+
     $profile = $this->_buildTemplateProfil(['id' => 1]);
 
     (new Class_Profil_Import($profile))
diff --git a/tests/scenarios/Templates/TemplatesWidgetInMenusTest.php b/tests/scenarios/Templates/TemplatesWidgetInMenusTest.php
index ac761a1d392..4bc30f1008a 100644
--- a/tests/scenarios/Templates/TemplatesWidgetInMenusTest.php
+++ b/tests/scenarios/Templates/TemplatesWidgetInMenusTest.php
@@ -173,3 +173,61 @@ class TemplatesWidgetLoginInMenuAndInHeaderTest extends Admin_AbstractController
     $this->assertXPath('//img[@src="logo.gif"]');
   }
 }
+
+
+
+
+class TemplatesWidgetInMenusWithLoginWidgetCacheTest extends AbstractControllerTestCase {
+  protected $_storm_default_to_volatile = true;
+
+
+  public function setUp() {
+    parent::setUp();
+    Class_AdminVar::set('MENU_BOITE', 1);
+    Class_AdminVar::set('CACHE_ACTIF', 1);
+
+    Storm_Cache::beVolatile();
+
+    $user =
+      $this->fixture(Class_Users::class,
+                     ['id' => 2323,
+                      'login' => 'Tor',
+                      'nom' => 'Tor',
+                      'prenom' => 'Termina',
+                      'password' => '2000']);
+
+    ZendAfi_Auth::getInstance()->logUser($user);
+
+    $this
+      ->_buildTemplateProfil(['id' => 1])
+      ->setCfgMenus(['H' => ['libelle' => 'Principal',
+                             'picto' => 'main.png',
+                             'menus' => []],
+                     8 => ['type_menu' => 'MENU',
+                           'libelle' => 'Menu contact',
+                           'preferences' => [],
+                           'menus' => [ 1 => ['type_menu' => 'MODULE_ACCUEIL_LOGIN',
+                                              'type_module' => 'LOGIN',
+                                              'lien_deconnection' => 'tchao',
+                                              'lien_compte' => 'To my account',
+                                              'IntonationFormStyle' => 'toggle',
+                                              'picto' => 'connect.png']]]])
+      ->setBoiteOfTypeInDivision(4,
+                                 'MENU',
+                                 ['picto' => 'logo.gif',
+                                  'libelle' => 'Contact',
+                                  'menu' => '1-8']);
+
+    $this->dispatch('/');
+  }
+
+
+  /** @test */
+  public function loginWidgetInMenuShouldNotContainsTor() {
+    $this->_response->setBody('');
+    ZendAfi_Auth::getInstance()->clearIdentity();
+    $this->dispatch('/');
+    $this->assertNotXPathContentContains('//div[contains(@class, "login")]', 'Tor');
+    $this->assertXPath('//div[contains(@class, "login")]//form[contains(@class, "login")]//input[@id="username"]');
+  }
+}
\ No newline at end of file
-- 
GitLab