[BUGFIX] Do not overwrite imagecaption in content adapter 99/18699/2
authorHelmut Hummel <h.hummel@bitmotion.de>
Tue, 12 Feb 2013 16:57:19 +0000 (17:57 +0100)
committerHelmut Hummel <helmut.hummel@typo3.org>
Tue, 5 Mar 2013 13:35:48 +0000 (14:35 +0100)
Problem:
Create a new content element of type file links,
add a file relation and a description to this file.
Switch this element to be an image element,
add an image and a description of the image

Expected result:
An image with the image description is shown

Actual result:
An image with the description of the file is shown.

Solution:
Check if the field actually is shown in the selected
content element type.

Fixes: #45415
Releases: 6.0, 6.1

Change-Id: I3aa0e47b336cfa881c637b7f5a9e68476ded88ae
Reviewed-on: https://review.typo3.org/18222
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
Reviewed-on: https://review.typo3.org/18699

typo3/sysext/core/Classes/Resource/Service/FrontendContentAdapterService.php
typo3/sysext/core/Tests/Unit/Resource/Service/FrontendContentAdapterServiceTest.php

index 9d32591..4716dea 100644 (file)
@@ -44,10 +44,18 @@ class FrontendContentAdapterService {
                                'captions' => 'imagecaption',
                                'links' => 'image_link',
                                'alternativeTexts' => 'altText',
+                               'typeMatch' => array(
+                                       'typeField' => 'CType',
+                                       'types' => array('image', 'textpic'),
+                               )
                        ),
                        'media' => array(
                                'paths' => 'media',
-                               'captions' => 'imagecaption'
+                               'captions' => 'imagecaption',
+                               'typeMatch' => array(
+                                       'typeField' => 'CType',
+                                       'types' => array('uploads'),
+                               )
                        )
                ),
                'pages' => array(
@@ -75,7 +83,7 @@ class FrontendContentAdapterService {
                }
                if (array_key_exists($table, static::$migrateFields)) {
                        foreach (static::$migrateFields[$table] as $migrateFieldName => $oldFieldNames) {
-                               if ($row !== NULL && isset($row[$migrateFieldName])) {
+                               if ($row !== NULL && isset($row[$migrateFieldName]) && self::fieldIsInType($migrateFieldName, $table, $row)) {
                                        /** @var $fileRepository \TYPO3\CMS\Core\Resource\FileRepository */
                                        $fileRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository');
                                        if ($table === 'pages' && isset($row['_LOCALIZED_UID']) && intval($row['sys_language_uid']) > 0) {
@@ -94,11 +102,12 @@ class FrontendContentAdapterService {
 
                                        foreach ($files as $file) {
                                                /** @var $file \TYPO3\CMS\Core\Resource\FileReference */
+                                               $fileProperties = $file->getProperties();
                                                $fileFieldContents['paths'][] = '../../' . $file->getPublicUrl();
-                                               $fileFieldContents['titleTexts'][] = $file->getProperty('title');
-                                               $fileFieldContents['captions'][] = $file->getProperty('description');
-                                               $fileFieldContents['links'][] = $file->getProperty('link');
-                                               $fileFieldContents['alternativeTexts'][] = $file->getProperty('alternative');
+                                               $fileFieldContents['titleTexts'][] = $fileProperties['title'];
+                                               $fileFieldContents['captions'][] = $fileProperties['description'];
+                                               $fileFieldContents['links'][] = $fileProperties['link'];
+                                               $fileFieldContents['alternativeTexts'][] = $fileProperties['alternative'];
                                                $fileFieldContents[$migrateFieldName .  '_fileUids'][] = $file->getOriginalFile()->getUid();
                                        }
                                        foreach ($oldFieldNames as $oldFieldType => $oldFieldName) {
@@ -117,6 +126,22 @@ class FrontendContentAdapterService {
                $row['_MIGRATED'] = TRUE;
        }
 
+       /**
+        * Check if fieldis in type
+        *
+        * @param string $fieldName
+        * @param string $table
+        * @param array $row
+        * @return boolean
+        */
+       static protected function fieldIsInType($fieldName, $table, array $row) {
+               $fieldConfiguration = static::$migrateFields[$table][$fieldName];
+               if (empty($fieldConfiguration['typeMatch'])) {
+                       return TRUE;
+               } else {
+                       return in_array($row[$fieldConfiguration['typeMatch']['typeField']], $fieldConfiguration['typeMatch']['types']);
+               }
+       }
 }
 
 
index 9292a78..6912fd8 100644 (file)
@@ -62,6 +62,7 @@ class FrontendContentAdapterServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCa
                        ->method('findByRelation')
                        ->will($this->returnValue(array()));
                $dbRow = array(
+                       'CType' => 'image',
                        'image' => '1'
                );
 
@@ -75,9 +76,6 @@ class FrontendContentAdapterServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCa
        public function imageFieldIsFilledWithPathOfImage() {
                $fileReference = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileReference', array(), array(), '', FALSE);
                $fileReference->expects($this->any())
-                       ->method('getProperty')
-                       ->will($this->returnArgument(0));
-               $fileReference->expects($this->any())
                        ->method('getOriginalFile')
                        ->will($this->returnValue($this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE)));
                $fileReference->expects($this->any())
@@ -87,12 +85,79 @@ class FrontendContentAdapterServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCa
                        ->method('findByRelation')
                        ->will($this->returnValue(array($fileReference)));
                $dbRow = array(
+                       'CType' => 'image',
                        'image' => '1'
                );
 
                \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow($dbRow, 'tt_content');
                $this->assertSame('../../path/to/file', $dbRow['image']);
        }
+
+       public function conteRowsOfDifferentTypesDataProvider() {
+               $filePropertiesImage = array(
+                       'title' => 'Image',
+                       'description' => 'IMAGE DESCRIPTION',
+               );
+
+               $filePropertiesMedia = array(
+                       'title' => 'Media',
+                       'description' => 'MEDIA DESCRIPTION',
+               );
+
+               return array(
+                       'Image Element' => array(
+                               array(
+                                       'CType' => 'image',
+                                       'image' => '',
+                                       'media' => '',
+                               ),
+                               'IMAGE DESCRIPTION',
+                               $filePropertiesImage
+                       ),
+                       'Textpic Element' => array(
+                               array(
+                                       'CType' => 'textpic',
+                                       'image' => '',
+                                       'media' => '',
+                               ),
+                               'IMAGE DESCRIPTION',
+                               $filePropertiesImage
+                       ),
+                       'Uploads Element' => array(
+                               array(
+                                       'CType' => 'uploads',
+                                       'image' => '',
+                                       'media' => '',
+                               ),
+                               'MEDIA DESCRIPTION',
+                               $filePropertiesMedia
+                       ),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider conteRowsOfDifferentTypesDataProvider
+        */
+       public function migrationOfLegacyFieldsIsOnlyDoneWhenRelationFieldIsVisibleInType($dbRow, $expectedCaption, $fileProperties) {
+               $fileReference = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileReference', array(), array(), '', FALSE);
+               $fileReference->expects($this->once())
+                       ->method('getProperties')
+                       ->will($this->returnValue($fileProperties));
+               $fileReference->expects($this->any())
+                       ->method('getOriginalFile')
+                       ->will($this->returnValue($this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE)));
+               $fileReference->expects($this->any())
+                       ->method('getPublicUrl')
+                       ->will($this->returnValue('path/to/file'));
+               $this->fileRepositoryMock->expects($this->any())
+                       ->method('findByRelation')
+                       ->will($this->returnValue(array($fileReference)));
+
+               \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow($dbRow, 'tt_content');
+               $this->assertSame($expectedCaption, $dbRow['imagecaption']);
+       }
+
 }