[BUGFIX] Fix broken table name mapping for namespaced extension models
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Mon, 8 Oct 2012 06:23:29 +0000 (08:23 +0200)
committerMarkus Günther <mail@markus-guenther.de>
Sat, 13 Oct 2012 14:06:07 +0000 (16:06 +0200)
With the introduction of namespaces came an additional
resolving mechanism for tablenames. The names for tables that belong
to an extension (not sysext) miss the last part of their name.

This patch applies the missing class name part to the algorithm.

Change-Id: Ie804fc5aa211b51d7f79716156ad07daaa634e36
Fixes: #41662
Releases: 6.0
Reviewed-on: http://review.typo3.org/15386
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
Reviewed-by: Markus Günther
Tested-by: Markus Günther
typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapFactory.php
typo3/sysext/extbase/Tests/Unit/Persistence/Mapper/DataMapFactoryTest.php

index e4cbd2e..bfb8d6b 100644 (file)
@@ -131,20 +131,7 @@ class DataMapFactory implements \TYPO3\CMS\Core\SingletonInterface {
                }
                $recordType = NULL;
                $subclasses = array();
-               if (strpos($className, '\\') !== FALSE) {
-                       $classNameParts = explode('\\', $className, 4);
-                       if (isset($classNameParts[0]) && $classNameParts[0] === 'TYPO3' && isset($classNameParts[1]) && $classNameParts[1] === 'CMS') {
-                               $extensionKey = $classNameParts[2];
-                               $classNameWithoutVendorAndProduct = $classNameParts[3];
-                       } else {
-                               $extensionKey = $classNameParts[1];
-                               $classNameWithoutVendorAndProduct = $classNameParts[2];
-                       }
-                       $classNameWithoutVendorAndProduct = str_replace('\\', '_', $classNameWithoutVendorAndProduct);
-                       $tableName = strtolower('tx_' . $extensionKey . '_' . $classNameWithoutVendorAndProduct);
-               } else {
-                       $tableName = strtolower($className);
-               }
+               $tableName = $this->resolveTableName($className);
                $columnMapping = array();
                $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
                $classSettings = $frameworkConfiguration['persistence']['classes'][$className];
@@ -195,6 +182,28 @@ class DataMapFactory implements \TYPO3\CMS\Core\SingletonInterface {
        }
 
        /**
+        * Resolve the table name for the given class name
+        *
+        * @param string $className
+        * @return string The table name
+        */
+       protected function resolveTableName($className) {
+               if (strpos($className, '\\') !== FALSE) {
+                       $classNameParts = explode('\\', $className, 6);
+                       // Skip vendor and product name for core classes
+                       if (strpos($className, 'TYPO3\\CMS\\') === 0) {
+                               $classPartsToSkip = 2;
+                       } else {
+                               $classPartsToSkip = 1;
+                       }
+                       $tableName = 'tx_' . strtolower(implode('_', array_slice($classNameParts, $classPartsToSkip)));
+               } else {
+                       $tableName = strtolower($className);
+               }
+               return $tableName;
+       }
+
+       /**
         * Resolves all subclasses for the given set of (sub-)classes.
         * The whole classes configuration is used to determine all subclasses recursively.
         *
index 35f7f11..8ed4d02 100644 (file)
@@ -381,6 +381,25 @@ class DataMapFactoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $this->assertSame($dataMap->getSubclasses(), $expectedSubclasses);
        }
 
+       /**
+        * @return array
+        */
+       public function classNameTableNameMappings() {
+               return array(
+                       'Core classes' => array('TYPO3\\CMS\\Belog\\Domain\\Model\\LogEntry', 'tx_belog_domain_model_logentry'),
+                       'Extension classes' => array('ExtbaseTeam\\BlogExample\\Domain\\Model\\Blog', 'tx_blogexample_domain_model_blog'),
+                       'Extension classes without namespace' => array('Tx_News_Domain_Model_News', 'tx_news_domain_model_news'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider classNameTableNameMappings
+        */
+       public function resolveTableNameReturnsExpectedTablenames($className, $expected) {
+               $dataMapFactory = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapFactory', array('dummy'));
+               $this->assertSame($expected, $dataMapFactory->_call('resolveTableName', $className));
+       }
 }