Merge "SpecialMovepage: Convert form to use OOUI controls"
[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 */
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)';
15
16 /**
17 * Tests a particular case of transformCssMedia, using the given input, globals,
18 * expected return, and message
19 *
20 * Asserts that $expectedReturn is returned.
21 *
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
27 *
28 * @param array $args Key-value array of arguments as shown above
29 */
30 protected function assertTransformCssMediaCase( $args ) {
31 $queryData = array();
32 if ( isset( $args['printableQuery'] ) ) {
33 $queryData['printable'] = $args['printableQuery'];
34 }
35
36 if ( isset( $args['handheldQuery'] ) ) {
37 $queryData['handheld'] = $args['handheldQuery'];
38 }
39
40 $fauxRequest = new FauxRequest( $queryData, false );
41 $this->setMwGlobals( array(
42 'wgRequest' => $fauxRequest,
43 ) );
44
45 $actualReturn = OutputPage::transformCssMedia( $args['media'] );
46 $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
47 }
48
49 /**
50 * Tests print requests
51 * @covers OutputPage::transformCssMedia
52 */
53 public function testPrintRequests() {
54 $this->assertTransformCssMediaCase( array(
55 'printableQuery' => '1',
56 'media' => 'screen',
57 'expectedReturn' => null,
58 'message' => 'On printable request, screen returns null'
59 ) );
60
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'
66 ) );
67
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'
73 ) );
74
75 $this->assertTransformCssMediaCase( array(
76 'printableQuery' => '1',
77 'media' => 'print',
78 'expectedReturn' => '',
79 'message' => 'On printable request, media print returns empty string'
80 ) );
81 }
82
83 /**
84 * Tests screen requests, without either query parameter set
85 * @covers OutputPage::transformCssMedia
86 */
87 public function testScreenRequests() {
88 $this->assertTransformCssMediaCase( array(
89 'media' => 'screen',
90 'expectedReturn' => 'screen',
91 'message' => 'On screen request, screen media type is preserved'
92 ) );
93
94 $this->assertTransformCssMediaCase( array(
95 'media' => 'handheld',
96 'expectedReturn' => 'handheld',
97 'message' => 'On screen request, handheld media type is preserved'
98 ) );
99
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.'
104 ) );
105
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.'
110 ) );
111
112 $this->assertTransformCssMediaCase( array(
113 'media' => 'print',
114 'expectedReturn' => 'print',
115 'message' => 'On screen request, print media type is preserved'
116 ) );
117 }
118
119 /**
120 * Tests handheld behavior
121 * @covers OutputPage::transformCssMedia
122 */
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'
129 ) );
130
131 $this->assertTransformCssMediaCase( array(
132 'handheldQuery' => '1',
133 'media' => 'screen',
134 'expectedReturn' => null,
135 'message' => 'On request with handheld querystring and media is screen, returns null'
136 ) );
137 }
138
139 public static function provideMakeResourceLoaderLink() {
140 return array(
141 // Load module script only
142 array(
143 array( 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ),
144 "<script>window.RLQ = window.RLQ || []; window.RLQ.push( function () {\n"
145 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.foo\u0026only=scripts\u0026skin=fallback");'
146 . "\n} );</script>"
147 ),
148 array(
149 // Don't condition wrap raw modules (like the startup module)
150 array( 'test.raw', ResourceLoaderModule::TYPE_SCRIPTS ),
151 '<script async src="http://127.0.0.1:8080/w/load.php?debug=false&amp;lang=en&amp;modules=test.raw&amp;only=scripts&amp;skin=fallback"></script>'
152 ),
153 // Load module styles only
154 // This also tests the order the modules are put into the url
155 array(
156 array( array( 'test.baz', 'test.foo', 'test.bar' ), ResourceLoaderModule::TYPE_STYLES ),
157
158 '<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">'
159 ),
160 // Load private module (only=scripts)
161 array(
162 array( 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ),
163 "<script>window.RLQ = window.RLQ || []; window.RLQ.push( function () {\n"
164 . "mw.test.baz({token:123});mw.loader.state({\"test.quux\":\"ready\"});\n"
165 . "} );</script>"
166 ),
167 // Load private module (combined)
168 array(
169 array( 'test.quux', ResourceLoaderModule::TYPE_COMBINED ),
170 "<script>window.RLQ = window.RLQ || []; window.RLQ.push( function () {\n"
171 . "mw.loader.implement(\"test.quux\",function($,jQuery){"
172 . "mw.test.baz({token:123});},{\"css\":[\".mw-icon{transition:none}"
173 . "\"]});\n} );</script>"
174 ),
175 // Load no modules
176 array(
177 array( array(), ResourceLoaderModule::TYPE_COMBINED ),
178 '',
179 ),
180 // noscript group
181 array(
182 array( 'test.noscript', ResourceLoaderModule::TYPE_STYLES ),
183 '<noscript><link rel=stylesheet href="http://127.0.0.1:8080/w/load.php?debug=false&amp;lang=en&amp;modules=test.noscript&amp;only=styles&amp;skin=fallback"></noscript>'
184 ),
185 // Load two modules in separate groups
186 array(
187 array( array( 'test.group.foo', 'test.group.bar' ), ResourceLoaderModule::TYPE_COMBINED ),
188 "<script>window.RLQ = window.RLQ || []; window.RLQ.push( function () {\n"
189 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.group.bar\u0026skin=fallback");'
190 . "\n} );</script>\n"
191 . "<script>window.RLQ = window.RLQ || []; window.RLQ.push( function () {\n"
192 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.group.foo\u0026skin=fallback");'
193 . "\n} );</script>"
194 ),
195 );
196 }
197
198 /**
199 * @dataProvider provideMakeResourceLoaderLink
200 * @covers OutputPage::makeResourceLoaderLink
201 * @covers ResourceLoader::makeLoaderImplementScript
202 * @covers ResourceLoader::makeModuleResponse
203 * @covers ResourceLoader::makeInlineScript
204 * @covers ResourceLoader::makeLoaderStateScript
205 * @covers ResourceLoader::createLoaderURL
206 */
207 public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
208 $this->setMwGlobals( array(
209 'wgResourceLoaderDebug' => false,
210 'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
211 // Affects whether CDATA is inserted
212 'wgWellFormedXml' => false,
213 ) );
214 $class = new ReflectionClass( 'OutputPage' );
215 $method = $class->getMethod( 'makeResourceLoaderLink' );
216 $method->setAccessible( true );
217 $ctx = new RequestContext();
218 $ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) );
219 $ctx->setLanguage( 'en' );
220 $out = new OutputPage( $ctx );
221 $rl = $out->getResourceLoader();
222 $rl->setMessageBlobStore( new NullMessageBlobStore() );
223 $rl->register( array(
224 'test.foo' => new ResourceLoaderTestModule( array(
225 'script' => 'mw.test.foo( { a: true } );',
226 'styles' => '.mw-test-foo { content: "style"; }',
227 ) ),
228 'test.bar' => new ResourceLoaderTestModule( array(
229 'script' => 'mw.test.bar( { a: true } );',
230 'styles' => '.mw-test-bar { content: "style"; }',
231 ) ),
232 'test.baz' => new ResourceLoaderTestModule( array(
233 'script' => 'mw.test.baz( { a: true } );',
234 'styles' => '.mw-test-baz { content: "style"; }',
235 ) ),
236 'test.quux' => new ResourceLoaderTestModule( array(
237 'script' => 'mw.test.baz( { token: 123 } );',
238 'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
239 'group' => 'private',
240 ) ),
241 'test.raw' => new ResourceLoaderTestModule( array(
242 'script' => 'mw.test.baz( { token: 123 } );',
243 'isRaw' => true,
244 ) ),
245 'test.noscript' => new ResourceLoaderTestModule( array(
246 'styles' => '.mw-test-noscript { content: "style"; }',
247 'group' => 'noscript',
248 ) ),
249 'test.group.bar' => new ResourceLoaderTestModule( array(
250 'styles' => '.mw-group-bar { content: "style"; }',
251 'group' => 'bar',
252 ) ),
253 'test.group.foo' => new ResourceLoaderTestModule( array(
254 'styles' => '.mw-group-foo { content: "style"; }',
255 'group' => 'foo',
256 ) ),
257 ) );
258 $links = $method->invokeArgs( $out, $args );
259 $actualHtml = implode( "\n", $links['html'] );
260 $this->assertEquals( $expectedHtml, $actualHtml );
261 }
262
263 /**
264 * @dataProvider provideVaryHeaders
265 * @covers OutputPage::addVaryHeader
266 * @covers OutputPage::getVaryHeader
267 * @covers OutputPage::getXVO
268 */
269 public function testVaryHeaders( $calls, $vary, $xvo ) {
270 // get rid of default Vary fields
271 $outputPage = $this->getMockBuilder( 'OutputPage' )
272 ->setConstructorArgs( array( new RequestContext() ) )
273 ->setMethods( array( 'getCacheVaryCookies' ) )
274 ->getMock();
275 $outputPage->expects( $this->any() )
276 ->method( 'getCacheVaryCookies' )
277 ->will( $this->returnValue( array() ) );
278 TestingAccessWrapper::newFromObject( $outputPage )->mVaryHeader = array();
279
280 foreach ( $calls as $call ) {
281 call_user_func_array( array( $outputPage, 'addVaryHeader' ), $call );
282 }
283 $this->assertEquals( $vary, $outputPage->getVaryHeader(), 'Vary:' );
284 $this->assertEquals( $xvo, $outputPage->getXVO(), 'X-Vary-Options:' );
285 }
286
287 public function provideVaryHeaders() {
288 // note: getXVO() automatically adds Vary: Cookie
289 return array(
290 array( // single header
291 array(
292 array( 'Cookie' ),
293 ),
294 'Vary: Cookie',
295 'X-Vary-Options: Cookie',
296 ),
297 array( // non-unique headers
298 array(
299 array( 'Cookie' ),
300 array( 'Accept-Language' ),
301 array( 'Cookie' ),
302 ),
303 'Vary: Cookie, Accept-Language',
304 'X-Vary-Options: Cookie,Accept-Language',
305 ),
306 array( // two headers with single options
307 array(
308 array( 'Cookie', array( 'string-contains=phpsessid' ) ),
309 array( 'Accept-Language', array( 'string-contains=en' ) ),
310 ),
311 'Vary: Cookie, Accept-Language',
312 'X-Vary-Options: Cookie;string-contains=phpsessid,Accept-Language;string-contains=en',
313 ),
314 array( // one header with multiple options
315 array(
316 array( 'Cookie', array( 'string-contains=phpsessid', 'string-contains=userId' ) ),
317 ),
318 'Vary: Cookie',
319 'X-Vary-Options: Cookie;string-contains=phpsessid;string-contains=userId',
320 ),
321 array( // Duplicate option
322 array(
323 array( 'Cookie', array( 'string-contains=phpsessid' ) ),
324 array( 'Cookie', array( 'string-contains=phpsessid' ) ),
325 array( 'Accept-Language', array( 'string-contains=en', 'string-contains=en' ) ),
326
327
328 ),
329 'Vary: Cookie, Accept-Language',
330 'X-Vary-Options: Cookie;string-contains=phpsessid,Accept-Language;string-contains=en',
331 ),
332 array( // Same header, different options
333 array(
334 array( 'Cookie', array( 'string-contains=phpsessid' ) ),
335 array( 'Cookie', array( 'string-contains=userId' ) ),
336 ),
337 'Vary: Cookie',
338 'X-Vary-Options: Cookie;string-contains=phpsessid;string-contains=userId',
339 ),
340 );
341 }
342 }
343
344 /**
345 * MessageBlobStore that doesn't do anything
346 */
347 class NullMessageBlobStore extends MessageBlobStore {
348 public function get( ResourceLoader $resourceLoader, $modules, $lang ) {
349 return array();
350 }
351
352 public function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
353 return false;
354 }
355
356 public function updateModule( $name, ResourceLoaderModule $module, $lang ) {
357 return;
358 }
359
360 public function updateMessage( $key ) {
361 }
362 public function clear() {
363 }
364 }