5 * @author Matthew Flaschen
9 * @todo factor tests in this class into providers and test methods
12 class OutputPageTest
extends MediaWikiTestCase
{
13 const SCREEN_MEDIA_QUERY
= 'screen and (min-width: 982px)';
14 const SCREEN_ONLY_MEDIA_QUERY
= 'only screen and (min-width: 982px)';
17 * Tests a particular case of transformCssMedia, using the given input, globals,
18 * expected return, and message
20 * Asserts that $expectedReturn is returned.
22 * options['printableQuery'] - value of query string for printable, or omitted for none
23 * options['handheldQuery'] - value of query string for handheld, or omitted for none
24 * options['media'] - passed into the method under the same name
25 * options['expectedReturn'] - expected return value
26 * options['message'] - PHPUnit message for assertion
28 * @param array $args Key-value array of arguments as shown above
30 protected function assertTransformCssMediaCase( $args ) {
32 if ( isset( $args['printableQuery'] ) ) {
33 $queryData['printable'] = $args['printableQuery'];
36 if ( isset( $args['handheldQuery'] ) ) {
37 $queryData['handheld'] = $args['handheldQuery'];
40 $fauxRequest = new FauxRequest( $queryData, false );
41 $this->setMwGlobals( array(
42 'wgRequest' => $fauxRequest,
45 $actualReturn = OutputPage
::transformCssMedia( $args['media'] );
46 $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
50 * Tests print requests
51 * @covers OutputPage::transformCssMedia
53 public function testPrintRequests() {
54 $this->assertTransformCssMediaCase( array(
55 'printableQuery' => '1',
57 'expectedReturn' => null,
58 'message' => 'On printable request, screen returns null'
61 $this->assertTransformCssMediaCase( array(
62 'printableQuery' => '1',
63 'media' => self
::SCREEN_MEDIA_QUERY
,
64 'expectedReturn' => null,
65 'message' => 'On printable request, screen media query returns null'
68 $this->assertTransformCssMediaCase( array(
69 'printableQuery' => '1',
70 'media' => self
::SCREEN_ONLY_MEDIA_QUERY
,
71 'expectedReturn' => null,
72 'message' => 'On printable request, screen media query with only returns null'
75 $this->assertTransformCssMediaCase( array(
76 'printableQuery' => '1',
78 'expectedReturn' => '',
79 'message' => 'On printable request, media print returns empty string'
84 * Tests screen requests, without either query parameter set
85 * @covers OutputPage::transformCssMedia
87 public function testScreenRequests() {
88 $this->assertTransformCssMediaCase( array(
90 'expectedReturn' => 'screen',
91 'message' => 'On screen request, screen media type is preserved'
94 $this->assertTransformCssMediaCase( array(
95 'media' => 'handheld',
96 'expectedReturn' => 'handheld',
97 'message' => 'On screen request, handheld media type is preserved'
100 $this->assertTransformCssMediaCase( array(
101 'media' => self
::SCREEN_MEDIA_QUERY
,
102 'expectedReturn' => self
::SCREEN_MEDIA_QUERY
,
103 'message' => 'On screen request, screen media query is preserved.'
106 $this->assertTransformCssMediaCase( array(
107 'media' => self
::SCREEN_ONLY_MEDIA_QUERY
,
108 'expectedReturn' => self
::SCREEN_ONLY_MEDIA_QUERY
,
109 'message' => 'On screen request, screen media query with only is preserved.'
112 $this->assertTransformCssMediaCase( array(
114 'expectedReturn' => 'print',
115 'message' => 'On screen request, print media type is preserved'
120 * Tests handheld behavior
121 * @covers OutputPage::transformCssMedia
123 public function testHandheld() {
124 $this->assertTransformCssMediaCase( array(
125 'handheldQuery' => '1',
126 'media' => 'handheld',
127 'expectedReturn' => '',
128 'message' => 'On request with handheld querystring and media is handheld, returns empty string'
131 $this->assertTransformCssMediaCase( array(
132 'handheldQuery' => '1',
134 'expectedReturn' => null,
135 'message' => 'On request with handheld querystring and media is screen, returns null'
139 public static function provideMakeResourceLoaderLink() {
140 // @codingStandardsIgnoreStart Generic.Files.LineLength
142 // Load module script only
144 array( 'test.foo', ResourceLoaderModule
::TYPE_SCRIPTS
),
145 "<script>window.RLQ = window.RLQ || []; window.RLQ.push( function () {\n"
146 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.foo\u0026only=scripts\u0026skin=fallback");'
150 // Don't condition wrap raw modules (like the startup module)
151 array( 'test.raw', ResourceLoaderModule
::TYPE_SCRIPTS
),
152 '<script async src="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.raw&only=scripts&skin=fallback"></script>'
154 // Load module styles only
155 // This also tests the order the modules are put into the url
157 array( array( 'test.baz', 'test.foo', 'test.bar' ), ResourceLoaderModule
::TYPE_STYLES
),
159 '<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">'
161 // Load private module (only=scripts)
163 array( 'test.quux', ResourceLoaderModule
::TYPE_SCRIPTS
),
164 "<script>window.RLQ = window.RLQ || []; window.RLQ.push( function () {\n"
165 . "mw.test.baz({token:123});mw.loader.state({\"test.quux\":\"ready\"});\n"
168 // Load private module (combined)
170 array( 'test.quux', ResourceLoaderModule
::TYPE_COMBINED
),
171 "<script>window.RLQ = window.RLQ || []; window.RLQ.push( function () {\n"
172 . "mw.loader.implement(\"test.quux\",function($,jQuery){"
173 . "mw.test.baz({token:123});},{\"css\":[\".mw-icon{transition:none}"
174 . "\"]});\n} );</script>"
178 array( array(), ResourceLoaderModule
::TYPE_COMBINED
),
183 array( 'test.noscript', ResourceLoaderModule
::TYPE_STYLES
),
184 '<noscript><link rel=stylesheet href="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.noscript&only=styles&skin=fallback"></noscript>'
186 // Load two modules in separate groups
188 array( array( 'test.group.foo', 'test.group.bar' ), ResourceLoaderModule
::TYPE_COMBINED
),
189 "<script>window.RLQ = window.RLQ || []; window.RLQ.push( function () {\n"
190 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.group.bar\u0026skin=fallback");'
191 . "\n} );</script>\n"
192 . "<script>window.RLQ = window.RLQ || []; window.RLQ.push( function () {\n"
193 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.group.foo\u0026skin=fallback");'
197 // @codingStandardsIgnoreEnd
201 * @dataProvider provideMakeResourceLoaderLink
202 * @covers OutputPage::makeResourceLoaderLink
203 * @covers ResourceLoader::makeLoaderImplementScript
204 * @covers ResourceLoader::makeModuleResponse
205 * @covers ResourceLoader::makeInlineScript
206 * @covers ResourceLoader::makeLoaderStateScript
207 * @covers ResourceLoader::createLoaderURL
209 public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
210 $this->setMwGlobals( array(
211 'wgResourceLoaderDebug' => false,
212 'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
213 // Affects whether CDATA is inserted
214 'wgWellFormedXml' => false,
216 $class = new ReflectionClass( 'OutputPage' );
217 $method = $class->getMethod( 'makeResourceLoaderLink' );
218 $method->setAccessible( true );
219 $ctx = new RequestContext();
220 $ctx->setSkin( SkinFactory
::getDefaultInstance()->makeSkin( 'fallback' ) );
221 $ctx->setLanguage( 'en' );
222 $out = new OutputPage( $ctx );
223 $rl = $out->getResourceLoader();
224 $rl->setMessageBlobStore( new NullMessageBlobStore() );
225 $rl->register( array(
226 'test.foo' => new ResourceLoaderTestModule( array(
227 'script' => 'mw.test.foo( { a: true } );',
228 'styles' => '.mw-test-foo { content: "style"; }',
230 'test.bar' => new ResourceLoaderTestModule( array(
231 'script' => 'mw.test.bar( { a: true } );',
232 'styles' => '.mw-test-bar { content: "style"; }',
234 'test.baz' => new ResourceLoaderTestModule( array(
235 'script' => 'mw.test.baz( { a: true } );',
236 'styles' => '.mw-test-baz { content: "style"; }',
238 'test.quux' => new ResourceLoaderTestModule( array(
239 'script' => 'mw.test.baz( { token: 123 } );',
240 'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
241 'group' => 'private',
243 'test.raw' => new ResourceLoaderTestModule( array(
244 'script' => 'mw.test.baz( { token: 123 } );',
247 'test.noscript' => new ResourceLoaderTestModule( array(
248 'styles' => '.mw-test-noscript { content: "style"; }',
249 'group' => 'noscript',
251 'test.group.bar' => new ResourceLoaderTestModule( array(
252 'styles' => '.mw-group-bar { content: "style"; }',
255 'test.group.foo' => new ResourceLoaderTestModule( array(
256 'styles' => '.mw-group-foo { content: "style"; }',
260 $links = $method->invokeArgs( $out, $args );
261 $actualHtml = implode( "\n", $links['html'] );
262 $this->assertEquals( $expectedHtml, $actualHtml );
266 * @dataProvider provideVaryHeaders
267 * @covers OutputPage::addVaryHeader
268 * @covers OutputPage::getVaryHeader
269 * @covers OutputPage::getKeyHeader
271 public function testVaryHeaders( $calls, $vary, $key ) {
272 // get rid of default Vary fields
273 $outputPage = $this->getMockBuilder( 'OutputPage' )
274 ->setConstructorArgs( array( new RequestContext() ) )
275 ->setMethods( array( 'getCacheVaryCookies' ) )
277 $outputPage->expects( $this->any() )
278 ->method( 'getCacheVaryCookies' )
279 ->will( $this->returnValue( array() ) );
280 TestingAccessWrapper
::newFromObject( $outputPage )->mVaryHeader
= array();
282 foreach ( $calls as $call ) {
283 call_user_func_array( array( $outputPage, 'addVaryHeader' ), $call );
285 $this->assertEquals( $vary, $outputPage->getVaryHeader(), 'Vary:' );
286 $this->assertEquals( $key, $outputPage->getKeyHeader(), 'Key:' );
289 public function provideVaryHeaders() {
290 // note: getKeyHeader() automatically adds Vary: Cookie
292 array( // single header
299 array( // non-unique headers
302 array( 'Accept-Language' ),
305 'Vary: Cookie, Accept-Language',
306 'Key: Cookie,Accept-Language',
308 array( // two headers with single options
310 array( 'Cookie', array( 'param=phpsessid' ) ),
311 array( 'Accept-Language', array( 'substr=en' ) ),
313 'Vary: Cookie, Accept-Language',
314 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
316 array( // one header with multiple options
318 array( 'Cookie', array( 'param=phpsessid', 'param=userId' ) ),
321 'Key: Cookie;param=phpsessid;param=userId',
323 array( // Duplicate option
325 array( 'Cookie', array( 'param=phpsessid' ) ),
326 array( 'Cookie', array( 'param=phpsessid' ) ),
327 array( 'Accept-Language', array( 'substr=en', 'substr=en' ) ),
329 'Vary: Cookie, Accept-Language',
330 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
332 array( // Same header, different options
334 array( 'Cookie', array( 'param=phpsessid' ) ),
335 array( 'Cookie', array( 'param=userId' ) ),
338 'Key: Cookie;param=phpsessid;param=userId',
344 * @covers OutputPage::haveCacheVaryCookies
346 function testHaveCacheVaryCookies() {
347 $request = new FauxRequest();
348 $context = new RequestContext();
349 $context->setRequest( $request );
350 $outputPage = new OutputPage( $context );
352 // No cookies are set.
353 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
355 // 'Token' is present but empty, so it shouldn't count.
356 $request->setCookie( 'Token', '' );
357 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
359 // 'Token' present and nonempty.
360 $request->setCookie( 'Token', '123' );
361 $this->assertTrue( $outputPage->haveCacheVaryCookies() );
366 * MessageBlobStore that doesn't do anything
368 class NullMessageBlobStore
extends MessageBlobStore
{
369 public function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
373 public function insertMessageBlob( $name, ResourceLoaderModule
$module, $lang ) {
377 public function updateModule( $name, ResourceLoaderModule
$module, $lang ) {
380 public function updateMessage( $key ) {
383 public function clear() {