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