[TASK] Make Repository::findByIdentifier() call the storage-backend 25/35325/3
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Thu, 11 Dec 2014 22:10:39 +0000 (23:10 +0100)
committerFrank Nägler <typo3@naegler.net>
Sun, 15 Mar 2015 15:19:58 +0000 (16:19 +0100)
Instead of creating a query itself, the Repository should
use existing functionality of the storage-backend.

Change-Id: I8f19b2e7f3c25bee2998d33e59727455d679b67d
Resolves: #63792
Releases: master
Reviewed-on: http://review.typo3.org/35325
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
Reviewed-by: Frank Nägler <typo3@naegler.net>
Tested-by: Frank Nägler <typo3@naegler.net>
typo3/sysext/extbase/Classes/Persistence/Repository.php
typo3/sysext/extbase/Tests/Unit/Persistence/RepositoryTest.php

index a3232b3..d723184 100644 (file)
@@ -182,30 +182,7 @@ class Repository implements RepositoryInterface, \TYPO3\CMS\Core\SingletonInterf
         * @api
         */
        public function findByIdentifier($identifier) {
-               /**
-                * @todo: This method must be changed again in 6.2 + 1
-                * This is marked @deprecated to be found in cleanup sessions.
-                *
-                * The repository should directly talk to the backend which
-                * does not respect query settings of the repository as
-                * findByIdentifier is strictly defined by finding an
-                * undeleted object by its identifier regardless if it
-                * is hidden/visible or a versioning/translation overlay.
-                *
-                * As a consequence users will be forced to overwrite this method
-                * and mimic this behaviour to be able to find objects by identifier
-                * respecting their query settings from 6.1 + 1 on.
-                */
-               if ($this->session->hasIdentifier($identifier, $this->objectType)) {
-                       $object = $this->session->getObjectByIdentifier($identifier, $this->objectType);
-               } else {
-                       $query = $this->createQuery();
-                       $query->getQuerySettings()->setRespectStoragePage(FALSE);
-                       $query->getQuerySettings()->setRespectSysLanguage(FALSE);
-                       $object = $query->matching($query->equals('uid', $identifier))->execute()->getFirst();
-               }
-
-               return $object;
+               return $this->persistenceManager->getObjectByIdentifier($identifier, $this->objectType);
        }
 
        /**
index 078bc89..0a41ae4 100644 (file)
@@ -61,13 +61,13 @@ class RepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
        /**
         * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
-        */
-       protected $querySettings;
+       */
+       protected $mockQuerySettings;
 
        /**
-        * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
+        * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
         */
-       protected $mockQuerySettings;
+       protected $mockConfigurationManager;
 
        protected function setUp() {
                $this->mockIdentityMap = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\IdentityMap::class);
@@ -76,9 +76,14 @@ class RepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->mockQuerySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
                $this->mockQuery->expects($this->any())->method('getQuerySettings')->will($this->returnValue($this->mockQuerySettings));
                $this->mockQueryFactory->expects($this->any())->method('create')->will($this->returnValue($this->mockQuery));
-               $this->mockBackend = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
                $this->mockSession = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class);
-               $this->mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
+               $this->mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
+               $this->mockBackend = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Backend::class, array('dummy'), array($this->mockConfigurationManager));
+               $this->inject($this->mockBackend, 'session', $this->mockSession);
+               $this->mockPersistenceManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, array('createQueryForType'));
+               $this->inject($this->mockBackend, 'persistenceManager', $this->mockPersistenceManager);
+               $this->inject($this->mockPersistenceManager, 'persistenceSession', $this->mockSession);
+               $this->inject($this->mockPersistenceManager, 'backend', $this->mockBackend);
                $this->mockPersistenceManager->expects($this->any())->method('createQueryForType')->will($this->returnValue($this->mockQuery));
                $this->mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
                $this->repository = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Repository::class, array('dummy'), array($this->mockObjectManager));
@@ -142,25 +147,20 @@ class RepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        /**
         * @test
         */
-       public function findByidentifierReturnsResultOfGetObjectByIdentifierCall() {
+       public function findByidentifierReturnsResultOfGetObjectByIdentifierCallFromBackend() {
                $identifier = '42';
                $object = new \stdClass();
 
                $expectedResult = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
                $expectedResult->expects($this->once())->method('getFirst')->will($this->returnValue($object));
 
-               $mockQuery = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
-               $mockQuery->expects($this->any())->method('getQuerySettings')->will($this->returnValue($this->mockQuerySettings));
-               $mockQuery->expects($this->once())->method('matching')->will($this->returnValue($mockQuery));
-               $mockQuery->expects($this->once())->method('execute')->will($this->returnValue($expectedResult));
-
-               $session = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class);
-               $session->expects($this->once())->method('hasIdentifier')->will($this->returnValue(FALSE));
+               $this->mockQuery->expects($this->any())->method('getQuerySettings')->will($this->returnValue($this->mockQuerySettings));
+               $this->mockQuery->expects($this->once())->method('matching')->will($this->returnValue($this->mockQuery));
+               $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($expectedResult));
 
-               $repository = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Repository::class, array('createQuery'), array($this->mockObjectManager));
-               $repository->_set('session', $session);
-               $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
-               $this->assertSame($object, $repository->findByIdentifier($identifier));
+               // skip backend, as we want to test the backend
+               $this->mockSession->expects($this->any())->method('hasIdentifier')->will($this->returnValue(FALSE));
+               $this->assertSame($object, $this->repository->findByIdentifier($identifier));
        }
 
        /**