[TASK] Namespace and move t3lib unit tests - 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / FormProtection / AbstractFormProtectionTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\FormProtection;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2011 Oliver Klee (typo3-coding@oliverklee.de)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 require_once 'Fixtures/FormProtectionTesting.php';
28
29 /**
30 * Testcase for the t3lib_formprotection_Abstract class.
31 *
32 * @package TYPO3
33 * @subpackage t3lib
34 * @author Oliver Klee <typo3-coding@oliverklee.de>
35 */
36 class AbstractFormProtectionTest extends \tx_phpunit_testcase {
37
38 /**
39 * @var t3lib_formProtection_Testing
40 */
41 private $fixture;
42
43 public function setUp() {
44 $this->fixture = new \TYPO3\CMS\Core\Tests\Unit\FormProtection\Fixtures\FormProtectionTesting();
45 }
46
47 public function tearDown() {
48 $this->fixture->__destruct();
49 unset($this->fixture);
50 }
51
52 /////////////////////////////////////////
53 // Tests concerning the basic functions
54 /////////////////////////////////////////
55 /**
56 * @test
57 */
58 public function constructionRetrievesToken() {
59 $className = uniqid('t3lib_formProtection');
60 eval((((((('class ' . $className) . ' extends \TYPO3\CMS\Core\Tests\Unit\FormProtection\Fixtures\FormProtectionTesting {') . 'public $tokenHasBeenRetrieved = FALSE; ') . 'protected function retrieveSessionToken() {') . '$this->tokenHasBeenRetrieved = TRUE;') . '}') . '}');
61 $fixture = new $className();
62 $this->assertTrue($fixture->tokenHasBeenRetrieved);
63 }
64
65 /**
66 * @test
67 */
68 public function cleanMakesTokenInvalid() {
69 $formName = 'foo';
70 $tokenId = $this->fixture->generateToken($formName);
71 $this->fixture->clean();
72 $this->assertFalse($this->fixture->validateToken($tokenId, $formName));
73 }
74
75 /**
76 * @test
77 */
78 public function cleanPersistsToken() {
79 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Tests\\Unit\\FormProtection\\Fixtures\\FormProtectionTesting', array('persistSessionToken'));
80 $fixture->expects($this->once())->method('persistSessionToken');
81 $fixture->clean();
82 }
83
84 ///////////////////////////////////
85 // Tests concerning generateToken
86 ///////////////////////////////////
87 /**
88 * @test
89 */
90 public function generateTokenFormForEmptyFormNameThrowsException() {
91 $this->setExpectedException('InvalidArgumentException', '$formName must not be empty.');
92 $this->fixture->generateToken('', 'edit', 'bar');
93 }
94
95 /**
96 * @test
97 */
98 public function generateTokenFormForEmptyActionNotThrowsException() {
99 $this->fixture->generateToken('foo', '', '42');
100 }
101
102 /**
103 * @test
104 */
105 public function generateTokenFormForEmptyFormInstanceNameNotThrowsException() {
106 $this->fixture->generateToken('foo', 'edit', '');
107 }
108
109 /**
110 * @test
111 */
112 public function generateTokenFormForOmittedActionAndFormInstanceNameNotThrowsException() {
113 $this->fixture->generateToken('foo');
114 }
115
116 /**
117 * @test
118 */
119 public function generateTokenReturns32CharacterHexToken() {
120 $this->assertRegexp('/^[0-9a-f]{40}$/', $this->fixture->generateToken('foo'));
121 }
122
123 /**
124 * @test
125 */
126 public function generateTokenCalledTwoTimesWithSameParametersReturnsSameTokens() {
127 $this->assertEquals($this->fixture->generateToken('foo', 'edit', 'bar'), $this->fixture->generateToken('foo', 'edit', 'bar'));
128 }
129
130 ///////////////////////////////////
131 // Tests concerning validateToken
132 ///////////////////////////////////
133 /**
134 * @test
135 */
136 public function validateTokenWithFourEmptyParametersNotThrowsException() {
137 $this->fixture->validateToken('', '', '', '');
138 }
139
140 /**
141 * @test
142 */
143 public function validateTokenWithTwoEmptyAndTwoMissingParametersNotThrowsException() {
144 $this->fixture->validateToken('', '');
145 }
146
147 /**
148 * @test
149 */
150 public function validateTokenWithDataFromGenerateTokenWithFormInstanceNameReturnsTrue() {
151 $formName = 'foo';
152 $action = 'edit';
153 $formInstanceName = 'bar';
154 $this->assertTrue($this->fixture->validateToken($this->fixture->generateToken($formName, $action, $formInstanceName), $formName, $action, $formInstanceName));
155 }
156
157 /**
158 * @test
159 */
160 public function validateTokenWithDataFromGenerateTokenWithMissingActionAndFormInstanceNameReturnsTrue() {
161 $formName = 'foo';
162 $this->assertTrue($this->fixture->validateToken($this->fixture->generateToken($formName), $formName));
163 }
164
165 /**
166 * @test
167 */
168 public function validateTokenWithValidDataCalledTwoTimesReturnsTrueOnSecondCall() {
169 $formName = 'foo';
170 $action = 'edit';
171 $formInstanceName = 'bar';
172 $tokenId = $this->fixture->generateToken($formName, $action, $formInstanceName);
173 $this->fixture->validateToken($tokenId, $formName, $action, $formInstanceName);
174 $this->assertTrue($this->fixture->validateToken($tokenId, $formName, $action, $formInstanceName));
175 }
176
177 /**
178 * @test
179 */
180 public function validateTokenWithMismatchingTokenIdReturnsFalse() {
181 $formName = 'foo';
182 $action = 'edit';
183 $formInstanceName = 'bar';
184 $this->fixture->generateToken($formName, $action, $formInstanceName);
185 $this->assertFalse($this->fixture->validateToken('Hello world!', $formName, $action, $formInstanceName));
186 }
187
188 /**
189 * @test
190 */
191 public function validateTokenWithMismatchingFormNameReturnsFalse() {
192 $formName = 'foo';
193 $action = 'edit';
194 $formInstanceName = 'bar';
195 $tokenId = $this->fixture->generateToken($formName, $action, $formInstanceName);
196 $this->assertFalse($this->fixture->validateToken($tokenId, 'espresso', $action, $formInstanceName));
197 }
198
199 /**
200 * @test
201 */
202 public function validateTokenWithMismatchingActionReturnsFalse() {
203 $formName = 'foo';
204 $action = 'edit';
205 $formInstanceName = 'bar';
206 $tokenId = $this->fixture->generateToken($formName, $action, $formInstanceName);
207 $this->assertFalse($this->fixture->validateToken($tokenId, $formName, 'delete', $formInstanceName));
208 }
209
210 /**
211 * @test
212 */
213 public function validateTokenWithMismatchingFormInstanceNameReturnsFalse() {
214 $formName = 'foo';
215 $action = 'edit';
216 $formInstanceName = 'bar';
217 $tokenId = $this->fixture->generateToken($formName, $action, $formInstanceName);
218 $this->assertFalse($this->fixture->validateToken($tokenId, $formName, $action, 'beer'));
219 }
220
221 /**
222 * @test
223 */
224 public function validateTokenForValidTokenNotCallsCreateValidationErrorMessage() {
225 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Tests\\Unit\\FormProtection\\Fixtures\\FormProtectionTesting', array('createValidationErrorMessage'));
226 $fixture->expects($this->never())->method('createValidationErrorMessage');
227 $formName = 'foo';
228 $action = 'edit';
229 $formInstanceName = 'bar';
230 $token = $fixture->generateToken($formName, $action, $formInstanceName);
231 $fixture->validateToken($token, $formName, $action, $formInstanceName);
232 $fixture->__destruct();
233 }
234
235 /**
236 * @test
237 */
238 public function validateTokenForInvalidTokenCallsCreateValidationErrorMessage() {
239 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Tests\\Unit\\FormProtection\\Fixtures\\FormProtectionTesting', array('createValidationErrorMessage'));
240 $fixture->expects($this->once())->method('createValidationErrorMessage');
241 $formName = 'foo';
242 $action = 'edit';
243 $formInstanceName = 'bar';
244 $fixture->generateToken($formName, $action, $formInstanceName);
245 $fixture->validateToken('an invalid token ...', $formName, $action, $formInstanceName);
246 $fixture->__destruct();
247 }
248
249 /**
250 * @test
251 */
252 public function validateTokenForInvalidFormNameCallsCreateValidationErrorMessage() {
253 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Tests\\Unit\\FormProtection\\Fixtures\\FormProtectionTesting', array('createValidationErrorMessage'));
254 $fixture->expects($this->once())->method('createValidationErrorMessage');
255 $formName = 'foo';
256 $action = 'edit';
257 $formInstanceName = 'bar';
258 $token = $fixture->generateToken($formName, $action, $formInstanceName);
259 $fixture->validateToken($token, 'another form name', $action, $formInstanceName);
260 $fixture->__destruct();
261 }
262
263 }
264
265 ?>