Follow-up to feature #2278: Added unit tests for the new checkModifyAccessList hook
[Packages/TYPO3.CMS.git] / tests / t3lib / t3lib_tcemain_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 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(PATH_t3lib . 'class.t3lib_beuserauth.php');
26
27 require_once(PATH_t3lib . 'class.t3lib_tcemain.php');
28
29 /**
30 * Testcase for the t3lib_TCEmain class in the TYPO3 core.
31 *
32 * @package TYPO3
33 * @subpackage t3lib
34 *
35 * @author Oliver Klee <typo3-coding@oliverklee.de>
36 */
37 class t3lib_tcemain_testcase extends tx_phpunit_testcase {
38 /**
39 * @var boolean
40 */
41 protected $backupGlobals = true;
42
43 /**
44 * @var t3lib_TCEmain
45 */
46 private $fixture;
47
48 /**
49 * @var t3lib_beUserAuth a simulated logged-in back-end user
50 */
51 private $backEndUser;
52
53 public function setUp() {
54 $this->backEndUser = $this->createBackEndUser();
55
56 $this->fixture = new t3lib_TCEmain();
57 $this->fixture->start(array(), '', $this->backEndUser);
58 }
59
60 public function tearDown() {
61 unset(
62 $this->fixture->BE_USER, $this->fixture, $this->backEndUser
63 );
64 }
65
66
67 //////////////////////
68 // Utility functions
69 //////////////////////
70
71 /**
72 * Creates a back-end user.
73 *
74 * @return t3lib_beUserAuth a back-end user
75 */
76 private function createBackEndUser() {
77 $user = new t3lib_beUserAuth();
78 $user->user = array();
79
80 return $user;
81 }
82
83
84 ////////////////////////////////////
85 // Tests for the utility functions
86 ////////////////////////////////////
87
88 /**
89 * @test
90 */
91 public function createBackEndUserCreatesBeUserAuthInstance() {
92 $this->assertTrue(
93 $this->createBackEndUser() instanceof t3lib_beUserAuth
94 );
95 }
96
97
98 //////////////////////////////////////
99 // Tests for the basic functionality
100 //////////////////////////////////////
101
102 /**
103 * @test
104 */
105 public function fixtureCanBeCreated() {
106 $this->assertTrue(
107 $this->fixture instanceof t3lib_TCEmain
108 );
109 }
110
111
112 //////////////////////////////////////////
113 // Test concerning checkModifyAccessList
114 //////////////////////////////////////////
115
116 /**
117 * @test
118 */
119 public function adminIsAllowedToModifyNonAdminTable() {
120 $this->fixture->admin = true;
121
122 $this->assertTrue(
123 $this->fixture->checkModifyAccessList('tt_content')
124 );
125 }
126
127 /**
128 * @test
129 */
130 public function nonAdminIsNorAllowedToModifyNonAdminTable() {
131 $this->fixture->admin = false;
132
133 $this->assertFalse(
134 $this->fixture->checkModifyAccessList('tt_content')
135 );
136 }
137
138 /**
139 * @test
140 */
141 public function nonAdminWithTableModifyAccessIsAllowedToModifyNonAdminTable() {
142 $this->fixture->admin = false;
143 $this->backEndUser->groupData['tables_modify'] = 'tt_content';
144
145 $this->assertTrue(
146 $this->fixture->checkModifyAccessList('tt_content')
147 );
148 }
149
150 /**
151 * @test
152 */
153 public function adminIsAllowedToModifyAdminTable() {
154 $this->fixture->admin = true;
155
156 $this->assertTrue(
157 $this->fixture->checkModifyAccessList('be_users')
158 );
159 }
160
161 /**
162 * @test
163 */
164 public function nonAdminIsNotAllowedToModifyAdminTable() {
165 $this->fixture->admin = false;
166
167 $this->assertFalse(
168 $this->fixture->checkModifyAccessList('be_users')
169 );
170 }
171
172 /**
173 * @test
174 */
175 public function nonAdminWithTableModifyAccessIsNotAllowedToModifyAdminTable() {
176 $this->fixture->admin = false;
177 $this->backEndUser->groupData['tables_modify'] = 'be_users';
178
179 $this->assertFalse(
180 $this->fixture->checkModifyAccessList('be_users')
181 );
182 }
183
184 /**
185 * @test
186 */
187 public function evalCheckValueDouble2() {
188 $testData = array (
189 '-0,5' => '-0.50',
190 '1000' => '1000.00',
191 '1000,10' => '1000.10',
192 '1000,0' => '1000.00',
193 '600.000.000,00' => '600000000.00',
194 '60aaa00' => '6000.00',
195 );
196 foreach ($testData as $value => $expectedReturnValue){
197 $returnValue = $this->fixture->checkValue_input_Eval($value, array('double2'), '');
198 $this->assertSame(
199 $returnValue['value'],
200 $expectedReturnValue
201 );
202 }
203 }
204
205 /**
206 * Tests whether a wrong interface on the 'checkModifyAccessList' hook throws an exception.
207 * @test
208 * @expectedException UnexpectedValueException
209 * @see t3lib_TCEmain::checkModifyAccessList()
210 */
211 public function doesCheckModifyAccessListThrowExceptionOnWrongHookInterface() {
212 $hookClass = uniqid('tx_coretest');
213 eval('class ' . $hookClass . ' {}');
214
215 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList'][] = $hookClass;
216
217 $this->fixture->checkModifyAccessList('tt_content');
218 }
219
220 /**
221 * Tests whether the 'checkModifyAccessList' hook is called correctly.
222 * @test
223 * @see t3lib_TCEmain::checkModifyAccessList()
224 */
225 public function doesCheckModifyAccessListHookGetsCalled() {
226 $hookClass = uniqid('tx_coretest');
227 $hookMock = $this->getMock(
228 't3lib_TCEmain_checkModifyAccessListHook',
229 array('checkModifyAccessList'),
230 array(),
231 $hookClass
232 );
233 $hookMock->expects($this->once())->method('checkModifyAccessList');
234
235 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList'][] = $hookClass;
236 $GLOBALS['T3_VAR']['getUserObj'][$hookClass] = $hookMock;
237
238 $this->fixture->checkModifyAccessList('tt_content');
239 }
240
241 /**
242 * Tests whether the 'checkModifyAccessList' hook modifies the $accessAllowed variable.
243 * @test
244 * @see t3lib_TCEmain::checkModifyAccessList()
245 */
246 public function doesCheckModifyAccessListHookModifyAccessAllowed() {
247 $hookClass = uniqid('tx_coretest');
248 eval('
249 class ' . $hookClass . ' implements t3lib_TCEmain_checkModifyAccessListHook {
250 public function checkModifyAccessList(&$accessAllowed, $table, t3lib_TCEmain $parent) { $accessAllowed = true; }
251 }
252 ');
253
254 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList'][] = $hookClass;
255
256 $this->assertTrue($this->fixture->checkModifyAccessList('tt_content'));
257 }
258 }
259 ?>