TYPO3 CMS  TYPO3_8-7
FormProtectionFactoryTest.php
Go to the documentation of this file.
1 <?php
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 
19 
23 class FormProtectionFactoryTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
24 {
25  protected function tearDown()
26  {
28  parent::tearDown();
29  }
30 
32  // Tests concerning get
34 
38  {
39  $this->expectException(\InvalidArgumentException::class);
40  $this->expectExceptionCode(1285352962);
41 
42  FormProtectionFactory::get('noSuchClass');
43  }
44 
49  {
50  $this->expectException(\InvalidArgumentException::class);
51  $this->expectExceptionCode(1285353026);
52 
53  FormProtectionFactory::get(\TYPO3\CMS\Core\Tests\Unit\FormProtection\FormProtectionFactoryTest::class);
54  }
55 
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,
66  \TYPO3\CMS\Core\FormProtection\BackendFormProtection::class,
67  $userMock,
68  $this->createMock(Registry::class)
69  )
70  );
71  }
72 
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 
95  {
96  $this->assertTrue(FormProtectionFactory::get(\TYPO3\CMS\Core\FormProtection\InstallToolFormProtection::class) instanceof \TYPO3\CMS\Core\FormProtection\InstallToolFormProtection);
97  }
98 
103  {
104  $this->assertSame(FormProtectionFactory::get(\TYPO3\CMS\Core\FormProtection\InstallToolFormProtection::class), FormProtectionFactory::get(\TYPO3\CMS\Core\FormProtection\InstallToolFormProtection::class));
105  }
106 
111  {
112  $this->assertNotSame(FormProtectionFactory::get(\TYPO3\CMS\Core\FormProtection\InstallToolFormProtection::class), FormProtectionFactory::get(\TYPO3\CMS\Core\FormProtection\DisabledFormProtection::class));
113  }
114 
116  // Tests concerning set
118 
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 
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 
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 
158  public function enqueueAjaxFlashMessageCallback(\TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage)
159  {
160  $this->assertFalse($flashMessage->isSessionMessage());
161  }
162 }
enqueueAjaxFlashMessageCallback(\TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage)
static get($classNameOrType='default',... $constructorArguments)
static set($classNameOrType, AbstractFormProtection $instance)