2 /***************************************************************
5 * (c) 2010 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 ***************************************************************/
25 require_once('fixtures/class.t3lib_formprotection_testing.php');
28 * Testcase for the t3lib_formprotection_Abstract class.
35 * @author Oliver Klee <typo3-coding@oliverklee.de>
37 class t3lib_formprotection_AbstractTest
extends tx_phpunit_testcase
{
39 * @var t3lib_formProtection_Testing
43 public function setUp() {
44 $this->fixture
= new t3lib_formProtection_Testing();
47 public function tearDown() {
48 $this->fixture
->__destruct();
49 unset($this->fixture
);
53 /////////////////////////////////////////
54 // Tests concerning the basic functions
55 /////////////////////////////////////////
60 public function constructionRetrievesTokens() {
61 $className = uniqid('t3lib_formProtection');
63 'class ' . $className . ' extends t3lib_formProtection_Testing {' .
64 'public $tokensHaveBeenRetrieved = FALSE; ' .
65 'protected function retrieveTokens() {' .
66 '$this->tokensHaveBeenRetrieved = TRUE;' .
71 $fixture = new $className();
74 $fixture->tokensHaveBeenRetrieved
81 public function cleanMakesTokenInvalid() {
83 $tokenId = $this->fixture
->generateToken($formName);
85 $this->fixture
->clean();
88 $this->fixture
->validateToken($tokenId, $formName)
95 public function cleanPersistsTokens() {
96 $fixture = $this->getMock(
97 't3lib_formProtection_Testing', array('persistTokens')
99 $fixture->expects($this->once())->method('persistTokens');
105 ///////////////////////////////////
106 // Tests concerning generateToken
107 ///////////////////////////////////
112 public function generateTokenFormForEmptyFormNameThrowsException() {
113 $this->setExpectedException(
114 'InvalidArgumentException', '$formName must not be empty.'
117 $this->fixture
->generateToken('', 'edit', 'bar');
123 public function generateTokenFormForEmptyActionNotThrowsException() {
124 $this->fixture
->generateToken('foo', '', '42');
130 public function generateTokenFormForEmptyFormInstanceNameNotThrowsException() {
131 $this->fixture
->generateToken('foo', 'edit', '');
137 public function generateTokenFormForOmittedActionAndFormInstanceNameNotThrowsException() {
138 $this->fixture
->generateToken('foo');
144 public function generateTokenReturns32CharacterHexToken() {
147 $this->fixture
->generateToken('foo')
154 public function generateTokenCalledTwoTimesWithSameParametersReturnsDifferentTokens() {
155 $this->assertNotEquals(
156 $this->fixture
->generateToken('foo', 'edit', 'bar'),
157 $this->fixture
->generateToken('foo', 'edit', 'bar')
164 public function generatingTooManyTokensInvalidatesOldestToken() {
165 $this->fixture
->setMaximumNumberOfTokens(2);
169 $token1 = $this->fixture
->generateToken($formName);
170 $token2 = $this->fixture
->generateToken($formName);
171 $token3 = $this->fixture
->generateToken($formName);
174 $this->fixture
->validateToken($token1, $formName)
181 public function generatingTooManyTokensNotInvalidatesNewestToken() {
182 $this->fixture
->setMaximumNumberOfTokens(2);
185 $formInstanceName = 'bar';
187 $token1 = $this->fixture
->generateToken($formName);
188 $token2 = $this->fixture
->generateToken($formName);
189 $token3 = $this->fixture
->generateToken($formName);
192 $this->fixture
->validateToken($token3, $formName)
199 public function generatingTooManyTokensNotInvalidatesTokenInTheMiddle() {
200 $this->fixture
->setMaximumNumberOfTokens(2);
203 $formInstanceName = 'bar';
205 $token1 = $this->fixture
->generateToken($formName);
206 $token2 = $this->fixture
->generateToken($formName);
207 $token3 = $this->fixture
->generateToken($formName);
210 $this->fixture
->validateToken($token2, $formName)
215 ///////////////////////////////////
216 // Tests concerning validateToken
217 ///////////////////////////////////
222 public function validateTokenWithFourEmptyParametersNotThrowsException() {
223 $this->fixture
->validateToken('', '', '', '');
229 public function validateTokenWithTwoEmptyAndTwoMissingParametersNotThrowsException() {
230 $this->fixture
->validateToken('', '');
236 public function validateTokenWithDataFromGenerateTokenWithFormInstanceNameReturnsTrue() {
239 $formInstanceName = 'bar';
242 $this->fixture
->validateToken(
243 $this->fixture
->generateToken($formName, $action, $formInstanceName),
254 public function validateTokenWithDataFromGenerateTokenWithMissingActionAndFormInstanceNameReturnsTrue() {
258 $this->fixture
->validateToken(
259 $this->fixture
->generateToken($formName), $formName
267 public function validateTokenWithValidDataDropsToken() {
270 $fixture = $this->getMock(
271 't3lib_formProtection_Testing', array('dropToken')
274 $tokenId = $fixture->generateToken($formName);
275 $fixture->expects($this->once())->method('dropToken')
278 $fixture->validateToken($tokenId, $formName);
284 public function validateTokenWithValidDataCalledTwoTimesReturnsFalseOnSecondCall() {
287 $formInstanceName = 'bar';
289 $tokenId = $this->fixture
->generateToken($formName, $action, $formInstanceName);
291 $this->fixture
->validateToken($tokenId, $formName, $action, $formInstanceName);
294 $this->fixture
->validateToken($tokenId, $formName, $action, $formInstanceName)
301 public function validateTokenWithMismatchingTokenIdReturnsFalse() {
304 $formInstanceName = 'bar';
306 $this->fixture
->generateToken($formName, $action, $formInstanceName);
309 $this->fixture
->validateToken(
310 'Hello world!', $formName, $action, $formInstanceName
318 public function validateTokenWithMismatchingFormNameReturnsFalse() {
321 $formInstanceName = 'bar';
323 $tokenId = $this->fixture
->generateToken($formName, $action, $formInstanceName);
326 $this->fixture
->validateToken(
327 $tokenId, 'espresso', $action, $formInstanceName
335 public function validateTokenWithMismatchingActionReturnsFalse() {
338 $formInstanceName = 'bar';
340 $tokenId = $this->fixture
->generateToken($formName, $action, $formInstanceName);
343 $this->fixture
->validateToken(
344 $tokenId, $formName, 'delete', $formInstanceName
352 public function validateTokenWithMismatchingFormInstanceNameReturnsFalse() {
355 $formInstanceName = 'bar';
357 $tokenId = $this->fixture
->generateToken($formName, $action, $formInstanceName);
360 $this->fixture
->validateToken(
361 $tokenId, $formName, $action, 'beer'
369 public function validateTokenWithTwoTokensForSameFormNameAndActionAndFormInstanceNameReturnsTrueForBoth() {
372 $formInstanceName = 'bar';
374 $tokenId1 = $this->fixture
->generateToken($formName, $action, $formInstanceName);
375 $tokenId2 = $this->fixture
->generateToken($formName, $action, $formInstanceName);
378 $this->fixture
->validateToken(
379 $tokenId1, $formName, $action, $formInstanceName
383 $this->fixture
->validateToken(
384 $tokenId2, $formName, $action, $formInstanceName
392 public function validateTokenWithTwoTokensForSameFormNameAndActionAndFormInstanceNameCalledInReverseOrderReturnsTrueForBoth() {
395 $formInstanceName = 'bar';
397 $tokenId1 = $this->fixture
->generateToken($formName, $action, $formInstanceName);
398 $tokenId2 = $this->fixture
->generateToken($formName, $action, $formInstanceName);
401 $this->fixture
->validateToken(
402 $tokenId2, $formName, $action, $formInstanceName
406 $this->fixture
->validateToken(
407 $tokenId1, $formName, $action, $formInstanceName
415 public function validateTokenForValidTokenNotCallsCreateValidationErrorMessage() {
416 $fixture = $this->getMock(
417 't3lib_formProtection_Testing', array('createValidationErrorMessage')
419 $fixture->expects($this->never())->method('createValidationErrorMessage');
423 $formInstanceName = 'bar';
425 $token = $fixture->generateToken($formName, $action, $formInstanceName);
426 $fixture->validateToken(
427 $token, $formName, $action, $formInstanceName
430 $fixture->__destruct();
436 public function validateTokenForInvalidTokenCallsCreateValidationErrorMessage() {
437 $fixture = $this->getMock(
438 't3lib_formProtection_Testing', array('createValidationErrorMessage')
440 $fixture->expects($this->once())->method('createValidationErrorMessage');
444 $formInstanceName = 'bar';
446 $fixture->generateToken($formName, $action, $formInstanceName);
447 $fixture->validateToken(
448 'an invalid token ...', $formName, $action, $formInstanceName
451 $fixture->__destruct();
457 public function validateTokenForInvalidFormNameCallsCreateValidationErrorMessage() {
458 $fixture = $this->getMock(
459 't3lib_formProtection_Testing', array('createValidationErrorMessage')
461 $fixture->expects($this->once())->method('createValidationErrorMessage');
465 $formInstanceName = 'bar';
467 $token = $fixture->generateToken($formName, $action, $formInstanceName);
468 $fixture->validateToken(
469 $token, 'another form name', $action, $formInstanceName
472 $fixture->__destruct();