1<?php 2 3/** 4 * See also: 5 * - ResourceLoaderImageModuleTest::testContext 6 * 7 * @group ResourceLoader 8 * @covers ResourceLoaderContext 9 */ 10class ResourceLoaderContextTest extends PHPUnit\Framework\TestCase { 11 12 use MediaWikiCoversValidator; 13 14 protected static function getResourceLoader() { 15 return new EmptyResourceLoader( new HashConfig( [ 16 'ResourceLoaderDebug' => false, 17 'LoadScript' => '/w/load.php', 18 ] ) ); 19 } 20 21 public function testEmpty() { 22 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) ); 23 24 // Request parameters 25 $this->assertEquals( [], $ctx->getModules() ); 26 $this->assertEquals( 'qqx', $ctx->getLanguage() ); 27 $this->assertFalse( $ctx->getDebug() ); 28 $this->assertNull( $ctx->getOnly() ); 29 $this->assertEquals( 'fallback', $ctx->getSkin() ); 30 $this->assertNull( $ctx->getUser() ); 31 $this->assertNull( $ctx->getContentOverrideCallback() ); 32 33 // Misc 34 $this->assertEquals( 'ltr', $ctx->getDirection() ); 35 $this->assertEquals( 'qqx|fallback||||||||', $ctx->getHash() ); 36 $this->assertSame( [], $ctx->getReqBase() ); 37 $this->assertInstanceOf( User::class, $ctx->getUserObj() ); 38 } 39 40 public function testDummy() { 41 $this->assertInstanceOf( 42 ResourceLoaderContext::class, 43 ResourceLoaderContext::newDummyContext() 44 ); 45 } 46 47 public function testAccessors() { 48 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) ); 49 $this->assertInstanceOf( ResourceLoader::class, $ctx->getResourceLoader() ); 50 $this->assertInstanceOf( WebRequest::class, $ctx->getRequest() ); 51 $this->assertInstanceOf( Psr\Log\LoggerInterface::class, $ctx->getLogger() ); 52 } 53 54 public function testTypicalRequest() { 55 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [ 56 'debug' => 'false', 57 'lang' => 'zh', 58 'modules' => 'foo|foo.quux,baz,bar|baz.quux', 59 'only' => 'styles', 60 'skin' => 'fallback', 61 ] ) ); 62 63 // Request parameters 64 $this->assertEquals( 65 $ctx->getModules(), 66 [ 'foo', 'foo.quux', 'foo.baz', 'foo.bar', 'baz.quux' ] 67 ); 68 $this->assertFalse( $ctx->getDebug() ); 69 $this->assertEquals( 'zh', $ctx->getLanguage() ); 70 $this->assertEquals( 'styles', $ctx->getOnly() ); 71 $this->assertEquals( 'fallback', $ctx->getSkin() ); 72 $this->assertNull( $ctx->getUser() ); 73 74 // Misc 75 $this->assertEquals( 'ltr', $ctx->getDirection() ); 76 $this->assertEquals( 'zh|fallback|||styles|||||', $ctx->getHash() ); 77 $this->assertSame( [ 'lang' => 'zh' ], $ctx->getReqBase() ); 78 } 79 80 public static function provideDirection() { 81 yield 'LTR language' => [ 82 [ 'lang' => 'en' ], 83 'ltr', 84 ]; 85 yield 'RTL language' => [ 86 [ 'lang' => 'he' ], 87 'rtl', 88 ]; 89 yield 'explicit LTR' => [ 90 [ 'lang' => 'he', 'dir' => 'ltr' ], 91 'ltr', 92 ]; 93 yield 'explicit RTL' => [ 94 [ 'lang' => 'en', 'dir' => 'rtl' ], 95 'rtl', 96 ]; 97 // Not supported, but tested to cover the case and detect change 98 yield 'invalid dir' => [ 99 [ 'lang' => 'he', 'dir' => 'xyz' ], 100 'rtl', 101 ]; 102 } 103 104 /** 105 * @dataProvider provideDirection 106 */ 107 public function testDirection( array $params, $expected ) { 108 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( $params ) ); 109 $this->assertEquals( $expected, $ctx->getDirection() ); 110 } 111 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' ); 117 118 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [ 119 'only' => 'styles' 120 ] ) ); 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' ); 124 125 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [ 126 'only' => 'scripts' 127 ] ) ); 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' ); 131 } 132 133 public function testGetUser() { 134 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) ); 135 $this->assertSame( null, $ctx->getUser() ); 136 $this->assertTrue( $ctx->getUserObj()->isAnon() ); 137 138 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [ 139 'user' => 'Example' 140 ] ) ); 141 $this->assertSame( 'Example', $ctx->getUser() ); 142 $this->assertEquals( 'Example', $ctx->getUserObj()->getName() ); 143 } 144 145 public function testMsg() { 146 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [ 147 'lang' => 'en' 148 ] ) ); 149 $msg = $ctx->msg( 'mainpage' ); 150 $this->assertInstanceOf( Message::class, $msg ); 151 $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() ); 152 } 153} 154