‪TYPO3CMS  ‪main
IntegrityServiceTest.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\Test;
21 use PHPUnit\Framework\MockObject\MockObject;
29 use TYPO3\CMS\Core\TypoScript\FrontendTypoScriptFactory;
35 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
36 
37 final class ‪IntegrityServiceTest extends FunctionalTestCase
38 {
39  protected bool ‪$resetSingletonInstances = true;
40  protected array ‪$coreExtensionsToLoad = ['redirects'];
42 
43  protected function ‪setUp(): void
44  {
45  parent::setUp();
46  $siteFinder = $this->‪getSiteFinderMock();
48  $typoScriptCache = $this->get(CacheManager::class)->getCache('typoscript');
49  $this->subject = new ‪IntegrityService(
52  $this->getMockBuilder(LinkService::class)->disableOriginalConstructor()->getMock(),
53  $siteFinder,
55  $this->get(PageInformationFactory::class),
56  $this->get(FrontendTypoScriptFactory::class),
57  $typoScriptCache,
58  $this->get(LogManager::class)->getLogger('Testing'),
59  ),
60  $siteFinder
61  );
62  }
63 
70  #[Test]
72  {
73  $this->importCSVDataSet(__DIR__ . '/Fixtures/IntegrityServiceTest_sourcePathWithMatchingSlugInLocalizedPageIsNotReportedAsConflict.csv');
74 
75  $result = $this->subject->findConflictingRedirects('localized-page');
76  $this->‪assertExpectedPathsFromGenerator([], $result);
77  }
78 
79  #[Test]
81  {
82  $this->importCSVDataSet(__DIR__ . '/Fixtures/SimplePages.csv');
83  $this->importCSVDataSet(__DIR__ . '/Fixtures/sys_redirect.csv');
84 
85  $expectedConflicts = [
86  [
87  'uri' => 'https://example.com/',
88  'redirect' => [
89  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
90  'source_host' => 'example.com',
91  'source_path' => '/',
92  'uid' => 7,
93  ],
94  ],
95  [
96  'uri' => 'https://example.com/about-us/we-are-here',
97  'redirect' => [
98  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
99  'source_host' => '*',
100  'source_path' => '/about-us/we-are-here',
101  'uid' => 1,
102  ],
103  ],
104  [
105  'uri' => 'https://example.com/contact',
106  'redirect' => [
107  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
108  'source_host' => 'example.com',
109  'source_path' => '/contact',
110  'uid' => 6,
111  ],
112  ],
113  [
114  'uri' => 'https://example.com/features',
115  'redirect' => [
116  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
117  'source_host' => '*',
118  'source_path' => '/features',
119  'uid' => 9,
120  ],
121  ],
122  ];
123 
124  $this->‪assertExpectedPathsFromGenerator($expectedConflicts, $this->subject->findConflictingRedirects('simple-page'));
125  }
126 
127  #[Test]
129  {
130  $this->importCSVDataSet(__DIR__ . '/Fixtures/LocalizedPages.csv');
131  $this->importCSVDataSet(__DIR__ . '/Fixtures/sys_redirect.csv');
132 
133  $expectedConflicts = [
134  [
135  'uri' => 'https://another.example.com/about-us/we-are-here',
136  'redirect' => [
137  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
138  'source_host' => '*',
139  'source_path' => '/about-us/we-are-here',
140  'uid' => 1,
141  ],
142  ],
143  [
144  'uri' => 'https://another.example.com/de/merkmale',
145  'redirect' => [
146  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
147  'source_host' => 'another.example.com',
148  'source_path' => '/de/merkmale',
149  'uid' => 4,
150  ],
151  ],
152  [
153  'uri' => 'https://another.example.com/features',
154  'redirect' => [
155  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
156  'source_host' => 'another.example.com',
157  'source_path' => '/features',
158  'uid' => 8,
159  ],
160  ],
161  ];
162  $conflicts = $this->subject->findConflictingRedirects('localized-page');
163  $this->‪assertExpectedPathsFromGenerator($expectedConflicts, $conflicts);
164  }
165 
166  #[Test]
168  {
169  $this->importCSVDataSet(__DIR__ . '/Fixtures/SimplePages.csv');
170  $this->importCSVDataSet(__DIR__ . '/Fixtures/LocalizedPages.csv');
171  $this->importCSVDataSet(__DIR__ . '/Fixtures/sys_redirect.csv');
172 
173  $expectedConflicts = [
174  [
175  'uri' => 'https://example.com/',
176  'redirect' => [
177  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
178  'source_host' => 'example.com',
179  'source_path' => '/',
180  'uid' => 7,
181  ],
182  ],
183  [
184  'uri' => 'https://example.com/about-us/we-are-here',
185  'redirect' => [
186  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
187  'source_host' => '*',
188  'source_path' => '/about-us/we-are-here',
189  'uid' => 1,
190  ],
191  ],
192  [
193  'uri' => 'https://another.example.com/about-us/we-are-here',
194  'redirect' => [
195  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
196  'source_host' => '*',
197  'source_path' => '/about-us/we-are-here',
198  'uid' => 1,
199  ],
200  ],
201  [
202  'uri' => 'https://another.example.com/de/merkmale',
203  'redirect' => [
204  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
205  'source_host' => 'another.example.com',
206  'source_path' => '/de/merkmale',
207  'uid' => 4,
208  ],
209  ],
210  [
211  'uri' => 'https://example.com/contact',
212  'redirect' => [
213  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
214  'source_host' => 'example.com',
215  'source_path' => '/contact',
216  'uid' => 6,
217  ],
218  ],
219  [
220  'uri' => 'https://example.com/features',
221  'redirect' => [
222  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
223  'source_host' => '*',
224  'source_path' => '/features',
225  'uid' => 9,
226  ],
227  ],
228  [
229  'uri' => 'https://another.example.com/features',
230  'redirect' => [
231  'integrity_status' => ‪RedirectConflict::SELF_REFERENCE,
232  'source_host' => 'another.example.com',
233  'source_path' => '/features',
234  'uid' => 8,
235  ],
236  ],
237  ];
238 
239  $this->‪assertExpectedPathsFromGenerator($expectedConflicts, $this->subject->findConflictingRedirects());
240  }
241 
242  private function ‪assertExpectedPathsFromGenerator(array $expectedConflicts, \Generator $generator): void
243  {
244  $matches = 0;
245  foreach ($generator as $reportedConflict) {
246  self::assertContains($reportedConflict, $expectedConflicts);
247  $matches++;
248  }
249  self::assertSame(count($expectedConflicts), $matches);
250  }
251 
252  private function ‪getSiteFinderMock(): MockObject&‪SiteFinder
253  {
254  $siteFinderMock = $this->getMockBuilder(SiteFinder::class)->disableOriginalConstructor()->getMock();
255 
256  $simpleSite = new ‪Site('simple-page', 1, [
257  'base' => 'https://example.com',
258  'languages' => [
259  [
260  'languageId' => 0,
261  'title' => 'United States',
262  'locale' => 'en_US.UTF-8',
263  ],
264  ],
265  ]);
266  $localizedSite = new ‪Site('localized-page', 100, [
267  'base' => 'https://another.example.com',
268  'languages' => [
269  [
270  'languageId' => 0,
271  'title' => 'United States',
272  'locale' => 'en_US.UTF-8',
273  ],
274  [
275  'base' => '/de/',
276  'languageId' => 1,
277  'title' => 'DE',
278  'locale' => 'de_DE.UTF-8',
279  ],
280  [
281  'base' => '/fr/',
282  'languageId' => 2,
283  'title' => 'FR',
284  'locale' => 'fr_FR.UTF-8',
285  ],
286  ],
287  ]);
288 
289  $siteFinderMock->method('getSiteByIdentifier')->willReturnMap([
290  ['simple-page', $simpleSite],
291  ['localized-page', $localizedSite],
292  ]);
293 
294  $siteFinderMock->method('getAllSites')->willReturn([$simpleSite, $localizedSite]);
295 
296  return $siteFinderMock;
297  }
298 }
‪TYPO3\CMS\Redirects\Utility\RedirectConflict
Definition: RedirectConflict.php:24
‪TYPO3\CMS\Redirects\Tests\Functional\Service\IntegrityServiceTest\assertExpectedPathsFromGenerator
‪assertExpectedPathsFromGenerator(array $expectedConflicts, \Generator $generator)
Definition: IntegrityServiceTest.php:242
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:25
‪TYPO3\CMS\Redirects\Service\RedirectService
Definition: RedirectService.php:54
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Redirects\Tests\Functional\Service\IntegrityServiceTest
Definition: IntegrityServiceTest.php:38
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Redirects\Service\RedirectCacheService
Definition: RedirectCacheService.php:34
‪TYPO3\CMS\Redirects\Tests\Functional\Service\IntegrityServiceTest\sourcePathWithMatchingSlugInLocalizedPageIsNotReportedAsConflict
‪sourcePathWithMatchingSlugInLocalizedPageIsNotReportedAsConflict()
Definition: IntegrityServiceTest.php:71
‪TYPO3\CMS\Redirects\Tests\Functional\Service
Definition: IntegrityServiceTest.php:18
‪TYPO3\CMS\Redirects\Tests\Functional\Service\IntegrityServiceTest\conflictingRedirectsAreFoundForDefinedSiteOnly
‪conflictingRedirectsAreFoundForDefinedSiteOnly()
Definition: IntegrityServiceTest.php:80
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Redirects\Tests\Functional\Service\IntegrityServiceTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: IntegrityServiceTest.php:39
‪TYPO3\CMS\Redirects\Tests\Functional\Service\IntegrityServiceTest\conflictingRedirectsAreFoundForLocalizedPages
‪conflictingRedirectsAreFoundForLocalizedPages()
Definition: IntegrityServiceTest.php:128
‪TYPO3\CMS\Redirects\Tests\Functional\Service\IntegrityServiceTest\getSiteFinderMock
‪getSiteFinderMock()
Definition: IntegrityServiceTest.php:252
‪TYPO3\CMS\Redirects\Tests\Functional\Service\IntegrityServiceTest\$coreExtensionsToLoad
‪array $coreExtensionsToLoad
Definition: IntegrityServiceTest.php:40
‪TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher
Definition: NoopEventDispatcher.php:29
‪TYPO3\CMS\Redirects\Tests\Functional\Service\IntegrityServiceTest\$subject
‪IntegrityService $subject
Definition: IntegrityServiceTest.php:41
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:33
‪TYPO3\CMS\Redirects\Service\IntegrityService
Definition: IntegrityService.php:33
‪TYPO3\CMS\Redirects\Tests\Functional\Service\IntegrityServiceTest\conflictingRedirectsAreFoundForAllSites
‪conflictingRedirectsAreFoundForAllSites()
Definition: IntegrityServiceTest.php:167
‪TYPO3\CMS\Redirects\Tests\Functional\Service\IntegrityServiceTest\setUp
‪setUp()
Definition: IntegrityServiceTest.php:43
‪TYPO3\CMS\Redirects\Utility\RedirectConflict\SELF_REFERENCE
‪const SELF_REFERENCE
Definition: RedirectConflict.php:26
‪TYPO3\CMS\Frontend\Page\PageInformationFactory
Definition: PageInformationFactory.php:63