JavaScriptContentTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. use MediaWiki\MediaWikiServices;
  3. /**
  4. * @group ContentHandler
  5. * @group Database
  6. * ^--- needed, because we do need the database to test link updates
  7. */
  8. class JavaScriptContentTest extends TextContentTest {
  9. public function newContent( $text ) {
  10. return new JavaScriptContent( $text );
  11. }
  12. public static function dataGetParserOutput() {
  13. return [
  14. [
  15. 'MediaWiki:Test.js',
  16. null,
  17. "hello <world>\n",
  18. "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello &lt;world&gt;\n\n</pre>"
  19. ],
  20. [
  21. 'MediaWiki:Test.js',
  22. null,
  23. "hello(); // [[world]]\n",
  24. "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello(); // [[world]]\n\n</pre>",
  25. [
  26. 'Links' => [
  27. [ 'World' => 0 ]
  28. ]
  29. ]
  30. ],
  31. // TODO: more...?
  32. ];
  33. }
  34. // XXX: Unused function
  35. public static function dataGetSection() {
  36. return [
  37. [ WikitextContentTest::$sections,
  38. '0',
  39. null
  40. ],
  41. [ WikitextContentTest::$sections,
  42. '2',
  43. null
  44. ],
  45. [ WikitextContentTest::$sections,
  46. '8',
  47. null
  48. ],
  49. ];
  50. }
  51. // XXX: Unused function
  52. public static function dataReplaceSection() {
  53. return [
  54. [ WikitextContentTest::$sections,
  55. '0',
  56. 'No more',
  57. null,
  58. null
  59. ],
  60. [ WikitextContentTest::$sections,
  61. '',
  62. 'No more',
  63. null,
  64. null
  65. ],
  66. [ WikitextContentTest::$sections,
  67. '2',
  68. "== TEST ==\nmore fun",
  69. null,
  70. null
  71. ],
  72. [ WikitextContentTest::$sections,
  73. '8',
  74. 'No more',
  75. null,
  76. null
  77. ],
  78. [ WikitextContentTest::$sections,
  79. 'new',
  80. 'No more',
  81. 'New',
  82. null
  83. ],
  84. ];
  85. }
  86. /**
  87. * @covers JavaScriptContent::addSectionHeader
  88. */
  89. public function testAddSectionHeader() {
  90. $content = $this->newContent( 'hello world' );
  91. $c = $content->addSectionHeader( 'test' );
  92. $this->assertTrue( $content->equals( $c ) );
  93. }
  94. // XXX: currently, preSaveTransform is applied to scripts. this may change or become optional.
  95. public static function dataPreSaveTransform() {
  96. return [
  97. [ 'hello this is ~~~',
  98. "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
  99. ],
  100. [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
  101. 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
  102. ],
  103. [ " Foo \n ",
  104. " Foo",
  105. ],
  106. ];
  107. }
  108. public static function dataPreloadTransform() {
  109. return [
  110. [
  111. 'hello this is ~~~',
  112. 'hello this is ~~~',
  113. ],
  114. [
  115. 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
  116. 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
  117. ],
  118. ];
  119. }
  120. public static function dataGetRedirectTarget() {
  121. return [
  122. [ '#REDIRECT [[Test]]',
  123. null,
  124. ],
  125. [ '#REDIRECT Test',
  126. null,
  127. ],
  128. [ '* #REDIRECT [[Test]]',
  129. null,
  130. ],
  131. ];
  132. }
  133. public static function dataIsCountable() {
  134. return [
  135. [ '',
  136. null,
  137. 'any',
  138. true
  139. ],
  140. [ 'Foo',
  141. null,
  142. 'any',
  143. true
  144. ],
  145. [ 'Foo',
  146. null,
  147. 'link',
  148. false
  149. ],
  150. [ 'Foo [[bar]]',
  151. null,
  152. 'link',
  153. false
  154. ],
  155. [ 'Foo',
  156. true,
  157. 'link',
  158. false
  159. ],
  160. [ 'Foo [[bar]]',
  161. false,
  162. 'link',
  163. false
  164. ],
  165. [ '#REDIRECT [[bar]]',
  166. true,
  167. 'any',
  168. true
  169. ],
  170. [ '#REDIRECT [[bar]]',
  171. true,
  172. 'link',
  173. false
  174. ],
  175. ];
  176. }
  177. public static function dataGetTextForSummary() {
  178. return [
  179. [ "hello\nworld.",
  180. 16,
  181. 'hello world.',
  182. ],
  183. [ 'hello world.',
  184. 8,
  185. 'hello...',
  186. ],
  187. [ '[[hello world]].',
  188. 8,
  189. '[[hel...',
  190. ],
  191. ];
  192. }
  193. /**
  194. * @covers JavaScriptContent::matchMagicWord
  195. */
  196. public function testMatchMagicWord() {
  197. $mw = MediaWikiServices::getInstance()->getMagicWordFactory()->get( "staticredirect" );
  198. $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
  199. $this->assertFalse(
  200. $content->matchMagicWord( $mw ),
  201. "should not have matched magic word, since it's not wikitext"
  202. );
  203. }
  204. /**
  205. * @covers JavaScriptContent::updateRedirect
  206. * @dataProvider provideUpdateRedirect
  207. */
  208. public function testUpdateRedirect( $oldText, $expectedText ) {
  209. $this->setMwGlobals( [
  210. 'wgServer' => '//example.org',
  211. 'wgScriptPath' => '/w',
  212. 'wgScript' => '/w/index.php',
  213. 'wgResourceBasePath' => '/w',
  214. ] );
  215. $target = Title::newFromText( "testUpdateRedirect_target" );
  216. $content = new JavaScriptContent( $oldText );
  217. $newContent = $content->updateRedirect( $target );
  218. $this->assertEquals( $expectedText, $newContent->getNativeData() );
  219. }
  220. public static function provideUpdateRedirect() {
  221. // phpcs:disable Generic.Files.LineLength
  222. return [
  223. [
  224. '#REDIRECT [[Someplace]]',
  225. '#REDIRECT [[Someplace]]',
  226. ],
  227. [
  228. '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");',
  229. '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target\u0026action=raw\u0026ctype=text/javascript");'
  230. ]
  231. ];
  232. // phpcs:enable
  233. }
  234. /**
  235. * @covers JavaScriptContent::getModel
  236. */
  237. public function testGetModel() {
  238. $content = $this->newContent( "hello world." );
  239. $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModel() );
  240. }
  241. /**
  242. * @covers JavaScriptContent::getContentHandler
  243. */
  244. public function testGetContentHandler() {
  245. $content = $this->newContent( "hello world." );
  246. $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelID() );
  247. }
  248. public static function dataEquals() {
  249. return [
  250. [ new JavaScriptContent( "hallo" ), null, false ],
  251. [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ],
  252. [ new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ],
  253. [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ],
  254. ];
  255. }
  256. /**
  257. * @covers JavaScriptContent::getRedirectTarget
  258. * @dataProvider provideGetRedirectTarget
  259. */
  260. public function testGetRedirectTarget( $title, $text ) {
  261. $this->setMwGlobals( [
  262. 'wgServer' => '//example.org',
  263. 'wgScriptPath' => '/w',
  264. 'wgScript' => '/w/index.php',
  265. 'wgResourceBasePath' => '/w',
  266. ] );
  267. $content = new JavaScriptContent( $text );
  268. $target = $content->getRedirectTarget();
  269. $this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
  270. }
  271. /**
  272. * Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
  273. */
  274. public static function provideGetRedirectTarget() {
  275. // phpcs:disable Generic.Files.LineLength
  276. return [
  277. [
  278. 'MediaWiki:MonoBook.js',
  279. '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");'
  280. ],
  281. [
  282. 'User:FooBar/common.js',
  283. '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");'
  284. ],
  285. [
  286. 'Gadget:FooBaz.js',
  287. '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");'
  288. ],
  289. // No #REDIRECT comment
  290. [
  291. null,
  292. 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");'
  293. ],
  294. // Different domain
  295. [
  296. null,
  297. '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");'
  298. ],
  299. ];
  300. // phpcs:enable
  301. }
  302. }