Add tests for OutputPage::addMeta and set{Index|Follow}Policy
[lhc/web/wiklou.git] / tests / phpunit / includes / OutputPageTest.php
1 <?php
2
3 /**
4 *
5 * @author Matthew Flaschen
6 *
7 * @group Output
8 *
9 * @todo factor tests in this class into providers and test methods
10 */
11 class OutputPageTest extends MediaWikiTestCase {
12 const SCREEN_MEDIA_QUERY = 'screen and (min-width: 982px)';
13 const SCREEN_ONLY_MEDIA_QUERY = 'only screen and (min-width: 982px)';
14
15 /**
16 * @covers OutputPage::addMeta
17 * @covers OutputPage::getMetaTags
18 * @covers OutputPage::getHeadLinksArray
19 */
20 public function testMetaTags() {
21 $outputPage = $this->newInstance();
22 $outputPage->addMeta( 'http:expires', '0' );
23 $outputPage->addMeta( 'keywords', 'first' );
24 $outputPage->addMeta( 'keywords', 'second' );
25
26 $expected = [
27 [ 'http:expires', '0' ],
28 [ 'keywords', 'first' ],
29 [ 'keywords', 'second' ],
30 ];
31 $this->assertSame( $expected, $outputPage->getMetaTags() );
32
33 $links = $outputPage->getHeadLinksArray();
34 $this->assertContains( '<meta http-equiv="expires" content="0"/>', $links );
35 $this->assertContains( '<meta name="keywords" content="first"/>', $links );
36 $this->assertContains( '<meta name="keywords" content="second"/>', $links );
37 $this->assertArrayNotHasKey( 'meta-robots', $links );
38 }
39
40 /**
41 * @covers OutputPage::setIndexPolicy
42 * @covers OutputPage::setFollowPolicy
43 * @covers OutputPage::getHeadLinksArray
44 */
45 public function testRobotsPolicies() {
46 $outputPage = $this->newInstance();
47 $outputPage->setIndexPolicy( 'noindex' );
48 $outputPage->setFollowPolicy( 'nofollow' );
49
50 $links = $outputPage->getHeadLinksArray();
51 $this->assertContains( '<meta name="robots" content="noindex,nofollow"/>', $links );
52 }
53
54 /**
55 * Tests a particular case of transformCssMedia, using the given input, globals,
56 * expected return, and message
57 *
58 * Asserts that $expectedReturn is returned.
59 *
60 * options['printableQuery'] - value of query string for printable, or omitted for none
61 * options['handheldQuery'] - value of query string for handheld, or omitted for none
62 * options['media'] - passed into the method under the same name
63 * options['expectedReturn'] - expected return value
64 * options['message'] - PHPUnit message for assertion
65 *
66 * @param array $args Key-value array of arguments as shown above
67 */
68 protected function assertTransformCssMediaCase( $args ) {
69 $queryData = [];
70 if ( isset( $args['printableQuery'] ) ) {
71 $queryData['printable'] = $args['printableQuery'];
72 }
73
74 if ( isset( $args['handheldQuery'] ) ) {
75 $queryData['handheld'] = $args['handheldQuery'];
76 }
77
78 $fauxRequest = new FauxRequest( $queryData, false );
79 $this->setMwGlobals( [
80 'wgRequest' => $fauxRequest,
81 ] );
82
83 $actualReturn = OutputPage::transformCssMedia( $args['media'] );
84 $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
85 }
86
87 /**
88 * Tests print requests
89 * @covers OutputPage::transformCssMedia
90 */
91 public function testPrintRequests() {
92 $this->assertTransformCssMediaCase( [
93 'printableQuery' => '1',
94 'media' => 'screen',
95 'expectedReturn' => null,
96 'message' => 'On printable request, screen returns null'
97 ] );
98
99 $this->assertTransformCssMediaCase( [
100 'printableQuery' => '1',
101 'media' => self::SCREEN_MEDIA_QUERY,
102 'expectedReturn' => null,
103 'message' => 'On printable request, screen media query returns null'
104 ] );
105
106 $this->assertTransformCssMediaCase( [
107 'printableQuery' => '1',
108 'media' => self::SCREEN_ONLY_MEDIA_QUERY,
109 'expectedReturn' => null,
110 'message' => 'On printable request, screen media query with only returns null'
111 ] );
112
113 $this->assertTransformCssMediaCase( [
114 'printableQuery' => '1',
115 'media' => 'print',
116 'expectedReturn' => '',
117 'message' => 'On printable request, media print returns empty string'
118 ] );
119 }
120
121 /**
122 * Tests screen requests, without either query parameter set
123 * @covers OutputPage::transformCssMedia
124 */
125 public function testScreenRequests() {
126 $this->assertTransformCssMediaCase( [
127 'media' => 'screen',
128 'expectedReturn' => 'screen',
129 'message' => 'On screen request, screen media type is preserved'
130 ] );
131
132 $this->assertTransformCssMediaCase( [
133 'media' => 'handheld',
134 'expectedReturn' => 'handheld',
135 'message' => 'On screen request, handheld media type is preserved'
136 ] );
137
138 $this->assertTransformCssMediaCase( [
139 'media' => self::SCREEN_MEDIA_QUERY,
140 'expectedReturn' => self::SCREEN_MEDIA_QUERY,
141 'message' => 'On screen request, screen media query is preserved.'
142 ] );
143
144 $this->assertTransformCssMediaCase( [
145 'media' => self::SCREEN_ONLY_MEDIA_QUERY,
146 'expectedReturn' => self::SCREEN_ONLY_MEDIA_QUERY,
147 'message' => 'On screen request, screen media query with only is preserved.'
148 ] );
149
150 $this->assertTransformCssMediaCase( [
151 'media' => 'print',
152 'expectedReturn' => 'print',
153 'message' => 'On screen request, print media type is preserved'
154 ] );
155 }
156
157 /**
158 * Tests handheld behavior
159 * @covers OutputPage::transformCssMedia
160 */
161 public function testHandheld() {
162 $this->assertTransformCssMediaCase( [
163 'handheldQuery' => '1',
164 'media' => 'handheld',
165 'expectedReturn' => '',
166 'message' => 'On request with handheld querystring and media is handheld, returns empty string'
167 ] );
168
169 $this->assertTransformCssMediaCase( [
170 'handheldQuery' => '1',
171 'media' => 'screen',
172 'expectedReturn' => null,
173 'message' => 'On request with handheld querystring and media is screen, returns null'
174 ] );
175 }
176
177 public static function provideTransformFilePath() {
178 $baseDir = dirname( __DIR__ ) . '/data/media';
179 return [
180 // File that matches basePath, and exists. Hash found and appended.
181 [ 'baseDir' => $baseDir, 'basePath' => '/w', '/w/test.jpg', '/w/test.jpg?edcf2' ],
182 // File that matches basePath, but not found on disk. Empty query.
183 [ 'baseDir' => $baseDir, 'basePath' => '/w', '/w/unknown.png', '/w/unknown.png?' ],
184 // File not matching basePath. Ignored.
185 [ 'baseDir' => $baseDir, 'basePath' => '/w', '/files/test.jpg' ],
186 // Empty string. Ignored.
187 [ 'baseDir' => $baseDir, 'basePath' => '/w', '', '' ],
188 // Similar path, but with domain component. Ignored.
189 [ 'baseDir' => $baseDir, 'basePath' => '/w', '//example.org/w/test.jpg' ],
190 [ 'baseDir' => $baseDir, 'basePath' => '/w', 'https://example.org/w/test.jpg' ],
191 // Unrelated path with domain component. Ignored.
192 [ 'baseDir' => $baseDir, 'basePath' => '/w', 'https://example.org/files/test.jpg' ],
193 [ 'baseDir' => $baseDir, 'basePath' => '/w', '//example.org/files/test.jpg' ],
194 // Unrelated path with domain, and empty base path (root mw install). Ignored.
195 [ 'baseDir' => $baseDir, 'basePath' => '', 'https://example.org/files/test.jpg' ],
196 [ 'baseDir' => $baseDir, 'basePath' => '', '//example.org/files/test.jpg' ], // T155310
197 ];
198 }
199
200 /**
201 * @dataProvider provideTransformFilePath
202 * @covers OutputPage::transformFilePath
203 * @covers OutputPage::transformResourcePath
204 */
205 public function testTransformResourcePath( $baseDir, $basePath, $path, $expected = null ) {
206 $this->setMwGlobals( 'IP', $baseDir );
207 $conf = new HashConfig( [ 'ResourceBasePath' => $basePath ] );
208
209 MediaWiki\suppressWarnings();
210 $actual = OutputPage::transformResourcePath( $conf, $path );
211 MediaWiki\restoreWarnings();
212
213 $this->assertEquals( $expected ?: $path, $actual );
214 }
215
216 public static function provideMakeResourceLoaderLink() {
217 // @codingStandardsIgnoreStart Generic.Files.LineLength
218 return [
219 // Single only=scripts load
220 [
221 [ 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ],
222 "<script>(window.RLQ=window.RLQ||[]).push(function(){"
223 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.foo\u0026only=scripts\u0026skin=fallback");'
224 . "});</script>"
225 ],
226 // Multiple only=styles load
227 [
228 [ [ 'test.baz', 'test.foo', 'test.bar' ], ResourceLoaderModule::TYPE_STYLES ],
229
230 '<link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?debug=false&amp;lang=en&amp;modules=test.bar%2Cbaz%2Cfoo&amp;only=styles&amp;skin=fallback"/>'
231 ],
232 // Private embed (only=scripts)
233 [
234 [ 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ],
235 "<script>(window.RLQ=window.RLQ||[]).push(function(){"
236 . "mw.test.baz({token:123});mw.loader.state({\"test.quux\":\"ready\"});"
237 . "});</script>"
238 ],
239 ];
240 // @codingStandardsIgnoreEnd
241 }
242
243 /**
244 * See ResourceLoaderClientHtmlTest for full coverage.
245 *
246 * @dataProvider provideMakeResourceLoaderLink
247 * @covers OutputPage::makeResourceLoaderLink
248 */
249 public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
250 $this->setMwGlobals( [
251 'wgResourceLoaderDebug' => false,
252 'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
253 ] );
254 $class = new ReflectionClass( 'OutputPage' );
255 $method = $class->getMethod( 'makeResourceLoaderLink' );
256 $method->setAccessible( true );
257 $ctx = new RequestContext();
258 $ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) );
259 $ctx->setLanguage( 'en' );
260 $out = new OutputPage( $ctx );
261 $rl = $out->getResourceLoader();
262 $rl->setMessageBlobStore( new NullMessageBlobStore() );
263 $rl->register( [
264 'test.foo' => new ResourceLoaderTestModule( [
265 'script' => 'mw.test.foo( { a: true } );',
266 'styles' => '.mw-test-foo { content: "style"; }',
267 ] ),
268 'test.bar' => new ResourceLoaderTestModule( [
269 'script' => 'mw.test.bar( { a: true } );',
270 'styles' => '.mw-test-bar { content: "style"; }',
271 ] ),
272 'test.baz' => new ResourceLoaderTestModule( [
273 'script' => 'mw.test.baz( { a: true } );',
274 'styles' => '.mw-test-baz { content: "style"; }',
275 ] ),
276 'test.quux' => new ResourceLoaderTestModule( [
277 'script' => 'mw.test.baz( { token: 123 } );',
278 'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
279 'group' => 'private',
280 ] ),
281 ] );
282 $links = $method->invokeArgs( $out, $args );
283 $actualHtml = strval( $links );
284 $this->assertEquals( $expectedHtml, $actualHtml );
285 }
286
287 /**
288 * @dataProvider provideVaryHeaders
289 * @covers OutputPage::addVaryHeader
290 * @covers OutputPage::getVaryHeader
291 * @covers OutputPage::getKeyHeader
292 */
293 public function testVaryHeaders( $calls, $vary, $key ) {
294 // get rid of default Vary fields
295 $outputPage = $this->getMockBuilder( 'OutputPage' )
296 ->setConstructorArgs( [ new RequestContext() ] )
297 ->setMethods( [ 'getCacheVaryCookies' ] )
298 ->getMock();
299 $outputPage->expects( $this->any() )
300 ->method( 'getCacheVaryCookies' )
301 ->will( $this->returnValue( [] ) );
302 TestingAccessWrapper::newFromObject( $outputPage )->mVaryHeader = [];
303
304 foreach ( $calls as $call ) {
305 call_user_func_array( [ $outputPage, 'addVaryHeader' ], $call );
306 }
307 $this->assertEquals( $vary, $outputPage->getVaryHeader(), 'Vary:' );
308 $this->assertEquals( $key, $outputPage->getKeyHeader(), 'Key:' );
309 }
310
311 public function provideVaryHeaders() {
312 // note: getKeyHeader() automatically adds Vary: Cookie
313 return [
314 [ // single header
315 [
316 [ 'Cookie' ],
317 ],
318 'Vary: Cookie',
319 'Key: Cookie',
320 ],
321 [ // non-unique headers
322 [
323 [ 'Cookie' ],
324 [ 'Accept-Language' ],
325 [ 'Cookie' ],
326 ],
327 'Vary: Cookie, Accept-Language',
328 'Key: Cookie,Accept-Language',
329 ],
330 [ // two headers with single options
331 [
332 [ 'Cookie', [ 'param=phpsessid' ] ],
333 [ 'Accept-Language', [ 'substr=en' ] ],
334 ],
335 'Vary: Cookie, Accept-Language',
336 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
337 ],
338 [ // one header with multiple options
339 [
340 [ 'Cookie', [ 'param=phpsessid', 'param=userId' ] ],
341 ],
342 'Vary: Cookie',
343 'Key: Cookie;param=phpsessid;param=userId',
344 ],
345 [ // Duplicate option
346 [
347 [ 'Cookie', [ 'param=phpsessid' ] ],
348 [ 'Cookie', [ 'param=phpsessid' ] ],
349 [ 'Accept-Language', [ 'substr=en', 'substr=en' ] ],
350 ],
351 'Vary: Cookie, Accept-Language',
352 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
353 ],
354 [ // Same header, different options
355 [
356 [ 'Cookie', [ 'param=phpsessid' ] ],
357 [ 'Cookie', [ 'param=userId' ] ],
358 ],
359 'Vary: Cookie',
360 'Key: Cookie;param=phpsessid;param=userId',
361 ],
362 ];
363 }
364
365 /**
366 * @covers OutputPage::haveCacheVaryCookies
367 */
368 public function testHaveCacheVaryCookies() {
369 $request = new FauxRequest();
370 $context = new RequestContext();
371 $context->setRequest( $request );
372 $outputPage = new OutputPage( $context );
373
374 // No cookies are set.
375 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
376
377 // 'Token' is present but empty, so it shouldn't count.
378 $request->setCookie( 'Token', '' );
379 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
380
381 // 'Token' present and nonempty.
382 $request->setCookie( 'Token', '123' );
383 $this->assertTrue( $outputPage->haveCacheVaryCookies() );
384 }
385
386 /*
387 * @covers OutputPage::addCategoryLinks
388 * @covers OutputPage::getCategories
389 */
390 public function testGetCategories() {
391 $fakeResultWrapper = new FakeResultWrapper( [
392 (object) [
393 'pp_value' => 1,
394 'page_title' => 'Test'
395 ],
396 (object) [
397 'page_title' => 'Test2'
398 ]
399 ] );
400 $outputPage = $this->getMockBuilder( 'OutputPage' )
401 ->setConstructorArgs( [ new RequestContext() ] )
402 ->setMethods( [ 'addCategoryLinksToLBAndGetResult' ] )
403 ->getMock();
404 $outputPage->expects( $this->any() )
405 ->method( 'addCategoryLinksToLBAndGetResult' )
406 ->will( $this->returnValue( $fakeResultWrapper ) );
407
408 $outputPage->addCategoryLinks( [
409 'Test' => 'Test',
410 'Test2' => 'Test2',
411 ] );
412 $this->assertEquals( [ 0 => 'Test', '1' => 'Test2' ], $outputPage->getCategories() );
413 $this->assertEquals( [ 0 => 'Test2' ], $outputPage->getCategories( 'normal' ) );
414 $this->assertEquals( [ 0 => 'Test' ], $outputPage->getCategories( 'hidden' ) );
415 }
416
417 /**
418 * @return OutputPage
419 */
420 private function newInstance() {
421 $context = new RequestContext();
422
423 $context->setConfig( new HashConfig( [
424 'AppleTouchIcon' => false,
425 'DisableLangConversion' => true,
426 'EnableAPI' => false,
427 'EnableCanonicalServerLink' => false,
428 'Favicon' => false,
429 'Feed' => false,
430 'LanguageCode' => false,
431 'ReferrerPolicy' => false,
432 'RightsPage' => false,
433 'RightsUrl' => false,
434 'UniversalEditButton' => false,
435 ] ) );
436
437 return new OutputPage( $context );
438 }
439 }
440
441 /**
442 * MessageBlobStore that doesn't do anything
443 */
444 class NullMessageBlobStore extends MessageBlobStore {
445 public function get( ResourceLoader $resourceLoader, $modules, $lang ) {
446 return [];
447 }
448
449 public function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
450 return false;
451 }
452
453 public function updateModule( $name, ResourceLoaderModule $module, $lang ) {
454 }
455
456 public function updateMessage( $key ) {
457 }
458
459 public function clear() {
460 }
461 }