Merge "DatabaseUpdater: Stop inserting junk into 'updatelog' every time the updater...
[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 string $lang
15 * @param string $dir
16 * @return ResourceLoaderContext
17 */
18 protected function getResourceLoaderContext( $lang = 'en', $dir = 'ltr' ) {
19 $resourceLoader = new ResourceLoader();
20 $request = new FauxRequest( [
21 'lang' => $lang,
22 'modules' => 'startup',
23 'only' => 'scripts',
24 'skin' => 'vector',
25 'target' => 'phpunit',
26 ] );
27 $ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
28 ->setConstructorArgs( [ $resourceLoader, $request ] )
29 ->setMethods( [ 'getDirection' ] )
30 ->getMock();
31 $ctx->method( 'getDirection' )->willReturn( $dir );
32 return $ctx;
33 }
34
35 public static function getSettings() {
36 return [
37 // For ResourceLoader::inDebugMode since it doesn't have context
38 'ResourceLoaderDebug' => true,
39
40 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
41 'CacheEpoch' => '20140101000000',
42
43 // For ResourceLoader::__construct()
44 'ResourceLoaderSources' => [],
45
46 // For wfScript()
47 'ScriptPath' => '/w',
48 'ScriptExtension' => '.php',
49 'Script' => '/w/index.php',
50 'LoadScript' => '/w/load.php',
51 ];
52 }
53
54 protected function setUp() {
55 parent::setUp();
56
57 ResourceLoader::clearCache();
58
59 $globals = [];
60 foreach ( self::getSettings() as $key => $value ) {
61 $globals['wg' . $key] = $value;
62 }
63 $this->setMwGlobals( $globals );
64 }
65 }
66
67 /* Stubs */
68
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' ];
82
83 public function __construct( $options = [] ) {
84 foreach ( $options as $key => $value ) {
85 $this->$key = $value;
86 }
87 }
88
89 public function getScript( ResourceLoaderContext $context ) {
90 return $this->validateScriptFile( 'input', $this->script );
91 }
92
93 public function getStyles( ResourceLoaderContext $context ) {
94 return [ '' => $this->styles ];
95 }
96
97 public function getMessages() {
98 return $this->messages;
99 }
100
101 public function getDependencies( ResourceLoaderContext $context = null ) {
102 return $this->dependencies;
103 }
104
105 public function getGroup() {
106 return $this->group;
107 }
108
109 public function getSource() {
110 return $this->source;
111 }
112 public function getPosition() {
113 return $this->position;
114 }
115
116 public function getType() {
117 return $this->type;
118 }
119
120 public function getSkipFunction() {
121 return $this->skipFunction;
122 }
123
124 public function isRaw() {
125 return $this->isRaw;
126 }
127 public function isKnownEmpty( ResourceLoaderContext $context ) {
128 return $this->isKnownEmpty;
129 }
130
131 public function enableModuleContentVersion() {
132 return true;
133 }
134 }
135
136 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
137 }
138
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() ) );
146 }
147 }