WikitextContentHandlerTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. use MediaWiki\MediaWikiServices;
  3. use MediaWiki\Revision\SlotRenderingProvider;
  4. /**
  5. * @group ContentHandler
  6. */
  7. class WikitextContentHandlerTest extends MediaWikiLangTestCase {
  8. /**
  9. * @var ContentHandler
  10. */
  11. private $handler;
  12. protected function setUp() {
  13. parent::setUp();
  14. $this->handler = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT );
  15. }
  16. /**
  17. * @covers WikitextContentHandler::serializeContent
  18. */
  19. public function testSerializeContent() {
  20. $content = new WikitextContent( 'hello world' );
  21. $this->assertEquals( 'hello world', $this->handler->serializeContent( $content ) );
  22. $this->assertEquals(
  23. 'hello world',
  24. $this->handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT )
  25. );
  26. try {
  27. $this->handler->serializeContent( $content, 'dummy/foo' );
  28. $this->fail( "serializeContent() should have failed on unknown format" );
  29. } catch ( MWException $e ) {
  30. // ok, as expected
  31. }
  32. }
  33. /**
  34. * @covers WikitextContentHandler::unserializeContent
  35. */
  36. public function testUnserializeContent() {
  37. $content = $this->handler->unserializeContent( 'hello world' );
  38. $this->assertEquals( 'hello world', $content->getNativeData() );
  39. $content = $this->handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
  40. $this->assertEquals( 'hello world', $content->getNativeData() );
  41. try {
  42. $this->handler->unserializeContent( 'hello world', 'dummy/foo' );
  43. $this->fail( "unserializeContent() should have failed on unknown format" );
  44. } catch ( MWException $e ) {
  45. // ok, as expected
  46. }
  47. }
  48. /**
  49. * @covers WikitextContentHandler::makeEmptyContent
  50. */
  51. public function testMakeEmptyContent() {
  52. $content = $this->handler->makeEmptyContent();
  53. $this->assertTrue( $content->isEmpty() );
  54. $this->assertEquals( '', $content->getNativeData() );
  55. }
  56. public static function dataIsSupportedFormat() {
  57. return [
  58. [ null, true ],
  59. [ CONTENT_FORMAT_WIKITEXT, true ],
  60. [ 99887766, false ],
  61. ];
  62. }
  63. /**
  64. * @dataProvider provideMakeRedirectContent
  65. * @param Title|string $title Title object or string for Title::newFromText()
  66. * @param string $expected Serialized form of the content object built
  67. * @covers WikitextContentHandler::makeRedirectContent
  68. */
  69. public function testMakeRedirectContent( $title, $expected ) {
  70. MediaWikiServices::getInstance()->getContentLanguage()->resetNamespaces();
  71. MediaWikiServices::getInstance()->resetServiceForTesting( 'MagicWordFactory' );
  72. if ( is_string( $title ) ) {
  73. $title = Title::newFromText( $title );
  74. }
  75. $content = $this->handler->makeRedirectContent( $title );
  76. $this->assertEquals( $expected, $content->serialize() );
  77. }
  78. public static function provideMakeRedirectContent() {
  79. return [
  80. [ 'Hello', '#REDIRECT [[Hello]]' ],
  81. [ 'Template:Hello', '#REDIRECT [[Template:Hello]]' ],
  82. [ 'Hello#section', '#REDIRECT [[Hello#section]]' ],
  83. [ 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ],
  84. [ 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ],
  85. [ 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ],
  86. [ Title::makeTitle( NS_MAIN, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ],
  87. [ Title::makeTitle( NS_MAIN, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ],
  88. [
  89. Title::makeTitle( NS_MAIN, 'Bar', 'fragment', 'google' ),
  90. '#REDIRECT [[google:Bar#fragment]]'
  91. ],
  92. ];
  93. }
  94. /**
  95. * @dataProvider dataIsSupportedFormat
  96. * @covers WikitextContentHandler::isSupportedFormat
  97. */
  98. public function testIsSupportedFormat( $format, $supported ) {
  99. $this->assertEquals( $supported, $this->handler->isSupportedFormat( $format ) );
  100. }
  101. /**
  102. * @covers WikitextContentHandler::supportsDirectEditing
  103. */
  104. public function testSupportsDirectEditing() {
  105. $handler = new WikiTextContentHandler();
  106. $this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
  107. }
  108. public static function dataMerge3() {
  109. return [
  110. [
  111. "first paragraph
  112. second paragraph\n",
  113. "FIRST paragraph
  114. second paragraph\n",
  115. "first paragraph
  116. SECOND paragraph\n",
  117. "FIRST paragraph
  118. SECOND paragraph\n",
  119. ],
  120. [ "first paragraph
  121. second paragraph\n",
  122. "Bla bla\n",
  123. "Blubberdibla\n",
  124. false,
  125. ],
  126. ];
  127. }
  128. /**
  129. * @dataProvider dataMerge3
  130. * @covers WikitextContentHandler::merge3
  131. */
  132. public function testMerge3( $old, $mine, $yours, $expected ) {
  133. $this->markTestSkippedIfNoDiff3();
  134. // test merge
  135. $oldContent = new WikitextContent( $old );
  136. $myContent = new WikitextContent( $mine );
  137. $yourContent = new WikitextContent( $yours );
  138. $merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
  139. $this->assertEquals( $expected, $merged ? $merged->getNativeData() : $merged );
  140. }
  141. public static function dataGetAutosummary() {
  142. return [
  143. [
  144. 'Hello there, world!',
  145. '#REDIRECT [[Foo]]',
  146. 0,
  147. '/^Redirected page .*Foo/'
  148. ],
  149. [
  150. null,
  151. 'Hello world!',
  152. EDIT_NEW,
  153. '/^Created page .*Hello/'
  154. ],
  155. [
  156. null,
  157. '',
  158. EDIT_NEW,
  159. '/^Created blank page$/'
  160. ],
  161. [
  162. 'Hello there, world!',
  163. '',
  164. 0,
  165. '/^Blanked/'
  166. ],
  167. [
  168. 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
  169. eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
  170. voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
  171. clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
  172. 'Hello world!',
  173. 0,
  174. '/^Replaced .*Hello/'
  175. ],
  176. [
  177. 'foo',
  178. 'bar',
  179. 0,
  180. '/^$/'
  181. ],
  182. ];
  183. }
  184. /**
  185. * @dataProvider dataGetAutosummary
  186. * @covers WikitextContentHandler::getAutosummary
  187. */
  188. public function testGetAutosummary( $old, $new, $flags, $expected ) {
  189. $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
  190. $newContent = is_null( $new ) ? null : new WikitextContent( $new );
  191. $summary = $this->handler->getAutosummary( $oldContent, $newContent, $flags );
  192. $this->assertTrue(
  193. (bool)preg_match( $expected, $summary ),
  194. "Autosummary didn't match expected pattern $expected: $summary"
  195. );
  196. }
  197. public static function dataGetChangeTag() {
  198. return [
  199. [
  200. null,
  201. '#REDIRECT [[Foo]]',
  202. 0,
  203. 'mw-new-redirect'
  204. ],
  205. [
  206. 'Lorem ipsum dolor',
  207. '#REDIRECT [[Foo]]',
  208. 0,
  209. 'mw-new-redirect'
  210. ],
  211. [
  212. '#REDIRECT [[Foo]]',
  213. 'Lorem ipsum dolor',
  214. 0,
  215. 'mw-removed-redirect'
  216. ],
  217. [
  218. '#REDIRECT [[Foo]]',
  219. '#REDIRECT [[Bar]]',
  220. 0,
  221. 'mw-changed-redirect-target'
  222. ],
  223. [
  224. null,
  225. 'Lorem ipsum dolor',
  226. EDIT_NEW,
  227. null // mw-newpage is not defined as a tag
  228. ],
  229. [
  230. null,
  231. '',
  232. EDIT_NEW,
  233. null // mw-newblank is not defined as a tag
  234. ],
  235. [
  236. 'Lorem ipsum dolor',
  237. '',
  238. 0,
  239. 'mw-blank'
  240. ],
  241. [
  242. 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
  243. eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
  244. voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
  245. clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
  246. 'Ipsum',
  247. 0,
  248. 'mw-replace'
  249. ],
  250. [
  251. 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
  252. eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
  253. voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
  254. clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
  255. 'Duis purus odio, rhoncus et finibus dapibus, facilisis ac urna. Pellentesque
  256. arcu, tristique nec tempus nec, suscipit vel arcu. Sed non dolor nec ligula
  257. congue tempor. Quisque pellentesque finibus orci a molestie. Nam maximus, purus
  258. euismod finibus mollis, dui ante malesuada felis, dignissim rutrum diam sapien.',
  259. 0,
  260. null
  261. ],
  262. ];
  263. }
  264. /**
  265. * @dataProvider dataGetChangeTag
  266. * @covers WikitextContentHandler::getChangeTag
  267. */
  268. public function testGetChangeTag( $old, $new, $flags, $expected ) {
  269. $this->setMwGlobals( 'wgSoftwareTags', [
  270. 'mw-new-redirect' => true,
  271. 'mw-removed-redirect' => true,
  272. 'mw-changed-redirect-target' => true,
  273. 'mw-newpage' => true,
  274. 'mw-newblank' => true,
  275. 'mw-blank' => true,
  276. 'mw-replace' => true,
  277. ] );
  278. $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
  279. $newContent = is_null( $new ) ? null : new WikitextContent( $new );
  280. $tag = $this->handler->getChangeTag( $oldContent, $newContent, $flags );
  281. $this->assertSame( $expected, $tag );
  282. }
  283. /**
  284. * @covers WikitextContentHandler::getDataForSearchIndex
  285. */
  286. public function testDataIndexFieldsFile() {
  287. $mockEngine = $this->createMock( SearchEngine::class );
  288. $title = Title::newFromText( 'Somefile.jpg', NS_FILE );
  289. $page = new WikiPage( $title );
  290. $fileHandler = $this->getMockBuilder( FileContentHandler::class )
  291. ->disableOriginalConstructor()
  292. ->setMethods( [ 'getDataForSearchIndex' ] )
  293. ->getMock();
  294. $handler = $this->getMockBuilder( WikitextContentHandler::class )
  295. ->disableOriginalConstructor()
  296. ->setMethods( [ 'getFileHandler' ] )
  297. ->getMock();
  298. $handler->method( 'getFileHandler' )->will( $this->returnValue( $fileHandler ) );
  299. $fileHandler->expects( $this->once() )
  300. ->method( 'getDataForSearchIndex' )
  301. ->will( $this->returnValue( [ 'file_text' => 'This is file content' ] ) );
  302. $data = $handler->getDataForSearchIndex( $page, new ParserOutput(), $mockEngine );
  303. $this->assertArrayHasKey( 'file_text', $data );
  304. $this->assertEquals( 'This is file content', $data['file_text'] );
  305. }
  306. public function testGetSecondaryDataUpdates() {
  307. $title = Title::newFromText( 'Somefile.jpg', NS_FILE );
  308. $content = new WikitextContent( '' );
  309. /** @var SlotRenderingProvider $srp */
  310. $srp = $this->getMock( SlotRenderingProvider::class );
  311. $handler = new WikitextContentHandler();
  312. $updates = $handler->getSecondaryDataUpdates( $title, $content, 'main', $srp );
  313. $this->assertEquals( [], $updates );
  314. }
  315. public function testGetDeletionUpdates() {
  316. $title = Title::newFromText( 'Somefile.jpg', NS_FILE );
  317. $content = new WikitextContent( '' );
  318. $srp = $this->getMock( SlotRenderingProvider::class );
  319. $handler = new WikitextContentHandler();
  320. $updates = $handler->getDeletionUpdates( $title, 'main' );
  321. $this->assertEquals( [], $updates );
  322. }
  323. }