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 ***************************************************************/
24 require_once 'fixtures/class.t3lib_formprotection_testing.php';
26 * Testcase for the t3lib_formprotection_Abstract class.
30 * @author Oliver Klee <typo3-coding@oliverklee.de>
32 class t3lib_formprotection_AbstractTest
extends tx_phpunit_testcase
{
35 * @var t3lib_formProtection_Testing
39 public function setUp() {
40 $this->fixture
= new t3lib_formProtection_Testing();
43 public function tearDown() {
44 $this->fixture
->__destruct();
45 unset($this->fixture
);
48 /////////////////////////////////////////
49 // Tests concerning the basic functions
50 /////////////////////////////////////////
54 public function constructionRetrievesToken() {
55 $className = uniqid('t3lib_formProtection');
56 eval((((((('class ' . $className) . ' extends t3lib_formProtection_Testing {') . 'public $tokenHasBeenRetrieved = FALSE; ') . 'protected function retrieveSessionToken() {') . '$this->tokenHasBeenRetrieved = TRUE;') . '}') . '}');
57 $fixture = new $className();
58 $this->assertTrue($fixture->tokenHasBeenRetrieved
);
64 public function cleanMakesTokenInvalid() {
66 $tokenId = $this->fixture
->generateToken($formName);
67 $this->fixture
->clean();
68 $this->assertFalse($this->fixture
->validateToken($tokenId, $formName));
74 public function cleanPersistsToken() {
75 $fixture = $this->getMock('t3lib_formProtection_Testing', array('persistSessionToken'));
76 $fixture->expects($this->once())->method('persistSessionToken');
80 ///////////////////////////////////
81 // Tests concerning generateToken
82 ///////////////////////////////////
86 public function generateTokenFormForEmptyFormNameThrowsException() {
87 $this->setExpectedException('InvalidArgumentException', '$formName must not be empty.');
88 $this->fixture
->generateToken('', 'edit', 'bar');
94 public function generateTokenFormForEmptyActionNotThrowsException() {
95 $this->fixture
->generateToken('foo', '', '42');
101 public function generateTokenFormForEmptyFormInstanceNameNotThrowsException() {
102 $this->fixture
->generateToken('foo', 'edit', '');
108 public function generateTokenFormForOmittedActionAndFormInstanceNameNotThrowsException() {
109 $this->fixture
->generateToken('foo');
115 public function generateTokenReturns32CharacterHexToken() {
116 $this->assertRegexp('/^[0-9a-f]{40}$/', $this->fixture
->generateToken('foo'));
122 public function generateTokenCalledTwoTimesWithSameParametersReturnsSameTokens() {
123 $this->assertEquals($this->fixture
->generateToken('foo', 'edit', 'bar'), $this->fixture
->generateToken('foo', 'edit', 'bar'));
126 ///////////////////////////////////
127 // Tests concerning validateToken
128 ///////////////////////////////////
132 public function validateTokenWithFourEmptyParametersNotThrowsException() {
133 $this->fixture
->validateToken('', '', '', '');
139 public function validateTokenWithTwoEmptyAndTwoMissingParametersNotThrowsException() {
140 $this->fixture
->validateToken('', '');
146 public function validateTokenWithDataFromGenerateTokenWithFormInstanceNameReturnsTrue() {
149 $formInstanceName = 'bar';
150 $this->assertTrue($this->fixture
->validateToken($this->fixture
->generateToken($formName, $action, $formInstanceName), $formName, $action, $formInstanceName));
156 public function validateTokenWithDataFromGenerateTokenWithMissingActionAndFormInstanceNameReturnsTrue() {
158 $this->assertTrue($this->fixture
->validateToken($this->fixture
->generateToken($formName), $formName));
164 public function validateTokenWithValidDataCalledTwoTimesReturnsTrueOnSecondCall() {
167 $formInstanceName = 'bar';
168 $tokenId = $this->fixture
->generateToken($formName, $action, $formInstanceName);
169 $this->fixture
->validateToken($tokenId, $formName, $action, $formInstanceName);
170 $this->assertTrue($this->fixture
->validateToken($tokenId, $formName, $action, $formInstanceName));
176 public function validateTokenWithMismatchingTokenIdReturnsFalse() {
179 $formInstanceName = 'bar';
180 $this->fixture
->generateToken($formName, $action, $formInstanceName);
181 $this->assertFalse($this->fixture
->validateToken('Hello world!', $formName, $action, $formInstanceName));
187 public function validateTokenWithMismatchingFormNameReturnsFalse() {
190 $formInstanceName = 'bar';
191 $tokenId = $this->fixture
->generateToken($formName, $action, $formInstanceName);
192 $this->assertFalse($this->fixture
->validateToken($tokenId, 'espresso', $action, $formInstanceName));
198 public function validateTokenWithMismatchingActionReturnsFalse() {
201 $formInstanceName = 'bar';
202 $tokenId = $this->fixture
->generateToken($formName, $action, $formInstanceName);
203 $this->assertFalse($this->fixture
->validateToken($tokenId, $formName, 'delete', $formInstanceName));
209 public function validateTokenWithMismatchingFormInstanceNameReturnsFalse() {
212 $formInstanceName = 'bar';
213 $tokenId = $this->fixture
->generateToken($formName, $action, $formInstanceName);
214 $this->assertFalse($this->fixture
->validateToken($tokenId, $formName, $action, 'beer'));
220 public function validateTokenForValidTokenNotCallsCreateValidationErrorMessage() {
221 $fixture = $this->getMock('t3lib_formProtection_Testing', array('createValidationErrorMessage'));
222 $fixture->expects($this->never())->method('createValidationErrorMessage');
225 $formInstanceName = 'bar';
226 $token = $fixture->generateToken($formName, $action, $formInstanceName);
227 $fixture->validateToken($token, $formName, $action, $formInstanceName);
228 $fixture->__destruct();
234 public function validateTokenForInvalidTokenCallsCreateValidationErrorMessage() {
235 $fixture = $this->getMock('t3lib_formProtection_Testing', array('createValidationErrorMessage'));
236 $fixture->expects($this->once())->method('createValidationErrorMessage');
239 $formInstanceName = 'bar';
240 $fixture->generateToken($formName, $action, $formInstanceName);
241 $fixture->validateToken('an invalid token ...', $formName, $action, $formInstanceName);
242 $fixture->__destruct();
248 public function validateTokenForInvalidFormNameCallsCreateValidationErrorMessage() {
249 $fixture = $this->getMock('t3lib_formProtection_Testing', array('createValidationErrorMessage'));
250 $fixture->expects($this->once())->method('createValidationErrorMessage');
253 $formInstanceName = 'bar';
254 $token = $fixture->generateToken($formName, $action, $formInstanceName);
255 $fixture->validateToken($token, 'another form name', $action, $formInstanceName);
256 $fixture->__destruct();