Merge "Revert "Move wfEscapeWikiText() to Parser::escapeWikitext()""
[lhc/web/wiklou.git] / tests / phpunit / ResourceLoaderTestCase.php
1 <?php
2
3 use Psr\Log\LoggerInterface;
4 use Psr\Log\NullLogger;
5
6 abstract class ResourceLoaderTestCase extends MediaWikiTestCase {
7 /**
8 * @param string $lang
9 * @param string $dir
10 * @return ResourceLoaderContext
11 */
12 protected function getResourceLoaderContext( $lang = 'en', $dir = 'ltr' ) {
13 $resourceLoader = new ResourceLoader();
14 $request = new FauxRequest( [
15 'lang' => $lang,
16 'modules' => 'startup',
17 'only' => 'scripts',
18 'skin' => 'vector',
19 'target' => 'phpunit',
20 ] );
21 $ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
22 ->setConstructorArgs( [ $resourceLoader, $request ] )
23 ->setMethods( [ 'getDirection' ] )
24 ->getMock();
25 $ctx->method( 'getDirection' )->willReturn( $dir );
26 return $ctx;
27 }
28
29 public static function getSettings() {
30 return [
31 // For ResourceLoader::inDebugMode since it doesn't have context
32 'ResourceLoaderDebug' => true,
33
34 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
35 'CacheEpoch' => '20140101000000',
36
37 // For ResourceLoader::__construct()
38 'ResourceLoaderSources' => [],
39
40 // For wfScript()
41 'ScriptPath' => '/w',
42 'ScriptExtension' => '.php',
43 'Script' => '/w/index.php',
44 'LoadScript' => '/w/load.php',
45 ];
46 }
47
48 protected function setUp() {
49 parent::setUp();
50
51 ResourceLoader::clearCache();
52
53 $globals = [];
54 foreach ( self::getSettings() as $key => $value ) {
55 $globals['wg' . $key] = $value;
56 }
57 $this->setMwGlobals( $globals );
58 }
59 }
60
61 /* Stubs */
62
63 class ResourceLoaderTestModule extends ResourceLoaderModule {
64 protected $messages = [];
65 protected $dependencies = [];
66 protected $group = null;
67 protected $source = 'local';
68 protected $position = 'bottom';
69 protected $script = '';
70 protected $styles = '';
71 protected $skipFunction = null;
72 protected $isRaw = false;
73 protected $isKnownEmpty = false;
74 protected $type = ResourceLoaderModule::LOAD_GENERAL;
75 protected $targets = [ 'phpunit' ];
76
77 public function __construct( $options = [] ) {
78 foreach ( $options as $key => $value ) {
79 $this->$key = $value;
80 }
81 }
82
83 public function getScript( ResourceLoaderContext $context ) {
84 return $this->validateScriptFile( 'input', $this->script );
85 }
86
87 public function getStyles( ResourceLoaderContext $context ) {
88 return [ '' => $this->styles ];
89 }
90
91 public function getMessages() {
92 return $this->messages;
93 }
94
95 public function getDependencies( ResourceLoaderContext $context = null ) {
96 return $this->dependencies;
97 }
98
99 public function getGroup() {
100 return $this->group;
101 }
102
103 public function getSource() {
104 return $this->source;
105 }
106 public function getPosition() {
107 return $this->position;
108 }
109
110 public function getType() {
111 return $this->type;
112 }
113
114 public function getSkipFunction() {
115 return $this->skipFunction;
116 }
117
118 public function isRaw() {
119 return $this->isRaw;
120 }
121 public function isKnownEmpty( ResourceLoaderContext $context ) {
122 return $this->isKnownEmpty;
123 }
124
125 public function enableModuleContentVersion() {
126 return true;
127 }
128 }
129
130 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
131 }
132
133 class EmptyResourceLoader extends ResourceLoader {
134 // TODO: This won't be needed once ResourceLoader is empty by default
135 // and default registrations are done from ServiceWiring instead.
136 public function __construct( Config $config = null, LoggerInterface $logger = null ) {
137 $this->setLogger( $logger ?: new NullLogger() );
138 $this->config = $config ?: ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
139 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
140 }
141 }