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';
16 * @return ResourceLoaderContext
18 protected function getResourceLoaderContext( $lang = 'en', $dir = 'ltr' ) {
19 $resourceLoader = new ResourceLoader();
20 $request = new FauxRequest( [
22 'modules' => 'startup',
25 'target' => 'phpunit',
27 $ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
28 ->setConstructorArgs( [ $resourceLoader, $request ] )
29 ->setMethods( [ 'getDirection' ] )
31 $ctx->method( 'getDirection' )->willReturn( $dir );
35 public static function getSettings() {
37 // For ResourceLoader::inDebugMode since it doesn't have context
38 'ResourceLoaderDebug' => true,
40 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
41 'CacheEpoch' => '20140101000000',
43 // For ResourceLoader::__construct()
44 'ResourceLoaderSources' => [],
48 'ScriptExtension' => '.php',
49 'Script' => '/w/index.php',
50 'LoadScript' => '/w/load.php',
54 protected function setUp() {
57 ResourceLoader
::clearCache();
60 foreach ( self
::getSettings() as $key => $value ) {
61 $globals['wg' . $key] = $value;
63 $this->setMwGlobals( $globals );
69 class ResourceLoaderTestModule
extends ResourceLoaderModule
{
70 protected $messages = [];
71 protected $dependencies = [];
72 protected $group = null;
73 protected $source = 'local';
74 protected $position = 'bottom';
75 protected $script = '';
76 protected $styles = '';
77 protected $skipFunction = null;
78 protected $isRaw = false;
79 protected $isKnownEmpty = false;
80 protected $type = ResourceLoaderModule
::LOAD_GENERAL
;
81 protected $targets = [ 'phpunit' ];
83 public function __construct( $options = [] ) {
84 foreach ( $options as $key => $value ) {
89 public function getScript( ResourceLoaderContext
$context ) {
90 return $this->validateScriptFile( 'input', $this->script
);
93 public function getStyles( ResourceLoaderContext
$context ) {
94 return [ '' => $this->styles
];
97 public function getMessages() {
98 return $this->messages
;
101 public function getDependencies( ResourceLoaderContext
$context = null ) {
102 return $this->dependencies
;
105 public function getGroup() {
109 public function getSource() {
110 return $this->source
;
112 public function getPosition() {
113 return $this->position
;
116 public function getType() {
120 public function getSkipFunction() {
121 return $this->skipFunction
;
124 public function isRaw() {
127 public function isKnownEmpty( ResourceLoaderContext
$context ) {
128 return $this->isKnownEmpty
;
131 public function enableModuleContentVersion() {
136 class ResourceLoaderFileModuleTestModule
extends ResourceLoaderFileModule
{
139 class EmptyResourceLoader
extends ResourceLoader
{
140 // TODO: This won't be needed once ResourceLoader is empty by default
141 // and default registrations are done from ServiceWiring instead.
142 public function __construct( Config
$config = null, LoggerInterface
$logger = null ) {
143 $this->setLogger( $logger ?
: new NullLogger() );
144 $this->config
= $config ?
: MediaWikiServices
::getInstance()->getMainConfig();
145 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );