From 752385546429657c8b56f9721a568201279fae50 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Albrecht=20K=C3=B6hnlein?= <ak@koehnlein.eu>
Date: Sun, 19 Mar 2023 21:06:31 +0100
Subject: [PATCH] [BUGFIX] Avoid PHP 8 warnings when processing the
 INTinclude-scripts
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Avoid PHP warnings when calling the method to process INTinclude
scripts when jsInline or jsFooterInline was set.

Previously, the jsInline and jsFooterInline was called after
$TSFE->INTincScript_loadJScode(), where TSFE considered
there were no *_INT objects, however, they were processed
later-on, thus no "divKey" was set and a PHP warning
was set.

Change-Id: Ie639d58a2efce10177a41042080534ce26a1a291
Resolves: #100216
Releases: main, 11.5
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/78065
Tested-by: Nikita Hovratov <nikita.h@live.de>
Tested-by: Albrecht Köhnlein <ak@koehnlein.eu>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Albrecht Köhnlein <ak@koehnlein.eu>
Reviewed-by: Nikita Hovratov <nikita.h@live.de>
---
 .../frontend/Classes/Http/RequestHandler.php  |  7 ++-
 .../Controller/DataSet/LiveDefaultPages.csv   | 11 ++++
 .../Controller/Fixtures/jsInline.typoscript   | 10 ++++
 ...riptFrontendControllerWithFrontendTest.php | 58 +++++++++++++++++++
 4 files changed, 84 insertions(+), 2 deletions(-)
 create mode 100644 typo3/sysext/frontend/Tests/Functional/Controller/DataSet/LiveDefaultPages.csv
 create mode 100644 typo3/sysext/frontend/Tests/Functional/Controller/Fixtures/jsInline.typoscript
 create mode 100644 typo3/sysext/frontend/Tests/Functional/Controller/TypoScriptFrontendControllerWithFrontendTest.php

diff --git a/typo3/sysext/frontend/Classes/Http/RequestHandler.php b/typo3/sysext/frontend/Classes/Http/RequestHandler.php
index 5e9a8a87f745..f16cefd9ae8c 100644
--- a/typo3/sysext/frontend/Classes/Http/RequestHandler.php
+++ b/typo3/sysext/frontend/Classes/Http/RequestHandler.php
@@ -715,13 +715,16 @@ class RequestHandler implements RequestHandlerInterface
             $controller->cObj
         );
 
-        $controller->INTincScript_loadJSCode();
-
         // Javascript inline code
         $inlineJS = $controller->cObj->cObjGet($controller->pSetup['jsInline.'] ?? null, 'jsInline.');
         // Javascript inline code for Footer
         $inlineFooterJs = $controller->cObj->cObjGet($controller->pSetup['jsFooterInline.'] ?? null, 'jsFooterInline.');
         $compressJs = (bool)($controller->config['config']['compressJs'] ?? false);
+
+        // Needs to be called after call cObjGet() calls in order to get all headerData and footerData and replacements
+        // see #100216
+        $controller->INTincScript_loadJSCode();
+
         if (($controller->config['config']['removeDefaultJS'] ?? '') === 'external') {
             /*
              * This keeps inlineJS from *_INT Objects from being moved to external files.
diff --git a/typo3/sysext/frontend/Tests/Functional/Controller/DataSet/LiveDefaultPages.csv b/typo3/sysext/frontend/Tests/Functional/Controller/DataSet/LiveDefaultPages.csv
new file mode 100644
index 000000000000..ea62798ff0fa
--- /dev/null
+++ b/typo3/sysext/frontend/Tests/Functional/Controller/DataSet/LiveDefaultPages.csv
@@ -0,0 +1,11 @@
+"pages"
+,"uid","pid","sorting","deleted","sys_language_uid","l10n_parent","t3_origuid","t3ver_wsid","t3ver_state","t3ver_stage","t3ver_oid","title","slug","l10n_state"
+,1,0,256,0,0,0,0,0,0,0,0,"Pre page without template","","{}"
+,2,1,256,0,0,0,0,0,0,0,0,"Root page having template with root flag set by tests","/","{}"
+,88,2,256,0,0,0,0,0,0,0,0,"Sub page 1","/subpage1/","{}"
+,89,88,256,0,0,0,0,0,0,0,0,"Sub sub page 1","/subpage1/sub1/","{}"
+,98,2,256,0,0,0,0,0,0,0,0,"Sub page 2 having template with root flag","/subpage2/","{}"
+,99,98,256,0,0,0,0,0,0,0,0,"Sub sub page 2","/subpage2/sub2/","{}"
+"sys_template"
+,"uid","pid","sorting","deleted","hidden","starttime","endtime","t3_origuid","root","clear","include_static_file","constants","config","basedOn","includeStaticAfterBasedOn","static_file_mode"
+,1,98,256,0,0,0,0,0,1,0,"","","","",0,0
diff --git a/typo3/sysext/frontend/Tests/Functional/Controller/Fixtures/jsInline.typoscript b/typo3/sysext/frontend/Tests/Functional/Controller/Fixtures/jsInline.typoscript
new file mode 100644
index 000000000000..bb119250f1ce
--- /dev/null
+++ b/typo3/sysext/frontend/Tests/Functional/Controller/Fixtures/jsInline.typoscript
@@ -0,0 +1,10 @@
+page = PAGE
+page {
+  jsInline {
+    10 = COA_INT
+    10 {
+      10 = TEXT
+      10.value = alert(yes);
+    }
+  }
+}
diff --git a/typo3/sysext/frontend/Tests/Functional/Controller/TypoScriptFrontendControllerWithFrontendTest.php b/typo3/sysext/frontend/Tests/Functional/Controller/TypoScriptFrontendControllerWithFrontendTest.php
new file mode 100644
index 000000000000..6249a53b82ed
--- /dev/null
+++ b/typo3/sysext/frontend/Tests/Functional/Controller/TypoScriptFrontendControllerWithFrontendTest.php
@@ -0,0 +1,58 @@
+<?php
+
+declare(strict_types=1);
+
+/*
+ * 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!
+ */
+
+namespace TYPO3\CMS\Frontend\Tests\Functional\Controller;
+
+use TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait;
+use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
+use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
+
+class TypoScriptFrontendControllerWithFrontendTest extends FunctionalTestCase
+{
+    use SiteBasedTestTrait;
+
+    protected const LANGUAGE_PRESETS = [
+        'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
+    ];
+
+    /**
+     * @test
+     */
+    public function jsIncludesWithUserIntIsRendered(): void
+    {
+        $this->importCSVDataSet(__DIR__ . '/DataSet/LiveDefaultPages.csv');
+        $this->setUpFrontendRootPage(
+            2,
+            [
+                'typo3/sysext/frontend/Tests/Functional/Controller/Fixtures/jsInline.typoscript',
+            ]
+        );
+        $this->writeSiteConfiguration(
+            'test',
+            $this->buildSiteConfiguration(2, 'https://website.local/'),
+            [$this->buildDefaultLanguageConfiguration('EN', '/en/')]
+        );
+
+        $response = $this->executeFrontendSubRequest(
+            (new InternalRequest('https://website.local/en/'))->withPageId(2)
+        );
+
+        $body = (string)$response->getBody();
+        self::assertStringContainsString('/*TS_inlineJSint*/
+alert(yes);', $body);
+    }
+}
-- 
GitLab