diff --git a/typo3/sysext/core/Classes/Resource/Service/FrontendContentAdapterService.php b/typo3/sysext/core/Classes/Resource/Service/FrontendContentAdapterService.php index 4e38085f8b0878a6f6c53cb75056bd19899141ce..38a0e008e8be060bbe82f4d9ae77031dc8f789ed 100644 --- a/typo3/sysext/core/Classes/Resource/Service/FrontendContentAdapterService.php +++ b/typo3/sysext/core/Classes/Resource/Service/FrontendContentAdapterService.php @@ -31,6 +31,19 @@ namespace TYPO3\CMS\Core\Resource\Service; */ class FrontendContentAdapterService { + /** + * Array containing all keys that are allowed in the migrateFields array. + * + * @var array + */ + static protected $availableMigrationFields = array( + 'paths', + 'titleTexts', + 'captions', + 'links', + 'alternativeTexts' + ); + /** * The name of the table * @@ -132,6 +145,53 @@ class FrontendContentAdapterService { $row['_MIGRATED'] = TRUE; } + /** + * Registers an additional record type for an existing migration configuration. + * + * For use in ext_localconf.php files. + * + * @param string $table Name of the table in the migration configuration + * @param string $field Name of the field in the migration configuration + * @param string $additionalType The additional type for which the migration should be applied + * @throws \RuntimeException + * @return void + */ + static public function registerAdditionalTypeForMigration($table, $field, $additionalType) { + + if (!isset(static::$migrateFields[$table][$field]['__typeMatch'])) { + throw new \RuntimeException('Additional types can only be added when there is already an existing type match configuration for the given table and field.', 1377600978); + } + + self::$migrateFields[$table][$field]['__typeMatch']['types'][] = $additionalType; + } + + /** + * Registers an additional field for migration. + * + * For use in ext_localconf.php files + * + * @param string $table Name of the table in the migration configuration + * @param string $field Name of the field in the migration configuration + * @param string $migrationField The file property that should be migrated, see $availableMigrateFields for available settings + * @param string $oldFieldName The name of the field in which the file property should be available + * @param string $typeField Optional field that switches the record type, will only have an effect if $types array is provided + * @param array $types The record types for which the migration should be active + * @throws \InvalidArgumentException + */ + static public function registerFieldForMigration($table, $field, $migrationField, $oldFieldName, $typeField = NULL, array $types = array()) { + + if (array_search($migrationField, static::$availableMigrationFields) === FALSE) { + throw new \InvalidArgumentException('The value for $migrationField is invalid. Valid values can be found in the $availableMigrationFields array.', 1377600978); + } + + self::$migrateFields[$table][$field][$migrationField] = $oldFieldName; + + if (isset($typeField) && (count($types) > 0)) { + self::$migrateFields[$table][$field]['__typeMatch']['types'] = $types; + self::$migrateFields[$table][$field]['__typeMatch']['typeField'] = (string)$typeField; + } + } + /** * Check if fieldis in type * diff --git a/typo3/sysext/core/Tests/Unit/Resource/Service/FrontendContentAdapterServiceTest.php b/typo3/sysext/core/Tests/Unit/Resource/Service/FrontendContentAdapterServiceTest.php index 8772b79d0a3480f972b698dc93adf497395e9213..5aa31eaed9aabbdba95a21389732aa84699a02ab 100644 --- a/typo3/sysext/core/Tests/Unit/Resource/Service/FrontendContentAdapterServiceTest.php +++ b/typo3/sysext/core/Tests/Unit/Resource/Service/FrontendContentAdapterServiceTest.php @@ -158,4 +158,142 @@ class FrontendContentAdapterServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCa $this->assertSame($expectedCaption, $dbRow['imagecaption']); } + /** + * @test + */ + public function registerAdditionalTypeForMigrationAddsTypeToArray() { + + $migrateFields = array( + 'testtable' => array( + 'testfield' => array( + 'paths' => 'oldfield', + '__typeMatch' => array( + 'typeField' => 'mytypefield', + 'types' => array('mytype'), + ) + ) + ), + ); + + $expectedResult = array( + 'testtable' => array( + 'testfield' => array( + 'paths' => 'oldfield', + '__typeMatch' => array( + 'typeField' => 'mytypefield', + 'types' => array('mytype', 'mytype2'), + ) + ) + ), + ); + + $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration(); + $frontendContentAdapterService->_setStatic('migrateFields', $migrateFields); + $frontendContentAdapterService->registerAdditionalTypeForMigration('testtable', 'testfield', 'mytype2'); + $this->assertEquals($expectedResult, $frontendContentAdapterService->_getStatic('migrateFields')); + } + + /** + * @test + * @expectedException \RuntimeException + */ + public function registerAdditionalTypeForMigrationThrowsExceptionIfNoConfigurationAvailable() { + $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration(); + $frontendContentAdapterService->registerAdditionalTypeForMigration('testtable', 'testfield', 'mytype2'); + } + + /** + * @test + * @expectedException \RuntimeException + */ + public function registerAdditionalTypeForMigrationThrowsExceptionIfNoTypeConfigurationAvailable() { + + $migrateFields = array( + 'testtable' => array( + 'testfield' => array( + 'paths' => 'oldfield', + ) + ), + ); + + $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration(); + $frontendContentAdapterService->_setStatic('migrateFields', $migrateFields); + $frontendContentAdapterService->registerAdditionalTypeForMigration('testtable', 'testfield', 'mytype2'); + } + + /** + * @test + * @dataProvider registerFieldForMigrationAddsCorrectConfigurationDataProvider + */ + public function registerFieldForMigrationAddsCorrectConfiguration($expectedResult, $table, $field, $migrationFields, $oldFieldName, $typeField, $types) { + $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration(); + $frontendContentAdapterService::registerFieldForMigration($table, $field, $migrationFields, $oldFieldName, $typeField, $types); + $newConfiguration = $frontendContentAdapterService->_getStatic('migrateFields'); + $this->assertEquals($expectedResult, $newConfiguration); + } + + /** + * Data provider for registerFieldForMigrationAddsCorrectConfiguration + * + * @return array + */ + public function registerFieldForMigrationAddsCorrectConfigurationDataProvider() { + return array( + 'table without type column' => array( + array( + 'tablename' => array( + 'newfield' => array( + 'paths' => 'oldfield', + ), + ), + ), + 'tablename', + 'newfield', + 'paths', + 'oldfield', + NULL, + array() + ), + 'table with type column' => array( + array( + 'tablename' => array( + 'newfield' => array( + 'paths' => 'oldfield', + '__typeMatch' => array( + 'typeField' => 'typecolumn', + 'types' => array('firsttype'), + ) + ), + ), + ), + 'tablename', + 'newfield', + 'paths', + 'oldfield', + 'typecolumn', + array('firsttype'), + ), + ); + } + + /** + * @test + * @expectedException \InvalidArgumentException + */ + public function registerFieldForMigrationThrowsExceptionForInvalidMigrationField() { + $frontendContentAdapterService = $this->getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration(); + $frontendContentAdapterService::registerFieldForMigration('table', 'field', 'invalidfield', 'oldfield'); + } + + /** + * Creates an accessible mock of the FrontendContentAdapterService class + * and sets the migrateFields property to an empty array. + * + * @return \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface + */ + protected function getAccessibleFrontendContentAdapterServiceWithEmptyConfiguration() { + $frontendContentAdapterService = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\Service\\FrontendContentAdapterService', array('dummy')); + $frontendContentAdapterService->_setStatic('migrateFields', array()); + return $frontendContentAdapterService; + } }