Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Html2Wt/DOMDiffTest.php

raw
1 <?php
2
3 namespace Test\Parsoid\Html2Wt;
4
5 use PHPUnit\Framework\TestCase;
6 use Wikimedia\Parsoid\Html2Wt\DiffMarkers;
7 use Wikimedia\Parsoid\Html2Wt\DiffUtils;
8 use Wikimedia\Parsoid\Html2Wt\DOMDiff;
9 use Wikimedia\Parsoid\Mocks\MockEnv;
10 use Wikimedia\Parsoid\Utils\ContentUtils;
11 use Wikimedia\Parsoid\Utils\DOMCompat;
12
13 /**
14 * Test DOM Diff, the tests used for validating DOMNDiff class port from JS
15 * and based on similar tests in tests/mocha/domdiff.js
16 * @coversDefaultClass \Wikimedia\Parsoid\Html2Wt\DOMDiff
17 */
18 class DOMDiffTest extends TestCase {
19
20 /**
21 * @covers ::doDOMDiff
22 * @dataProvider provideDiff
23 * @param array $test
24 */
25 public function testDOMDiff( array $test ) {
26 $mockEnv = new MockEnv( [] );
27
28 $oldDOM = ContentUtils::createAndLoadDocument(
29 $test['orig'], [ 'markNew' => true ]
30 );
31 $newDOM = ContentUtils::createAndLoadDocument(
32 $test['edit'], [ 'markNew' => true ]
33 );
34
35 $oldBody = DOMCompat::getBody( $oldDOM );
36 $body = DOMCompat::getBody( $newDOM );
37
38 $domDiff = new DOMDiff( $mockEnv );
39 $domDiff->diff( $oldBody, $body );
40
41 foreach ( $test['specs'] as $spec ) {
42 if ( $spec['selector'] === 'body' ) { // Hmm .. why is this?
43 $node = $body;
44 } else {
45 $nodes = DOMCompat::querySelectorAll( $body, $spec['selector'] );
46 $this->assertCount( 1, $nodes );
47 $node = $nodes[0];
48 }
49 if ( isset( $spec['diff'] ) ) {
50 $this->assertTrue( DiffUtils::isDiffMarker( $node, $spec['diff'] ) );
51 } elseif ( isset( $spec['markers'] ) ) {
52 $markers = DiffUtils::getDiffMark( $node )->diff ?? [];
53
54 $this->assertSameSize( $spec['markers'], $markers,
55 'number of markers does not match' );
56
57 foreach ( $markers as $k => $m ) {
58 $this->assertEquals( $spec['markers'][$k], $m,
59 'markers do not match' );
60 }
61 }
62 }
63 }
64
65 /**
66 * FIXME: The subtree-changed marker seems to be applied inconsistently.
67 * Check if that marker is still needed / used by serialization code and
68 * update code accordingly. If possible, simplify / reduce the different
69 * markers being used.
70 */
71 public function provideDiff(): array {
72 return [
73 [
74 [
75 'desc' => 'ignore attribute order in a node',
76 'orig' => '<font size="1" class="x">foo</font>',
77 'edit' => '<font class="x" size="1">foo</font>',
78 'specs' => [
79 [ 'selector' => 'font', 'markers' => [] ],
80 ]
81 ]
82 ],
83 [
84 [
85 'desc' => 'changing text in a node',
86 'orig' => '<p>a</p><p>b</p>',
87 'edit' => '<p>A</p><p>b</p>',
88 'specs' => [
89 [ 'selector' => 'body > p:first-child', 'markers' => [ DiffMarkers::CHILDREN_CHANGED,
90 DiffMarkers::SUBTREE_CHANGED ] ],
91 [ 'selector' => 'body > p:first-child > meta:first-child', 'diff' => DiffMarkers::DELETED ]
92 ]
93 ]
94 ],
95 [
96 [
97 'desc' => 'deleting a node',
98 'orig' => '<p>a</p><p>b</p>',
99 'edit' => '<p>a</p>',
100 'specs' => [
101 [ 'selector' => 'body', 'markers' => [ DiffMarkers::CHILDREN_CHANGED ] ],
102 [ 'selector' => 'body > p + meta', 'diff' => DiffMarkers::DELETED ]
103 ]
104 ]
105 ],
106 [
107 [
108 'desc' => 'adding multiple nodes',
109 'orig' => '<p>a</p>',
110 'edit' => '<p>x</p><p>a</p><p>y</p>',
111 'specs' => [
112 [ 'selector' => 'body', 'markers' => [ DiffMarkers::CHILDREN_CHANGED ] ],
113 [ 'selector' => 'body > p:nth-child(1)', 'markers' => [ DiffMarkers::CHILDREN_CHANGED,
114 DiffMarkers::SUBTREE_CHANGED ] ],
115 [ 'selector' => 'body > p:nth-child(1) > meta', 'diff' => DiffMarkers::DELETED ],
116 [ 'selector' => 'body > p:nth-child(2)', 'markers' => [ DiffMarkers::INSERTED ] ],
117 [ 'selector' => 'body > p:nth-child(3)', 'markers' => [ DiffMarkers::INSERTED ] ]
118 ]
119 ]
120 ],
121 [
122 [
123 'desc' => 'reordering nodes',
124 'orig' => '<p>a</p><p>b</p>',
125 'edit' => '<p>b</p><p>a</p>',
126 'specs' => [
127 [ 'selector' => 'body > p:nth-child(1)', 'markers' => [ DiffMarkers::CHILDREN_CHANGED,
128 DiffMarkers::SUBTREE_CHANGED ] ],
129 [ 'selector' => 'body > p:nth-child(1) > meta', 'diff' => DiffMarkers::DELETED ],
130 [ 'selector' => 'body > p:nth-child(2)', 'markers' => [ DiffMarkers::CHILDREN_CHANGED,
131 DiffMarkers::SUBTREE_CHANGED ] ],
132 [ 'selector' => 'body > p:nth-child(2) > meta', 'diff' => DiffMarkers::DELETED ]
133 ]
134 ]
135 ],
136 [
137 [
138 'desc' => 'adding and deleting nodes',
139 'orig' => '<p>a</p><p>b</p><p>c</p>',
140 'edit' => '<p>x</p><p>b</p>',
141 'specs' => [
142 [ 'selector' => 'body', 'markers' => [ DiffMarkers::CHILDREN_CHANGED ] ],
143 [ 'selector' => 'body > p:nth-child(1)', 'markers' => [ DiffMarkers::CHILDREN_CHANGED,
144 DiffMarkers::SUBTREE_CHANGED ] ],
145 [ 'selector' => 'body > p:nth-child(1) > meta', 'diff' => DiffMarkers::DELETED ],
146 [ 'selector' => 'body > meta:nth-child(3)', 'diff' => DiffMarkers::DELETED ]
147 ]
148 ]
149 ],
150 [
151 [
152 'desc' => 'changing an attribute',
153 'orig' => '<p class="a">a</p><p class="b">b</p>',
154 'edit' => '<p class="X">a</p><p class="b">b</p>',
155 'specs' => [
156 [ 'selector' => 'body', 'markers' => [ DiffMarkers::CHILDREN_CHANGED ] ],
157 [ 'selector' => 'body > p:nth-child(1)', 'markers' => [ DiffMarkers::MODIFIED_WRAPPER ] ]
158 ]
159 ]
160 ],
161 [
162 [
163 'desc' => 'changing data-mw for a template',
164 'orig' => '<p about="#mwt1" typeof="mw:Transclusion" data-mw=\'{"parts":[{"template":' .
165 '{"target":{"wt":"1x","href":"./Template:1x"},"params":{"1":{"wt":"a"}},"i":0}}]}\'>a</p>',
166 'edit' => '<p about="#mwt1" typeof="mw:Transclusion" data-mw=\'{"parts":[{"template":' .
167 '{"target":{"wt":"1x","href":"./Template:1x"},"params":{"1":{"wt":"foo"}},"i":' .
168 '0}}]}\'>foo</p>',
169 'specs' => [
170 [ 'selector' => 'body', 'markers' => [ DiffMarkers::CHILDREN_CHANGED ] ],
171 [ 'selector' => 'body > p:nth-child(1)', 'markers' => [ DiffMarkers::MODIFIED_WRAPPER ] ],
172 ]
173 ]
174 ],
175 // The additional subtrees added to the template's content should simply be ignored
176 [
177 [
178 'desc' => 'adding additional DOM trees to templated content',
179 'orig' => '<p about="#mwt1" typeof="mw:Transclusion" data-mw=\'{"parts":[{"template":' .
180 '{"target":{"wt":"1x","href":"./Template:1x"},"params":{"1":{"wt":"a"}},"i":0}}]}\'>a</p>',
181 'edit' => '<p about="#mwt1" typeof="mw:Transclusion" data-mw=\'{"parts":[{"template":' .
182 '{"target":{"wt":"1x","href":"./Template:1x"},"params":{"1":{"wt":' .
183 '"foo\\n\\nbar\\n\\nbaz"}},' .
184 '"i":0}}]}\'>foo</p><p about="#mwt1">bar</p><p about="#mwt1">baz</p>',
185 'specs' => [
186 [ 'selector' => 'body', 'markers' => [ DiffMarkers::CHILDREN_CHANGED ] ],
187 [ 'selector' => 'body > p:nth-child(1)', 'markers' => [ DiffMarkers::MODIFIED_WRAPPER ] ],
188 ]
189 ]
190 ]
191 ];
192 }
193
194 }