class DomHelperTraitTest in Embed 8
Tests \Drupal\embed\DomHelperTrait.
@group embed
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\embed\Unit\DomHelperTraitTest uses DomHelperTrait
Expanded class hierarchy of DomHelperTraitTest
File
- tests/
src/ Unit/ DomHelperTraitTest.php, line 15
Namespace
Drupal\Tests\embed\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DomHelperTrait:: |
protected | function | Rename a DOMNode tag. | |
DomHelperTrait:: |
public | function | Convert the attributes on a DOMNode object to an array. | |
DomHelperTrait:: |
protected | function | Replace the contents of a DOMNode. | |
DomHelperTrait:: |
protected | function | Set the contents of a DOMNode. | |
DomHelperTraitTest:: |
protected | property | The DOM Document used for testing. | |
DomHelperTraitTest:: |
protected | property | The DOM Node used for testing. | |
DomHelperTraitTest:: |
public | function | ||
DomHelperTraitTest:: |
public | function | ||
DomHelperTraitTest:: |
public | function |
Overrides UnitTestCase:: |
|
DomHelperTraitTest:: |
public | function | Tests DomHelperTrait::changeNodeName(). | |
DomHelperTraitTest:: |
public | function | Test DomHelperTrait::getNodeAttributesAsArray(). | |
DomHelperTraitTest:: |
public | function | Test DomHelperTrait::replaceNodeContent(). | |
DomHelperTraitTest:: |
public | function | Tests DomHelperTrait::setNodeContent(). | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |