Merge "SpecialUserrights: Use session data instead of URL parameter for success"
[lhc/web/wiklou.git] / tests / phpunit / ResourceLoaderTestCase.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Psr\Log\LoggerInterface;
5 use Psr\Log\NullLogger;
6
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';
12
13 /**
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
18 */
19 protected function getResourceLoaderContext( $options = [] ) {
20 if ( is_string( $options ) ) {
21 // Back-compat for extension tests
22 $options = [ 'lang' => $options ];
23 }
24 $options += [
25 'lang' => 'en',
26 'dir' => 'ltr',
27 ];
28 $resourceLoader = new ResourceLoader();
29 $request = new FauxRequest( [
30 'lang' => $options['lang'],
31 'modules' => 'startup',
32 'only' => 'scripts',
33 'skin' => 'vector',
34 'target' => 'phpunit',
35 ] );
36 $ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
37 ->setConstructorArgs( [ $resourceLoader, $request ] )
38 ->setMethods( [ 'getDirection' ] )
39 ->getMock();
40 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
41 return $ctx;
42 }
43
44 public static function getSettings() {
45 return [
46 // For ResourceLoader::inDebugMode since it doesn't have context
47 'ResourceLoaderDebug' => true,
48
49 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
50 'CacheEpoch' => '20140101000000',
51
52 // For ResourceLoader::__construct()
53 'ResourceLoaderSources' => [],
54
55 // For wfScript()
56 'ScriptPath' => '/w',
57 'ScriptExtension' => '.php',
58 'Script' => '/w/index.php',
59 'LoadScript' => '/w/load.php',
60 ];
61 }
62
63 protected function setUp() {
64 parent::setUp();
65
66 ResourceLoader::clearCache();
67
68 $globals = [];
69 foreach ( self::getSettings() as $key => $value ) {
70 $globals['wg' . $key] = $value;
71 }
72 $this->setMwGlobals( $globals );
73 }
74 }
75
76 /* Stubs */
77
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' ];
91
92 public function __construct( $options = [] ) {
93 foreach ( $options as $key => $value ) {
94 $this->$key = $value;
95 }
96 }
97
98 public function getScript( ResourceLoaderContext $context ) {
99 return $this->validateScriptFile( 'input', $this->script );
100 }
101
102 public function getStyles( ResourceLoaderContext $context ) {
103 return [ '' => $this->styles ];
104 }
105
106 public function getMessages() {
107 return $this->messages;
108 }
109
110 public function getDependencies( ResourceLoaderContext $context = null ) {
111 return $this->dependencies;
112 }
113
114 public function getGroup() {
115 return $this->group;
116 }
117
118 public function getSource() {
119 return $this->source;
120 }
121 public function getPosition() {
122 return $this->position;
123 }
124
125 public function getType() {
126 return $this->type;
127 }
128
129 public function getSkipFunction() {
130 return $this->skipFunction;
131 }
132
133 public function isRaw() {
134 return $this->isRaw;
135 }
136 public function isKnownEmpty( ResourceLoaderContext $context ) {
137 return $this->isKnownEmpty;
138 }
139
140 public function enableModuleContentVersion() {
141 return true;
142 }
143 }
144
145 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
146 }
147
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() ) );
155 }
156 }