TextContentTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 TextContentTest extends MediaWikiLangTestCase {
  9. protected $context;
  10. protected function setUp() {
  11. parent::setUp();
  12. // trigger purging of all page related tables
  13. $this->tablesUsed[] = 'page';
  14. $this->tablesUsed[] = 'revision';
  15. // Anon user
  16. $user = new User();
  17. $user->setName( '127.0.0.1' );
  18. $this->context = new RequestContext( new FauxRequest() );
  19. $this->context->setTitle( Title::newFromText( 'Test' ) );
  20. $this->context->setUser( $user );
  21. $this->setMwGlobals( [
  22. 'wgUser' => $user,
  23. 'wgTextModelsToParse' => [
  24. CONTENT_MODEL_WIKITEXT,
  25. CONTENT_MODEL_CSS,
  26. CONTENT_MODEL_JAVASCRIPT,
  27. ],
  28. 'wgTidyConfig' => [ 'driver' => 'disabled' ],
  29. 'wgCapitalLinks' => true,
  30. 'wgHooks' => [], // bypass hook ContentGetParserOutput that force custom rendering
  31. ] );
  32. MWTidy::destroySingleton();
  33. }
  34. protected function tearDown() {
  35. MWTidy::destroySingleton();
  36. parent::tearDown();
  37. }
  38. public function newContent( $text ) {
  39. return new TextContent( $text );
  40. }
  41. public static function dataGetParserOutput() {
  42. return [
  43. [
  44. 'TextContentTest_testGetParserOutput',
  45. CONTENT_MODEL_TEXT,
  46. "hello ''world'' & [[stuff]]\n", "hello ''world'' &amp; [[stuff]]",
  47. [
  48. 'Links' => []
  49. ]
  50. ],
  51. // TODO: more...?
  52. ];
  53. }
  54. /**
  55. * @dataProvider dataGetParserOutput
  56. * @covers TextContent::getParserOutput
  57. */
  58. public function testGetParserOutput( $title, $model, $text, $expectedHtml,
  59. $expectedFields = null
  60. ) {
  61. $title = Title::newFromText( $title );
  62. $content = ContentHandler::makeContent( $text, $title, $model );
  63. $po = $content->getParserOutput( $title );
  64. $html = $po->getText();
  65. $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
  66. $this->assertEquals( $expectedHtml, trim( $html ) );
  67. if ( $expectedFields ) {
  68. foreach ( $expectedFields as $field => $exp ) {
  69. $f = 'get' . ucfirst( $field );
  70. $v = call_user_func( [ $po, $f ] );
  71. if ( is_array( $exp ) ) {
  72. $this->assertArrayEquals( $exp, $v );
  73. } else {
  74. $this->assertEquals( $exp, $v );
  75. }
  76. }
  77. }
  78. // TODO: assert more properties
  79. }
  80. public static function dataPreSaveTransform() {
  81. return [
  82. [
  83. # 0: no signature resolution
  84. 'hello this is ~~~',
  85. 'hello this is ~~~',
  86. ],
  87. [
  88. # 1: rtrim
  89. " Foo \n ",
  90. ' Foo',
  91. ],
  92. [
  93. # 2: newline normalization
  94. "LF\n\nCRLF\r\n\r\nCR\r\rEND",
  95. "LF\n\nCRLF\n\nCR\n\nEND",
  96. ],
  97. ];
  98. }
  99. /**
  100. * @dataProvider dataPreSaveTransform
  101. * @covers TextContent::preSaveTransform
  102. */
  103. public function testPreSaveTransform( $text, $expected ) {
  104. $options = ParserOptions::newFromUserAndLang( $this->context->getUser(),
  105. MediaWikiServices::getInstance()->getContentLanguage() );
  106. $content = $this->newContent( $text );
  107. $content = $content->preSaveTransform(
  108. $this->context->getTitle(),
  109. $this->context->getUser(),
  110. $options
  111. );
  112. $this->assertEquals( $expected, $content->getNativeData() );
  113. }
  114. public static function dataPreloadTransform() {
  115. return [
  116. [
  117. 'hello this is ~~~',
  118. 'hello this is ~~~',
  119. ],
  120. ];
  121. }
  122. /**
  123. * @dataProvider dataPreloadTransform
  124. * @covers TextContent::preloadTransform
  125. */
  126. public function testPreloadTransform( $text, $expected ) {
  127. $options = ParserOptions::newFromUserAndLang( $this->context->getUser(),
  128. MediaWikiServices::getInstance()->getContentLanguage() );
  129. $content = $this->newContent( $text );
  130. $content = $content->preloadTransform( $this->context->getTitle(), $options );
  131. $this->assertEquals( $expected, $content->getNativeData() );
  132. }
  133. public static function dataGetRedirectTarget() {
  134. return [
  135. [ '#REDIRECT [[Test]]',
  136. null,
  137. ],
  138. ];
  139. }
  140. /**
  141. * @dataProvider dataGetRedirectTarget
  142. * @covers TextContent::getRedirectTarget
  143. */
  144. public function testGetRedirectTarget( $text, $expected ) {
  145. $content = $this->newContent( $text );
  146. $t = $content->getRedirectTarget();
  147. if ( is_null( $expected ) ) {
  148. $this->assertNull( $t, "text should not have generated a redirect target: $text" );
  149. } else {
  150. $this->assertEquals( $expected, $t->getPrefixedText() );
  151. }
  152. }
  153. /**
  154. * @dataProvider dataGetRedirectTarget
  155. * @covers TextContent::isRedirect
  156. */
  157. public function testIsRedirect( $text, $expected ) {
  158. $content = $this->newContent( $text );
  159. $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
  160. }
  161. public static function dataIsCountable() {
  162. return [
  163. [ '',
  164. null,
  165. 'any',
  166. true
  167. ],
  168. [ 'Foo',
  169. null,
  170. 'any',
  171. true
  172. ],
  173. ];
  174. }
  175. /**
  176. * @dataProvider dataIsCountable
  177. * @covers TextContent::isCountable
  178. */
  179. public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
  180. $this->setMwGlobals( 'wgArticleCountMethod', $mode );
  181. $content = $this->newContent( $text );
  182. $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
  183. $this->assertEquals(
  184. $expected,
  185. $v,
  186. 'isCountable() returned unexpected value ' . var_export( $v, true )
  187. . ' instead of ' . var_export( $expected, true )
  188. . " in mode `$mode` for text \"$text\""
  189. );
  190. }
  191. public static function dataGetTextForSummary() {
  192. return [
  193. [ "hello\nworld.",
  194. 16,
  195. 'hello world.',
  196. ],
  197. [ 'hello world.',
  198. 8,
  199. 'hello...',
  200. ],
  201. [ '[[hello world]].',
  202. 8,
  203. '[[hel...',
  204. ],
  205. ];
  206. }
  207. /**
  208. * @dataProvider dataGetTextForSummary
  209. * @covers TextContent::getTextForSummary
  210. */
  211. public function testGetTextForSummary( $text, $maxlength, $expected ) {
  212. $content = $this->newContent( $text );
  213. $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
  214. }
  215. /**
  216. * @covers TextContent::getTextForSearchIndex
  217. */
  218. public function testGetTextForSearchIndex() {
  219. $content = $this->newContent( 'hello world.' );
  220. $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
  221. }
  222. /**
  223. * @covers TextContent::copy
  224. */
  225. public function testCopy() {
  226. $content = $this->newContent( 'hello world.' );
  227. $copy = $content->copy();
  228. $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
  229. $this->assertEquals( 'hello world.', $copy->getNativeData() );
  230. }
  231. /**
  232. * @covers TextContent::getSize
  233. */
  234. public function testGetSize() {
  235. $content = $this->newContent( 'hello world.' );
  236. $this->assertEquals( 12, $content->getSize() );
  237. }
  238. /**
  239. * @covers TextContent::getNativeData
  240. */
  241. public function testGetNativeData() {
  242. $content = $this->newContent( 'hello world.' );
  243. $this->assertEquals( 'hello world.', $content->getNativeData() );
  244. }
  245. /**
  246. * @covers TextContent::getWikitextForTransclusion
  247. */
  248. public function testGetWikitextForTransclusion() {
  249. $content = $this->newContent( 'hello world.' );
  250. $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
  251. }
  252. /**
  253. * @covers TextContent::getModel
  254. */
  255. public function testGetModel() {
  256. $content = $this->newContent( "hello world." );
  257. $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
  258. }
  259. /**
  260. * @covers TextContent::getContentHandler
  261. */
  262. public function testGetContentHandler() {
  263. $content = $this->newContent( "hello world." );
  264. $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
  265. }
  266. public static function dataIsEmpty() {
  267. return [
  268. [ '', true ],
  269. [ ' ', false ],
  270. [ '0', false ],
  271. [ 'hallo welt.', false ],
  272. ];
  273. }
  274. /**
  275. * @dataProvider dataIsEmpty
  276. * @covers TextContent::isEmpty
  277. */
  278. public function testIsEmpty( $text, $empty ) {
  279. $content = $this->newContent( $text );
  280. $this->assertEquals( $empty, $content->isEmpty() );
  281. }
  282. public static function dataEquals() {
  283. return [
  284. [ new TextContent( "hallo" ), null, false ],
  285. [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
  286. [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
  287. [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
  288. [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
  289. ];
  290. }
  291. /**
  292. * @dataProvider dataEquals
  293. * @covers TextContent::equals
  294. */
  295. public function testEquals( Content $a, Content $b = null, $equal = false ) {
  296. $this->assertEquals( $equal, $a->equals( $b ) );
  297. }
  298. public static function dataGetDeletionUpdates() {
  299. return [
  300. [
  301. CONTENT_MODEL_TEXT, "hello ''world''\n",
  302. []
  303. ],
  304. [
  305. CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
  306. []
  307. ],
  308. // TODO: more...?
  309. ];
  310. }
  311. /**
  312. * @dataProvider dataGetDeletionUpdates
  313. * @covers TextContent::getDeletionUpdates
  314. */
  315. public function testDeletionUpdates( $model, $text, $expectedStuff ) {
  316. $page = $this->getNonexistingTestPage( get_class( $this ) . '-' . $this->getName() );
  317. $title = $page->getTitle();
  318. $content = ContentHandler::makeContent( $text, $title, $model );
  319. $page->doEditContent( $content, '' );
  320. $updates = $content->getDeletionUpdates( $page );
  321. // make updates accessible by class name
  322. foreach ( $updates as $update ) {
  323. $class = get_class( $update );
  324. $updates[$class] = $update;
  325. }
  326. foreach ( $expectedStuff as $class => $fieldValues ) {
  327. $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
  328. $update = $updates[$class];
  329. foreach ( $fieldValues as $field => $value ) {
  330. $v = $update->$field; # if the field doesn't exist, just crash and burn
  331. $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
  332. }
  333. }
  334. // make phpunit happy even if $expectedStuff was empty
  335. $this->assertTrue( true );
  336. }
  337. public static function provideConvert() {
  338. return [
  339. [ // #0
  340. 'Hallo Welt',
  341. CONTENT_MODEL_WIKITEXT,
  342. 'lossless',
  343. 'Hallo Welt'
  344. ],
  345. [ // #1
  346. 'Hallo Welt',
  347. CONTENT_MODEL_WIKITEXT,
  348. 'lossless',
  349. 'Hallo Welt'
  350. ],
  351. [ // #1
  352. 'Hallo Welt',
  353. CONTENT_MODEL_CSS,
  354. 'lossless',
  355. 'Hallo Welt'
  356. ],
  357. [ // #1
  358. 'Hallo Welt',
  359. CONTENT_MODEL_JAVASCRIPT,
  360. 'lossless',
  361. 'Hallo Welt'
  362. ],
  363. ];
  364. }
  365. /**
  366. * @dataProvider provideConvert
  367. * @covers TextContent::convert
  368. */
  369. public function testConvert( $text, $model, $lossy, $expectedNative ) {
  370. $content = $this->newContent( $text );
  371. $converted = $content->convert( $model, $lossy );
  372. if ( $expectedNative === false ) {
  373. $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
  374. } else {
  375. $this->assertInstanceOf( Content::class, $converted );
  376. $this->assertEquals( $expectedNative, $converted->getNativeData() );
  377. }
  378. }
  379. /**
  380. * @covers TextContent::normalizeLineEndings
  381. * @dataProvider provideNormalizeLineEndings
  382. */
  383. public function testNormalizeLineEndings( $input, $expected ) {
  384. $this->assertEquals( $expected, TextContent::normalizeLineEndings( $input ) );
  385. }
  386. public static function provideNormalizeLineEndings() {
  387. return [
  388. [
  389. "Foo\r\nbar",
  390. "Foo\nbar"
  391. ],
  392. [
  393. "Foo\rbar",
  394. "Foo\nbar"
  395. ],
  396. [
  397. "Foobar\n ",
  398. "Foobar"
  399. ]
  400. ];
  401. }
  402. }