TYPO3 CMS  TYPO3_6-2
BackendFormProtectionTest.php
Go to the documentation of this file.
1 <?php
3 
23 
27  protected $subject;
28 
33 
37  public function setUp() {
39 
40  $GLOBALS['BE_USER'] = $this->getMock(
41  'TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication',
42  array('getSessionData', 'setAndSaveSessionData')
43  );
44  $GLOBALS['BE_USER']->user['uid'] = 1;
45 
46  $this->subject = $this->getAccessibleMock(
47  'TYPO3\\CMS\\Core\\FormProtection\BackendFormProtection',
48  array('acquireLock', 'releaseLock', 'getLanguageService', 'isAjaxRequest')
49  );
50  }
51 
52  public function tearDown() {
54  parent::tearDown();
55  }
56 
58  // Utility functions
60 
64  protected function getBackendUser() {
65  return $GLOBALS['BE_USER'];
66  }
67 
69  // Tests for the utility functions
71 
76  $this->assertInstanceOf(
77  'TYPO3\\CMS\\Core\\Authentication\BackendUserAuthentication',
78  $this->getBackendUser()
79  );
80  }
81 
83  // Tests concerning the reading and saving of the tokens
85 
90  $this->getBackendUser()
91  ->expects($this->once())
92  ->method('getSessionData')
93  ->with('formSessionToken')
94  ->will($this->returnValue(array()));
95  $this->subject->_call('retrieveSessionToken');
96  }
97 
102  $sessionToken = '881ffea2159ac72182557b79dc0c723f5a8d20136f9fab56cdd4f8b3a1dbcfcd';
103  $formName = 'foo';
104  $action = 'edit';
105  $formInstanceName = '42';
106 
108  $formName . $action . $formInstanceName . $sessionToken
109  );
110 
111  $this->getBackendUser()
112  ->expects($this->atLeastOnce())
113  ->method('getSessionData')
114  ->with('formSessionToken')
115  ->will($this->returnValue($sessionToken));
116 
117  $this->subject->_call('retrieveSessionToken');
118 
119  $this->assertTrue(
120  $this->subject->validateToken($tokenId, $formName, $action, $formInstanceName)
121  );
122  }
123 
128  public function restoreSessionTokenFromRegistryThrowsExceptionIfSessionTokenIsEmpty() {
130  $registryMock = $this->getMock('TYPO3\\CMS\\Core\\Registry');
131  $this->subject->injectRegistry($registryMock);
132  $this->subject->setSessionTokenFromRegistry();
133  }
134 
139  $sessionToken = $this->getUniqueId('test_');
140  $this->subject->_set('sessionToken', $sessionToken);
141  $this->getBackendUser()
142  ->expects($this->once())
143  ->method('setAndSaveSessionData')
144  ->with('formSessionToken', $sessionToken);
145  $this->subject->persistSessionToken();
146  }
147 
148 
150  // Tests concerning createValidationErrorMessage
152 
156  public function createValidationErrorMessageAddsFlashMessage() {
158  $flashMessageServiceMock = $this->getMock('TYPO3\\CMS\\Core\\Messaging\\FlashMessageService');
160  'TYPO3\\CMS\\Core\\Messaging\\FlashMessageService',
161  $flashMessageServiceMock
162  );
163  $flashMessageQueueMock = $this->getMock(
164  'TYPO3\\CMS\\Core\\Messaging\\FlashMessageQueue',
165  array(),
166  array(),
167  '',
168  FALSE
169  );
170  $flashMessageServiceMock
171  ->expects($this->once())
172  ->method('getMessageQueueByIdentifier')
173  ->will($this->returnValue($flashMessageQueueMock));
174  $flashMessageQueueMock
175  ->expects($this->once())
176  ->method('enqueue')
177  ->with($this->isInstanceOf('TYPO3\\CMS\\Core\\Messaging\\FlashMessage'))
178  ->will($this->returnCallback(array($this, 'enqueueFlashMessageCallback')));
179 
180  $languageServiceMock = $this->getMock('TYPO3\CMS\Lang\LanguageService', array(), array(), '', FALSE);
181  $languageServiceMock->expects($this->once())->method('sL')->will($this->returnValue('foo'));
182  $this->subject->expects($this->once())->method('getLanguageService')->will($this->returnValue($languageServiceMock));
183 
184  $this->subject->_call('createValidationErrorMessage');
185  }
186 
190  public function enqueueFlashMessageCallback(\TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage) {
191  $this->assertEquals(\TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, $flashMessage->getSeverity());
192  }
193 
197  public function createValidationErrorMessageAddsErrorFlashMessageButNotInSessionInAjaxRequest() {
199  $flashMessageServiceMock = $this->getMock('TYPO3\\CMS\\Core\\Messaging\\FlashMessageService');
201  'TYPO3\\CMS\\Core\\Messaging\\FlashMessageService',
202  $flashMessageServiceMock
203  );
204  $flashMessageQueueMock = $this->getMock(
205  'TYPO3\\CMS\\Core\\Messaging\\FlashMessageQueue',
206  array(),
207  array(),
208  '',
209  FALSE
210  );
211  $flashMessageServiceMock
212  ->expects($this->once())
213  ->method('getMessageQueueByIdentifier')
214  ->will($this->returnValue($flashMessageQueueMock));
215  $flashMessageQueueMock
216  ->expects($this->once())
217  ->method('enqueue')
218  ->with($this->isInstanceOf('TYPO3\\CMS\\Core\\Messaging\\FlashMessage'))
219  ->will($this->returnCallback(array($this, 'enqueueAjaxFlashMessageCallback')));
220 
221  $languageServiceMock = $this->getMock('TYPO3\CMS\Lang\LanguageService', array(), array(), '', FALSE);
222  $languageServiceMock->expects($this->once())->method('sL')->will($this->returnValue('foo'));
223  $this->subject->expects($this->once())->method('getLanguageService')->will($this->returnValue($languageServiceMock));
224 
225  $this->subject->expects($this->any())->method('isAjaxRequest')->will($this->returnValue(TRUE));
226  $this->subject->_call('createValidationErrorMessage');
227  }
228 
232  public function enqueueAjaxFlashMessageCallback(\TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage) {
233  $this->assertFalse($flashMessage->isSessionMessage());
234  }
235 }
static setSingletonInstance($className, \TYPO3\CMS\Core\SingletonInterface $instance)
static hmac($input, $additionalSecret='')
static resetSingletonInstances(array $newSingletonInstances)
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)
enqueueAjaxFlashMessageCallback(\TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
enqueueFlashMessageCallback(\TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage)