[TASK] Update caching framework to latest FLOW3 version
[Packages/TYPO3.CMS.git] / tests / t3lib / formprotection / class.t3lib_formprotection_AbstractTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Oliver Klee (typo3-coding@oliverklee.de)
6 * All rights reserved
7 *
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.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
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.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 require_once('fixtures/class.t3lib_formprotection_testing.php');
26
27 /**
28 * Testcase for the t3lib_formprotection_Abstract class.
29 *
30 * @package TYPO3
31 * @subpackage t3lib
32 *
33 * @author Oliver Klee <typo3-coding@oliverklee.de>
34 */
35 class t3lib_formprotection_AbstractTest extends tx_phpunit_testcase {
36 /**
37 * @var t3lib_formProtection_Testing
38 */
39 private $fixture;
40
41 public function setUp() {
42 $this->fixture = new t3lib_formProtection_Testing();
43 }
44
45 public function tearDown() {
46 $this->fixture->__destruct();
47 unset($this->fixture);
48 }
49
50
51 /////////////////////////////////////////
52 // Tests concerning the basic functions
53 /////////////////////////////////////////
54
55 /**
56 * @test
57 */
58 public function constructionRetrievesToken() {
59 $className = uniqid('t3lib_formProtection');
60 eval(
61 'class ' . $className . ' extends t3lib_formProtection_Testing {' .
62 'public $tokenHasBeenRetrieved = FALSE; ' .
63 'protected function retrieveSessionToken() {' .
64 '$this->tokenHasBeenRetrieved = TRUE;' .
65 '}' .
66 '}'
67 );
68
69 $fixture = new $className();
70
71 $this->assertTrue(
72 $fixture->tokenHasBeenRetrieved
73 );
74 }
75
76 /**
77 * @test
78 */
79 public function cleanMakesTokenInvalid() {
80 $formName = 'foo';
81 $tokenId = $this->fixture->generateToken($formName);
82
83 $this->fixture->clean();
84
85 $this->assertFalse(
86 $this->fixture->validateToken($tokenId, $formName)
87 );
88 }
89
90 /**
91 * @test
92 */
93 public function cleanPersistsToken() {
94 $fixture = $this->getMock(
95 't3lib_formProtection_Testing', array('persistSessionToken')
96 );
97 $fixture->expects($this->once())->method('persistSessionToken');
98
99 $fixture->clean();
100 }
101
102
103 ///////////////////////////////////
104 // Tests concerning generateToken
105 ///////////////////////////////////
106
107 /**
108 * @test
109 */
110 public function generateTokenFormForEmptyFormNameThrowsException() {
111 $this->setExpectedException(
112 'InvalidArgumentException', '$formName must not be empty.'
113 );
114
115 $this->fixture->generateToken('', 'edit', 'bar');
116 }
117
118 /**
119 * @test
120 */
121 public function generateTokenFormForEmptyActionNotThrowsException() {
122 $this->fixture->generateToken('foo', '', '42');
123 }
124
125 /**
126 * @test
127 */
128 public function generateTokenFormForEmptyFormInstanceNameNotThrowsException() {
129 $this->fixture->generateToken('foo', 'edit', '');
130 }
131
132 /**
133 * @test
134 */
135 public function generateTokenFormForOmittedActionAndFormInstanceNameNotThrowsException() {
136 $this->fixture->generateToken('foo');
137 }
138
139 /**
140 * @test
141 */
142 public function generateTokenReturns32CharacterHexToken() {
143 $this->assertRegexp(
144 '/^[0-9a-f]{40}$/',
145 $this->fixture->generateToken('foo')
146 );
147 }
148
149 /**
150 * @test
151 */
152 public function generateTokenCalledTwoTimesWithSameParametersReturnsSameTokens() {
153 $this->assertEquals(
154 $this->fixture->generateToken('foo', 'edit', 'bar'),
155 $this->fixture->generateToken('foo', 'edit', 'bar')
156 );
157 }
158
159
160 ///////////////////////////////////
161 // Tests concerning validateToken
162 ///////////////////////////////////
163
164 /**
165 * @test
166 */
167 public function validateTokenWithFourEmptyParametersNotThrowsException() {
168 $this->fixture->validateToken('', '', '', '');
169 }
170
171 /**
172 * @test
173 */
174 public function validateTokenWithTwoEmptyAndTwoMissingParametersNotThrowsException() {
175 $this->fixture->validateToken('', '');
176 }
177
178 /**
179 * @test
180 */
181 public function validateTokenWithDataFromGenerateTokenWithFormInstanceNameReturnsTrue() {
182 $formName = 'foo';
183 $action = 'edit';
184 $formInstanceName = 'bar';
185
186 $this->assertTrue(
187 $this->fixture->validateToken(
188 $this->fixture->generateToken($formName, $action, $formInstanceName),
189 $formName,
190 $action,
191 $formInstanceName
192 )
193 );
194 }
195
196 /**
197 * @test
198 */
199 public function validateTokenWithDataFromGenerateTokenWithMissingActionAndFormInstanceNameReturnsTrue() {
200 $formName = 'foo';
201
202 $this->assertTrue(
203 $this->fixture->validateToken(
204 $this->fixture->generateToken($formName), $formName
205 )
206 );
207 }
208
209 /**
210 * @test
211 */
212 public function validateTokenWithValidDataCalledTwoTimesReturnsTrueOnSecondCall() {
213 $formName = 'foo';
214 $action = 'edit';
215 $formInstanceName = 'bar';
216
217 $tokenId = $this->fixture->generateToken($formName, $action, $formInstanceName);
218
219 $this->fixture->validateToken($tokenId, $formName, $action, $formInstanceName);
220
221 $this->assertTrue(
222 $this->fixture->validateToken($tokenId, $formName, $action, $formInstanceName)
223 );
224 }
225
226 /**
227 * @test
228 */
229 public function validateTokenWithMismatchingTokenIdReturnsFalse() {
230 $formName = 'foo';
231 $action = 'edit';
232 $formInstanceName = 'bar';
233
234 $this->fixture->generateToken($formName, $action, $formInstanceName);
235
236 $this->assertFalse(
237 $this->fixture->validateToken(
238 'Hello world!', $formName, $action, $formInstanceName
239 )
240 );
241 }
242
243 /**
244 * @test
245 */
246 public function validateTokenWithMismatchingFormNameReturnsFalse() {
247 $formName = 'foo';
248 $action = 'edit';
249 $formInstanceName = 'bar';
250
251 $tokenId = $this->fixture->generateToken($formName, $action, $formInstanceName);
252
253 $this->assertFalse(
254 $this->fixture->validateToken(
255 $tokenId, 'espresso', $action, $formInstanceName
256 )
257 );
258 }
259
260 /**
261 * @test
262 */
263 public function validateTokenWithMismatchingActionReturnsFalse() {
264 $formName = 'foo';
265 $action = 'edit';
266 $formInstanceName = 'bar';
267
268 $tokenId = $this->fixture->generateToken($formName, $action, $formInstanceName);
269
270 $this->assertFalse(
271 $this->fixture->validateToken(
272 $tokenId, $formName, 'delete', $formInstanceName
273 )
274 );
275 }
276
277 /**
278 * @test
279 */
280 public function validateTokenWithMismatchingFormInstanceNameReturnsFalse() {
281 $formName = 'foo';
282 $action = 'edit';
283 $formInstanceName = 'bar';
284
285 $tokenId = $this->fixture->generateToken($formName, $action, $formInstanceName);
286
287 $this->assertFalse(
288 $this->fixture->validateToken(
289 $tokenId, $formName, $action, 'beer'
290 )
291 );
292 }
293
294 /**
295 * @test
296 */
297 public function validateTokenForValidTokenNotCallsCreateValidationErrorMessage() {
298 $fixture = $this->getMock(
299 't3lib_formProtection_Testing', array('createValidationErrorMessage')
300 );
301 $fixture->expects($this->never())->method('createValidationErrorMessage');
302
303 $formName = 'foo';
304 $action = 'edit';
305 $formInstanceName = 'bar';
306
307 $token = $fixture->generateToken($formName, $action, $formInstanceName);
308 $fixture->validateToken(
309 $token, $formName, $action, $formInstanceName
310 );
311
312 $fixture->__destruct();
313 }
314
315 /**
316 * @test
317 */
318 public function validateTokenForInvalidTokenCallsCreateValidationErrorMessage() {
319 $fixture = $this->getMock(
320 't3lib_formProtection_Testing', array('createValidationErrorMessage')
321 );
322 $fixture->expects($this->once())->method('createValidationErrorMessage');
323
324 $formName = 'foo';
325 $action = 'edit';
326 $formInstanceName = 'bar';
327
328 $fixture->generateToken($formName, $action, $formInstanceName);
329 $fixture->validateToken(
330 'an invalid token ...', $formName, $action, $formInstanceName
331 );
332
333 $fixture->__destruct();
334 }
335
336 /**
337 * @test
338 */
339 public function validateTokenForInvalidFormNameCallsCreateValidationErrorMessage() {
340 $fixture = $this->getMock(
341 't3lib_formProtection_Testing', array('createValidationErrorMessage')
342 );
343 $fixture->expects($this->once())->method('createValidationErrorMessage');
344
345 $formName = 'foo';
346 $action = 'edit';
347 $formInstanceName = 'bar';
348
349 $token = $fixture->generateToken($formName, $action, $formInstanceName);
350 $fixture->validateToken(
351 $token, 'another form name', $action, $formInstanceName
352 );
353
354 $fixture->__destruct();
355 }
356 }
357 ?>