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