Merge "TransformHandler: add test for variant conversion"

This commit is contained in:
jenkins-bot 2022-09-26 16:39:09 +00:00 committed by Gerrit Code Review
commit 33706fd187
3 changed files with 139 additions and 37 deletions

View file

@ -97,6 +97,7 @@
"psy/psysh": "^0.11.1",
"seld/jsonlint": "1.8.3",
"wikimedia/alea": "^0.9.3",
"wikimedia/langconv": "^0.4.2",
"wikimedia/testing-access-wrapper": "~2.0",
"wmde/hamcrest-html-matchers": "^1.0.0"
},

View file

@ -70,6 +70,11 @@ class ParsoidHandlerTest extends MediaWikiIntegrationTestCase {
],
];
public function setUp(): void {
// enable Pig Latin variant conversion
$this->overrideConfigValue( 'UsePigLatinVariant', true );
}
private function createRouter( $authority, $request ) {
return $this->newRouter( [
'authority' => $authority,
@ -1552,35 +1557,82 @@ class ParsoidHandlerTest extends MediaWikiIntegrationTestCase {
$this->assertSame( $wikitext, $actual );
}
public function provideGetRequestAttributes() {
// TODO: oldid in path
// TODO: oldid in body
// TODO: html as string
// TODO: html with headers
// TODO: wikitext override (tryToCreatePageConfig)
// TODO: wikitext loaded (tryToCreatePageConfig)
// TODO: ...
public function provideLanguageConversion() {
$profileVersion = Parsoid::AVAILABLE_VERSIONS[0];
$htmlProfileUri = 'https://www.mediawiki.org/wiki/Specs/HTML/' . $profileVersion;
$htmlContentType = "text/html; charset=utf-8; profile=\"$htmlProfileUri\"";
$defaultAttribs = [
'oldid' => null,
'pageName' => __METHOD__,
'opts' => [],
'envOptions' => [
'inputContentVersion' => Parsoid::defaultHTMLVersion()
]
];
$attribs = [
'pagelanguage' => 'en',
'opts' => [
'updates' => [
'variant' => [
'source' => 'en',
'target' => 'en-x-piglatin'
]
],
],
] + $defaultAttribs;
$revision = [
'contentmodel' => CONTENT_MODEL_WIKITEXT,
'html' => [
'headers' => [
'content-type' => $htmlContentType,
],
'body' => '<p>test language conversion</p>',
],
];
yield [
$attribs,
$revision,
'>esttay anguagelay onversioncay<',
[
'content-type' => $htmlContentType,
'content-language' => 'en-x-piglatin',
]
];
}
/**
* @dataProvider provideGetRequestAttributes
* @dataProvider provideLanguageConversion
*/
public function testGetRequestAttributes() {
// TODO: also test tryToCreatePageConfig
$this->fail( 'TBD' );
}
public function testLanguageConversion(
array $attribs,
array $revision,
string $expectedText,
array $expectedHeaders
) {
$handler = $this->newParsoidHandler();
public function provideGetRequestAttributesThrows() {
// TODO: should require html when serializing
// TODO: should error when revision not found
}
$pageConfig = $handler->tryToCreatePageConfig( $attribs, null, true );
$response = $handler->languageConversion( $pageConfig, $attribs, $revision );
/**
* @dataProvider provideGetRequestAttributesThrows
*/
public function testGetRequestAttributesThrows() {
// TODO: also test tryToCreatePageConfig
$this->fail( 'TBD' );
$body = $response->getBody();
$body->rewind();
$actual = $body->getContents();
$pb = json_decode( $actual, true );
$this->assertNotEmpty( $pb );
$this->assertArrayHasKey( 'html', $pb );
$this->assertArrayHasKey( 'body', $pb['html'] );
$this->assertStringContainsString( $expectedText, $pb['html']['body'] );
foreach ( $expectedHeaders as $key => $value ) {
$this->assertArrayHasKey( $key, $pb['html']['headers'] );
$this->assertSame( $value, $pb['html']['headers'][$key] );
}
}
}

View file

@ -8,8 +8,6 @@ use MediaWiki\Rest\Handler\ParsoidFormatHelper;
use MediaWiki\Rest\Handler\TransformHandler;
use MediaWiki\Rest\RequestData;
use MediaWiki\Rest\RequestInterface;
use MediaWiki\Rest\Response;
use MediaWiki\Rest\StringStream;
use MediaWikiIntegrationTestCase;
use Wikimedia\Parsoid\Parsoid;
@ -19,9 +17,18 @@ use Wikimedia\Parsoid\Parsoid;
class TransformHandlerTest extends MediaWikiIntegrationTestCase {
use HandlerTestTrait;
public function setUp(): void {
// enable Pig Latin variant conversion
$this->overrideConfigValue( 'UsePigLatinVariant', true );
}
public function provideRequest() {
$profileVersion = Parsoid::AVAILABLE_VERSIONS[0];
$htmlProfileUri = 'https://www.mediawiki.org/wiki/Specs/HTML/' . $profileVersion;
$htmlContentType = "text/html; charset=utf-8; profile=\"$htmlProfileUri\"";
$pbProfileUri = 'https://www.mediawiki.org/wiki/Specs/pagebundle/' . $profileVersion;
$pbContentType = "application/json; charset=utf-8; profile=\"$pbProfileUri\"";
$wikitextProfileUri = 'https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0';
$defaultParams = [
@ -32,7 +39,7 @@ class TransformHandlerTest extends MediaWikiIntegrationTestCase {
];
// Convert wikitext to HTML ////////////////////////////////////////////////////////////////
$request = new RequestData( $defaultParams + [
$request = new RequestData( [
'pathParams' => [
'from' => ParsoidFormatHelper::FORMAT_WIKITEXT,
'format' => ParsoidFormatHelper::FORMAT_HTML,
@ -40,20 +47,17 @@ class TransformHandlerTest extends MediaWikiIntegrationTestCase {
'bodyContents' => json_encode( [
'wikitext' => '== h2 ==',
] )
] );
$response = new Response();
$response->setBody( new StringStream( '<h2> h2 </h2>' ) );
] + $defaultParams );
yield 'should transform wikitext to HTML' => [
$request,
'>h2</h2>',
200,
[ 'content-type' => "text/html; charset=utf-8; profile=\"$htmlProfileUri\"" ],
[ 'content-type' => $htmlContentType ],
];
// Convert HTML to wikitext ////////////////////////////////////////////////////////////////
$request = new RequestData( $defaultParams + [
$request = new RequestData( [
'pathParams' => [
'from' => ParsoidFormatHelper::FORMAT_HTML,
'format' => ParsoidFormatHelper::FORMAT_WIKITEXT,
@ -61,10 +65,7 @@ class TransformHandlerTest extends MediaWikiIntegrationTestCase {
'bodyContents' => json_encode( [
'html' => '<pre>hi ho</pre>',
] )
] );
$response = new Response();
$response->setBody( new StringStream( '<h2> h2 </h2>' ) );
] + $defaultParams );
yield 'should transform HTML to wikitext' => [
$request,
@ -72,6 +73,50 @@ class TransformHandlerTest extends MediaWikiIntegrationTestCase {
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' => '<p>test language conversion</p>',
],
],
'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
'<meta http-equiv=\"content-language\" content=\"en-x-piglatin\"/>'
] ,
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 ],
];
}
/**
@ -98,9 +143,13 @@ class TransformHandlerTest extends MediaWikiIntegrationTestCase {
);
$response = $this->executeHandler( $handler, $request );
$response->getBody()->rewind();
$data = $response->getBody()->getContents();
$this->assertSame( $expectedStatus, $response->getStatusCode(), 'Status' );
$this->assertStringContainsString( $expectedText, $response->getBody()->getContents() );
foreach ( (array)$expectedText as $txt ) {
$this->assertStringContainsString( $txt, $data );
}
foreach ( $expectedHeaders as $key => $expectedHeader ) {
$this->assertSame( $expectedHeader, $response->getHeaderLine( $key ), $key );