95eda7141656e28e19ce6f99d252d7df4334d5d2
[yaffs-website] / web / modules / contrib / embed / tests / src / Unit / DomHelperTraitTest.php
1 <?php
2
3 namespace Drupal\Tests\embed\Unit;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Serialization\Json;
7 use Drupal\Tests\UnitTestCase;
8 use Drupal\embed\DomHelperTrait;
9
10 /**
11  * Tests \Drupal\embed\DomHelperTrait.
12  *
13  * @group embed
14  */
15 class DomHelperTraitTest extends UnitTestCase {
16   use DomHelperTrait;
17
18   /**
19    * The DOM Document used for testing.
20    *
21    * @var \DOMDocument
22    */
23   protected $document;
24
25   /**
26    * The DOM Node used for testing.
27    *
28    * @var \DOMElement
29    */
30   protected $node;
31
32   /**
33    * {@inheritdoc}
34    */
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;
38   }
39
40   /**
41    * Tests DomHelperTrait::changeNodeName().
42    */
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>');
47   }
48
49   /**
50    * Tests DomHelperTrait::setNodeContent().
51    *
52    * @dataProvider providerTestSetNodeContent
53    */
54   public function testSetNodeContent($content, $expected_output) {
55     $this->setNodeContent($this->node, $content);
56     $this->assertEquals(Html::serialize($this->document), $expected_output);
57   }
58
59   /**
60    * @return array
61    * @see ::testSetNodeContent()
62    */
63   public function providerTestSetNodeContent() {
64     return [
65       'empty' => [
66         '',
67         '<outer><test foo="bar" namespace:foo="bar"></test></outer>',
68       ],
69       'single node without children' => [
70         '<div></div>',
71         '<outer><test foo="bar" namespace:foo="bar"><div></div></test></outer>',
72       ],
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>',
76       ],
77       'multiple nodes' => [
78         '<p>first</p><p>second</p>',
79         '<outer><test foo="bar" namespace:foo="bar"><p>first</p><p>second</p></test></outer>',
80       ],
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>',
84       ],
85     ];
86   }
87
88   /**
89    * Test DomHelperTrait::replaceNodeContent().
90    *
91    * @dataProvider providerTestReplaceNodeContent
92    */
93   public function testReplaceNodeContent($content, $expected_output) {
94     $this->replaceNodeContent($this->node, $content);
95     $this->assertEquals($expected_output, Html::serialize($this->document));
96   }
97
98   /**
99    * @return array
100    * @see ::testReplaceNodeContent()
101    */
102   public function providerTestReplaceNodeContent() {
103     return [
104       'empty' => [
105         '',
106         '<outer></outer>',
107       ],
108       'single node without children' => [
109         '<div></div>',
110         '<outer><div></div></outer>',
111       ],
112       'single node with children' => [
113         '<div><replacement replaced="true" /></div>',
114         '<outer><div><replacement replaced="true"></replacement></div></outer>',
115       ],
116       'multiple nodes' => [
117         '<p>first</p><p>second</p>',
118         '<outer><p>first</p><p>second</p></outer>',
119       ],
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>',
123       ],
124     ];
125   }
126
127   /**
128    * Test DomHelperTrait::getNodeAttributesAsArray().
129    */
130   public function testGetNodeAttributesAsArray() {
131     $attributes = $this->getNodeAttributesAsArray($this->node);
132     $this->assertArrayEquals(['foo' => 'bar', 'namespace:foo' => 'bar'], $attributes);
133
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);
142   }
143
144 }