[TASK] Modernize category collection functional tests 43/57143/5
authorChristian Kuhn <lolli@schwarzbu.ch>
Thu, 7 Jun 2018 21:15:51 +0000 (23:15 +0200)
committerWouter Wolters <typo3@wouterwolters.nl>
Thu, 7 Jun 2018 21:38:58 +0000 (23:38 +0200)
Use a dummy extension and .csv imports from the
functional test framework instead of creating
tables and records on the fly.

Resolves: #85179
Releases: master
Change-Id: Ia8f077984e588a2bb142a0e516aad198f914bec9
Reviewed-on: https://review.typo3.org/57143
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
typo3/sysext/core/Tests/Functional/Category/Collection/CategoryCollectionTest.php
typo3/sysext/core/Tests/Functional/Category/Collection/Fixtures/DataSet/categoryRelations.csv [new file with mode: 0644]
typo3/sysext/core/Tests/Functional/Category/Collection/Fixtures/Extensions/test/Configuration/TCA/tx_test_test.php [new file with mode: 0644]
typo3/sysext/core/Tests/Functional/Category/Collection/Fixtures/Extensions/test/ext_emconf.php [new file with mode: 0644]
typo3/sysext/core/Tests/Functional/Category/Collection/Fixtures/Extensions/test/ext_tables.sql [new file with mode: 0644]

index 6a4ba5f..7b59b59 100644 (file)
@@ -14,48 +14,25 @@ namespace TYPO3\CMS\Core\Tests\Functional\Category\Collection;
  * The TYPO3 project - inspiring people to share!
  */
 
-use Doctrine\DBAL\Schema\Column;
-use Doctrine\DBAL\Schema\TableDiff;
-use Doctrine\DBAL\Types\Type;
 use TYPO3\CMS\Core\Category\Collection\CategoryCollection;
-use TYPO3\CMS\Core\Database\Connection;
-use TYPO3\CMS\Core\Database\ConnectionPool;
-use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
 
 /**
- * Test case for \TYPO3\CMS\Core\Category\Collection\CategoryCollection
+ * Test case
  */
-class CategoryCollectionTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
+class CategoryCollectionTest extends FunctionalTestCase
 {
     /**
-     * @var CategoryCollection
-     */
-    private $subject;
-
-    /**
-     * @var string
-     */
-    private $tableName = 'tx_foo_5001615c50bed';
-
-    /**
-     * @var array
-     */
-    private $tables = ['sys_category', 'sys_category_record_mm'];
-
-    /**
-     * @var int
-     */
-    private $categoryUid = 0;
-
-    /**
      * @var array
      */
     private $collectionRecord = [];
 
     /**
-     * @var int
+     * @var array Load test fixture extension
      */
-    private $numberOfRecords = 5;
+    protected $testExtensionsToLoad = [
+        'typo3/sysext/core/Tests/Functional/Category/Collection/Fixtures/Extensions/test'
+    ];
 
     /**
      * Sets up this test suite.
@@ -63,29 +40,13 @@ class CategoryCollectionTest extends \TYPO3\TestingFramework\Core\Functional\Fun
     protected function setUp()
     {
         parent::setUp();
-        $this->subject = GeneralUtility::makeInstance(CategoryCollection::class, $this->tableName);
         $this->collectionRecord = [
             'uid' => 0,
             'title' => $this->getUniqueId('title'),
             'description' => $this->getUniqueId('description'),
-            'table_name' => $this->tableName,
+            'table_name' => 'tx_test_test',
         ];
-        $GLOBALS['TCA'][$this->tableName] = ['ctrl' => []];
-        // prepare environment
-        $this->createDummyTable();
-        $this->populateDummyTable();
-        $this->prepareTables();
-        $this->makeRelationBetweenCategoryAndDummyTable();
-    }
-
-    /**
-     * Tears down this test suite.
-     */
-    protected function tearDown()
-    {
-        $this->purgePreparedTables();
-        $this->dropDummyTable();
-        parent::tearDown();
+        $this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/categoryRelations.csv');
     }
 
     /**
@@ -94,12 +55,13 @@ class CategoryCollectionTest extends \TYPO3\TestingFramework\Core\Functional\Fun
      */
     public function checkIfFromArrayMethodSetCorrectProperties()
     {
-        $this->subject->fromArray($this->collectionRecord);
-        $this->assertEquals($this->collectionRecord['uid'], $this->subject->getIdentifier());
-        $this->assertEquals($this->collectionRecord['uid'], $this->subject->getUid());
-        $this->assertEquals($this->collectionRecord['title'], $this->subject->getTitle());
-        $this->assertEquals($this->collectionRecord['description'], $this->subject->getDescription());
-        $this->assertEquals($this->collectionRecord['table_name'], $this->subject->getItemTableName());
+        $subject = new CategoryCollection('tx_test_test');
+        $subject->fromArray($this->collectionRecord);
+        $this->assertEquals($this->collectionRecord['uid'], $subject->getIdentifier());
+        $this->assertEquals($this->collectionRecord['uid'], $subject->getUid());
+        $this->assertEquals($this->collectionRecord['title'], $subject->getTitle());
+        $this->assertEquals($this->collectionRecord['description'], $subject->getDescription());
+        $this->assertEquals($this->collectionRecord['table_name'], $subject->getItemTableName());
     }
 
     /**
@@ -128,9 +90,10 @@ class CategoryCollectionTest extends \TYPO3\TestingFramework\Core\Functional\Fun
      */
     public function getCollectedRecordsReturnsEmptyRecordSet()
     {
+        $subject = new CategoryCollection('tx_test_test');
         $method = new \ReflectionMethod(CategoryCollection::class, 'getCollectedRecords');
         $method->setAccessible(true);
-        $records = $method->invoke($this->subject);
+        $records = $method->invoke($subject);
         $this->assertInternalType('array', $records);
         $this->assertEmpty($records);
     }
@@ -159,16 +122,16 @@ class CategoryCollectionTest extends \TYPO3\TestingFramework\Core\Functional\Fun
     public function canLoadADummyCollectionFromDatabase()
     {
         /** @var $collection CategoryCollection */
-        $collection = CategoryCollection::load($this->categoryUid, true, $this->tableName);
+        $collection = CategoryCollection::load(1, true, 'tx_test_test');
         // Check the number of record
-        $this->assertEquals($this->numberOfRecords, $collection->count());
+        $this->assertEquals(5, $collection->count());
         // Check that the first record is the one expected
         $queryBuilder = $this->getConnectionPool()
-            ->getQueryBuilderForTable($this->tableName);
+            ->getQueryBuilderForTable('tx_test_test');
         $queryBuilder->getRestrictions()->removeAll();
         $statement = $queryBuilder
             ->select('*')
-            ->from($this->tableName)
+            ->from('tx_test_test')
             ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT)))
             ->execute();
         $record = $statement->fetch();
@@ -176,14 +139,14 @@ class CategoryCollectionTest extends \TYPO3\TestingFramework\Core\Functional\Fun
         $this->assertEquals($record, $collection->current());
         // Add a new record
         $fakeRecord = [
-            'uid' => $this->numberOfRecords + 1,
+            'uid' => 6,
             'pid' => 0,
             'title' => $this->getUniqueId('title'),
             'categories' => 0
         ];
         // Check the number of records
         $collection->add($fakeRecord);
-        $this->assertEquals($this->numberOfRecords + 1, $collection->count());
+        $this->assertEquals(6, $collection->count());
     }
 
     /**
@@ -191,17 +154,17 @@ class CategoryCollectionTest extends \TYPO3\TestingFramework\Core\Functional\Fun
      */
     public function canLoadADummyCollectionFromDatabaseAndAddRecord()
     {
-        $collection = CategoryCollection::load($this->categoryUid, true, $this->tableName);
+        $collection = CategoryCollection::load(1, true, 'tx_test_test');
         // Add a new record
         $fakeRecord = [
-            'uid' => $this->numberOfRecords + 1,
+            'uid' => 6,
             'pid' => 0,
             'title' => $this->getUniqueId('title'),
             'categories' => 0
         ];
         // Check the number of records
         $collection->add($fakeRecord);
-        $this->assertEquals($this->numberOfRecords + 1, $collection->count());
+        $this->assertEquals(6, $collection->count());
     }
 
     /**
@@ -210,7 +173,7 @@ class CategoryCollectionTest extends \TYPO3\TestingFramework\Core\Functional\Fun
     public function canLoadADummyCollectionWithoutContentFromDatabase()
     {
         /** @var $collection CategoryCollection */
-        $collection = CategoryCollection::load($this->categoryUid, false, $this->tableName);
+        $collection = CategoryCollection::load(1, false, 'tx_test_test');
         // Check the number of record
         $this->assertEquals(0, $collection->count());
     }
@@ -228,143 +191,7 @@ class CategoryCollectionTest extends \TYPO3\TestingFramework\Core\Functional\Fun
             ->getConnectionForTable('sys_category_record_mm')
             ->update('sys_category_record_mm', $fakeName, ['uid_foreign' => 1]);
         // Check the number of records
-        $collection = CategoryCollection::load($this->categoryUid, true, $this->tableName);
-        $this->assertEquals($this->numberOfRecords - 1, $collection->count());
-    }
-
-    /********************/
-    /* INTERNAL METHODS */
-    /********************/
-    /**
-     * Create dummy table for testing purpose
-     */
-    private function populateDummyTable()
-    {
-        for ($index = 1; $index <= $this->numberOfRecords; $index++) {
-            $values = [
-                'title' => $this->getUniqueId('title')
-            ];
-            $this->getConnectionPool()
-                ->getConnectionForTable($this->tableName)
-                ->insert($this->tableName, $values);
-        }
-    }
-
-    /**
-     * Make relation between tables
-     */
-    private function makeRelationBetweenCategoryAndDummyTable()
-    {
-        for ($index = 1; $index <= $this->numberOfRecords; $index++) {
-            $values = [
-                'uid_local' => $this->categoryUid,
-                'uid_foreign' => $index,
-                'tablenames' => $this->tableName,
-                'fieldname' => 'categories'
-            ];
-            $this->getConnectionPool()
-                ->getConnectionForTable('sys_category_record_mm')
-                ->insert('sys_category_record_mm', $values);
-        }
-    }
-
-    /**
-     * Create dummy table for testing purpose
-     */
-    private function createDummyTable()
-    {
-        $connection = $this->getConnectionPool()
-            ->getConnectionForTable($this->tableName);
-        $currentSchema = $connection->getSchemaManager()->createSchema();
-        $targetSchema = clone $currentSchema;
-
-        $table = $targetSchema->createTable($this->tableName);
-        $table->addColumn('uid', Type::INTEGER, ['length' => 11, 'unsigned' => true, 'autoincrement' => true]);
-        $table->addColumn('pid', Type::INTEGER, ['length' => 11, 'notnull' => true, 'default' => 0]);
-        $table->addColumn('title', Type::STRING);
-        $table->addColumn('tcategories', Type::INTEGER, ['length' => 11, 'unsigned' => true, 'notnull' => true, 'default' => 0]);
-        $table->addColumn('sys_category_is_dummy_record', Type::INTEGER, ['length' => 11, 'unsigned' => true, 'notnull' => true, 'default' => 0]);
-        $table->setPrimaryKey(['uid']);
-
-        $queries = $currentSchema->getMigrateToSql($targetSchema, $connection->getDatabasePlatform());
-        foreach ($queries as $query) {
-            $connection->query($query);
-        }
-    }
-
-    /**
-     * Drop dummy table
-     */
-    private function dropDummyTable()
-    {
-        $connection = $this->getConnectionPool()
-            ->getConnectionForTable($this->tableName);
-        $currentSchema = $connection->getSchemaManager()->createSchema();
-        $targetSchema = clone $currentSchema;
-
-        $targetSchema->dropTable($this->tableName);
-
-        $queries = $currentSchema->getMigrateToSql($targetSchema, $connection->getDatabasePlatform());
-        foreach ($queries as $query) {
-            $connection->query($query);
-        }
-    }
-
-    /**
-     * Add is_dummy_record record and create dummy record
-     */
-    private function prepareTables()
-    {
-        $connection = $this->getConnectionPool()
-            ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
-        $currentSchema = $connection->getSchemaManager()->createSchema();
-        $targetSchema = clone $currentSchema;
-
-        $columnOptions = ['length' => 1, 'unsigned' => true, 'notnull' => true, 'default' => 0];
-        foreach ($this->tables as $table) {
-            $targetSchema
-                ->getTable($table)
-                ->addColumn('is_dummy_record', Type::SMALLINT, $columnOptions);
-        }
-
-        $queries = $currentSchema->getMigrateToSql($targetSchema, $connection->getDatabasePlatform());
-        foreach ($queries as $query) {
-            $connection->query($query);
-        }
-
-        $values = [
-            'title' => $this->getUniqueId('title'),
-            'l10n_diffsource' => '',
-            'description' => '',
-            'is_dummy_record' => 1
-        ];
-
-        $connection->insert('sys_category', $values, [ 'l10n_diffsource' => Connection::PARAM_LOB ]);
-        $this->categoryUid = $connection->lastInsertId('sys_category');
-    }
-
-    /**
-     * Drops previously added dummy columns from core tables.
-     *
-     * @throws \Doctrine\DBAL\DBALException
-     * @throws \Doctrine\DBAL\Schema\SchemaException
-     * @see prepareTables()
-     */
-    private function purgePreparedTables()
-    {
-        $connection = $this->getConnectionPool()
-            ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
-
-        foreach ($this->tables as $table) {
-            $diff = new TableDiff(
-                $table,
-                [],
-                [],
-                [
-                    'is_dummy_record' => new Column('is_dummy_record', Type::getType(Type::SMALLINT))
-                ]
-            );
-            $connection->getSchemaManager()->alterTable($diff);
-        }
+        $collection = CategoryCollection::load(1, true, 'tx_test_test');
+        $this->assertEquals(4, $collection->count());
     }
 }
diff --git a/typo3/sysext/core/Tests/Functional/Category/Collection/Fixtures/DataSet/categoryRelations.csv b/typo3/sysext/core/Tests/Functional/Category/Collection/Fixtures/DataSet/categoryRelations.csv
new file mode 100644 (file)
index 0000000..95abab4
--- /dev/null
@@ -0,0 +1,17 @@
+"tx_test_test",,,,
+,"uid","pid","title",
+,1,0,foo1,
+,2,0,foo2,
+,3,0,foo3,
+,4,0,foo4,
+,5,0,foo5,
+"sys_category",,,,
+,"uid","pid","title",
+,1,0,cat1,
+"sys_category_record_mm",,,,
+,"uid_local","uid_foreign","tablenames","fieldname"
+,1,1,tx_test_test,categories
+,1,2,tx_test_test,categories
+,1,3,tx_test_test,categories
+,1,4,tx_test_test,categories
+,1,5,tx_test_test,categories
\ No newline at end of file
diff --git a/typo3/sysext/core/Tests/Functional/Category/Collection/Fixtures/Extensions/test/Configuration/TCA/tx_test_test.php b/typo3/sysext/core/Tests/Functional/Category/Collection/Fixtures/Extensions/test/Configuration/TCA/tx_test_test.php
new file mode 100644 (file)
index 0000000..65ac37e
--- /dev/null
@@ -0,0 +1,5 @@
+<?php
+return [
+    'ctrl' => [],
+    'columns' => [],
+];
diff --git a/typo3/sysext/core/Tests/Functional/Category/Collection/Fixtures/Extensions/test/ext_emconf.php b/typo3/sysext/core/Tests/Functional/Category/Collection/Fixtures/Extensions/test/ext_emconf.php
new file mode 100644 (file)
index 0000000..dfc04bd
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+$EM_CONF[$_EXTKEY] = [
+    'title' => 'test extension',
+    'description' => '',
+    'category' => '',
+    'version' => '9.3.0',
+    'state' => 'beta',
+    'uploadfolder' => 0,
+    'createDirs' => '',
+    'clearCacheOnLoad' => 0,
+    'author' => 'Christian Kuhn',
+    'author_email' => 'lolli@schwarzbu.ch',
+    'author_company' => '',
+    'constraints' => [
+        'depends' => [
+            'typo3' => '9.3.0',
+        ],
+        'conflicts' => [],
+        'suggests' => [],
+    ],
+];
diff --git a/typo3/sysext/core/Tests/Functional/Category/Collection/Fixtures/Extensions/test/ext_tables.sql b/typo3/sysext/core/Tests/Functional/Category/Collection/Fixtures/Extensions/test/ext_tables.sql
new file mode 100644 (file)
index 0000000..be9b576
--- /dev/null
@@ -0,0 +1,6 @@
+#
+# Table structure for table 'tx_test_test'
+#
+CREATE TABLE tx_test_test (
+       title tinytext
+);