From e4f970dfff39807843b9908983a982b07820cf11 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?ANDRE=20s=C3=A9bastien?= <sandre@afi-sa.fr>
Date: Wed, 10 Nov 2021 18:00:16 +0100
Subject: [PATCH] hotline : #143811 : random in carousel have always same
 records

---
 VERSIONS_HOTLINE/143811                       |  1 +
 .../Library/Widget/Carousel/Record/View.php   | 16 ++++-
 tests/scenarios/Numel/NumelMapTest.php        | 15 ++--
 .../Templates/TemplatesWidgetCarouselTest.php | 69 ++++++++++++++++++-
 4 files changed, 89 insertions(+), 12 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/143811

diff --git a/VERSIONS_HOTLINE/143811 b/VERSIONS_HOTLINE/143811
new file mode 100644
index 00000000000..955e0de49fe
--- /dev/null
+++ b/VERSIONS_HOTLINE/143811
@@ -0,0 +1 @@
+ - ticket #143811 : Magasin de thèmes : Correction du tri aléatoire dans les carousels de notices.
\ No newline at end of file
diff --git a/library/templates/Intonation/Library/Widget/Carousel/Record/View.php b/library/templates/Intonation/Library/Widget/Carousel/Record/View.php
index bd5327031c8..5aa59476b6d 100644
--- a/library/templates/Intonation/Library/Widget/Carousel/Record/View.php
+++ b/library/templates/Intonation/Library/Widget/Carousel/Record/View.php
@@ -22,6 +22,7 @@
 
 class Intonation_Library_Widget_Carousel_Record_View extends Intonation_Library_Widget_Carousel_View {
 
+  protected static $_storm_limit = 100;
 
   protected function _findElements() {
     $order = $this->_settings->getOrder();
@@ -36,18 +37,27 @@ class Intonation_Library_Widget_Carousel_Record_View extends Intonation_Library_
                            'url_image=""',
                            $order]);
 
-    $params = ['limit' => $this->_getSize(),
+    $params = ['limit' => $this->_getLimit(),
                'order' => new Class_MoteurRecherche_OrderCriteria(implode(', ', $order))];
 
     $records = $this->_findRecords($params);
 
-    if ($this->_settings->isDisplayRandom())
+    if ($this->_settings->isDisplayRandom()) {
       shuffle($records);
+      array_slice($records, 0, $this->_getSize());
+    }
 
     return $records;
   }
 
 
+  protected function _getLimit() {
+    return $this->_settings->isDisplayRandom()
+      ? static::$_storm_limit
+      : $this->_getSize();
+  }
+
+
   protected function _findRecords($params) {
     $selection_id = $this->_settings->getIdPanier();
     $order = $this->_settings->getOrder();
@@ -89,7 +99,7 @@ class Intonation_Library_Widget_Carousel_Record_View extends Intonation_Library_
       return [];
 
     return Class_Notice::findAllByRequeteRecherche($request_ids,
-                                                   $this->_getSize(), 1);
+                                                   $this->_getLimit(), 1);
   }
 
 
diff --git a/tests/scenarios/Numel/NumelMapTest.php b/tests/scenarios/Numel/NumelMapTest.php
index df5f3d78ee4..93571c9f4e8 100644
--- a/tests/scenarios/Numel/NumelMapTest.php
+++ b/tests/scenarios/Numel/NumelMapTest.php
@@ -176,14 +176,13 @@ class NumelKioskMapLimit1000Test extends AbstractControllerTestCase {
                                    ->render()]);
 
     $notices_ids = [];
-    for ($pos = 0; $pos < 1000; $pos++)
+    for ($pos = 1; $pos <= 100; $pos++)
       $notices_ids [] = $pos;
 
-    $notices_100_ids = array_slice($notices_ids, 0, 100);
 
     $this->_mock_sql = $this->mock()
                             ->whenCalled('fetchAllByColumn')
-                            ->with('select notices.id_notice from notices Where (MATCH(facettes) AGAINST(\'Q444\' IN BOOLEAN MODE)) and type=1 order by url_image="no", url_image=""  LIMIT 0,1000')
+                            ->with('select notices.id_notice from notices Where (MATCH(facettes) AGAINST(\'Q444\' IN BOOLEAN MODE)) and type=1 order by url_image="no", url_image=""  LIMIT 0,100')
                             ->answers($notices_ids);
 
     Zend_Registry::set('sql', $this->_mock_sql);
@@ -191,8 +190,8 @@ class NumelKioskMapLimit1000Test extends AbstractControllerTestCase {
     Storm_Test_ObjectWrapper::onLoaderOfModel(Class_Notice::class)
 
       ->whenCalled('findAllBy')
-      ->with(['id_notice' => $notices_100_ids,
-              'order' => 'FIELD(id_notice, ' . implode(',', $notices_100_ids) . ')'])
+      ->with(['id_notice' => $notices_ids,
+              'order' => 'FIELD(id_notice, ' . implode(',', $notices_ids) . ')'])
       ->answers([$notice_fier])
 
       ->whenCalled('findAllBy')
@@ -211,7 +210,7 @@ class NumelKioskMapLimit1000Test extends AbstractControllerTestCase {
 
     $preferences = ['layout' => Intonation_Library_Widget_Carousel_Definition::MAP,
                     'rendering' => 'card',
-                    'size' => 1000,
+                    'size' => 50,
                     'id_catalogue' => 444,
                     'titre' => 'Les cartes postales sur la carte',
                     'order' => 'RAND()'
@@ -229,8 +228,8 @@ class NumelKioskMapLimit1000Test extends AbstractControllerTestCase {
 
 
   /** @test */
-  public function fetchAllByColumnWillBeCalledWithLimit1000() {
-    $this->assertEquals('select notices.id_notice from notices Where (MATCH(facettes) AGAINST(\'Q444\' IN BOOLEAN MODE)) and type=1 order by url_image="no", url_image=""  LIMIT 0,1000', $this->_mock_sql->getFirstAttributeForLastCallOn('fetchAllByColumn'));
+  public function fetchAllByColumnWillBeCalledWithLimit100() {
+    $this->assertEquals('select notices.id_notice from notices Where (MATCH(facettes) AGAINST(\'Q444\' IN BOOLEAN MODE)) and type=1 order by url_image="no", url_image=""  LIMIT 0,100', $this->_mock_sql->getFirstAttributeForLastCallOn('fetchAllByColumn'));
   }
 
 
diff --git a/tests/scenarios/Templates/TemplatesWidgetCarouselTest.php b/tests/scenarios/Templates/TemplatesWidgetCarouselTest.php
index 62b8fb83e48..f08cb4f625b 100644
--- a/tests/scenarios/Templates/TemplatesWidgetCarouselTest.php
+++ b/tests/scenarios/Templates/TemplatesWidgetCarouselTest.php
@@ -152,4 +152,71 @@ class TemplatesWidgetCarouselLoaderTestTest extends ArticleLoaderGetArticlesByPr
     $article = $this->getArticles(['filter_by_local' => true]);
     $this->assertQueryEquals('SELECT `cms_article`.* FROM `cms_article` WHERE ((DEBUT IS NULL) OR (DEBUT <= CURDATE())) AND ((FIN IS NULL) OR (FIN >= CURDATE())) AND (PARENT_ID=0) AND ((cms_article.langue = "fr" or exists (select \'x\' from cms_article as translation where translation.PARENT_ID = cms_article.ID_ARTICLE and trim(translation.langue) = "fr") or cms_article.langue = "" or cms_article.langue is null)) ORDER BY `DATE_CREATION` DESC');
   }
-}
\ No newline at end of file
+}
+
+
+
+
+/* hotline: #143811 */
+class TemplatesWidgetCarouselRecordNewsTest extends AbstractControllerTestCase {
+
+  protected $_storm_default_to_volatile = true;
+
+  public function setUp() {
+    parent::setUp();
+
+    $this->_buildTemplateProfil(['id' => 1,
+                                 'libelle' => 'Randomize carousel'])
+         ->setBoiteOfTypeInDivision(2,
+                                    Intonation_Library_Widget_Carousel_Record_Definition::CODE,
+                                    ['layout' => 'horizontal_list',
+                                     'rendering' => 'card-overlay',
+                                     'size' => 2,
+                                     'rss' => 0,
+                                     'embeded_code' => 0,
+                                     'link_to_all' => 1,
+                                     'all_layout' => 'list',
+                                     'all_rendering' => 'card-horizontal',
+                                     'description_length' => 20,
+                                     'titre' => 'Toutes les nouveautés',
+                                     'order' => 'RAND()',
+                                     'id_catalogue' => 10,
+                                     'type_module' => 'KIOSQUE'
+                                    ])
+         ->assertSave();
+
+    $this->fixture(Class_Catalogue::class,
+                   ['id' => 10,
+                    'libelle' => 'Catalogue nouveauté',
+                    'type_doc' => 1
+                   ]);
+
+    for ($i = 1; $i <= 100; $i++)
+      $this->fixture(Class_Notice::class,
+                     ['id' => $i]);
+
+    Zend_Registry::set('sql', null);
+    Storm_Cache::beVolatile();
+
+    $cache = (new Storm_Cache)
+      ->memoize(['select notices.id_notice from notices Where (notices.type_doc=\'1\') and type=1 order by url_image="no", url_image=""  LIMIT 0,100',
+                 'NoticeLoader',
+                 'getNoticeIdsByRequeteRecherche'],
+                function () {return range(1, 100);});
+
+    $this->dispatch('/index');
+  }
+
+
+  /** @test */
+  public function carouselShouldBePresentWithTwoRandomRecords() {
+    $this->assertXpathCount(2, '//div[contains(@class, "boite kiosque")]//div[contains(@class, "card-deck")]//div[contains(@class, "card_with_overlay")]');
+  }
+
+
+  /** @test */
+  public function carouselRecordsShouldBeShuffled() {
+    $this->assertRegExp('/.*(\/recherche\/viewnotice\/id\/(([^1-2]\/)|([0-9][0-9]0?\/)))./',
+                        $this->_response->getBody());
+  }
+}
-- 
GitLab