‪TYPO3CMS  ‪main
PathUtilityPublicPathsTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
27 final class ‪PathUtilityPublicPathsTest extends UnitTestCase
28 {
29  protected bool ‪$backupEnvironment = true;
30 
31  protected array ‪$serverBackup;
32 
33  protected function ‪setUp(): void
34  {
35  $this->serverBackup = ‪$_SERVER;
36  parent::setUp();
37  }
38 
39  protected function ‪tearDown(): void
40  {
42  parent::tearDown();
43  }
44 
48  protected static function ‪simulateWebRequest(string $publicDir, string $subDirectory = '/'): void
49  {
50  ‪$_SERVER['HTTP_HOST'] = 'localhost';
51  ‪$_SERVER['SCRIPT_NAME'] = $subDirectory . 'index.php';
52 
55  false,
56  true,
58  $publicDir,
61  $publicDir . '/index.php',
62  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
63  );
64  }
65 
66  protected static function ‪simulateWebRequestInComposerMode(): void
67  {
69  }
70 
71  protected static function ‪simulateWebRequestInComposerModeInSubDirectory(string $subDirectory = '/subDir/'): void
72  {
73  ‪self::simulateWebRequest(‪Environment::getPublicPath() . '/typo3temp', $subDirectory);
74  }
75 
76  protected static function ‪simulateTraditionalWebRequest(): void
77  {
79  }
80 
81  protected static function ‪simulateTraditionalWebRequestInSubDirectory(string $subDirectory = '/subDir/'): void
82  {
84  }
85 
86  #[Test]
88  {
89  $this->expectException(InvalidFileException::class);
90  $this->expectExceptionCode(1630089406);
91  ‪PathUtility::getPublicResourceWebPath('typo3/sysext/core/Resources/Public/Icons/Extension.svg');
92  }
93 
94  #[Test]
96  {
98  $this->expectException(InvalidFileException::class);
99  $this->expectExceptionCode(1635268969);
100  ‪PathUtility::getPublicResourceWebPath('EXT:core/Resources/Private/Font/nimbus.ttf');
101  }
102 
104  {
105  return [
106  'public assets are resolved to absolute url' => [
107  'EXT:core/Resources/Public/Icons/Extension.svg',
108  '/typo3/sysext/core/Resources/Public/Icons/Extension.svg',
110  ],
111  'public assets are resolved to relative url on cli' => [
112  'EXT:core/Resources/Public/Icons/Extension.svg',
113  'typo3/sysext/core/Resources/Public/Icons/Extension.svg',
114  fn() => true,
115  ],
116  'public assets are resolved to absolute url with sub directory prefixed' => [
117  'EXT:core/Resources/Public/Icons/Extension.svg',
118  '/cms/typo3/sysext/core/Resources/Public/Icons/Extension.svg',
120  ],
121  'public assets are resolved to absolute url of published assets in Composer mode' => [
122  'EXT:core/Resources/Public/Icons/Extension.svg',
123  '/_assets/d25de869aebcd01495d2fe67ad5b0e25/Icons/Extension.svg',
125  ],
126  'public assets are resolved to absolute url of published assets in Composer mode with sub directory prefixed' => [
127  'EXT:core/Resources/Public/Icons/Extension.svg',
128  '/cms/_assets/d25de869aebcd01495d2fe67ad5b0e25/Icons/Extension.svg',
130  ],
131  ];
132  }
133 
137  #[DataProvider('getPublicResourceWebPathResolvesUrlsCorrectlyDataProvider')]
138  #[Test]
139  public function ‪getPublicResourceWebPathResolvesUrlsCorrectly(string $pathReference, string $expectedUrl, callable $setup): void
140  {
141  $setup();
142  self::assertSame(
143  $expectedUrl,
145  );
146  }
147 }
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest\tryingToResolveNonExtensionResourcesThrowsException
‪tryingToResolveNonExtensionResourcesThrowsException()
Definition: PathUtilityPublicPathsTest.php:87
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest\tryingToResolvePrivateResourcesFromComposerPackagesThrowsException
‪tryingToResolvePrivateResourcesFromComposerPackagesThrowsException()
Definition: PathUtilityPublicPathsTest.php:95
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest\simulateWebRequestInComposerModeInSubDirectory
‪static simulateWebRequestInComposerModeInSubDirectory(string $subDirectory='/subDir/')
Definition: PathUtilityPublicPathsTest.php:71
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest\simulateWebRequestInComposerMode
‪static simulateWebRequestInComposerMode()
Definition: PathUtilityPublicPathsTest.php:66
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest\simulateTraditionalWebRequest
‪static simulateTraditionalWebRequest()
Definition: PathUtilityPublicPathsTest.php:76
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static getConfigPath()
Definition: Environment.php:212
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:160
‪TYPO3\CMS\Core\Utility\PathUtility\getPublicResourceWebPath
‪static getPublicResourceWebPath(string $resourcePath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:97
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest\simulateTraditionalWebRequestInSubDirectory
‪static simulateTraditionalWebRequestInSubDirectory(string $subDirectory='/subDir/')
Definition: PathUtilityPublicPathsTest.php:81
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest\setUp
‪setUp()
Definition: PathUtilityPublicPathsTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest\tearDown
‪tearDown()
Definition: PathUtilityPublicPathsTest.php:39
‪TYPO3\CMS\Core\Resource\Exception\InvalidFileException
Definition: InvalidFileException.php:23
‪TYPO3\CMS\Core\Core\Environment\initialize
‪static initialize(ApplicationContext $context, bool $cli, bool $composerMode, string $projectPath, string $publicPath, string $varPath, string $configPath, string $currentScript, string $os)
Definition: Environment.php:100
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest\$backupEnvironment
‪bool $backupEnvironment
Definition: PathUtilityPublicPathsTest.php:29
‪$_SERVER
‪$_SERVER['TYPO3_DEPRECATED_ENTRYPOINT']
Definition: legacy-backend.php:20
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest\getPublicResourceWebPathResolvesUrlsCorrectly
‪getPublicResourceWebPathResolvesUrlsCorrectly(string $pathReference, string $expectedUrl, callable $setup)
Definition: PathUtilityPublicPathsTest.php:139
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest\$serverBackup
‪array $serverBackup
Definition: PathUtilityPublicPathsTest.php:31
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest\getPublicResourceWebPathResolvesUrlsCorrectlyDataProvider
‪static getPublicResourceWebPathResolvesUrlsCorrectlyDataProvider()
Definition: PathUtilityPublicPathsTest.php:103
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest
Definition: PathUtilityPublicPathsTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityPublicPathsTest\simulateWebRequest
‪static simulateWebRequest(string $publicDir, string $subDirectory='/')
Definition: PathUtilityPublicPathsTest.php:48
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static getContext()
Definition: Environment.php:128
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static isWindows()
Definition: Environment.php:276