‪TYPO3CMS  ‪main
PolicyProviderTest.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;
22 use Psr\Http\Message\ServerRequestInterface;
29 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
30 
31 final class ‪PolicyProviderTest extends FunctionalTestCase
32 {
34 
35  protected const ‪LANGUAGE_PRESETS = [
36  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
37  'FR' => ['id' => 1, 'title' => 'Français', 'locale' => 'fr_FR.UTF8'],
38  ];
39 
40  protected function ‪setUp(): void
41  {
42  parent::setUp();
43 
46  true,
47  true,
49  ‪Environment::getPublicPath() . '/typo3temp/public',
53  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
54  );
55 
57  'relative',
58  $this->‪buildSiteConfiguration(1000, '/relative/'),
59  [
60  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
61  $this->‪buildLanguageConfiguration('FR', '/fr/'),
62  ]
63  );
65  'absolute-same-site',
66  $this->‪buildSiteConfiguration(1000, 'https://website.local/same/'),
67  [
68  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
69  $this->‪buildLanguageConfiguration('FR', '/fr/'),
70  ]
71  );
73  'absolute-cross-site',
74  $this->‪buildSiteConfiguration(1000, 'https://website.local/cross/'),
75  [
76  $this->‪buildDefaultLanguageConfiguration('EN', 'https://en.website.local/'),
77  $this->‪buildLanguageConfiguration('FR', 'https://fr.website.local/'),
78  ]
79  );
80  }
81 
82  public static function ‪defaultReportingUriBaseIsResolvedDataProvider(): \Generator
83  {
84  $frontendRelative = ‪Scope::frontendSiteIdentifier('relative');
85  $frontendAbsoluteSameSite = ‪Scope::frontendSiteIdentifier('absolute-same-site');
86  $frontendAbsoluteCrossSite = ‪Scope::frontendSiteIdentifier('absolute-cross-site');
87 
88  // parts: scope (BE, FE, FE+site) | language preset | absolute | expected URI
89  yield [$frontendRelative, null, false, '/relative/en/@http-reporting?csp=report'];
90  yield [$frontendRelative, 'EN', false, '/relative/en/@http-reporting?csp=report'];
91  yield [$frontendRelative, 'FR', false, '/relative/fr/@http-reporting?csp=report'];
92  yield [$frontendRelative, null, true, 'https://website.fallback/relative/en/@http-reporting?csp=report'];
93  yield [$frontendRelative, 'EN', true, 'https://website.fallback/relative/en/@http-reporting?csp=report'];
94  yield [$frontendRelative, 'FR', true, 'https://website.fallback/relative/fr/@http-reporting?csp=report'];
95 
96  yield [$frontendAbsoluteSameSite, null, false, '/same/en/@http-reporting?csp=report'];
97  yield [$frontendAbsoluteSameSite, 'EN', false, '/same/en/@http-reporting?csp=report'];
98  yield [$frontendAbsoluteSameSite, 'FR', false, '/same/fr/@http-reporting?csp=report'];
99  yield [$frontendAbsoluteSameSite, null, true, 'https://website.local/same/en/@http-reporting?csp=report'];
100  yield [$frontendAbsoluteSameSite, 'EN', true, 'https://website.local/same/en/@http-reporting?csp=report'];
101  yield [$frontendAbsoluteSameSite, 'FR', true, 'https://website.local/same/fr/@http-reporting?csp=report'];
102 
103  yield [$frontendAbsoluteCrossSite, null, false, '/@http-reporting?csp=report'];
104  yield [$frontendAbsoluteCrossSite, 'EN', false, '/@http-reporting?csp=report'];
105  yield [$frontendAbsoluteCrossSite, 'FR', false, '/@http-reporting?csp=report'];
106  yield [$frontendAbsoluteCrossSite, null, true, 'https://en.website.local/@http-reporting?csp=report'];
107  yield [$frontendAbsoluteCrossSite, 'EN', true, 'https://en.website.local/@http-reporting?csp=report'];
108  yield [$frontendAbsoluteCrossSite, 'FR', true, 'https://fr.website.local/@http-reporting?csp=report'];
109 
110  yield [‪Scope::frontend(), null, false, '/@http-reporting?csp=report'];
111  yield [‪Scope::frontend(), null, true, 'https://website.fallback/@http-reporting?csp=report'];
112 
113  yield [‪Scope::backend(), null, false, '/typo3/@http-reporting?csp=report'];
114  yield [‪Scope::backend(), null, true, 'https://website.fallback/typo3/@http-reporting?csp=report'];
115  }
116 
117  #[DataProvider('defaultReportingUriBaseIsResolvedDataProvider')]
118  #[Test]
119  public function ‪defaultReportingUriBaseIsResolved(‪Scope $scope, ?string $languagePreset, bool $absolute, string $expectation): void
120  {
121  $request = $this->‪buildServerRequest($scope, $languagePreset);
122  $subject = $this->get(PolicyProvider::class);
123  $actual = (string)$subject->getDefaultReportingUriBase($scope, $request, $absolute);
124  self::assertSame($expectation, $actual);
125  }
126 
127  private function ‪buildServerRequest(‪Scope $scope, ?string $languagePreset): ServerRequestInterface
128  {
129  $request = new ‪ServerRequest(
130  '/',
131  'GET',
132  null,
133  [],
134  [
135  'HTTPS' => 'on',
136  'HTTP_HOST' => 'website.fallback',
137  ]
138  );
139  if ($scope->siteIdentifier !== null) {
140  $site = $this->get(SiteFinder::class)->getSiteByIdentifier($scope->siteIdentifier);
141  $request = $request
142  ->withAttribute('site', $site)
143  ->withAttribute(
144  'siteLanguage',
145  $languagePreset !== null
146  ? $site->getLanguageById($this->resolveLanguagePreset($languagePreset)['id'])
147  : $site->getDefaultLanguage()
148  );
149  }
150  return $request;
151  }
152 }
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildLanguageConfiguration
‪buildLanguageConfiguration(string $identifier, string $base, array $fallbackIdentifiers=[], string $fallbackType=null)
Definition: SiteBasedTestTrait.php:108
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\backend
‪static backend()
Definition: Scope.php:41
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:37
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\PolicyProviderTest\buildServerRequest
‪buildServerRequest(Scope $scope, ?string $languagePreset)
Definition: PolicyProviderTest.php:126
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:50
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\PolicyProviderTest\defaultReportingUriBaseIsResolvedDataProvider
‪static defaultReportingUriBaseIsResolvedDataProvider()
Definition: PolicyProviderTest.php:81
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:88
‪TYPO3\CMS\Core\Core\Environment\getCurrentScript
‪static getCurrentScript()
Definition: Environment.php:220
‪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\Security\ContentSecurityPolicy\Scope\frontend
‪static frontend()
Definition: Scope.php:46
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\PolicyProviderTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: PolicyProviderTest.php:34
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:160
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy
Definition: ModelServiceTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\PolicyProviderTest\defaultReportingUriBaseIsResolved
‪defaultReportingUriBaseIsResolved(Scope $scope, ?string $languagePreset, bool $absolute, string $expectation)
Definition: PolicyProviderTest.php:118
‪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\Security\ContentSecurityPolicy\Scope
Definition: Scope.php:30
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\PolicyProviderTest
Definition: PolicyProviderTest.php:32
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:98
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\frontendSiteIdentifier
‪static frontendSiteIdentifier(string $siteIdentifier)
Definition: Scope.php:64
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\PolicyProvider
Definition: PolicyProvider.php:38
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static getContext()
Definition: Environment.php:128
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\PolicyProviderTest\setUp
‪setUp()
Definition: PolicyProviderTest.php:39
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static isWindows()
Definition: Environment.php:276