3 namespace Drupal\Tests\Core\Render;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\Core\Render\Element;
10 * @coversDefaultClass \Drupal\Core\Render\Element
13 class ElementTest extends UnitTestCase {
16 * Tests the property() method.
18 public function testProperty() {
19 $this->assertTrue(Element::property('#property'));
20 $this->assertFalse(Element::property('property'));
21 $this->assertFalse(Element::property('property#'));
25 * Tests the properties() method.
27 public function testProperties() {
29 '#property1' => 'property1',
30 '#property2' => 'property2',
31 'property3' => 'property3'
34 $properties = Element::properties($element);
36 $this->assertContains('#property1', $properties);
37 $this->assertContains('#property2', $properties);
38 $this->assertNotContains('property3', $properties);
42 * Tests the child() method.
44 public function testChild() {
45 $this->assertFalse(Element::child('#property'));
46 $this->assertTrue(Element::child('property'));
47 $this->assertTrue(Element::child('property#'));
51 * Tests the children() method.
53 public function testChildren() {
55 'child2' => ['#weight' => 10],
56 'child1' => ['#weight' => 0],
57 'child3' => ['#weight' => 20],
58 '#property' => 'property',
61 $expected = ['child2', 'child1', 'child3'];
62 $element_copy = $element;
63 $this->assertSame($expected, Element::children($element_copy));
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));
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']);
78 // The order should stay the same if no weights present.
79 $element_no_weight = [
83 '#property' => 'property',
86 $expected = ['child2', 'child1', 'child3'];
87 $this->assertSame($expected, Element::children($element_no_weight, TRUE));
89 // The order of children with same weight should be preserved.
90 $element_mixed_weight = [
91 'child5' => ['#weight' => 10],
92 'child3' => ['#weight' => -10],
94 'child4' => ['#weight' => 10],
98 $expected = ['child3', 'child1', 'child2', 'child5', 'child4'];
99 $this->assertSame($expected, Element::children($element_mixed_weight, TRUE));
103 * Tests the children() method with an invalid key.
105 public function testInvalidChildren() {
109 $this->setExpectedException(\PHPUnit_Framework_Error::class, '"foo" is an invalid render array key');
110 Element::children($element);
114 * Tests the children() method with an ignored key/value pair.
116 public function testIgnoredChildren() {
120 $this->assertSame([], Element::children($element));
124 * Tests the visibleChildren() method.
126 * @param array $element
127 * The test element array.
128 * @param array $expected_keys
129 * The expected keys to be returned from Element::getVisibleChildren().
131 * @dataProvider providerVisibleChildren
133 public function testVisibleChildren(array $element, array $expected_keys) {
134 $this->assertSame($expected_keys, Element::getVisibleChildren($element));
138 * Data provider for testVisibleChildren.
142 public function providerVisibleChildren() {
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']],
157 * Tests the setAttributes() method.
159 * @dataProvider providerTestSetAttributes
161 public function testSetAttributes($element, $map, $expected_element) {
162 Element::setAttributes($element, $map);
163 $this->assertSame($expected_element, $element);
167 * Data provider for testSetAttributes().
169 public function providerTestSetAttributes() {
170 $base = ['#id' => 'id', '#class' => []];
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' => []]]],
181 * @dataProvider providerTestIsEmpty
183 public function testIsEmpty(array $element, $expected) {
184 $this->assertSame(Element::isEmpty($element), $expected);
187 public function providerTestIsEmpty() {
190 [['#cache' => []], TRUE],
191 [['#cache' => ['tags' => ['foo']]], TRUE],
192 [['#cache' => ['contexts' => ['bar']]], TRUE],
194 [['#cache' => [], '#markup' => 'llamas are awesome'], FALSE],
195 [['#markup' => 'llamas are the most awesome ever'], FALSE],
197 [['#cache' => [], '#any_other_property' => TRUE], FALSE],
198 [['#any_other_property' => TRUE], FALSE],