From 1c3952d0ec8d4927444402f25f021023242e020e Mon Sep 17 00:00:00 2001
From: Sebastian Bumann <bumann.sebastian@gmail.com>
Date: Tue, 2 Aug 2016 17:36:29 +0200
Subject: [PATCH] [TASK] Doctrine: Migrate BackendConfigurationManager

Resolves: #77354
Releases: master
Change-Id: If725d008965a22fbf3c73d74cf0fb950887b30b5
Reviewed-on: https://review.typo3.org/49322
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
---
 .../BackendConfigurationManager.php           | 48 ++++++++--
 .../BackendConfigurationManagerTest.php       | 92 +++++++++++++++++++
 .../BackendConfigurationManagerTest.php       | 39 --------
 3 files changed, 134 insertions(+), 45 deletions(-)
 create mode 100644 typo3/sysext/extbase/Tests/Functional/Configuration/BackendConfigurationManagerTest.php

diff --git a/typo3/sysext/extbase/Classes/Configuration/BackendConfigurationManager.php b/typo3/sysext/extbase/Classes/Configuration/BackendConfigurationManager.php
index 6a0493c8c518..c9f3964c5f4f 100644
--- a/typo3/sysext/extbase/Classes/Configuration/BackendConfigurationManager.php
+++ b/typo3/sysext/extbase/Classes/Configuration/BackendConfigurationManager.php
@@ -13,6 +13,10 @@ namespace TYPO3\CMS\Extbase\Configuration;
  *
  * The TYPO3 project - inspiring people to share!
  */
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
+use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
  * A general purpose configuration manager used in backend mode.
@@ -159,9 +163,25 @@ class BackendConfigurationManager extends \TYPO3\CMS\Extbase\Configuration\Abstr
      */
     protected function getCurrentPageIdFromCurrentSiteRoot()
     {
-        $rootPage = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
-            'uid', 'pages', 'deleted=0 AND hidden=0 AND is_siteroot=1', '', 'sorting'
-        );
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('pages');
+
+        $queryBuilder
+            ->getRestrictions()
+            ->removeAll()
+            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
+            ->add(GeneralUtility::makeInstance(HiddenRestriction::class));
+
+        $rootPage = $queryBuilder
+            ->select('uid')
+            ->from('pages')
+            ->where(
+                $queryBuilder->expr()->eq('is_siteroot', 1)
+            )
+            ->orderBy('sorting')
+            ->execute()
+            ->fetch();
+
         if (empty($rootPage)) {
             return 0;
         }
@@ -176,9 +196,25 @@ class BackendConfigurationManager extends \TYPO3\CMS\Extbase\Configuration\Abstr
      */
     protected function getCurrentPageIdFromRootTemplate()
     {
-        $rootTemplate = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
-            'pid', 'sys_template', 'deleted=0 AND hidden=0 AND root=1', '', 'crdate'
-        );
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('sys_template');
+
+        $queryBuilder
+            ->getRestrictions()
+            ->removeAll()
+            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
+            ->add(GeneralUtility::makeInstance(HiddenRestriction::class));
+
+        $rootTemplate = $queryBuilder
+            ->select('pid')
+            ->from('sys_template')
+            ->where(
+                $queryBuilder->expr()->eq('root', 1)
+            )
+            ->orderBy('crdate')
+            ->execute()
+            ->fetch();
+
         if (empty($rootTemplate)) {
             return 0;
         }
diff --git a/typo3/sysext/extbase/Tests/Functional/Configuration/BackendConfigurationManagerTest.php b/typo3/sysext/extbase/Tests/Functional/Configuration/BackendConfigurationManagerTest.php
new file mode 100644
index 000000000000..937c017337c6
--- /dev/null
+++ b/typo3/sysext/extbase/Tests/Functional/Configuration/BackendConfigurationManagerTest.php
@@ -0,0 +1,92 @@
+<?php
+namespace TYPO3\CMS\Extbase\Tests\Functional\Configuration;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Tests\FunctionalTestCase;
+use TYPO3\CMS\Extbase\Configuration\AbstractConfigurationManager;
+use TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager;
+use TYPO3\CMS\Extbase\Service\TypoScriptService;
+
+/**
+ * Test case
+ */
+class BackendConfigurationManagerTest extends FunctionalTestCase
+{
+    /**
+     * Warning: white box test
+     *
+     * @test
+     */
+    public function getCurrentPageIdReturnsPidFromFirstRootTemplateIfIdIsNotSetAndNoRootPageWasFound()
+    {
+        $backendConfigurationManager = $this->getAccessibleMock(BackendConfigurationManager::class, ['getTypoScriptSetup']);
+        $mockTypoScriptService = $this->getMockBuilder(TypoScriptService::class)->getMock();
+        $backendConfigurationManager->_set('typoScriptService', $mockTypoScriptService);
+
+        (new ConnectionPool())->getConnectionForTable('sys_template')->insert(
+            'sys_template',
+            [
+                'pid' => 123,
+                'deleted' => 0,
+                'hidden' => 0,
+                'root' => 1
+            ]
+        );
+
+        $actualResult = $backendConfigurationManager->_call('getCurrentPageId');
+        $this->assertEquals(123, $actualResult);
+    }
+
+    /**
+     * Warning: white box test
+     *
+     * @test
+     */
+    public function getCurrentPageIdReturnsUidFromFirstRootPageIfIdIsNotSet()
+    {
+        $backendConfigurationManager = $this->getAccessibleMock(BackendConfigurationManager::class, ['getTypoScriptSetup']);
+        $mockTypoScriptService = $this->getMockBuilder(TypoScriptService::class)->getMock();
+        $backendConfigurationManager->_set('typoScriptService', $mockTypoScriptService);
+
+        (new ConnectionPool())->getConnectionForTable('pages')->insert(
+            'pages',
+            [
+                'deleted' => 0,
+                'hidden' => 0,
+                'is_siteroot' => 1
+            ]
+        );
+
+        $actualResult = $backendConfigurationManager->_call('getCurrentPageId');
+        $this->assertEquals(1, $actualResult);
+    }
+
+    /**
+     * Warning: white box test
+     *
+     * @test
+     */
+    public function getCurrentPageIdReturnsDefaultStoragePidIfIdIsNotSetNoRootTemplateAndRootPageWasFound()
+    {
+        $backendConfigurationManager = $this->getAccessibleMock(BackendConfigurationManager::class, ['getTypoScriptSetup']);
+        $mockTypoScriptService = $this->getMockBuilder(TypoScriptService::class)->getMock();
+        $backendConfigurationManager->_set('typoScriptService', $mockTypoScriptService);
+
+        $expectedResult = AbstractConfigurationManager::DEFAULT_BACKEND_STORAGE_PID;
+        $actualResult = $backendConfigurationManager->_call('getCurrentPageId');
+        $this->assertEquals($expectedResult, $actualResult);
+    }
+}
diff --git a/typo3/sysext/extbase/Tests/Unit/Configuration/BackendConfigurationManagerTest.php b/typo3/sysext/extbase/Tests/Unit/Configuration/BackendConfigurationManagerTest.php
index 141f8b69eb0b..c1d9a9424e5e 100644
--- a/typo3/sysext/extbase/Tests/Unit/Configuration/BackendConfigurationManagerTest.php
+++ b/typo3/sysext/extbase/Tests/Unit/Configuration/BackendConfigurationManagerTest.php
@@ -64,45 +64,6 @@ class BackendConfigurationManagerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
         $this->assertEquals($expectedResult, $actualResult);
     }
 
-    /**
-     * @test
-     */
-    public function getCurrentPageIdReturnsPidFromFirstRootTemplateIfIdIsNotSetAndNoRootPageWasFound()
-    {
-        $GLOBALS['TYPO3_DB']->expects($this->at(0))->method('exec_SELECTgetSingleRow')->with('uid', 'pages', 'deleted=0 AND hidden=0 AND is_siteroot=1', '', 'sorting')->will($this->returnValue(array()));
-        $GLOBALS['TYPO3_DB']->expects($this->at(1))->method('exec_SELECTgetSingleRow')->with('pid', 'sys_template', 'deleted=0 AND hidden=0 AND root=1', '', 'crdate')->will($this->returnValue(
-            array('pid' => 123)
-        ));
-        $expectedResult = 123;
-        $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
-        $this->assertEquals($expectedResult, $actualResult);
-    }
-
-    /**
-     * @test
-     */
-    public function getCurrentPageIdReturnsUidFromFirstRootPageIfIdIsNotSet()
-    {
-        $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetSingleRow')->with('uid', 'pages', 'deleted=0 AND hidden=0 AND is_siteroot=1', '', 'sorting')->will($this->returnValue(
-            array('uid' => 321)
-        ));
-        $expectedResult = 321;
-        $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
-        $this->assertEquals($expectedResult, $actualResult);
-    }
-
-    /**
-     * @test
-     */
-    public function getCurrentPageIdReturnsDefaultStoragePidIfIdIsNotSetNoRootTemplateAndRootPageWasFound()
-    {
-        $GLOBALS['TYPO3_DB']->expects($this->at(0))->method('exec_SELECTgetSingleRow')->with('uid', 'pages', 'deleted=0 AND hidden=0 AND is_siteroot=1', '', 'sorting')->will($this->returnValue(array()));
-        $GLOBALS['TYPO3_DB']->expects($this->at(1))->method('exec_SELECTgetSingleRow')->with('pid', 'sys_template', 'deleted=0 AND hidden=0 AND root=1', '', 'crdate')->will($this->returnValue(array()));
-        $expectedResult = \TYPO3\CMS\Extbase\Configuration\AbstractConfigurationManager::DEFAULT_BACKEND_STORAGE_PID;
-        $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
-        $this->assertEquals($expectedResult, $actualResult);
-    }
-
     /**
      * @test
      */
-- 
GitLab