You are here

class DomHelperTraitTest in Embed 8

Tests \Drupal\embed\DomHelperTrait.

@group embed

Hierarchy

Expanded class hierarchy of DomHelperTraitTest

File

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

Namespace

Drupal\Tests\embed\Unit
View source
class DomHelperTraitTest extends UnitTestCase {
  use DomHelperTrait;

  /**
   * The DOM Document used for testing.
   *
   * @var \DOMDocument
   */
  protected $document;

  /**
   * The DOM Node used for testing.
   *
   * @var \DOMElement
   */
  protected $node;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $this->document = Html::load('<outer><test foo="bar" namespace:foo="bar"><test bar="foo"></test></test></outer>');
    $this->node = $this->document
      ->getElementsByTagName('body')
      ->item(0)->firstChild->firstChild;
  }

  /**
   * Tests DomHelperTrait::changeNodeName().
   */
  public function testChangeNodeName() {
    $this
      ->changeNodeName($this->node, 'tested');
    $this
      ->assertEquals($this->node->tagName, 'tested');
    $this
      ->assertEquals(Html::serialize($this->document), '<outer><tested foo="bar" namespace:foo="bar"><test bar="foo"></test></tested></outer>');
  }

  /**
   * Tests DomHelperTrait::setNodeContent().
   *
   * @dataProvider providerTestSetNodeContent
   */
  public function testSetNodeContent($content, $expected_output) {
    $this
      ->setNodeContent($this->node, $content);
    $this
      ->assertEquals(Html::serialize($this->document), $expected_output);
  }

  /**
   * @return array
   * @see ::testSetNodeContent()
   */
  public function providerTestSetNodeContent() {
    return [
      'empty' => [
        '',
        '<outer><test foo="bar" namespace:foo="bar"></test></outer>',
      ],
      'single node without children' => [
        '<div></div>',
        '<outer><test foo="bar" namespace:foo="bar"><div></div></test></outer>',
      ],
      'single node with children' => [
        '<div><replacement replaced="true" /></div>',
        '<outer><test foo="bar" namespace:foo="bar"><div><replacement replaced="true"></replacement></div></test></outer>',
      ],
      'multiple nodes' => [
        '<p>first</p><p>second</p>',
        '<outer><test foo="bar" namespace:foo="bar"><p>first</p><p>second</p></test></outer>',
      ],
      'multiple nodes, with a text node, comment node and element node' => [
        'Second <!-- comment --> <p>third</p>',
        '<outer><test foo="bar" namespace:foo="bar">Second <!-- comment --> <p>third</p></test></outer>',
      ],
    ];
  }

  /**
   * Test DomHelperTrait::replaceNodeContent().
   *
   * @dataProvider providerTestReplaceNodeContent
   */
  public function testReplaceNodeContent($content, $expected_output) {
    $this
      ->replaceNodeContent($this->node, $content);
    $this
      ->assertEquals($expected_output, Html::serialize($this->document));
  }

  /**
   * @return array
   * @see ::testReplaceNodeContent()
   */
  public function providerTestReplaceNodeContent() {
    return [
      'empty' => [
        '',
        '<outer></outer>',
      ],
      'single node without children' => [
        '<div></div>',
        '<outer><div></div></outer>',
      ],
      'single node with children' => [
        '<div><replacement replaced="true" /></div>',
        '<outer><div><replacement replaced="true"></replacement></div></outer>',
      ],
      'multiple nodes' => [
        '<p>first</p><p>second</p>',
        '<outer><p>first</p><p>second</p></outer>',
      ],
      'multiple nodes, with a text node, comment node and element node' => [
        'Second <!-- comment --> <p>third</p>',
        '<outer>Second <!-- comment --> <p>third</p></outer>',
      ],
    ];
  }

  /**
   * Test DomHelperTrait::getNodeAttributesAsArray().
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DomHelperTrait::changeNodeName protected function Rename a DOMNode tag.
DomHelperTrait::getNodeAttributesAsArray public function Convert the attributes on a DOMNode object to an array.
DomHelperTrait::replaceNodeContent protected function Replace the contents of a DOMNode.
DomHelperTrait::setNodeContent protected function Set the contents of a DOMNode.
DomHelperTraitTest::$document protected property The DOM Document used for testing.
DomHelperTraitTest::$node protected property The DOM Node used for testing.
DomHelperTraitTest::providerTestReplaceNodeContent public function
DomHelperTraitTest::providerTestSetNodeContent public function
DomHelperTraitTest::setUp public function Overrides UnitTestCase::setUp
DomHelperTraitTest::testChangeNodeName public function Tests DomHelperTrait::changeNodeName().
DomHelperTraitTest::testGetNodeAttributesAsArray public function Test DomHelperTrait::getNodeAttributesAsArray().
DomHelperTraitTest::testReplaceNodeContent public function Test DomHelperTrait::replaceNodeContent().
DomHelperTraitTest::testSetNodeContent public function Tests DomHelperTrait::setNodeContent().
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.