| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Html2Wt; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Wikimedia\Parsoid\DOM\Document; |
| 7 |
use Wikimedia\Parsoid\DOM\Element; |
| 8 |
use Wikimedia\Parsoid\Html2Wt\DiffMarkers; |
| 9 |
use Wikimedia\Parsoid\Html2Wt\DiffUtils; |
| 10 |
use Wikimedia\Parsoid\Html2Wt\DOMDiff; |
| 11 |
use Wikimedia\Parsoid\Mocks\MockEnv; |
| 12 |
use Wikimedia\Parsoid\Utils\ContentUtils; |
| 13 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 14 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 15 |
|
| 16 |
/** |
| 17 |
* Based on tests/mocha/domdiff.js |
| 18 |
* @coversDefaultClass \Wikimedia\Parsoid\Html2Wt\DiffUtils |
| 19 |
*/ |
| 20 |
class DiffUtilsTest extends TestCase { |
| 21 |
|
| 22 |
/** @var Document[] */ |
| 23 |
private $liveDocs = []; |
| 24 |
|
| 25 |
/** |
| 26 |
* @covers ::isDiffMarker |
| 27 |
*/ |
| 28 |
public function testIsDiffMarker_changingTextInNode() { |
| 29 |
$orig = '<p>a</p><p>b</p>'; |
| 30 |
$edit = '<p>A</p><p>b</p>'; |
| 31 |
|
| 32 |
$body = $this->parseAndDiff( $orig, $edit ); |
| 33 |
|
| 34 |
$this->checkMarkers( |
| 35 |
$this->selectNode( $body, 'body > p:first-child' ), |
| 36 |
[ DiffMarkers::CHILDREN_CHANGED, DiffMarkers::SUBTREE_CHANGED ] |
| 37 |
); |
| 38 |
|
| 39 |
$this->assertTrue( DiffUtils::isDiffMarker( |
| 40 |
$this->selectNode( $body, 'body > p:first-child > meta:first-child' ), |
| 41 |
DiffMarkers::DELETED |
| 42 |
) ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @covers ::isDiffMarker |
| 47 |
*/ |
| 48 |
public function testIsDiffMarker_deletingNode() { |
| 49 |
$orig = '<p>a</p><p>b</p>'; |
| 50 |
$edit = '<p>a</p>'; |
| 51 |
|
| 52 |
$body = $this->parseAndDiff( $orig, $edit ); |
| 53 |
|
| 54 |
$this->checkMarkers( $body, [ DiffMarkers::CHILDREN_CHANGED ] ); |
| 55 |
|
| 56 |
$this->assertTrue( DiffUtils::isDiffMarker( |
| 57 |
$this->selectNode( $body, 'body > p + meta' ), |
| 58 |
DiffMarkers::DELETED |
| 59 |
) ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @covers ::isDiffMarker |
| 64 |
*/ |
| 65 |
public function testIsDiffMarker_reorderingNodes() { |
| 66 |
$orig = '<p>a</p><p>b</p>'; |
| 67 |
$edit = '<p>b</p><p>a</p>'; |
| 68 |
|
| 69 |
$body = $this->parseAndDiff( $orig, $edit ); |
| 70 |
|
| 71 |
$this->checkMarkers( |
| 72 |
$this->selectNode( $body, 'body > p:nth-child(1)' ), |
| 73 |
[ DiffMarkers::CHILDREN_CHANGED, DiffMarkers::SUBTREE_CHANGED ] |
| 74 |
); |
| 75 |
|
| 76 |
$this->assertTrue( DiffUtils::isDiffMarker( |
| 77 |
$this->selectNode( $body, 'body > p:nth-child(1) > meta' ), |
| 78 |
DiffMarkers::DELETED |
| 79 |
) ); |
| 80 |
|
| 81 |
$this->checkMarkers( |
| 82 |
$this->selectNode( $body, 'body > p:nth-child(2)' ), |
| 83 |
[ DiffMarkers::CHILDREN_CHANGED, DiffMarkers::SUBTREE_CHANGED ] |
| 84 |
); |
| 85 |
|
| 86 |
$this->assertTrue( DiffUtils::isDiffMarker( |
| 87 |
$this->selectNode( $body, 'body > p:nth-child(2) > meta' ), |
| 88 |
DiffMarkers::DELETED |
| 89 |
) ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @covers ::isDiffMarker |
| 94 |
*/ |
| 95 |
public function testIsDiffMarker_addingMultipleNodes() { |
| 96 |
$orig = '<p>a</p>'; |
| 97 |
$edit = '<p>x</p><p>a</p><p>y</p>'; |
| 98 |
|
| 99 |
$body = $this->parseAndDiff( $orig, $edit ); |
| 100 |
|
| 101 |
$this->checkMarkers( $body, [ DiffMarkers::CHILDREN_CHANGED ] ); |
| 102 |
|
| 103 |
$this->checkMarkers( |
| 104 |
$this->selectNode( $body, 'body > p:nth-child(1)' ), |
| 105 |
[ DiffMarkers::CHILDREN_CHANGED, DiffMarkers::SUBTREE_CHANGED ] |
| 106 |
); |
| 107 |
|
| 108 |
$this->assertTrue( DiffUtils::isDiffMarker( |
| 109 |
$this->selectNode( $body, 'body > p:nth-child(1) > meta' ), |
| 110 |
DiffMarkers::DELETED |
| 111 |
) ); |
| 112 |
|
| 113 |
$this->checkMarkers( |
| 114 |
$this->selectNode( $body, 'body > p:nth-child(2)' ), |
| 115 |
[ DiffMarkers::INSERTED ] |
| 116 |
); |
| 117 |
|
| 118 |
$this->checkMarkers( |
| 119 |
$this->selectNode( $body, 'body > p:nth-child(3)' ), |
| 120 |
[ DiffMarkers::INSERTED ] |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @covers ::isDiffMarker |
| 126 |
*/ |
| 127 |
public function testIsDiffMarker_addingAndDeletingNodes() { |
| 128 |
$orig = '<p>a</p><p>b</p><p>c</p>'; |
| 129 |
$edit = '<p>x</p><p>b</p>'; |
| 130 |
|
| 131 |
$body = $this->parseAndDiff( $orig, $edit ); |
| 132 |
|
| 133 |
$this->checkMarkers( $body, [ DiffMarkers::CHILDREN_CHANGED ] ); |
| 134 |
|
| 135 |
$this->checkMarkers( |
| 136 |
$this->selectNode( $body, 'body > p:nth-child(1)' ), |
| 137 |
[ DiffMarkers::CHILDREN_CHANGED, DiffMarkers::SUBTREE_CHANGED ] |
| 138 |
); |
| 139 |
|
| 140 |
$this->assertTrue( DiffUtils::isDiffMarker( |
| 141 |
$this->selectNode( $body, 'body > p:nth-child(1) > meta' ), |
| 142 |
DiffMarkers::DELETED |
| 143 |
) ); |
| 144 |
|
| 145 |
$this->assertTrue( DiffUtils::isDiffMarker( |
| 146 |
$this->selectNode( $body, 'body > meta:nth-child(3)' ), |
| 147 |
DiffMarkers::DELETED |
| 148 |
) ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* @coversNothing |
| 153 |
*/ |
| 154 |
public function testSomething_changingAttribute() { |
| 155 |
$orig = '<p class="a">a</p><p class="b">b</p>'; |
| 156 |
$edit = '<p class="X">a</p><p class="b">b</p>'; |
| 157 |
|
| 158 |
$body = $this->parseAndDiff( $orig, $edit ); |
| 159 |
|
| 160 |
$this->checkMarkers( $body, [ DiffMarkers::CHILDREN_CHANGED ] ); |
| 161 |
|
| 162 |
$this->checkMarkers( |
| 163 |
$this->selectNode( $body, 'body > p:nth-child(1)' ), |
| 164 |
[ DiffMarkers::MODIFIED_WRAPPER ] |
| 165 |
); |
| 166 |
|
| 167 |
$this->expectNotToPerformAssertions(); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* @coversNothing |
| 172 |
*/ |
| 173 |
public function testSomething_changingDataMwForTemplate() { |
| 174 |
$orig = '<p about="#mwt1" typeof="mw:Transclusion" data-mw=\'{"parts":[{"template":' . |
| 175 |
'{"target":{"wt":"1x","href":"./Template:1x"},"params":{"1":{"wt":"a"}},"i":0}}]}\'>a</p>'; |
| 176 |
$edit = '<p about="#mwt1" typeof="mw:Transclusion" data-mw=\'{"parts":[{"template":' . |
| 177 |
'{"target":{"wt":"1x","href":"./Template:1x"},"params":{"1":{"wt":"foo"}},"i":0}}]}\'>foo</p>'; |
| 178 |
|
| 179 |
$body = $this->parseAndDiff( $orig, $edit ); |
| 180 |
|
| 181 |
$this->checkMarkers( $body, [ DiffMarkers::CHILDREN_CHANGED ] ); |
| 182 |
|
| 183 |
$this->checkMarkers( |
| 184 |
$this->selectNode( $body, 'body > p:nth-child(1)' ), |
| 185 |
[ DiffMarkers::MODIFIED_WRAPPER ] |
| 186 |
); |
| 187 |
|
| 188 |
$this->expectNotToPerformAssertions(); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @coversNothing |
| 193 |
* The additional subtrees added to the template's content should simply be ignored |
| 194 |
*/ |
| 195 |
public function testSomething_addingAdditionalDomTreesToTemplatedContent() { |
| 196 |
$orig = '<p about="#mwt1" typeof="mw:Transclusion" data-mw=\'{"parts":[{"template":' . |
| 197 |
'{"target":{"wt":"1x","href":"./Template:1x"},"params":' . |
| 198 |
'{"1":{"wt":"a"}},"i":0}}]}\'>a</p>'; |
| 199 |
$edit = '<p about="#mwt1" typeof="mw:Transclusion" data-mw=\'{"parts":[{"template":' . |
| 200 |
'{"target":{"wt":"1x","href":"./Template:1x"},"params":' . |
| 201 |
'{"1":{"wt":"foo\n\nbar\n\nbaz"}},"i":0}}]}\'>foo</p>' . |
| 202 |
'<p about="#mwt1">bar</p><p about="#mwt1">baz</p>'; |
| 203 |
|
| 204 |
$body = $this->parseAndDiff( $orig, $edit ); |
| 205 |
|
| 206 |
$this->checkMarkers( $body, [ DiffMarkers::CHILDREN_CHANGED ] ); |
| 207 |
|
| 208 |
$this->checkMarkers( |
| 209 |
$this->selectNode( $body, 'body > p:nth-child(1)' ), |
| 210 |
[ DiffMarkers::MODIFIED_WRAPPER ] |
| 211 |
); |
| 212 |
|
| 213 |
$this->expectNotToPerformAssertions(); |
| 214 |
} |
| 215 |
|
| 216 |
private function parseAndDiff( string $html1, string $html2 ): Element { |
| 217 |
$mockEnv = new MockEnv( [] ); |
| 218 |
|
| 219 |
$doc1 = ContentUtils::createAndLoadDocument( $html1 ); |
| 220 |
$doc2 = ContentUtils::createAndLoadDocument( $html2 ); |
| 221 |
|
| 222 |
$body1 = DOMCompat::getBody( $doc1 ); |
| 223 |
$body2 = DOMCompat::getBody( $doc2 ); |
| 224 |
|
| 225 |
$domDiff = new DOMDiff( $mockEnv ); |
| 226 |
$domDiff->diff( $body1, $body2 ); |
| 227 |
|
| 228 |
// Prevent GC from reclaiming doc2 once we exit this function. |
| 229 |
// Necessary hack because we use PHPDOM which wraps libxml. |
| 230 |
$this->liveDocs[] = $doc2; |
| 231 |
|
| 232 |
return DOMCompat::getBody( $doc2 ); |
| 233 |
} |
| 234 |
|
| 235 |
private function selectNode( Element $body, string $selector ): Element { |
| 236 |
$nodes = DOMCompat::querySelectorAll( $body, $selector ); |
| 237 |
if ( count( $nodes ) !== 1 ) { |
| 238 |
$this->fail( 'It should be exactly one node for the selector' ); |
| 239 |
} |
| 240 |
return $nodes[0]; |
| 241 |
} |
| 242 |
|
| 243 |
private function checkMarkers( Element $node, array $markers ): void { |
| 244 |
$data = DOMDataUtils::getNodeData( $node ); |
| 245 |
$diff = $data->parsoid_diff->diff; |
| 246 |
if ( count( $markers ) !== count( $diff ) ) { |
| 247 |
var_dump( $markers ); |
| 248 |
var_dump( $diff ); |
| 249 |
$this->fail( 'Count of diff should be equal count of markers' ); |
| 250 |
} |
| 251 |
foreach ( $markers as $key => $value ) { |
| 252 |
if ( $diff[$key] !== $value ) { |
| 253 |
$this->fail( 'Diff is not equal to the marker' ); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
} |