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( Config
::class, $ctx->getConfig() );
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() );
81 public static function provideDirection() {
82 yield
'LTR language' => [
86 yield
'RTL language' => [
90 yield
'explicit LTR' => [
91 [ 'lang' => 'he', 'dir' => 'ltr' ],
94 yield
'explicit RTL' => [
95 [ 'lang' => 'en', 'dir' => 'rtl' ],
98 // Not supported, but tested to cover the case and detect change
99 yield
'invalid dir' => [
100 [ 'lang' => 'he', 'dir' => 'xyz' ],
106 * @dataProvider provideDirection
108 public function testDirection( array $params, $expected ) {
109 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( $params ) );
110 $this->assertEquals( $expected, $ctx->getDirection() );
113 public function testShouldInclude() {
114 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
115 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
116 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
117 $this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
119 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
122 $this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
123 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
124 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
126 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
129 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
130 $this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
131 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
134 public function testGetUser() {
135 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
136 $this->assertSame( null, $ctx->getUser() );
137 $this->assertTrue( $ctx->getUserObj()->isAnon() );
139 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
142 $this->assertSame( 'Example', $ctx->getUser() );
143 $this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
146 public function testMsg() {
147 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
150 $msg = $ctx->msg( 'mainpage' );
151 $this->assertInstanceOf( Message
::class, $msg );
152 $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );