[BUGFIX] Reset legacy fields if no relations are found 48/17848/2
authorHelmut Hummel <h.hummel@bitmotion.de>
Wed, 30 Jan 2013 16:39:28 +0000 (17:39 +0100)
committerJigal van Hemert <jigal@xs4all.nl>
Wed, 30 Jan 2013 17:28:12 +0000 (18:28 +0100)
If you add multiple images to a tt_content element
but mark all of them hidden, an image is shown which has
the uid of the number of images added to the element.

This is the case because TYPO3 puts the relation count in the
field and the frontend adapter service does not reset
the field if no relations are found.

The solution is to set the legacy fields in any case.

This change adds a test which fails without a fix
and another test which shows that the original behaviour
is not affected.

Resolves: #44988
Releases: 6.0, 6.1

Change-Id: Ia4690fb0a68cd4d6832aef6712bc69adf00f8a5f
Reviewed-on: https://review.typo3.org/17848
Reviewed-by: Jigal van Hemert
Tested-by: Jigal van Hemert
typo3/sysext/core/Classes/Resource/Service/FrontendContentAdapterService.php
typo3/sysext/core/Tests/Unit/Resource/Service/FrontendContentAdapterServiceTest.php [new file with mode: 0644]

index 42f0253..9d32591 100644 (file)
@@ -109,9 +109,7 @@ class FrontendContentAdapterService {
                                                        // For all other fields, separate by newline
                                                        $fieldContents = implode(chr(10), $fileFieldContents[$oldFieldType]);
                                                }
-                                               if ($fieldContents) {
-                                                       $row[$oldFieldName] = $fieldContents;
-                                               }
+                                               $row[$oldFieldName] = $fieldContents;
                                        }
                                }
                        }
diff --git a/typo3/sysext/core/Tests/Unit/Resource/Service/FrontendContentAdapterServiceTest.php b/typo3/sysext/core/Tests/Unit/Resource/Service/FrontendContentAdapterServiceTest.php
new file mode 100644 (file)
index 0000000..9292a78
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Resource\Service;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2013 Helmut Hummel <helmut.hummel@typo3.org>
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+/**
+ * Tests for the Frontend Content Adapter
+ */
+class FrontendContentAdapterServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+       /**
+        * @var \PHPUnit_Framework_MockObject_MockObject
+        */
+       protected $fileRepositoryMock;
+
+       /**
+        * @var array A backup of registered singleton instances
+        */
+       protected $singletonInstances = array();
+
+       /**
+        * Saving the singletons
+        */
+       public function setUp() {
+               $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
+               $this->fileRepositoryMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileRepository');
+               \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository', $this->fileRepositoryMock);
+       }
+
+       /**
+        * Restoring the singletons
+        */
+       public function tearDown() {
+               \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
+               unset($this->fileRepositoryMock);
+       }
+
+       /**
+        * @test
+        */
+       public function emptyRelationResetsLegacyFields() {
+               $this->fileRepositoryMock->expects($this->any())
+                       ->method('findByRelation')
+                       ->will($this->returnValue(array()));
+               $dbRow = array(
+                       'image' => '1'
+               );
+
+               \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow($dbRow, 'tt_content');
+               $this->assertEmpty($dbRow['image']);
+       }
+
+       /**
+        * @test
+        */
+       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())
+                       ->method('getPublicUrl')
+                       ->will($this->returnValue('path/to/file'));
+               $this->fileRepositoryMock->expects($this->any())
+                       ->method('findByRelation')
+                       ->will($this->returnValue(array($fileReference)));
+               $dbRow = array(
+                       'image' => '1'
+               );
+
+               \TYPO3\CMS\Core\Resource\Service\FrontendContentAdapterService::modifyDBRow($dbRow, 'tt_content');
+               $this->assertSame('../../path/to/file', $dbRow['image']);
+       }
+}
+
+
+?>
\ No newline at end of file