3 use Psr\Log\LoggerInterface
;
4 use Psr\Log\NullLogger
;
6 abstract class ResourceLoaderTestCase
extends MediaWikiTestCase
{
7 // Version hash for a blank file module.
8 // Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
9 // and ResourceLoaderFileModule::getDefinitionSummary().
10 const BLANK_VERSION
= '09p30q0';
15 * @return ResourceLoaderContext
17 protected function getResourceLoaderContext( $lang = 'en', $dir = 'ltr' ) {
18 $resourceLoader = new ResourceLoader();
19 $request = new FauxRequest( [
21 'modules' => 'startup',
24 'target' => 'phpunit',
26 $ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
27 ->setConstructorArgs( [ $resourceLoader, $request ] )
28 ->setMethods( [ 'getDirection' ] )
30 $ctx->method( 'getDirection' )->willReturn( $dir );
34 public static function getSettings() {
36 // For ResourceLoader::inDebugMode since it doesn't have context
37 'ResourceLoaderDebug' => true,
39 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
40 'CacheEpoch' => '20140101000000',
42 // For ResourceLoader::__construct()
43 'ResourceLoaderSources' => [],
47 'ScriptExtension' => '.php',
48 'Script' => '/w/index.php',
49 'LoadScript' => '/w/load.php',
53 protected function setUp() {
56 ResourceLoader
::clearCache();
59 foreach ( self
::getSettings() as $key => $value ) {
60 $globals['wg' . $key] = $value;
62 $this->setMwGlobals( $globals );
68 class ResourceLoaderTestModule
extends ResourceLoaderModule
{
69 protected $messages = [];
70 protected $dependencies = [];
71 protected $group = null;
72 protected $source = 'local';
73 protected $position = 'bottom';
74 protected $script = '';
75 protected $styles = '';
76 protected $skipFunction = null;
77 protected $isRaw = false;
78 protected $isKnownEmpty = false;
79 protected $type = ResourceLoaderModule
::LOAD_GENERAL
;
80 protected $targets = [ 'phpunit' ];
82 public function __construct( $options = [] ) {
83 foreach ( $options as $key => $value ) {
88 public function getScript( ResourceLoaderContext
$context ) {
89 return $this->validateScriptFile( 'input', $this->script
);
92 public function getStyles( ResourceLoaderContext
$context ) {
93 return [ '' => $this->styles
];
96 public function getMessages() {
97 return $this->messages
;
100 public function getDependencies( ResourceLoaderContext
$context = null ) {
101 return $this->dependencies
;
104 public function getGroup() {
108 public function getSource() {
109 return $this->source
;
111 public function getPosition() {
112 return $this->position
;
115 public function getType() {
119 public function getSkipFunction() {
120 return $this->skipFunction
;
123 public function isRaw() {
126 public function isKnownEmpty( ResourceLoaderContext
$context ) {
127 return $this->isKnownEmpty
;
130 public function enableModuleContentVersion() {
135 class ResourceLoaderFileModuleTestModule
extends ResourceLoaderFileModule
{
138 class EmptyResourceLoader
extends ResourceLoader
{
139 // TODO: This won't be needed once ResourceLoader is empty by default
140 // and default registrations are done from ServiceWiring instead.
141 public function __construct( Config
$config = null, LoggerInterface
$logger = null ) {
142 $this->setLogger( $logger ?
: new NullLogger() );
143 $this->config
= $config ?
: ConfigFactory
::getDefaultInstance()->makeConfig( 'main' );
144 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );