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