[BUGFIX] getPlainValue for @lazy objects
authorBenjamin Bretz <pleaserebootuniverse@googlemail.com>
Sat, 13 Apr 2013 12:30:22 +0000 (14:30 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sat, 13 Apr 2013 15:58:10 +0000 (17:58 +0200)
Typo3DbBackend::getPlainValue now loads lazy objects if they are
instances of LazyLoadingProxy

Fixes: #28149
Releases: 6.1, 6.0
Change-Id: Ibb7c342d2e0910190dcf414cef60a0e7d5f9d051
Reviewed-on: https://review.typo3.org/19926
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbBackend.php
typo3/sysext/extbase/Tests/Unit/Persistence/Generic/Storage/Typo3DbBackendTest.php

index aa854e4..51b9ccd 100644 (file)
@@ -632,10 +632,15 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
                if ($input instanceof \DateTime) {
                        return $input->format('U');
                } elseif (is_object($input)) {
-                       if ($input instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface) {
-                               return $input->getUid();
+                       if ($input instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
+                               $realInput = $input->_loadRealInstance();
                        } else {
-                               throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException('An object of class "' . get_class($input) . '" could not be converted to a plain value.', 1274799934);
+                               $realInput = $input;
+                       }
+                       if ($realInput instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface) {
+                               return $realInput->getUid();
+                       } else {
+                               throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException('An object of class "' . get_class($realInput) . '" could not be converted to a plain value.', 1274799934);
                        }
                } elseif (is_bool($input)) {
                        return $input === TRUE ? 1 : 0;
index 14b85c4..bad00e3 100644 (file)
@@ -483,6 +483,130 @@ class Typo3DbBackendTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $mockTypo3DbBackend->_set('pageRepository', $pageRepositoryMock);
                $this->assertSame(array($comparisonRow), $mockTypo3DbBackend->_call('doLanguageAndWorkspaceOverlay', $sourceMock, array($row), $languageUid, $workspaceUid));
        }
+
+       /**
+        * @test
+        * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException
+        */
+       public function getPlainValueThrowsExceptionIfInputIsArray() {
+               $mockTypo3DbBackend = $this->getAccessibleMock(
+                       'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
+                       array('dummy'),
+                       array(),
+                       '',
+                       FALSE
+               );
+               $mockTypo3DbBackend->_call('getPlainValue', array());
+       }
+
+       /**
+        * @test
+        */
+       public function getPlainValueReturnsTimestampIfDateTimeObjectIsGiven() {
+               $mockTypo3DbBackend = $this->getAccessibleMock(
+                       'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
+                       array('dummy'),
+                       array(),
+                       '',
+                       FALSE
+               );
+               $input = new \DateTime('@1365866253');
+               $this->assertSame('1365866253', $mockTypo3DbBackend->_call('getPlainValue', $input));
+       }
+
+       /**
+        * @test
+        */
+       public function getPlainValueReturnsIntegerOneIfValueIsBooleanTrue() {
+               $mockTypo3DbBackend = $this->getAccessibleMock(
+                       'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
+                       array('dummy'),
+                       array(),
+                       '',
+                       FALSE
+               );
+               $this->assertSame(1, $mockTypo3DbBackend->_call('getPlainValue', TRUE));
+       }
+
+       /**
+        * @test
+        */
+       public function getPlainValueReturnsIntegerZeroIfValueIsBooleanFalse() {
+               $mockTypo3DbBackend = $this->getAccessibleMock(
+                       'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
+                       array('dummy'),
+                       array(),
+                       '',
+                       FALSE
+               );
+               $this->assertSame(0, $mockTypo3DbBackend->_call('getPlainValue', FALSE));
+       }
+
+       /**
+        * @test
+        * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException
+        */
+       public function getPlainValueCallsGetRealInstanceOnInputIfInputIsInstanceOfLazyLoadingProxy() {
+               $mockTypo3DbBackend = $this->getAccessibleMock(
+                       'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
+                       array('dummy'),
+                       array(),
+                       '',
+                       FALSE
+               );
+               $input = $this->getMock(
+                       'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\LazyLoadingProxy',
+                       array(),
+                       array(),
+                       '',
+                       FALSE
+               );
+               $input
+                       ->expects($this->once())
+                       ->method('_loadRealInstance');
+               $mockTypo3DbBackend->_call('getPlainValue', $input);
+       }
+
+       /**
+        * @test
+        */
+       public function getPlainValueCallsGetUidOnDomainObjectInterfaceInput() {
+               $mockTypo3DbBackend = $this->getAccessibleMock(
+                       'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
+                       array('dummy'),
+                       array(),
+                       '',
+                       FALSE
+               );
+               $input = $this->getMock(
+                       'TYPO3\\CMS\\Extbase\\DomainObject\\DomainObjectInterface',
+                       array(),
+                       array(),
+                       '',
+                       FALSE
+               );
+               $input
+                       ->expects($this->once())
+                       ->method('getUid')
+                       ->will($this->returnValue(23));
+               $this->assertSame(23, $mockTypo3DbBackend->_call('getPlainValue', $input));
+       }
+
+       /**
+        * @test
+        */
+       public function getPlainValueReturnsSimpleType() {
+               $mockTypo3DbBackend = $this->getAccessibleMock(
+                       'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
+                       array('dummy'),
+                       array(),
+                       '',
+                       FALSE
+               );
+               $value = uniqid('foo_');
+               $this->assertSame($value, $mockTypo3DbBackend->_call('getPlainValue', $value));
+       }
+
 }
 
 ?>
\ No newline at end of file