5 * @author Matthew Flaschen
9 * @todo factor tests in this class into providers and test methods
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)';
16 * @covers OutputPage::addMeta
17 * @covers OutputPage::getMetaTags
18 * @covers OutputPage::getHeadLinksArray
20 public function testMetaTags() {
21 $outputPage = $this->newInstance();
22 $outputPage->addMeta( 'http:expires', '0' );
23 $outputPage->addMeta( 'keywords', 'first' );
24 $outputPage->addMeta( 'keywords', 'second' );
27 [ 'http:expires', '0' ],
28 [ 'keywords', 'first' ],
29 [ 'keywords', 'second' ],
31 $this->assertSame( $expected, $outputPage->getMetaTags() );
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 );
41 * @covers OutputPage::setIndexPolicy
42 * @covers OutputPage::setFollowPolicy
43 * @covers OutputPage::getHeadLinksArray
45 public function testRobotsPolicies() {
46 $outputPage = $this->newInstance();
47 $outputPage->setIndexPolicy( 'noindex' );
48 $outputPage->setFollowPolicy( 'nofollow' );
50 $links = $outputPage->getHeadLinksArray();
51 $this->assertContains( '<meta name="robots" content="noindex,nofollow"/>', $links );
55 * Tests a particular case of transformCssMedia, using the given input, globals,
56 * expected return, and message
58 * Asserts that $expectedReturn is returned.
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
66 * @param array $args Key-value array of arguments as shown above
68 protected function assertTransformCssMediaCase( $args ) {
70 if ( isset( $args['printableQuery'] ) ) {
71 $queryData['printable'] = $args['printableQuery'];
74 if ( isset( $args['handheldQuery'] ) ) {
75 $queryData['handheld'] = $args['handheldQuery'];
78 $fauxRequest = new FauxRequest( $queryData, false );
79 $this->setMwGlobals( [
80 'wgRequest' => $fauxRequest,
83 $actualReturn = OutputPage
::transformCssMedia( $args['media'] );
84 $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
88 * Tests print requests
89 * @covers OutputPage::transformCssMedia
91 public function testPrintRequests() {
92 $this->assertTransformCssMediaCase( [
93 'printableQuery' => '1',
95 'expectedReturn' => null,
96 'message' => 'On printable request, screen returns null'
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'
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'
113 $this->assertTransformCssMediaCase( [
114 'printableQuery' => '1',
116 'expectedReturn' => '',
117 'message' => 'On printable request, media print returns empty string'
122 * Tests screen requests, without either query parameter set
123 * @covers OutputPage::transformCssMedia
125 public function testScreenRequests() {
126 $this->assertTransformCssMediaCase( [
128 'expectedReturn' => 'screen',
129 'message' => 'On screen request, screen media type is preserved'
132 $this->assertTransformCssMediaCase( [
133 'media' => 'handheld',
134 'expectedReturn' => 'handheld',
135 'message' => 'On screen request, handheld media type is preserved'
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.'
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.'
150 $this->assertTransformCssMediaCase( [
152 'expectedReturn' => 'print',
153 'message' => 'On screen request, print media type is preserved'
158 * Tests handheld behavior
159 * @covers OutputPage::transformCssMedia
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'
169 $this->assertTransformCssMediaCase( [
170 'handheldQuery' => '1',
172 'expectedReturn' => null,
173 'message' => 'On request with handheld querystring and media is screen, returns null'
177 public static function provideTransformFilePath() {
178 $baseDir = dirname( __DIR__
) . '/data/media';
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
201 * @dataProvider provideTransformFilePath
202 * @covers OutputPage::transformFilePath
203 * @covers OutputPage::transformResourcePath
205 public function testTransformResourcePath( $baseDir, $basePath, $path, $expected = null ) {
206 $this->setMwGlobals( 'IP', $baseDir );
207 $conf = new HashConfig( [ 'ResourceBasePath' => $basePath ] );
209 MediaWiki\
suppressWarnings();
210 $actual = OutputPage
::transformResourcePath( $conf, $path );
211 MediaWiki\restoreWarnings
();
213 $this->assertEquals( $expected ?
: $path, $actual );
216 public static function provideMakeResourceLoaderLink() {
217 // @codingStandardsIgnoreStart Generic.Files.LineLength
219 // Single only=scripts load
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");'
226 // Multiple only=styles load
228 [ [ 'test.baz', 'test.foo', 'test.bar' ], ResourceLoaderModule
::TYPE_STYLES
],
230 '<link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.bar%2Cbaz%2Cfoo&only=styles&skin=fallback"/>'
232 // Private embed (only=scripts)
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\"});"
240 // @codingStandardsIgnoreEnd
244 * See ResourceLoaderClientHtmlTest for full coverage.
246 * @dataProvider provideMakeResourceLoaderLink
247 * @covers OutputPage::makeResourceLoaderLink
249 public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
250 $this->setMwGlobals( [
251 'wgResourceLoaderDebug' => false,
252 'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
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() );
264 'test.foo' => new ResourceLoaderTestModule( [
265 'script' => 'mw.test.foo( { a: true } );',
266 'styles' => '.mw-test-foo { content: "style"; }',
268 'test.bar' => new ResourceLoaderTestModule( [
269 'script' => 'mw.test.bar( { a: true } );',
270 'styles' => '.mw-test-bar { content: "style"; }',
272 'test.baz' => new ResourceLoaderTestModule( [
273 'script' => 'mw.test.baz( { a: true } );',
274 'styles' => '.mw-test-baz { content: "style"; }',
276 'test.quux' => new ResourceLoaderTestModule( [
277 'script' => 'mw.test.baz( { token: 123 } );',
278 'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
279 'group' => 'private',
282 $links = $method->invokeArgs( $out, $args );
283 $actualHtml = strval( $links );
284 $this->assertEquals( $expectedHtml, $actualHtml );
288 * @dataProvider provideVaryHeaders
289 * @covers OutputPage::addVaryHeader
290 * @covers OutputPage::getVaryHeader
291 * @covers OutputPage::getKeyHeader
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' ] )
299 $outputPage->expects( $this->any() )
300 ->method( 'getCacheVaryCookies' )
301 ->will( $this->returnValue( [] ) );
302 TestingAccessWrapper
::newFromObject( $outputPage )->mVaryHeader
= [];
304 foreach ( $calls as $call ) {
305 call_user_func_array( [ $outputPage, 'addVaryHeader' ], $call );
307 $this->assertEquals( $vary, $outputPage->getVaryHeader(), 'Vary:' );
308 $this->assertEquals( $key, $outputPage->getKeyHeader(), 'Key:' );
311 public function provideVaryHeaders() {
312 // note: getKeyHeader() automatically adds Vary: Cookie
321 [ // non-unique headers
324 [ 'Accept-Language' ],
327 'Vary: Cookie, Accept-Language',
328 'Key: Cookie,Accept-Language',
330 [ // two headers with single options
332 [ 'Cookie', [ 'param=phpsessid' ] ],
333 [ 'Accept-Language', [ 'substr=en' ] ],
335 'Vary: Cookie, Accept-Language',
336 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
338 [ // one header with multiple options
340 [ 'Cookie', [ 'param=phpsessid', 'param=userId' ] ],
343 'Key: Cookie;param=phpsessid;param=userId',
345 [ // Duplicate option
347 [ 'Cookie', [ 'param=phpsessid' ] ],
348 [ 'Cookie', [ 'param=phpsessid' ] ],
349 [ 'Accept-Language', [ 'substr=en', 'substr=en' ] ],
351 'Vary: Cookie, Accept-Language',
352 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
354 [ // Same header, different options
356 [ 'Cookie', [ 'param=phpsessid' ] ],
357 [ 'Cookie', [ 'param=userId' ] ],
360 'Key: Cookie;param=phpsessid;param=userId',
366 * @covers OutputPage::haveCacheVaryCookies
368 public function testHaveCacheVaryCookies() {
369 $request = new FauxRequest();
370 $context = new RequestContext();
371 $context->setRequest( $request );
372 $outputPage = new OutputPage( $context );
374 // No cookies are set.
375 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
377 // 'Token' is present but empty, so it shouldn't count.
378 $request->setCookie( 'Token', '' );
379 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
381 // 'Token' present and nonempty.
382 $request->setCookie( 'Token', '123' );
383 $this->assertTrue( $outputPage->haveCacheVaryCookies() );
387 * @covers OutputPage::addCategoryLinks
388 * @covers OutputPage::getCategories
390 public function testGetCategories() {
391 $fakeResultWrapper = new FakeResultWrapper( [
394 'page_title' => 'Test'
397 'page_title' => 'Test2'
400 $outputPage = $this->getMockBuilder( 'OutputPage' )
401 ->setConstructorArgs( [ new RequestContext() ] )
402 ->setMethods( [ 'addCategoryLinksToLBAndGetResult' ] )
404 $outputPage->expects( $this->any() )
405 ->method( 'addCategoryLinksToLBAndGetResult' )
406 ->will( $this->returnValue( $fakeResultWrapper ) );
408 $outputPage->addCategoryLinks( [
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' ) );
420 private function newInstance() {
421 $context = new RequestContext();
423 $context->setConfig( new HashConfig( [
424 'AppleTouchIcon' => false,
425 'DisableLangConversion' => true,
426 'EnableAPI' => false,
427 'EnableCanonicalServerLink' => false,
430 'LanguageCode' => false,
431 'ReferrerPolicy' => false,
432 'RightsPage' => false,
433 'RightsUrl' => false,
434 'UniversalEditButton' => false,
437 return new OutputPage( $context );
442 * MessageBlobStore that doesn't do anything
444 class NullMessageBlobStore
extends MessageBlobStore
{
445 public function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
449 public function insertMessageBlob( $name, ResourceLoaderModule
$module, $lang ) {
453 public function updateModule( $name, ResourceLoaderModule
$module, $lang ) {
456 public function updateMessage( $key ) {
459 public function clear() {