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() {
141 // Load module script only
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");'
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&lang=en&modules=test.raw&only=scripts&skin=fallback"></script>'
153 // Load module styles only
154 // This also tests the order the modules are put into the url
156 array( array( 'test.baz', 'test.foo', 'test.bar' ), ResourceLoaderModule
::TYPE_STYLES
),
158 '<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">'
160 // Load private module (only=scripts)
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"
167 // Load private module (combined)
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>"
177 array( array(), ResourceLoaderModule
::TYPE_COMBINED
),
182 array( 'test.noscript', ResourceLoaderModule
::TYPE_STYLES
),
183 '<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>'
185 // Load two modules in separate groups
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");'
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
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,
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"; }',
228 'test.bar' => new ResourceLoaderTestModule( array(
229 'script' => 'mw.test.bar( { a: true } );',
230 'styles' => '.mw-test-bar { content: "style"; }',
232 'test.baz' => new ResourceLoaderTestModule( array(
233 'script' => 'mw.test.baz( { a: true } );',
234 'styles' => '.mw-test-baz { content: "style"; }',
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',
241 'test.raw' => new ResourceLoaderTestModule( array(
242 'script' => 'mw.test.baz( { token: 123 } );',
245 'test.noscript' => new ResourceLoaderTestModule( array(
246 'styles' => '.mw-test-noscript { content: "style"; }',
247 'group' => 'noscript',
249 'test.group.bar' => new ResourceLoaderTestModule( array(
250 'styles' => '.mw-group-bar { content: "style"; }',
253 'test.group.foo' => new ResourceLoaderTestModule( array(
254 'styles' => '.mw-group-foo { content: "style"; }',
258 $links = $method->invokeArgs( $out, $args );
259 $actualHtml = implode( "\n", $links['html'] );
260 $this->assertEquals( $expectedHtml, $actualHtml );
265 * MessageBlobStore that doesn't do anything
267 class NullMessageBlobStore
extends MessageBlobStore
{
268 public function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
272 public function insertMessageBlob( $name, ResourceLoaderModule
$module, $lang ) {
276 public function updateModule( $name, ResourceLoaderModule
$module, $lang ) {
280 public function updateMessage( $key ) {
282 public function clear() {