[BUGFIX] Make category TCA changes cacheable
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Category / CategoryRegistryTest.php
index dac5f40..b6943d7 100644 (file)
@@ -28,13 +28,14 @@ namespace TYPO3\CMS\Core\Tests\Unit\Category;
  * Testcase for CategoryRegistry
  *
  * @author Oliver Hader <oliver.hader@typo3.org>
+ * @author Fabien Udriot <fabien.udriot@typo3.org>
  */
 class CategoryRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
        /**
         * @var \TYPO3\CMS\Core\Category\CategoryRegistry
         */
-       protected $fixture;
+       protected $subject;
 
        /**
         * @var array
@@ -45,86 +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());
+                       $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->assertFalse($this->fixture->add('test_extension_a', uniqid('undefined'), '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->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']]);
-               $this->assertEquals('categories', $registry['test_extension_b'][$this->tables['second']]);
+       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->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']]);
-               $this->assertEquals('categories', $registry['test_extension_b'][$this->tables['second']]);
+       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->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();
-               $this->assertEquals($this->tables['first'], $registry['test_extension_a'][$this->tables['first']]);
+       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->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]));
@@ -138,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]));
@@ -148,6 +170,84 @@ class CategoryRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->assertEquals($matches[2][0], 'categories');
        }
 
-}
+       /**
+        * @test
+        */
+       public function areDefaultCategorizedTablesLoaded() {
+               $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = $this->tables['first'] . ',' . $this->tables['second'];
+               list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
+
+               $this->assertArrayHasKey('categories', $tca[$this->tables['first']]['columns']);
+               $this->assertArrayHasKey('categories', $tca[$this->tables['second']]['columns']);
+       }
+
+       /**
+        * @test
+        */
+       public function canApplyTca() {
+               $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']);
+               }
+       }
+
+       /**
+        * @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')
+                       );
+               }
 
-?>
\ No newline at end of file
+       }
+
+}