MediaWiki  1.32.0
TitleMethodsTest.php
Go to the documentation of this file.
1 <?php
2 
4 
13 
14  protected function setUp() {
15  parent::setUp();
16 
18  'wgExtraNamespaces',
19  [
20  12302 => 'TEST-JS',
21  12303 => 'TEST-JS_TALK',
22  ]
23  );
24 
26  'wgNamespaceContentModels',
27  [
28  12302 => CONTENT_MODEL_JAVASCRIPT,
29  ]
30  );
31  }
32 
33  public static function provideEquals() {
34  return [
35  [ 'Main Page', 'Main Page', true ],
36  [ 'Main Page', 'Not The Main Page', false ],
37  [ 'Main Page', 'Project:Main Page', false ],
38  [ 'File:Example.png', 'Image:Example.png', true ],
39  [ 'Special:Version', 'Special:Version', true ],
40  [ 'Special:Version', 'Special:Recentchanges', false ],
41  [ 'Special:Version', 'Main Page', false ],
42  ];
43  }
44 
49  public function testEquals( $titleA, $titleB, $expectedBool ) {
50  $titleA = Title::newFromText( $titleA );
51  $titleB = Title::newFromText( $titleB );
52 
53  $this->assertEquals( $expectedBool, $titleA->equals( $titleB ) );
54  $this->assertEquals( $expectedBool, $titleB->equals( $titleA ) );
55  }
56 
57  public static function provideInNamespace() {
58  return [
59  [ 'Main Page', NS_MAIN, true ],
60  [ 'Main Page', NS_TALK, false ],
61  [ 'Main Page', NS_USER, false ],
62  [ 'User:Foo', NS_USER, true ],
63  [ 'User:Foo', NS_USER_TALK, false ],
64  [ 'User:Foo', NS_TEMPLATE, false ],
65  [ 'User_talk:Foo', NS_USER_TALK, true ],
66  [ 'User_talk:Foo', NS_USER, false ],
67  ];
68  }
69 
74  public function testInNamespace( $title, $ns, $expectedBool ) {
76  $this->assertEquals( $expectedBool, $title->inNamespace( $ns ) );
77  }
78 
82  public function testInNamespaces() {
83  $mainpage = Title::newFromText( 'Main Page' );
84  $this->assertTrue( $mainpage->inNamespaces( NS_MAIN, NS_USER ) );
85  $this->assertTrue( $mainpage->inNamespaces( [ NS_MAIN, NS_USER ] ) );
86  $this->assertTrue( $mainpage->inNamespaces( [ NS_USER, NS_MAIN ] ) );
87  $this->assertFalse( $mainpage->inNamespaces( [ NS_PROJECT, NS_TEMPLATE ] ) );
88  }
89 
90  public static function provideHasSubjectNamespace() {
91  return [
92  [ 'Main Page', NS_MAIN, true ],
93  [ 'Main Page', NS_TALK, true ],
94  [ 'Main Page', NS_USER, false ],
95  [ 'User:Foo', NS_USER, true ],
96  [ 'User:Foo', NS_USER_TALK, true ],
97  [ 'User:Foo', NS_TEMPLATE, false ],
98  [ 'User_talk:Foo', NS_USER_TALK, true ],
99  [ 'User_talk:Foo', NS_USER, true ],
100  ];
101  }
102 
107  public function testHasSubjectNamespace( $title, $ns, $expectedBool ) {
109  $this->assertEquals( $expectedBool, $title->hasSubjectNamespace( $ns ) );
110  }
111 
112  public function dataGetContentModel() {
113  return [
114  [ 'Help:Foo', CONTENT_MODEL_WIKITEXT ],
115  [ 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ],
116  [ 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ],
117  [ 'User:Foo', CONTENT_MODEL_WIKITEXT ],
118  [ 'User:Foo.js', CONTENT_MODEL_WIKITEXT ],
119  [ 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ],
120  [ 'User:Foo/bar.css', CONTENT_MODEL_CSS ],
121  [ 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ],
122  [ 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ],
123  [ 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ],
124  [ 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ],
125  [ 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ],
126  [ 'MediaWiki:Foo/bar.css', CONTENT_MODEL_CSS ],
127  [ 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ],
128  [ 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ],
129  [ 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ],
130  [ 'TEST-JS:Foo', CONTENT_MODEL_JAVASCRIPT ],
131  [ 'TEST-JS:Foo.js', CONTENT_MODEL_JAVASCRIPT ],
132  [ 'TEST-JS:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ],
133  [ 'TEST-JS_TALK:Foo.js', CONTENT_MODEL_WIKITEXT ],
134  ];
135  }
136 
141  public function testGetContentModel( $title, $expectedModelId ) {
143  $this->assertEquals( $expectedModelId, $title->getContentModel() );
144  }
145 
150  public function testHasContentModel( $title, $expectedModelId ) {
152  $this->assertTrue( $title->hasContentModel( $expectedModelId ) );
153  }
154 
155  public static function provideIsSiteConfigPage() {
156  return [
157  [ 'Help:Foo', false ],
158  [ 'Help:Foo.js', false ],
159  [ 'Help:Foo/bar.js', false ],
160  [ 'User:Foo', false ],
161  [ 'User:Foo.js', false ],
162  [ 'User:Foo/bar.js', false ],
163  [ 'User:Foo/bar.json', false ],
164  [ 'User:Foo/bar.css', false ],
165  [ 'User:Foo/bar.JS', false ],
166  [ 'User:Foo/bar.JSON', false ],
167  [ 'User:Foo/bar.CSS', false ],
168  [ 'User talk:Foo/bar.css', false ],
169  [ 'User:Foo/bar.js.xxx', false ],
170  [ 'User:Foo/bar.xxx', false ],
171  [ 'MediaWiki:Foo.js', true ],
172  [ 'MediaWiki:Foo.json', true ],
173  [ 'MediaWiki:Foo.css', true ],
174  [ 'MediaWiki:Foo.JS', false ],
175  [ 'MediaWiki:Foo.JSON', false ],
176  [ 'MediaWiki:Foo.CSS', false ],
177  [ 'MediaWiki:Foo/bar.css', true ],
178  [ 'MediaWiki:Foo.css.xxx', false ],
179  [ 'TEST-JS:Foo', false ],
180  [ 'TEST-JS:Foo.js', false ],
181  ];
182  }
183 
188  public function testSiteConfigPage( $title, $expectedBool ) {
190  $this->assertEquals( $expectedBool, $title->isSiteConfigPage() );
191  }
192 
193  public static function provideIsUserConfigPage() {
194  return [
195  [ 'Help:Foo', false ],
196  [ 'Help:Foo.js', false ],
197  [ 'Help:Foo/bar.js', false ],
198  [ 'User:Foo', false ],
199  [ 'User:Foo.js', false ],
200  [ 'User:Foo/bar.js', true ],
201  [ 'User:Foo/bar.JS', false ],
202  [ 'User:Foo/bar.json', true ],
203  [ 'User:Foo/bar.JSON', false ],
204  [ 'User:Foo/bar.css', true ],
205  [ 'User:Foo/bar.CSS', false ],
206  [ 'User talk:Foo/bar.css', false ],
207  [ 'User:Foo/bar.js.xxx', false ],
208  [ 'User:Foo/bar.xxx', false ],
209  [ 'MediaWiki:Foo.js', false ],
210  [ 'MediaWiki:Foo.json', false ],
211  [ 'MediaWiki:Foo.css', false ],
212  [ 'MediaWiki:Foo.JS', false ],
213  [ 'MediaWiki:Foo.JSON', false ],
214  [ 'MediaWiki:Foo.CSS', false ],
215  [ 'MediaWiki:Foo.css.xxx', false ],
216  [ 'TEST-JS:Foo', false ],
217  [ 'TEST-JS:Foo.js', false ],
218  ];
219  }
220 
225  public function testIsUserConfigPage( $title, $expectedBool ) {
227  $this->assertEquals( $expectedBool, $title->isUserConfigPage() );
228  }
229 
230  public static function provideIsUserCssConfigPage() {
231  return [
232  [ 'Help:Foo', false ],
233  [ 'Help:Foo.css', false ],
234  [ 'User:Foo', false ],
235  [ 'User:Foo.js', false ],
236  [ 'User:Foo.json', false ],
237  [ 'User:Foo.css', false ],
238  [ 'User:Foo/bar.js', false ],
239  [ 'User:Foo/bar.json', false ],
240  [ 'User:Foo/bar.css', true ],
241  ];
242  }
243 
248  public function testIsUserCssConfigPage( $title, $expectedBool ) {
250  $this->assertEquals( $expectedBool, $title->isUserCssConfigPage() );
251  }
252 
253  public static function provideIsUserJsConfigPage() {
254  return [
255  [ 'Help:Foo', false ],
256  [ 'Help:Foo.css', false ],
257  [ 'User:Foo', false ],
258  [ 'User:Foo.js', false ],
259  [ 'User:Foo.json', false ],
260  [ 'User:Foo.css', false ],
261  [ 'User:Foo/bar.js', true ],
262  [ 'User:Foo/bar.json', false ],
263  [ 'User:Foo/bar.css', false ],
264  ];
265  }
266 
271  public function testIsUserJsConfigPage( $title, $expectedBool ) {
273  $this->assertEquals( $expectedBool, $title->isUserJsConfigPage() );
274  }
275 
276  public static function provideIsWikitextPage() {
277  return [
278  [ 'Help:Foo', true ],
279  [ 'Help:Foo.js', true ],
280  [ 'Help:Foo/bar.js', true ],
281  [ 'User:Foo', true ],
282  [ 'User:Foo.js', true ],
283  [ 'User:Foo/bar.js', false ],
284  [ 'User:Foo/bar.json', false ],
285  [ 'User:Foo/bar.css', false ],
286  [ 'User talk:Foo/bar.css', true ],
287  [ 'User:Foo/bar.js.xxx', true ],
288  [ 'User:Foo/bar.xxx', true ],
289  [ 'MediaWiki:Foo.js', false ],
290  [ 'User:Foo/bar.JS', true ],
291  [ 'User:Foo/bar.JSON', true ],
292  [ 'User:Foo/bar.CSS', true ],
293  [ 'MediaWiki:Foo.json', false ],
294  [ 'MediaWiki:Foo.css', false ],
295  [ 'MediaWiki:Foo.JS', true ],
296  [ 'MediaWiki:Foo.JSON', true ],
297  [ 'MediaWiki:Foo.CSS', true ],
298  [ 'MediaWiki:Foo.css.xxx', true ],
299  [ 'TEST-JS:Foo', false ],
300  [ 'TEST-JS:Foo.js', false ],
301  ];
302  }
303 
308  public function testIsWikitextPage( $title, $expectedBool ) {
310  $this->assertEquals( $expectedBool, $title->isWikitextPage() );
311  }
312 
313  public static function provideGetOtherPage() {
314  return [
315  [ 'Main Page', 'Talk:Main Page' ],
316  [ 'Talk:Main Page', 'Main Page' ],
317  [ 'Help:Main Page', 'Help talk:Main Page' ],
318  [ 'Help talk:Main Page', 'Help:Main Page' ],
319  [ 'Special:FooBar', null ],
320  [ 'Media:File.jpg', null ],
321  ];
322  }
323 
331  public function testGetOtherPage( $text, $expected ) {
332  if ( $expected === null ) {
333  $this->setExpectedException( MWException::class );
334  }
335 
336  $title = Title::newFromText( $text );
337  $this->assertEquals( $expected, $title->getOtherPage()->getPrefixedText() );
338  }
339 
343  public function testClearCaches() {
344  $linkCache = MediaWikiServices::getInstance()->getLinkCache();
345 
346  $title1 = Title::newFromText( 'Foo' );
347  $linkCache->addGoodLinkObj( 23, $title1 );
348 
350 
351  $title2 = Title::newFromText( 'Foo' );
352  $this->assertNotSame( $title1, $title2, 'title cache should be empty' );
353  $this->assertEquals( 0, $linkCache->getGoodLinkID( 'Foo' ), 'link cache should be empty' );
354  }
355 }
TitleMethodsTest\testHasSubjectNamespace
testHasSubjectNamespace( $title, $ns, $expectedBool)
provideHasSubjectNamespace Title::hasSubjectNamespace
Definition: TitleMethodsTest.php:107
TitleMethodsTest\testClearCaches
testClearCaches()
Title::clearCaches.
Definition: TitleMethodsTest.php:343
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:280
TitleMethodsTest\testEquals
testEquals( $titleA, $titleB, $expectedBool)
provideEquals Title::equals
Definition: TitleMethodsTest.php:49
TitleMethodsTest\provideGetOtherPage
static provideGetOtherPage()
Definition: TitleMethodsTest.php:313
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:901
TitleMethodsTest\provideIsUserCssConfigPage
static provideIsUserCssConfigPage()
Definition: TitleMethodsTest.php:230
TitleMethodsTest\testHasContentModel
testHasContentModel( $title, $expectedModelId)
dataGetContentModel Title::hasContentModel
Definition: TitleMethodsTest.php:150
Title\clearCaches
static clearCaches()
Text form (spaces not underscores) of the main part.
Definition: Title.php:3705
TitleMethodsTest\provideIsWikitextPage
static provideIsWikitextPage()
Definition: TitleMethodsTest.php:276
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition: Defines.php:237
NS_TEMPLATE
const NS_TEMPLATE
Definition: Defines.php:74
TitleMethodsTest\provideIsUserConfigPage
static provideIsUserConfigPage()
Definition: TitleMethodsTest.php:193
TitleMethodsTest\provideIsUserJsConfigPage
static provideIsUserJsConfigPage()
Definition: TitleMethodsTest.php:253
TitleMethodsTest\testInNamespaces
testInNamespaces()
Title::inNamespaces.
Definition: TitleMethodsTest.php:82
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:235
TitleMethodsTest
ContentHandler Database.
Definition: TitleMethodsTest.php:12
TitleMethodsTest\testIsWikitextPage
testIsWikitextPage( $title, $expectedBool)
provideIsWikitextPage Title::isWikitextPage
Definition: TitleMethodsTest.php:308
php
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
TitleMethodsTest\provideIsSiteConfigPage
static provideIsSiteConfigPage()
Definition: TitleMethodsTest.php:155
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:68
TitleMethodsTest\testGetOtherPage
testGetOtherPage( $text, $expected)
provideGetOtherpage Title::getOtherPage
Definition: TitleMethodsTest.php:331
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
TitleMethodsTest\testGetContentModel
testGetContentModel( $title, $expectedModelId)
dataGetContentModel Title::getContentModel
Definition: TitleMethodsTest.php:141
TitleMethodsTest\testIsUserJsConfigPage
testIsUserJsConfigPage( $title, $expectedBool)
provideIsUserJsConfigPage Title::isUserJsConfigPage
Definition: TitleMethodsTest.php:271
TitleMethodsTest\provideEquals
static provideEquals()
Definition: TitleMethodsTest.php:33
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:67
TitleMethodsTest\provideHasSubjectNamespace
static provideHasSubjectNamespace()
Definition: TitleMethodsTest.php:90
TitleMethodsTest\provideInNamespace
static provideInNamespace()
Definition: TitleMethodsTest.php:57
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:8
TitleMethodsTest\dataGetContentModel
dataGetContentModel()
Definition: TitleMethodsTest.php:112
TitleMethodsTest\setUp
setUp()
Definition: TitleMethodsTest.php:14
TitleMethodsTest\testIsUserCssConfigPage
testIsUserCssConfigPage( $title, $expectedBool)
provideIsUserCssConfigPage Title::isUserCssConfigPage
Definition: TitleMethodsTest.php:248
NS_USER
const NS_USER
Definition: Defines.php:66
TitleMethodsTest\testSiteConfigPage
testSiteConfigPage( $title, $expectedBool)
provideIsSiteConfigPage Title::isSiteConfigPage
Definition: TitleMethodsTest.php:188
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:2036
NS_TALK
const NS_TALK
Definition: Defines.php:65
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:236
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:23
TitleMethodsTest\testInNamespace
testInNamespace( $title, $ns, $expectedBool)
provideInNamespace Title::inNamespace
Definition: TitleMethodsTest.php:74
TitleMethodsTest\testIsUserConfigPage
testIsUserConfigPage( $title, $expectedBool)
provideIsUserConfigPage Title::isUserConfigPage
Definition: TitleMethodsTest.php:225