2 /***************************************************************
5 * (c) 2010-2011 Oliver Klee (typo3-coding@oliverklee.de)
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
26 * Testcase for the t3lib_formprotection_BackendFormProtection class.
33 * @author Oliver Klee <typo3-coding@oliverklee.de>
35 class t3lib_formprotection_BackendFormProtectionTest
extends tx_phpunit_testcase
{
37 * a backup of the current BE user
39 * @var t3lib_beUserAuth
41 private $backEndUserBackup = NULL;
44 * @var t3lib_formprotection_BackendFormProtection
48 public function setUp() {
49 $this->backEndUserBackup
= $GLOBALS['BE_USER'];
50 $GLOBALS['BE_USER'] = $this->getMock(
52 array('getSessionData', 'setAndSaveSessionData')
55 $className = $this->createAccessibleProxyClass();
56 $this->fixture
= new $className;
59 public function tearDown() {
60 $this->fixture
->__destruct();
61 unset($this->fixture
);
63 $GLOBALS['BE_USER'] = $this->backEndUserBackup
;
65 t3lib_FlashMessageQueue
::getAllMessagesAndFlush();
69 //////////////////////
71 //////////////////////
74 * Creates a subclass t3lib_formprotection_BackendFormProtection with retrieveTokens made
77 * @return string the name of the created class, will not be empty
79 private function createAccessibleProxyClass() {
80 $className = 't3lib_formprotection_BackendFormProtectionAccessibleProxy';
81 if (!class_exists($className)) {
83 'class ' . $className . ' extends t3lib_formprotection_BackendFormProtection {' .
84 ' public function createValidationErrorMessage() {' .
85 ' parent::createValidationErrorMessage();' .
87 ' public function retrieveTokens() {' .
88 ' return parent::retrieveTokens();' .
98 ////////////////////////////////////
99 // Tests for the utility functions
100 ////////////////////////////////////
105 public function createAccessibleProxyCreatesBackendFormProtectionSubclass() {
106 $className = $this->createAccessibleProxyClass();
109 (new $className()) instanceof t3lib_formprotection_BackendFormProtection
114 //////////////////////////////////////////////////////////
115 // Tests concerning the reading and saving of the tokens
116 //////////////////////////////////////////////////////////
121 public function retrieveTokensReadsTokensFromSessionData() {
122 $GLOBALS['BE_USER']->expects($this->once())->method('getSessionData')
123 ->with('formTokens')->will($this->returnValue(array()));
125 $this->fixture
->retrieveTokens();
131 public function tokensFromSessionDataAreAvailableForValidateToken() {
132 $tokenId = '51a655b55c54d54e5454c5f521f6552a';
135 $formInstanceName = '42';
137 $GLOBALS['BE_USER']->expects($this->once())->method('getSessionData')
138 ->with('formTokens')->will($this->returnValue(array(
140 'formName' => $formName,
142 'formInstanceName' => $formInstanceName,
146 $this->fixture
->retrieveTokens();
149 $this->fixture
->validateToken($tokenId, $formName, $action, $formInstanceName)
156 public function persistTokensWritesTokensToSession() {
159 $formInstanceName = '42';
161 $tokenId = $this->fixture
->generateToken(
162 $formName, $action, $formInstanceName
166 'formName' => $formName,
168 'formInstanceName' => $formInstanceName,
172 $GLOBALS['BE_USER']->expects($this->once())
173 ->method('setAndSaveSessionData')->with('formTokens', $allTokens);
175 $this->fixture
->persistTokens();
179 //////////////////////////////////////////////////
180 // Tests concerning createValidationErrorMessage
181 //////////////////////////////////////////////////
186 public function createValidationErrorMessageAddsErrorFlashMessage() {
187 $this->fixture
->createValidationErrorMessage();
189 $messages = t3lib_FlashMessageQueue
::getAllMessagesAndFlush();
190 $this->assertContains(
191 $GLOBALS['LANG']->sL(
192 'LLL:EXT:lang/locallang_core.xml:error.formProtection.tokenInvalid'
194 $messages[0]->render()