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);
}
}