Skip to content

Commit

Permalink
[BUGFIX] Ensure that relative target link can be set in EXT:redirects
Browse files Browse the repository at this point in the history
The TYPO3 system extension `redirects` now fully supports the setting
of relative links as the `target` URL, which is a documented feature.

Previously, an issue arose when attempting to use path links with
leading slashes as URLs. This issue has been addressed in #101083,
marking the initial step toward resolving the problem of relative
target links.

Additionally, the fix now allows for linking to arbitrary resources,
even those that do not currently exist in the File Abstraction Layer
(FAL).

The class `TYPO3\CMS\Core\LinkHandling\LegacyLinkNotationConverter`
has been changed to limit the handling of arbitrary resources
to the smallest scope to reuse existing code and to use them
as `LinkService::TYPE_URL` links.

The superflous outer catch block has been removed to remove
dead code. In addition, a test for coverage has been added
that covers DataHandler and the lower LinkHandling layer.

Resolves: #103387
Related: #101083
Releases: main, 12.4
Change-Id: I51cad29e5551824a0ad8d4528aa43cff7e0f9da7
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83444
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Simon Schaufelberger <simonschaufi+typo3@gmail.com>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
  • Loading branch information
sbuerk committed Apr 21, 2024
1 parent aee514a commit eebf251
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 6 deletions.
Expand Up @@ -18,7 +18,7 @@
namespace TYPO3\CMS\Core\LinkHandling;

use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
use TYPO3\CMS\Core\Resource\Exception as ResourceException;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\Folder;
use TYPO3\CMS\Core\Resource\ResourceFactory;
Expand Down Expand Up @@ -218,7 +218,11 @@ protected function getFileOrFolderObjectFromMixedIdentifier(string $mixedIdentif
if (str_contains($fileIdentifier, '#')) {
[$fileIdentifier, $fragment] = explode('#', $fileIdentifier, 2);
}
$fileOrFolderObject = $this->getResourceFactory()->retrieveFileOrFolderObject($fileIdentifier);
try {
$fileOrFolderObject = $this->getResourceFactory()->retrieveFileOrFolderObject($fileIdentifier);
} catch (ResourceException $e) {
$fileOrFolderObject = null;
}
// Links to a file/folder in the main TYPO3 directory should not be considered as file links, but an external link
if ($fileOrFolderObject instanceof ResourceInterface && $fileOrFolderObject->getStorage()->isFallbackStorage()) {
return [
Expand Down Expand Up @@ -250,10 +254,6 @@ protected function getFileOrFolderObjectFromMixedIdentifier(string $mixedIdentif
// Element wasn't found
$result['type'] = LinkService::TYPE_UNKNOWN;
$result['file'] = $mixedIdentifier;
} catch (ResourceDoesNotExistException $e) {
// Resource was not found
$result['type'] = LinkService::TYPE_UNKNOWN;
$result['file'] = $mixedIdentifier;
}

return $result;
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,94 @@
<?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\Core\Tests\Functional\LinkHandling;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\LinkHandling\LegacyLinkNotationConverter;
use TYPO3\CMS\Core\LinkHandling\LinkService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

final class LegacyLinkNotationConverterTest extends FunctionalTestCase
{
/**
* @var array<string, non-empty-string>
*/
protected array $pathsToProvideInTestInstance = [
'typo3/sysext/core/Tests/Functional/LinkHandling/Fixtures/fileadmin/' => 'fileadmin/',
];

protected function tearDown(): void
{
GeneralUtility::rmdir(Environment::getPublicPath() . '/fileadmin', true);
mkdir(Environment::getPublicPath() . '/fileadmin');
GeneralUtility::rmdir(Environment::getPublicPath() . '/typo3temp/assets/_processed_', true);
parent::tearDown();
}

public static function resolveReturnExpectedResultDataProvider(): \Generator
{
yield '#1 linkParameter looking like absolute path without tailing slash and not existing (file and folder) in FAL resolves to type URL' => [
'linkParameter' => '/path/not/existing/in/fal',
'expected' => [
'type' => LinkService::TYPE_URL,
'url' => '/path/not/existing/in/fal',
],
];
yield '#2 linkParameter looking like absolute path with tailing slash and not existing (file and folder) in FAL resolves to type URL' => [
'linkParameter' => '/path/not/existing/in/fal/',
'expected' => [
'type' => LinkService::TYPE_URL,
'url' => '/path/not/existing/in/fal/',
],
];
yield '#3 linkParameter looking like absolute path without tailing slash and existing folder in FAL resolves to type URL' => [
'linkParameter' => '/fileadmin/some/path',
'expected' => [
'type' => LinkService::TYPE_URL,
'url' => '/fileadmin/some/path',
],
];
yield '#4 linkParameter looking like absolute path with tailing slash and existing folder in FAL resolves to type URL' => [
'linkParameter' => '/fileadmin/some/path/',
'expected' => [
'type' => LinkService::TYPE_URL,
'url' => '/fileadmin/some/path/',
],
];
yield '#5 linkParameter for existing file returns type URL on fallback storage' => [
'linkParameter' => '/fileadmin/some/path/typo3-logo.png',
'expected' => [
'type' => LinkService::TYPE_URL,
'url' => '/fileadmin/some/path/typo3-logo.png',
],
];
}

#[DataProvider('resolveReturnExpectedResultDataProvider')]
#[Test]
public function resolveReturnsExpectedResult(
string $linkParameter,
array $expected,
): void {
$subject = GeneralUtility::makeInstance(LegacyLinkNotationConverter::class);

self::assertSame($expected, $subject->resolve($linkParameter));
}
}
@@ -0,0 +1,66 @@
<?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\Core\Tests\Functional\LinkHandling;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\LinkHandling\LinkService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

final class LinkServiceTest extends FunctionalTestCase
{
/**
* @var array<string, non-empty-string>
*/
protected array $pathsToProvideInTestInstance = [
'typo3/sysext/core/Tests/Functional/LinkHandling/Fixtures/fileadmin/' => 'fileadmin/',
];

protected function tearDown(): void
{
GeneralUtility::rmdir(Environment::getPublicPath() . '/fileadmin', true);
mkdir(Environment::getPublicPath() . '/fileadmin');
GeneralUtility::rmdir(Environment::getPublicPath() . '/typo3temp/assets/_processed_', true);
parent::tearDown();
}

/**
* @return array<string, array<string, mixed>>
*/
public static function resolveReturnExpectedResultDataProvider(): array
{
// Reuse DataProviders of concrete link-type testcases
return [
...LegacyLinkNotationConverterTest::resolveReturnExpectedResultDataProvider(),
// @todo Add tests for other link types and combine them here for the link service level
];
}

#[DataProvider('resolveReturnExpectedResultDataProvider')]
#[Test]
public function resolveReturnsExpectedResult(
string $linkParameter,
array $expected,
): void {
$subject = GeneralUtility::makeInstance(LinkService::class);

self::assertSame($expected, $subject->resolve($linkParameter));
}
}
@@ -0,0 +1,6 @@
"pages",
,"uid","pid","title","slug","l10n_parent","l10n_source","sys_language_uid","doktype"
,1,0,"Root 1","/",0,0,0,1
"sys_redirect",
,"uid","pid","deleted","disabled","source_host","source_path","target"
,1,1,0,0,"*","/test-redirect-1/","/relative-target/"
@@ -0,0 +1,3 @@
"pages",,,,,,,
,"uid","pid","title","slug","l10n_parent","l10n_source","sys_language_uid","doktype"
,1,0,"Root 1","/",0,0,0,1
Expand Up @@ -23,7 +23,9 @@
use Symfony\Component\DependencyInjection\Container;
use TYPO3\CMS\Core\Configuration\SiteWriter;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\DataHandling\Model\CorrelationId;
use TYPO3\CMS\Core\DataHandling\PageDoktypeRegistry;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\EventDispatcher\ListenerProvider;
use TYPO3\CMS\Core\LinkHandling\LinkService;
Expand Down Expand Up @@ -557,6 +559,37 @@ public function spacerWithSubPagesDoesNotCreateAutoRedirectForSpacerButUpdatesSu
$this->assertSlugsAndRedirectsExists($slugs, $redirects);
}

#[Test]
public function relativeTargetCanBeSetUsingDataHandler(): void
{
$newRedirect = StringUtility::getUniqueId('NEW');
$dataMap = [
'sys_redirect' => [
$newRedirect => [
'pid' => 1,
'deleted' => 0,
'disabled' => 0,
'source_host' => '*',
'source_path' => '/test-redirect-1/',
'target' => '/relative-target/',
],
],
];
$this->importCSVDataSet(__DIR__ . '/Fixtures/RelativeTargetDataHandler.csv');
$this->buildBaseSite();

// For testing scenario we need to allow redirect records be added to normal pages.
$dokTypeRegistry = GeneralUtility::makeInstance(PageDoktypeRegistry::class);
$dokTypeRegistry->addAllowedRecordTypes(['sys_redirect'], PageRepository::DOKTYPE_DEFAULT);

$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
$dataHandler->start($dataMap, []);
$dataHandler->process_datamap();

self::assertSame([], $dataHandler->errorLog);
$this->assertCSVDataSet(__DIR__ . '/Fixtures/AssertionDataSets/RelativeTargetDataHandler.csv');
}

protected function buildBaseSite(): void
{
$configuration = [
Expand Down

0 comments on commit eebf251

Please sign in to comment.