2013-01-14 03:26:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Matthew Flaschen
|
|
|
|
|
*
|
|
|
|
|
* @group Output
|
|
|
|
|
*
|
2013-10-24 19:35:04 +00:00
|
|
|
* @todo factor tests in this class into providers and test methods
|
|
|
|
|
*
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
|
|
|
|
class OutputPageTest extends MediaWikiTestCase {
|
|
|
|
|
const SCREEN_MEDIA_QUERY = 'screen and (min-width: 982px)';
|
|
|
|
|
const SCREEN_ONLY_MEDIA_QUERY = 'only screen and (min-width: 982px)';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests a particular case of transformCssMedia, using the given input, globals,
|
|
|
|
|
* expected return, and message
|
|
|
|
|
*
|
|
|
|
|
* Asserts that $expectedReturn is returned.
|
|
|
|
|
*
|
|
|
|
|
* options['printableQuery'] - value of query string for printable, or omitted for none
|
|
|
|
|
* options['handheldQuery'] - value of query string for handheld, or omitted for none
|
|
|
|
|
* options['media'] - passed into the method under the same name
|
|
|
|
|
* options['expectedReturn'] - expected return value
|
|
|
|
|
* options['message'] - PHPUnit message for assertion
|
|
|
|
|
*
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param array $args Key-value array of arguments as shown above
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
2013-02-14 11:22:13 +00:00
|
|
|
protected function assertTransformCssMediaCase( $args ) {
|
2013-01-14 03:26:15 +00:00
|
|
|
$queryData = array();
|
|
|
|
|
if ( isset( $args['printableQuery'] ) ) {
|
|
|
|
|
$queryData['printable'] = $args['printableQuery'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $args['handheldQuery'] ) ) {
|
|
|
|
|
$queryData['handheld'] = $args['handheldQuery'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fauxRequest = new FauxRequest( $queryData, false );
|
2014-07-20 19:50:34 +00:00
|
|
|
$this->setMwGlobals( array(
|
2013-02-14 11:22:13 +00:00
|
|
|
'wgRequest' => $fauxRequest,
|
2013-01-14 03:26:15 +00:00
|
|
|
) );
|
|
|
|
|
|
|
|
|
|
$actualReturn = OutputPage::transformCssMedia( $args['media'] );
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
|
2013-01-14 03:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests print requests
|
2013-10-24 19:35:04 +00:00
|
|
|
* @covers OutputPage::transformCssMedia
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testPrintRequests() {
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'printableQuery' => '1',
|
|
|
|
|
'media' => 'screen',
|
|
|
|
|
'expectedReturn' => null,
|
|
|
|
|
'message' => 'On printable request, screen returns null'
|
|
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'printableQuery' => '1',
|
|
|
|
|
'media' => self::SCREEN_MEDIA_QUERY,
|
|
|
|
|
'expectedReturn' => null,
|
|
|
|
|
'message' => 'On printable request, screen media query returns null'
|
|
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'printableQuery' => '1',
|
|
|
|
|
'media' => self::SCREEN_ONLY_MEDIA_QUERY,
|
|
|
|
|
'expectedReturn' => null,
|
|
|
|
|
'message' => 'On printable request, screen media query with only returns null'
|
|
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'printableQuery' => '1',
|
|
|
|
|
'media' => 'print',
|
|
|
|
|
'expectedReturn' => '',
|
|
|
|
|
'message' => 'On printable request, media print returns empty string'
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests screen requests, without either query parameter set
|
2013-10-24 19:35:04 +00:00
|
|
|
* @covers OutputPage::transformCssMedia
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testScreenRequests() {
|
|
|
|
|
$this->assertTransformCssMediaCase( array(
|
|
|
|
|
'media' => 'screen',
|
|
|
|
|
'expectedReturn' => 'screen',
|
2013-05-11 19:05:43 +00:00
|
|
|
'message' => 'On screen request, screen media type is preserved'
|
|
|
|
|
) );
|
|
|
|
|
|
|
|
|
|
$this->assertTransformCssMediaCase( array(
|
|
|
|
|
'media' => 'handheld',
|
|
|
|
|
'expectedReturn' => 'handheld',
|
|
|
|
|
'message' => 'On screen request, handheld media type is preserved'
|
2013-01-14 03:26:15 +00:00
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'media' => self::SCREEN_MEDIA_QUERY,
|
|
|
|
|
'expectedReturn' => self::SCREEN_MEDIA_QUERY,
|
|
|
|
|
'message' => 'On screen request, screen media query is preserved.'
|
|
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'media' => self::SCREEN_ONLY_MEDIA_QUERY,
|
|
|
|
|
'expectedReturn' => self::SCREEN_ONLY_MEDIA_QUERY,
|
|
|
|
|
'message' => 'On screen request, screen media query with only is preserved.'
|
|
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'media' => 'print',
|
|
|
|
|
'expectedReturn' => 'print',
|
|
|
|
|
'message' => 'On screen request, print media type is preserved'
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-05-11 19:05:43 +00:00
|
|
|
* Tests handheld behavior
|
2013-10-24 19:35:04 +00:00
|
|
|
* @covers OutputPage::transformCssMedia
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testHandheld() {
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'handheldQuery' => '1',
|
|
|
|
|
'media' => 'handheld',
|
|
|
|
|
'expectedReturn' => '',
|
|
|
|
|
'message' => 'On request with handheld querystring and media is handheld, returns empty string'
|
|
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'handheldQuery' => '1',
|
|
|
|
|
'media' => 'screen',
|
|
|
|
|
'expectedReturn' => null,
|
|
|
|
|
'message' => 'On request with handheld querystring and media is screen, returns null'
|
|
|
|
|
) );
|
|
|
|
|
}
|
2014-06-28 20:40:22 +00:00
|
|
|
|
|
|
|
|
public static function provideMakeResourceLoaderLink() {
|
|
|
|
|
return array(
|
|
|
|
|
// Load module script only
|
|
|
|
|
array(
|
|
|
|
|
array( 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ),
|
2015-03-21 05:14:02 +00:00
|
|
|
'<script>if(window.mw){
|
|
|
|
|
document.write("\u003Cscript src=\"http://127.0.0.1:8080/w/load.php?debug=false\u0026amp;lang=en\u0026amp;modules=test.foo\u0026amp;only=scripts\u0026amp;skin=fallback\u0026amp;*\"\u003E\u003C/script\u003E");
|
|
|
|
|
}</script>
|
2014-09-08 16:31:54 +00:00
|
|
|
'
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
// Don't condition wrap raw modules (like the startup module)
|
|
|
|
|
array( 'test.raw', ResourceLoaderModule::TYPE_SCRIPTS ),
|
2015-03-21 05:14:02 +00:00
|
|
|
'<script src="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.raw&only=scripts&skin=fallback&*"></script>
|
2014-06-28 20:40:22 +00:00
|
|
|
'
|
|
|
|
|
),
|
|
|
|
|
// Load module styles only
|
|
|
|
|
// This also tests the order the modules are put into the url
|
|
|
|
|
array(
|
|
|
|
|
array( array( 'test.baz', 'test.foo', 'test.bar' ), ResourceLoaderModule::TYPE_STYLES ),
|
2015-03-21 05:14:02 +00:00
|
|
|
'<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&*">
|
2014-08-13 18:06:10 +00:00
|
|
|
'
|
|
|
|
|
),
|
OutputPage: Restore ResourceLoader condition wrap for embedded modules
Follows-up 9272bc6c47, 03c503da22.
* In 9272bc6c47, the condition wrap was removed from
OutputPage for no reason. This went unnnoticed as I had also
accidentally made the cond wrap in makeModuleResponse apply
to both only=scripts and regular (faux) responses, such as by
OutputPage embedding private modules.
* In 03c503da22, the latter bug was fixed, thus exposing the former.
This wrapper belongs in OutputPage, not in ResourceLoader. It's
OutputPage making the loader request. And just like in other places,
it's the "client"'s responsibility to ensure the request is either not
made or wrapped appropriately.
The test for "private module (only=scripts)" could be removed but
I'll keep it so we can see how this changes in the future. It's
a case that can't ever happen, but if it would, it currently gets
a double condition wrapper, which is fine.
Change-Id: Id333e4958ed769831fabca02164c1e8505962d57
2014-09-02 01:25:13 +00:00
|
|
|
// Load private module (only=scripts)
|
2014-08-13 18:06:10 +00:00
|
|
|
array(
|
|
|
|
|
array( 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ),
|
2015-03-21 05:14:02 +00:00
|
|
|
'<script>if(window.mw){
|
|
|
|
|
mw.test.baz({token:123});mw.loader.state({"test.quux":"ready"});
|
|
|
|
|
|
|
|
|
|
}</script>
|
2014-06-28 20:40:22 +00:00
|
|
|
'
|
|
|
|
|
),
|
|
|
|
|
// Load private module (combined)
|
|
|
|
|
array(
|
|
|
|
|
array( 'test.quux', ResourceLoaderModule::TYPE_COMBINED ),
|
2015-03-21 05:14:02 +00:00
|
|
|
'<script>if(window.mw){
|
2014-12-09 01:17:53 +00:00
|
|
|
mw.loader.implement("test.quux",function($,jQuery){mw.test.baz({token:123});},{"css":[".mw-icon{transition:none}\n"]});
|
2015-03-21 05:14:02 +00:00
|
|
|
|
|
|
|
|
}</script>
|
2014-06-28 20:40:22 +00:00
|
|
|
'
|
|
|
|
|
),
|
2014-12-16 00:41:45 +00:00
|
|
|
// Load module script with ESI
|
2014-06-28 20:40:22 +00:00
|
|
|
array(
|
|
|
|
|
array( 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS, true ),
|
2015-03-21 05:14:02 +00:00
|
|
|
'<script><esi:include src="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.foo&only=scripts&skin=fallback&*" /></script>
|
2014-06-28 20:40:22 +00:00
|
|
|
'
|
|
|
|
|
),
|
2014-12-16 00:41:45 +00:00
|
|
|
// Load module styles with ESI
|
2014-06-28 20:40:22 +00:00
|
|
|
array(
|
|
|
|
|
array( 'test.foo', ResourceLoaderModule::TYPE_STYLES, true ),
|
2015-03-21 05:14:02 +00:00
|
|
|
'<style><esi:include src="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.foo&only=styles&skin=fallback&*" /></style>
|
2014-08-24 03:26:48 +00:00
|
|
|
',
|
|
|
|
|
),
|
|
|
|
|
// Load no modules
|
|
|
|
|
array(
|
|
|
|
|
array( array(), ResourceLoaderModule::TYPE_COMBINED ),
|
|
|
|
|
'',
|
|
|
|
|
),
|
|
|
|
|
// noscript group
|
|
|
|
|
array(
|
|
|
|
|
array( 'test.noscript', ResourceLoaderModule::TYPE_STYLES ),
|
2015-03-21 05:14:02 +00:00
|
|
|
'<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>
|
2014-08-24 03:26:48 +00:00
|
|
|
'
|
|
|
|
|
),
|
|
|
|
|
// Load two modules in separate groups
|
|
|
|
|
array(
|
|
|
|
|
array( array( 'test.group.foo', 'test.group.bar' ), ResourceLoaderModule::TYPE_COMBINED ),
|
2015-03-21 05:14:02 +00:00
|
|
|
'<script>if(window.mw){
|
|
|
|
|
document.write("\u003Cscript src=\"http://127.0.0.1:8080/w/load.php?debug=false\u0026amp;lang=en\u0026amp;modules=test.group.bar\u0026amp;skin=fallback\u0026amp;*\"\u003E\u003C/script\u003E");
|
|
|
|
|
}</script>
|
|
|
|
|
<script>if(window.mw){
|
|
|
|
|
document.write("\u003Cscript src=\"http://127.0.0.1:8080/w/load.php?debug=false\u0026amp;lang=en\u0026amp;modules=test.group.foo\u0026amp;skin=fallback\u0026amp;*\"\u003E\u003C/script\u003E");
|
|
|
|
|
}</script>
|
2015-03-06 02:12:49 +00:00
|
|
|
'
|
2014-06-28 20:40:22 +00:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideMakeResourceLoaderLink
|
|
|
|
|
* @covers OutputPage::makeResourceLoaderLink
|
2015-03-23 03:57:45 +00:00
|
|
|
* @covers ResourceLoader::makeLoaderImplementScript
|
|
|
|
|
* @covers ResourceLoader::makeModuleResponse
|
|
|
|
|
* @covers ResourceLoader::makeInlineScript
|
|
|
|
|
* @covers ResourceLoader::makeLoaderStateScript
|
|
|
|
|
* @covers ResourceLoader::createLoaderURL
|
2014-06-28 20:40:22 +00:00
|
|
|
*/
|
2014-07-19 21:12:10 +00:00
|
|
|
public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
|
2014-06-28 20:40:22 +00:00
|
|
|
$this->setMwGlobals( array(
|
2014-07-11 14:35:14 +00:00
|
|
|
'wgResourceLoaderDebug' => false,
|
2014-06-28 20:40:22 +00:00
|
|
|
'wgResourceLoaderUseESI' => true,
|
|
|
|
|
'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
|
|
|
|
|
// Affects whether CDATA is inserted
|
|
|
|
|
'wgWellFormedXml' => false,
|
|
|
|
|
) );
|
|
|
|
|
$class = new ReflectionClass( 'OutputPage' );
|
|
|
|
|
$method = $class->getMethod( 'makeResourceLoaderLink' );
|
|
|
|
|
$method->setAccessible( true );
|
|
|
|
|
$ctx = new RequestContext();
|
2014-08-09 12:36:35 +00:00
|
|
|
$ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) );
|
2014-07-11 14:35:14 +00:00
|
|
|
$ctx->setLanguage( 'en' );
|
2014-06-28 20:40:22 +00:00
|
|
|
$out = new OutputPage( $ctx );
|
|
|
|
|
$rl = $out->getResourceLoader();
|
2015-03-29 04:24:31 +00:00
|
|
|
$rl->setMessageBlobStore( new NullMessageBlobStore() );
|
2014-06-28 20:40:22 +00:00
|
|
|
$rl->register( array(
|
2014-07-19 21:12:10 +00:00
|
|
|
'test.foo' => new ResourceLoaderTestModule( array(
|
2014-06-28 20:40:22 +00:00
|
|
|
'script' => 'mw.test.foo( { a: true } );',
|
|
|
|
|
'styles' => '.mw-test-foo { content: "style"; }',
|
2015-06-16 19:06:19 +00:00
|
|
|
) ),
|
2014-07-19 21:12:10 +00:00
|
|
|
'test.bar' => new ResourceLoaderTestModule( array(
|
2014-06-28 20:40:22 +00:00
|
|
|
'script' => 'mw.test.bar( { a: true } );',
|
|
|
|
|
'styles' => '.mw-test-bar { content: "style"; }',
|
2015-06-16 19:06:19 +00:00
|
|
|
) ),
|
2014-07-19 21:12:10 +00:00
|
|
|
'test.baz' => new ResourceLoaderTestModule( array(
|
2014-06-28 20:40:22 +00:00
|
|
|
'script' => 'mw.test.baz( { a: true } );',
|
|
|
|
|
'styles' => '.mw-test-baz { content: "style"; }',
|
2015-06-16 19:06:19 +00:00
|
|
|
) ),
|
2014-07-19 21:12:10 +00:00
|
|
|
'test.quux' => new ResourceLoaderTestModule( array(
|
2014-06-28 20:40:22 +00:00
|
|
|
'script' => 'mw.test.baz( { token: 123 } );',
|
|
|
|
|
'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
|
|
|
|
|
'group' => 'private',
|
2015-06-16 19:06:19 +00:00
|
|
|
) ),
|
2014-09-08 16:31:54 +00:00
|
|
|
'test.raw' => new ResourceLoaderTestModule( array(
|
|
|
|
|
'script' => 'mw.test.baz( { token: 123 } );',
|
|
|
|
|
'isRaw' => true,
|
2015-06-16 19:06:19 +00:00
|
|
|
) ),
|
2014-08-24 03:26:48 +00:00
|
|
|
'test.noscript' => new ResourceLoaderTestModule( array(
|
|
|
|
|
'styles' => '.mw-test-noscript { content: "style"; }',
|
|
|
|
|
'group' => 'noscript',
|
2015-06-16 19:06:19 +00:00
|
|
|
) ),
|
2014-08-24 03:26:48 +00:00
|
|
|
'test.group.bar' => new ResourceLoaderTestModule( array(
|
2014-09-08 16:31:54 +00:00
|
|
|
'styles' => '.mw-group-bar { content: "style"; }',
|
|
|
|
|
'group' => 'bar',
|
2015-06-16 19:06:19 +00:00
|
|
|
) ),
|
2014-08-24 03:26:48 +00:00
|
|
|
'test.group.foo' => new ResourceLoaderTestModule( array(
|
2014-09-08 16:31:54 +00:00
|
|
|
'styles' => '.mw-group-foo { content: "style"; }',
|
|
|
|
|
'group' => 'foo',
|
2015-06-16 19:06:19 +00:00
|
|
|
) ),
|
2014-06-28 20:40:22 +00:00
|
|
|
) );
|
|
|
|
|
$links = $method->invokeArgs( $out, $args );
|
2014-08-06 15:54:22 +00:00
|
|
|
// Strip comments to avoid variation due to wgDBname in WikiID and cache key
|
|
|
|
|
$actualHtml = preg_replace( '#/\*[^*]+\*/#', '', $links['html'] );
|
|
|
|
|
$this->assertEquals( $expectedHtml, $actualHtml );
|
2014-06-28 20:40:22 +00:00
|
|
|
}
|
2013-01-14 03:26:15 +00:00
|
|
|
}
|
2015-03-29 04:24:31 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MessageBlobStore that doesn't do anything
|
|
|
|
|
*/
|
|
|
|
|
class NullMessageBlobStore extends MessageBlobStore {
|
2015-06-16 19:06:19 +00:00
|
|
|
public function get( ResourceLoader $resourceLoader, $modules, $lang ) {
|
2015-03-29 04:24:31 +00:00
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 19:06:19 +00:00
|
|
|
public function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
|
2015-03-29 04:24:31 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 19:06:19 +00:00
|
|
|
public function updateModule( $name, ResourceLoaderModule $module, $lang ) {
|
2015-03-29 04:24:31 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 19:06:19 +00:00
|
|
|
public function updateMessage( $key ) {
|
2015-03-29 04:24:31 +00:00
|
|
|
}
|
|
|
|
|
public function clear() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|