[BUGFIX] Add missing namespace parts
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / FormProtection / FormProtectionFactoryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\FormProtection;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
18 use TYPO3\CMS\Core\Registry;
19
20 /**
21 * Testcase
22 */
23 class FormProtectionFactoryTest extends \TYPO3\Components\TestingFramework\Core\Unit\UnitTestCase
24 {
25 protected function tearDown()
26 {
27 FormProtectionFactory::purgeInstances();
28 parent::tearDown();
29 }
30
31 /////////////////////////
32 // Tests concerning get
33 /////////////////////////
34 /**
35 * @test
36 */
37 public function getForNotExistingClassThrowsException()
38 {
39 $this->expectException(\InvalidArgumentException::class);
40 $this->expectExceptionCode(1285352962);
41
42 FormProtectionFactory::get('noSuchClass');
43 }
44
45 /**
46 * @test
47 */
48 public function getForClassThatIsNoFormProtectionSubclassThrowsException()
49 {
50 $this->expectException(\InvalidArgumentException::class);
51 $this->expectExceptionCode(1285353026);
52
53 FormProtectionFactory::get(\TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest::class);
54 }
55
56 /**
57 * @test
58 */
59 public function getForTypeBackEndWithExistingBackEndReturnsBackEndFormProtection()
60 {
61 $userMock = $this->createMock(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class);
62 $userMock->user = ['uid' => $this->getUniqueId()];
63 $this->assertInstanceOf(
64 \TYPO3\CMS\Core\FormProtection\BackendFormProtection::class,
65 FormProtectionFactory::get(
66 \TYPO3\CMS\Core\FormProtection\BackendFormProtection::class,
67 $userMock,
68 $this->createMock(Registry::class)
69 )
70 );
71 }
72
73 /**
74 * @test
75 */
76 public function getForTypeBackEndCalledTwoTimesReturnsTheSameInstance()
77 {
78 $userMock = $this->createMock(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class);
79 $userMock->user = ['uid' => $this->getUniqueId()];
80 $arguments = [
81 \TYPO3\CMS\Core\FormProtection\BackendFormProtection::class,
82 $userMock,
83 $this->createMock(Registry::class)
84 ];
85 $this->assertSame(
86 call_user_func_array([FormProtectionFactory::class, 'get'], $arguments),
87 call_user_func_array([FormProtectionFactory::class, 'get'], $arguments)
88 );
89 }
90
91 /**
92 * @test
93 */
94 public function getForTypeInstallToolReturnsInstallToolFormProtection()
95 {
96 $this->assertTrue(FormProtectionFactory::get(\TYPO3\CMS\Core\FormProtection\InstallToolFormProtection::class) instanceof \TYPO3\CMS\Core\FormProtection\InstallToolFormProtection);
97 }
98
99 /**
100 * @test
101 */
102 public function getForTypeInstallToolCalledTwoTimesReturnsTheSameInstance()
103 {
104 $this->assertSame(FormProtectionFactory::get(\TYPO3\CMS\Core\FormProtection\InstallToolFormProtection::class), FormProtectionFactory::get(\TYPO3\CMS\Core\FormProtection\InstallToolFormProtection::class));
105 }
106
107 /**
108 * @test
109 */
110 public function getForTypesInstallToolAndDisabledReturnsDifferentInstances()
111 {
112 $this->assertNotSame(FormProtectionFactory::get(\TYPO3\CMS\Core\FormProtection\InstallToolFormProtection::class), FormProtectionFactory::get(\TYPO3\CMS\Core\FormProtection\DisabledFormProtection::class));
113 }
114
115 /////////////////////////
116 // Tests concerning set
117 /////////////////////////
118 /**
119 * @test
120 */
121 public function setSetsInstanceForType()
122 {
123 $instance = new \TYPO3\CMS\Core\Tests\Unit\FormProtection\Fixtures\FormProtectionTesting();
124 FormProtectionFactory::set(\TYPO3\CMS\Core\FormProtection\BackendFormProtection::class, $instance);
125 $this->assertSame($instance, FormProtectionFactory::get(\TYPO3\CMS\Core\FormProtection\BackendFormProtection::class));
126 }
127
128 /**
129 * @test
130 */
131 public function setNotSetsInstanceForOtherType()
132 {
133 $instance = new \TYPO3\CMS\Core\Tests\Unit\FormProtection\Fixtures\FormProtectionTesting();
134 FormProtectionFactory::set(\TYPO3\CMS\Core\FormProtection\BackendFormProtection::class, $instance);
135 $this->assertNotSame($instance, FormProtectionFactory::get(\TYPO3\CMS\Core\FormProtection\InstallToolFormProtection::class));
136 }
137
138 /**
139 * @test
140 */
141 public function createValidationErrorMessageAddsErrorFlashMessageButNotInSessionInAjaxRequest()
142 {
143 $flashMessageQueueMock = $this->createMock(\TYPO3\CMS\Core\Messaging\FlashMessageQueue::class);
144 $flashMessageQueueMock
145 ->expects($this->once())
146 ->method('enqueue')
147 ->with($this->isInstanceOf(\TYPO3\CMS\Core\Messaging\FlashMessage::class))
148 ->will($this->returnCallback([$this, 'enqueueAjaxFlashMessageCallback']));
149 $languageServiceMock = $this->createMock(\TYPO3\CMS\Lang\LanguageService::class);
150 $languageServiceMock->expects($this->once())->method('sL')->will($this->returnValue('foo'));
151
152 FormProtectionFactory::getMessageClosure($languageServiceMock, $flashMessageQueueMock, true)->__invoke();
153 }
154
155 /**
156 * @param \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage
157 */
158 public function enqueueAjaxFlashMessageCallback(\TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage)
159 {
160 $this->assertFalse($flashMessage->isSessionMessage());
161 }
162 }