class AttributesTest
Tests the Drupal\Core\Template\Attribute functionality.
@group Common
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\PhpunitCompatibilityTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Common\AttributesTest extends \Drupal\Tests\UnitTestCase
 
 
Expanded class hierarchy of AttributesTest
File
- 
              core/
tests/ Drupal/ Tests/ Core/ Common/ AttributesTest.php, line 13  
Namespace
Drupal\Tests\Core\CommonView source
class AttributesTest extends UnitTestCase {
  
  /**
   * Provides data for the Attribute test.
   *
   * @return array
   */
  public function providerTestAttributeData() {
    return [
      // Verify that special characters are HTML encoded.
[
        [
          '&"\'<>' => 'value',
        ],
        ' &"'<>="value"',
        'HTML encode attribute names.',
      ],
      [
        [
          'title' => '&"\'<>',
        ],
        ' title="&"'<>"',
        'HTML encode attribute values.',
      ],
      // Verify multi-value attributes are concatenated with spaces.
[
        [
          'class' => [
            'first',
            'last',
          ],
        ],
        ' class="first last"',
        'Concatenate multi-value attributes.',
      ],
      // Verify boolean attribute values are rendered correctly.
[
        [
          'disabled' => TRUE,
        ],
        ' disabled',
        'Boolean attribute is rendered.',
      ],
      [
        [
          'disabled' => FALSE,
        ],
        '',
        'Boolean attribute is not rendered.',
      ],
      // Verify empty attribute values are rendered.
[
        [
          'alt' => '',
        ],
        ' alt=""',
        'Empty attribute value #1.',
      ],
      [
        [
          'alt' => NULL,
        ],
        '',
        'Null attribute value #2.',
      ],
      // Verify multiple attributes are rendered.
[
        [
          'id' => 'id-test',
          'class' => [
            'first',
            'last',
          ],
          'alt' => 'Alternate',
        ],
        ' id="id-test" class="first last" alt="Alternate"',
        'Multiple attributes.',
      ],
      // Verify empty attributes array is rendered.
[
        [],
        '',
        'Empty attributes array.',
      ],
    ];
  }
  
  /**
   * Tests casting an Attribute object to a string.
   *
   * @see \Drupal\Core\Template\Attribute::__toString()
   *
   * @dataProvider providerTestAttributeData
   */
  public function testDrupalAttributes($attributes, $expected, $message) {
    $this->assertSame($expected, (string) new Attribute($attributes), $message);
  }
  
  /**
   * Test attribute iteration
   */
  public function testAttributeIteration() {
    $attribute = new Attribute([
      'key1' => 'value1',
    ]);
    foreach ($attribute as $value) {
      $this->assertSame((string) $value, 'value1', 'Iterate over attribute.');
    }
  }
  
  /**
   * Test AttributeValueBase copy.
   */
  public function testAttributeValueBaseCopy() {
    $original_attributes = new Attribute([
      'checked' => TRUE,
      'class' => [
        'who',
        'is',
        'on',
      ],
      'id' => 'first',
    ]);
    $attributes['selected'] = $original_attributes['checked'];
    $attributes['id'] = $original_attributes['id'];
    $attributes = new Attribute($attributes);
    $this->assertSame(' checked class="who is on" id="first"', (string) $original_attributes, 'Original boolean value used with original name.');
    $this->assertSame(' selected id="first"', (string) $attributes, 'Original boolean value used with new name.');
  }
}
Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overrides | 
|---|---|---|---|---|---|
| AttributesTest::providerTestAttributeData | public | function | Provides data for the Attribute test. | ||
| AttributesTest::testAttributeIteration | public | function | Test attribute iteration | ||
| AttributesTest::testAttributeValueBaseCopy | public | function | Test AttributeValueBase copy. | ||
| AttributesTest::testDrupalAttributes | public | function | Tests casting an Attribute object to a string. | ||
| PhpunitCompatibilityTrait::getMock | Deprecated | public | function | Returns a mock object for the specified class using the available method. | |
| PhpunitCompatibilityTrait::setExpectedException | Deprecated | public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
| UnitTestCase::$randomGenerator | protected | property | The random generator. | ||
| UnitTestCase::$root | protected | property | The app root. | 1 | |
| UnitTestCase::assertArrayEquals | protected | function | Asserts if two arrays are equal by sorting them first. | ||
| UnitTestCase::getBlockMockWithMachineName | Deprecated | protected | function | Mocks a block with a block plugin. | 1 | 
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | ||
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | ||
| UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | ||
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | ||
| UnitTestCase::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | ||
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | ||
| UnitTestCase::randomMachineName | public | function | Generates a unique random string containing letters and numbers. | ||
| UnitTestCase::setUp | protected | function | 340 | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.