Commit 874d3d7b authored by Oliver Klee's avatar Oliver Klee Committed by Stefan Bürk
Browse files

[BUGFIX] Allow XCLASSing of WorkspaceRecord

The factory method WorkspaceRecord::get() now uses a proper
makeInstance() call instead of new for creating new instances,
which allows XCLASSing the class and aligns object creation
to "the TYPO3 way" in that place.

Also add a regression test.

Resolves: #97423
Relates: #97754
Releases: main, 11.5
Change-Id: I76e5ce3a1f908bf0efa4faaa439724e2d1d7cbb2
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/74347

Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Simon Schaufelberger's avatarSimon Schaufelberger <simonschaufi+typo3@gmail.com>
Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
Reviewed-by: Simon Schaufelberger's avatarSimon Schaufelberger <simonschaufi+typo3@gmail.com>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
parent 8c9ea809
...@@ -4205,11 +4205,6 @@ parameters: ...@@ -4205,11 +4205,6 @@ parameters:
count: 1 count: 1
path: ../../typo3/sysext/scheduler/Classes/Scheduler.php path: ../../typo3/sysext/scheduler/Classes/Scheduler.php
-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 1
path: ../../typo3/sysext/workspaces/Classes/Domain/Record/WorkspaceRecord.php
- -
message: "#^Offset 0 does not exist on non\\-empty\\-array\\<string, mixed\\>\\.$#" message: "#^Offset 0 does not exist on non\\-empty\\-array\\<string, mixed\\>\\.$#"
count: 2 count: 2
......
...@@ -78,9 +78,8 @@ class WorkspaceRecord extends AbstractRecord ...@@ -78,9 +78,8 @@ class WorkspaceRecord extends AbstractRecord
} elseif (empty($record)) { } elseif (empty($record)) {
$record = static::fetch('sys_workspace', $uid); $record = static::fetch('sys_workspace', $uid);
} }
// [phpstan] Unsafe usage of new static()
// todo: Either mark this class or its constructor final or use new self instead. return GeneralUtility::makeInstance(self::class, $record);
return new static($record);
} }
/** /**
......
<?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\Workspaces\Tests\Unit\Domain\Model;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityFixture;
use TYPO3\CMS\Workspaces\Domain\Record\AbstractRecord;
use TYPO3\CMS\Workspaces\Domain\Record\WorkspaceRecord;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
class WorkspaceRecordTest extends UnitTestCase
{
protected function setUp(): void
{
parent::setUp();
GeneralUtilityFixture::resetFinalClassNameCache();
}
protected function tearDown(): void
{
GeneralUtilityFixture::resetFinalClassNameCache();
parent::tearDown();
}
/**
* @test
*/
public function isAbstractRecord(): void
{
$subject = new WorkspaceRecord([]);
self::assertInstanceOf(AbstractRecord::class, $subject);
}
/**
* @test
*/
public function getReturnsWorkspaceRecordInstance(): void
{
$instance = WorkspaceRecord::get(1, ['title' => '']);
self::assertInstanceOf(WorkspaceRecord::class, $instance);
}
/**
* @test
*/
public function getWithNonZeroUidAndNonEmptyDataReturnsInstanceWithTheProvidedData(): void
{
$title = 'some record title';
$instance = WorkspaceRecord::get(1, ['title' => $title]);
self::assertSame($title, $instance->getTitle());
}
/**
* @test
*/
public function getCalledTwoTimesWithTheSameUidAndDataDataReturnsDifferentInstancesForEachCall(): void
{
$uid = 1;
$data = ['title' => ''];
$instance1 = WorkspaceRecord::get($uid, $data);
$instance2 = WorkspaceRecord::get($uid, $data);
self::assertNotSame($instance1, $instance2);
}
/**
* @test
*/
public function getForConfiguredXclassReturnsInstanceOfXclass(): void
{
$xclassInstance = new class([]) extends WorkspaceRecord {
};
$xclassName = get_class($xclassInstance);
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][WorkspaceRecord::class] = ['className' => $xclassName];
$instance = WorkspaceRecord::get(1, ['title' => '']);
self::assertInstanceOf($xclassName, $instance);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment