From 709deb867a055cefb7592a17478b2f2fc6d541cc Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Mon, 22 Nov 2021 14:19:21 +0100
Subject: [PATCH] [TASK] Ensure tests for extensions work
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This change makes some FE calls easier for testing
in extensions, such as EXT:solr.

Resolves: #96050
Releases: master, 11.5
Change-Id: I3482ac0abe4e813ff5cba795da9b730ee0efbdee
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/72249
Tested-by: core-ci <typo3@b13.com>
Tested-by: Rafael Kähm <rafael.kaehm@dkd.de>
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Rafael Kähm <rafael.kaehm@dkd.de>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 .../ViewHelpers/Transform/HtmlViewHelperTest.php       | 10 ++++++++--
 .../Classes/ContentObject/ContentObjectRenderer.php    |  5 +++--
 .../Controller/TypoScriptFrontendController.php        |  4 ++--
 .../frontend/Classes/Typolink/PageLinkBuilder.php      |  7 +++++++
 4 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/typo3/sysext/fluid/Tests/Functional/ViewHelpers/Transform/HtmlViewHelperTest.php b/typo3/sysext/fluid/Tests/Functional/ViewHelpers/Transform/HtmlViewHelperTest.php
index 55ea6ec8d65b..868baee3f0e8 100644
--- a/typo3/sysext/fluid/Tests/Functional/ViewHelpers/Transform/HtmlViewHelperTest.php
+++ b/typo3/sysext/fluid/Tests/Functional/ViewHelpers/Transform/HtmlViewHelperTest.php
@@ -19,6 +19,7 @@ namespace TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Transform;
 
 use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
 use TYPO3\CMS\Core\Http\ServerRequest;
+use TYPO3\CMS\Core\Site\Entity\NullSite;
 use TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait;
 use TYPO3\CMS\Fluid\View\StandaloneView;
 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
@@ -43,8 +44,13 @@ class HtmlViewHelperTest extends FunctionalTestCase
             [$this->buildDefaultLanguageConfiguration('EN', '/')]
         );
 
-        $GLOBALS['TYPO3_REQUEST'] = (new ServerRequest('https://typo3.localhost/', 'GET'))
-            ->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_BE);
+        // A nullsite is used, so PageLinkBuilder does not "detect" the default site (from TSFE) as the same
+        // site making all links absolute for our tests
+        $rootPageSite = new NullSite();
+        $GLOBALS['TYPO3_REQUEST'] = (new ServerRequest('https://typo3-2.localhost/', 'GET'))
+            ->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_BE)
+            ->withAttribute('site', $rootPageSite)
+            ->withAttribute('language', $rootPageSite->getDefaultLanguage());
     }
 
     public static function isTransformedDataProvider(): array
diff --git a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
index cbee092877aa..defd173d257d 100644
--- a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
+++ b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
@@ -6798,9 +6798,10 @@ class ContentObjectRenderer implements LoggerAwareInterface
     }
 
     /**
-     * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
+     * @return TypoScriptFrontendController|null
+     * @internal this is set to public so extensions such as EXT:solr can use the method in tests.
      */
-    protected function getTypoScriptFrontendController()
+    public function getTypoScriptFrontendController()
     {
         return $this->typoScriptFrontendController ?: $GLOBALS['TSFE'] ?? null;
     }
diff --git a/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php b/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
index 436f1c655dd9..657ec433c8f5 100644
--- a/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
+++ b/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
@@ -3545,11 +3545,11 @@ class TypoScriptFrontendController implements LoggerAwareInterface
     /**
      * Returns the current BE user.
      *
-     * @return \TYPO3\CMS\Backend\FrontendBackendUserAuthentication
+     * @return FrontendBackendUserAuthentication|null
      */
     protected function getBackendUser()
     {
-        return $GLOBALS['BE_USER'];
+        return $GLOBALS['BE_USER'] ?? null;
     }
 
     /**
diff --git a/typo3/sysext/frontend/Classes/Typolink/PageLinkBuilder.php b/typo3/sysext/frontend/Classes/Typolink/PageLinkBuilder.php
index cd95ac9ae507..a34ea386f8a5 100644
--- a/typo3/sysext/frontend/Classes/Typolink/PageLinkBuilder.php
+++ b/typo3/sysext/frontend/Classes/Typolink/PageLinkBuilder.php
@@ -41,6 +41,7 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
 use TYPO3\CMS\Core\Utility\RootlineUtility;
 use TYPO3\CMS\Frontend\ContentObject\TypolinkModifyLinkConfigForPageLinksHookInterface;
+use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
 
 /**
  * Builds a TypoLink to a certain page
@@ -649,6 +650,9 @@ class PageLinkBuilder extends AbstractTypolinkBuilder
      */
     protected function getCurrentSite(): ?SiteInterface
     {
+        if ($this->typoScriptFrontendController instanceof TypoScriptFrontendController) {
+            return $this->typoScriptFrontendController->getSite();
+        }
         if (isset($GLOBALS['TYPO3_REQUEST']) && $GLOBALS['TYPO3_REQUEST'] instanceof ServerRequestInterface) {
             return $GLOBALS['TYPO3_REQUEST']->getAttribute('site', null);
         }
@@ -672,6 +676,9 @@ class PageLinkBuilder extends AbstractTypolinkBuilder
      */
     protected function getCurrentSiteLanguage(): ?SiteLanguage
     {
+        if ($this->typoScriptFrontendController instanceof TypoScriptFrontendController) {
+            return $this->typoScriptFrontendController->getLanguage();
+        }
         if (isset($GLOBALS['TYPO3_REQUEST']) && $GLOBALS['TYPO3_REQUEST'] instanceof ServerRequestInterface) {
             return $GLOBALS['TYPO3_REQUEST']->getAttribute('language', null);
         }
-- 
GitLab