'POST', 'headers' => [ 'content-type' => 'application/json', ], ]; // Convert wikitext to HTML //////////////////////////////////////////////////////////////// $request = new RequestData( [ 'pathParams' => [ 'from' => ParsoidFormatHelper::FORMAT_WIKITEXT, 'format' => ParsoidFormatHelper::FORMAT_HTML, ], 'bodyContents' => json_encode( [ 'wikitext' => '== h2 ==', ] ) ] + $defaultParams ); yield 'should transform wikitext to HTML' => [ $request, '>h2', 200, [ 'content-type' => $htmlContentType ], ]; // Convert HTML to wikitext //////////////////////////////////////////////////////////////// $request = new RequestData( [ 'pathParams' => [ 'from' => ParsoidFormatHelper::FORMAT_HTML, 'format' => ParsoidFormatHelper::FORMAT_WIKITEXT, ], 'bodyContents' => json_encode( [ 'html' => '
hi ho
', ] ) ] + $defaultParams ); yield 'should transform HTML to wikitext' => [ $request, 'hi ho', 200, [ 'content-type' => "text/plain; charset=utf-8; profile=\"$wikitextProfileUri\"" ], ]; // Perform language variant conversion ////////////////////////////////////////////////////// $request = new RequestData( [ 'pathParams' => [ 'from' => ParsoidFormatHelper::FORMAT_PAGEBUNDLE, 'format' => ParsoidFormatHelper::FORMAT_PAGEBUNDLE, ], 'bodyContents' => json_encode( [ // NOTE: input for pb2pb is expected in the 'original' structure for some reason 'original' => [ 'html' => [ 'headers' => [ 'content-type' => $htmlContentType, ], 'body' => '

test language conversion

', ], ], 'updates' => [ 'variant' => [ 'source' => 'en', 'target' => 'en-x-piglatin' ] ] ] ), 'headers' => [ 'content-type' => 'application/json', 'content-language' => 'en', 'accept-language' => 'en-x-piglatin', ] ] + $defaultParams ); yield 'should apply language variant conversion' => [ $request, [ // pig latin! '>esttay anguagelay onversioncay<', // NOTE: quotes are escaped because this is embedded in JSON '' ] , 200, // NOTE: Parsoid returns a content-language header in the page bundle, // but that header is not applied to the HTTP response, which is JSON. [ 'content-type' => $pbContentType ], ]; } /** * @dataProvider provideRequest * @covers \MediaWiki\Rest\Handler\TransformHandler::execute */ public function testRequest( RequestInterface $request, $expectedText, $expectedStatus = 200, $expectedHeaders = [] ) { $parsoidSettings = MainConfigSchema::getDefaultValue( MainConfigNames::ParsoidSettings ); $dataAccess = $this->getServiceContainer()->getParsoidDataAccess(); $siteConfig = $this->getServiceContainer()->getParsoidSiteConfig(); $pageConfigFactory = $this->getServiceContainer()->getParsoidPageConfigFactory(); $handler = new TransformHandler( $parsoidSettings, $siteConfig, $pageConfigFactory, $dataAccess ); $response = $this->executeHandler( $handler, $request ); $response->getBody()->rewind(); $data = $response->getBody()->getContents(); $this->assertSame( $expectedStatus, $response->getStatusCode(), 'Status' ); foreach ( (array)$expectedText as $txt ) { $this->assertStringContainsString( $txt, $data ); } foreach ( $expectedHeaders as $key => $expectedHeader ) { $this->assertSame( $expectedHeader, $response->getHeaderLine( $key ), $key ); } } }