[FEATURE] Support multiple display conditions in TCA
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / ElementConditionMatcherTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form\Element;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Sebastian Michaelsen <michaelsen@t3seo.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 /**
28 * Test case
29 */
30 class ElementConditionMatcherTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
31
32 /**
33 * @var \TYPO3\CMS\Backend\Form\ElementConditionMatcher
34 */
35 protected $fixture;
36
37 /**
38 * Sets up this test case.
39 */
40 protected function setUp() {
41 $this->fixture = new \TYPO3\CMS\Backend\Form\ElementConditionMatcher();
42 }
43
44 /**
45 * Tears down this test case.
46 */
47 protected function tearDown() {
48 unset($this->fixture);
49 }
50
51 /**
52 * Returns data sets for the test matchConditionStrings
53 * Each dataset is an array with the following elements:
54 * - the condition string
55 * - the current record
56 * - the current flexform value key
57 * - the expected result
58 *
59 * @return array
60 */
61 public function conditionStringDataProvider() {
62 return array(
63 'Invalid condition string' => array(
64 'xINVALIDx:',
65 array(),
66 NULL,
67 FALSE,
68 ),
69 'Not loaded extension compares to loaded as FALSE' => array(
70 'EXT:neverloadedext:LOADED:TRUE',
71 array(),
72 NULL,
73 FALSE,
74 ),
75 'Not loaded extension compares to not loaded as TRUE' => array(
76 'EXT:neverloadedext:LOADED:FALSE',
77 array(),
78 NULL,
79 TRUE,
80 ),
81 'Loaded extension compares to TRUE' => array(
82 'EXT:backend:LOADED:TRUE',
83 array(),
84 NULL,
85 TRUE,
86 ),
87 'Loaded extension compares to FALSE' => array(
88 'EXT:backend:LOADED:FALSE',
89 array(),
90 NULL,
91 FALSE,
92 ),
93 'Field is not greater zero if not given' => array(
94 'FIELD:uid:>:0',
95 array(),
96 NULL,
97 FALSE,
98 ),
99 'Field is not equal 0 if not given' => array(
100 'FIELD:uid:=:0',
101 array(),
102 NULL,
103 FALSE,
104 ),
105 'Field value string comparison' => array(
106 'FIELD:foo:=:bar',
107 array('foo' => 'bar'),
108 NULL,
109 TRUE,
110 ),
111 'Field value comparison for required value is false for different value' => array(
112 'FIELD:foo:REQ:FALSE',
113 array('foo' => 'bar'),
114 NULL,
115 FALSE,
116 ),
117 'Field value string not equal comparison' => array(
118 'FIELD:foo:!=:baz',
119 array('foo' => 'bar'),
120 NULL,
121 TRUE,
122 ),
123 'Field value in range' => array(
124 'FIELD:uid:-:3-42',
125 array('uid' => '23'),
126 NULL,
127 TRUE,
128 ),
129 'Field value greater than' => array(
130 'FIELD:uid:>=:42',
131 array('uid' => '23'),
132 NULL,
133 FALSE,
134 ),
135 'Flexform value invalid comparison' => array(
136 'FIELD:foo:=:bar',
137 array(
138 'foo' => array(
139 'vDEF' => 'bar'
140 ),
141 ),
142 'vDEF',
143 TRUE,
144 ),
145 'Flexform value valid comparison' => array(
146 'FIELD:parentRec.foo:=:bar',
147 array(
148 'parentRec' => array(
149 'foo' => 'bar'
150 ),
151 ),
152 'vDEF',
153 TRUE,
154 ),
155 'Field is value for default languge without flexform' => array(
156 'HIDE_L10N_SIBLINGS',
157 array(),
158 NULL,
159 FALSE,
160 ),
161 'Field is value for default languge with flexform' => array(
162 'HIDE_L10N_SIBLINGS',
163 array(),
164 'vDEF',
165 TRUE,
166 ),
167 'Field is value for default languge with sibling' => array(
168 'HIDE_L10N_SIBLINGS',
169 array(),
170 'vEN',
171 FALSE,
172 ),
173 'New is TRUE for new comparison with TRUE' => array(
174 'REC:NEW:TRUE',
175 array('uid' => NULL),
176 NULL,
177 TRUE,
178 ),
179 'New is FALSE for new comparison with FALSE' => array(
180 'REC:NEW:FALSE',
181 array('uid' => NULL),
182 NULL,
183 FALSE,
184 ),
185 'New is FALSE for not new element' => array(
186 'REC:NEW:TRUE',
187 array('uid' => 42),
188 NULL,
189 FALSE,
190 ),
191 'New is TRUE for not new element compared to FALSE' => array(
192 'REC:NEW:FALSE',
193 array('uid' => 42),
194 NULL,
195 TRUE,
196 ),
197 'Version is TRUE for versioned row' => array(
198 'VERSION:IS:TRUE',
199 array(
200 'uid' => 42,
201 'pid' => -1
202 ),
203 NULL,
204 TRUE,
205 ),
206 'Version is TRUE for not versioned row compared with FALSE' => array(
207 'VERSION:IS:FALSE',
208 array(
209 'uid' => 42,
210 'pid' => 1
211 ),
212 NULL,
213 TRUE,
214 ),
215 'Version is TRUE for NULL row compared with TRUE' => array(
216 'VERSION:IS:TRUE',
217 array(
218 'uid' => NULL,
219 'pid' => NULL,
220 ),
221 NULL,
222 FALSE,
223 ),
224 'Multiple conditions with AND compare to TRUE if all are OK' => array(
225 array(
226 'AND' => array(
227 'FIELD:testField:>:9',
228 'FIELD:testField:<:11',
229 ),
230 ),
231 array(
232 'testField' => 10
233 ),
234 NULL,
235 TRUE,
236 ),
237 'Multiple conditions with AND compare to FALSE if one fails' => array(
238 array(
239 'AND' => array(
240 'FIELD:testField:>:9',
241 'FIELD:testField:<:11',
242 )
243 ),
244 array(
245 'testField' => 99
246 ),
247 NULL,
248 FALSE,
249 ),
250 'Multiple conditions with OR compare to TRUE if one is OK' => array(
251 array(
252 'OR' => array(
253 'FIELD:testField:<:9',
254 'FIELD:testField:<:11',
255 ),
256 ),
257 array(
258 'testField' => 10
259 ),
260 NULL,
261 TRUE,
262 ),
263 'Multiple conditions with OR compare to FALSE is all fail' => array(
264 array(
265 'OR' => array(
266 'FIELD:testField:<:9',
267 'FIELD:testField:<:11',
268 ),
269 ),
270 array(
271 'testField' => 99
272 ),
273 NULL,
274 FALSE,
275 ),
276 'Multiple conditions without operator due to misconfiguration compare to TRUE' => array(
277 array(
278 '' => array(
279 'FIELD:testField:<:9',
280 'FIELD:testField:>:11',
281 )
282 ),
283 array(
284 'testField' => 99
285 ),
286 NULL,
287 TRUE,
288 ),
289 'Multiple nested conditions evaluate to TRUE' => array(
290 array(
291 'AND' => array(
292 'FIELD:testField:>:9',
293 'OR' => array(
294 'FIELD:testField:<:100',
295 'FIELD:testField:>:-100',
296 ),
297 ),
298 ),
299 array(
300 'testField' => 10
301 ),
302 NULL,
303 TRUE,
304 ),
305 'Multiple nested conditions evaluate to FALSE' => array(
306 array(
307 'AND' => array(
308 'FIELD:testField:>:9',
309 'OR' => array(
310 'FIELD:testField:<:100',
311 'FIELD:testField:>:-100',
312 ),
313 ),
314 ),
315 array(
316 'testField' => -999
317 ),
318 NULL,
319 FALSE,
320 ),
321 );
322 }
323
324 /**
325 * @param string $condition
326 * @param array $record
327 * @param string $flexformValueKey
328 * @param string $expectedResult
329 * @dataProvider conditionStringDataProvider
330 * @test
331 */
332 public function matchConditionStrings($condition, array $record, $flexformValueKey, $expectedResult) {
333 $this->assertEquals($expectedResult, $this->fixture->match($condition, $record, $flexformValueKey));
334 }
335
336 /**
337 * @test
338 */
339 public function matchHideForNonAdminsReturnsTrueIfBackendUserIsAdmin() {
340 /** @var $backendUserMock \TYPO3\CMS\Core\Authentication\BackendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject */
341 $backendUserMock = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
342 $backendUserMock
343 ->expects($this->once())
344 ->method('isAdmin')
345 ->will($this->returnValue(TRUE));
346 $GLOBALS['BE_USER'] = $backendUserMock;
347 $this->assertTrue($this->fixture->match('HIDE_FOR_NON_ADMINS'));
348 }
349
350 /**
351 * @test
352 */
353 public function matchHideForNonAdminsReturnsFalseIfBackendUserIsNotAdmin() {
354 /** @var $backendUserMock \TYPO3\CMS\Core\Authentication\BackendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject */
355 $backendUserMock = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
356 $backendUserMock
357 ->expects($this->once())
358 ->method('isAdmin')
359 ->will($this->returnValue(FALSE));
360 $GLOBALS['BE_USER'] = $backendUserMock;
361 $this->assertFalse($this->fixture->match('HIDE_FOR_NON_ADMINS'));
362 }
363
364 /**
365 * @test
366 */
367 public function matchHideL10NSiblingsExceptAdminReturnsTrueIfBackendUserIsAdmin() {
368 /** @var $backendUserMock \TYPO3\CMS\Core\Authentication\BackendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject */
369 $backendUserMock = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
370 $backendUserMock
371 ->expects($this->once())
372 ->method('isAdmin')
373 ->will($this->returnValue(TRUE));
374 $GLOBALS['BE_USER'] = $backendUserMock;
375 $this->assertTrue($this->fixture->match('HIDE_L10N_SIBLINGS:except_admin'), array(), 'vEN');
376 }
377
378 /**
379 * @test
380 */
381 public function matchHideL10NSiblingsExceptAdminReturnsFalseIfBackendUserIsNotAdmin() {
382 /** @var $backendUserMock \TYPO3\CMS\Core\Authentication\BackendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject */
383 $backendUserMock = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
384 $backendUserMock
385 ->expects($this->once())
386 ->method('isAdmin')
387 ->will($this->returnValue(FALSE));
388 $GLOBALS['BE_USER'] = $backendUserMock;
389 $this->assertFalse($this->fixture->match('HIDE_L10N_SIBLINGS:except_admin'), array(), 'vEN');
390 }
391 }
392 ?>