You are here

public function DomHelperTraitTest::testGetNodeAttributesAsArray in Embed 8

Test DomHelperTrait::getNodeAttributesAsArray().

File

tests/src/Unit/DomHelperTraitTest.php, line 130

Class

DomHelperTraitTest
Tests \Drupal\embed\DomHelperTrait.

Namespace

Drupal\Tests\embed\Unit

Code

public function testGetNodeAttributesAsArray() {
  $attributes = $this
    ->getNodeAttributesAsArray($this->node);
  $this
    ->assertArrayEquals([
    'foo' => 'bar',
    'namespace:foo' => 'bar',
  ], $attributes);

  // Test more complex attributes with special characters.
  $string = "TEST: A <complex> 'encoded' \"JSON\" string";
  $object = [
    'nested' => [
      'array' => TRUE,
    ],
    'string' => $string,
  ];
  $html = '<test data-json-string=\'' . Json::encode($string) . '\' data-json-object=\'' . Json::encode($object) . '\'></test>';
  $document = Html::load($html);
  $node = $document
    ->getElementsByTagName('body')
    ->item(0)->firstChild;
  $attributes = $this
    ->getNodeAttributesAsArray($node);
  $this
    ->assertArrayEquals([
    'data-json-string' => $string,
    'data-json-object' => $object,
  ], $attributes);
}