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