diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php index b04eb51..785d506 100644 --- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -58,6 +58,67 @@ public function testRemove() { } /** + * Test setting an attributes. + * @covers ::setAttribute() + */ + public function testSetAttribute() { + $attribute = new Attribute(); + + // Test empty attribtues are not being set. + $attribute->setAttribute('id', NULL); + $this->assertEmpty($attribute['class']); + $attributes = array('alt', 'id', 'src', 'title', 'value', NULL, FALSE, ''); + foreach ($attributes as $key) { + // Test various permutations of adding values to empty Attribute objects. + foreach (array(NULL, FALSE, '') as $value) { + // Single value. + $attribute->setAttribute($key, $value); + $this->assertEmpty((string) $attribute); + } + } + + // Test adding various attributes. + $attributes = array('alt', 'id', 'src', 'title', 'value'); + foreach ($attributes as $key) { + $attribute = new Attribute(); + $attribute->setAttribute($key, 'kitten'); + $this->assertEquals('kitten', $attribute[$key]); + } + } + + /** + * Test removing of attributes. + */ + public function testRemoveAttribute() { + $attributes = array( + 'alt' => 'Alternative text', + 'id' => 'bunny', + 'src' => 'zebra', + 'style' => 'color: pink;', + 'title' => 'lama', + 'value' => 'ostrich', + ); + $attribute = new Attribute($attributes); + + // Single value. + $attribute->removeAttribute('alt'); + $this->assertEmpty($attribute['alt']); + + // Multiple values. + $attribute->removeAttribute('id', 'src'); + $this->assertEmpty($attribute['id']); + $this->assertEmpty($attribute['src']); + + // Single value in array. + $attribute->removeAttribute(['style']); + $this->assertEmpty($attribute['style']); + + // Multiple values in array. + $attribute->removeAttribute(['title', 'value']); + $this->assertEmpty((string) $attribute); + } + + /** * Tests adding class attributes with the AttributeArray helper method. * @covers ::addClass() */