TitleMethodsTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. use MediaWiki\MediaWikiServices;
  3. /**
  4. * @group ContentHandler
  5. * @group Database
  6. *
  7. * @note We don't make assumptions about the main namespace.
  8. * But we do expect the Help namespace to contain Wikitext.
  9. */
  10. class TitleMethodsTest extends MediaWikiLangTestCase {
  11. protected function setUp() {
  12. parent::setUp();
  13. $this->mergeMwGlobalArrayValue(
  14. 'wgExtraNamespaces',
  15. [
  16. 12302 => 'TEST-JS',
  17. 12303 => 'TEST-JS_TALK',
  18. ]
  19. );
  20. $this->mergeMwGlobalArrayValue(
  21. 'wgNamespaceContentModels',
  22. [
  23. 12302 => CONTENT_MODEL_JAVASCRIPT,
  24. ]
  25. );
  26. }
  27. public static function provideEquals() {
  28. return [
  29. [ 'Main Page', 'Main Page', true ],
  30. [ 'Main Page', 'Not The Main Page', false ],
  31. [ 'Main Page', 'Project:Main Page', false ],
  32. [ 'File:Example.png', 'Image:Example.png', true ],
  33. [ 'Special:Version', 'Special:Version', true ],
  34. [ 'Special:Version', 'Special:Recentchanges', false ],
  35. [ 'Special:Version', 'Main Page', false ],
  36. ];
  37. }
  38. /**
  39. * @dataProvider provideEquals
  40. * @covers Title::equals
  41. */
  42. public function testEquals( $titleA, $titleB, $expectedBool ) {
  43. $titleA = Title::newFromText( $titleA );
  44. $titleB = Title::newFromText( $titleB );
  45. $this->assertEquals( $expectedBool, $titleA->equals( $titleB ) );
  46. $this->assertEquals( $expectedBool, $titleB->equals( $titleA ) );
  47. }
  48. public static function provideInNamespace() {
  49. return [
  50. [ 'Main Page', NS_MAIN, true ],
  51. [ 'Main Page', NS_TALK, false ],
  52. [ 'Main Page', NS_USER, false ],
  53. [ 'User:Foo', NS_USER, true ],
  54. [ 'User:Foo', NS_USER_TALK, false ],
  55. [ 'User:Foo', NS_TEMPLATE, false ],
  56. [ 'User_talk:Foo', NS_USER_TALK, true ],
  57. [ 'User_talk:Foo', NS_USER, false ],
  58. ];
  59. }
  60. /**
  61. * @dataProvider provideInNamespace
  62. * @covers Title::inNamespace
  63. */
  64. public function testInNamespace( $title, $ns, $expectedBool ) {
  65. $title = Title::newFromText( $title );
  66. $this->assertEquals( $expectedBool, $title->inNamespace( $ns ) );
  67. }
  68. /**
  69. * @covers Title::inNamespaces
  70. */
  71. public function testInNamespaces() {
  72. $mainpage = Title::newFromText( 'Main Page' );
  73. $this->assertTrue( $mainpage->inNamespaces( NS_MAIN, NS_USER ) );
  74. $this->assertTrue( $mainpage->inNamespaces( [ NS_MAIN, NS_USER ] ) );
  75. $this->assertTrue( $mainpage->inNamespaces( [ NS_USER, NS_MAIN ] ) );
  76. $this->assertFalse( $mainpage->inNamespaces( [ NS_PROJECT, NS_TEMPLATE ] ) );
  77. }
  78. public static function provideHasSubjectNamespace() {
  79. return [
  80. [ 'Main Page', NS_MAIN, true ],
  81. [ 'Main Page', NS_TALK, true ],
  82. [ 'Main Page', NS_USER, false ],
  83. [ 'User:Foo', NS_USER, true ],
  84. [ 'User:Foo', NS_USER_TALK, true ],
  85. [ 'User:Foo', NS_TEMPLATE, false ],
  86. [ 'User_talk:Foo', NS_USER_TALK, true ],
  87. [ 'User_talk:Foo', NS_USER, true ],
  88. ];
  89. }
  90. /**
  91. * @dataProvider provideHasSubjectNamespace
  92. * @covers Title::hasSubjectNamespace
  93. */
  94. public function testHasSubjectNamespace( $title, $ns, $expectedBool ) {
  95. $title = Title::newFromText( $title );
  96. $this->assertEquals( $expectedBool, $title->hasSubjectNamespace( $ns ) );
  97. }
  98. public function dataGetContentModel() {
  99. return [
  100. [ 'Help:Foo', CONTENT_MODEL_WIKITEXT ],
  101. [ 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ],
  102. [ 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ],
  103. [ 'User:Foo', CONTENT_MODEL_WIKITEXT ],
  104. [ 'User:Foo.js', CONTENT_MODEL_WIKITEXT ],
  105. [ 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ],
  106. [ 'User:Foo/bar.css', CONTENT_MODEL_CSS ],
  107. [ 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ],
  108. [ 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ],
  109. [ 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ],
  110. [ 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ],
  111. [ 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ],
  112. [ 'MediaWiki:Foo/bar.css', CONTENT_MODEL_CSS ],
  113. [ 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ],
  114. [ 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ],
  115. [ 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ],
  116. [ 'TEST-JS:Foo', CONTENT_MODEL_JAVASCRIPT ],
  117. [ 'TEST-JS:Foo.js', CONTENT_MODEL_JAVASCRIPT ],
  118. [ 'TEST-JS:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ],
  119. [ 'TEST-JS_TALK:Foo.js', CONTENT_MODEL_WIKITEXT ],
  120. ];
  121. }
  122. /**
  123. * @dataProvider dataGetContentModel
  124. * @covers Title::getContentModel
  125. */
  126. public function testGetContentModel( $title, $expectedModelId ) {
  127. $title = Title::newFromText( $title );
  128. $this->assertEquals( $expectedModelId, $title->getContentModel() );
  129. }
  130. /**
  131. * @dataProvider dataGetContentModel
  132. * @covers Title::hasContentModel
  133. */
  134. public function testHasContentModel( $title, $expectedModelId ) {
  135. $title = Title::newFromText( $title );
  136. $this->assertTrue( $title->hasContentModel( $expectedModelId ) );
  137. }
  138. public static function provideIsSiteConfigPage() {
  139. return [
  140. [ 'Help:Foo', false ],
  141. [ 'Help:Foo.js', false ],
  142. [ 'Help:Foo/bar.js', false ],
  143. [ 'User:Foo', false ],
  144. [ 'User:Foo.js', false ],
  145. [ 'User:Foo/bar.js', false ],
  146. [ 'User:Foo/bar.json', false ],
  147. [ 'User:Foo/bar.css', false ],
  148. [ 'User:Foo/bar.JS', false ],
  149. [ 'User:Foo/bar.JSON', false ],
  150. [ 'User:Foo/bar.CSS', false ],
  151. [ 'User talk:Foo/bar.css', false ],
  152. [ 'User:Foo/bar.js.xxx', false ],
  153. [ 'User:Foo/bar.xxx', false ],
  154. [ 'MediaWiki:Foo.js', true ],
  155. [ 'MediaWiki:Foo.json', true ],
  156. [ 'MediaWiki:Foo.css', true ],
  157. [ 'MediaWiki:Foo.JS', false ],
  158. [ 'MediaWiki:Foo.JSON', false ],
  159. [ 'MediaWiki:Foo.CSS', false ],
  160. [ 'MediaWiki:Foo/bar.css', true ],
  161. [ 'MediaWiki:Foo.css.xxx', false ],
  162. [ 'TEST-JS:Foo', false ],
  163. [ 'TEST-JS:Foo.js', false ],
  164. ];
  165. }
  166. /**
  167. * @dataProvider provideIsSiteConfigPage
  168. * @covers Title::isSiteConfigPage
  169. */
  170. public function testSiteConfigPage( $title, $expectedBool ) {
  171. $title = Title::newFromText( $title );
  172. $this->assertEquals( $expectedBool, $title->isSiteConfigPage() );
  173. }
  174. public static function provideIsUserConfigPage() {
  175. return [
  176. [ 'Help:Foo', false ],
  177. [ 'Help:Foo.js', false ],
  178. [ 'Help:Foo/bar.js', false ],
  179. [ 'User:Foo', false ],
  180. [ 'User:Foo.js', false ],
  181. [ 'User:Foo/bar.js', true ],
  182. [ 'User:Foo/bar.JS', false ],
  183. [ 'User:Foo/bar.json', true ],
  184. [ 'User:Foo/bar.JSON', false ],
  185. [ 'User:Foo/bar.css', true ],
  186. [ 'User:Foo/bar.CSS', false ],
  187. [ 'User talk:Foo/bar.css', false ],
  188. [ 'User:Foo/bar.js.xxx', false ],
  189. [ 'User:Foo/bar.xxx', false ],
  190. [ 'MediaWiki:Foo.js', false ],
  191. [ 'MediaWiki:Foo.json', false ],
  192. [ 'MediaWiki:Foo.css', false ],
  193. [ 'MediaWiki:Foo.JS', false ],
  194. [ 'MediaWiki:Foo.JSON', false ],
  195. [ 'MediaWiki:Foo.CSS', false ],
  196. [ 'MediaWiki:Foo.css.xxx', false ],
  197. [ 'TEST-JS:Foo', false ],
  198. [ 'TEST-JS:Foo.js', false ],
  199. ];
  200. }
  201. /**
  202. * @dataProvider provideIsUserConfigPage
  203. * @covers Title::isUserConfigPage
  204. */
  205. public function testIsUserConfigPage( $title, $expectedBool ) {
  206. $title = Title::newFromText( $title );
  207. $this->assertEquals( $expectedBool, $title->isUserConfigPage() );
  208. }
  209. public static function provideIsUserCssConfigPage() {
  210. return [
  211. [ 'Help:Foo', false ],
  212. [ 'Help:Foo.css', false ],
  213. [ 'User:Foo', false ],
  214. [ 'User:Foo.js', false ],
  215. [ 'User:Foo.json', false ],
  216. [ 'User:Foo.css', false ],
  217. [ 'User:Foo/bar.js', false ],
  218. [ 'User:Foo/bar.json', false ],
  219. [ 'User:Foo/bar.css', true ],
  220. ];
  221. }
  222. /**
  223. * @dataProvider provideIsUserCssConfigPage
  224. * @covers Title::isUserCssConfigPage
  225. */
  226. public function testIsUserCssConfigPage( $title, $expectedBool ) {
  227. $title = Title::newFromText( $title );
  228. $this->assertEquals( $expectedBool, $title->isUserCssConfigPage() );
  229. }
  230. public static function provideIsUserJsConfigPage() {
  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', true ],
  239. [ 'User:Foo/bar.json', false ],
  240. [ 'User:Foo/bar.css', false ],
  241. ];
  242. }
  243. /**
  244. * @dataProvider provideIsUserJsConfigPage
  245. * @covers Title::isUserJsConfigPage
  246. */
  247. public function testIsUserJsConfigPage( $title, $expectedBool ) {
  248. $title = Title::newFromText( $title );
  249. $this->assertEquals( $expectedBool, $title->isUserJsConfigPage() );
  250. }
  251. public static function provideIsWikitextPage() {
  252. return [
  253. [ 'Help:Foo', true ],
  254. [ 'Help:Foo.js', true ],
  255. [ 'Help:Foo/bar.js', true ],
  256. [ 'User:Foo', true ],
  257. [ 'User:Foo.js', true ],
  258. [ 'User:Foo/bar.js', false ],
  259. [ 'User:Foo/bar.json', false ],
  260. [ 'User:Foo/bar.css', false ],
  261. [ 'User talk:Foo/bar.css', true ],
  262. [ 'User:Foo/bar.js.xxx', true ],
  263. [ 'User:Foo/bar.xxx', true ],
  264. [ 'MediaWiki:Foo.js', false ],
  265. [ 'User:Foo/bar.JS', true ],
  266. [ 'User:Foo/bar.JSON', true ],
  267. [ 'User:Foo/bar.CSS', true ],
  268. [ 'MediaWiki:Foo.json', false ],
  269. [ 'MediaWiki:Foo.css', false ],
  270. [ 'MediaWiki:Foo.JS', true ],
  271. [ 'MediaWiki:Foo.JSON', true ],
  272. [ 'MediaWiki:Foo.CSS', true ],
  273. [ 'MediaWiki:Foo.css.xxx', true ],
  274. [ 'TEST-JS:Foo', false ],
  275. [ 'TEST-JS:Foo.js', false ],
  276. ];
  277. }
  278. /**
  279. * @dataProvider provideIsWikitextPage
  280. * @covers Title::isWikitextPage
  281. */
  282. public function testIsWikitextPage( $title, $expectedBool ) {
  283. $title = Title::newFromText( $title );
  284. $this->assertEquals( $expectedBool, $title->isWikitextPage() );
  285. }
  286. public static function provideGetOtherPage() {
  287. return [
  288. [ 'Main Page', 'Talk:Main Page' ],
  289. [ 'Talk:Main Page', 'Main Page' ],
  290. [ 'Help:Main Page', 'Help talk:Main Page' ],
  291. [ 'Help talk:Main Page', 'Help:Main Page' ],
  292. [ 'Special:FooBar', null ],
  293. [ 'Media:File.jpg', null ],
  294. ];
  295. }
  296. /**
  297. * @dataProvider provideGetOtherpage
  298. * @covers Title::getOtherPage
  299. *
  300. * @param string $text
  301. * @param string|null $expected
  302. */
  303. public function testGetOtherPage( $text, $expected ) {
  304. if ( $expected === null ) {
  305. $this->setExpectedException( MWException::class );
  306. }
  307. $title = Title::newFromText( $text );
  308. $this->assertEquals( $expected, $title->getOtherPage()->getPrefixedText() );
  309. }
  310. /**
  311. * @covers Title::clearCaches
  312. */
  313. public function testClearCaches() {
  314. $linkCache = MediaWikiServices::getInstance()->getLinkCache();
  315. $title1 = Title::newFromText( 'Foo' );
  316. $linkCache->addGoodLinkObj( 23, $title1 );
  317. Title::clearCaches();
  318. $title2 = Title::newFromText( 'Foo' );
  319. $this->assertNotSame( $title1, $title2, 'title cache should be empty' );
  320. $this->assertEquals( 0, $linkCache->getGoodLinkID( 'Foo' ), 'link cache should be empty' );
  321. }
  322. }