‪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 Psr\Http\Message\ServerRequestInterface;
27 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
28 
29 final class ‪PolicyProviderTest extends FunctionalTestCase
30 {
32 
33  protected const ‪LANGUAGE_PRESETS = [
34  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
35  'FR' => ['id' => 1, 'title' => 'Fran├žais', 'locale' => 'fr_FR.UTF8'],
36  ];
37 
38  protected bool ‪$initializeDatabase = false;
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 
121  public function ‪defaultReportingUriBaseIsResolved(‪Scope $scope, ?string $languagePreset, bool $absolute, string $expectation): void
122  {
123  $request = $this->‪buildServerRequest($scope, $languagePreset);
124  $subject = $this->get(PolicyProvider::class);
125  $actual = (string)$subject->getDefaultReportingUriBase($scope, $request, $absolute);
126  self::assertSame($expectation, $actual);
127  }
128 
129  private function ‪buildServerRequest(‪Scope $scope, ?string $languagePreset): ServerRequestInterface
130  {
131  $request = new ‪ServerRequest(
132  '/',
133  'GET',
134  null,
135  [],
136  [
137  'HTTPS' => 'on',
138  'HTTP_HOST' => 'website.fallback',
139  ]
140  );
141  if ($scope->siteIdentifier !== null) {
142  $site = $this->get(SiteFinder::class)->getSiteByIdentifier($scope->siteIdentifier);
143  $request = $request
144  ->withAttribute('site', $site)
145  ->withAttribute(
146  'siteLanguage',
147  $languagePreset !== null
148  ? $site->getLanguageById($this->resolveLanguagePreset($languagePreset)['id'])
149  : $site->getDefaultLanguage()
150  );
151  }
152  return $request;
153  }
154 }
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildLanguageConfiguration
‪buildLanguageConfiguration(string $identifier, string $base, array $fallbackIdentifiers=[], string $fallbackType=null)
Definition: SiteBasedTestTrait.php:116
‪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:128
‪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:96
‪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:32
‪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:120
‪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:30
‪TYPO3\CMS\Core\Tests\Functional\Security\ContentSecurityPolicy\PolicyProviderTest\$initializeDatabase
‪bool $initializeDatabase
Definition: PolicyProviderTest.php:37
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:106
‪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:40
‪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