From 8503ba3e8b5bb470f8b7efd91dc88cb37d7dc7f6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20B=C3=BCrk?= <stefan@buerk.tech>
Date: Thu, 4 Apr 2024 10:53:18 +0200
Subject: [PATCH] [BUGFIX] Ensure correct access for LazyLoadingProxy test
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

To harden the Extbase LazyLoadingProxy implementation
a new test has been added with #101400, which creates
a proxy directly and using ObjectAccess::getProperty()
to access a property of the not loadable child proxy
class instance.

Due to the use of magic methods`__get()` and `__isset()`
methods in the `LazyLoadingProxy` class and some wrong
assumptions of the Symfony PropertyAccessor simply null
has been retrieved even if the concrete class could not
been retrieved (property on a null value instead of an
object). The property accessor is used under the hood
within the Extbase internal ObjectAccess implementation.

The Symfony Team recently declared that behaviour a bug
and fixed it with releases `7.0.6` and `6.4.6` and now
correctly throwing the `PropertyNotAccessibleException`.

Failing nightlies with Symfony releases containing the
bugfix revealed a incorrect test implementation, using
a constructed and unrealistic construct.

This change modifies the test to use a correct access
on the LazyLoadingProxy without changing the scenario
of the test.

[1] https://github.com/symfony/symfony/releases/tag/v7.0.6
[2] https://github.com/symfony/symfony/releases/tag/v6.4.6
[3] https://github.com/symfony/symfony/pull/54194

Resolves: #103531
Related: #101400
Releases: main, 12.4
Change-Id: I5bdd52955af138c1e99b4492c9b5a43839c743cf
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83645
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Torben Hansen <derhansen@gmail.com>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Torben Hansen <derhansen@gmail.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
---
 .../Persistence/LazyLoadingProxyTest.php         | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/typo3/sysext/extbase/Tests/Functional/Persistence/LazyLoadingProxyTest.php b/typo3/sysext/extbase/Tests/Functional/Persistence/LazyLoadingProxyTest.php
index a47e0365942d..e46768a5733d 100644
--- a/typo3/sysext/extbase/Tests/Functional/Persistence/LazyLoadingProxyTest.php
+++ b/typo3/sysext/extbase/Tests/Functional/Persistence/LazyLoadingProxyTest.php
@@ -20,8 +20,9 @@ namespace TYPO3\CMS\Extbase\Tests\Functional\Persistence;
 use PHPUnit\Framework\Attributes\Test;
 use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
 use TYPO3\CMS\Core\Http\ServerRequest;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy;
-use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
+use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
 use TYPO3Tests\BlogExample\Domain\Model\Administrator;
 use TYPO3Tests\BlogExample\Domain\Model\Blog;
@@ -63,7 +64,16 @@ final class LazyLoadingProxyTest extends FunctionalTestCase
     #[Test]
     public function nonExistingLazyLoadedPropertyReturnsNull(): void
     {
-        $lazyLoadingProxy = new LazyLoadingProxy(new Blog(), 'administrator', 0);
-        self::assertNull(ObjectAccess::getProperty($lazyLoadingProxy, 'name'));
+        $lazyLoadingProxy = new LazyLoadingProxy(
+            new Blog(),
+            'administrator',
+            0,
+            GeneralUtility::makeInstance(DataMapper::class)
+        );
+        // Directly using the magic `__get()` method here to avoid PHPStan complaining
+        // about the dynamic property issue and spare an ignore pattern or annotation.
+        // See: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property
+        // This equals to: self::assertNull($lazyLoadingProxy->name);
+        self::assertNull($lazyLoadingProxy->__get('name'));
     }
 }
-- 
GitLab