Debian Codegraph @

tests/phpunit/includes/content/WikitextContentHandlerTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Content\FileContentHandler;
4 use MediaWiki\Content\WikitextContent;
5 use MediaWiki\Content\WikitextContentHandler;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Page\PageReferenceValue;
8 use MediaWiki\Parser\ParserOptions;
9 use MediaWiki\Parser\ParserOutput;
10 use MediaWiki\Parser\ParserOutputFlags;
11 use MediaWiki\Title\Title;
12 use MediaWiki\Title\TitleFactory;
13 use MediaWiki\User\UserIdentity;
14 use Wikimedia\Rdbms\IDBAccessObject;
15 use Wikimedia\TestingAccessWrapper;
16
17 /**
18 * See also unit tests at \MediaWiki\Tests\Unit\WikitextContentHandlerTest
19 *
20 * @group ContentHandler
21 * @covers \MediaWiki\Content\WikitextContentHandler
22 * @covers \MediaWiki\Content\TextContentHandler
23 * @covers \MediaWiki\Content\ContentHandler
24 */
25 class WikitextContentHandlerTest extends MediaWikiLangTestCase {
26 private WikitextContentHandler $handler;
27
28 protected function setUp(): void {
29 parent::setUp();
30
31 $this->handler = $this->getServiceContainer()->getContentHandlerFactory()
32 ->getContentHandler( CONTENT_MODEL_WIKITEXT );
33 }
34
35 public static function dataMerge3() {
36 return [
37 [
38 "first paragraph
39
40 second paragraph\n",
41
42 "FIRST paragraph
43
44 second paragraph\n",
45
46 "first paragraph
47
48 SECOND paragraph\n",
49
50 "FIRST paragraph
51
52 SECOND paragraph\n",
53 ],
54
55 [ "first paragraph
56 second paragraph\n",
57
58 "Bla bla\n",
59
60 "Blubberdibla\n",
61
62 false,
63 ],
64 ];
65 }
66
67 /**
68 * @dataProvider dataMerge3
69 */
70 public function testMerge3( $old, $mine, $yours, $expected ) {
71 $this->markTestSkippedIfNoDiff3();
72
73 // test merge
74 $oldContent = new WikitextContent( $old );
75 $myContent = new WikitextContent( $mine );
76 $yourContent = new WikitextContent( $yours );
77
78 $merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
79
80 $this->assertEquals( $expected, $merged ? $merged->getText() : $merged );
81 }
82
83 public static function dataGetAutosummary() {
84 return [
85 [
86 'Hello there, world!',
87 '#REDIRECT [[Foo]]',
88 0,
89 '/^Redirected page .*Foo/'
90 ],
91
92 [
93 null,
94 'Hello world!',
95 EDIT_NEW,
96 '/^Created page .*Hello/'
97 ],
98
99 [
100 null,
101 '',
102 EDIT_NEW,
103 '/^Created blank page$/'
104 ],
105
106 [
107 'Hello there, world!',
108 '',
109 0,
110 '/^Blanked/'
111 ],
112
113 [
114 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
115 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
116 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
117 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
118 'Hello world!',
119 0,
120 '/^Replaced .*Hello/'
121 ],
122
123 [
124 'foo',
125 'bar',
126 0,
127 '/^$/'
128 ],
129 ];
130 }
131
132 /**
133 * @dataProvider dataGetAutosummary
134 */
135 public function testGetAutosummary( $old, $new, $flags, $expected ) {
136 $oldContent = $old === null ? null : new WikitextContent( $old );
137 $newContent = $new === null ? null : new WikitextContent( $new );
138
139 $summary = $this->handler->getAutosummary( $oldContent, $newContent, $flags );
140
141 $this->assertTrue(
142 (bool)preg_match( $expected, $summary ),
143 "Autosummary didn't match expected pattern $expected: $summary"
144 );
145 }
146
147 public static function dataGetChangeTag() {
148 return [
149 [
150 null,
151 '#REDIRECT [[Foo]]',
152 0,
153 'mw-new-redirect'
154 ],
155
156 [
157 'Lorem ipsum dolor',
158 '#REDIRECT [[Foo]]',
159 0,
160 'mw-new-redirect'
161 ],
162
163 [
164 '#REDIRECT [[Foo]]',
165 'Lorem ipsum dolor',
166 0,
167 'mw-removed-redirect'
168 ],
169
170 [
171 '#REDIRECT [[Foo]]',
172 '#REDIRECT [[Bar]]',
173 0,
174 'mw-changed-redirect-target'
175 ],
176
177 [
178 null,
179 'Lorem ipsum dolor',
180 EDIT_NEW,
181 null // mw-newpage is not defined as a tag
182 ],
183
184 [
185 null,
186 '',
187 EDIT_NEW,
188 null // mw-newblank is not defined as a tag
189 ],
190
191 [
192 'Lorem ipsum dolor',
193 '',
194 0,
195 'mw-blank'
196 ],
197
198 [
199 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
200 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
201 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
202 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
203 'Ipsum',
204 0,
205 'mw-replace'
206 ],
207
208 [
209 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
210 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
211 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
212 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
213 'Duis purus odio, rhoncus et finibus dapibus, facilisis ac urna. Pellentesque
214 arcu, tristique nec tempus nec, suscipit vel arcu. Sed non dolor nec ligula
215 congue tempor. Quisque pellentesque finibus orci a molestie. Nam maximus, purus
216 euismod finibus mollis, dui ante malesuada felis, dignissim rutrum diam sapien.',
217 0,
218 null
219 ],
220 ];
221 }
222
223 /**
224 * @dataProvider dataGetChangeTag
225 */
226 public function testGetChangeTag( $old, $new, $flags, $expected ) {
227 $this->overrideConfigValue( MainConfigNames::SoftwareTags, [
228 'mw-new-redirect' => true,
229 'mw-removed-redirect' => true,
230 'mw-changed-redirect-target' => true,
231 'mw-newpage' => true,
232 'mw-newblank' => true,
233 'mw-blank' => true,
234 'mw-replace' => true,
235 ] );
236 $oldContent = $old === null ? null : new WikitextContent( $old );
237 $newContent = $new === null ? null : new WikitextContent( $new );
238
239 $tag = $this->handler->getChangeTag( $oldContent, $newContent, $flags );
240
241 $this->assertSame( $expected, $tag );
242 }
243
244 public function testGetFieldsForSearchIndex() {
245 $searchEngine = $this->createMock( SearchEngine::class );
246
247 $searchEngine->method( 'makeSearchFieldMapping' )
248 ->willReturnCallback( static function ( $name, $type ) {
249 return new DummySearchIndexFieldDefinition( $name, $type );
250 } );
251
252 $this->hideDeprecated( 'MediaWiki\\Content\\ContentHandler::getForModelID' );
253
254 $fields = $this->handler->getFieldsForSearchIndex( $searchEngine );
255
256 $this->assertArrayHasKey( 'category', $fields );
257 $this->assertArrayHasKey( 'external_link', $fields );
258 $this->assertArrayHasKey( 'outgoing_link', $fields );
259 $this->assertArrayHasKey( 'template', $fields );
260 $this->assertArrayHasKey( 'content_model', $fields );
261 }
262
263 public function testDataIndexFieldsFile() {
264 $mockEngine = $this->createMock( SearchEngine::class );
265 $title = Title::makeTitle( NS_FILE, 'Somefile.jpg' );
266 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
267 // Mark the page as not existent to avoid DB queries.
268 $pageWrapper = TestingAccessWrapper::newFromObject( $page );
269 $pageWrapper->mLatest = null;
270 $pageWrapper->mId = 0;
271 $pageWrapper->mDataLoadedFrom = IDBAccessObject::READ_NORMAL;
272
273 $fileHandler = $this->getMockBuilder( FileContentHandler::class )
274 ->disableOriginalConstructor()
275 ->onlyMethods( [ 'getDataForSearchIndex' ] )
276 ->getMock();
277
278 $handler = $this->getMockBuilder( WikitextContentHandler::class )
279 ->disableOriginalConstructor()
280 ->onlyMethods( [ 'getFileHandler' ] )
281 ->getMock();
282
283 $handler->method( 'getFileHandler' )->willReturn( $fileHandler );
284 $fileHandler->expects( $this->once() )
285 ->method( 'getDataForSearchIndex' )
286 ->willReturn( [ 'file_text' => 'This is file content' ] );
287
288 $data = $handler->getDataForSearchIndex( $page, new ParserOutput( '' ), $mockEngine );
289 $this->assertArrayHasKey( 'file_text', $data );
290 $this->assertEquals( 'This is file content', $data['file_text'] );
291 }
292
293 public function testHadSignature() {
294 $services = $this->getServiceContainer();
295
296 $pageObj = PageReferenceValue::localReference( NS_MAIN, __CLASS__ );
297 // Force a content model in the converted Title to avoid DB queries.
298 $title = $services->getTitleFactory()->newFromPageReference( $pageObj );
299 $title->setContentModel( CONTENT_MODEL_WIKITEXT );
300 $titleFactory = $this->createMock( TitleFactory::class );
301 $titleFactory->method( 'newFromPageReference' )
302 ->with( $pageObj )
303 ->willReturn( $title );
304 $this->setService( 'TitleFactory', $titleFactory );
305
306 $contentTransformer = $services->getContentTransformer();
307 $contentRenderer = $services->getContentRenderer();
308 $this->hideDeprecated( 'AbstractContent::preSaveTransform' );
309
310 $content = new WikitextContent( '~~~~' );
311 $pstContent = $contentTransformer->preSaveTransform(
312 $content,
313 $pageObj,
314 $this->createMock( UserIdentity::class ),
315 ParserOptions::newFromAnon()
316 );
317
318 $this->assertTrue( $contentRenderer->getParserOutput( $pstContent, $pageObj )->getOutputFlag(
319 ParserOutputFlags::USER_SIGNATURE
320 ) );
321 }
322 }