You are here

public function AttributeTest::testRemoveClasses 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::testRemoveClasses()

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

File

core/tests/Drupal/Tests/Core/Template/AttributeTest.php, line 203
Contains \Drupal\Tests\Core\Template\AttributeTest.

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 = array(
    'example-class',
    'aa',
    'xx',
    'yy',
    'red',
    'green',
    'blue',
    'red',
  );
  $attribute = new Attribute(array(
    '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(array(
    'xx',
    'yy',
  ), $attribute['class']
    ->value());

  // Remove an array of classes.
  $attribute
    ->removeClass(array(
    'red',
    'green',
    'blue',
  ));
  $this
    ->assertNotContains(array(
    'red',
    'green',
    'blue',
  ), $attribute['class']
    ->value());

  // Remove a class that does not exist.
  $attribute
    ->removeClass('gg');
  $this
    ->assertNotContains(array(
    'gg',
  ), $attribute['class']
    ->value());

  // Test that the array index remains sequential.
  $this
    ->assertArrayEquals(array(
    'aa',
  ), $attribute['class']
    ->value());
  $attribute
    ->removeClass('aa');
  $this
    ->assertEmpty((string) $attribute);
}