You are here

public function AttributeTest::testRemoveAttribute in Drupal 8

Same name and namespace in other branches
  1. 9 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()

Tests removing attributes. @covers ::removeAttribute

File

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

Class

AttributeTest
@coversDefaultClass \Drupal\Core\Template\Attribute @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);
}