Debian Codegraph @

tests/phpunit/integration/includes/Rest/Handler/TransformHandlerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest\Handler;
4
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\Rest\Handler\Helper\ParsoidFormatHelper;
7 use MediaWiki\Rest\Handler\TransformHandler;
8 use MediaWiki\Rest\RequestData;
9 use MediaWiki\Rest\RequestInterface;
10 use MediaWikiIntegrationTestCase;
11 use Wikimedia\Parsoid\Parsoid;
12
13 /**
14 * @group Database
15 */
16 class TransformHandlerTest extends MediaWikiIntegrationTestCase {
17 use HandlerTestTrait;
18
19 public static function provideRequest() {
20 $profileVersion = Parsoid::AVAILABLE_VERSIONS[0];
21 $htmlProfileUri = 'https://www.mediawiki.org/wiki/Specs/HTML/' . $profileVersion;
22 $htmlContentType = "text/html; charset=utf-8; profile=\"$htmlProfileUri\"";
23 $pbProfileUri = 'https://www.mediawiki.org/wiki/Specs/pagebundle/' . $profileVersion;
24 $pbContentType = "application/json; charset=utf-8; profile=\"$pbProfileUri\"";
25
26 $wikitextProfileUri = 'https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0';
27
28 $defaultParams = [
29 'method' => 'POST',
30 'headers' => [
31 'content-type' => 'application/json',
32 ],
33 ];
34
35 // Convert wikitext to HTML ////////////////////////////////////////////////////////////////
36 $request = new RequestData( [
37 'pathParams' => [
38 'from' => ParsoidFormatHelper::FORMAT_WIKITEXT,
39 'format' => ParsoidFormatHelper::FORMAT_HTML,
40 ],
41 'bodyContents' => json_encode( [
42 'wikitext' => '== h2 ==',
43 ] )
44 ] + $defaultParams );
45
46 yield 'should transform wikitext to HTML' => [
47 $request,
48 '>h2</h2>',
49 200,
50 [ 'content-type' => $htmlContentType ],
51 ];
52
53 // Convert HTML to wikitext ////////////////////////////////////////////////////////////////
54 $request = new RequestData( [
55 'pathParams' => [
56 'from' => ParsoidFormatHelper::FORMAT_HTML,
57 'format' => ParsoidFormatHelper::FORMAT_WIKITEXT,
58 ],
59 'bodyContents' => json_encode( [
60 'html' => '<pre>hi ho</pre>',
61 ] )
62 ] + $defaultParams );
63
64 yield 'should transform HTML to wikitext' => [
65 $request,
66 'hi ho',
67 200,
68 [ 'content-type' => "text/plain; charset=utf-8; profile=\"$wikitextProfileUri\"" ],
69 ];
70
71 // Perform language variant conversion //////////////////////////////////////////////////////
72 $request = new RequestData( [
73 'pathParams' => [
74 'from' => ParsoidFormatHelper::FORMAT_PAGEBUNDLE,
75 'format' => ParsoidFormatHelper::FORMAT_PAGEBUNDLE,
76 ],
77 'bodyContents' => json_encode( [
78 // NOTE: input for pb2pb is expected in the 'original' structure for some reason
79 'original' => [
80 'html' => [
81 'headers' => [
82 'content-type' => $htmlContentType,
83 ],
84 'body' => '<p>test language conversion</p>',
85 ],
86 ],
87 'updates' => [
88 'variant' => [
89 'source' => 'en',
90 'target' => 'en-x-piglatin'
91 ]
92 ]
93 ] ),
94 'headers' => [
95 'content-type' => 'application/json',
96 'content-language' => 'en',
97 'accept-language' => 'en-x-piglatin',
98 ]
99 ] + $defaultParams );
100
101 yield 'should apply language variant conversion' => [
102 $request,
103 [
104 // pig latin!
105 '>esttay anguagelay onversioncay<',
106 // NOTE: quotes are escaped because this is embedded in JSON
107 '<meta http-equiv=\"content-language\" content=\"en-x-piglatin\"/>'
108 ],
109 200,
110 // NOTE: Parsoid returns a content-language header in the page bundle,
111 // but that header is not applied to the HTTP response, which is JSON.
112 [ 'content-type' => $pbContentType ],
113 ];
114 }
115
116 /**
117 * @dataProvider provideRequest
118 * @covers \MediaWiki\Rest\Handler\TransformHandler::execute
119 */
120 public function testRequest(
121 RequestInterface $request,
122 $expectedText,
123 $expectedStatus = 200,
124 $expectedHeaders = []
125 ) {
126 $this->overrideConfigValue( MainConfigNames::UsePigLatinVariant, true );
127
128 $revisionLookup = $this->getServiceContainer()->getRevisionLookup();
129 $dataAccess = $this->getServiceContainer()->getParsoidDataAccess();
130 $siteConfig = $this->getServiceContainer()->getParsoidSiteConfig();
131 $pageConfigFactory = $this->getServiceContainer()->getParsoidPageConfigFactory();
132
133 $handler = new TransformHandler(
134 $revisionLookup,
135 $siteConfig,
136 $pageConfigFactory,
137 $dataAccess
138 );
139 $response = $this->executeHandler( $handler, $request );
140 $response->getBody()->rewind();
141 $data = $response->getBody()->getContents();
142
143 $this->assertSame( $expectedStatus, $response->getStatusCode(), 'Status' );
144
145 foreach ( (array)$expectedText as $txt ) {
146 $this->assertStringContainsString( $txt, $data );
147 }
148
149 foreach ( $expectedHeaders as $key => $expectedHeader ) {
150 $this->assertSame( $expectedHeader, $response->getHeaderLine( $key ), $key );
151 }
152 }
153
154 }