1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
<?php
use MediaWiki\MediaWikiServices;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\TestingAccessWrapper;
/**
* @covers ResourceLoaderWikiModule
*/
class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
/**
* @dataProvider provideConstructor
*/
public function testConstructor( $params ) {
$module = new ResourceLoaderWikiModule( $params );
$this->assertInstanceOf( ResourceLoaderWikiModule::class, $module );
}
public static function provideConstructor() {
yield 'null' => [ null ];
yield 'empty' => [ [] ];
yield 'unknown settings' => [ [ 'foo' => 'baz' ] ];
yield 'real settings' => [ [ 'MediaWiki:Common.js' ] ];
}
private function prepareTitleInfo( array $mockInfo ) {
$module = TestingAccessWrapper::newFromClass( ResourceLoaderWikiModule::class );
$info = [];
foreach ( $mockInfo as $key => $val ) {
$info[ $module->makeTitleKey( Title::newFromText( $key ) ) ] = $val;
}
return $info;
}
/**
* @dataProvider provideGetPages
*/
public function testGetPages( $params, Config $config, $expected ) {
$module = new ResourceLoaderWikiModule( $params );
$module->setConfig( $config );
// Because getPages is protected..
$getPages = new ReflectionMethod( $module, 'getPages' );
$getPages->setAccessible( true );
$out = $getPages->invoke( $module, ResourceLoaderContext::newDummyContext() );
$this->assertSame( $expected, $out );
}
public static function provideGetPages() {
$settings = self::getSettings() + [
'UseSiteJs' => true,
'UseSiteCss' => true,
];
$params = [
'styles' => [ 'MediaWiki:Common.css' ],
'scripts' => [ 'MediaWiki:Common.js' ],
];
return [
[ [], new HashConfig( $settings ), [] ],
[ $params, new HashConfig( $settings ), [
'MediaWiki:Common.js' => [ 'type' => 'script' ],
'MediaWiki:Common.css' => [ 'type' => 'style' ]
] ],
[ $params, new HashConfig( [ 'UseSiteCss' => false ] + $settings ), [
'MediaWiki:Common.js' => [ 'type' => 'script' ],
] ],
[ $params, new HashConfig( [ 'UseSiteJs' => false ] + $settings ), [
'MediaWiki:Common.css' => [ 'type' => 'style' ],
] ],
[ $params,
new HashConfig(
[ 'UseSiteJs' => false, 'UseSiteCss' => false ]
),
[]
],
];
}
/**
* @dataProvider provideGetGroup
*/
public function testGetGroup( $params, $expected ) {
$module = new ResourceLoaderWikiModule( $params );
$this->assertSame( $expected, $module->getGroup() );
}
public static function provideGetGroup() {
yield 'no group' => [ [], null ];
yield 'some group' => [ [ 'group' => 'foobar' ], 'foobar' ];
}
/**
* @dataProvider provideGetType
*/
public function testGetType( $params, $expected ) {
$module = new ResourceLoaderWikiModule( $params );
$this->assertSame( $expected, $module->getType() );
}
public static function provideGetType() {
yield 'empty' => [
[],
ResourceLoaderWikiModule::LOAD_GENERAL,
];
yield 'scripts' => [
[ 'scripts' => [ 'Example.js' ] ],
ResourceLoaderWikiModule::LOAD_GENERAL,
];
yield 'styles' => [
[ 'styles' => [ 'Example.css' ] ],
ResourceLoaderWikiModule::LOAD_STYLES,
];
yield 'styles and scripts' => [
[ 'styles' => [ 'Example.css' ], 'scripts' => [ 'Example.js' ] ],
ResourceLoaderWikiModule::LOAD_GENERAL,
];
}
/**
* @dataProvider provideIsKnownEmpty
*/
public function testIsKnownEmpty( $titleInfo, $group, $dependencies, $expected ) {
$module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
->disableOriginalConstructor()
->setMethods( [ 'getTitleInfo', 'getGroup', 'getDependencies' ] )
->getMock();
$module->method( 'getTitleInfo' )
->willReturn( $this->prepareTitleInfo( $titleInfo ) );
$module->method( 'getGroup' )
->willReturn( $group );
$module->method( 'getDependencies' )
->willReturn( $dependencies );
$context = $this->createMock( ResourceLoaderContext::class );
$this->assertSame( $expected, $module->isKnownEmpty( $context ) );
}
public static function provideIsKnownEmpty() {
yield 'nothing' => [
[],
null,
[],
// No pages exist, considered empty.
true,
];
yield 'an empty page exists (no group)' => [
[ 'Project:Example/foo.js' => [ 'page_len' => 0 ] ],
null,
[],
// There is an existing page, so we should let the module be queued.
// Its emptiness might be temporary, hence considered non-empty (T70488).
false,
];
yield 'an empty page exists (site group)' => [
[ 'MediaWiki:Foo.js' => [ 'page_len' => 0 ] ],
'site',
[],
// There is an existing page, hence considered non-empty.
false,
];
yield 'an empty page exists (user group)' => [
[ 'User:Example/foo.js' => [ 'page_len' => 0 ] ],
'user',
[],
// There is an existing page, but it is empty.
// For user-specific modules, don't bother loading a known-empty module.
// Given user-specific HTML output, this will vary and re-appear if/when
// the page becomes non-empty again.
true,
];
yield 'no pages but having dependencies (no group)' => [
[],
null,
[ 'another-module' ],
false,
];
yield 'no pages but having dependencies (site group)' => [
[],
'site',
[ 'another-module' ],
false,
];
yield 'no pages but having dependencies (user group)' => [
[],
'user',
[ 'another-module' ],
false,
];
yield 'a non-empty page exists (user group)' => [
[ 'User:Example/foo.js' => [ 'page_len' => 25 ] ],
'user',
[],
false,
];
yield 'a non-empty page exists (site group)' => [
[ 'MediaWiki:Foo.js' => [ 'page_len' => 25 ] ],
'site',
[],
false,
];
}
public function testGetTitleInfo() {
$pages = [
'MediaWiki:Common.css' => [ 'type' => 'styles' ],
'mediawiki: fallback.css' => [ 'type' => 'styles' ],
];
$titleInfo = $this->prepareTitleInfo( [
'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
] );
$expected = $titleInfo;
$module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
->setMethods( [ 'getPages', 'getTitleInfo' ] )
->getMock();
$module->method( 'getPages' )->willReturn( $pages );
$module->method( 'getTitleInfo' )->willReturn( $titleInfo );
$context = $this->getMockBuilder( ResourceLoaderContext::class )
->disableOriginalConstructor()
->getMock();
$module = TestingAccessWrapper::newFromObject( $module );
$this->assertSame( $expected, $module->getTitleInfo( $context ), 'Title info' );
}
public function testGetPreloadedTitleInfo() {
$pages = [
'MediaWiki:Common.css' => [ 'type' => 'styles' ],
// Regression against T145673. It's impossible to statically declare page names in
// a canonical way since the canonical prefix is localised. As such, the preload
// cache computed the right cache key, but failed to find the results when
// doing an intersect on the canonical result, producing an empty array.
'mediawiki: fallback.css' => [ 'type' => 'styles' ],
];
$titleInfo = $this->prepareTitleInfo( [
'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
] );
$expected = $titleInfo;
$module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
->setMethods( [ 'getPages' ] )
->getMock();
$module->method( 'getPages' )->willReturn( $pages );
// Can't mock static methods
$module::$returnFetchTitleInfo = $titleInfo;
$rl = new EmptyResourceLoader();
$context = new ResourceLoaderContext( $rl, new FauxRequest() );
TestResourceLoaderWikiModule::invalidateModuleCache(
Title::newFromText( 'MediaWiki:Common.css' ),
null,
null,
wfWikiID()
);
TestResourceLoaderWikiModule::preloadTitleInfo(
$context,
$this->createMock( IDatabase::class ),
[ 'testmodule' ]
);
$module = TestingAccessWrapper::newFromObject( $module );
$this->assertSame( $expected, $module->getTitleInfo( $context ), 'Title info' );
}
public function testGetPreloadedBadTitle() {
// Set up
TestResourceLoaderWikiModule::$returnFetchTitleInfo = [];
$rl = new EmptyResourceLoader();
$rl->getConfig()->set( 'UseSiteJs', true );
$rl->getConfig()->set( 'UseSiteCss', true );
$rl->register( 'testmodule', [
'class' => TestResourceLoaderWikiModule::class,
// Covers preloadTitleInfo branch for invalid page name
'styles' => [ '[x]' ],
] );
$context = new ResourceLoaderContext( $rl, new FauxRequest() );
// Act
TestResourceLoaderWikiModule::preloadTitleInfo(
$context,
$this->createMock( IDatabase::class ),
[ 'testmodule' ]
);
// Assert
$module = TestingAccessWrapper::newFromObject( $rl->getModule( 'testmodule' ) );
$this->assertSame( [], $module->getTitleInfo( $context ), 'Title info' );
}
public function testGetPreloadedTitleInfoEmpty() {
$context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
// This covers the early return case
$this->assertSame(
null,
ResourceLoaderWikiModule::preloadTitleInfo(
$context,
$this->createMock( IDatabase::class ),
[]
)
);
}
public static function provideGetContent() {
yield 'Bad title' => [ null, '[x]' ];
yield 'No JS content found' => [ null, [
'text' => 'MediaWiki:Foo.js',
'ns' => NS_MEDIAWIKI,
'title' => 'Foo.js',
] ];
yield 'JS content' => [ 'code;', [
'text' => 'MediaWiki:Foo.js',
'ns' => NS_MEDIAWIKI,
'title' => 'Foo.js',
], new JavaScriptContent( 'code;' ) ];
yield 'CSS content' => [ 'code {}', [
'text' => 'MediaWiki:Foo.css',
'ns' => NS_MEDIAWIKI,
'title' => 'Foo.css',
], new CssContent( 'code {}' ) ];
yield 'Wikitext content' => [ null, [
'text' => 'MediaWiki:Foo',
'ns' => NS_MEDIAWIKI,
'title' => 'Foo',
], new WikitextContent( 'code;' ) ];
}
/**
* @dataProvider provideGetContent
*/
public function testGetContent( $expected, $title, Content $contentObj = null ) {
$context = $this->getResourceLoaderContext( [], new EmptyResourceLoader );
$module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
->setMethods( [ 'getContentObj' ] )->getMock();
$module->method( 'getContentObj' )
->willReturn( $contentObj );
if ( is_array( $title ) ) {
$title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ];
$titleText = $title['text'];
// Mock Title db access via LinkCache
MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
$title['id'],
new TitleValue( $title['ns'], $title['title'] ),
$title['len'],
$title['redirect']
);
} else {
$titleText = $title;
}
$module = TestingAccessWrapper::newFromObject( $module );
$this->assertSame(
$expected,
$module->getContent( $titleText, $context )
);
}
public function testContentOverrides() {
$pages = [
'MediaWiki:Common.css' => [ 'type' => 'style' ],
];
$module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
->setMethods( [ 'getPages' ] )
->getMock();
$module->method( 'getPages' )->willReturn( $pages );
$rl = new EmptyResourceLoader();
$context = new DerivativeResourceLoaderContext(
new ResourceLoaderContext( $rl, new FauxRequest() )
);
$context->setContentOverrideCallback( function ( Title $t ) {
if ( $t->getPrefixedText() === 'MediaWiki:Common.css' ) {
return new CssContent( '.override{}' );
}
return null;
} );
$this->assertTrue( $module->shouldEmbedModule( $context ) );
$this->assertSame( [
'all' => [
"/*\nMediaWiki:Common.css\n*/\n.override{}"
]
], $module->getStyles( $context ) );
$context->setContentOverrideCallback( function ( Title $t ) {
if ( $t->getPrefixedText() === 'MediaWiki:Skin.css' ) {
return new CssContent( '.override{}' );
}
return null;
} );
$this->assertFalse( $module->shouldEmbedModule( $context ) );
}
public function testGetContentForRedirects() {
// Set up context and module object
$context = new DerivativeResourceLoaderContext(
$this->getResourceLoaderContext( [], new EmptyResourceLoader )
);
$module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
->setMethods( [ 'getPages' ] )
->getMock();
$module->method( 'getPages' )
->willReturn( [
'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
] );
$context->setContentOverrideCallback( function ( Title $title ) {
if ( $title->getPrefixedText() === 'MediaWiki:Redirect.js' ) {
$handler = new JavaScriptContentHandler();
return $handler->makeRedirectContent(
Title::makeTitle( NS_MEDIAWIKI, 'Target.js' )
);
} elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) {
return new JavaScriptContent( 'target;' );
} else {
return null;
}
} );
// Mock away Title's db queries with LinkCache
MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
1, // id
new TitleValue( NS_MEDIAWIKI, 'Redirect.js' ),
1, // len
1 // redirect
);
$this->assertSame(
"/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
$module->getScript( $context ),
'Redirect resolved by getContent'
);
}
public function tearDown() {
Title::clearCaches();
parent::tearDown();
}
}
class TestResourceLoaderWikiModule extends ResourceLoaderWikiModule {
public static $returnFetchTitleInfo = null;
protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = null ) {
$ret = self::$returnFetchTitleInfo;
self::$returnFetchTitleInfo = null;
return $ret;
}
}
|