‪TYPO3CMS  ‪main
FormProtectionFactoryTest.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\Log\NullLogger;
36 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
37 
38 final class ‪FormProtectionFactoryTest extends UnitTestCase
39 {
42 
43  protected function ‪setUp(): void
44  {
45  $this->runtimeCacheMock = new ‪VariableFrontend('null', new ‪TransientMemoryBackend('null', ['logger' => new NullLogger()]));
46  $this->subject = new ‪FormProtectionFactory(
49  new ‪Locales(),
50  $this->createMock(LocalizationFactory::class),
51  new ‪NullFrontend('null')
52  ),
53  new ‪Registry(),
54  $this->runtimeCacheMock
55  );
56  parent::setUp();
57  }
58 
59  protected function ‪tearDown(): void
60  {
61  $this->runtimeCacheMock->flush();
62  parent::tearDown();
63  }
64 
65  #[Test]
67  {
68  $formProtection = $this->subject->createForType('invalid-type');
69  self::assertInstanceOf(DisabledFormProtection::class, $formProtection);
70  }
71 
72  #[Test]
74  {
75  $formProtection = $this->subject->createForType('invalid-type');
76  self::assertInstanceOf(DisabledFormProtection::class, $formProtection);
77  $formProtectionDisabled = $this->subject->createForType('disabled');
78  self::assertInstanceOf(DisabledFormProtection::class, $formProtectionDisabled);
79  self::assertSame($formProtectionDisabled, $formProtection);
80  }
81 
82  #[Test]
84  {
85  $formProtection = $this->subject->createForType('frontend');
86  self::assertInstanceOf(DisabledFormProtection::class, $formProtection);
87  $formProtection = $this->subject->createForType('backend');
88  self::assertInstanceOf(DisabledFormProtection::class, $formProtection);
89  }
90 
91  #[Test]
93  {
94  $formProtection = $this->subject->createForType('installtool');
95  self::assertInstanceOf(InstallToolFormProtection::class, $formProtection);
96  }
97 
98  #[Test]
100  {
101  $user = new ‪BackendUserAuthentication();
102  ‪$GLOBALS['BE_USER'] = $user;
103  $formProtection = $this->subject->createForType('backend');
104  // User is not logged in
105  self::assertInstanceOf(DisabledFormProtection::class, $formProtection);
106  }
107 
108  #[Test]
110  {
111  $user = new ‪BackendUserAuthentication();
112  $user->user = ['uid' => 13];
113  ‪$GLOBALS['BE_USER'] = $user;
114  $formProtection = $this->subject->createForType('backend');
115  // User is now logged in
116  self::assertInstanceOf(BackendFormProtection::class, $formProtection);
117  }
118 
119  #[Test]
121  {
122  $user = new ‪BackendUserAuthentication();
123  ‪$GLOBALS['BE_USER'] = $user;
124  $formProtection = $this->subject->createForType('backend');
125  // User is not logged in
126  self::assertInstanceOf(DisabledFormProtection::class, $formProtection);
127  $user->user = ['uid' => 13];
128  $formProtection = $this->subject->createForType('backend');
129  // User is now logged in, but we still get the disabled form protection due to the "singleton" concept
130  self::assertInstanceOf(DisabledFormProtection::class, $formProtection);
131  // we need to manually flush this here, aas next test should expect a cleared state.
132  $this->runtimeCacheMock->flush();
133  $formProtection = $this->subject->createForType('backend');
134  // User is now logged in, now we get a backend form protection, due to the "purge instance" concept.
135  self::assertInstanceOf(BackendFormProtection::class, $formProtection);
136  }
137 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest\createForTypeReturnsTheSameInstanceEvenThoughUserWasLoggedInLaterOn
‪createForTypeReturnsTheSameInstanceEvenThoughUserWasLoggedInLaterOn()
Definition: FormProtectionFactoryTest.php:120
‪TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend
Definition: TransientMemoryBackend.php:25
‪TYPO3\CMS\Core\FormProtection\DisabledFormProtection
Definition: DisabledFormProtection.php:23
‪TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest\createForTypeReturnsDisabledIfInvalidTypeIsGiven
‪createForTypeReturnsDisabledIfInvalidTypeIsGiven()
Definition: FormProtectionFactoryTest.php:66
‪TYPO3\CMS\Core\Localization\LocalizationFactory
Definition: LocalizationFactory.php:31
‪TYPO3\CMS\Core\Registry
Definition: Registry.php:33
‪TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest\createForTypeReturnsDisabledIfInvalidTypeIsGivenAndSameInstanceIfDisabledIsGivenLaterOn
‪createForTypeReturnsDisabledIfInvalidTypeIsGivenAndSameInstanceIfDisabledIsGivenLaterOn()
Definition: FormProtectionFactoryTest.php:73
‪TYPO3\CMS\Core\FormProtection\BackendFormProtection
Definition: BackendFormProtection.php:75
‪TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest\createForTypeReturnsDisabledForValidTypeButWithoutValidGlobalArguments
‪createForTypeReturnsDisabledForValidTypeButWithoutValidGlobalArguments()
Definition: FormProtectionFactoryTest.php:83
‪TYPO3\CMS\Core\Localization\Locales
Definition: Locales.php:36
‪TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest
Definition: FormProtectionFactoryTest.php:39
‪TYPO3\CMS\Core\Cache\Frontend\NullFrontend
Definition: NullFrontend.php:30
‪TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest\createForTypeReturnsDisabledIfBackendUserIsNotAvailable
‪createForTypeReturnsDisabledIfBackendUserIsNotAvailable()
Definition: FormProtectionFactoryTest.php:99
‪TYPO3\CMS\Core\FormProtection\InstallToolFormProtection
Definition: InstallToolFormProtection.php:61
‪TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest\$runtimeCacheMock
‪FrontendInterface $runtimeCacheMock
Definition: FormProtectionFactoryTest.php:41
‪TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest\$subject
‪FormProtectionFactory $subject
Definition: FormProtectionFactoryTest.php:40
‪TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
Definition: VariableFrontend.php:25
‪TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest\createForTypeAlwaysReturnsInstallToolRegardlessOfRequirementsIfRequested
‪createForTypeAlwaysReturnsInstallToolRegardlessOfRequirementsIfRequested()
Definition: FormProtectionFactoryTest.php:92
‪TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest\setUp
‪setUp()
Definition: FormProtectionFactoryTest.php:43
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory
Definition: FormProtectionFactory.php:43
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest\tearDown
‪tearDown()
Definition: FormProtectionFactoryTest.php:59
‪TYPO3\CMS\Core\Tests\Unit\FormProtection
Definition: AbstractFormProtectionTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest\createForTypeReturnsBackendIfBackendUserIsLoggedIn
‪createForTypeReturnsBackendIfBackendUserIsLoggedIn()
Definition: FormProtectionFactoryTest.php:109
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27