From 3fae8867a44aff46feb0e60364a508ffa261dccc Mon Sep 17 00:00:00 2001
From: Daniel Siepmann <daniel.siepmann@codappix.com>
Date: Wed, 5 Feb 2025 12:24:43 +0100
Subject: [PATCH] Fix broken data processing within model in newer TYPO3
 version

TYPO3 changed how models are instantiated and prepared.
We now adopt the code to still initialize the data processing.
Furthermore, the feature is now covered with a test.

Relates: #11596
---
 Classes/Domain/Model/Event.php                | 24 ++++++-----
 Documentation/Changelog/5.0.1.rst             | 38 ++++++++++++++++++
 .../Domain/Model/EventTest.php                | 17 ++------
 Tests/Functional/Frontend/DatesTest.php       | 13 ++++++
 .../DateWithAssociatedPageViaEvent.php        | 40 +++++++++++++++++++
 .../Private/Templates/Date/Show.html          |  8 ++++
 ext_emconf.php                                |  2 +-
 7 files changed, 117 insertions(+), 25 deletions(-)
 create mode 100644 Documentation/Changelog/5.0.1.rst
 rename Tests/{Unit => Functional}/Domain/Model/EventTest.php (77%)
 create mode 100644 Tests/Functional/Frontend/DatesTestFixtures/DateWithAssociatedPageViaEvent.php

diff --git a/Classes/Domain/Model/Event.php b/Classes/Domain/Model/Event.php
index 7c742cf..f4e1ffa 100644
--- a/Classes/Domain/Model/Event.php
+++ b/Classes/Domain/Model/Event.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
 
 namespace WerkraumMedia\Events\Domain\Model;
 
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Annotation\ORM\Cascade;
 use TYPO3\CMS\Extbase\Annotation\ORM\Lazy;
 use TYPO3\CMS\Extbase\Domain\Model\FileReference;
@@ -60,6 +61,8 @@ class Event extends AbstractEntity
 
     protected string $pages = '';
 
+    protected array $resolvedPages = [];
+
     /**
      * @var ObjectStorage<Category>
      */
@@ -91,16 +94,18 @@ class Event extends AbstractEntity
     public function __construct()
     {
         $this->initStorageObjects();
-    }
-
-    public function injectDataProcessingForModels(DataProcessingForModels $dataProcessing): void
-    {
-        $this->dataProcessing = $dataProcessing;
+        $this->initializeDataProcessing();
     }
 
     public function initializeObject(): void
     {
         $this->initStorageObjects();
+        $this->initializeDataProcessing();
+    }
+
+    private function initializeDataProcessing(): void
+    {
+        $this->dataProcessing = GeneralUtility::makeInstance(DataProcessingForModels::class);
     }
 
     protected function initStorageObjects(): void
@@ -341,14 +346,13 @@ class Event extends AbstractEntity
 
     public function getPages(): array
     {
-        static $pages = null;
-        if (is_array($pages)) {
-            return $pages;
+        if ($this->resolvedPages !== []) {
+            return $this->resolvedPages;
         }
 
-        $pages = $this->dataProcessing->process($this);
+        $this->resolvedPages = $this->dataProcessing->process($this);
 
-        return $pages;
+        return $this->resolvedPages;
     }
 
     public function addCategory(Category $category): void
diff --git a/Documentation/Changelog/5.0.1.rst b/Documentation/Changelog/5.0.1.rst
new file mode 100644
index 0000000..09069f5
--- /dev/null
+++ b/Documentation/Changelog/5.0.1.rst
@@ -0,0 +1,38 @@
+5.0.1
+=====
+
+Breaking
+--------
+
+Nothing
+
+Features
+--------
+
+Nothing
+
+Fixes
+-----
+
+* Fix broken data processing within model in newer TYPO3 version.
+
+  TYPO3 changed how models are instantiated and prepared.
+  We now adopt the code to still initialize the data processing.
+  Furthermore, the feature is now covered with a test.
+
+* Fix broken data processing caching within model.
+
+  Each model might have different pages.
+  The previous caching implementation would share the result between multiple
+  instances.
+  This is now solved via an internal property instead of static variable.
+
+Tasks
+-----
+
+Nothing
+
+Deprecation
+-----------
+
+Nothing
diff --git a/Tests/Unit/Domain/Model/EventTest.php b/Tests/Functional/Domain/Model/EventTest.php
similarity index 77%
rename from Tests/Unit/Domain/Model/EventTest.php
rename to Tests/Functional/Domain/Model/EventTest.php
index b2e4125..9753972 100644
--- a/Tests/Unit/Domain/Model/EventTest.php
+++ b/Tests/Functional/Domain/Model/EventTest.php
@@ -2,27 +2,16 @@
 
 declare(strict_types=1);
 
-namespace WerkraumMedia\Events\Tests\Unit\Domain\Model;
+namespace WerkraumMedia\Events\Tests\Functional\Domain\Model;
 
 use PHPUnit\Framework\Attributes\Test;
-use PHPUnit\Framework\TestCase;
 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
 use WerkraumMedia\Events\Domain\Model\Category;
 use WerkraumMedia\Events\Domain\Model\Event;
+use WerkraumMedia\Events\Tests\Functional\AbstractFunctionalTestCase;
 
-class EventTest extends TestCase
+class EventTest extends AbstractFunctionalTestCase
 {
-    #[Test]
-    public function canBeCreated(): void
-    {
-        $subject = new Event();
-
-        self::assertInstanceOf(
-            Event::class,
-            $subject
-        );
-    }
-
     #[Test]
     public function returnsSortedFeatures(): void
     {
diff --git a/Tests/Functional/Frontend/DatesTest.php b/Tests/Functional/Frontend/DatesTest.php
index 4f43410..ed920bf 100644
--- a/Tests/Functional/Frontend/DatesTest.php
+++ b/Tests/Functional/Frontend/DatesTest.php
@@ -244,6 +244,19 @@ final class DatesTest extends AbstractFrontendTestCase
         self::assertStringContainsString('<title>Title of Event 15.02.2023 00:00</title>', $html);
     }
 
+    #[Test]
+    public function appliedDataProcessingForPagesAssociatedToEvent(): void
+    {
+        $this->importPHPDataSet(__DIR__ . '/DatesTestFixtures/DateWithAssociatedPageViaEvent.php');
+
+        $response = $this->issueDetailRequest();
+
+        self::assertSame(200, $response->getStatusCode());
+        $html = (string)$response->getBody();
+
+        self::assertStringContainsString('<li><a href="/referenced">Page 3 Referenced from event</a></li>', $html);
+    }
+
     private function issueDetailRequest(): ResponseInterface
     {
         $request = new InternalRequest('https://example.com/');
diff --git a/Tests/Functional/Frontend/DatesTestFixtures/DateWithAssociatedPageViaEvent.php b/Tests/Functional/Frontend/DatesTestFixtures/DateWithAssociatedPageViaEvent.php
new file mode 100644
index 0000000..08b2a20
--- /dev/null
+++ b/Tests/Functional/Frontend/DatesTestFixtures/DateWithAssociatedPageViaEvent.php
@@ -0,0 +1,40 @@
+<?php
+
+declare(strict_types=1);
+
+return  [
+    'tt_content' => [
+        0 => [
+            'uid' => '1',
+            'pid' => '1',
+            'CType' => 'events_dateshowtest',
+            'header' => 'Singleview',
+        ],
+    ],
+    'tx_events_domain_model_event' => [
+        0 => [
+            'uid' => '1',
+            'pid' => '2',
+            'title' => 'Title of Event with associated page',
+            'hidden' => '0',
+            'pages' => '3',
+        ],
+    ],
+    'tx_events_domain_model_date' => [
+        0 => [
+            'uid' => '1',
+            'pid' => '2',
+            'event' => '1',
+            'start' => '1676419200',
+            'end' => '1676484000',
+        ],
+    ],
+    'pages' => [
+        [
+            'uid' => 3,
+            'pid' => 1,
+            'title' => 'Page 3 Referenced from event',
+            'slug' => '/referenced',
+        ],
+    ],
+];
diff --git a/Tests/Functional/Frontend/Fixtures/Extensions/example/Resources/Private/Templates/Date/Show.html b/Tests/Functional/Frontend/Fixtures/Extensions/example/Resources/Private/Templates/Date/Show.html
index eb85541..035918d 100644
--- a/Tests/Functional/Frontend/Fixtures/Extensions/example/Resources/Private/Templates/Date/Show.html
+++ b/Tests/Functional/Frontend/Fixtures/Extensions/example/Resources/Private/Templates/Date/Show.html
@@ -70,4 +70,12 @@
         </div>
     </div>
 
+    <f:if condition="{date.event.pages.menu}">
+        <ul>
+            <f:for each="{date.event.pages.menu}" as="page">
+                <li><a href="{page.link}">{page.title}</a></li>
+            </f:for>
+        </ul>
+    </f:if>
+
 </html>
diff --git a/ext_emconf.php b/ext_emconf.php
index 87ecdb4..7f0e82b 100644
--- a/ext_emconf.php
+++ b/ext_emconf.php
@@ -9,7 +9,7 @@ $EM_CONF['events'] = [
     'author' => 'Dirk Koritnik, Daniel Siepmann',
     'author_email' => 'koritnik@werkraum-media.de, coding@daniel-siepmann.de',
     'state' => 'stable',
-    'version' => '5.0.0',
+    'version' => '5.0.1',
     'constraints' => [
         'depends' => [
             'typo3' => '',