‪TYPO3CMS  11.5
RedirectRepositoryTest.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 
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 
26 class ‪RedirectRepositoryTest extends FunctionalTestCase
27 {
28  protected ‪$coreExtensionsToLoad = ['redirects'];
29 
30  public function ‪demandProvider(): array
31  {
32  $allRecordCount = 6;
33  return [
34  'default demand' => [$this->‪getDemand(), $allRecordCount, $allRecordCount - 4],
35  'configuration with hitCount' => [$this->‪getDemand(2), $allRecordCount, $allRecordCount - 3],
36  'configuration with statusCode 302' => [
37  $this->‪getDemand(0, [302]),
38  $allRecordCount,
39  $allRecordCount - 1,
40  ],
41  'demand with statusCode 302, 303' => [
42  $this->‪getDemand(0, [302, 303]),
43  $allRecordCount,
44  $allRecordCount - 2,
45  ],
46  'demand with domain' => [
47  $this->‪getDemand(0, [], ['foo.com']),
48  $allRecordCount,
49  $allRecordCount - 2,
50  ],
51  'demand with domains' => [
52  $this->‪getDemand(0, [], ['foo.com', 'bar.com']),
53  $allRecordCount,
54  $allRecordCount - 3,
55  ],
56  'demand with path' => [
57  $this->‪getDemand(0, [], [], '/foo'),
58  $allRecordCount,
59  $allRecordCount - 1,
60  ],
61  'demand with path starts with' => [
62  $this->‪getDemand(0, [], [], '/foo%'),
63  $allRecordCount,
64  $allRecordCount - 3,
65  ],
66  'demand with path ends with' => [
67  $this->‪getDemand(0, [], [], '%/foo'),
68  $allRecordCount,
69  $allRecordCount - 1,
70  ],
71  'demand with path in the middle' => [
72  $this->‪getDemand(0, [], [], '%foo%'),
73  $allRecordCount,
74  $allRecordCount - 3,
75  ],
76  ];
77  }
78 
86  public function ‪removeByDemandWorks(
87  ‪Demand $demand,
88  int $redirectBeforeCleanup,
89  int $redirectAfterCleanup
90  ): void {
91  self::assertSame(0, $this->‪getRedirectCount());
92  $this->importCSVDataSet(__DIR__ . '/Fixtures/RedirectRepositoryTest_redirects.csv');
93 
94  self::assertSame($redirectBeforeCleanup, $this->‪getRedirectCount());
95  $repository = new ‪RedirectRepository();
96  $repository->removeByDemand($demand);
97  self::assertSame($redirectAfterCleanup, $this->‪getRedirectCount());
98  }
99 
100  protected function ‪getRedirectCount(): int
101  {
102  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
103  ->getQueryBuilderForTable('sys_redirect');
104  return (int)$queryBuilder
105  ->count('uid')
106  ->from('sys_redirect')
107  ->executeQuery()
108  ->fetchOne();
109  }
110 
111  private function ‪getDemand(
112  int $hitCount = 0,
113  array $statusCodes = [],
114  array $domains = [],
115  string $path = ''
116  ): ‪Demand {
117  return new ‪Demand(
118  1,
119  '',
120  '',
121  $domains,
122  $path,
123  '',
124  $statusCodes,
125  $hitCount,
126  new \DateTimeImmutable('90 days ago')
127  );
128  }
129 }
‪TYPO3\CMS\Redirects\Repository\Demand
Definition: Demand.php:28
‪TYPO3\CMS\Redirects\Tests\Functional\Repository\RedirectRepositoryTest
Definition: RedirectRepositoryTest.php:27
‪TYPO3\CMS\Redirects\Tests\Functional\Repository\RedirectRepositoryTest\removeByDemandWorks
‪removeByDemandWorks(Demand $demand, int $redirectBeforeCleanup, int $redirectAfterCleanup)
Definition: RedirectRepositoryTest.php:86
‪TYPO3\CMS\Redirects\Tests\Functional\Repository\RedirectRepositoryTest\$coreExtensionsToLoad
‪$coreExtensionsToLoad
Definition: RedirectRepositoryTest.php:28
‪TYPO3\CMS\Redirects\Tests\Functional\Repository\RedirectRepositoryTest\demandProvider
‪demandProvider()
Definition: RedirectRepositoryTest.php:30
‪TYPO3\CMS\Redirects\Tests\Functional\Repository
Definition: RedirectRepositoryTest.php:18
‪TYPO3\CMS\Redirects\Repository\RedirectRepository
Definition: RedirectRepository.php:31
‪TYPO3\CMS\Redirects\Tests\Functional\Repository\RedirectRepositoryTest\getRedirectCount
‪getRedirectCount()
Definition: RedirectRepositoryTest.php:100
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Redirects\Tests\Functional\Repository\RedirectRepositoryTest\getDemand
‪getDemand(int $hitCount=0, array $statusCodes=[], array $domains=[], string $path='')
Definition: RedirectRepositoryTest.php:111