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->assertInstanceOf( User
::class, $ctx->getUserObj() );
41 public function testDummy() {
42 $this->assertInstanceOf(
43 ResourceLoaderContext
::class,
44 ResourceLoaderContext
::newDummyContext()
48 public function testAccessors() {
49 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
50 $this->assertInstanceOf( ResourceLoader
::class, $ctx->getResourceLoader() );
51 $this->assertInstanceOf( WebRequest
::class, $ctx->getRequest() );
52 $this->assertInstanceOf( Psr\Log\LoggerInterface
::class, $ctx->getLogger() );
55 public function testTypicalRequest() {
56 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
59 'modules' => 'foo|foo.quux,baz,bar|baz.quux',
67 [ 'foo', 'foo.quux', 'foo.baz', 'foo.bar', 'baz.quux' ]
69 $this->assertEquals( false, $ctx->getDebug() );
70 $this->assertEquals( 'zh', $ctx->getLanguage() );
71 $this->assertEquals( 'styles', $ctx->getOnly() );
72 $this->assertEquals( 'fallback', $ctx->getSkin() );
73 $this->assertEquals( null, $ctx->getUser() );
76 $this->assertEquals( 'ltr', $ctx->getDirection() );
77 $this->assertEquals( 'zh|fallback|||styles|||||', $ctx->getHash() );
80 public static function provideDirection() {
81 yield
'LTR language' => [
85 yield
'RTL language' => [
89 yield
'explicit LTR' => [
90 [ 'lang' => 'he', 'dir' => 'ltr' ],
93 yield
'explicit RTL' => [
94 [ 'lang' => 'en', 'dir' => 'rtl' ],
97 // Not supported, but tested to cover the case and detect change
98 yield
'invalid dir' => [
99 [ 'lang' => 'he', 'dir' => 'xyz' ],
105 * @dataProvider provideDirection
107 public function testDirection( array $params, $expected ) {
108 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( $params ) );
109 $this->assertEquals( $expected, $ctx->getDirection() );
112 public function testShouldInclude() {
113 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
114 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
115 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
116 $this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
118 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
121 $this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
122 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
123 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
125 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
128 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
129 $this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
130 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
133 public function testGetUser() {
134 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
135 $this->assertSame( null, $ctx->getUser() );
136 $this->assertTrue( $ctx->getUserObj()->isAnon() );
138 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
141 $this->assertSame( 'Example', $ctx->getUser() );
142 $this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
145 public function testMsg() {
146 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
149 $msg = $ctx->msg( 'mainpage' );
150 $this->assertInstanceOf( Message
::class, $msg );
151 $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );