From 67e12fd0d5c6d9f220b049214d7102745c2b3cf0 Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Tue, 30 Oct 2018 16:16:17 +0100
Subject: [PATCH] hotline #80536 : and search between different thesauri roots

---
 VERSIONS_HOTLINE/80536                        |  1 +
 library/Class/CodifThesaurus.php              | 10 ++
 library/Class/MoteurRecherche.php             |  5 +-
 .../controllers/RechercheControllerTest.php   | 94 +++++++++++++++++++
 4 files changed, 109 insertions(+), 1 deletion(-)
 create mode 100644 VERSIONS_HOTLINE/80536

diff --git a/VERSIONS_HOTLINE/80536 b/VERSIONS_HOTLINE/80536
new file mode 100644
index 00000000000..d560ee51e50
--- /dev/null
+++ b/VERSIONS_HOTLINE/80536
@@ -0,0 +1 @@
+ - ticket #80536 : MINSOC: les critères SearchAxe prend le pas sur les critères Custom_multifacet_*
\ No newline at end of file
diff --git a/library/Class/CodifThesaurus.php b/library/Class/CodifThesaurus.php
index bf26ef475d5..655a775c6b0 100644
--- a/library/Class/CodifThesaurus.php
+++ b/library/Class/CodifThesaurus.php
@@ -192,6 +192,16 @@ class CodifThesaurusLoader extends Storm_Model_Loader {
   }
 
 
+  public function findRootParentCode($id_thesaurus) {
+    $parent_str=substr($id_thesaurus,0,strlen($id_thesaurus)-(4-strlen($id_thesaurus)%4));
+    if (!$parent_str)
+      return null;
+    if (strlen($parent_str) > 4)
+      return static::findRootParentCode($parent_str);
+    return $parent_str;
+  }
+
+
   public function findParent($id_thesaurus) {
     $parent_str=substr($id_thesaurus,0,strlen($id_thesaurus)-(4-strlen($id_thesaurus)%4));
     if (!$parent_str)
diff --git a/library/Class/MoteurRecherche.php b/library/Class/MoteurRecherche.php
index b92dd84ecfd..d6c68b07903 100644
--- a/library/Class/MoteurRecherche.php
+++ b/library/Class/MoteurRecherche.php
@@ -229,8 +229,11 @@ class Class_MoteurRecherche {
   public function visitMultiFacet($facet) {
     if (!$facet)
       return;
-
     $code = substr($facet, 0, 1);
+    if ($code == Class_CodifThesaurus::CODE_FACETTE) {
+      if ($root = Class_CodifThesaurus::findRootParentCode(substr($facet, 1)))
+        $code = $root;
+    }
     if (!isset($this->multi_facets[$code]))
       $this->multi_facets[$code] = [];
     $this->multi_facets[$code][] = $facet;
diff --git a/tests/application/modules/opac/controllers/RechercheControllerTest.php b/tests/application/modules/opac/controllers/RechercheControllerTest.php
index 6c56041a42c..3ea036a1c65 100644
--- a/tests/application/modules/opac/controllers/RechercheControllerTest.php
+++ b/tests/application/modules/opac/controllers/RechercheControllerTest.php
@@ -3119,6 +3119,100 @@ class RechercheControllerSimpleActionWithEmptyDomainSettingsAndSiteLinkedTest ex
 
 
 
+class RechercheControlleSimpleActionWithMultifacetsThesauriTest extends RechercheControllerNoticeTestCase {
+  public function setUp() {
+    parent::setUp();
+      $this->fixture('Class_CodifThesaurus',
+                   ['id' => 3,
+                    'libelle' => 'Document',
+                    'libelle_facette' => 'Document',
+                    'id_thesaurus' => 'DOCU',
+                    'id_origine' => null,
+                    'code' => 'DOCU',
+                    'rules' => '{"label":" 99$t "}']);
+
+      $this->fixture('Class_CodifThesaurus',
+                   ['id' => 30,
+                    'libelle' => 'Musique',
+                    'libelle_facette' => 'Musique',
+                    'id_thesaurus' => 'MUSI',
+                    'id_origine' => null,
+                    'code' => 'MUSI',
+                    'rules' => '{"label":" 95$t "}']);
+
+    $this->fixture('Class_CodifThesaurus',
+                   ['id' => 4,
+                    'libelle' => 'SIFI',
+                    'libelle_facette' => 'Science fiction',
+                    'id_thesaurus' => 'DOCU0001',
+                    'code' => 'SIFI',
+                    'rules' => '{"label":" 99$t "}']);
+
+    $this->fixture('Class_CodifThesaurus',
+                   ['id' => 5,
+                    'libelle' => 'BD',
+                    'libelle_facette' => 'Bande dessinee',
+                    'id_thesaurus' => 'DOCU0002',
+                    'code' => 'BD',
+                    'rules' => '{"label":" 99$t "}']);
+
+    $this->fixture('Class_CodifThesaurus',
+                   ['id' => 6,
+                    'libelle' => 'BD',
+                    'libelle_facette' => 'Manga',
+                    'id_thesaurus' => 'DOCU00020001',
+                    'code' => 'Manga',
+                    'rules' => '{"label":" 90$t "}']);
+
+    $this->fixture('Class_CodifThesaurus',
+                   ['id' => 31,
+                    'libelle' => 'Experimentale',
+                    'libelle_facette' => 'Experimentale',
+                    'id_origine' => 30,
+                    'id_thesaurus' => 'MUSI0001',
+                    'code' => 'EXPE',
+                    'rules' => '{"label":" 95$t "}']);
+
+    $this->fixture('Class_Article',
+                   ['id' => 1,
+                    'titre' => 'News of the sprint',
+                    'id_user' => 1,
+                    'indexation' => 1,
+                    'contenu' => 'Articles are going to be auto indexed !',
+                    'domaine_ids' => '3']);
+
+    Class_Article::find(1)->index();
+
+    $this->mock_sql
+      ->whenCalled('fetchAll')
+      ->with("select id_notice, facettes from notices Where MATCH(facettes) AGAINST('+(HDOCU0001 HDOCU0002 HDOCU00020001) +(HMUSI0001)' IN BOOLEAN MODE)", true, false)
+      ->answers([  [1, ''] ])
+
+      ->whenCalled('fetchOne')
+      ->answers(1)
+      ->whenCalled('fetchAll')
+      ->answers([Class_Notice::find(1)->toArray()])
+      ->beStrict();
+
+
+    Class_Profil::getCurrentProfil()
+      ->setCfgModules(['recherche' =>
+                       ['resultatsimple' =>
+                        ['liste_format' =>  Class_Systeme_ModulesAppli::LISTE_FORMAT_MUR]]]);
+
+    $this->dispatch('/recherche/simple/multifacets/HDOCU0001-HDOCU0002-HDOCU00020001-HMUSI0001', true);
+  }
+
+
+  /** @test */
+  public function newsOfTheSPrintShouldExistsAsANotice() {
+    $this->assertNotNull(Class_Notice::find(1));
+  }
+
+}
+
+
+
 class RechercheControlleSimpleActionWithEmptyDomainSettingsAndArticlesLinkedTest extends RechercheControllerNoticeTestCase {
   public function setUp() {
     parent::setUp();
-- 
GitLab