Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
aba1cd8b EM |
2 | |
3 | /** | |
4 | * Class CRM_Utils_ArrayTest | |
acb109b7 | 5 | * @group headless |
aba1cd8b | 6 | */ |
6a488035 | 7 | class CRM_Utils_ArrayTest extends CiviUnitTestCase { |
6a488035 | 8 | |
00be9182 | 9 | public function testIndexArray() { |
6a488035 TO |
10 | $inputs = array(); |
11 | $inputs[] = array( | |
12 | 'lang' => 'en', | |
13 | 'msgid' => 'greeting', | |
17deafad | 14 | 'familiar' => FALSE, |
21dfd5f5 | 15 | 'value' => 'Hello', |
6a488035 TO |
16 | ); |
17 | $inputs[] = array( | |
18 | 'lang' => 'en', | |
19 | 'msgid' => 'parting', | |
21dfd5f5 | 20 | 'value' => 'Goodbye', |
6a488035 TO |
21 | ); |
22 | $inputs[] = array( | |
23 | 'lang' => 'fr', | |
24 | 'msgid' => 'greeting', | |
21dfd5f5 | 25 | 'value' => 'Bon jour', |
6a488035 TO |
26 | ); |
27 | $inputs[] = array( | |
28 | 'lang' => 'fr', | |
29 | 'msgid' => 'parting', | |
21dfd5f5 | 30 | 'value' => 'Au revoir', |
6a488035 TO |
31 | ); |
32 | $inputs[] = array( | |
33 | 'lang' => 'en', | |
34 | 'msgid' => 'greeting', | |
17deafad | 35 | 'familiar' => TRUE, |
21dfd5f5 | 36 | 'value' => 'Hey', |
6a488035 | 37 | ); |
928c5201 TO |
38 | $inputs[] = array( |
39 | 'msgid' => 'greeting', | |
40 | 'familiar' => TRUE, | |
21dfd5f5 | 41 | 'value' => 'Universal greeting', |
928c5201 | 42 | ); |
6a488035 TO |
43 | |
44 | $byLangMsgid = CRM_Utils_Array::index(array('lang', 'msgid'), $inputs); | |
45 | $this->assertEquals($inputs[4], $byLangMsgid['en']['greeting']); | |
46 | $this->assertEquals($inputs[1], $byLangMsgid['en']['parting']); | |
47 | $this->assertEquals($inputs[2], $byLangMsgid['fr']['greeting']); | |
48 | $this->assertEquals($inputs[3], $byLangMsgid['fr']['parting']); | |
928c5201 | 49 | $this->assertEquals($inputs[5], $byLangMsgid[NULL]['greeting']); |
6a488035 TO |
50 | } |
51 | ||
00be9182 | 52 | public function testCollect() { |
6a488035 TO |
53 | $arr = array( |
54 | array('catWord' => 'cat', 'dogWord' => 'dog'), | |
55 | array('catWord' => 'chat', 'dogWord' => 'chien'), | |
56 | array('catWord' => 'gato'), | |
57 | ); | |
58 | $expected = array('cat', 'chat', 'gato'); | |
59 | $this->assertEquals($expected, CRM_Utils_Array::collect('catWord', $arr)); | |
60 | ||
61 | $arr = array(); | |
6c6e6187 TO |
62 | $arr['en'] = (object) array('catWord' => 'cat', 'dogWord' => 'dog'); |
63 | $arr['fr'] = (object) array('catWord' => 'chat', 'dogWord' => 'chien'); | |
64 | $arr['es'] = (object) array('catWord' => 'gato'); | |
6a488035 TO |
65 | $expected = array('en' => 'cat', 'fr' => 'chat', 'es' => 'gato'); |
66 | $this->assertEquals($expected, CRM_Utils_Array::collect('catWord', $arr)); | |
67 | } | |
68 | ||
00be9182 | 69 | public function testProduct0() { |
17deafad TO |
70 | $actual = CRM_Utils_Array::product( |
71 | array(), | |
72 | array('base data' => 1) | |
73 | ); | |
74 | $this->assertEquals(array( | |
75 | array('base data' => 1), | |
76 | ), $actual); | |
77 | } | |
78 | ||
00be9182 | 79 | public function testProduct1() { |
17deafad TO |
80 | $actual = CRM_Utils_Array::product( |
81 | array('dim1' => array('a', 'b')), | |
82 | array('base data' => 1) | |
83 | ); | |
84 | $this->assertEquals(array( | |
85 | array('base data' => 1, 'dim1' => 'a'), | |
86 | array('base data' => 1, 'dim1' => 'b'), | |
87 | ), $actual); | |
88 | } | |
89 | ||
00be9182 | 90 | public function testProduct3() { |
17deafad TO |
91 | $actual = CRM_Utils_Array::product( |
92 | array('dim1' => array('a', 'b'), 'dim2' => array('alpha', 'beta'), 'dim3' => array('one', 'two')), | |
93 | array('base data' => 1) | |
94 | ); | |
95 | $this->assertEquals(array( | |
96 | array('base data' => 1, 'dim1' => 'a', 'dim2' => 'alpha', 'dim3' => 'one'), | |
97 | array('base data' => 1, 'dim1' => 'a', 'dim2' => 'alpha', 'dim3' => 'two'), | |
98 | array('base data' => 1, 'dim1' => 'a', 'dim2' => 'beta', 'dim3' => 'one'), | |
99 | array('base data' => 1, 'dim1' => 'a', 'dim2' => 'beta', 'dim3' => 'two'), | |
100 | array('base data' => 1, 'dim1' => 'b', 'dim2' => 'alpha', 'dim3' => 'one'), | |
101 | array('base data' => 1, 'dim1' => 'b', 'dim2' => 'alpha', 'dim3' => 'two'), | |
102 | array('base data' => 1, 'dim1' => 'b', 'dim2' => 'beta', 'dim3' => 'one'), | |
103 | array('base data' => 1, 'dim1' => 'b', 'dim2' => 'beta', 'dim3' => 'two'), | |
104 | ), $actual); | |
105 | } | |
82376c19 | 106 | |
00be9182 | 107 | public function testIsSubset() { |
82376c19 TO |
108 | $this->assertTrue(CRM_Utils_Array::isSubset(array(), array())); |
109 | $this->assertTrue(CRM_Utils_Array::isSubset(array('a'), array('a'))); | |
6c6e6187 TO |
110 | $this->assertTrue(CRM_Utils_Array::isSubset(array('a'), array('b', 'a', 'c'))); |
111 | $this->assertTrue(CRM_Utils_Array::isSubset(array('b', 'd'), array('a', 'b', 'c', 'd'))); | |
82376c19 TO |
112 | $this->assertFalse(CRM_Utils_Array::isSubset(array('a'), array())); |
113 | $this->assertFalse(CRM_Utils_Array::isSubset(array('a'), array('b'))); | |
6c6e6187 | 114 | $this->assertFalse(CRM_Utils_Array::isSubset(array('a'), array('b', 'c', 'd'))); |
82376c19 | 115 | } |
f63d0d11 | 116 | |
00be9182 | 117 | public function testRemove() { |
f63d0d11 CW |
118 | $data = array( |
119 | 'one' => 1, | |
120 | 'two' => 2, | |
121 | 'three' => 3, | |
122 | 'four' => 4, | |
123 | 'five' => 5, | |
124 | 'six' => 6, | |
125 | ); | |
126 | CRM_Utils_Array::remove($data, 'one', 'two', array('three', 'four'), 'five'); | |
127 | $this->assertEquals($data, array('six' => 6)); | |
128 | } | |
96025800 | 129 | |
393f41dd TO |
130 | public function testGetSetPathParts() { |
131 | $arr = array( | |
132 | 'one' => '1', | |
133 | 'two' => array( | |
134 | 'half' => 2, | |
135 | ), | |
136 | ); | |
137 | $this->assertEquals('1', CRM_Utils_Array::pathGet($arr, array('one'))); | |
138 | $this->assertEquals('2', CRM_Utils_Array::pathGet($arr, array('two', 'half'))); | |
139 | $this->assertEquals(NULL, CRM_Utils_Array::pathGet($arr, array('zoo', 'half'))); | |
140 | CRM_Utils_Array::pathSet($arr, array('zoo', 'half'), '3'); | |
141 | $this->assertEquals(3, CRM_Utils_Array::pathGet($arr, array('zoo', 'half'))); | |
142 | $this->assertEquals(3, $arr['zoo']['half']); | |
143 | } | |
144 | ||
6db70618 TO |
145 | public function getSortExamples() { |
146 | $red = array('label' => 'Red', 'id' => 1, 'weight' => '90'); | |
147 | $orange = array('label' => 'Orange', 'id' => 2, 'weight' => '70'); | |
148 | $yellow = array('label' => 'Yellow', 'id' => 3, 'weight' => '10'); | |
149 | $green = array('label' => 'Green', 'id' => 4, 'weight' => '70'); | |
150 | $blue = array('label' => 'Blue', 'id' => 5, 'weight' => '70'); | |
151 | ||
152 | $examples = array(); | |
153 | $examples[] = array( | |
154 | array( | |
155 | 'r' => $red, | |
156 | 'y' => $yellow, | |
157 | 'g' => $green, | |
158 | 'o' => $orange, | |
159 | 'b' => $blue, | |
160 | ), | |
161 | 'id', | |
162 | array( | |
163 | 'r' => $red, | |
164 | 'o' => $orange, | |
165 | 'y' => $yellow, | |
166 | 'g' => $green, | |
167 | 'b' => $blue, | |
168 | ), | |
169 | ); | |
170 | $examples[] = array( | |
171 | array( | |
172 | 'r' => $red, | |
173 | 'y' => $yellow, | |
174 | 'g' => $green, | |
175 | 'o' => $orange, | |
176 | 'b' => $blue, | |
177 | ), | |
178 | 'label', | |
179 | array( | |
180 | 'b' => $blue, | |
181 | 'g' => $green, | |
182 | 'o' => $orange, | |
183 | 'r' => $red, | |
184 | 'y' => $yellow, | |
185 | ), | |
186 | ); | |
187 | $examples[] = array( | |
188 | array( | |
189 | 'r' => $red, | |
190 | 'g' => $green, | |
191 | 'y' => $yellow, | |
192 | 'o' => $orange, | |
193 | 'b' => $blue, | |
194 | ), | |
195 | array('weight', 'id'), | |
196 | array( | |
197 | 'y' => $yellow, | |
198 | 'o' => $orange, | |
199 | 'g' => $green, | |
200 | 'b' => $blue, | |
201 | 'r' => $red, | |
202 | ), | |
203 | ); | |
204 | ||
205 | return $examples; | |
206 | } | |
207 | ||
208 | /** | |
209 | * @param array $array | |
210 | * @param string|array $field | |
211 | * @param $expected | |
212 | * @dataProvider getSortExamples | |
213 | */ | |
214 | public function testCrmArraySortByField($array, $field, $expected) { | |
215 | $actual = CRM_Utils_Array::crmArraySortByField($array, $field); | |
216 | ||
217 | // assertEquals() has nicer error output, but it's not precise about order. | |
218 | $this->assertEquals($expected, $actual); | |
219 | ||
220 | $aIter = new ArrayIterator($actual); | |
221 | $eIter = new ArrayIterator($expected); | |
222 | $this->assertEquals($eIter->count(), $aIter->count()); | |
223 | $pos = 0; | |
224 | while ($aIter->valid()) { | |
225 | $this->assertEquals($eIter->key(), $aIter->key(), "Keys at offset $pos do not match"); | |
226 | $this->assertEquals($eIter->current(), $aIter->current(), "Values at offset $pos do not match"); | |
227 | $aIter->next(); | |
228 | $eIter->next(); | |
229 | $pos++; | |
230 | } | |
231 | } | |
232 | ||
7d812442 CW |
233 | public function getRecursiveIssetExamples() { |
234 | return [ | |
235 | [ | |
236 | [[[], [0, 1, 2], []]], [0, 1, 2], TRUE, | |
237 | ], | |
238 | [ | |
239 | [[[], [0, 1, 2], []]], [0, 1, 3], FALSE, | |
240 | ], | |
241 | [ | |
242 | [], ['foo'], FALSE, | |
243 | ], | |
244 | [ | |
245 | [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'wrong'], FALSE, | |
246 | ], | |
247 | [ | |
248 | [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right'], TRUE, | |
249 | ], | |
250 | [ | |
251 | [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right', 'foo'], TRUE, | |
252 | ], | |
253 | ]; | |
254 | } | |
255 | ||
256 | /** | |
257 | * @param $array | |
258 | * @param $path | |
259 | * @param $expected | |
260 | * @dataProvider getRecursiveIssetExamples | |
261 | */ | |
262 | public function testRecursiveIsset($array, $path, $expected) { | |
263 | $result = CRM_Utils_Array::recursiveIsset($array, $path); | |
264 | $this->assertEquals($expected, $result); | |
265 | } | |
266 | ||
267 | public function getRecursiveValueExamples() { | |
268 | return [ | |
269 | [ | |
270 | [[[], [0, 1, 2], []]], [0, 1, 2], NULL, 2, | |
271 | ], | |
272 | [ | |
273 | [[[], [0, 1, 2], []]], [0, 1, 3], NULL, NULL, | |
274 | ], | |
275 | [ | |
276 | [], ['foo'], FALSE, FALSE, | |
277 | ], | |
278 | [ | |
279 | [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'wrong'], 'nada', 'nada', | |
280 | ], | |
281 | [ | |
282 | [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right'], NULL, ['foo' => 1, 'bar' => 2] | |
283 | ], | |
284 | [ | |
285 | [NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right', 'foo'], NULL, 1, | |
286 | ], | |
287 | ]; | |
288 | } | |
289 | ||
290 | /** | |
291 | * @param $array | |
292 | * @param $path | |
293 | * @param $expected | |
294 | * @dataProvider getRecursiveValueExamples | |
295 | */ | |
296 | public function testRecursiveValue($array, $path, $default, $expected) { | |
297 | $result = CRM_Utils_Array::recursiveValue($array, $path, $default); | |
298 | $this->assertEquals($expected, $result); | |
299 | } | |
300 | ||
f18ccb71 | 301 | /** |
302 | * Get values for build test. | |
303 | */ | |
304 | public function getBuildValueExamples() { | |
305 | return [ | |
306 | [ | |
307 | [0, 'email', 2, 'location'], [0 => ['email' => [2 => ['location' => 'llama']]]] | |
308 | ] | |
309 | ]; | |
310 | } | |
311 | ||
312 | /** | |
313 | * Test the build recursive function. | |
314 | * | |
315 | * @param $path | |
316 | * @param $expected | |
317 | * | |
318 | * @dataProvider getBuildValueExamples | |
319 | */ | |
320 | public function testBuildRecursiveValue($path, $expected) { | |
321 | $result = CRM_Utils_Array::recursiveBuild($path, 'llama'); | |
322 | $this->assertEquals($expected, $result); | |
323 | } | |
324 | ||
6a488035 | 325 | } |