a
b
'; $edit = 'A
b
'; $body = $this->parseAndDiff( $orig, $edit ); $this->checkMarkers( $this->selectNode( $body, 'body > p:first-child' ), [ DiffMarkers::CHILDREN_CHANGED, DiffMarkers::SUBTREE_CHANGED ] ); $this->assertTrue( DiffUtils::isDiffMarker( $this->selectNode( $body, 'body > p:first-child > meta:first-child' ), DiffMarkers::DELETED ) ); } /** * @covers ::isDiffMarker */ public function testIsDiffMarker_deletingNode() { $orig = 'a
b
'; $edit = 'a
'; $body = $this->parseAndDiff( $orig, $edit ); $this->checkMarkers( $body, [ DiffMarkers::CHILDREN_CHANGED ] ); $this->assertTrue( DiffUtils::isDiffMarker( $this->selectNode( $body, 'body > p + meta' ), DiffMarkers::DELETED ) ); } /** * @covers ::isDiffMarker */ public function testIsDiffMarker_reorderingNodes() { $orig = 'a
b
'; $edit = 'b
a
'; $body = $this->parseAndDiff( $orig, $edit ); $this->checkMarkers( $this->selectNode( $body, 'body > p:nth-child(1)' ), [ DiffMarkers::CHILDREN_CHANGED, DiffMarkers::SUBTREE_CHANGED ] ); $this->assertTrue( DiffUtils::isDiffMarker( $this->selectNode( $body, 'body > p:nth-child(1) > meta' ), DiffMarkers::DELETED ) ); $this->checkMarkers( $this->selectNode( $body, 'body > p:nth-child(2)' ), [ DiffMarkers::CHILDREN_CHANGED, DiffMarkers::SUBTREE_CHANGED ] ); $this->assertTrue( DiffUtils::isDiffMarker( $this->selectNode( $body, 'body > p:nth-child(2) > meta' ), DiffMarkers::DELETED ) ); } /** * @covers ::isDiffMarker */ public function testIsDiffMarker_addingMultipleNodes() { $orig = 'a
'; $edit = 'x
a
y
'; $body = $this->parseAndDiff( $orig, $edit ); $this->checkMarkers( $body, [ DiffMarkers::CHILDREN_CHANGED ] ); $this->checkMarkers( $this->selectNode( $body, 'body > p:nth-child(1)' ), [ DiffMarkers::CHILDREN_CHANGED, DiffMarkers::SUBTREE_CHANGED ] ); $this->assertTrue( DiffUtils::isDiffMarker( $this->selectNode( $body, 'body > p:nth-child(1) > meta' ), DiffMarkers::DELETED ) ); $this->checkMarkers( $this->selectNode( $body, 'body > p:nth-child(2)' ), [ DiffMarkers::INSERTED ] ); $this->checkMarkers( $this->selectNode( $body, 'body > p:nth-child(3)' ), [ DiffMarkers::INSERTED ] ); } /** * @covers ::isDiffMarker */ public function testIsDiffMarker_addingAndDeletingNodes() { $orig = 'a
b
c
'; $edit = 'x
b
'; $body = $this->parseAndDiff( $orig, $edit ); $this->checkMarkers( $body, [ DiffMarkers::CHILDREN_CHANGED ] ); $this->checkMarkers( $this->selectNode( $body, 'body > p:nth-child(1)' ), [ DiffMarkers::CHILDREN_CHANGED, DiffMarkers::SUBTREE_CHANGED ] ); $this->assertTrue( DiffUtils::isDiffMarker( $this->selectNode( $body, 'body > p:nth-child(1) > meta' ), DiffMarkers::DELETED ) ); $this->assertTrue( DiffUtils::isDiffMarker( $this->selectNode( $body, 'body > meta:nth-child(3)' ), DiffMarkers::DELETED ) ); } /** * @coversNothing */ public function testSomething_changingAttribute() { $orig = 'a
b
'; $edit = 'a
b
'; $body = $this->parseAndDiff( $orig, $edit ); $this->checkMarkers( $body, [ DiffMarkers::CHILDREN_CHANGED ] ); $this->checkMarkers( $this->selectNode( $body, 'body > p:nth-child(1)' ), [ DiffMarkers::MODIFIED_WRAPPER ] ); $this->expectNotToPerformAssertions(); } /** * @coversNothing */ public function testSomething_changingDataMwForTemplate() { $orig = 'a
'; $edit = 'foo
'; $body = $this->parseAndDiff( $orig, $edit ); $this->checkMarkers( $body, [ DiffMarkers::CHILDREN_CHANGED ] ); $this->checkMarkers( $this->selectNode( $body, 'body > p:nth-child(1)' ), [ DiffMarkers::MODIFIED_WRAPPER ] ); $this->expectNotToPerformAssertions(); } /** * @coversNothing * The additional subtrees added to the template's content should simply be ignored */ public function testSomething_addingAdditionalDomTreesToTemplatedContent() { $orig = 'a
'; $edit = 'foo
' . 'bar
baz
'; $body = $this->parseAndDiff( $orig, $edit ); $this->checkMarkers( $body, [ DiffMarkers::CHILDREN_CHANGED ] ); $this->checkMarkers( $this->selectNode( $body, 'body > p:nth-child(1)' ), [ DiffMarkers::MODIFIED_WRAPPER ] ); $this->expectNotToPerformAssertions(); } private function parseAndDiff( string $html1, string $html2 ): Element { $mockEnv = new MockEnv( [] ); $doc1 = ContentUtils::createAndLoadDocument( $html1 ); $doc2 = ContentUtils::createAndLoadDocument( $html2 ); $body1 = DOMCompat::getBody( $doc1 ); $body2 = DOMCompat::getBody( $doc2 ); $domDiff = new DOMDiff( $mockEnv ); $domDiff->diff( $body1, $body2 ); // Prevent GC from reclaiming doc2 once we exit this function. // Necessary hack because we use PHPDOM which wraps libxml. $this->liveDocs[] = $doc2; return DOMCompat::getBody( $doc2 ); } private function selectNode( Element $body, string $selector ): Element { $nodes = DOMCompat::querySelectorAll( $body, $selector ); if ( count( $nodes ) !== 1 ) { $this->fail( 'It should be exactly one node for the selector' ); } return $nodes[0]; } private function checkMarkers( Element $node, array $markers ): void { $data = DOMDataUtils::getNodeData( $node ); $diff = $data->parsoid_diff->diff; if ( count( $markers ) !== count( $diff ) ) { var_dump( $markers ); var_dump( $diff ); $this->fail( 'Count of diff should be equal count of markers' ); } foreach ( $markers as $key => $value ) { if ( $diff[$key] !== $value ) { $this->fail( 'Diff is not equal to the marker' ); } } } }