function AttributeTest::testRemoveClasses

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

Tests removing class attributes with the AttributeArray helper method. @covers ::removeClass

File

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

Class

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

Namespace

Drupal\Tests\Core\Template

Code

public function testRemoveClasses() : void {
    // Add duplicate class to ensure that both duplicates are removed.
    $classes = [
        'example-class',
        'aa',
        'xx',
        'yy',
        'red',
        'green',
        'blue',
        'red',
    ];
    $attribute = new Attribute([
        'class' => $classes,
    ]);
    // Remove one class.
    $attribute->removeClass('example-class');
    $this->assertNotContains('example-class', $attribute['class']->value());
    // Remove multiple classes.
    $attribute->removeClass('xx', 'yy');
    $this->assertNotContains([
        'xx',
        'yy',
    ], $attribute['class']->value());
    // Remove an array of classes.
    $attribute->removeClass([
        'red',
        'green',
        'blue',
    ]);
    $this->assertNotContains([
        'red',
        'green',
        'blue',
    ], $attribute['class']->value());
    // Remove a class that does not exist.
    $attribute->removeClass('gg');
    $this->assertNotContains([
        'gg',
    ], $attribute['class']->value());
    // Test that the array index remains sequential.
    $this->assertEquals([
        'aa',
    ], $attribute['class']->value());
    $attribute->removeClass('aa');
    $this->assertEmpty((string) $attribute);
}

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