View source
<?php
namespace Drupal\Tests\Core\Common;
use Drupal\Core\Template\Attribute;
use Drupal\Tests\UnitTestCase;
class AttributesTest extends UnitTestCase {
public function providerTestAttributeData() {
return [
[
[
'&"\'<>' => 'value',
],
' &"'<>="value"',
'HTML encode attribute names.',
],
[
[
'title' => '&"\'<>',
],
' title="&"'<>"',
'HTML encode attribute values.',
],
[
[
'class' => [
'first',
'last',
],
],
' class="first last"',
'Concatenate multi-value attributes.',
],
[
[
'disabled' => TRUE,
],
' disabled',
'Boolean attribute is rendered.',
],
[
[
'disabled' => FALSE,
],
'',
'Boolean attribute is not rendered.',
],
[
[
'alt' => '',
],
' alt=""',
'Empty attribute value #1.',
],
[
[
'alt' => NULL,
],
'',
'Null attribute value #2.',
],
[
[
'id' => 'id-test',
'class' => [
'first',
'last',
],
'alt' => 'Alternate',
],
' id="id-test" class="first last" alt="Alternate"',
'Multiple attributes.',
],
[
[],
'',
'Empty attributes array.',
],
];
}
public function testDrupalAttributes($attributes, $expected, $message) {
$this
->assertSame($expected, (string) new Attribute($attributes), $message);
}
public function testAttributeIteration() {
$attribute = new Attribute([
'key1' => 'value1',
]);
foreach ($attribute as $value) {
$this
->assertSame((string) $value, 'value1', 'Iterate over attribute.');
}
}
public function testAttributeValueBaseCopy() {
$original_attributes = new Attribute([
'checked' => TRUE,
'class' => [
'who',
'is',
'on',
],
'id' => 'first',
]);
$attributes['selected'] = $original_attributes['checked'];
$attributes['id'] = $original_attributes['id'];
$attributes = new Attribute($attributes);
$this
->assertSame(' checked class="who is on" id="first"', (string) $original_attributes, 'Original boolean value used with original name.');
$this
->assertSame(' selected id="first"', (string) $attributes, 'Original boolean value used with new name.');
}
}