X-Git-Url: https://yaffs.net/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fembed%2Ftests%2Fsrc%2FUnit%2FDomHelperTraitTest.php;fp=web%2Fmodules%2Fcontrib%2Fembed%2Ftests%2Fsrc%2FUnit%2FDomHelperTraitTest.php;h=95eda7141656e28e19ce6f99d252d7df4334d5d2;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/embed/tests/src/Unit/DomHelperTraitTest.php b/web/modules/contrib/embed/tests/src/Unit/DomHelperTraitTest.php new file mode 100644 index 000000000..95eda7141 --- /dev/null +++ b/web/modules/contrib/embed/tests/src/Unit/DomHelperTraitTest.php @@ -0,0 +1,144 @@ +document = Html::load(''); + $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), ''); + } + + /** + * 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' => [ + '', + '', + ], + 'single node without children' => [ + '
', + '
', + ], + 'single node with children' => [ + '
', + '
', + ], + 'multiple nodes' => [ + '

first

second

', + '

first

second

', + ], + 'multiple nodes, with a text node, comment node and element node' => [ + 'Second

third

', + 'Second

third

', + ], + ]; + } + + /** + * 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' => [ + '', + '', + ], + 'single node without children' => [ + '
', + '
', + ], + 'single node with children' => [ + '
', + '
', + ], + 'multiple nodes' => [ + '

first

second

', + '

first

second

', + ], + 'multiple nodes, with a text node, comment node and element node' => [ + 'Second

third

', + 'Second

third

', + ], + ]; + } + + /** + * 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 'encoded' \"JSON\" string"; + $object = ['nested' => ['array' => TRUE], 'string' => $string]; + $html = ''; + $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); + } + +}