From 847013aba91c7a3121921944c2b88820006b4720 Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@afi-sa.fr>
Date: Wed, 18 Nov 2015 11:18:53 +0100
Subject: [PATCH] rel #29947 : clean removables sitotheques

---
 library/Class/Import/Typo3.php           | 40 ++++++++++++++++--------
 tests/library/Class/Import/Typo3Test.php | 35 ++++++++++++++++++++-
 2 files changed, 61 insertions(+), 14 deletions(-)

diff --git a/library/Class/Import/Typo3.php b/library/Class/Import/Typo3.php
index c7509520c52..cde93a4ca6d 100644
--- a/library/Class/Import/Typo3.php
+++ b/library/Class/Import/Typo3.php
@@ -223,9 +223,8 @@ class Class_Import_Typo3 {
     $t3news = $this->t3db->findAllSitesSince($update_date);
 
     foreach($t3news as $new) {
-      if ($site = Class_Sitotheque::findFirstByCustomFieldValue(SELF::UID_TYPO3_CF,
-                                                                $new['uid'])) {
-        $this->updateSite($site,$new);
+      if ($site = $this->_getSite($new['uid'])) {
+        $this->updateSite($site, $new);
         continue;
       }
 
@@ -236,6 +235,11 @@ class Class_Import_Typo3 {
   }
 
 
+  protected function _getSite($uid) {
+    return Class_Sitotheque::findFirstByCustomFieldValue(SELF::UID_TYPO3_CF, $uid);
+  }
+
+
   public function importSites() {
     Class_Sitotheque::deleteBy([]);
 
@@ -899,27 +903,37 @@ class Class_Import_Typo3 {
 
 
   public function postProcess() {
-    $this->_cleanArticles();
+    $this
+      ->_cleanArticles()
+      ->_cleanSites();
+  }
+
+
+  protected function _cleanSites() {
+    $this->_cleanModelsBy('findRemovableArticles', '_getSite');
   }
 
 
   protected function _cleanArticles() {
-    foreach(['findRemovableArticles', 'findRemovableContents', 'findRemovableEvents'] as $method)
-      $this->_cleanArticlesBy($method);
+    foreach(['findRemovableArticles',
+             'findRemovableContents',
+             'findRemovableEvents'] as $method)
+      $this->_cleanModelsBy($method, '_getArticle');
 
     return $this;
   }
 
 
-  protected function _cleanArticlesBy($method) {
-    foreach($this->t3db->$method() as $item)
-      if ($article = $this->_getArticle($item['uid']))
-        $this->_cleanArticle($article);
+  protected function _cleanModelsBy($db_method, $model_getter) {
+    foreach($this->t3db->$db_method() as $item)
+      if ($model = $this->$model_getter($item['uid']))
+        $this->_cleanModel($model);
   }
 
 
-  protected function _cleanArticle($article) {
-    $article->unindex();
-    $article->deleteWithCustomFields();
+  /** @param $model use Trait_Indexable, Trait_CustomFields */
+  protected function _cleanModel($model) {
+    $model->unindex();
+    $model->deleteWithCustomFields();
   }
 }
\ No newline at end of file
diff --git a/tests/library/Class/Import/Typo3Test.php b/tests/library/Class/Import/Typo3Test.php
index 1c7b3a4c91e..eb663126397 100644
--- a/tests/library/Class/Import/Typo3Test.php
+++ b/tests/library/Class/Import/Typo3Test.php
@@ -1145,9 +1145,30 @@ class Import_Typo3PostProcessTest extends Import_Typo3TestCase {
          ->setCustomField('uid_typo3', '777')
          ->saveWithCustomFields();
 
+    $this->fixture('Class_Sitotheque',
+                   ['id' => 1004,
+                    'titre' => 'Testing sito',
+                    'date_maj' => '2015-11-18 10:47:37',
+                    'url' => 'http://my.server.com',
+                    'tags' => '',
+                    'description' => 'My super duper hyper cooler desc'])
+         ->setCustomField('uid_typo3', '8883')
+         ->saveWithCustomFields();
+
+    $this->fixture('Class_Sitotheque',
+                   ['id' => 1005,
+                    'titre' => 'Testing sito not removable',
+                    'date_maj' => '2015-11-18 10:12:37',
+                    'url' => 'http://your.server.com',
+                    'tags' => '',
+                    'description' => 'My desc'])
+         ->setCustomField('uid_typo3', '8882')
+         ->saveWithCustomFields();
+
     $this->migration
       ->setTypo3DB($this->mock()
-                   ->whenCalled('findRemovableArticles')->answers([ ['uid' => '3455'] ])
+                   ->whenCalled('findRemovableArticles')->answers([['uid' => '3455'],
+                                                                   ['uid' => '8883']])
                    ->whenCalled('findRemovableContents')->answers([ ['uid' => '4897'] ])
                    ->whenCalled('findRemovableEvents')->answers([ ['uid' => '8938'] ]))
       ->postProcess();
@@ -1182,4 +1203,16 @@ class Import_Typo3PostProcessTest extends Import_Typo3TestCase {
   public function shouldNotDeleteAllArticles() {
     $this->assertNotNull(Class_Article::find(999));
   }
+
+
+  /** @test */
+  public function shouldDeleteSito() {
+    $this->assertNull(Class_Sitotheque::find(1004));
+  }
+
+
+  /** @test */
+  public function shouldNotDeleteAllSitos() {
+    $this->assertNotNull(Class_Sitotheque::find(1005));
+  }
 }
\ No newline at end of file
-- 
GitLab