3 use MediaWiki\MediaWikiServices
;
4 use Psr\Log\LoggerInterface
;
5 use Psr\Log\NullLogger
;
7 abstract class ResourceLoaderTestCase
extends MediaWikiTestCase
{
8 // Version hash for a blank file module.
9 // Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
10 // and ResourceLoaderFileModule::getDefinitionSummary().
11 const BLANK_VERSION
= '09p30q0';
14 * @param array|string $options Language code or options array
15 * - string 'lang' Language code
16 * - string 'dir' Language direction (ltr or rtl)
17 * @return ResourceLoaderContext
19 protected function getResourceLoaderContext( $options = [] ) {
20 if ( is_string( $options ) ) {
21 // Back-compat for extension tests
22 $options = [ 'lang' => $options ];
28 $resourceLoader = new ResourceLoader();
29 $request = new FauxRequest( [
30 'lang' => $options['lang'],
31 'modules' => 'startup',
34 'target' => 'phpunit',
36 $ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
37 ->setConstructorArgs( [ $resourceLoader, $request ] )
38 ->setMethods( [ 'getDirection' ] )
40 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
44 public static function getSettings() {
46 // For ResourceLoader::inDebugMode since it doesn't have context
47 'ResourceLoaderDebug' => true,
49 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
50 'CacheEpoch' => '20140101000000',
52 // For ResourceLoader::__construct()
53 'ResourceLoaderSources' => [],
57 'ScriptExtension' => '.php',
58 'Script' => '/w/index.php',
59 'LoadScript' => '/w/load.php',
63 protected function setUp() {
66 ResourceLoader
::clearCache();
69 foreach ( self
::getSettings() as $key => $value ) {
70 $globals['wg' . $key] = $value;
72 $this->setMwGlobals( $globals );
78 class ResourceLoaderTestModule
extends ResourceLoaderModule
{
79 protected $messages = [];
80 protected $dependencies = [];
81 protected $group = null;
82 protected $source = 'local';
83 protected $position = 'bottom';
84 protected $script = '';
85 protected $styles = '';
86 protected $skipFunction = null;
87 protected $isRaw = false;
88 protected $isKnownEmpty = false;
89 protected $type = ResourceLoaderModule
::LOAD_GENERAL
;
90 protected $targets = [ 'phpunit' ];
92 public function __construct( $options = [] ) {
93 foreach ( $options as $key => $value ) {
98 public function getScript( ResourceLoaderContext
$context ) {
99 return $this->validateScriptFile( 'input', $this->script
);
102 public function getStyles( ResourceLoaderContext
$context ) {
103 return [ '' => $this->styles
];
106 public function getMessages() {
107 return $this->messages
;
110 public function getDependencies( ResourceLoaderContext
$context = null ) {
111 return $this->dependencies
;
114 public function getGroup() {
118 public function getSource() {
119 return $this->source
;
121 public function getPosition() {
122 return $this->position
;
125 public function getType() {
129 public function getSkipFunction() {
130 return $this->skipFunction
;
133 public function isRaw() {
136 public function isKnownEmpty( ResourceLoaderContext
$context ) {
137 return $this->isKnownEmpty
;
140 public function enableModuleContentVersion() {
145 class ResourceLoaderFileModuleTestModule
extends ResourceLoaderFileModule
{
148 class EmptyResourceLoader
extends ResourceLoader
{
149 // TODO: This won't be needed once ResourceLoader is empty by default
150 // and default registrations are done from ServiceWiring instead.
151 public function __construct( Config
$config = null, LoggerInterface
$logger = null ) {
152 $this->setLogger( $logger ?
: new NullLogger() );
153 $this->config
= $config ?
: MediaWikiServices
::getInstance()->getMainConfig();
154 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );