818244832485734441c39c3f8960ab8042df6e23
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Render / ElementTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Render;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\Core\Render\Element;
8
9 /**
10  * @coversDefaultClass \Drupal\Core\Render\Element
11  * @group Render
12  */
13 class ElementTest extends UnitTestCase {
14
15   /**
16    * Tests the property() method.
17    */
18   public function testProperty() {
19     $this->assertTrue(Element::property('#property'));
20     $this->assertFalse(Element::property('property'));
21     $this->assertFalse(Element::property('property#'));
22   }
23
24   /**
25    * Tests the properties() method.
26    */
27   public function testProperties() {
28     $element = [
29       '#property1' => 'property1',
30       '#property2' => 'property2',
31       'property3' => 'property3',
32     ];
33
34     $properties = Element::properties($element);
35
36     $this->assertContains('#property1', $properties);
37     $this->assertContains('#property2', $properties);
38     $this->assertNotContains('property3', $properties);
39   }
40
41   /**
42    * Tests the child() method.
43    */
44   public function testChild() {
45     $this->assertFalse(Element::child('#property'));
46     $this->assertTrue(Element::child('property'));
47     $this->assertTrue(Element::child('property#'));
48   }
49
50   /**
51    * Tests the children() method.
52    */
53   public function testChildren() {
54     $element = [
55       'child2' => ['#weight' => 10],
56       'child1' => ['#weight' => 0],
57       'child3' => ['#weight' => 20],
58       '#property' => 'property',
59     ];
60
61     $expected = ['child2', 'child1', 'child3'];
62     $element_copy = $element;
63     $this->assertSame($expected, Element::children($element_copy));
64
65     // If #sorted is already set, no sorting should happen.
66     $element_copy = $element;
67     $element_copy['#sorted'] = TRUE;
68     $expected = ['child2', 'child1', 'child3'];
69     $this->assertSame($expected, Element::children($element_copy, TRUE));
70
71     // Test with weight sorting, #sorted property should be added.
72     $expected = ['child1', 'child2', 'child3'];
73     $element_copy = $element;
74     $this->assertSame($expected, Element::children($element_copy, TRUE));
75     $this->assertArrayHasKey('#sorted', $element_copy);
76     $this->assertTrue($element_copy['#sorted']);
77
78     // The order should stay the same if no weights present.
79     $element_no_weight = [
80       'child2' => [],
81       'child1' => [],
82       'child3' => [],
83       '#property' => 'property',
84     ];
85
86     $expected = ['child2', 'child1', 'child3'];
87     $this->assertSame($expected, Element::children($element_no_weight, TRUE));
88
89     // The order of children with same weight should be preserved.
90     $element_mixed_weight = [
91       'child5' => ['#weight' => 10],
92       'child3' => ['#weight' => -10],
93       'child1' => [],
94       'child4' => ['#weight' => 10],
95       'child2' => [],
96     ];
97
98     $expected = ['child3', 'child1', 'child2', 'child5', 'child4'];
99     $this->assertSame($expected, Element::children($element_mixed_weight, TRUE));
100   }
101
102   /**
103    * Tests the children() method with an invalid key.
104    */
105   public function testInvalidChildren() {
106     $element = [
107       'foo' => 'bar',
108     ];
109     $this->setExpectedException(\PHPUnit_Framework_Error::class, '"foo" is an invalid render array key');
110     Element::children($element);
111   }
112
113   /**
114    * Tests the children() method with an ignored key/value pair.
115    */
116   public function testIgnoredChildren() {
117     $element = [
118       'foo' => NULL,
119     ];
120     $this->assertSame([], Element::children($element));
121   }
122
123   /**
124    * Tests the visibleChildren() method.
125    *
126    * @param array $element
127    *   The test element array.
128    * @param array $expected_keys
129    *   The expected keys to be returned from Element::getVisibleChildren().
130    *
131    * @dataProvider providerVisibleChildren
132    */
133   public function testVisibleChildren(array $element, array $expected_keys) {
134     $this->assertSame($expected_keys, Element::getVisibleChildren($element));
135   }
136
137   /**
138    * Data provider for testVisibleChildren.
139    *
140    * @return array
141    */
142   public function providerVisibleChildren() {
143     return [
144       [['#property1' => '', '#property2' => []], []],
145       [['#property1' => '', 'child1' => []], ['child1']],
146       [['#property1' => '', 'child1' => [], 'child2' => ['#access' => TRUE]], ['child1', 'child2']],
147       [['#property1' => '', 'child1' => [], 'child2' => ['#access' => FALSE]], ['child1']],
148       'access_result_object_allowed' => [['#property1' => '', 'child1' => [], 'child2' => ['#access' => AccessResult::allowed()]], ['child1', 'child2']],
149       'access_result_object_forbidden' => [['#property1' => '', 'child1' => [], 'child2' => ['#access' => AccessResult::forbidden()]], ['child1']],
150       [['#property1' => '', 'child1' => [], 'child2' => ['#type' => 'textfield']], ['child1', 'child2']],
151       [['#property1' => '', 'child1' => [], 'child2' => ['#type' => 'value']], ['child1']],
152       [['#property1' => '', 'child1' => [], 'child2' => ['#type' => 'hidden']], ['child1']],
153     ];
154   }
155
156   /**
157    * Tests the setAttributes() method.
158    *
159    * @dataProvider providerTestSetAttributes
160    */
161   public function testSetAttributes($element, $map, $expected_element) {
162     Element::setAttributes($element, $map);
163     $this->assertSame($expected_element, $element);
164   }
165
166   /**
167    * Data provider for testSetAttributes().
168    */
169   public function providerTestSetAttributes() {
170     $base = ['#id' => 'id', '#class' => []];
171     return [
172       [$base, [], $base],
173       [$base, ['id', 'class'], $base + ['#attributes' => ['id' => 'id', 'class' => []]]],
174       [$base + ['#attributes' => ['id' => 'id-not-overwritten']], ['id', 'class'], $base + ['#attributes' => ['id' => 'id-not-overwritten', 'class' => []]]],
175     ];
176   }
177
178   /**
179    * @covers ::isEmpty
180    *
181    * @dataProvider providerTestIsEmpty
182    */
183   public function testIsEmpty(array $element, $expected) {
184     $this->assertSame(Element::isEmpty($element), $expected);
185   }
186
187   public function providerTestIsEmpty() {
188     return [
189       [[], TRUE],
190       [['#cache' => []], TRUE],
191       [['#cache' => ['tags' => ['foo']]], TRUE],
192       [['#cache' => ['contexts' => ['bar']]], TRUE],
193
194       [['#cache' => [], '#markup' => 'llamas are awesome'], FALSE],
195       [['#markup' => 'llamas are the most awesome ever'], FALSE],
196
197       [['#cache' => [], '#any_other_property' => TRUE], FALSE],
198       [['#any_other_property' => TRUE], FALSE],
199     ];
200   }
201
202 }