[TASK] Update caching framework to latest FLOW3 version
[Packages/TYPO3.CMS.git] / tests / t3lib / class.t3lib_tcemainTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-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 /**
26 * Testcase for the t3lib_TCEmain class in the TYPO3 core.
27 *
28 * @package TYPO3
29 * @subpackage t3lib
30 *
31 * @author Oliver Klee <typo3-coding@oliverklee.de>
32 * @author Tolleiv Nietsch <info@tolleiv.de>
33 */
34 class t3lib_tcemainTest extends tx_phpunit_testcase {
35
36 /**
37 * Enable backup of global and system variables
38 *
39 * @var boolean
40 */
41 protected $backupGlobals = TRUE;
42
43 /**
44 * Exclude TYPO3_DB from backup/ restore of $GLOBALS
45 * because resource types cannot be handled during serializing
46 *
47 * @var array
48 */
49 protected $backupGlobalsBlacklist = array('TYPO3_DB');
50
51 /**
52 * a backup of the global database
53 *
54 * @var t3lib_DB
55 */
56 protected $databaseBackup = NULL;
57
58 /**
59 * @var t3lib_TCEmain
60 */
61 private $fixture;
62
63 /**
64 * @var t3lib_beUserAuth a mock logged-in back-end user
65 */
66 private $backEndUser;
67
68 public function setUp() {
69 $this->databaseBackup = $GLOBALS['TYPO3_DB'];
70
71 $this->backEndUser = $this->getMock('t3lib_beUserAuth');
72
73 $this->fixture = new t3lib_TCEmain();
74 $this->fixture->start(array(), '', $this->backEndUser);
75 }
76
77 public function tearDown() {
78 t3lib_div::purgeInstances();
79
80 $GLOBALS['TYPO3_DB'] = $this->databaseBackup;
81
82 unset(
83 $this->fixture->BE_USER, $this->fixture, $this->backEndUser, $this->databaseBackup
84 );
85 }
86
87
88 //////////////////////////////////////
89 // Tests for the basic functionality
90 //////////////////////////////////////
91
92 /**
93 * @test
94 */
95 public function fixtureCanBeCreated() {
96 $this->assertTrue(
97 $this->fixture instanceof t3lib_TCEmain
98 );
99 }
100
101
102 //////////////////////////////////////////
103 // Test concerning checkModifyAccessList
104 //////////////////////////////////////////
105
106 /**
107 * @test
108 */
109 public function adminIsAllowedToModifyNonAdminTable() {
110 $this->fixture->admin = TRUE;
111
112 $this->assertTrue(
113 $this->fixture->checkModifyAccessList('tt_content')
114 );
115 }
116
117 /**
118 * @test
119 */
120 public function nonAdminIsNorAllowedToModifyNonAdminTable() {
121 $this->fixture->admin = FALSE;
122
123 $this->assertFalse(
124 $this->fixture->checkModifyAccessList('tt_content')
125 );
126 }
127
128 /**
129 * @test
130 */
131 public function nonAdminWithTableModifyAccessIsAllowedToModifyNonAdminTable() {
132 $this->fixture->admin = FALSE;
133 $this->backEndUser->groupData['tables_modify'] = 'tt_content';
134
135 $this->assertTrue(
136 $this->fixture->checkModifyAccessList('tt_content')
137 );
138 }
139
140 /**
141 * @test
142 */
143 public function adminIsAllowedToModifyAdminTable() {
144 $this->fixture->admin = TRUE;
145
146 $this->assertTrue(
147 $this->fixture->checkModifyAccessList('be_users')
148 );
149 }
150
151 /**
152 * @test
153 */
154 public function nonAdminIsNotAllowedToModifyAdminTable() {
155 $this->fixture->admin = FALSE;
156
157 $this->assertFalse(
158 $this->fixture->checkModifyAccessList('be_users')
159 );
160 }
161
162 /**
163 * @test
164 */
165 public function nonAdminWithTableModifyAccessIsNotAllowedToModifyAdminTable() {
166 $this->fixture->admin = FALSE;
167 $this->backEndUser->groupData['tables_modify'] = 'be_users';
168
169 $this->assertFalse(
170 $this->fixture->checkModifyAccessList('be_users')
171 );
172 }
173
174 /**
175 * @test
176 */
177 public function evalCheckValueDouble2() {
178 $testData = array (
179 '-0,5' => '-0.50',
180 '1000' => '1000.00',
181 '1000,10' => '1000.10',
182 '1000,0' => '1000.00',
183 '600.000.000,00' => '600000000.00',
184 '60aaa00' => '6000.00',
185 );
186 foreach ($testData as $value => $expectedReturnValue){
187 $returnValue = $this->fixture->checkValue_input_Eval($value, array('double2'), '');
188 $this->assertSame(
189 $returnValue['value'],
190 $expectedReturnValue
191 );
192 }
193 }
194
195
196 ///////////////////////////////////////////
197 // Tests concerning checkModifyAccessList
198 ///////////////////////////////////////////
199
200 /**
201 * Tests whether a wrong interface on the 'checkModifyAccessList' hook throws an exception.
202 * @test
203 * @expectedException UnexpectedValueException
204 * @see t3lib_TCEmain::checkModifyAccessList()
205 */
206 public function doesCheckModifyAccessListThrowExceptionOnWrongHookInterface() {
207 $hookClass = uniqid('tx_coretest');
208 eval('class ' . $hookClass . ' {}');
209
210 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList'][] = $hookClass;
211
212 $this->fixture->checkModifyAccessList('tt_content');
213 }
214
215 /**
216 * Tests whether the 'checkModifyAccessList' hook is called correctly.
217 * @test
218 * @see t3lib_TCEmain::checkModifyAccessList()
219 */
220 public function doesCheckModifyAccessListHookGetsCalled() {
221 $hookClass = uniqid('tx_coretest');
222 $hookMock = $this->getMock(
223 't3lib_TCEmain_checkModifyAccessListHook',
224 array('checkModifyAccessList'),
225 array(),
226 $hookClass
227 );
228 $hookMock->expects($this->once())->method('checkModifyAccessList');
229
230 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList'][] = $hookClass;
231 $GLOBALS['T3_VAR']['getUserObj'][$hookClass] = $hookMock;
232
233 $this->fixture->checkModifyAccessList('tt_content');
234 }
235
236 /**
237 * Tests whether the 'checkModifyAccessList' hook modifies the $accessAllowed variable.
238 * @test
239 * @see t3lib_TCEmain::checkModifyAccessList()
240 */
241 public function doesCheckModifyAccessListHookModifyAccessAllowed() {
242 $hookClass = uniqid('tx_coretest');
243 eval('
244 class ' . $hookClass . ' implements t3lib_TCEmain_checkModifyAccessListHook {
245 public function checkModifyAccessList(&$accessAllowed, $table, t3lib_TCEmain $parent) { $accessAllowed = TRUE; }
246 }
247 ');
248
249 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList'][] = $hookClass;
250
251 $this->assertTrue($this->fixture->checkModifyAccessList('tt_content'));
252 }
253
254
255 /////////////////////////////////////
256 // Tests concerning process_datamap
257 /////////////////////////////////////
258
259 /**
260 * @test
261 */
262 public function processDatamapForFrozenNonZeroWorkspaceReturnsFalse() {
263 $fixture = $this->getMock('t3lib_TCEmain', array('newlog'));
264
265 $this->backEndUser->workspace = 1;
266 $this->backEndUser->workspaceRec = array('freeze' => TRUE);
267
268 $fixture->BE_USER = $this->backEndUser;
269
270 $this->assertFalse(
271 $fixture->process_datamap()
272 );
273 }
274
275 /**
276 * @test
277 */
278 public function processDatamapWhenEditingRecordInWorkspaceCreatesNewRecordInWorkspace() {
279 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB');
280
281 $fixture = $this->getMock(
282 't3lib_TCEmain',
283 array('newlog', 'checkModifyAccessList', 'tableReadOnly', 'checkRecordUpdateAccess')
284 );
285 $fixture->bypassWorkspaceRestrictions = FALSE;
286 $fixture->datamap = array(
287 'pages' => array(
288 '1' => array(
289 'header' => 'demo',
290 ),
291 ),
292 );
293
294 $fixture->expects($this->once())->method('checkModifyAccessList')->with('pages')->will($this->returnValue(TRUE));
295 $fixture->expects($this->once())->method('tableReadOnly')->with('pages')->will($this->returnValue(FALSE));
296 $fixture->expects($this->once())->method('checkRecordUpdateAccess')->will($this->returnValue(TRUE));
297
298 $backEndUser = $this->getMock('t3lib_beUserAuth');
299 $backEndUser->workspace = 1;
300 $backEndUser->workspaceRec = array('freeze' => FALSE);
301 $backEndUser->expects($this->once())->method('workspaceAllowAutoCreation')->will($this->returnValue(TRUE));
302 $backEndUser->expects($this->once())->method('workspaceCannotEditRecord')->will($this->returnValue(TRUE));
303 $backEndUser->expects($this->once())->method('recordEditAccessInternals')->with('pages', 1)->will($this->returnValue(TRUE));
304
305 $fixture->BE_USER = $backEndUser;
306
307 $createdTceMain = $this->getMock('t3lib_TCEmain', array());
308 $createdTceMain->expects($this->once())->method('start')->with(
309 array(),
310 array('pages' => array(
311 1 => array(
312 'version' => array(
313 'action' => 'new',
314 'treeLevels' => -1,
315 'label' => 'Auto-created for WS #1',
316 )
317 )
318 ))
319 );
320 $createdTceMain->expects($this->never())->method('process_datamap');
321 $createdTceMain->expects($this->once())->method('process_cmdmap');
322 t3lib_div::addInstance('t3lib_TCEmain', $createdTceMain);
323
324 $fixture->process_datamap();
325 }
326 }
327 ?>