View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\Core\Render;
use Drupal\Core\Access\AccessResult;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Render\Element;
class ElementTest extends UnitTestCase {
public function testProperty() {
$this
->assertTrue(Element::property('#property'));
$this
->assertFalse(Element::property('property'));
$this
->assertFalse(Element::property('property#'));
$this
->assertFalse(Element::property(0));
}
public function testProperties() {
$element = [
'#property1' => 'property1',
'#property2' => 'property2',
'property3' => 'property3',
0 => [],
];
$properties = Element::properties($element);
$this
->assertSame([
'#property1',
'#property2',
], $properties);
}
public function testChild() {
$this
->assertFalse(Element::child('#property'));
$this
->assertTrue(Element::child('property'));
$this
->assertTrue(Element::child('property#'));
}
public function testChildren() {
$element = [
'child2' => [
'#weight' => 10,
],
'child1' => [
'#weight' => 0,
],
'child3' => [
'#weight' => 20,
],
'#property' => 'property',
];
$expected = [
'child2',
'child1',
'child3',
];
$element_copy = $element;
$this
->assertSame($expected, Element::children($element_copy));
$element_copy = $element;
$element_copy['#sorted'] = TRUE;
$expected = [
'child2',
'child1',
'child3',
];
$this
->assertSame($expected, Element::children($element_copy, TRUE));
$expected = [
'child1',
'child2',
'child3',
];
$element_copy = $element;
$this
->assertSame($expected, Element::children($element_copy, TRUE));
$this
->assertArrayHasKey('#sorted', $element_copy);
$this
->assertTrue($element_copy['#sorted']);
$element_no_weight = [
'child2' => [],
'child1' => [],
'child3' => [],
'#property' => 'property',
];
$expected = [
'child2',
'child1',
'child3',
];
$this
->assertSame($expected, Element::children($element_no_weight, TRUE));
$element_mixed_weight = [
'child5' => [
'#weight' => 10,
],
'child3' => [
'#weight' => -10,
],
'child1' => [],
'child4' => [
'#weight' => 10,
],
'child2' => [],
];
$expected = [
'child3',
'child1',
'child2',
'child5',
'child4',
];
$this
->assertSame($expected, Element::children($element_mixed_weight, TRUE));
}
public function testInvalidChildren() {
$element = [
'foo' => 'bar',
];
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('"foo" is an invalid render array key. Value should be an array but got a string.');
Element::children($element);
}
public function testIgnoredChildren() {
$element = [
'foo' => NULL,
];
$this
->assertSame([], Element::children($element));
}
public function testVisibleChildren(array $element, array $expected_keys) {
$this
->assertSame($expected_keys, Element::getVisibleChildren($element));
}
public static function providerVisibleChildren() {
return [
[
[
'#property1' => '',
'#property2' => [],
],
[],
],
[
[
'#property1' => '',
'child1' => [],
],
[
'child1',
],
],
[
[
'#property1' => '',
'child1' => [],
'child2' => [
'#access' => TRUE,
],
],
[
'child1',
'child2',
],
],
[
[
'#property1' => '',
'child1' => [],
'child2' => [
'#access' => FALSE,
],
],
[
'child1',
],
],
'access_result_object_allowed' => [
[
'#property1' => '',
'child1' => [],
'child2' => [
'#access' => AccessResult::allowed(),
],
],
[
'child1',
'child2',
],
],
'access_result_object_forbidden' => [
[
'#property1' => '',
'child1' => [],
'child2' => [
'#access' => AccessResult::forbidden(),
],
],
[
'child1',
],
],
[
[
'#property1' => '',
'child1' => [],
'child2' => [
'#type' => 'textfield',
],
],
[
'child1',
'child2',
],
],
[
[
'#property1' => '',
'child1' => [],
'child2' => [
'#type' => 'value',
],
],
[
'child1',
],
],
[
[
'#property1' => '',
'child1' => [],
'child2' => [
'#type' => 'hidden',
],
],
[
'child1',
],
],
];
}
public function testSetAttributes($element, $map, $expected_element) {
Element::setAttributes($element, $map);
$this
->assertSame($expected_element, $element);
}
public static function providerTestSetAttributes() {
$base = [
'#id' => 'id',
'#class' => [],
];
return [
[
$base,
[],
$base,
],
[
$base,
[
'id',
'class',
],
$base + [
'#attributes' => [
'id' => 'id',
'class' => [],
],
],
],
[
$base + [
'#attributes' => [
'id' => 'id-not-overwritten',
],
],
[
'id',
'class',
],
$base + [
'#attributes' => [
'id' => 'id-not-overwritten',
'class' => [],
],
],
],
];
}
public function testIsEmpty(array $element, $expected) {
$this
->assertSame(Element::isEmpty($element), $expected);
}
public static function providerTestIsEmpty() {
return [
[
[],
TRUE,
],
[
[
'#attached' => [],
],
FALSE,
],
[
[
'#cache' => [],
],
TRUE,
],
[
[
'#weight' => [],
],
TRUE,
],
[
[
'#attached' => [],
'#cache' => [],
],
FALSE,
],
[
[
'#attached' => [],
'#weight' => [],
],
FALSE,
],
[
[
'#attached' => [],
'#weight' => [],
'#cache' => [],
],
FALSE,
],
[
[
'#cache' => [],
'#weight' => [],
],
TRUE,
],
[
[
'#cache' => [],
'#weight' => [],
'#any_other_property' => [],
],
FALSE,
],
[
[
'#attached' => [],
'#weight' => [],
'#cache' => [],
'#any_other_property' => [],
],
FALSE,
],
[
[
'#cache' => [],
'#weight' => [],
'#attached' => [],
],
FALSE,
],
[
[
'#cache' => [],
'#weight' => [],
],
TRUE,
],
[
[
'#weight' => [],
'#cache' => [],
],
TRUE,
],
[
[
'#cache' => [],
],
TRUE,
],
[
[
'#cache' => [
'tags' => [
'foo',
],
],
],
TRUE,
],
[
[
'#cache' => [
'contexts' => [
'bar',
],
],
],
TRUE,
],
[
[
'#cache' => [],
'#markup' => 'llamas are awesome',
],
FALSE,
],
[
[
'#markup' => 'llamas are the most awesome ever',
],
FALSE,
],
[
[
'#cache' => [],
'#any_other_property' => TRUE,
],
FALSE,
],
[
[
'#any_other_property' => TRUE,
],
FALSE,
],
];
}
public function testIsRenderArray($build, $expected) {
$this
->assertSame($expected, Element::isRenderArray($build));
}
public function dataProviderIsRenderArray() {
return [
'valid markup render array' => [
[
'#markup' => 'hello world',
],
TRUE,
],
'invalid "foo" string' => [
[
'foo',
'#markup' => 'hello world',
],
FALSE,
],
'null is not an array' => [
NULL,
FALSE,
],
'an empty array is not a render array' => [
[],
FALSE,
],
'funny enough a key with # is valid' => [
[
'#' => TRUE,
],
TRUE,
],
'nested arrays can be valid too' => [
[
'one' => [
2 => [
'#three' => 'charm!',
],
],
],
TRUE,
],
];
}
}