890d17171ebca424b6c410e44d85d6d479840ea0
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Utility / SortArrayTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Utility;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Component\Utility\SortArray;
7
8 /**
9  * Tests the SortArray component.
10  *
11  * @group Utility
12  *
13  * @coversDefaultClass \Drupal\Component\Utility\SortArray
14  */
15 class SortArrayTest extends UnitTestCase {
16
17   /**
18    * Tests SortArray::sortByWeightElement() input against expected output.
19    *
20    * @dataProvider providerSortByWeightElement
21    * @covers ::sortByWeightElement
22    * @covers ::sortByKeyInt
23    *
24    * @param array $a
25    *   The first input array for the SortArray::sortByWeightElement() method.
26    * @param array $b
27    *   The second input array for the SortArray::sortByWeightElement().
28    * @param int $expected
29    *   The expected output from calling the method.
30    */
31   public function testSortByWeightElement($a, $b, $expected) {
32     $result = SortArray::sortByWeightElement($a, $b);
33     $this->assertBothNegativePositiveOrZero($expected, $result);
34   }
35
36   /**
37    * Data provider for SortArray::sortByWeightElement().
38    *
39    * @return array
40    *   An array of tests, matching the parameter inputs for
41    *   testSortByWeightElement.
42    *
43    * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByWeightElement()
44    */
45   public function providerSortByWeightElement() {
46     $tests = [];
47
48     // Weights set and equal.
49     $tests[] = [
50       ['weight' => 1],
51       ['weight' => 1],
52       0
53     ];
54
55     // Weights set and $a is less (lighter) than $b.
56     $tests[] = [
57       ['weight' => 1],
58       ['weight' => 2],
59       -1
60     ];
61
62     // Weights set and $a is greater (heavier) than $b.
63     $tests[] = [
64       ['weight' => 2],
65       ['weight' => 1],
66       1
67     ];
68
69     // Weights not set.
70     $tests[] = [
71       [],
72       [],
73       0
74     ];
75
76     // Weights for $b not set.
77     $tests[] = [
78       ['weight' => 1],
79       [],
80       1
81     ];
82
83     // Weights for $a not set.
84     $tests[] = [
85       [],
86       ['weight' => 1],
87       -1
88     ];
89
90     return $tests;
91   }
92
93   /**
94    * Tests SortArray::sortByWeightProperty() input against expected output.
95    *
96    * @dataProvider providerSortByWeightProperty
97    * @covers ::sortByWeightProperty
98    * @covers ::sortByKeyInt
99    *
100    * @param array $a
101    *   The first input array for the SortArray::sortByWeightProperty() method.
102    * @param array $b
103    *   The second input array for the SortArray::sortByWeightProperty().
104    * @param int $expected
105    *   The expected output from calling the method.
106    */
107   public function testSortByWeightProperty($a, $b, $expected) {
108     $result = SortArray::sortByWeightProperty($a, $b);
109     $this->assertBothNegativePositiveOrZero($expected, $result);
110   }
111
112   /**
113    * Data provider for SortArray::sortByWeightProperty().
114    *
115    * @return array
116    *   An array of tests, matching the parameter inputs for
117    *   testSortByWeightProperty.
118    *
119    * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByWeightProperty()
120    */
121   public function providerSortByWeightProperty() {
122     $tests = [];
123
124     // Weights set and equal.
125     $tests[] = [
126       ['#weight' => 1],
127       ['#weight' => 1],
128       0
129     ];
130
131     // Weights set and $a is less (lighter) than $b.
132     $tests[] = [
133       ['#weight' => 1],
134       ['#weight' => 2],
135       -1
136     ];
137
138     // Weights set and $a is greater (heavier) than $b.
139     $tests[] = [
140       ['#weight' => 2],
141       ['#weight' => 1],
142       1
143     ];
144
145     // Weights not set.
146     $tests[] = [
147       [],
148       [],
149       0
150     ];
151
152     // Weights for $b not set.
153     $tests[] = [
154       ['#weight' => 1],
155       [],
156       1
157     ];
158
159     // Weights for $a not set.
160     $tests[] = [
161       [],
162       ['#weight' => 1],
163       -1
164     ];
165
166     return $tests;
167   }
168
169   /**
170    * Tests SortArray::sortByTitleElement() input against expected output.
171    *
172    * @dataProvider providerSortByTitleElement
173    * @covers ::sortByTitleElement
174    * @covers ::sortByKeyString
175    *
176    * @param array $a
177    *   The first input item for comparison.
178    * @param array $b
179    *   The second item for comparison.
180    * @param int $expected
181    *   The expected output from calling the method.
182    */
183   public function testSortByTitleElement($a, $b, $expected) {
184     $result = SortArray::sortByTitleElement($a, $b);
185     $this->assertBothNegativePositiveOrZero($expected, $result);
186   }
187
188   /**
189    * Data provider for SortArray::sortByTitleElement().
190    *
191    * @return array
192    *   An array of tests, matching the parameter inputs for
193    *   testSortByTitleElement.
194    *
195    * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleElement()
196    */
197   public function providerSortByTitleElement() {
198     $tests = [];
199
200     // Titles set and equal.
201     $tests[] = [
202       ['title' => 'test'],
203       ['title' => 'test'],
204       0
205     ];
206
207     // Title $a not set.
208     $tests[] = [
209       [],
210       ['title' => 'test'],
211       -4
212     ];
213
214     // Title $b not set.
215     $tests[] = [
216       ['title' => 'test'],
217       [],
218       4
219     ];
220
221     // Titles set but not equal.
222     $tests[] = [
223       ['title' => 'test'],
224       ['title' => 'testing'],
225       -1
226     ];
227
228     // Titles set but not equal.
229     $tests[] = [
230       ['title' => 'testing'],
231       ['title' => 'test'],
232       1
233     ];
234
235     return $tests;
236   }
237
238   /**
239    * Tests SortArray::sortByTitleProperty() input against expected output.
240    *
241    * @dataProvider providerSortByTitleProperty
242    * @covers ::sortByTitleProperty
243    * @covers ::sortByKeyString
244    *
245    * @param array $a
246    *   The first input item for comparison.
247    * @param array $b
248    *   The second item for comparison.
249    * @param int $expected
250    *   The expected output from calling the method.
251    */
252   public function testSortByTitleProperty($a, $b, $expected) {
253     $result = SortArray::sortByTitleProperty($a, $b);
254     $this->assertBothNegativePositiveOrZero($expected, $result);
255   }
256
257   /**
258    * Data provider for SortArray::sortByTitleProperty().
259    *
260    * @return array
261    *   An array of tests, matching the parameter inputs for
262    *   testSortByTitleProperty.
263    *
264    * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleProperty()
265    */
266   public function providerSortByTitleProperty() {
267     $tests = [];
268
269     // Titles set and equal.
270     $tests[] = [
271       ['#title' => 'test'],
272       ['#title' => 'test'],
273       0
274     ];
275
276     // Title $a not set.
277     $tests[] = [
278       [],
279       ['#title' => 'test'],
280       -4
281     ];
282
283     // Title $b not set.
284     $tests[] = [
285       ['#title' => 'test'],
286       [],
287       4
288     ];
289
290     // Titles set but not equal.
291     $tests[] = [
292       ['#title' => 'test'],
293       ['#title' => 'testing'],
294       -1
295     ];
296
297     // Titles set but not equal.
298     $tests[] = [
299       ['#title' => 'testing'],
300       ['#title' => 'test'],
301       1
302     ];
303
304     return $tests;
305   }
306
307   /**
308    * Asserts that numbers are either both negative, both positive or both zero.
309    *
310    * The exact values returned by comparison functions differ between PHP
311    * versions and are considered an "implementation detail".
312    *
313    * @param int $expected
314    *   Expected comparison function return value.
315    * @param int $result
316    *   Actual comparison function return value.
317    */
318   protected function assertBothNegativePositiveOrZero($expected, $result) {
319     $this->assertTrue(is_numeric($expected) && is_numeric($result), 'Parameters are numeric.');
320     $this->assertTrue(($expected < 0 && $result < 0) || ($expected > 0 && $result > 0) || ($expected === 0 && $result === 0), 'Numbers are either both negative, both positive or both zero.');
321   }
322
323 }