‪TYPO3CMS  ‪main
IncrementHitCountTest.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 Psr\EventDispatcher\EventDispatcherInterface;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
27 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
28 
29 final class ‪IncrementHitCountTest extends FunctionalTestCase
30 {
31  protected array ‪$coreExtensionsToLoad = ['redirects'];
32 
33  protected function ‪setUp(): void
34  {
35  parent::setUp();
36  $this->importCSVDataSet(__DIR__ . '/Fixtures/Redirects.csv');
37  }
38 
39  #[Test]
40  public function ‪hitCountIsCorrectlyIncremented(): void
41  {
42  $matchedRecord = BackendUtility::getRecord('sys_redirect', 12);
43 
44  // Assert current hit count
45  self::assertEquals(3, (int)$matchedRecord['hitcount']);
46 
47  $this->get(EventDispatcherInterface::class)->dispatch(
49  new ‪ServerRequest('https://acme.com'),
50  new ‪RedirectResponse($matchedRecord['target']),
51  $matchedRecord,
52  new ‪Uri($matchedRecord['target'])
53  )
54  );
55 
56  // Assert hit count is not incremented, due to missing feature flag
57  self::assertEquals(3, (int)(BackendUtility::getRecord('sys_redirect', 12)['hitcount']));
58 
59  // Set flag
60  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['features']['redirects.hitCount'] = true;
61 
62  // Use another record, which has "disable_hitcount" set
63  $matchedRecord = BackendUtility::getRecord('sys_redirect', 13);
64 
65  // Assert current hit count
66  self::assertEquals(0, (int)$matchedRecord['hitcount']);
67 
68  $this->get(EventDispatcherInterface::class)->dispatch(
70  new ‪ServerRequest('https://acme.com'),
71  new ‪RedirectResponse($matchedRecord['target']),
72  $matchedRecord,
73  new ‪Uri($matchedRecord['target'])
74  )
75  );
76 
77  // Assert hit count is not incremented, due to records' disable flag
78  self::assertEquals(0, (int)(BackendUtility::getRecord('sys_redirect', 13)['hitcount']));
79 
80  // Check record=12 again, as the feature flag is now enabled and the hit count is not disabled for the record
81  $matchedRecord = BackendUtility::getRecord('sys_redirect', 12);
82 
83  // Assert current hit count
84  self::assertEquals(3, (int)$matchedRecord['hitcount']);
85 
86  $this->get(EventDispatcherInterface::class)->dispatch(
88  new ‪ServerRequest('https://acme.com'),
89  new ‪RedirectResponse($matchedRecord['target']),
90  $matchedRecord,
91  new ‪Uri($matchedRecord['target'])
92  )
93  );
94 
95  // Assert incremented hit count
96  self::assertEquals(4, (int)(BackendUtility::getRecord('sys_redirect', 12)['hitcount']));
97  }
98 }
‪TYPO3\CMS\Redirects\Tests\Functional\EventListener\IncrementHitCountTest\$coreExtensionsToLoad
‪array $coreExtensionsToLoad
Definition: IncrementHitCountTest.php:31
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:30
‪TYPO3\CMS\Redirects\Tests\Functional\EventListener\IncrementHitCountTest\setUp
‪setUp()
Definition: IncrementHitCountTest.php:33
‪TYPO3\CMS\Redirects\Tests\Functional\EventListener\IncrementHitCountTest\hitCountIsCorrectlyIncremented
‪hitCountIsCorrectlyIncremented()
Definition: IncrementHitCountTest.php:40
‪TYPO3\CMS\Redirects\Tests\Functional\EventListener\IncrementHitCountTest
Definition: IncrementHitCountTest.php:30
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:30
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Redirects\Tests\Functional\EventListener
Definition: AddPageTypeZeroSourceTest.php:18
‪TYPO3\CMS\Redirects\Event\RedirectWasHitEvent
Definition: RedirectWasHitEvent.php:33