function AttributesUnitTest::testDrupalAttributes

Tests that drupal_html_class() cleans the class name properly.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Common/AttributesUnitTest.php, line 28
Definition of Drupal\system\Tests\Common\AttributesUnitTest.

Class

AttributesUnitTest
Tests the Drupal\Core\Template\Attribute functionality.

Namespace

Drupal\system\Tests\Common

Code

function testDrupalAttributes() {

  // Verify that special characters are HTML encoded.
  $this
    ->assertIdentical((string) new Attribute(array(
    'title' => '&"\'<>',
  )), ' title="&amp;&quot;&#039;&lt;&gt;"', 'HTML encode attribute values.');

  // Verify multi-value attributes are concatenated with spaces.
  $attributes = array(
    'class' => array(
      'first',
      'last',
    ),
  );
  $this
    ->assertIdentical((string) new Attribute(array(
    'class' => array(
      'first',
      'last',
    ),
  )), ' class="first last"', 'Concatenate multi-value attributes.');

  // Verify empty attribute values are rendered.
  $this
    ->assertIdentical((string) new Attribute(array(
    'alt' => '',
  )), ' alt=""', 'Empty attribute value #1.');
  $this
    ->assertIdentical((string) new Attribute(array(
    'alt' => NULL,
  )), ' alt=""', 'Empty attribute value #2.');

  // Verify multiple attributes are rendered.
  $attributes = array(
    'id' => 'id-test',
    'class' => array(
      'first',
      'last',
    ),
    'alt' => 'Alternate',
  );
  $this
    ->assertIdentical((string) new Attribute($attributes), ' id="id-test" class="first last" alt="Alternate"', 'Multiple attributes.');

  // Verify empty attributes array is rendered.
  $this
    ->assertIdentical((string) new Attribute(array()), '', 'Empty attributes array.');
  $attributes_array = array(
    'key1' => 'value1',
  );
  $attribute = new Attribute($attributes_array);
  foreach ($attribute as $value) {
    $this
      ->assertIdentical((string) $value, 'value1', 'Iterate over attribute.');
  }
}