‪TYPO3CMS  10.4
SlugServiceTest.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\Log\NullLogger;
32 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
33 
37 class ‪SlugServiceTest extends FunctionalTestCase
38 {
42  private ‪$subject;
43 
48 
49  private ‪$languages = [
50  [
51  'title' => 'English',
52  'enabled' => true,
53  'languageId' => '0',
54  'base' => '/en/',
55  'typo3Language' => 'default',
56  'locale' => 'en_US.UTF-8',
57  'iso-639-1' => 'en',
58  'navigationTitle' => 'English',
59  'hreflang' => 'en-us',
60  'direction' => 'ltr',
61  'flag' => 'us',
62  ],
63  [
64  'title' => 'German',
65  'enabled' => true,
66  'languageId' => '1',
67  'base' => 'https://de.example.com/',
68  'typo3Language' => 'de',
69  'locale' => 'de_DE.UTF-8',
70  'iso-639-1' => 'de',
71  'navigationTitle' => 'German',
72  'hreflang' => 'de-de',
73  'direction' => 'ltr',
74  'flag' => 'de',
75  ],
76  [
77  'title' => 'Spanish',
78  'enabled' => true,
79  'languageId' => '2',
80  'base' => '/es/',
81  'typo3Language' => 'es',
82  'locale' => 'es_ES.UTF-8',
83  'iso-639-1' => 'es',
84  'navigationTitle' => 'Spanish',
85  'hreflang' => 'es-es',
86  'direction' => 'ltr',
87  'flag' => 'es',
88  ],
89  ];
90 
91  protected ‪$coreExtensionsToLoad = ['redirects'];
92 
93  protected function ‪setUp(): void
94  {
95  parent::setUp();
96  $this->correlationId = ‪CorrelationId::forScope(‪StringUtility::getUniqueId('test'));
97  $this->setUpBackendUserFromFixture(1);
98  }
99 
100  protected function ‪tearDown(): void
101  {
102  unset($this->subject, $this->correlationId);
103  parent::tearDown();
104  }
105 
114  {
115  $this->‪buildBaseSite();
116  $this->‪createSubject();
117  $this->importDataSet(__DIR__ . '/Fixtures/SlugServiceTest_pages_test1.xml');
118  $this->subject->rebuildSlugsForSlugChange(2, '/dummy-1-2', '/test-new', $this->correlationId);
119 
120  // These are the slugs after rebuildSlugsForSlugChange() has run
121  $slugs = [
122  '/',
123  '/test-new',
124  '/dummy-1-3',
125  '/dummy-1-4',
126  '/test-new/dummy-1-2-5',
127  '/test-new/dummy-1-2-6',
128  '/test-new/dummy-1-2-7',
129  '/dummy-1-3/dummy-1-3-8',
130  '/dummy-1-3/dummy-1-3-9',
131  '/dummy-1-4/dummy-1-4-10',
132  ];
133 
134  // This redirects should exists, after rebuildSlugsForSlugChange() has run
135  $redirects = [
136  ['source_host' => '*', 'source_path' => '/dummy-1-2', 'target' => 't3://page?uid=2&_language=0'],
137  ['source_host' => '*', 'source_path' => '/dummy-1-2/dummy-1-2-5', 'target' => 't3://page?uid=5&_language=0'],
138  ['source_host' => '*', 'source_path' => '/dummy-1-2/dummy-1-2-6', 'target' => 't3://page?uid=6&_language=0'],
139  ['source_host' => '*', 'source_path' => '/dummy-1-2/dummy-1-2-7', 'target' => 't3://page?uid=7&_language=0'],
140  ];
141 
142  $this->‪assertSlugsAndRedirectsExists($slugs, $redirects);
143  }
144 
153  {
154  $this->‪buildBaseSite();
155  $this->‪createSubject();
156  $this->importDataSet(__DIR__ . '/Fixtures/SlugServiceTest_pages_test2.xml');
157  $this->subject->rebuildSlugsForSlugChange(1, '/', '/new-home', $this->correlationId);
158 
159  // These are the slugs after rebuildSlugsForSlugChange() has run
160  $slugs = [
161  '/new-home',
162  '/new-home/dummy-1-2',
163  '/new-home/dummy-1-3',
164  '/new-home/dummy-1-4',
165  '/new-home/dummy-1-2/dummy-1-2-5',
166  '/new-home/dummy-1-2/dummy-1-2-6',
167  '/new-home/dummy-1-2/dummy-1-2-7',
168  '/new-home/dummy-1-3/dummy-1-3-8',
169  '/new-home/dummy-1-3/dummy-1-3-9',
170  '/new-home/dummy-1-4/dummy-1-4-10',
171  ];
172 
173  // This redirects should exists, after rebuildSlugsForSlugChange() has run
174  $redirects = [
175  ['source_host' => '*', 'source_path' => '/', 'target' => 't3://page?uid=1&_language=0'],
176  ['source_host' => '*', 'source_path' => '/dummy-1-2', 'target' => 't3://page?uid=2&_language=0'],
177  ['source_host' => '*', 'source_path' => '/dummy-1-3', 'target' => 't3://page?uid=3&_language=0'],
178  ['source_host' => '*', 'source_path' => '/dummy-1-4', 'target' => 't3://page?uid=4&_language=0'],
179  ['source_host' => '*', 'source_path' => '/dummy-1-2/dummy-1-2-5', 'target' => 't3://page?uid=5&_language=0'],
180  ['source_host' => '*', 'source_path' => '/dummy-1-2/dummy-1-2-6', 'target' => 't3://page?uid=6&_language=0'],
181  ['source_host' => '*', 'source_path' => '/dummy-1-2/dummy-1-2-7', 'target' => 't3://page?uid=7&_language=0'],
182  ['source_host' => '*', 'source_path' => '/dummy-1-3/dummy-1-3-8', 'target' => 't3://page?uid=8&_language=0'],
183  ['source_host' => '*', 'source_path' => '/dummy-1-3/dummy-1-3-9', 'target' => 't3://page?uid=9&_language=0'],
184  ['source_host' => '*', 'source_path' => '/dummy-1-4/dummy-1-4-10', 'target' => 't3://page?uid=10&_language=0'],
185  ];
186 
187  $this->‪assertSlugsAndRedirectsExists($slugs, $redirects);
188  }
189 
198  {
200  $this->‪createSubject();
201  $this->importDataSet(__DIR__ . '/Fixtures/SlugServiceTest_pages_test1.xml');
202  $this->subject->rebuildSlugsForSlugChange(2, '/dummy-1-2', '/test-new', $this->correlationId);
203 
204  // These are the slugs after rebuildSlugsForSlugChange() has run
205  $slugs = [
206  '/',
207  '/test-new',
208  '/dummy-1-3',
209  '/dummy-1-4',
210  '/test-new/dummy-1-2-5',
211  '/test-new/dummy-1-2-6',
212  '/test-new/dummy-1-2-7',
213  '/dummy-1-3/dummy-1-3-8',
214  '/dummy-1-3/dummy-1-3-9',
215  '/dummy-1-4/dummy-1-4-10',
216  ];
217 
218  // This redirects should exists, after rebuildSlugsForSlugChange() has run
219  $redirects = [
220  ['source_host' => '*', 'source_path' => '/sub-folder/dummy-1-2', 'target' => 't3://page?uid=2&_language=0'],
221  ['source_host' => '*', 'source_path' => '/sub-folder/dummy-1-2/dummy-1-2-5', 'target' => 't3://page?uid=5&_language=0'],
222  ['source_host' => '*', 'source_path' => '/sub-folder/dummy-1-2/dummy-1-2-6', 'target' => 't3://page?uid=6&_language=0'],
223  ['source_host' => '*', 'source_path' => '/sub-folder/dummy-1-2/dummy-1-2-7', 'target' => 't3://page?uid=7&_language=0'],
224  ];
225 
226  $this->‪assertSlugsAndRedirectsExists($slugs, $redirects);
227  }
228 
237  {
239  $this->‪createSubject();
240  $this->importDataSet(__DIR__ . '/Fixtures/SlugServiceTest_pages_test3.xml');
241  $this->subject->rebuildSlugsForSlugChange(31, '/dummy-1-3', '/test-new', $this->correlationId);
242 
243  // These are the slugs after rebuildSlugsForSlugChange() has run
244  $slugs = [
245  '/',
246  '/dummy-1-2',
247  '/test-new',
248  '/dummy-1-3',
249  '/dummy-1-4',
250  '/dummy-1-2/dummy-1-2-5',
251  '/dummy-1-2/dummy-1-2-6',
252  '/dummy-1-2/dummy-1-2-7',
253  '/dummy-1-3/dummy-1-3-8',
254  '/test-new/dummy-1-3-8',
255  '/dummy-1-3/dummy-1-3-9',
256  '/dummy-1-4/dummy-1-4-10',
257  ];
258 
259  // This redirects should exists, after rebuildSlugsForSlugChange() has run
260  $redirects = [
261  ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-3', 'target' => 't3://page?uid=3&_language=1'],
262  ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-3/dummy-1-3-8', 'target' => 't3://page?uid=8&_language=1'],
263  ];
264 
265  $this->‪assertSlugsAndRedirectsExists($slugs, $redirects);
266  }
267 
276  {
278  $this->‪createSubject();
279  $this->importDataSet(__DIR__ . '/Fixtures/SlugServiceTest_pages_test3.xml');
280  $this->subject->rebuildSlugsForSlugChange(31, '/dummy-1-3', '/test-new', $this->correlationId);
281 
282  // These are the slugs after rebuildSlugsForSlugChange() has run
283  $slugs = [
284  '/',
285  '/dummy-1-2',
286  '/test-new',
287  '/dummy-1-3',
288  '/dummy-1-4',
289  '/dummy-1-2/dummy-1-2-5',
290  '/dummy-1-2/dummy-1-2-6',
291  '/dummy-1-2/dummy-1-2-7',
292  '/dummy-1-3/dummy-1-3-8',
293  '/test-new/dummy-1-3-8',
294  '/dummy-1-3/dummy-1-3-9',
295  '/dummy-1-4/dummy-1-4-10',
296  ];
297 
298  // This redirects should exists, after rebuildSlugsForSlugChange() has run
299  $redirects = [
300  ['source_host' => 'de.example.com', 'source_path' => '/sub-folder/dummy-1-3', 'target' => 't3://page?uid=3&_language=1'],
301  ['source_host' => 'de.example.com', 'source_path' => '/sub-folder/dummy-1-3/dummy-1-3-8', 'target' => 't3://page?uid=8&_language=1'],
302  ];
303 
304  $this->‪assertSlugsAndRedirectsExists($slugs, $redirects);
305  }
306 
315  {
317  $this->‪createSubject();
318  $this->importDataSet(__DIR__ . '/Fixtures/SlugServiceTest_pages_test3.xml');
319  $this->subject->rebuildSlugsForSlugChange(3, '/dummy-1-3', '/test-new', $this->correlationId);
320 
321  // These are the slugs after rebuildSlugsForSlugChange() has run
322  $slugs = [
323  '/',
324  '/dummy-1-2',
325  '/test-new',
326  '/dummy-1-3',
327  '/dummy-1-4',
328  '/dummy-1-2/dummy-1-2-5',
329  '/dummy-1-2/dummy-1-2-6',
330  '/dummy-1-2/dummy-1-2-7',
331  '/dummy-1-3/dummy-1-3-8',
332  '/test-new/dummy-1-3-8',
333  '/test-new/dummy-1-3-9',
334  '/dummy-1-4/dummy-1-4-10',
335  ];
336 
337  // This redirects should exists, after rebuildSlugsForSlugChange() has run
338  $redirects = [
339  ['source_host' => '*', 'source_path' => '/sub-folder/en/dummy-1-3', 'target' => 't3://page?uid=3&_language=0'],
340  ['source_host' => '*', 'source_path' => '/sub-folder/en/dummy-1-3/dummy-1-3-8', 'target' => 't3://page?uid=8&_language=0'],
341  ['source_host' => '*', 'source_path' => '/sub-folder/en/dummy-1-3/dummy-1-3-9', 'target' => 't3://page?uid=9&_language=0'],
342  ];
343 
344  $this->‪assertSlugsAndRedirectsExists($slugs, $redirects);
345  }
346 
355  {
357  $this->‪createSubject();
358  $this->importDataSet(__DIR__ . '/Fixtures/SlugServiceTest_pages_test4.xml');
359  $this->subject->rebuildSlugsForSlugChange(5, '/', '/test-new', $this->correlationId);
360 
361  // These are the slugs after rebuildSlugsForSlugChange() has run
362  $slugs = [
363  '/',
364  '/dummy-1-2',
365  '/dummy-1-3',
366  '/dummy-1-2/dummy-1-2-3',
367  '/test-new',
368  '/test-new/dummy-1-2',
369  '/test-new/dummy-1-3',
370  '/test-new/dummy-1-2/dummy-1-2-3'
371  ];
372 
373  // This redirects should exists, after rebuildSlugsForSlugChange() has run
374  $redirects = [
375  ['source_host' => 'de.example.com', 'source_path' => '/', 'target' => 't3://page?uid=1&_language=1'],
376  ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-2', 'target' => 't3://page?uid=2&_language=1'],
377  ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-3', 'target' => 't3://page?uid=3&_language=1'],
378  ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-2/dummy-1-2-3', 'target' => 't3://page?uid=4&_language=1'],
379  ];
380 
381  $this->‪assertSlugsAndRedirectsExists($slugs, $redirects);
382  }
383 
384  protected function ‪buildBaseSite(): void
385  {
386  $configuration = [
387  'rootPageId' => 1,
388  'base' => '/',
389  ];
390  $siteConfiguration = GeneralUtility::makeInstance(SiteConfiguration::class);
391  $siteConfiguration->write('testing', $configuration);
392  }
393 
394  protected function ‪buildBaseSiteInSubfolder(): void
395  {
396  $configuration = [
397  'rootPageId' => 1,
398  'base' => '/sub-folder',
399  ];
400  $siteConfiguration = GeneralUtility::makeInstance(SiteConfiguration::class);
401  $siteConfiguration->write('testing', $configuration);
402  }
403 
404  protected function ‪buildBaseSiteWithLanguages(): void
405  {
406  $configuration = [
407  'rootPageId' => 1,
408  'base' => '/',
409  'languages' => ‪$this->languages,
410  ];
411  $siteConfiguration = GeneralUtility::makeInstance(SiteConfiguration::class);
412  $siteConfiguration->write('testing', $configuration);
413  }
414 
415  protected function ‪buildBaseSiteWithLanguagesInSubFolder(): void
416  {
418  array_walk(‪$languages, static function (&$languageData) {
419  $languageData['base'] = (
420  strpos($languageData['base'], 'http') === false
421  ? $languageData['base']
422  : $languageData['base'] . 'sub-folder/'
423  );
424  });
425  $configuration = [
426  'rootPageId' => 1,
427  'base' => '/sub-folder',
428  'languages' => ‪$languages,
429  ];
430  $siteConfiguration = GeneralUtility::makeInstance(SiteConfiguration::class);
431  $siteConfiguration->write('testing', $configuration);
432  }
433 
434  protected function ‪createSubject(): void
435  {
436  GeneralUtility::makeInstance(SiteMatcher::class)->refresh();
437  $this->subject = new ‪SlugService(
438  GeneralUtility::makeInstance(Context::class),
439  GeneralUtility::makeInstance(LanguageService::class),
440  GeneralUtility::makeInstance(SiteFinder::class),
441  GeneralUtility::makeInstance(PageRepository::class),
442  GeneralUtility::makeInstance(LinkService::class)
443  );
444  $this->subject->setLogger(new NullLogger());
445  }
446 
447  protected function ‪assertSlugsAndRedirectsExists(array $slugs, array $redirects): void
448  {
449  $pageRecords = $this->getAllRecords('pages');
450  self::assertCount(count($slugs), $pageRecords);
451  foreach ($pageRecords as $record) {
452  self::assertContains($record['slug'], $slugs, 'unexpected slug: ' . $record['slug']);
453  }
454 
455  $redirectRecords = $this->getAllRecords('sys_redirect');
456  self::assertCount(count($redirects), $redirectRecords);
457  foreach ($redirectRecords as $record) {
458  $combination = [
459  'source_host' => $record['source_host'],
460  'source_path' => $record['source_path'],
461  'target' => $record['target'],
462  ];
463  self::assertContains($combination, $redirects, 'wrong redirect found');
464  }
465  }
466 }
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\$subject
‪SlugService $subject
Definition: SlugServiceTest.php:41
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirectsWithLanguagesForSiteroot
‪rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirectsWithLanguagesForSiteroot()
Definition: SlugServiceTest.php:352
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirects
‪rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirects()
Definition: SlugServiceTest.php:111
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\setUp
‪setUp()
Definition: SlugServiceTest.php:91
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirectsForRootChange
‪rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirectsForRootChange()
Definition: SlugServiceTest.php:150
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\buildBaseSite
‪buildBaseSite()
Definition: SlugServiceTest.php:382
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirectsWithLanguagesInSubFolder
‪rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirectsWithLanguagesInSubFolder()
Definition: SlugServiceTest.php:273
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\$languages
‪$languages
Definition: SlugServiceTest.php:47
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\buildBaseSiteWithLanguages
‪buildBaseSiteWithLanguages()
Definition: SlugServiceTest.php:402
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirectsWithDefaultLanguageInSubFolder
‪rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirectsWithDefaultLanguageInSubFolder()
Definition: SlugServiceTest.php:312
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:41
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirectsWithLanguages
‪rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirectsWithLanguages()
Definition: SlugServiceTest.php:234
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\assertSlugsAndRedirectsExists
‪assertSlugsAndRedirectsExists(array $slugs, array $redirects)
Definition: SlugServiceTest.php:445
‪TYPO3\CMS\Redirects\Tests\Functional\Service
Definition: IntegrityServiceTest.php:18
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\tearDown
‪tearDown()
Definition: SlugServiceTest.php:98
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirectsWithSubFolderBase
‪rebuildSlugsForSlugChangeRenamesSubSlugsAndCreatesRedirectsWithSubFolderBase()
Definition: SlugServiceTest.php:195
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\buildBaseSiteInSubfolder
‪buildBaseSiteInSubfolder()
Definition: SlugServiceTest.php:392
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId
Definition: CorrelationId.php:29
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\buildBaseSiteWithLanguagesInSubFolder
‪buildBaseSiteWithLanguagesInSubFolder()
Definition: SlugServiceTest.php:413
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest
Definition: SlugServiceTest.php:38
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\$coreExtensionsToLoad
‪$coreExtensionsToLoad
Definition: SlugServiceTest.php:89
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\$correlationId
‪CorrelationId $correlationId
Definition: SlugServiceTest.php:45
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Redirects\Service\SlugService
Definition: SlugService.php:48
‪TYPO3\CMS\Redirects\Tests\Functional\Service\SlugServiceTest\createSubject
‪createSubject()
Definition: SlugServiceTest.php:432
‪TYPO3\CMS\Core\Routing\SiteMatcher
Definition: SiteMatcher.php:51
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\forScope
‪static static forScope(string $scope)
Definition: CorrelationId.php:57