You are here

public function AttributeTest::testSetAttribute in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 84
Contains \Drupal\Tests\Core\Template\AttributeTest.

Class

AttributeTest
@coversDefaultClass \Drupal\Core\Template\Attribute @group Template

Namespace

Drupal\Tests\Core\Template

Code

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
    ->assertArrayEquals([
    'kitten',
    'cat',
  ], $attribute['class']
    ->value());

  // Test adding boolean attributes.
  $attribute = new Attribute();
  $attribute['checked'] = TRUE;
  $this
    ->assertTrue($attribute['checked']
    ->value());
}