[BUGFIX] Manipulation of file field migration config 60/23360/4
authorAlexander Stehlik <alexander.stehlik@googlemail.com>
Thu, 27 Feb 2014 08:57:05 +0000 (09:57 +0100)
committerStefan Neufeind <typo3.neufeind@speedpartner.de>
Fri, 21 Mar 2014 22:51:08 +0000 (23:51 +0100)
This patch introduces new methods to the FrontendContentAdapterServer
that allow the manipulation of the file field migration configuration.
It is now possible to add additional (content) types for which the
migration should be active and to add custom migration configurations.

Releases: 6.2
Resolves: #46067
Documentation: #56365
Change-Id: Ibc23be8a414192c8795d9bb4f09316cf608e240b
Reviewed-on: https://review.typo3.org/23360
Reviewed-by: Markus Klein
Tested-by: Markus Klein
Tested-by: Tom Peters
Reviewed-by: Stefan Neufeind
Tested-by: Stefan Neufeind
typo3/sysext/core/Classes/Resource/Service/FrontendContentAdapterService.php
typo3/sysext/core/Tests/Unit/Resource/Service/FrontendContentAdapterServiceTest.php

index 4e38085..38a0e00 100644 (file)
@@ -32,6 +32,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
         *
         * @var string
@@ -133,6 +146,53 @@ class FrontendContentAdapterService {
        }
 
        /**
+        * 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
         *
         * @param string $fieldName
index 8772b79..5aa31ea 100644 (file)
@@ -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;
+       }
 }