‪TYPO3CMS  11.5
PersistedAliasMapperTest.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 
29 use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Scenario\DataHandlerFactory;
30 use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Scenario\DataHandlerWriter;
31 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
32 
33 class ‪PersistedAliasMapperTest extends FunctionalTestCase
34 {
35  private const ‪ASPECT_CONFIGURATION = [
36  'tableName' => 'tt_content',
37  'routeFieldName' => 'header',
38  ];
39 
40  private const ‪SLUG_CONFIGURATION = [
41  'type' => 'slug',
42  'generatorOptions' => [
43  'prefixParentPageSlug' => false,
44  ],
45  'fallbackCharacter' => '-',
46  'eval' => 'required,uniqueInSite',
47  'default' => '',
48  ];
49 
50  private const ‪LANGUAGE_MAP = [
51  'es-es' => 3,
52  'fr-ca' => 2,
53  'fr-fr' => 1,
54  'default' => 0,
55  ];
56 
57  private const ‪SITE_ADDITION = [
58  'acme' => 0,
59  'other' => 4000,
60  ];
61 
65  private ‪$subject;
66 
70  private ‪$sites;
71 
72  protected function ‪setUp(): void
73  {
74  parent::setUp();
75  $this->withDatabaseSnapshot(function () {
76  $this->‪setUpDatabase();
77  });
78 
79  // declare tt_content.header as `slug` field having `uniqueInSite` set
80  $tableName = self::ASPECT_CONFIGURATION['tableName'];
81  $fieldName = self::ASPECT_CONFIGURATION['routeFieldName'];
82  ‪$GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'] = ‪self::SLUG_CONFIGURATION;
83 
84  $languages = [
85  [
86  'languageId' => 3,
87  'base' => '/es-es/',
88  'locale' => 'es_ES.UTF-8',
89  'fallbackType' => 'fallback',
90  'fallbacks' => [0],
91  'title' => 'Spanish',
92  ],
93  [
94  'languageId' => 2,
95  'base' => '/fr-ca/',
96  'locale' => 'fr_CA.UTF-8',
97  'fallbackType' => 'fallback',
98  'fallbacks' => [1, 0],
99  'title' => 'Franco-Canadian',
100  ],
101  [
102  'languageId' => 1,
103  'base' => '/fr-fr/',
104  'locale' => 'fr_FR.UTF-8',
105  'fallbackType' => 'fallback',
106  'fallbacks' => [0],
107  'French',
108  ],
109  [
110  'languageId' => 0,
111  'base' => 'en_US.UTF-8',
112  'locale' => '/en-us/',
113  ],
114  ];
115  $this->sites = [
116  'acme' => new ‪Site('acme-inc', 1000, [
117  'identifier' => 'acme-inc',
118  'rootPageId' => 1000,
119  'base' => 'https://acme.com/',
120  'languages' => $languages,
121  ]),
122  'other' => new ‪Site('other-inc', 5000, [
123  'identifier' => 'other-inc',
124  'rootPageId' => 5000,
125  'base' => 'https://other.com/',
126  'languages' => $languages,
127  ]),
128  ];
129  $this->‪writeSiteConfiguration($this->sites['acme']);
130  $this->‪writeSiteConfiguration($this->sites['other']);
131  $this->subject = new ‪PersistedAliasMapper(self::ASPECT_CONFIGURATION);
132  $this->subject->setSiteLanguage($this->sites['acme']->getLanguageById(0));
133  $this->subject->setSite($this->sites['acme']);
134  }
135 
136  protected function ‪setUpDatabase(): void
137  {
138  $backendUser = $this->setUpBackendUserFromFixture(1);
140 
141  $scenarioFile = __DIR__ . '/Fixtures/AspectScenario.yaml';
142  $factory = DataHandlerFactory::fromYamlFile($scenarioFile);
143  $writer = DataHandlerWriter::withBackendUser($backendUser);
144  $writer->invokeFactory($factory);
145  if (!empty($writer->getErrors())) {
146  self::fail(var_export($writer->getErrors(), true));
147  }
148  }
149 
150  protected function ‪tearDown(): void
151  {
152  unset($this->subject, $this->sites);
153  parent::tearDown();
154  }
155 
156  public function ‪languageAwareRecordsAreResolvedDataProvider(): array
157  {
158  $baseDataSet = [
159  'non-existing, default language' => ['this-value-does-not-exist', 'default', null],
160 
161  '30xx-slug, default language' => ['30xx-slug', 'default', '3010'],
162  '30xx-slug, fr-fr language' => ['30xx-slug', 'fr-fr', '3010'],
163  '30xx-slug, fr-ca language' => ['30xx-slug', 'fr-ca', '3010'],
164 
165  '30xx-slug-fr-ca, fr-ca language' => ['30xx-slug-fr-ca', 'fr-ca', '3010'],
166  // '30xx-slug-fr-ca' available in default language as well, fallbacks to that one
167  '30xx-slug-fr-ca, fr-fr language' => ['30xx-slug-fr-ca', 'fr-fr', '3030'],
168  // '30xx-slug-fr-ca' available in default language, use it directly
169  '30xx-slug-fr-ca, default language' => ['30xx-slug-fr-ca', 'default', '3030'],
170 
171  '30xx-slug-fr, fr-ca language' => ['30xx-slug-fr', 'fr-ca', '3010'],
172  '30xx-slug-fr, fr-fr language' => ['30xx-slug-fr', 'fr-fr', '3010'],
173  // '30xx-slug-fr-ca' available in default language, use it directly
174  '30xx-slug-fr, default language' => ['30xx-slug-fr', 'default', '3020'],
175 
176  // basically the same, but being stored in reverse order in database
177  '40xx-slug, default language' => ['40xx-slug', 'default', '4040'],
178  '40xx-slug, fr-fr language' => ['40xx-slug', 'fr-fr', '4040'],
179  '40xx-slug, fr-ca language' => ['40xx-slug', 'fr-ca', '4040'],
180 
181  '40xx-slug-fr-ca, fr-ca language' => ['40xx-slug-fr-ca', 'fr-ca', '4040'],
182  // '40xx-slug-fr-ca' available in default language as well, fallbacks to that one
183  '40xx-slug-fr-ca, fr-fr language' => ['40xx-slug-fr-ca', 'fr-fr', '4030'],
184  // '40xx-slug-fr-ca' available in default language, use it directly
185  '40xx-slug-fr-ca, default language' => ['40xx-slug-fr-ca', 'default', '4030'],
186 
187  '40xx-slug-fr, fr-ca language' => ['40xx-slug-fr', 'fr-ca', '4040'],
188  '40xx-slug-fr, fr-fr language' => ['40xx-slug-fr', 'fr-fr', '4040'],
189  // '40xx-slug-fr-ca' available in default language, use it directly
190  '40xx-slug-fr, default language' => ['40xx-slug-fr', 'default', '4020'],
191  ];
192  // permute $baseDataSet to be either prepended
193  // with site identifier argument 'acme' or 'other'
194  $dataSet = [];
195  foreach (['acme', 'other'] as $site) {
196  foreach ($baseDataSet as $key => $arguments) {
197  array_unshift($arguments, $site);
198  $dataSet[$site . ':' . $key] = $arguments;
199  }
200  }
201  return $dataSet;
202  }
203 
213  public function ‪languageAwareRecordsAreResolved(string $identifier, string $requestValue, string $language, ?string $expectation): void
214  {
215  $this->subject->setSiteLanguage(
216  $this->sites[$identifier]->getLanguageById(self::LANGUAGE_MAP[$language])
217  );
218  $this->subject->setSite(
219  $this->sites[$identifier]
220  );
221  if ($expectation !== null) {
222  $expectation += self::SITE_ADDITION[$identifier];
223  $expectation = (string)$expectation;
224  }
225  self::assertSame($expectation, $this->subject->resolve($requestValue));
226  }
227 
228  public function ‪recordVisibilityDataProvider(): array
229  {
230  $rawContext = new ‪Context();
231  $visibleContext = new ‪Context();
232  $visibleContext->setAspect(
233  'visibility',
234  new ‪VisibilityAspect(false, true, false)
235  );
236  $frontendGroupsContext = new ‪Context();
237  $frontendGroupsContext->setAspect(
238  'frontend.user',
239  new ‪UserAspect(null, [13])
240  );
241  $scheduledContext = new ‪Context();
242  $scheduledContext->setAspect(
243  'date',
244  new ‪DateTimeAspect(new \DateTimeImmutable('@20000'))
245  );
246 
247  return [
248  'hidden-visibility-slug, raw context' => [
249  $rawContext,
250  ['slug' => 'hidden-visibility-slug', 'uid' => '4051'],
251  false,
252  ],
253  // fe_group slugs are always considered
254  'restricted-visibility-slug, raw context' => [
255  $rawContext,
256  ['slug' => 'restricted-visibility-slug', 'uid' => '4052'],
257  true,
258  ],
259  'scheduled-visibility-slug, raw context' => [
260  $rawContext,
261  ['slug' => 'scheduled-visibility-slug', 'uid' => '4053'],
262  false,
263  ],
264  'hidden-visibility-slug, visibility context (include hidden content)' => [
265  $visibleContext,
266  ['slug' => 'hidden-visibility-slug', 'uid' => '4051'],
267  true,
268  ],
269  // fe_group slugs are always considered
270  'restricted-visibility-slug, frontend-groups context (13)' => [
271  $frontendGroupsContext,
272  ['slug' => 'restricted-visibility-slug', 'uid' => '4052'],
273  true,
274  ],
275  'scheduled-visibility-slug, scheduled context (timestamp 20000)' => [
276  $scheduledContext,
277  ['slug' => 'scheduled-visibility-slug', 'uid' => '4053'],
278  false, // @todo actually `true`, Start-/EndTimeRestriction do not support Context, yet
279  ],
280  ];
281  }
282 
291  public function ‪recordVisibilityIsConsideredForResolving(Context $context, array $parameters, bool $expectation): void
292  {
293  $this->subject->setContext($context);
294  $expectedResult = $expectation ? $parameters['uid'] : null;
295  self::assertSame($expectedResult, $this->subject->resolve($parameters['slug']));
296  }
297 
306  public function ‪recordVisibilityIsConsideredForGeneration(Context $context, array $parameters, bool $expectation): void
307  {
308  $this->subject->setContext($context);
309  $expectedResult = $expectation ? $parameters['slug'] : null;
310  self::assertSame($expectedResult, $this->subject->generate($parameters['uid']));
311  }
312 
313  private function ‪writeSiteConfiguration(‪Site $site): void
314  {
315  try {
316  // ensure no previous site configuration influences the test
317  $path = $this->instancePath . '/typo3conf/sites';
318  $cache = $this->get('cache.core');
319  ‪GeneralUtility::rmdir($path . '/' . $site->‪getIdentifier(), true);
320  GeneralUtility::makeInstance(SiteConfiguration::class, $path, $cache)->write($site->‪getIdentifier(), $site->‪getConfiguration());
321  } catch (\‪Exception $exception) {
322  self::markTestSkipped($exception->getMessage());
323  }
324  }
325 }
‪TYPO3\CMS\Core\Context\VisibilityAspect
Definition: VisibilityAspect.php:31
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\recordVisibilityIsConsideredForGeneration
‪recordVisibilityIsConsideredForGeneration(Context $context, array $parameters, bool $expectation)
Definition: PersistedAliasMapperTest.php:304
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\LANGUAGE_MAP
‪const LANGUAGE_MAP
Definition: PersistedAliasMapperTest.php:50
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\tearDown
‪tearDown()
Definition: PersistedAliasMapperTest.php:148
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\SLUG_CONFIGURATION
‪const SLUG_CONFIGURATION
Definition: PersistedAliasMapperTest.php:40
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\recordVisibilityDataProvider
‪recordVisibilityDataProvider()
Definition: PersistedAliasMapperTest.php:226
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect
Definition: PersistedAliasMapperTest.php:18
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:43
‪TYPO3\CMS\Core\Site\Entity\Site\getConfiguration
‪array getConfiguration()
Definition: Site.php:314
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\languageAwareRecordsAreResolvedDataProvider
‪languageAwareRecordsAreResolvedDataProvider()
Definition: PersistedAliasMapperTest.php:154
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\$sites
‪Site[] $sites
Definition: PersistedAliasMapperTest.php:68
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\setUpDatabase
‪setUpDatabase()
Definition: PersistedAliasMapperTest.php:134
‪TYPO3\CMS\Core\Site\Entity\Site\getIdentifier
‪string getIdentifier()
Definition: Site.php:179
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\languageAwareRecordsAreResolved
‪languageAwareRecordsAreResolved(string $identifier, string $requestValue, string $language, ?string $expectation)
Definition: PersistedAliasMapperTest.php:211
‪TYPO3\CMS\Core\Core\Bootstrap\initializeLanguageObject
‪static initializeLanguageObject()
Definition: Bootstrap.php:598
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\SITE_ADDITION
‪const SITE_ADDITION
Definition: PersistedAliasMapperTest.php:57
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\writeSiteConfiguration
‪writeSiteConfiguration(Site $site)
Definition: PersistedAliasMapperTest.php:311
‪TYPO3\CMS\Core\Routing\Aspect\PersistedAliasMapper
Definition: PersistedAliasMapper.php:53
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\$subject
‪PersistedAliasMapper $subject
Definition: PersistedAliasMapperTest.php:64
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest
Definition: PersistedAliasMapperTest.php:34
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:70
‪TYPO3\CMS\Core\Utility\GeneralUtility\rmdir
‪static bool rmdir($path, $removeNonEmpty=false)
Definition: GeneralUtility.php:1961
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\recordVisibilityIsConsideredForResolving
‪recordVisibilityIsConsideredForResolving(Context $context, array $parameters, bool $expectation)
Definition: PersistedAliasMapperTest.php:289
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\ASPECT_CONFIGURATION
‪const ASPECT_CONFIGURATION
Definition: PersistedAliasMapperTest.php:35
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Context\DateTimeAspect
Definition: DateTimeAspect.php:35
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:37
‪TYPO3\CMS\Core\Tests\Functional\Routing\Aspect\PersistedAliasMapperTest\setUp
‪setUp()
Definition: PersistedAliasMapperTest.php:70