function AttributeTest::testRemoveAttribute

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::testRemoveAttribute()
  2. 10 core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::testRemoveAttribute()
  3. 11.x core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::testRemoveAttribute()

Tests removing attributes. @covers ::removeAttribute

File

core/tests/Drupal/Tests/Core/Template/AttributeTest.php, line 109

Class

AttributeTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Template%21Attribute.php/class/Attribute/9" title="Collects, sanitizes, and renders HTML attributes." class="local">\Drupal\Core\Template\Attribute</a> @group Template

Namespace

Drupal\Tests\Core\Template

Code

public function testRemoveAttribute() {
    $attributes = [
        'alt' => 'Alternative text',
        'id' => 'bunny',
        'src' => 'zebra',
        'style' => 'color: pink;',
        'title' => 'kitten',
        'value' => 'ostrich',
        'checked' => TRUE,
    ];
    $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']);
    // Boolean value.
    $attribute->removeAttribute('checked');
    $this->assertEmpty($attribute['checked']);
    // Multiple values in array.
    $attribute->removeAttribute([
        'title',
        'value',
    ]);
    $this->assertEmpty((string) $attribute);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.