3 namespace Drupal\Tests\embed\Unit;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Serialization\Json;
7 use Drupal\Tests\UnitTestCase;
8 use Drupal\embed\DomHelperTrait;
11 * Tests \Drupal\embed\DomHelperTrait.
15 class DomHelperTraitTest extends UnitTestCase {
19 * The DOM Document used for testing.
26 * The DOM Node used for testing.
35 public function setUp() {
36 $this->document = Html::load('<outer><test foo="bar" namespace:foo="bar"><test bar="foo"></test></test></outer>');
37 $this->node = $this->document->getElementsByTagName('body')->item(0)->firstChild->firstChild;
41 * Tests DomHelperTrait::changeNodeName().
43 public function testChangeNodeName() {
44 $this->changeNodeName($this->node, 'tested');
45 $this->assertEquals($this->node->tagName, 'tested');
46 $this->assertEquals(Html::serialize($this->document), '<outer><tested foo="bar" namespace:foo="bar"><test bar="foo"></test></tested></outer>');
50 * Tests DomHelperTrait::setNodeContent().
52 * @dataProvider providerTestSetNodeContent
54 public function testSetNodeContent($content, $expected_output) {
55 $this->setNodeContent($this->node, $content);
56 $this->assertEquals(Html::serialize($this->document), $expected_output);
61 * @see ::testSetNodeContent()
63 public function providerTestSetNodeContent() {
67 '<outer><test foo="bar" namespace:foo="bar"></test></outer>',
69 'single node without children' => [
71 '<outer><test foo="bar" namespace:foo="bar"><div></div></test></outer>',
73 'single node with children' => [
74 '<div><replacement replaced="true" /></div>',
75 '<outer><test foo="bar" namespace:foo="bar"><div><replacement replaced="true"></replacement></div></test></outer>',
78 '<p>first</p><p>second</p>',
79 '<outer><test foo="bar" namespace:foo="bar"><p>first</p><p>second</p></test></outer>',
81 'multiple nodes, with a text node, comment node and element node' => [
82 'Second <!-- comment --> <p>third</p>',
83 '<outer><test foo="bar" namespace:foo="bar">Second <!-- comment --> <p>third</p></test></outer>',
89 * Test DomHelperTrait::replaceNodeContent().
91 * @dataProvider providerTestReplaceNodeContent
93 public function testReplaceNodeContent($content, $expected_output) {
94 $this->replaceNodeContent($this->node, $content);
95 $this->assertEquals($expected_output, Html::serialize($this->document));
100 * @see ::testReplaceNodeContent()
102 public function providerTestReplaceNodeContent() {
108 'single node without children' => [
110 '<outer><div></div></outer>',
112 'single node with children' => [
113 '<div><replacement replaced="true" /></div>',
114 '<outer><div><replacement replaced="true"></replacement></div></outer>',
116 'multiple nodes' => [
117 '<p>first</p><p>second</p>',
118 '<outer><p>first</p><p>second</p></outer>',
120 'multiple nodes, with a text node, comment node and element node' => [
121 'Second <!-- comment --> <p>third</p>',
122 '<outer>Second <!-- comment --> <p>third</p></outer>',
128 * Test DomHelperTrait::getNodeAttributesAsArray().
130 public function testGetNodeAttributesAsArray() {
131 $attributes = $this->getNodeAttributesAsArray($this->node);
132 $this->assertArrayEquals(['foo' => 'bar', 'namespace:foo' => 'bar'], $attributes);
134 // Test more complex attributes with special characters.
135 $string = "TEST: A <complex> 'encoded' \"JSON\" string";
136 $object = ['nested' => ['array' => TRUE], 'string' => $string];
137 $html = '<test data-json-string=\'' . Json::encode($string) . '\' data-json-object=\'' . Json::encode($object) . '\'></test>';
138 $document = Html::load($html);
139 $node = $document->getElementsByTagName('body')->item(0)->firstChild;
140 $attributes = $this->getNodeAttributesAsArray($node);
141 $this->assertArrayEquals(['data-json-string' => $string, 'data-json-object' => $object], $attributes);