$wt ] ); $pageConfig = new MockPageConfig( $siteConfig, $opts, $pageContent ); $out = $parsoid->wikitext2html( $pageConfig, $parserOpts ); if ( !empty( $parserOpts['pageBundle'] ) ) { $this->assertTrue( $out instanceof PageBundle ); $this->assertEquals( $expected, $out->html ); } else { $this->assertEquals( $expected, $out ); } } public function provideWt2Html(): array { return [ [ "'''hi ho'''", "

hi ho

", [ 'body_only' => true, 'wrapSections' => false, ] ], [ "'''hi ho'''", "

hi ho

", [ 'body_only' => true, 'wrapSections' => false, 'pageBundle' => true, ] ], ]; } /** * @covers ::wikitext2lint * @dataProvider provideWt2Lint */ public function testWt2Lint( $wt, $expected, $parserOpts = [] ) { $opts = [ 'linting' => true, ]; $siteConfig = new MockSiteConfig( $opts ); $dataAccess = new MockDataAccess( $siteConfig, $opts ); $parsoid = new Parsoid( $siteConfig, $dataAccess ); $pageContent = new MockPageContent( [ 'main' => $wt ] ); $pageConfig = new MockPageConfig( $siteConfig, $opts, $pageContent ); $lint = $parsoid->wikitext2lint( $pageConfig, $parserOpts ); $this->assertEquals( $expected, $lint ); } public function provideWt2Lint() { return [ [ "[http://google.com This is [[Google]]'s search page]", [ [ 'type' => 'wikilink-in-extlink', 'dsr' => [ 0, 52, 19, 1 ], 'params' => [], 'templateInfo' => null, ] ] ] ]; } /** * @covers ::html2wikitext * @dataProvider provideHtml2Wt */ public function testHtml2Wt( $input, $expected, $parserOpts = [] ) { $opts = []; $siteConfig = new MockSiteConfig( $opts ); $dataAccess = new MockDataAccess( $siteConfig, $opts ); $parsoid = new Parsoid( $siteConfig, $dataAccess ); $pageContent = new MockPageContent( [ 'main' => '' ] ); $pageConfig = new MockPageConfig( $siteConfig, $opts, $pageContent ); $wt = $parsoid->html2wikitext( $pageConfig, $input, $parserOpts ); $this->assertEquals( $expected, $wt ); } public function provideHtml2Wt() { return [ [ "
hi
\n
ho
", " hi\n
ho
" ], [ "

", '' ] ]; } /** * @covers ::pb2pb * @dataProvider providePb2Pb */ public function testPb2Pb( $update, $input, $expected, $testOpts = [] ) { $opts = []; $siteConfig = new MockSiteConfig( $opts ); $dataAccess = new MockDataAccess( $siteConfig, $opts ); $parsoid = new Parsoid( $siteConfig, $dataAccess ); $pageContent = new MockPageContent( [ 'main' => $testOpts['pageContent'] ?? '' ] ); $pageConfig = new MockPageConfig( $siteConfig, [ 'pageLanguage' => $testOpts['pageLanguage'] ?? 'en' ], $pageContent ); $pb = new PageBundle( $input['html'], PHPUtils::jsonDecode( $input['parsoid'] ?? 'null' ), PHPUtils::jsonDecode( $input['mw'] ?? 'null' ), $input['version'] ?? null, $input['headers'] ?? null, $input['contentmodel'] ?? null ); $out = $parsoid->pb2pb( $pageConfig, $update, $pb, $testOpts ); $this->assertTrue( $out instanceof PageBundle ); $this->assertEquals( $expected['html'], $out->html ); $this->assertEquals( $expected['parsoid'] ?? 'null', PHPUtils::jsonEncode( $out->parsoid ) ); $this->assertEquals( $expected['mw'] ?? 'null', PHPUtils::jsonEncode( $out->mw ) ); $this->assertEquals( $expected['version'] ?? null, $out->version ); if ( isset( $expected['headers'] ) ) { $this->assertEquals( $expected['headers'] ?? null, $out->headers ); } } public function providePb2Pb() { // phpcs:disable Generic.Files.LineLength.TooLong return [ [ 'convertoffsets', [ 'html' => '

La Luna è l\'unico satellite naturale della Terra

La Luna è l\'unico satellite naturale della Terra

', 'parsoid' => '{"counter":2,"ids":{"mwAA":{"dsr":[0,49,0,0]},"mwAQ":{"dsr":[51,100,0,0]}},"offsetType":"byte"}', 'mw' => null, ], [ 'html' => '

La Luna è l\'unico satellite naturale della Terra

La Luna è l\'unico satellite naturale della Terra

', 'parsoid' => '{"counter":2,"ids":{"mwAA":{"dsr":[0,48,0,0]},"mwAQ":{"dsr":[50,98,0,0]}},"offsetType":"ucs2"}', 'mw' => '{"ids":[]}', 'version' => self::$defaultContentVersion, ], [ 'inputOffsetType' => 'byte', 'outputOffsetType' => 'ucs2', // Note the accented e char that is a multi-byte char. // Without the pageContent property, offset conversion will not work. 'pageContent' => "La Luna è l'unico satellite naturale della Terra\n\nLa Luna è l'unico satellite naturale della Terra", 'body_only' => true ] ], [ 'convertoffsets', [ 'html' => '

La Luna è l\'unico satellite naturale della Terra

La Luna è l\'unico satellite naturale della Terra

', 'parsoid' => '{"counter":2,"ids":{"mwAA":{"dsr":[0,48,0,0]},"mwAQ":{"dsr":[50,98,0,0]}},"offsetType":"ucs2"}', 'mw' => null, ], [ 'html' => '

La Luna è l\'unico satellite naturale della Terra

La Luna è l\'unico satellite naturale della Terra

', 'parsoid' => '{"counter":2,"ids":{"mwAA":{"dsr":[0,49,0,0]},"mwAQ":{"dsr":[51,100,0,0]}},"offsetType":"byte"}', 'mw' => '{"ids":[]}', 'version' => self::$defaultContentVersion, ], [ 'inputOffsetType' => 'ucs2', 'outputOffsetType' => 'byte', 'pageContent' => "La Luna è l'unico satellite naturale della Terra\n\nLa Luna è l'unico satellite naturale della Terra", 'body_only' => true ] ], [ 'redlinks', [ 'html' => '

Special:Version Doesnotexist Redirected

', 'parsoid' => null, 'mw' => null, ], [ 'html' => '

Special:Version Doesnotexist Redirected

', 'parsoid' => '{"counter":-1,"ids":[],"offsetType":"byte"}', 'mw' => '{"ids":[]}', 'version' => self::$defaultContentVersion, ], [ 'body_only' => true, ] ], [ 'redlinks', [ 'html' => '

abcd

' . "\n" . '', 'parsoid' => '{"counter":2,"ids":{"mwAA":{"dsr":[0,24,0,0]},"mwAQ":{"dsr":[0,23,0,0]},"mwAg":{"stx":"piped","a":{"href":"./Not_an_article"},"sa":{"href":"Not an article"},"dsr":[0,23,17,2]}},"offsetType":"byte"}', 'mw' => '{"ids":[]}', ], [ 'html' => '

abcd

' . "\n", 'parsoid' => '{"counter":-1,"ids":{"mwAA":{"dsr":[0,24,0,0]},"mwAQ":{"dsr":[0,23,0,0]},"mwAg":{"stx":"piped","a":{"href":"./Not_an_article"},"sa":{"href":"Not an article"},"dsr":[0,23,17,2]}},"offsetType":"byte"}', 'mw' => '{"ids":[]}', 'version' => self::$defaultContentVersion, ], [ 'body_only' => true, ], ], // Note that id attributes are preserved, even if no data-parsoid // is provided. [ 'redlinks', [ 'html' => '

abcd

', 'parsoid' => null, 'mw' => null, ], [ 'html' => '

abcd

', 'parsoid' => '{"counter":-1,"ids":[],"offsetType":"byte"}', 'mw' => '{"ids":[]}', 'version' => self::$defaultContentVersion, ], [ 'body_only' => true, ], ], // Language Variant conversion endpoint [ 'variant', [ 'html' => '

абвг abcd x

', 'parsoid' => null, 'mw' => null, ], [ 'html' => '

abvg abcd x

', 'parsoid' => '{"counter":-1,"ids":[],"offsetType":"byte"}', 'mw' => '{"ids":[]}', 'version' => self::$defaultContentVersion, ], [ 'body_only' => true, 'pageLanguage' => new Bcp47CodeValue( 'sr' ), 'variant' => [ 'wikitext' => new Bcp47CodeValue( 'sr-Cyrl' ), 'html' => new Bcp47CodeValue( 'sr-Latn' ), ] ] ], [ 'variant', [ 'html' => '

абвг abcd x

', 'parsoid' => null, 'mw' => null, ], [ 'html' => '

абвг абцд x

', 'parsoid' => '{"counter":-1,"ids":[],"offsetType":"byte"}', 'mw' => '{"ids":[]}', 'version' => self::$defaultContentVersion, ], [ 'body_only' => true, 'pageLanguage' => new Bcp47CodeValue( 'sr' ), 'variant' => [ 'wikitext' => new Bcp47CodeValue( 'sr-Latn' ), 'html' => new Bcp47CodeValue( 'sr-Cyrl' ), ] ] ], [ 'variant', [ 'html' => '

abcd

', 'parsoid' => '{"counter":2,"ids":{"mwAA":{"dsr":[0,11,0,0]},"mwAQ":{"dsr":[0,10,0,0]},"mwAg":{"dsr":[0,10,3,3]}},"offsetType":"byte"}', 'mw' => '{"ids":[]}', ], [ 'html' => '

абцд

', 'parsoid' => '{"counter":-1,"ids":{"mwAA":{"dsr":[0,11,0,0]},"mwAQ":{"dsr":[0,10,0,0]},"mwAg":{"dsr":[0,10,3,3]}},"offsetType":"byte"}', 'mw' => '{"ids":[]}', 'version' => self::$defaultContentVersion, ], [ 'body_only' => true, 'pageLanguage' => new Bcp47CodeValue( 'sr' ), 'variant' => [ 'wikitext' => new Bcp47CodeValue( 'sr-Latn' ), 'html' => new Bcp47CodeValue( 'sr-Cyrl' ), ] ] ], // Note that id attributes are preserved, even if no data-parsoid // is provided. [ 'variant', [ 'html' => '

abcd

', 'parsoid' => null, 'mw' => null, ], [ 'html' => '

абцд

', 'parsoid' => '{"counter":-1,"ids":[],"offsetType":"byte"}', 'mw' => '{"ids":[]}', 'version' => self::$defaultContentVersion, ], [ 'body_only' => true, 'pageLanguage' => new Bcp47CodeValue( 'sr' ), 'variant' => [ 'wikitext' => new Bcp47CodeValue( 'sr-Latn' ), 'html' => new Bcp47CodeValue( 'sr-Cyrl' ), ] ] ], ]; // phpcs:enable Generic.Files.LineLength.TooLong } /** * @covers ::implementsLanguageConversionBcp47 * @dataProvider provideImplementsLanguageConversionBcp47 */ public function testImplementsLanguageConversionBcp47( string $htmlVariantCode, $expected ) { $opts = []; $siteConfig = new MockSiteConfig( $opts ); $dataAccess = new MockDataAccess( $siteConfig, $opts ); $parsoid = new Parsoid( $siteConfig, $dataAccess ); $pageContent = new MockPageContent( [ 'main' => '' ] ); $pageConfig = new MockPageConfig( $siteConfig, $opts, $pageContent ); $actual = $parsoid->implementsLanguageConversionBcp47( $pageConfig, new Bcp47CodeValue( $htmlVariantCode ) ); $this->assertEquals( $expected, $actual ); } public function provideImplementsLanguageConversionBcp47() { yield 'Variant conversion is implemented for en-x-piglatin' => [ 'en-x-piglatin', true ]; yield 'Variant conversion is not implemented for kk-latn' => [ 'kk-latn', false ]; } /** * @covers ::recordParseMetrics */ public function testParseMetrics() { $opts = []; $wt = "testing '''123'''"; $siteConfig = new MockSiteConfig( $opts ); $dataAccess = new MockDataAccess( $siteConfig, $opts ); $parsoid = new Parsoid( $siteConfig, $dataAccess ); $pageContent = new MockPageContent( [ 'main' => $wt ] ); $pageConfig = new MockPageConfig( $siteConfig, $opts, $pageContent ); $parsoid->wikitext2html( $pageConfig ); $metrics = $siteConfig->metrics(); $this->assertInstanceOf( MockMetrics::class, $metrics ); $log = $metrics->log; $this->assertContains( [ 'timing', 'entry.wt2html.pageWithOldid.size.input', 17.0 ], $log ); } /** * @covers ::recordSerializationMetrics */ public function testSerializationMetrics() { $opts = []; $html = "

hiho

"; $siteConfig = new MockSiteConfig( $opts ); $dataAccess = new MockDataAccess( $siteConfig, $opts ); $parsoid = new Parsoid( $siteConfig, $dataAccess ); $pageContent = new MockPageContent( [ 'main' => '' ] ); $pageConfig = new MockPageConfig( $siteConfig, $opts, $pageContent ); $wt = $parsoid->html2wikitext( $pageConfig, $html ); $this->assertEquals( 'hiho', $wt ); $metrics = $siteConfig->metrics(); $this->assertInstanceOf( MockMetrics::class, $metrics ); $log = $metrics->log; $this->assertCount( 14, $log ); $this->assertEquals( [ 'timing', 'entry.html2wt.size.input', 11.0 ], $log[6] ); $this->assertEquals( [ 'timing', 'entry.html2wt.size.output', 4.0 ], $log[10] ); } }