[FEATURE] New TCA displayCond options BIT and !BIT
[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 of 1 against multi-value field of 5 returns true' => array(
112 'FIELD:content:BIT:1',
113 array('content' => '5'),
114 NULL,
115 TRUE
116 ),
117 'Field value comparison of 2 against multi-value field of 5 returns false' => array(
118 'FIELD:content:BIT:2',
119 array('content' => '5'),
120 NULL,
121 FALSE
122 ),
123 'Field value of 5 negated comparison against multi-value field of 5 returns false' => array(
124 'FIELD:content:!BIT:5',
125 array('content' => '5'),
126 NULL,
127 FALSE
128 ),
129 'Field value comparison for required value is false for different value' => array(
130 'FIELD:foo:REQ:FALSE',
131 array('foo' => 'bar'),
132 NULL,
133 FALSE,
134 ),
135 'Field value string not equal comparison' => array(
136 'FIELD:foo:!=:baz',
137 array('foo' => 'bar'),
138 NULL,
139 TRUE,
140 ),
141 'Field value in range' => array(
142 'FIELD:uid:-:3-42',
143 array('uid' => '23'),
144 NULL,
145 TRUE,
146 ),
147 'Field value greater than' => array(
148 'FIELD:uid:>=:42',
149 array('uid' => '23'),
150 NULL,
151 FALSE,
152 ),
153 'Flexform value invalid comparison' => array(
154 'FIELD:foo:=:bar',
155 array(
156 'foo' => array(
157 'vDEF' => 'bar'
158 ),
159 ),
160 'vDEF',
161 TRUE,
162 ),
163 'Flexform value valid comparison' => array(
164 'FIELD:parentRec.foo:=:bar',
165 array(
166 'parentRec' => array(
167 'foo' => 'bar'
168 ),
169 ),
170 'vDEF',
171 TRUE,
172 ),
173 'Field is value for default languge without flexform' => array(
174 'HIDE_L10N_SIBLINGS',
175 array(),
176 NULL,
177 FALSE,
178 ),
179 'Field is value for default languge with flexform' => array(
180 'HIDE_L10N_SIBLINGS',
181 array(),
182 'vDEF',
183 TRUE,
184 ),
185 'Field is value for default languge with sibling' => array(
186 'HIDE_L10N_SIBLINGS',
187 array(),
188 'vEN',
189 FALSE,
190 ),
191 'New is TRUE for new comparison with TRUE' => array(
192 'REC:NEW:TRUE',
193 array('uid' => NULL),
194 NULL,
195 TRUE,
196 ),
197 'New is FALSE for new comparison with FALSE' => array(
198 'REC:NEW:FALSE',
199 array('uid' => NULL),
200 NULL,
201 FALSE,
202 ),
203 'New is FALSE for not new element' => array(
204 'REC:NEW:TRUE',
205 array('uid' => 42),
206 NULL,
207 FALSE,
208 ),
209 'New is TRUE for not new element compared to FALSE' => array(
210 'REC:NEW:FALSE',
211 array('uid' => 42),
212 NULL,
213 TRUE,
214 ),
215 'Version is TRUE for versioned row' => array(
216 'VERSION:IS:TRUE',
217 array(
218 'uid' => 42,
219 'pid' => -1
220 ),
221 NULL,
222 TRUE,
223 ),
224 'Version is TRUE for not versioned row compared with FALSE' => array(
225 'VERSION:IS:FALSE',
226 array(
227 'uid' => 42,
228 'pid' => 1
229 ),
230 NULL,
231 TRUE,
232 ),
233 'Version is TRUE for NULL row compared with TRUE' => array(
234 'VERSION:IS:TRUE',
235 array(
236 'uid' => NULL,
237 'pid' => NULL,
238 ),
239 NULL,
240 FALSE,
241 ),
242 'Multiple conditions with AND compare to TRUE if all are OK' => array(
243 array(
244 'AND' => array(
245 'FIELD:testField:>:9',
246 'FIELD:testField:<:11',
247 ),
248 ),
249 array(
250 'testField' => 10
251 ),
252 NULL,
253 TRUE,
254 ),
255 'Multiple conditions with AND compare to FALSE if one fails' => array(
256 array(
257 'AND' => array(
258 'FIELD:testField:>:9',
259 'FIELD:testField:<:11',
260 )
261 ),
262 array(
263 'testField' => 99
264 ),
265 NULL,
266 FALSE,
267 ),
268 'Multiple conditions with OR compare to TRUE if one is OK' => array(
269 array(
270 'OR' => array(
271 'FIELD:testField:<:9',
272 'FIELD:testField:<:11',
273 ),
274 ),
275 array(
276 'testField' => 10
277 ),
278 NULL,
279 TRUE,
280 ),
281 'Multiple conditions with OR compare to FALSE is all fail' => array(
282 array(
283 'OR' => array(
284 'FIELD:testField:<:9',
285 'FIELD:testField:<:11',
286 ),
287 ),
288 array(
289 'testField' => 99
290 ),
291 NULL,
292 FALSE,
293 ),
294 'Multiple conditions without operator due to misconfiguration compare to TRUE' => array(
295 array(
296 '' => array(
297 'FIELD:testField:<:9',
298 'FIELD:testField:>:11',
299 )
300 ),
301 array(
302 'testField' => 99
303 ),
304 NULL,
305 TRUE,
306 ),
307 'Multiple nested conditions evaluate to TRUE' => array(
308 array(
309 'AND' => array(
310 'FIELD:testField:>:9',
311 'OR' => array(
312 'FIELD:testField:<:100',
313 'FIELD:testField:>:-100',
314 ),
315 ),
316 ),
317 array(
318 'testField' => 10
319 ),
320 NULL,
321 TRUE,
322 ),
323 'Multiple nested conditions evaluate to FALSE' => array(
324 array(
325 'AND' => array(
326 'FIELD:testField:>:9',
327 'OR' => array(
328 'FIELD:testField:<:100',
329 'FIELD:testField:>:-100',
330 ),
331 ),
332 ),
333 array(
334 'testField' => -999
335 ),
336 NULL,
337 FALSE,
338 ),
339 );
340 }
341
342 /**
343 * @param string $condition
344 * @param array $record
345 * @param string $flexformValueKey
346 * @param string $expectedResult
347 * @dataProvider conditionStringDataProvider
348 * @test
349 */
350 public function matchConditionStrings($condition, array $record, $flexformValueKey, $expectedResult) {
351 $this->assertEquals($expectedResult, $this->fixture->match($condition, $record, $flexformValueKey));
352 }
353
354 /**
355 * @test
356 */
357 public function matchHideForNonAdminsReturnsTrueIfBackendUserIsAdmin() {
358 /** @var $backendUserMock \TYPO3\CMS\Core\Authentication\BackendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject */
359 $backendUserMock = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
360 $backendUserMock
361 ->expects($this->once())
362 ->method('isAdmin')
363 ->will($this->returnValue(TRUE));
364 $GLOBALS['BE_USER'] = $backendUserMock;
365 $this->assertTrue($this->fixture->match('HIDE_FOR_NON_ADMINS'));
366 }
367
368 /**
369 * @test
370 */
371 public function matchHideForNonAdminsReturnsFalseIfBackendUserIsNotAdmin() {
372 /** @var $backendUserMock \TYPO3\CMS\Core\Authentication\BackendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject */
373 $backendUserMock = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
374 $backendUserMock
375 ->expects($this->once())
376 ->method('isAdmin')
377 ->will($this->returnValue(FALSE));
378 $GLOBALS['BE_USER'] = $backendUserMock;
379 $this->assertFalse($this->fixture->match('HIDE_FOR_NON_ADMINS'));
380 }
381
382 /**
383 * @test
384 */
385 public function matchHideL10NSiblingsExceptAdminReturnsTrueIfBackendUserIsAdmin() {
386 /** @var $backendUserMock \TYPO3\CMS\Core\Authentication\BackendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject */
387 $backendUserMock = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
388 $backendUserMock
389 ->expects($this->once())
390 ->method('isAdmin')
391 ->will($this->returnValue(TRUE));
392 $GLOBALS['BE_USER'] = $backendUserMock;
393 $this->assertTrue($this->fixture->match('HIDE_L10N_SIBLINGS:except_admin'), array(), 'vEN');
394 }
395
396 /**
397 * @test
398 */
399 public function matchHideL10NSiblingsExceptAdminReturnsFalseIfBackendUserIsNotAdmin() {
400 /** @var $backendUserMock \TYPO3\CMS\Core\Authentication\BackendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject */
401 $backendUserMock = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
402 $backendUserMock
403 ->expects($this->once())
404 ->method('isAdmin')
405 ->will($this->returnValue(FALSE));
406 $GLOBALS['BE_USER'] = $backendUserMock;
407 $this->assertFalse($this->fixture->match('HIDE_L10N_SIBLINGS:except_admin'), array(), 'vEN');
408 }
409 }
410 ?>