You are here

public function AttributeTest::testRemoveClasses in Drupal 9

Same name and namespace in other branches
  1. 8 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 200

Class

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

Namespace

Drupal\Tests\Core\Template

Code

public function testRemoveClasses() {

  // 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);
}