public function AttributeTest::testAddClasses in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::testAddClasses()
Tests adding class attributes with the AttributeArray helper method. @covers ::addClass
File
- core/
tests/ Drupal/ Tests/ Core/ Template/ AttributeTest.php, line 151 - Contains \Drupal\Tests\Core\Template\AttributeTest.
Class
- AttributeTest
- @coversDefaultClass \Drupal\Core\Template\Attribute @group Template
Namespace
Drupal\Tests\Core\TemplateCode
public function testAddClasses() {
// Add empty Attribute object with no classes.
$attribute = new Attribute();
// Add no class on empty attribute.
$attribute
->addClass();
$this
->assertEmpty($attribute['class']);
// Test various permutations of adding values to empty Attribute objects.
foreach (array(
NULL,
FALSE,
'',
[],
) as $value) {
// Single value.
$attribute
->addClass($value);
$this
->assertEmpty((string) $attribute);
// Multiple values.
$attribute
->addClass($value, $value);
$this
->assertEmpty((string) $attribute);
// Single value in array.
$attribute
->addClass([
$value,
]);
$this
->assertEmpty((string) $attribute);
// Single value in arrays.
$attribute
->addClass([
$value,
], [
$value,
]);
$this
->assertEmpty((string) $attribute);
}
// Add one class on empty attribute.
$attribute
->addClass('banana');
$this
->assertArrayEquals(array(
'banana',
), $attribute['class']
->value());
// Add one class.
$attribute
->addClass('aa');
$this
->assertArrayEquals(array(
'banana',
'aa',
), $attribute['class']
->value());
// Add multiple classes.
$attribute
->addClass('xx', 'yy');
$this
->assertArrayEquals(array(
'banana',
'aa',
'xx',
'yy',
), $attribute['class']
->value());
// Add an array of classes.
$attribute
->addClass(array(
'red',
'green',
'blue',
));
$this
->assertArrayEquals(array(
'banana',
'aa',
'xx',
'yy',
'red',
'green',
'blue',
), $attribute['class']
->value());
// Add an array of duplicate classes.
$attribute
->addClass(array(
'red',
'green',
'blue',
), array(
'aa',
'aa',
'banana',
), 'yy');
$this
->assertEquals('banana aa xx yy red green blue', (string) $attribute['class']);
}