[TASK] Namespace and move t3lib unit tests - 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / FormProtection / InstallToolFormProtectionTest.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(\TYPO3\CMS\Core\Extension\ExtensionManager::extPath('install') . 'mod/class.tx_install.php');
28
29 /**
30 * Testcase for the \TYPO3\CMS\Core\FormProtection\InstallToolFormProtection class.
31 *
32 * @package TYPO3
33 * @subpackage t3lib
34 *
35 * @author Oliver Klee <typo3-coding@oliverklee.de>
36 */
37 class InstallToolFormProtectionTest extends \tx_phpunit_testcase {
38 /**
39 * @var \TYPO3\CMS\Core\FormProtection\InstallToolFormProtection
40 */
41 private $fixture;
42
43 /**
44 * backup of $_SESSION
45 *
46 * @var array
47 */
48 private $sessionBackup;
49
50 public function setUp() {
51 $this->sessionBackup = $_SESSION;
52
53 $className = $this->createAccessibleProxyClass();
54 $this->fixture = new $className();
55 }
56
57 public function tearDown() {
58 $this->fixture->__destruct();
59 unset($this->fixture);
60
61 \TYPO3\CMS\Core\Messaging\FlashMessageQueue::getAllMessagesAndFlush();
62
63 $_SESSION = $this->sessionBackup;
64 }
65
66
67 //////////////////////
68 // Utility functions
69 //////////////////////
70
71 /**
72 * Creates a subclass \TYPO3\CMS\Core\FormProtection\InstallToolFormProtection with retrieveTokens made
73 * public.
74 *
75 * @return string the name of the created class, will not be empty
76 */
77 private function createAccessibleProxyClass() {
78 $className = 't3lib_formprotection_InstallToolFormProtectionAccessibleProxy';
79 if (!class_exists($className)) {
80 eval(
81 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\FormProtection\\InstallToolFormProtection {' .
82 ' public $sessionToken;' .
83 ' public function createValidationErrorMessage() {' .
84 ' parent::createValidationErrorMessage();' .
85 ' }' .
86 ' public function retrieveSessionToken() {' .
87 ' parent::retrieveSessionToken();' .
88 ' }' .
89 '}'
90 );
91 }
92
93 return $className;
94 }
95
96
97 ////////////////////////////////////
98 // Tests for the utility functions
99 ////////////////////////////////////
100
101 /**
102 * @test
103 */
104 public function createAccessibleProxyCreatesInstallToolFormProtectionSubclass() {
105 $className = $this->createAccessibleProxyClass();
106
107 $this->assertTrue(
108 (new $className()) instanceof \TYPO3\CMS\Core\FormProtection\InstallToolFormProtection
109 );
110 }
111
112
113 //////////////////////////////////////////////////////////
114 // Tests concerning the reading and saving of the tokens
115 //////////////////////////////////////////////////////////
116
117 /**
118 * @test
119 */
120 public function tokenFromSessionDataIsAvailableForValidateToken() {
121 $sessionToken = '881ffea2159ac72182557b79dc0c723f5a8d20136f9fab56cdd4f8b3a1dbcfcd';
122 $formName = 'foo';
123 $action = 'edit';
124 $formInstanceName = '42';
125
126 $tokenId = \t3lib_div::hmac($formName . $action . $formInstanceName . $sessionToken);
127
128 $_SESSION['installToolFormToken'] = $sessionToken;
129
130 $this->fixture->retrieveSessionToken();
131
132 $this->assertTrue(
133 $this->fixture->validateToken($tokenId, $formName, $action, $formInstanceName)
134 );
135 }
136
137 /**
138 * @test
139 */
140 public function persistSessionTokenWritesTokensToSession() {
141 $_SESSION['installToolFormToken'] = 'foo';
142
143 $this->fixture->sessionToken = '881ffea2159ac72182557b79dc0c723f5a8d20136f9fab56cdd4f8b3a1dbcfcd';
144
145 $this->fixture->persistSessionToken();
146
147 $this->assertEquals(
148 '881ffea2159ac72182557b79dc0c723f5a8d20136f9fab56cdd4f8b3a1dbcfcd',
149 $_SESSION['installToolFormToken']
150 );
151 }
152
153
154 //////////////////////////////////////////////////
155 // Tests concerning createValidationErrorMessage
156 //////////////////////////////////////////////////
157
158 /**
159 * @test
160 */
161 public function createValidationErrorMessageAddsErrorMessage() {
162 $installTool = $this->getMock(
163 'TYPO3\\CMS\\Install\\Installer', array('addErrorMessage'), array(), '', FALSE
164 );
165 $installTool->expects($this->once())->method('addErrorMessage')
166 ->with(
167 'Validating the security token of this form has failed. ' .
168 'Please reload the form and submit it again.'
169 );
170 $this->fixture->injectInstallTool($installTool);
171
172 $this->fixture->createValidationErrorMessage();
173 }
174 }
175 ?>