function AttributeTest::testSetAttribute

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::testSetAttribute()
  2. 8.9.x core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::testSetAttribute()
  3. 11.x 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 83

Class

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

Namespace

Drupal\Tests\Core\Template

Code

public function testSetAttribute() : void {
    $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->assertEquals([
        'kitten',
        'cat',
    ], $attribute['class']->value());
    // Test adding boolean attributes.
    $attribute = new Attribute();
    $attribute['checked'] = TRUE;
    $this->assertTrue($attribute['checked']->value());
}

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