Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Utils/DOMDataUtilsTest.php

raw
1 <?php
2
3 namespace Test\Parsoid\Utils;
4
5 use Wikimedia\Parsoid\Core\PageBundle;
6 use Wikimedia\Parsoid\Utils\ContentUtils;
7 use Wikimedia\Parsoid\Utils\DOMCompat;
8 use Wikimedia\Parsoid\Utils\DOMDataUtils;
9 use Wikimedia\Parsoid\Utils\DOMUtils;
10
11 /**
12 * @coversDefaultClass \Wikimedia\Parsoid\Utils\DOMDataUtils
13 */
14 class DOMDataUtilsTest extends \PHPUnit\Framework\TestCase {
15
16 /**
17 * @covers ::injectPageBundle
18 */
19 public function testInjectPageBundle() {
20 // Note that injectPageBundle does not need a "prepared" document.
21 $doc = DOMUtils::parseHTML( "Hello, world" );
22 DOMDataUtils::injectPageBundle( $doc,
23 new PageBundle(
24 '',
25 [ "counter" => -1, "ids" => [] ],
26 [ "ids" => [] ]
27 )
28 );
29 // Note that we use the 'native' getElementById, not
30 // DOMCompat::getElementById, in order to test T232390
31 $el = $doc->getElementById( 'mw-pagebundle' );
32 $this->assertNotEquals( null, $el );
33 $this->assertEquals( 'script', DOMCompat::nodeName( $el ) );
34 }
35
36 /**
37 * @covers ::storeInPageBundle
38 */
39 public function testStoreInPageBundle() {
40 $doc = ContentUtils::createDocument( "<p>Hello, world</p>" );
41 $p = DOMCompat::querySelector( $doc, 'p' );
42 DOMDataUtils::storeInPageBundle( $p, (object)[
43 'parsoid' => [ 'go' => 'team' ],
44 'mw' => [ 'test' => 'me' ],
45 ], DOMDataUtils::usedIdIndex( $p ) );
46 $id = DOMCompat::getAttribute( $p, 'id' ) ?? '';
47 $this->assertNotEquals( '', $id );
48 // Use the 'native' getElementById, not DOMCompat::getElementById,
49 // in order to test T232390.
50 $el = $doc->getElementById( $id );
51 $this->assertEquals( $p, $el );
52 }
53
54 /**
55 * @covers ::extractPageBundle
56 */
57 public function testExtractPageBundle() {
58 $html = <<<'EOF'
59 <html>
60 <head>
61 <script id="mw-pagebundle" type="application/x-mw-pagebundle">
62 {"parsoid":
63 {"counter":1,"ids":{"mwAA":{"dsr":[0,13,0,0]},
64 "mwAQ":{"dsr":[0,12,0,0]}},"offsetType":"byte"},"mw":{"ids":[]}}
65 </script>
66 </head>
67 <body><p id="mwAQ">Hello, world</p>
68 EOF;
69 $doc = DOMUtils::parseHTML( $html );
70 $pb = DOMDataUtils::extractPageBundle( $doc );
71 self::assertIsArray( $pb->parsoid['ids'] );
72 }
73 }