[TASK] Extract displayCond functionality from FormEngine to new class
[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:', array(), NULL, FALSE,
65 ),
66 'EXT (#1)' => array (
67 'EXT:neverloadedext:LOADED:TRUE', array(), NULL, FALSE
68 ),
69 'EXT (#2)' => array (
70 'EXT:neverloadedext:LOADED:FALSE', array(), NULL, TRUE
71 ),
72 'EXT (#3)' => array (
73 'EXT:backend:LOADED:TRUE', array(), NULL, TRUE
74 ),
75 'EXT (#4)' => array (
76 'EXT:backend:LOADED:FALSE', array(), NULL, FALSE
77 ),
78 'FIELD (#1)' => array(
79 'FIELD:uid:>:0', array(), NULL, FALSE
80 ),
81 'FIELD (#2)' => array(
82 'FIELD:uid:=:0', array(), NULL, FALSE
83 ),
84 'FIELD (#3)' => array(
85 'FIELD:foo:=:bar', array('foo' => 'bar'), NULL, TRUE
86 ),
87 'FIELD (#4)' => array(
88 'FIELD:foo:REQ:FALSE', array('foo' => 'bar'), NULL, FALSE
89 ),
90 'FIELD (#5)' => array(
91 'FIELD:foo:!=:baz', array('foo' => 'bar'), NULL, TRUE
92 ),
93 'FIELD (#6)' => array(
94 'FIELD:uid:-:3-42', array('uid' => '23'), NULL, TRUE
95 ),
96 'FIELD (#7)' => array(
97 'FIELD:uid:>=:42', array('uid' => '23'), NULL, FALSE
98 ),
99 'FIELD (#8)' => array(
100 'FIELD:foo:=:bar', array('foo' => array('vDEF' => 'bar')), 'vDEF', TRUE
101 ),
102 'FIELD (#9)' => array(
103 'FIELD:parentRec.foo:=:bar', array('parentRec' => array('foo' => 'bar')), 'vDEF', TRUE
104 ),
105 'HIDE_L10N_SIBLINGS (#1)' => array(
106 'HIDE_L10N_SIBLINGS', array(), NULL, FALSE
107 ),
108 'HIDE_L10N_SIBLINGS (#2)' => array(
109 'HIDE_L10N_SIBLINGS', array(), 'vDEF', TRUE
110 ),
111 'HIDE_L10N_SIBLINGS (#3)' => array(
112 'HIDE_L10N_SIBLINGS', array(), 'vEN', FALSE
113 ),
114 'REC (#1)' => array(
115 'REC:NEW:TRUE', array('uid' => NULL), NULL, TRUE
116 ),
117 'REC (#2)' => array(
118 'REC:NEW:FALSE', array('uid' => NULL), NULL, FALSE
119 ),
120 'REC (#3)' => array(
121 'REC:NEW:TRUE', array('uid' => 42), NULL, FALSE
122 ),
123 'REC (#4)' => array(
124 'REC:NEW:FALSE', array('uid' => 42), NULL, TRUE
125 ),
126 'VERSION (#1)' => array(
127 'VERSION:IS:TRUE', array('uid' => 42, 'pid' => -1), NULL, TRUE
128 ),
129 'VERSION (#2)' => array(
130 'VERSION:IS:FALSE', array('uid' => 42, 'pid' => 1), NULL, TRUE
131 ),
132 'VERSION (#3)' => array(
133 'VERSION:IS:TRUE', array('uid' => NULL, 'pid' => NULL), NULL, FALSE
134 ),
135 );
136 }
137
138 /**
139 * @param string $condition
140 * @param array $record
141 * @param string $flexformValueKey
142 * @param string $expectedResult
143 * @dataProvider conditionStringDataProvider
144 * @test
145 */
146 public function matchConditionStrings($condition, array $record, $flexformValueKey, $expectedResult) {
147 $this->assertEquals($expectedResult, $this->fixture->match($condition, $record, $flexformValueKey));
148 }
149
150 /**
151 * @test
152 */
153 public function matchHideForNonAdminsReturnsTrueIfBackendUserIsAdmin() {
154 /** @var $backendUserMock \TYPO3\CMS\Core\Authentication\BackendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject */
155 $backendUserMock = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
156 $backendUserMock
157 ->expects($this->once())
158 ->method('isAdmin')
159 ->will($this->returnValue(TRUE));
160 $GLOBALS['BE_USER'] = $backendUserMock;
161 $this->assertTrue($this->fixture->match('HIDE_FOR_NON_ADMINS'));
162 }
163
164 /**
165 * @test
166 */
167 public function matchHideForNonAdminsReturnsFalseIfBackendUserIsNotAdmin() {
168 /** @var $backendUserMock \TYPO3\CMS\Core\Authentication\BackendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject */
169 $backendUserMock = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
170 $backendUserMock
171 ->expects($this->once())
172 ->method('isAdmin')
173 ->will($this->returnValue(FALSE));
174 $GLOBALS['BE_USER'] = $backendUserMock;
175 $this->assertFalse($this->fixture->match('HIDE_FOR_NON_ADMINS'));
176 }
177
178 /**
179 * @test
180 */
181 public function matchHideL10NSiblingsExceptAdminReturnsTrueIfBackendUserIsAdmin() {
182 /** @var $backendUserMock \TYPO3\CMS\Core\Authentication\BackendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject */
183 $backendUserMock = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
184 $backendUserMock
185 ->expects($this->once())
186 ->method('isAdmin')
187 ->will($this->returnValue(TRUE));
188 $GLOBALS['BE_USER'] = $backendUserMock;
189 $this->assertTrue($this->fixture->match('HIDE_L10N_SIBLINGS:except_admin'), array(), 'vEN');
190 }
191
192 /**
193 * @test
194 */
195 public function matchHideL10NSiblingsExceptAdminReturnsFalseIfBackendUserIsNotAdmin() {
196 /** @var $backendUserMock \TYPO3\CMS\Core\Authentication\BackendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject */
197 $backendUserMock = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
198 $backendUserMock
199 ->expects($this->once())
200 ->method('isAdmin')
201 ->will($this->returnValue(FALSE));
202 $GLOBALS['BE_USER'] = $backendUserMock;
203 $this->assertFalse($this->fixture->match('HIDE_L10N_SIBLINGS:except_admin'), array(), 'vEN');
204 }
205 }
206 ?>