From 7e24c3a8e194a28c0dfef705a63f98b87b53cd9c Mon Sep 17 00:00:00 2001
From: Patrick Barroca <pbarroca@sandbox.pergame.net>
Date: Tue, 10 Nov 2015 11:13:18 +0100
Subject: [PATCH] rel #29947: handle events and custom fields deletion + tests

---
 library/Class/Import/Typo3.php           | 12 +++-
 library/Class/Import/Typo3/Db.php        |  5 ++
 tests/library/Class/Import/Typo3Test.php | 77 +++++++++++++++++++++++-
 3 files changed, 89 insertions(+), 5 deletions(-)

diff --git a/library/Class/Import/Typo3.php b/library/Class/Import/Typo3.php
index 3cdf1cbb2c6..c7509520c52 100644
--- a/library/Class/Import/Typo3.php
+++ b/library/Class/Import/Typo3.php
@@ -38,7 +38,7 @@ class Class_Import_Typo3 {
     $unknown_koha_urls = [];
 
   public function __construct() {
-    $this->t3db = new Class_Import_Typo3_DB();
+    $this->t3db = new Class_Import_Typo3_Db();
     $this->report = ['categories_created' => 0,
                      'domaine_created' => 0,
                      'categories_sites_created' => 0,
@@ -90,6 +90,12 @@ class Class_Import_Typo3 {
     $_SERVER['HTTP_HOST']='localhost';
     $logger = Class_Import_Typo3_Logs::getInstance();
 
+    if ('postprocess' == $what) {
+      $logger->addLogRow("\n\n ******** Post-Process effacement des articles pid=-1");
+      $this->postProcess();
+      return $logger->report();
+    }
+
     if ('update' == $what && !$last_update) {
       $logger->addErrorRow("Mode mise à jour demandé sans date de référence");
       return $logger->report();
@@ -898,7 +904,7 @@ class Class_Import_Typo3 {
 
 
   protected function _cleanArticles() {
-    foreach(['findRemovableArticles', 'findRemovableContents'] as $method)
+    foreach(['findRemovableArticles', 'findRemovableContents', 'findRemovableEvents'] as $method)
       $this->_cleanArticlesBy($method);
 
     return $this;
@@ -914,6 +920,6 @@ class Class_Import_Typo3 {
 
   protected function _cleanArticle($article) {
     $article->unindex();
-    $article->delete();
+    $article->deleteWithCustomFields();
   }
 }
\ No newline at end of file
diff --git a/library/Class/Import/Typo3/Db.php b/library/Class/Import/Typo3/Db.php
index dd018342cfd..d3d757cae8a 100644
--- a/library/Class/Import/Typo3/Db.php
+++ b/library/Class/Import/Typo3/Db.php
@@ -122,6 +122,11 @@ class Class_Import_Typo3_Db {
   }
 
 
+  public function findRemovableEvents() {
+    return $this->t3db->fetchAll('select uid from tx_cal_event where pid=-1');
+  }
+
+
   public function findAllContents($page_id=false) {
     $pid_sql='';
     if ($page_id)
diff --git a/tests/library/Class/Import/Typo3Test.php b/tests/library/Class/Import/Typo3Test.php
index dbea0557ed9..1c7b3a4c91e 100644
--- a/tests/library/Class/Import/Typo3Test.php
+++ b/tests/library/Class/Import/Typo3Test.php
@@ -24,12 +24,12 @@ include_once("Typo3Fixture.php");
 
 
 abstract class Import_Typo3TestCase extends ModelTestCase {
+  protected $_storm_default_to_volatile = true;
   protected $migration;
 
   public function setUp() {
     parent::setUp();
     Class_Import_Typo3_Logs::getInstance()->cleans();
-    Storm_Model_Loader::defaultToVolatile();
 
     $this->mock_sql = Storm_Test_ObjectWrapper::mock();
     $this->old_sql = Zend_Registry::get('sql');
@@ -58,7 +58,6 @@ abstract class Import_Typo3TestCase extends ModelTestCase {
 
   public function tearDown() {
     Zend_Registry::set('sql', $this->old_sql);
-    Storm_Model_Loader::defaultToDb();
     parent::tearDown();
   }
 }
@@ -1109,4 +1108,78 @@ class Import_Typo3AdvicesTest extends Import_Typo3TestCase {
   public function userKeyShouldBeTestUser() {
     $this->assertEquals('--0--test_user', $this->advice->getUserKey());
   }
+}
+
+
+
+
+class Import_Typo3PostProcessTest extends Import_Typo3TestCase {
+  public function setUp() {
+    parent::setUp();
+
+    $this->fixture('Class_Article',
+                   ['id' => 123456789,
+                    'titre' => 'Mon super titre',
+                    'contenu' => 'Mon super contenu'])
+         ->setCustomField('uid_typo3', '3455')
+         ->saveWithCustomFields();
+
+    $this->fixture('Class_Article',
+                   ['id' => 888,
+                    'titre' => 'Mon super titre 2',
+                    'contenu' => 'Mon super contenu 2'])
+         ->setCustomField('uid_typo3', '4897')
+         ->saveWithCustomFields();
+
+    $this->fixture('Class_Article',
+                   ['id' => 555,
+                    'titre' => 'Mon super event',
+                    'contenu' => 'Mon super contenu'])
+         ->setCustomField('uid_typo3', '8938')
+         ->saveWithCustomFields();
+
+    $this->fixture('Class_Article',
+                   ['id' => 999,
+                    'titre' => 'Mon super titre 3',
+                    'contenu' => 'Mon super contenu 3'])
+         ->setCustomField('uid_typo3', '777')
+         ->saveWithCustomFields();
+
+    $this->migration
+      ->setTypo3DB($this->mock()
+                   ->whenCalled('findRemovableArticles')->answers([ ['uid' => '3455'] ])
+                   ->whenCalled('findRemovableContents')->answers([ ['uid' => '4897'] ])
+                   ->whenCalled('findRemovableEvents')->answers([ ['uid' => '8938'] ]))
+      ->postProcess();
+  }
+
+
+  /** @test */
+  public function shouldDeleteNews() {
+    $this->assertNull(Class_Article::find(123456789));
+  }
+
+
+  /** @test */
+  public function customFieldsShouldBeDeleted() {
+    $this->assertNull(Class_CustomField_Value::findFirstBy(['value' => '3455']));
+  }
+
+
+  /** @test */
+  public function shouldDeletePages() {
+    $this->assertNull(Class_Article::find(888));
+  }
+
+
+  /** @test */
+  public function shouldDeleteEvents() {
+    $this->assertNull(Class_Article::find(555));
+  }
+
+
+  /** @test */
+  public function shouldNotDeleteAllArticles() {
+    $this->assertNotNull(Class_Article::find(999));
+  }
 }
\ No newline at end of file
-- 
GitLab