5 * - ResourceLoaderImageModuleTest::testContext
7 * @group ResourceLoader
8 * @covers ResourceLoaderContext
10 class ResourceLoaderContextTest
extends PHPUnit\Framework\TestCase
{
12 use MediaWikiCoversValidator
;
14 protected static function getResourceLoader() {
15 return new EmptyResourceLoader( new HashConfig( [
16 'ResourceLoaderDebug' => false,
17 'LoadScript' => '/w/load.php',
18 // For ResourceLoader::register()
19 'ResourceModuleSkinStyles' => [],
23 public function testEmpty() {
24 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
27 $this->assertEquals( [], $ctx->getModules() );
28 $this->assertEquals( 'qqx', $ctx->getLanguage() );
29 $this->assertEquals( false, $ctx->getDebug() );
30 $this->assertEquals( null, $ctx->getOnly() );
31 $this->assertEquals( 'fallback', $ctx->getSkin() );
32 $this->assertEquals( null, $ctx->getUser() );
33 $this->assertNull( $ctx->getContentOverrideCallback() );
36 $this->assertEquals( 'ltr', $ctx->getDirection() );
37 $this->assertEquals( 'qqx|fallback||||||||', $ctx->getHash() );
38 $this->assertSame( [], $ctx->getReqBase() );
39 $this->assertInstanceOf( User
::class, $ctx->getUserObj() );
42 public function testDummy() {
43 $this->assertInstanceOf(
44 ResourceLoaderContext
::class,
45 ResourceLoaderContext
::newDummyContext()
49 public function testAccessors() {
50 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
51 $this->assertInstanceOf( ResourceLoader
::class, $ctx->getResourceLoader() );
52 $this->assertInstanceOf( WebRequest
::class, $ctx->getRequest() );
53 $this->assertInstanceOf( Psr\Log\LoggerInterface
::class, $ctx->getLogger() );
56 public function testTypicalRequest() {
57 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
60 'modules' => 'foo|foo.quux,baz,bar|baz.quux',
68 [ 'foo', 'foo.quux', 'foo.baz', 'foo.bar', 'baz.quux' ]
70 $this->assertEquals( false, $ctx->getDebug() );
71 $this->assertEquals( 'zh', $ctx->getLanguage() );
72 $this->assertEquals( 'styles', $ctx->getOnly() );
73 $this->assertEquals( 'fallback', $ctx->getSkin() );
74 $this->assertEquals( null, $ctx->getUser() );
77 $this->assertEquals( 'ltr', $ctx->getDirection() );
78 $this->assertEquals( 'zh|fallback|||styles|||||', $ctx->getHash() );
79 $this->assertSame( [ 'lang' => 'zh' ], $ctx->getReqBase() );
82 public static function provideDirection() {
83 yield
'LTR language' => [
87 yield
'RTL language' => [
91 yield
'explicit LTR' => [
92 [ 'lang' => 'he', 'dir' => 'ltr' ],
95 yield
'explicit RTL' => [
96 [ 'lang' => 'en', 'dir' => 'rtl' ],
99 // Not supported, but tested to cover the case and detect change
100 yield
'invalid dir' => [
101 [ 'lang' => 'he', 'dir' => 'xyz' ],
107 * @dataProvider provideDirection
109 public function testDirection( array $params, $expected ) {
110 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( $params ) );
111 $this->assertEquals( $expected, $ctx->getDirection() );
114 public function testShouldInclude() {
115 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
116 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
117 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
118 $this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
120 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
123 $this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
124 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
125 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
127 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
130 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
131 $this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
132 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
135 public function testGetUser() {
136 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
137 $this->assertSame( null, $ctx->getUser() );
138 $this->assertTrue( $ctx->getUserObj()->isAnon() );
140 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
143 $this->assertSame( 'Example', $ctx->getUser() );
144 $this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
147 public function testMsg() {
148 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
151 $msg = $ctx->msg( 'mainpage' );
152 $this->assertInstanceOf( Message
::class, $msg );
153 $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );