From 64e39ee7731241b883810fb0f5d3512a6f2561af Mon Sep 17 00:00:00 2001
From: Arthur Suzuki <arthur.suzuki@biblibre.com>
Date: Thu, 5 Jan 2023 16:25:17 +0000
Subject: [PATCH] hotline#169355 : compute facets for more than 1000 items per
 records

---
 VERSIONS_HOTLINE/169355                       |  1 +
 cosmogramme/sql/patch/patch_446.php           |  7 ++++
 .../Integration/PhaseItemFacets.php           |  6 +--
 tests/db/UpgradeDBTest.php                    | 23 +++++++++++
 .../Integration/PhaseItemFacetsTest.php       | 39 +++++++++++++++----
 tests/library/Class/ModelTestCase.php         |  1 +
 6 files changed, 66 insertions(+), 11 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/169355
 create mode 100644 cosmogramme/sql/patch/patch_446.php

diff --git a/VERSIONS_HOTLINE/169355 b/VERSIONS_HOTLINE/169355
new file mode 100644
index 00000000000..6723a9bab78
--- /dev/null
+++ b/VERSIONS_HOTLINE/169355
@@ -0,0 +1 @@
+ - correctif #169355 : Cosmogramme : Activation du calcul des facettes pour les notices de plus de 1000 exemplaires (revues et périodiques)
\ No newline at end of file
diff --git a/cosmogramme/sql/patch/patch_446.php b/cosmogramme/sql/patch/patch_446.php
new file mode 100644
index 00000000000..b698a02c098
--- /dev/null
+++ b/cosmogramme/sql/patch/patch_446.php
@@ -0,0 +1,7 @@
+<?php
+$adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
+
+try {
+  $adapter->query("insert into variables (clef, valeur, commentaire, type_champ, liste, groupe, ordre, verrou, hidden) VALUES ('max_items',1000,'Nombre maximum d\'exemplaires traités par notices',0,'',4,1,0,0)");
+}
+catch(Exception $e) {}
diff --git a/library/Class/Cosmogramme/Integration/PhaseItemFacets.php b/library/Class/Cosmogramme/Integration/PhaseItemFacets.php
index 153da8d0387..ac1a65ace3f 100644
--- a/library/Class/Cosmogramme/Integration/PhaseItemFacets.php
+++ b/library/Class/Cosmogramme/Integration/PhaseItemFacets.php
@@ -23,7 +23,6 @@
 class Class_Cosmogramme_Integration_PhaseItemFacets
   extends Class_Cosmogramme_Integration_PhaseAbstract {
   const MY_ID = 7;
-  const MAX_ITEMS = 1000;
 
   protected $_previous_records, $_last_update_date;
   protected $_db_reset = true;
@@ -80,12 +79,13 @@ class Class_Cosmogramme_Integration_PhaseItemFacets
         return $this->_resetDbConnection()
                     ->_phase;
 
-      if (static::MAX_ITEMS < Class_Exemplaire::countBy(['id_notice' => $record->getId()])) {
+      $max_items = Class_CosmoVar::get('max_items');
+      if ($max_items < Class_Exemplaire::countBy(['id_notice' => $record->getId()])) {
         $this->_log->error('<span class="rouge">'
                            . $this->_('La notice "%s" (id: %s) a plus de %s exemplaires: facettes non mises à jour',
                                       $record->getTitrePrincipal(),
                                       $record->getId(),
-                                      static::MAX_ITEMS)
+                                      $max_items)
                            . '</span>');
         $this->_setData('pointeur_notice', $record->getId());
         $this->_incrementData('nombre');
diff --git a/tests/db/UpgradeDBTest.php b/tests/db/UpgradeDBTest.php
index fb0a8409ee7..c5787eab19b 100644
--- a/tests/db/UpgradeDBTest.php
+++ b/tests/db/UpgradeDBTest.php
@@ -5069,3 +5069,26 @@ class UpgradeDB_445_Test extends UpgradeDBTestCase {
     $this->assertFieldType('cms_article', 'id_cat', 'int(11)');
   }
 }
+
+
+
+
+class UpgradeDB_446_Test extends UpgradeDBTestCase {
+  public function prepare() {
+    $this->silentQuery("delete from variables where clef='max_items'");
+  }
+
+
+  /** @test */
+  public function tableVariablesShouldHaveMaxItemsVariableInGroupe4() {
+    $this->assertEquals(4,
+                          $this->query("select * from variables where clef='max_items'")->fetch()['groupe']);
+  }
+
+
+  /** @test */
+  public function tableVariablesShouldHaveMaxItemsVariableSetTo1000() {
+    $this->assertEquals(1000,
+                        $this->query("select * from variables where clef='max_items'")->fetch()['valeur']);
+  }
+}
diff --git a/tests/library/Class/Cosmogramme/Integration/PhaseItemFacetsTest.php b/tests/library/Class/Cosmogramme/Integration/PhaseItemFacetsTest.php
index 6c2c36b519f..398b0073fa9 100644
--- a/tests/library/Class/Cosmogramme/Integration/PhaseItemFacetsTest.php
+++ b/tests/library/Class/Cosmogramme/Integration/PhaseItemFacetsTest.php
@@ -47,7 +47,7 @@ class PhaseItemFacetsBadPreviousPhaseTest extends PhaseItemFacetsTestCase {
 
 
 
-class PhaseItemFacetsExpectedPreviousPhaseTest extends PhaseItemFacetsTestCase {
+abstract class PhaseItemFacetsExpectedPreviousPhaseTestCase extends PhaseItemFacetsTestCase {
   protected function _getPreviousPhase() {
     return (new Class_Cosmogramme_Integration_Phase(4))
       ->beCron();
@@ -55,7 +55,7 @@ class PhaseItemFacetsExpectedPreviousPhaseTest extends PhaseItemFacetsTestCase {
 
 
   protected function _prepareFixtures() {
-    $this->fixture('Class_CosmoVar',
+    $this->fixture(Class_CosmoVar::class,
                    ['id' => 'date_maj_facettes',
                     'valeur' => '']);
 
@@ -96,12 +96,6 @@ class PhaseItemFacetsExpectedPreviousPhaseTest extends PhaseItemFacetsTestCase {
   }
 
 
-  /** @test */
-  public function logShouldContainsRecordTwoHasMoreThan1000Items() {
-    $this->assertLogContains('La notice "Les cassetouts" (id: 2) a plus de 1000 exemplaires');
-  }
-
-
   /** @test */
   public function logShouldContainsProcessedRecordCount() {
     $this->assertLogContains('2 notices traitées');
@@ -116,6 +110,34 @@ class PhaseItemFacetsExpectedPreviousPhaseTest extends PhaseItemFacetsTestCase {
 
 
 
+
+class PhaseItemWithMaxItems1000 extends PhaseItemFacetsExpectedPreviousPhaseTestCase {
+  /** @test */
+  public function logShouldContainsRecordTwoHasMoreThan1000Items() {
+    $this->assertLogContains('La notice "Les cassetouts" (id: 2) a plus de 1000 exemplaires');
+  }
+}
+
+
+
+
+class PhaseItemWithMaxItems10000 extends PhaseItemFacetsExpectedPreviousPhaseTestCase {
+  protected function _prepareFixtures() {
+    parent::_prepareFixtures();
+    $this->fixture(Class_CosmoVar::class,
+                   ['id' => 'max_items',
+                    'valeur' => '10000']);
+  }
+
+  /** @test */
+  public function logShouldNotContainsRecordTwoHasMoreThan1000Items() {
+    $this->assertNotLogContains('La notice "Les cassetouts" (id: 2) a plus de 1000 exemplaires');
+  }
+}
+
+
+
+
 abstract class PhaseItemFacetsCallbackTest extends PhaseItemFacetsTestCase {
   protected function _getPreviousPhase() {
     return (new Class_Cosmogramme_Integration_Phase(4));
@@ -165,6 +187,7 @@ class PhaseItemFacetsExpectedExceptionPhaseTest extends PhaseItemFacetsTestCase
 
 
 
+
 /* Fix hotline #64218  */
 class PhaseItemFacetsExecutePhaseTest extends PhaseItemFacetsTestCase {
   protected function _getPreviousPhase() {
diff --git a/tests/library/Class/ModelTestCase.php b/tests/library/Class/ModelTestCase.php
index 746c0b76e4c..d5f6c8578fb 100644
--- a/tests/library/Class/ModelTestCase.php
+++ b/tests/library/Class/ModelTestCase.php
@@ -66,6 +66,7 @@ abstract class ModelTestCase extends Storm_Test_ModelTestCase {
     Class_AdminVar::set("AVIS_MAX_SAISIE", 100);
     Class_AdminVar::set("FORCE_HTTPS", 0);
     Class_AdminVar::set('NOM_DOMAINE', 'http://localhost');
+    Class_CosmoVar::setValueOf('max_items',1000);
     Class_Crypt::setPhpCommand($this->mock()
                                ->whenCalled('password_hash')
                                ->willDo(function($pass, $crypt) { return $pass; }));
-- 
GitLab