[FEATURE] Allow multiple category fields per table
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Category / CategoryRegistryTest.php
index 08a6423..1f74533 100644 (file)
@@ -52,7 +52,13 @@ class CategoryRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        '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()
+                               ),
+                       );
                }
        }
 
@@ -64,13 +70,6 @@ class CategoryRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                unset($this->fixture);
        }
 
-       /**
-        * @test
-        */
-       public function isRegistryEmptyByDefault() {
-               $this->assertEquals(array(), $this->fixture->get());
-       }
-
        /**
         * @test
         */
@@ -82,40 +81,37 @@ class CategoryRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function doesAddReturnFalseOnUndefinedTable() {
-               $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
-
-               $this->assertFalse(
-                       $this->fixture->add('test_extension_a', $this->tables['first'], 'categories')
-               );
+               $this->assertFalse($this->fixture->add('test_extension_a', 'undefined_table', 'categories'));
        }
 
        /**
         * @test
-        * @expectedException \RuntimeException
+        * @expectedException \InvalidArgumentException
         * @expectedExceptionCode 1369122038
         */
-       public function doesAddThrowExceptionOnEmptyAdds() {
+       public function doesAddThrowExceptionOnEmptyTablename() {
                $this->fixture->add('test_extension_a', '', 'categories');
        }
 
+       /**
+        * @test
+        * @expectedException \InvalidArgumentException
+        * @expectedExceptionCode 1378976970
+        */
+       public function doesAddThrowExceptionIfOptionsIsNotAnArray() {
+               $this->fixture->add('test_extension_a', $this->tables['first'], 'categories', 'not_an_array');
+       }
+
        /**
         * @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']
-               );
+               $this->fixture->add('test_extension_a', $this->tables['second'], 'categories');
+               $this->fixture->applyTca();
 
-               $this->assertEquals(
-                       'categories',
-                       $registry['test_extension_b'][$this->tables['second']]['fieldName']
-               );
+               $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
+               $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
        }
 
        /**
@@ -124,31 +120,34 @@ class CategoryRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        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->fixture->applyTca();
 
-               $this->assertEquals(
-                       'categories',
-                       $registry['test_extension_a'][$this->tables['first']]['fieldName']
-               );
+               $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
+               $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
+       }
 
-               $this->assertEquals(
-                       'categories',
-                       $registry['test_extension_b'][$this->tables['second']]['fieldName']
-               );
+       /**
+        * @test
+        */
+       public function areElementsOfDifferentExtensionsOnSameTableRegistered() {
+               $this->fixture->add('test_extension_a', $this->tables['first'], 'categories1');
+               $this->fixture->add('test_extension_b', $this->tables['first'], 'categories2');
+               $this->fixture->applyTca();
+
+               $this->assertArrayHasKey('categories1', $GLOBALS['TCA'][$this->tables['first']]['columns']);
+               $this->assertArrayHasKey('categories2', $GLOBALS['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->fixture->add('test_extension_a', $this->tables['first'], 'categories1');
+               $this->fixture->add('test_extension_a', $this->tables['first'], 'categories2');
+               $this->fixture->applyTca();
 
-               $this->assertEquals(
-                       $this->tables['first'],
-                       $registry['test_extension_a'][$this->tables['first']]['fieldName']
-               );
+               $this->assertArrayHasKey('categories1', $GLOBALS['TCA'][$this->tables['first']]['columns']);
+               $this->assertArrayHasKey('categories2', $GLOBALS['TCA'][$this->tables['first']]['columns']);
        }
 
        /**
@@ -189,44 +188,79 @@ class CategoryRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = $this->tables['first'] . ',' . $this->tables['second'];
                $this->fixture->applyTca();
 
-               $registry = $this->fixture->get();
-               $this->assertCount(2, $registry['core']);
-               $this->assertSame(key($registry['core']), $this->tables['first']);
+               $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
+               $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
        }
 
        /**
         * @test
         */
-       public function areEmptyStringDefaultCategorizedTablesLoaded() {
-               $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = '';
+       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();
 
-               $registry = $this->fixture->get();
-               $this->assertNull($registry['core']);
+               $this->assertNotEmpty($GLOBALS['TCA'][$this->tables['first']]['columns']['categories']);
+               $this->assertNotEmpty($GLOBALS['TCA'][$this->tables['second']]['columns']['categories']);
+       }
+
+       /**
+        * @test
+        */
+       public function isRegisteredReturnsTrueIfElementIsAlreadyRegistered() {
+               $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
+               $this->assertTrue($this->fixture->isRegistered($this->tables['first'], 'categories'));
        }
 
        /**
         * @test
         */
-       public function areNullDefaultCategorizedTablesLoaded() {
-               $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = NULL;
+       public function isRegisteredReturnsFalseIfElementIsNotRegistered() {
+               $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
+               $this->assertFalse($this->fixture->isRegistered($this->tables['first'], '_not_registered'));
+               $this->assertFalse($this->fixture->isRegistered($this->tables['second'], 'categories'));
+       }
+
+       /**
+        * @test
+        */
+       public function tabIsAddedForElement() {
+               $this->fixture->add('text_extension_a', $this->tables['first']);
                $this->fixture->applyTca();
 
-               $registry = $this->fixture->get();
-               $this->assertNull($registry['core']);
+               foreach($GLOBALS['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 canApplyTca() {
-               $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
-               $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
+       public function tabIsNotAddedForElementIfFieldListIsSpecified() {
+               $this->fixture->add('text_extension_a', $this->tables['first'], 'categories', array('fieldList' => 'categories'));
+               $this->fixture->applyTca();
+
+               foreach($GLOBALS['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->fixture->add('text_extension_a', $this->tables['first'], 'categories1');
+               $this->fixture->add('text_extension_a', $this->tables['first'], 'categories2');
                $this->fixture->applyTca();
-               foreach ($this->tables as $table) {
-                       $this->assertNotEmpty($GLOBALS['TCA'][$table]['columns']['categories']);
+
+               foreach($GLOBALS['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