[TASK] Doctrine: Migrate ExtensionService 13/48513/6
authorXavier Ley <xavierley@gmail.com>
Wed, 8 Jun 2016 16:05:37 +0000 (18:05 +0200)
committerJan Helke <typo3@helke.de>
Thu, 9 Jun 2016 08:37:32 +0000 (10:37 +0200)
Resolves: #76528
Releases: master
Change-Id: I34e9fb0b3bb4bf3e55b3dc43d6358ce3113d5df5
Reviewed-on: https://review.typo3.org/48513
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Reviewed-by: Susanne Moog <typo3@susannemoog.de>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Tested-by: Susanne Moog <typo3@susannemoog.de>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
typo3/sysext/extbase/Classes/Service/ExtensionService.php
typo3/sysext/extbase/Tests/Unit/Service/ExtensionServiceTest.php

index 0a20671..4211438 100644 (file)
@@ -14,6 +14,10 @@ namespace TYPO3\CMS\Extbase\Service;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
 /**
  * Service for determining basic extension params
  */
@@ -158,7 +162,22 @@ class ExtensionService implements \TYPO3\CMS\Core\SingletonInterface
         $pluginSignature = strtolower($extensionName . '_' . $pluginName);
         if ($frameworkConfiguration['view']['defaultPid'] === 'auto') {
             if (!array_key_exists($pluginSignature, $this->targetPidPluginCache)) {
-                $pages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('pid', 'tt_content', 'list_type=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($pluginSignature, 'tt_content') . ' AND CType="list"' . $GLOBALS['TSFE']->sys_page->enableFields('tt_content') . ' AND sys_language_uid=' . $GLOBALS['TSFE']->sys_language_uid, '', '', 2);
+                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+                    ->getQueryBuilderForTable('tt_content');
+                $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
+
+                $pages = $queryBuilder
+                    ->select('pid')
+                    ->from('tt_content')
+                    ->where(
+                        $queryBuilder->expr()->eq('list_type', $queryBuilder->createNamedParameter($pluginSignature)),
+                        $queryBuilder->expr()->eq('CType', $queryBuilder->createNamedParameter('list')),
+                        $queryBuilder->expr()->eq('sys_language_uid', (int)$GLOBALS['TSFE']->sys_language_uid)
+                    )
+                    ->setMaxResults(2)
+                    ->execute()
+                    ->fetchAll();
+
                 if (count($pages) > 1) {
                     throw new \TYPO3\CMS\Extbase\Exception('There is more than one "' . $pluginSignature . '" plugin in the current page tree. Please remove one plugin or set the TypoScript configuration "plugin.tx_' . $pluginSignature . '.view.defaultPid" to a fixed page id', 1280773643);
                 }
index 73fc214..60e42b0 100644 (file)
@@ -15,6 +15,16 @@ namespace TYPO3\CMS\Extbase\Tests\Unit\Service;
  */
 use TYPO3\CMS\Extbase\Exception;
 
+use Doctrine\DBAL\Statement;
+use Prophecy\Argument;
+use Prophecy\Prophecy\ObjectProphecy;
+use TYPO3\CMS\Core\Database\Connection;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
+use TYPO3\CMS\Core\Database\Query\QueryBuilder;
+use TYPO3\CMS\Core\Tests\Unit\Database\Mocks\MockPlatform;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
 /**
  * Test case
  */
@@ -34,6 +44,7 @@ class ExtensionServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     {
         $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array('fullQuoteStr', 'exec_SELECTgetRows'));
         $GLOBALS['TSFE'] = new \stdClass();
+        $GLOBALS['TSFE']->gr_list = '';
         $this->extensionService = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Service\ExtensionService::class, array('dummy'));
         $this->mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
         $this->extensionService->_set('configurationManager', $this->mockConfigurationManager);
@@ -75,6 +86,32 @@ class ExtensionServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     }
 
     /**
+     * Setup and return a mocked database connection that allows
+     * the QueryBuilder to work.
+     *
+     * @return ObjectProphecy
+     */
+    protected function getMockDatabaseConnection(): ObjectProphecy
+    {
+        $connection = $this->prophesize(Connection::class);
+        $connection->getDatabasePlatform()->willReturn(new MockPlatform());
+        $connection->getExpressionBuilder()->willReturn(new ExpressionBuilder($connection->reveal()));
+        $connection->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
+
+        $queryBuilder = new QueryBuilder(
+            $connection->reveal(),
+            null,
+            new \Doctrine\DBAL\Query\QueryBuilder($connection->reveal())
+        );
+
+        $connectionPool = $this->prophesize(ConnectionPool::class);
+        $connectionPool->getQueryBuilderForTable('tt_content')->willReturn($queryBuilder);
+        GeneralUtility::addInstance(ConnectionPool::class, $connectionPool->reveal());
+
+        return $connection;
+    }
+
+    /**
      * DataProvider for getPluginNamespaceByPluginSignatureTests()
      *
      * @return array
@@ -226,45 +263,78 @@ class ExtensionServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
 
     /**
      * @test
+     * @todo This should rather be a functional test since it needs a connection / querybuilder
      */
     public function getTargetPidByPluginSignatureDeterminesTheTargetPidIfDefaultPidIsAuto()
     {
-        $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
-        $pluginSignature = 'extensionname_someplugin';
-        $GLOBALS['TSFE']->sys_page = $this->getMock(\TYPO3\CMS\Frontend\Page\PageRepository::class, array('enableFields'));
-        $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->with('tt_content')->will($this->returnValue(' AND enable_fields'));
-        $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->with($pluginSignature, 'tt_content')->will($this->returnValue('"pluginSignature"'));
-        $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('pid', 'tt_content', 'list_type="pluginSignature" AND CType="list" AND enable_fields AND sys_language_uid=', '', '')->will($this->returnValue(array(array('pid' => '321'))));
+        $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will(
+            $this->returnValue(['view' => ['defaultPid' => 'auto']])
+        );
         $expectedResult = 321;
+
+        $statement = $this->prophesize(Statement::class);
+        $statement->fetchAll()->shouldBeCalled()->willReturn([['pid' => (string)$expectedResult]]);
+
+        $connection = $this->getMockDatabaseConnection();
+        $connection->executeQuery(
+            'SELECT pid FROM tt_content WHERE (list_type = :dcValue1) AND (CType = :dcValue2) AND (sys_language_uid = 0) LIMIT 2',
+            ['dcValue1' => 'extensionname_someplugin', 'dcValue2' => 'list'],
+            Argument::cetera()
+        )->shouldBeCalled()->willReturn($statement->reveal());
+
         $actualResult = $this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
         $this->assertEquals($expectedResult, $actualResult);
     }
 
     /**
      * @test
+     * @todo This should rather be a functional test since it needs a connection / querybuilder
      */
     public function getTargetPidByPluginSignatureReturnsNullIfTargetPidCouldNotBeDetermined()
     {
-        $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
-        $GLOBALS['TSFE']->sys_page = $this->getMock(\TYPO3\CMS\Frontend\Page\PageRepository::class, array('enableFields'));
-        $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->will($this->returnValue(' AND enable_fields'));
-        $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->will($this->returnValue('"pluginSignature"'));
-        $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnValue(array()));
+        $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will(
+            $this->returnValue(['view' => ['defaultPid' => 'auto']])
+        );
+
+        $statement = $this->prophesize(Statement::class);
+        $statement->fetchAll()->shouldBeCalled()->willReturn([]);
+
+        $connection = $this->getMockDatabaseConnection();
+        $connection->executeQuery(
+            'SELECT pid FROM tt_content WHERE (list_type = :dcValue1) AND (CType = :dcValue2) AND (sys_language_uid = 0) LIMIT 2',
+            ['dcValue1' => 'extensionname_someplugin', 'dcValue2' => 'list'],
+            Argument::cetera()
+        )->shouldBeCalled()->willReturn($statement->reveal());
+
         $this->assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin'));
     }
 
     /**
      * @test
+     * @todo This should rather be a functional test since it needs a connection / querybuilder
      */
     public function getTargetPidByPluginSignatureThrowsExceptionIfMoreThanOneTargetPidsWereFound()
     {
         $this->expectException(Exception::class);
         $this->expectExceptionCode(1280773643);
-        $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
-        $GLOBALS['TSFE']->sys_page = $this->getMock(\TYPO3\CMS\Frontend\Page\PageRepository::class, array('enableFields'));
-        $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->will($this->returnValue(' AND enable_fields'));
-        $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->will($this->returnValue('"pluginSignature"'));
-        $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnValue(array(array('pid' => 123), array('pid' => 124))));
+
+        $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will(
+            $this->returnValue(['view' => ['defaultPid' => 'auto']])
+        );
+
+        $statement = $this->prophesize(Statement::class);
+        $statement->fetchAll()->shouldBeCalled()->willReturn([['pid' => 123], ['pid' => 124]]);
+
+        $connection = $this->getMockDatabaseConnection();
+        $connection->executeQuery(
+            'SELECT pid FROM tt_content WHERE (list_type = :dcValue1) AND (CType = :dcValue2) AND (sys_language_uid = 0) LIMIT 2',
+            ['dcValue1' => 'extensionname_someplugin', 'dcValue2' => 'list'],
+            Argument::cetera()
+        )->shouldBeCalled()->willReturn($statement->reveal());
+
+        $this->expectException(\TYPO3\CMS\Extbase\Exception::class);
+        $this->expectExceptionCode(1280773643);
+
         $this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
     }