MediaWiki  1.27.2
ResourceLoaderFileModuleTest.php
Go to the documentation of this file.
1 <?php
2 
8 
9  protected function setUp() {
10  parent::setUp();
11 
12  // The return value of the closure shouldn't matter since this test should
13  // never call it
15  'fakeskin',
16  'FakeSkin',
17  function () {
18  }
19  );
20  }
21 
22  private static function getModules() {
23  $base = [
24  'localBasePath' => realpath( __DIR__ ),
25  ];
26 
27  return [
28  'noTemplateModule' => [],
29 
30  'htmlTemplateModule' => $base + [
31  'templates' => [
32  'templates/template.html',
33  'templates/template2.html',
34  ]
35  ],
36 
37  'aliasedHtmlTemplateModule' => $base + [
38  'templates' => [
39  'foo.html' => 'templates/template.html',
40  'bar.html' => 'templates/template2.html',
41  ]
42  ],
43 
44  'templateModuleHandlebars' => $base + [
45  'templates' => [
46  'templates/template_awesome.handlebars',
47  ],
48  ],
49 
50  'aliasFooFromBar' => $base + [
51  'templates' => [
52  'foo.foo' => 'templates/template.bar',
53  ],
54  ],
55  ];
56  }
57 
58  public static function providerTemplateDependencies() {
59  $modules = self::getModules();
60 
61  return [
62  [
63  $modules['noTemplateModule'],
64  [],
65  ],
66  [
67  $modules['htmlTemplateModule'],
68  [
69  'mediawiki.template',
70  ],
71  ],
72  [
73  $modules['templateModuleHandlebars'],
74  [
75  'mediawiki.template',
76  'mediawiki.template.handlebars',
77  ],
78  ],
79  [
80  $modules['aliasFooFromBar'],
81  [
82  'mediawiki.template',
83  'mediawiki.template.foo',
84  ],
85  ],
86  ];
87  }
88 
94  public function testTemplateDependencies( $module, $expected ) {
95  $rl = new ResourceLoaderFileModule( $module );
96  $this->assertEquals( $rl->getDependencies(), $expected );
97  }
98 
104  public function testGetAllSkinStyleFiles() {
105  $baseParams = [
106  'scripts' => [
107  'foo.js',
108  'bar.js',
109  ],
110  'styles' => [
111  'foo.css',
112  'bar.css' => [ 'media' => 'print' ],
113  'screen.less' => [ 'media' => 'screen' ],
114  'screen-query.css' => [ 'media' => 'screen and (min-width: 400px)' ],
115  ],
116  'skinStyles' => [
117  'default' => 'quux-fallback.less',
118  'fakeskin' => [
119  'baz-vector.css',
120  'quux-vector.less',
121  ],
122  ],
123  'messages' => [
124  'hello',
125  'world',
126  ],
127  ];
128 
129  $module = new ResourceLoaderFileModule( $baseParams );
130 
131  $this->assertEquals(
132  [
133  'foo.css',
134  'baz-vector.css',
135  'quux-vector.less',
136  'quux-fallback.less',
137  'bar.css',
138  'screen.less',
139  'screen-query.css',
140  ],
141  array_map( 'basename', $module->getAllStyleFiles() )
142  );
143  }
144 
150  private static function stripNoflip( $css ) {
151  return str_replace( '/*@noflip*/ ', '', $css );
152  }
153 
161  public function testMixedCssAnnotations() {
162  $basePath = __DIR__ . '/../../data/css';
163  $testModule = new ResourceLoaderFileModule( [
164  'localBasePath' => $basePath,
165  'styles' => [ 'test.css' ],
166  ] );
167  $expectedModule = new ResourceLoaderFileModule( [
168  'localBasePath' => $basePath,
169  'styles' => [ 'expected.css' ],
170  ] );
171 
172  $contextLtr = $this->getResourceLoaderContext( 'en', 'ltr' );
173  $contextRtl = $this->getResourceLoaderContext( 'he', 'rtl' );
174 
175  // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
176  // the @noflip annotations are always preserved, we need to strip them first.
177  $this->assertEquals(
178  $expectedModule->getStyles( $contextLtr ),
179  self::stripNoflip( $testModule->getStyles( $contextLtr ) ),
180  "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
181  );
182  $this->assertEquals(
183  $expectedModule->getStyles( $contextLtr ),
184  self::stripNoflip( $testModule->getStyles( $contextRtl ) ),
185  "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
186  );
187  }
188 
189  public static function providerGetTemplates() {
190  $modules = self::getModules();
191 
192  return [
193  [
194  $modules['noTemplateModule'],
195  [],
196  ],
197  [
198  $modules['templateModuleHandlebars'],
199  [
200  'templates/template_awesome.handlebars' => "wow\n",
201  ],
202  ],
203  [
204  $modules['htmlTemplateModule'],
205  [
206  'templates/template.html' => "<strong>hello</strong>\n",
207  'templates/template2.html' => "<div>goodbye</div>\n",
208  ],
209  ],
210  [
211  $modules['aliasedHtmlTemplateModule'],
212  [
213  'foo.html' => "<strong>hello</strong>\n",
214  'bar.html' => "<div>goodbye</div>\n",
215  ],
216  ],
217  ];
218  }
219 
224  public function testGetTemplates( $module, $expected ) {
225  $rl = new ResourceLoaderFileModule( $module );
226 
227  $this->assertEquals( $rl->getTemplates(), $expected );
228  }
229 
230  public function testBomConcatenation() {
231  $basePath = __DIR__ . '/../../data/css';
232  $testModule = new ResourceLoaderFileModule( [
233  'localBasePath' => $basePath,
234  'styles' => [ 'bom.css' ],
235  ] );
236  $this->assertEquals(
237  substr( file_get_contents( "$basePath/bom.css" ), 0, 10 ),
238  "\xef\xbb\xbf.efbbbf",
239  'File has leading BOM'
240  );
241 
242  $contextLtr = $this->getResourceLoaderContext( 'en', 'ltr' );
243  $this->assertEquals(
244  $testModule->getStyles( $contextLtr ),
245  [ 'all' => ".efbbbf_bom_char_at_start_of_file {}\n" ],
246  'Leading BOM removed when concatenating files'
247  );
248  }
249 }
testTemplateDependencies($module, $expected)
providerTemplateDependencies ResourceLoaderFileModule::__construct ResourceLoaderFileModule::getDepen...
testMixedCssAnnotations()
What happens when you mix and ? This really is an integration test, but oh well. ...
static stripNoflip($css)
Strip annotations from CSS code.
$basePath
Definition: exportSites.php:3
getResourceLoaderContext($lang= 'en', $dir= 'ltr')
$css
ResourceLoader module based on local JavaScript/CSS files.
testGetTemplates($module, $expected)
providerGetTemplates ResourceLoaderFileModule::getTemplates
testGetAllSkinStyleFiles()
ResourceLoaderFileModule::getAllStyleFiles ResourceLoaderFileModule::getAllSkinStyleFiles ResourceLoa...
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static getDefaultInstance()
Definition: SkinFactory.php:50