From 194d5e4cdd359c6f02f63e5cd6f7ec19ddcd3dd3 Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Fri, 4 Sep 2015 15:10:55 +0200
Subject: [PATCH] hotline #29082 fix articles and sito indexation into domains

---
 library/Class/NoticeDomain.php              | 10 +++-------
 library/Trait/Indexable.php                 | 10 +++++-----
 tests/library/Class/AgendaSQYImportTest.php | 20 ++++++++++----------
 tests/library/Class/Import/Typo3Test.php    |  4 ++--
 4 files changed, 20 insertions(+), 24 deletions(-)

diff --git a/library/Class/NoticeDomain.php b/library/Class/NoticeDomain.php
index 383b1976bd8..5d2f5bffa13 100644
--- a/library/Class/NoticeDomain.php
+++ b/library/Class/NoticeDomain.php
@@ -29,13 +29,9 @@ class NoticeDomainLoader extends Storm_Model_Loader {
     }
 
     $all_domain_ids =  array_filter(array_unique($all_domain_ids));
-
-    $domains = [];
-    foreach ($all_domain_ids as $id) {
-      $domains[] = Class_Catalogue::find($id);
-    }
-    return $domains;
-
+    return $all_domain_ids
+      ? Class_Catalogue::findAllBy(['id_catalogue' => $all_domain_ids])
+      : [];
   }
 
   public function getClesNoticesForDomain($domain_id) {
diff --git a/library/Trait/Indexable.php b/library/Trait/Indexable.php
index 48e4947df09..9c82a864404 100644
--- a/library/Trait/Indexable.php
+++ b/library/Trait/Indexable.php
@@ -74,16 +74,16 @@ trait Trait_Indexable {
   public function unindex() {
     $alpha_key = $this->getAlphaKey();
 
-    $domains = $this->getDomaines();
-    if (empty($domains)) {
+    $domains_ids = Class_Catalogue::getIds($this->getDomaines());
+    if (empty($domains_ids)) {
       Class_NoticeDomain::deleteBy(['record_alpha_key' => $alpha_key,
                                     'panier_id' => 0]);
     }
 
-    $domains_in_db = Class_NoticeDomain::getDomainsForRecordAlphaKey($alpha_key);
+    $domains_in_db_ids = Class_Catalogue::getIds(Class_NoticeDomain::getDomainsForRecordAlphaKey($alpha_key));
 
-    foreach(array_diff($domains_in_db, $domains) as $domain)
-      Class_NoticeDomain::deleteBy(['domain_id' => $domain->getId()]);
+    if ($to_delete = array_diff($domains_in_db_ids, $domains_ids))
+      Class_NoticeDomain::deleteBy(['domain_id' => $to_delete]);
   }
 
 
diff --git a/tests/library/Class/AgendaSQYImportTest.php b/tests/library/Class/AgendaSQYImportTest.php
index 7da00334b86..f8eafbd9ddc 100644
--- a/tests/library/Class/AgendaSQYImportTest.php
+++ b/tests/library/Class/AgendaSQYImportTest.php
@@ -78,7 +78,7 @@ class AgendaSQYImportTest extends Storm_Test_ModelTestCase {
 
     Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Catalogue')
     ->whenCalled('save')->answers(true)
-    ->whenCalled('findAllBy')->answers(null)
+    ->whenCalled('findAllBy')->answers([])
     ->whenCalled('findAllBy')->with(['where' => 'parent_id is null',
                                      'order' => 'libelle'])->answers([$domaine_musique]);
 
@@ -96,7 +96,7 @@ class AgendaSQYImportTest extends Storm_Test_ModelTestCase {
 
     Storm_Test_ObjectWrapper::onLoaderOfModel('Class_ArticleCategorie')
     ->whenCalled('save')->answers(true)
-    ->whenCalled('findAllBy')->answers(null)
+    ->whenCalled('findAllBy')->answers([])
     ->whenCalled('findAllBy')->with(['id_cat_mere' => 0])->answers([$categorie_musique]);
 
 
@@ -379,8 +379,8 @@ class AgendaSQYImportTest extends Storm_Test_ModelTestCase {
   }
 
 
-  /** 
-   * @test 
+  /**
+   * @test
    * @depends lieuFermeBelEbatShouldHavePhone0130483344
    */
   public function lieuFermeBelEbatMailShouldBeSet($ferme) {
@@ -388,16 +388,16 @@ class AgendaSQYImportTest extends Storm_Test_ModelTestCase {
   }
 
 
-  /** 
-   * @test 
+  /**
+   * @test
    * @depends lieuFermeBelEbatShouldHavePhone0130483344
    */
   public function lieuFermeBelEbatLatitueShouldBe48_7($ferme) {
     $this->assertEquals('48.770757', $ferme->getLatitude());
   }
 
-  /** 
-   * @test 
+  /**
+   * @test
    * @depends lieuFermeBelEbatShouldHavePhone0130483344
    */
   public function lieuFermeBelEbatLongitudeShouldBe2($ferme) {
@@ -405,8 +405,8 @@ class AgendaSQYImportTest extends Storm_Test_ModelTestCase {
   }
 
 
-  /** 
-   * @test 
+  /**
+   * @test
    * @depends lieuFermeBelEbatShouldHavePhone0130483344
    */
   public function lieuFermeBelEbatUrlShouldBeLaFermeDeBelEbatDotFr($ferme) {
diff --git a/tests/library/Class/Import/Typo3Test.php b/tests/library/Class/Import/Typo3Test.php
index f26b41ce45e..38efa977bfc 100644
--- a/tests/library/Class/Import/Typo3Test.php
+++ b/tests/library/Class/Import/Typo3Test.php
@@ -271,7 +271,7 @@ class Import_Typo3ArticleTest extends Import_Typo3TestCase {
 
   /** @test */
   public function articleABordDuBateauPirateShouldBeInDomainAutres() {
-    $this->assertEquals(['Divers', 'NOS TOPS 5', 'Autres'],
+    $this->assertEquals(['Divers', 'Autres', 'NOS TOPS 5'],
                         $this->bateau_pirate->getDomaineLibelles());
   }
 
@@ -601,7 +601,7 @@ class Import_Typo3SitothequeTest extends Import_Typo3TestCase {
 
   /** @test */
   public function museoParcShouldBeInDomainAutres() {
-    $this->assertEquals(['Divers', 'NOS TOPS 5', 'Autres'],
+    $this->assertEquals(['Divers', 'Autres', 'NOS TOPS 5'],
                         $this->sito->getDomaineLibelles());
   }
 
-- 
GitLab