Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / resourceloader / ResourceLoaderWikiModuleTest.php
bloba332528ffb9ad72cdb00ae3fdbffd59225f2cb7c
1 <?php
3 class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
5 /**
6 * @covers ResourceLoaderWikiModule::__construct
7 * @dataProvider provideConstructor
8 */
9 public function testConstructor( $params ) {
10 $module = new ResourceLoaderWikiModule( $params );
11 $this->assertInstanceOf( 'ResourceLoaderWikiModule', $module );
14 public static function provideConstructor() {
15 return [
16 // Nothing
17 [ null ],
18 [ [] ],
19 // Unrecognized settings
20 [ [ 'foo' => 'baz' ] ],
21 // Real settings
22 [ [ 'scripts' => [ 'MediaWiki:Common.js' ] ] ],
26 /**
27 * @dataProvider provideGetPages
28 * @covers ResourceLoaderWikiModule::getPages
30 public function testGetPages( $params, Config $config, $expected ) {
31 $module = new ResourceLoaderWikiModule( $params );
32 $module->setConfig( $config );
34 // Because getPages is protected..
35 $getPages = new ReflectionMethod( $module, 'getPages' );
36 $getPages->setAccessible( true );
37 $out = $getPages->invoke( $module, ResourceLoaderContext::newDummyContext() );
38 $this->assertEquals( $expected, $out );
41 public static function provideGetPages() {
42 $settings = self::getSettings() + [
43 'UseSiteJs' => true,
44 'UseSiteCss' => true,
47 $params = [
48 'styles' => [ 'MediaWiki:Common.css' ],
49 'scripts' => [ 'MediaWiki:Common.js' ],
52 return [
53 [ [], new HashConfig( $settings ), [] ],
54 [ $params, new HashConfig( $settings ), [
55 'MediaWiki:Common.js' => [ 'type' => 'script' ],
56 'MediaWiki:Common.css' => [ 'type' => 'style' ]
57 ] ],
58 [ $params, new HashConfig( [ 'UseSiteCss' => false ] + $settings ), [
59 'MediaWiki:Common.js' => [ 'type' => 'script' ],
60 ] ],
61 [ $params, new HashConfig( [ 'UseSiteJs' => false ] + $settings ), [
62 'MediaWiki:Common.css' => [ 'type' => 'style' ],
63 ] ],
64 [ $params,
65 new HashConfig(
66 [ 'UseSiteJs' => false, 'UseSiteCss' => false ]
73 /**
74 * @covers ResourceLoaderWikiModule::getGroup
75 * @dataProvider provideGetGroup
77 public function testGetGroup( $params, $expected ) {
78 $module = new ResourceLoaderWikiModule( $params );
79 $this->assertEquals( $expected, $module->getGroup() );
82 public static function provideGetGroup() {
83 return [
84 // No group specified
85 [ [], null ],
86 // A random group
87 [ [ 'group' => 'foobar' ], 'foobar' ],
91 /**
92 * @covers ResourceLoaderWikiModule::isKnownEmpty
93 * @dataProvider provideIsKnownEmpty
95 public function testIsKnownEmpty( $titleInfo, $group, $expected ) {
96 $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' )
97 ->setMethods( [ 'getTitleInfo', 'getGroup' ] )
98 ->getMock();
99 $module->expects( $this->any() )
100 ->method( 'getTitleInfo' )
101 ->will( $this->returnValue( $titleInfo ) );
102 $module->expects( $this->any() )
103 ->method( 'getGroup' )
104 ->will( $this->returnValue( $group ) );
105 $context = $this->getMockBuilder( 'ResourceLoaderContext' )
106 ->disableOriginalConstructor()
107 ->getMock();
108 $this->assertEquals( $expected, $module->isKnownEmpty( $context ) );
111 public static function provideIsKnownEmpty() {
112 return [
113 // No valid pages
114 [ [], 'test1', true ],
115 // 'site' module with a non-empty page
117 [ 'MediaWiki:Common.js' => [ 'page_len' => 1234 ] ],
118 'site',
119 false,
121 // 'site' module with an empty page
123 [ 'MediaWiki:Foo.js' => [ 'page_len' => 0 ] ],
124 'site',
125 false,
127 // 'user' module with a non-empty page
129 [ 'User:Example/common.js' => [ 'page_len' => 25 ] ],
130 'user',
131 false,
133 // 'user' module with an empty page
135 [ 'User:Example/foo.js' => [ 'page_len' => 0 ] ],
136 'user',
137 true,
143 * @covers ResourceLoaderWikiModule::getTitleInfo
145 public function testGetTitleInfo() {
146 $pages = [
147 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
148 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
150 $titleInfo = [
151 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
152 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
154 $expected = $titleInfo;
156 $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
157 ->setMethods( [ 'getPages' ] )
158 ->getMock();
159 $module->method( 'getPages' )->willReturn( $pages );
160 // Can't mock static methods
161 $module::$returnFetchTitleInfo = $titleInfo;
163 $context = $this->getMockBuilder( 'ResourceLoaderContext' )
164 ->disableOriginalConstructor()
165 ->getMock();
167 $module = TestingAccessWrapper::newFromObject( $module );
168 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
172 * @covers ResourceLoaderWikiModule::getTitleInfo
173 * @covers ResourceLoaderWikiModule::setTitleInfo
174 * @covers ResourceLoaderWikiModule::preloadTitleInfo
176 public function testGetPreloadedTitleInfo() {
177 $pages = [
178 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
179 // Regression against T145673. It's impossible to statically declare page names in
180 // a canonical way since the canonical prefix is localised. As such, the preload
181 // cache computed the right cache key, but failed to find the results when
182 // doing an intersect on the canonical result, producing an empty array.
183 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
185 $titleInfo = [
186 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
187 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
189 $expected = $titleInfo;
191 $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
192 ->setMethods( [ 'getPages' ] )
193 ->getMock();
194 $module->method( 'getPages' )->willReturn( $pages );
195 // Can't mock static methods
196 $module::$returnFetchTitleInfo = $titleInfo;
198 $rl = new EmptyResourceLoader();
199 $rl->register( 'testmodule', $module );
200 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
202 TestResourceLoaderWikiModule::invalidateModuleCache(
203 Title::newFromText( 'MediaWiki:Common.css' ),
204 null,
205 null,
206 wfWikiID()
208 TestResourceLoaderWikiModule::preloadTitleInfo(
209 $context,
210 wfGetDB( DB_REPLICA ),
211 [ 'testmodule' ]
214 $module = TestingAccessWrapper::newFromObject( $module );
215 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
219 class TestResourceLoaderWikiModule extends ResourceLoaderWikiModule {
220 public static $returnFetchTitleInfo = null;
221 protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = null ) {
222 $ret = self::$returnFetchTitleInfo;
223 self::$returnFetchTitleInfo = null;
224 return $ret;