c715988eea5b546d0736c97f5e74938602e4a586
[lhc/web/wiklou.git] / tests / phpunit / includes / changes / ChangesListBooleanFilterTest.php
1 <?php
2
3 /**
4 * @covers ChangesListBooleanFilter
5 */
6 class ChangesListBooleanFilterTest extends MediaWikiTestCase {
7 public function testGetJsData() {
8 $group = new ChangesListBooleanFilterGroup( [
9 'name' => 'group',
10 'priority' => 2,
11 'filters' => [],
12 ] );
13
14 $definition = [
15 'group' => $group,
16 'label' => 'main-label',
17 'description' => 'main-description',
18 'default' => 1,
19 'priority' => 1,
20 ];
21
22 $fooFilter = new ChangesListBooleanFilter(
23 $definition + [ 'name' => 'hidefoo' ]
24 );
25
26 $barFilter = new ChangesListBooleanFilter(
27 $definition + [ 'name' => 'hidebar' ]
28 );
29
30 $bazFilter = new ChangesListBooleanFilter(
31 $definition + [ 'name' => 'hidebaz' ]
32 );
33
34 $fooFilter->conflictsWith(
35 $barFilter,
36 'foo-bar-global-conflict',
37 'foo-conflicts-bar',
38 'bar-conflicts-foo'
39 );
40
41 $fooFilter->setAsSupersetOf( $bazFilter, 'foo-superset-of-baz' );
42
43 $fooData = $fooFilter->getJsData();
44 $this->assertArrayEquals(
45 [
46 'name' => 'hidefoo',
47 'label' => 'main-label',
48 'description' => 'main-description',
49 'default' => 1,
50 'priority' => 1,
51 'cssClass' => null,
52 'conflicts' => [
53 [
54 'group' => 'group',
55 'filter' => 'hidebar',
56 'globalDescription' => 'foo-bar-global-conflict',
57 'contextDescription' => 'foo-conflicts-bar',
58 ]
59 ],
60 'subset' => [
61 [
62 'group' => 'group',
63 'filter' => 'hidebaz',
64 ],
65
66 ],
67 'messageKeys' => [
68 'main-label',
69 'main-description',
70 'foo-bar-global-conflict',
71 'foo-conflicts-bar',
72 ],
73 ],
74 $fooData,
75 /** ordered= */ false,
76 /** named= */ true
77 );
78
79 $barData = $barFilter->getJsData();
80 $this->assertArrayEquals(
81 [
82 'name' => 'hidebar',
83 'label' => 'main-label',
84 'description' => 'main-description',
85 'default' => 1,
86 'priority' => 1,
87 'cssClass' => null,
88 'conflicts' => [
89 [
90 'group' => 'group',
91 'filter' => 'hidefoo',
92 'globalDescription' => 'foo-bar-global-conflict',
93 'contextDescription' => 'bar-conflicts-foo',
94 ]
95 ],
96 'subset' => [],
97 'messageKeys' => [
98 'main-label',
99 'main-description',
100 'foo-bar-global-conflict',
101 'bar-conflicts-foo',
102 ],
103 ],
104 $barData,
105 /** ordered= */ false,
106 /** named= */ true
107 );
108 }
109
110 /**
111 * @expectedException MWException
112 * @expectedExceptionMessage Supersets can only be defined for filters in the same group
113 */
114 public function testSetAsSupersetOf() {
115 $groupA = new ChangesListBooleanFilterGroup( [
116 'name' => 'groupA',
117 'priority' => 2,
118 'filters' => [
119 [
120 'name' => 'foo',
121 'default' => false,
122 ],
123 [
124 'name' => 'bar',
125 'default' => false,
126 ]
127 ],
128 ] );
129
130 $groupB = new ChangesListBooleanFilterGroup( [
131 'name' => 'groupB',
132 'priority' => 3,
133 'filters' => [
134 [
135 'name' => 'baz',
136 'default' => true,
137 ],
138 ],
139 ] );
140
141 $foo = TestingAccessWrapper::newFromObject( $groupA->getFilter( 'foo' ) );
142
143 $bar = $groupA->getFilter( 'bar' );
144
145 $baz = $groupB->getFilter( 'baz' );
146
147 $foo->setAsSupersetOf( $bar );
148 $this->assertArrayEquals( [
149 [
150 'group' => 'groupA',
151 'filter' => 'bar',
152 ],
153 ],
154 $foo->subsetFilters,
155 /** ordered= */ false,
156 /** named= */ true
157 );
158
159 $foo->setAsSupersetOf( $baz, 'some-message' );
160 }
161
162 public function testIsFeatureAvailableOnStructuredUi() {
163 $specialPage = $this->getMockBuilder( 'ChangesListSpecialPage' )
164 ->setConstructorArgs( [
165 'ChangesListSpecialPage',
166 '',
167 ] )
168 ->getMockForAbstractClass();
169
170 $groupA = new ChangesListBooleanFilterGroup( [
171 'name' => 'groupA',
172 'priority' => 1,
173 'filters' => [],
174 ] );
175
176 $foo = new ChangesListBooleanFilter( [
177 'name' => 'hidefoo',
178 'group' => $groupA,
179 'label' => 'foo-label',
180 'description' => 'foo-description',
181 'default' => true,
182 'showHide' => 'showhidefoo',
183 'priority' => 2,
184 ] );
185
186 $this->assertEquals(
187 true,
188 $foo->isFeatureAvailableOnStructuredUi( $specialPage ),
189 'Same filter appears on both'
190 );
191
192 // Should only be legacy ones that haven't been ported yet
193 $bar = new ChangesListBooleanFilter( [
194 'name' => 'hidebar',
195 'default' => true,
196 'group' => $groupA,
197 'showHide' => 'showhidebar',
198 'priority' => 2,
199 ] );
200
201 $this->assertEquals(
202 false,
203 $bar->isFeatureAvailableOnStructuredUi( $specialPage ),
204 'Only on unstructured UI'
205 );
206
207 $baz = new ChangesListBooleanFilter( [
208 'name' => 'hidebaz',
209 'default' => true,
210 'group' => $groupA,
211 'showHide' => 'showhidebaz',
212 'isReplacedInStructuredUi' => true,
213 'priority' => 2,
214 ] );
215
216 $this->assertEquals(
217 true,
218 $baz->isFeatureAvailableOnStructuredUi( $specialPage ),
219 'Legacy filter does not appear directly in new UI, but equivalent ' .
220 'does and is marked with isReplacedInStructuredUi'
221 );
222 }
223 }