[TASK] Add resolved site object to FormEngine 90/57990/3
authorChristian Kuhn <lolli@schwarzbu.ch>
Wed, 22 Aug 2018 16:00:30 +0000 (18:00 +0200)
committerAndreas Fernandez <a.fernandez@scripting-base.de>
Wed, 22 Aug 2018 17:07:37 +0000 (19:07 +0200)
With pseudo sites we now always have a site object in backend.
The site object is needed multiple times in FormEngine, the
upcoming slug handling adds another usage.
The patch adds a FormEngine data provider to resolve the
site object for any given page or record and adds it as 'site'
to the result array.

Resolves: #85927
Releases: master
Change-Id: I7ebbd2e6591b54a54b6a5b56f95161b4ba6e5cee
Reviewed-on: https://review.typo3.org/57990
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
typo3/sysext/backend/Classes/Form/FormDataCompiler.php
typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseSystemLanguageRows.php
typo3/sysext/backend/Classes/Form/FormDataProvider/SiteResolving.php [new file with mode: 0644]
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseSystemLanguageRowsTest.php
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/SiteResolvingTest.php [new file with mode: 0644]
typo3/sysext/core/Configuration/DefaultConfiguration.php

index a881e8d..01dcab8 100644 (file)
@@ -169,6 +169,8 @@ class FormDataCompiler
             'effectivePid' => 0,
             // Rootline of page the record that is handled is located at as created by BackendUtility::BEgetRootline()
             'rootline' => [],
+            // The resolved SiteInterface object of the page or page given record is located on.
+            'site' => null,
             // For "edit", this is the permission bitmask of the page that is edited, or of the page a record is located at
             // For "new", this is the permission bitmask of the page the record is added to
             // @todo: Remove if not needed on a lower level
index 0116b7f..b279e78 100644 (file)
@@ -16,12 +16,10 @@ namespace TYPO3\CMS\Backend\Form\FormDataProvider;
 
 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
-use TYPO3\CMS\Core\Exception\SiteNotFoundException;
 use TYPO3\CMS\Core\Localization\LanguageService;
 use TYPO3\CMS\Core\Messaging\FlashMessage;
 use TYPO3\CMS\Core\Messaging\FlashMessageService;
-use TYPO3\CMS\Core\Site\PseudoSiteFinder;
-use TYPO3\CMS\Core\Site\SiteFinder;
+use TYPO3\CMS\Core\Site\Entity\SiteInterface;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
@@ -34,17 +32,18 @@ class DatabaseSystemLanguageRows implements FormDataProviderInterface
      *
      * @param array $result
      * @return array
-     * @throws \UnexpectedValueException
+     * @throws \LogicException
      */
     public function addData(array $result)
     {
-        $pageIdDefaultLanguage = $result['defaultLanguagePageRow']['uid'] ?? $result['effectivePid'];
-        try {
-            $site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pageIdDefaultLanguage);
-        } catch (SiteNotFoundException $e) {
-            // Check for a pseudo site
-            $site = GeneralUtility::makeInstance(PseudoSiteFinder::class)->getSiteByPageId($pageIdDefaultLanguage);
+        $site = $result['site'] ?? null;
+        if (!$site instanceof SiteInterface) {
+            throw new \LogicException(
+                'No valid site object found in $result[\'site\']',
+                1534952559
+            );
         }
+        $pageIdDefaultLanguage = $result['defaultLanguagePageRow']['uid'] ?? $result['effectivePid'];
         $languages = $site->getAvailableLanguages($this->getBackendUser(), true, $pageIdDefaultLanguage);
 
         $languageRows = [];
diff --git a/typo3/sysext/backend/Classes/Form/FormDataProvider/SiteResolving.php b/typo3/sysext/backend/Classes/Form/FormDataProvider/SiteResolving.php
new file mode 100644 (file)
index 0000000..8a912ef
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+declare(strict_types = 1);
+namespace TYPO3\CMS\Backend\Form\FormDataProvider;
+
+/*
+ * 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\Backend\Form\FormDataProviderInterface;
+use TYPO3\CMS\Core\Exception\SiteNotFoundException;
+use TYPO3\CMS\Core\Site\PseudoSiteFinder;
+use TYPO3\CMS\Core\Site\SiteFinder;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * This data provider is used in casual edit record / new record / edit page / new page
+ * scenarios: It find the site object for a page and adds it as 'site' in $result.
+ *
+ * Note this data provider has a loose dependency to DatabaseDefaultLanguagePageRow,
+ * it needs that to determine the correct base pid if localized pages are edited.
+ */
+class SiteResolving implements FormDataProviderInterface
+{
+    /**
+     * Find and add site object
+     *
+     * @param array $result
+     * @return array
+     */
+    public function addData(array $result): array
+    {
+        $pageIdDefaultLanguage = $result['defaultLanguagePageRow']['uid'] ?? $result['effectivePid'];
+        try {
+            $result['site'] = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pageIdDefaultLanguage);
+        } catch (SiteNotFoundException $e) {
+            // Check for a pseudo site
+            $result['site'] = GeneralUtility::makeInstance(PseudoSiteFinder::class)->getSiteByPageId($pageIdDefaultLanguage);
+        }
+        return $result;
+    }
+}
index 95ae6c7..899bae9 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+declare(strict_types = 1);
 namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
 
 /*
@@ -20,8 +21,6 @@ use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 use TYPO3\CMS\Core\Localization\LanguageService;
 use TYPO3\CMS\Core\Site\Entity\Site;
 use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
-use TYPO3\CMS\Core\Site\SiteFinder;
-use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 /**
@@ -32,6 +31,16 @@ class DatabaseSystemLanguageRowsTest extends UnitTestCase
     /**
      * @test
      */
+    public function addDataThrowsExceptionIfSiteObjectIsNotSet()
+    {
+        $this->expectException(\LogicException::class);
+        $this->expectExceptionCode(1534952559);
+        (new DatabaseSystemLanguageRows())->addData([]);
+    }
+
+    /**
+     * @test
+     */
     public function addDataSetsDefaultLanguageAndAllEntries()
     {
         $languageService = $this->prophesize(LanguageService::class);
@@ -40,10 +49,7 @@ class DatabaseSystemLanguageRowsTest extends UnitTestCase
         $backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
         $GLOBALS['BE_USER'] = $backendUserProphecy->reveal();
 
-        $siteFinderProphecy = $this->prophesize(SiteFinder::class);
-        GeneralUtility::addInstance(SiteFinder::class, $siteFinderProphecy->reveal());
         $siteProphecy = $this->prophesize(Site::class);
-        $siteFinderProphecy->getSiteByPageId(42)->willReturn($siteProphecy->reveal());
         $siteLanguageMinusOne = $this->prophesize(SiteLanguage::class);
         $siteLanguageMinusOne->getLanguageId()->willReturn(-1);
         $siteLanguageMinusOne->getTitle()->willReturn('All');
@@ -64,8 +70,8 @@ class DatabaseSystemLanguageRowsTest extends UnitTestCase
         ];
         $siteProphecy->getAvailableLanguages(Argument::cetera())->willReturn($siteLanguages);
         $input = [
-            'pageTsConfig' => [],
             'effectivePid' => 42,
+            'site' => $siteProphecy->reveal(),
         ];
         $expected = [
             'systemLanguageRows' => [
diff --git a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/SiteResolvingTest.php b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/SiteResolvingTest.php
new file mode 100644 (file)
index 0000000..7b4d51f
--- /dev/null
@@ -0,0 +1,69 @@
+<?php
+declare(strict_types = 1);
+namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
+
+/*
+ * 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\Backend\Form\FormDataProvider\SiteResolving;
+use TYPO3\CMS\Core\Site\Entity\Site;
+use TYPO3\CMS\Core\Site\SiteFinder;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
+
+/**
+ * Test case
+ */
+class SiteResolvingTest extends UnitTestCase
+{
+    /**
+     * @test
+     */
+    public function addDataAddsSiteObjectOfDefaultLanguageRow()
+    {
+        $siteFinderProphecy = $this->prophesize(SiteFinder::class);
+        GeneralUtility::addInstance(SiteFinder::class, $siteFinderProphecy->reveal());
+        $siteProphecy = $this->prophesize(Site::class);
+        $siteProphecyRevelation = $siteProphecy->reveal();
+        $siteFinderProphecy->getSiteByPageId(23)->willReturn($siteProphecyRevelation);
+        $input = [
+            'defaultLanguagePageRow' => [
+                'uid' => 23,
+            ],
+            'effectivePid' => 42,
+            'site' => $siteProphecyRevelation,
+        ];
+        $expected = $input;
+        $expected['site'] = $siteProphecy->reveal();
+        $this->assertSame($expected, (new SiteResolving())->addData($input));
+    }
+
+    /**
+     * @test
+     */
+    public function addDataAddsSiteObjectOfEffectivePid()
+    {
+        $siteFinderProphecy = $this->prophesize(SiteFinder::class);
+        GeneralUtility::addInstance(SiteFinder::class, $siteFinderProphecy->reveal());
+        $siteProphecy = $this->prophesize(Site::class);
+        $siteProphecyRevelation = $siteProphecy->reveal();
+        $siteFinderProphecy->getSiteByPageId(42)->willReturn($siteProphecyRevelation);
+        $input = [
+            'effectivePid' => 42,
+            'site' => $siteProphecyRevelation,
+        ];
+        $expected = $input;
+        $expected['site'] = $siteProphecy->reveal();
+        $this->assertSame($expected, (new SiteResolving())->addData($input));
+    }
+}
index 363ee23..de3197b 100644 (file)
@@ -410,11 +410,16 @@ return [
                             \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRecordOverrideValues::class,
                         ],
                     ],
-                    \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseSystemLanguageRows::class => [
+                    \TYPO3\CMS\Backend\Form\FormDataProvider\SiteResolving::class => [
                         'depends' => [
                             \TYPO3\CMS\Backend\Form\FormDataProvider\TcaGroup::class,
                             \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseDefaultLanguagePageRow::class,
-                            \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRecordOverrideValues::class,
+                        ],
+                    ],
+                    \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseSystemLanguageRows::class => [
+                        'depends' => [
+                            \TYPO3\CMS\Backend\Form\FormDataProvider\TcaGroup::class,
+                            \TYPO3\CMS\Backend\Form\FormDataProvider\SiteResolving::class,
                         ],
                     ],
                     \TYPO3\CMS\Backend\Form\FormDataProvider\DatabasePageLanguageOverlayRows::class => [
@@ -644,10 +649,15 @@ return [
                             \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseUniqueUidNewRow::class,
                         ],
                     ],
-                    \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseSystemLanguageRows::class => [
+                    \TYPO3\CMS\Backend\Form\FormDataProvider\SiteResolving::class => [
                         'depends' => [
-                            \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseDefaultLanguagePageRow::class,
                             \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowDefaultValues::class,
+                            \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseDefaultLanguagePageRow::class,
+                        ],
+                    ],
+                    \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseSystemLanguageRows::class => [
+                        'depends' => [
+                            \TYPO3\CMS\Backend\Form\FormDataProvider\SiteResolving::class,
                         ],
                     ],
                     \TYPO3\CMS\Backend\Form\FormDataProvider\DatabasePageLanguageOverlayRows::class => [
@@ -908,8 +918,14 @@ return [
                             \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRecordOverrideValues::class,
                         ],
                     ],
+                    \TYPO3\CMS\Backend\Form\FormDataProvider\SiteResolving::class => [
+                        'depends' => [
+                            \TYPO3\CMS\Backend\Form\FormDataProvider\TcaGroup::class,
+                        ],
+                    ],
                     \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseSystemLanguageRows::class => [
                         'depends' => [
+                            \TYPO3\CMS\Backend\Form\FormDataProvider\SiteResolving::class,
                             \TYPO3\CMS\Backend\Form\FormDataProvider\TcaGroup::class,
                             \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRecordOverrideValues::class,
                         ],