public function AttributeTest::testSetAttribute in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::testSetAttribute()
Tests setting attributes. @covers ::setAttribute
File
- core/
tests/ Drupal/ Tests/ Core/ Template/ AttributeTest.php, line 81
Class
- AttributeTest
- @coversDefaultClass \Drupal\Core\Template\Attribute @group Template
Namespace
Drupal\Tests\Core\TemplateCode
public function testSetAttribute() {
$attribute = new Attribute();
// Test adding various attributes.
$attributes = [
'alt',
'id',
'src',
'title',
'value',
];
foreach ($attributes as $key) {
foreach ([
'kitten',
'',
] as $value) {
$attribute = new Attribute();
$attribute
->setAttribute($key, $value);
$this
->assertEquals($value, $attribute[$key]);
}
}
// Test adding array to class.
$attribute = new Attribute();
$attribute
->setAttribute('class', [
'kitten',
'cat',
]);
$this
->assertEquals([
'kitten',
'cat',
], $attribute['class']
->value());
// Test adding boolean attributes.
$attribute = new Attribute();
$attribute['checked'] = TRUE;
$this
->assertTrue($attribute['checked']
->value());
}