[BUGFIX] Make category TCA changes cacheable
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Category / CategoryRegistryTest.php
index 0f248ee..b6943d7 100644 (file)
@@ -35,7 +35,7 @@ class CategoryRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        /**
         * @var \TYPO3\CMS\Core\Category\CategoryRegistry
         */
-       protected $fixture;
+       protected $subject;
 
        /**
         * @var array
@@ -46,110 +46,107 @@ class CategoryRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * Sets up this test suite.
         */
        protected function setUp() {
-               $this->fixture = new \TYPO3\CMS\Core\Category\CategoryRegistry();
+               $this->subject = new \TYPO3\CMS\Core\Category\CategoryRegistry();
                $this->tables = array(
                        'first' => uniqid('first'),
                        'second' => uniqid('second')
                );
                foreach ($this->tables as $tableName) {
-                       $GLOBALS['TCA'][$tableName] = array('ctrl' => array(), 'columns' => array());
+                       $GLOBALS['TCA'][$tableName] = array(
+                               'ctrl' => array(),
+                               'columns' => array(),
+                               'types' => array(
+                                       '1' => array()
+                               ),
+                       );
                }
        }
 
        /**
-        * Tears down this test suite.
+        * @test
         */
-       protected function tearDown() {
-               unset($this->tables);
-               unset($this->fixture);
+       public function doesAddReturnTrueOnDefinedTable() {
+               $this->assertTrue($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
        }
 
        /**
         * @test
+        * @expectedException \InvalidArgumentException
+        * @expectedExceptionCode 1369122038
         */
-       public function isRegistryEmptyByDefault() {
-               $this->assertEquals(array(), $this->fixture->get());
+       public function doesAddThrowExceptionOnEmptyTablename() {
+               $this->subject->add('test_extension_a', '', 'categories');
        }
 
        /**
         * @test
         */
-       public function doesAddReturnTrueOnDefinedTable() {
-               $this->assertTrue($this->fixture->add('test_extension_a', $this->tables['first'], 'categories'));
+       public function addCategoryRegistryTcaChangesReturnsGlobalTcaArray() {
+               $GLOBALS['TCA'] = array(
+                       'foo' => array(
+                               'bar' => 'baz'
+                       )
+               );
+               list($tca) = $this->subject->addCategoryRegistryTcaChanges($GLOBALS['TCA']);
+               $this->assertSame($tca, $GLOBALS['TCA']);
        }
 
        /**
         * @test
         */
-       public function doesAddReturnFalseOnUndefinedTable() {
-               $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
+       public function areMultipleElementsOfSameExtensionRegistered() {
+               $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
+               $this->subject->add('test_extension_a', $this->tables['second'], 'categories');
+               list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
 
-               $this->assertFalse(
-                       $this->fixture->add('test_extension_a', $this->tables['first'], 'categories')
-               );
+               $this->assertArrayHasKey('categories', $tca[$this->tables['first']]['columns']);
+               $this->assertArrayHasKey('categories', $tca[$this->tables['second']]['columns']);
        }
 
        /**
         * @test
         */
-       public function areMultipleElementsOfSameExtensionRegistered() {
-               $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
-               $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
-               $registry = $this->fixture->get();
-               ob_flush();
-
-               $this->assertEquals(
-                       'categories',
-                       $registry['test_extension_a'][$this->tables['first']]['fieldName']
-               );
+       public function areElementsOfDifferentExtensionsRegistered() {
+               $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
+               $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
+               list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
 
-               $this->assertEquals(
-                       'categories',
-                       $registry['test_extension_b'][$this->tables['second']]['fieldName']
-               );
+               $this->assertArrayHasKey('categories', $tca[$this->tables['first']]['columns']);
+               $this->assertArrayHasKey('categories', $tca[$this->tables['second']]['columns']);
        }
 
        /**
         * @test
         */
-       public function areElementsOfDifferentExtensionsRegistered() {
-               $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
-               $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
-               $registry = $this->fixture->get();
-
-               $this->assertEquals(
-                       'categories',
-                       $registry['test_extension_a'][$this->tables['first']]['fieldName']
-               );
+       public function areElementsOfDifferentExtensionsOnSameTableRegistered() {
+               $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
+               $this->subject->add('test_extension_b', $this->tables['first'], 'categories2');
+               list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
 
-               $this->assertEquals(
-                       'categories',
-                       $registry['test_extension_b'][$this->tables['second']]['fieldName']
-               );
+               $this->assertArrayHasKey('categories1', $tca[$this->tables['first']]['columns']);
+               $this->assertArrayHasKey('categories2', $tca[$this->tables['first']]['columns']);
        }
 
        /**
         * @test
         */
-       public function areElementsOnSameTableOverridden() {
-               $this->fixture->add('test_extension_a', $this->tables['first'], $this->tables['first']);
-               $this->fixture->add('test_extension_b', $this->tables['second'], $this->tables['second']);
-               $registry = $this->fixture->get();
+       public function areElementsOfSameExtensionOnSameTableRegistered() {
+               $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
+               $this->subject->add('test_extension_a', $this->tables['first'], 'categories2');
+               list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
 
-               $this->assertEquals(
-                       $this->tables['first'],
-                       $registry['test_extension_a'][$this->tables['first']]['fieldName']
-               );
+               $this->assertArrayHasKey('categories1', $tca[$this->tables['first']]['columns']);
+               $this->assertArrayHasKey('categories2', $tca[$this->tables['first']]['columns']);
        }
 
        /**
         * @test
         */
        public function areDatabaseDefinitionsOfAllElementsAvailable() {
-               $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
-               $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
-               $this->fixture->add('test_extension_c', $this->tables['first'], 'categories');
-               $definitions = $this->fixture->getDatabaseTableDefinitions();
+               $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
+               $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
+               $this->subject->add('test_extension_c', $this->tables['first'], 'categories');
+               $definitions = $this->subject->getDatabaseTableDefinitions();
                $matches = array();
                preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
                $this->assertEquals(2, count($matches[0]));
@@ -163,9 +160,9 @@ class CategoryRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function areDatabaseDefinitionsOfParticularExtensionAvailable() {
-               $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
-               $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
-               $definitions = $this->fixture->getDatabaseTableDefinition('test_extension_a');
+               $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
+               $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
+               $definitions = $this->subject->getDatabaseTableDefinition('test_extension_a');
                $matches = array();
                preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
                $this->assertEquals(1, count($matches[0]));
@@ -176,26 +173,81 @@ class CategoryRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        /**
         * @test
         */
-       public function areDefaultCategorizedTabledLoaded() {
+       public function areDefaultCategorizedTablesLoaded() {
                $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = $this->tables['first'] . ',' . $this->tables['second'];
-               $this->fixture->applyTca();
+               list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
 
-               $registry = $this->fixture->get();
-               $this->assertCount(2, $registry['core']);
-               $this->assertSame(key($registry['core']), $this->tables['first']);
+               $this->assertArrayHasKey('categories', $tca[$this->tables['first']]['columns']);
+               $this->assertArrayHasKey('categories', $tca[$this->tables['second']]['columns']);
        }
 
        /**
         * @test
         */
        public function canApplyTca() {
-               $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
-               $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
-               $this->fixture->applyTca();
-               foreach ($this->tables as $table) {
-                       $this->assertNotEmpty($GLOBALS['TCA'][$table]['columns']['categories']);
+               $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
+               $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
+               list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
+
+               $this->assertNotEmpty($tca[$this->tables['first']]['columns']['categories']);
+               $this->assertNotEmpty($tca[$this->tables['second']]['columns']['categories']);
+       }
+
+       /**
+        * @test
+        */
+       public function isRegisteredReturnsTrueIfElementIsAlreadyRegistered() {
+               $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
+               $this->assertTrue($this->subject->isRegistered($this->tables['first'], 'categories'));
+       }
+
+       /**
+        * @test
+        */
+       public function isRegisteredReturnsFalseIfElementIsNotRegistered() {
+               $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
+               $this->assertFalse($this->subject->isRegistered($this->tables['first'], '_not_registered'));
+               $this->assertFalse($this->subject->isRegistered($this->tables['second'], 'categories'));
+       }
+
+       /**
+        * @test
+        */
+       public function tabIsAddedForElement() {
+               $this->subject->add('text_extension_a', $this->tables['first']);
+               list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
+
+               foreach($tca[$this->tables['first']]['types'] as $typeConfig) {
+                       $this->assertContains('--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
                }
        }
-}
 
-?>
\ No newline at end of file
+       /**
+        * @test
+        */
+       public function tabIsNotAddedForElementIfFieldListIsSpecified() {
+               $this->subject->add('text_extension_a', $this->tables['first'], 'categories', array('fieldList' => 'categories'));
+               list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
+
+               foreach($tca[$this->tables['first']]['types'] as $typeConfig) {
+                       $this->assertNotContains('--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
+               }
+       }
+
+       /**
+        * @test
+        */
+       public function tabIsAddedOnlyOncePerTable() {
+               $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
+               $this->subject->add('text_extension_a', $this->tables['first'], 'categories2');
+               list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
+
+               foreach($tca[$this->tables['first']]['types'] as $typeConfig) {
+                       $this->assertSame(
+                               1, substr_count($typeConfig['showitem'], '--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category')
+                       );
+               }
+
+       }
+
+}