d07a9e1400044db328fb1a7a40b7b848c9a209c0
[lhc/web/wiklou.git] / tests / phpunit / includes / page / ArticleViewTest.php
1 <?php
2 use MediaWiki\MediaWikiServices;
3 use MediaWiki\Storage\MutableRevisionRecord;
4 use MediaWiki\Storage\RevisionRecord;
5 use MediaWiki\Storage\SlotRecord;
6 use PHPUnit\Framework\MockObject\MockObject;
7
8 /**
9 * @covers \Article::view()
10 */
11 class ArticleViewTest extends MediaWikiTestCase {
12
13 protected function setUp() {
14 parent::setUp();
15
16 $this->setUserLang( 'qqx' );
17 }
18
19 private function getHtml( OutputPage $output ) {
20 return preg_replace( '/<!--.*?-->/s', '', $output->getHTML() );
21 }
22
23 /**
24 * @param string|Title $title
25 * @param Content[]|string[] $revisionContents Content of the revisions to create
26 * (as Content or string).
27 * @param RevisionRecord[] &$revisions will be filled with the RevisionRecord for $content.
28 *
29 * @return WikiPage
30 * @throws MWException
31 */
32 private function getPage( $title, array $revisionContents = [], array &$revisions = [] ) {
33 if ( is_string( $title ) ) {
34 $title = Title::makeTitle( $this->getDefaultWikitextNS(), $title );
35 }
36
37 $page = WikiPage::factory( $title );
38
39 $user = $this->getTestUser()->getUser();
40
41 foreach ( $revisionContents as $key => $cont ) {
42 if ( is_string( $cont ) ) {
43 $cont = new WikitextContent( $cont );
44 }
45
46 $u = $page->newPageUpdater( $user );
47 $u->setContent( SlotRecord::MAIN, $cont );
48 $rev = $u->saveRevision( CommentStoreComment::newUnsavedComment( 'Rev ' . $key ) );
49
50 $revisions[ $key ] = $rev;
51 }
52
53 return $page;
54 }
55
56 /**
57 * @covers Article::getOldId()
58 * @covers Article::getRevIdFetched()
59 */
60 public function testGetOldId() {
61 $revisions = [];
62 $page = $this->getPage( __METHOD__, [ 1 => 'Test A', 2 => 'Test B' ], $revisions );
63
64 $idA = $revisions[1]->getId();
65 $idB = $revisions[2]->getId();
66
67 // oldid in constructor
68 $article = new Article( $page->getTitle(), $idA );
69 $this->assertSame( $idA, $article->getOldID() );
70 $article->getRevisionFetched();
71 $this->assertSame( $idA, $article->getRevIdFetched() );
72
73 // oldid 0 in constructor
74 $article = new Article( $page->getTitle(), 0 );
75 $this->assertSame( 0, $article->getOldID() );
76 $article->getRevisionFetched();
77 $this->assertSame( $idB, $article->getRevIdFetched() );
78
79 // oldid in request
80 $article = new Article( $page->getTitle() );
81 $context = new RequestContext();
82 $context->setRequest( new FauxRequest( [ 'oldid' => $idA ] ) );
83 $article->setContext( $context );
84 $this->assertSame( $idA, $article->getOldID() );
85 $article->getRevisionFetched();
86 $this->assertSame( $idA, $article->getRevIdFetched() );
87
88 // no oldid
89 $article = new Article( $page->getTitle() );
90 $context = new RequestContext();
91 $context->setRequest( new FauxRequest( [] ) );
92 $article->setContext( $context );
93 $this->assertSame( 0, $article->getOldID() );
94 $article->getRevisionFetched();
95 $this->assertSame( $idB, $article->getRevIdFetched() );
96 }
97
98 public function testView() {
99 $page = $this->getPage( __METHOD__, [ 1 => 'Test A', 2 => 'Test B' ] );
100
101 $article = new Article( $page->getTitle(), 0 );
102 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
103 $article->view();
104
105 $output = $article->getContext()->getOutput();
106 $this->assertContains( 'Test B', $this->getHtml( $output ) );
107 $this->assertNotContains( 'id="mw-revision-info"', $this->getHtml( $output ) );
108 $this->assertNotContains( 'id="mw-revision-nav"', $this->getHtml( $output ) );
109 }
110
111 public function testViewCached() {
112 $page = $this->getPage( __METHOD__, [ 1 => 'Test A', 2 => 'Test B' ] );
113
114 $po = new ParserOutput( 'Cached Text' );
115
116 $article = new Article( $page->getTitle(), 0 );
117 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
118
119 $cache = MediaWikiServices::getInstance()->getParserCache();
120 $cache->save( $po, $page, $article->getParserOptions() );
121
122 $article->view();
123
124 $output = $article->getContext()->getOutput();
125 $this->assertContains( 'Cached Text', $this->getHtml( $output ) );
126 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
127 $this->assertNotContains( 'Test B', $this->getHtml( $output ) );
128 }
129
130 /**
131 * @covers Article::getRedirectTarget()
132 */
133 public function testViewRedirect() {
134 $target = Title::makeTitle( $this->getDefaultWikitextNS(), 'Test_Target' );
135 $redirectText = '#REDIRECT [[' . $target->getPrefixedText() . ']]';
136
137 $page = $this->getPage( __METHOD__, [ $redirectText ] );
138
139 $article = new Article( $page->getTitle(), 0 );
140 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
141 $article->view();
142
143 $this->assertNotNull(
144 $article->getRedirectTarget()->getPrefixedDBkey()
145 );
146 $this->assertSame(
147 $target->getPrefixedDBkey(),
148 $article->getRedirectTarget()->getPrefixedDBkey()
149 );
150
151 $output = $article->getContext()->getOutput();
152 $this->assertContains( 'class="redirectText"', $this->getHtml( $output ) );
153 $this->assertContains(
154 '>' . htmlspecialchars( $target->getPrefixedText() ) . '<',
155 $this->getHtml( $output )
156 );
157 }
158
159 public function testViewNonText() {
160 $dummy = $this->getPage( __METHOD__, [ 'Dummy' ] );
161 $dummyRev = $dummy->getRevision()->getRevisionRecord();
162 $title = $dummy->getTitle();
163
164 /** @var MockObject|ContentHandler $mockHandler */
165 $mockHandler = $this->getMockBuilder( ContentHandler::class )
166 ->setMethods(
167 [
168 'isParserCacheSupported',
169 'serializeContent',
170 'unserializeContent',
171 'makeEmptyContent',
172 ]
173 )
174 ->setConstructorArgs( [ 'NotText', [ 'application/frobnitz' ] ] )
175 ->getMock();
176
177 $mockHandler->method( 'isParserCacheSupported' )
178 ->willReturn( false );
179
180 $this->setTemporaryHook(
181 'ContentHandlerForModelID',
182 function ( $id, &$handler ) use ( $mockHandler ) {
183 $handler = $mockHandler;
184 }
185 );
186
187 /** @var MockObject|Content $content */
188 $content = $this->getMock( Content::class );
189 $content->method( 'getParserOutput' )
190 ->willReturn( new ParserOutput( 'Structured Output' ) );
191 $content->method( 'getModel' )
192 ->willReturn( 'NotText' );
193 $content->method( 'getNativeData' )
194 ->willReturn( [ (object)[ 'x' => 'stuff' ] ] );
195 $content->method( 'copy' )
196 ->willReturn( $content );
197
198 $rev = new MutableRevisionRecord( $title );
199 $rev->setId( $dummyRev->getId() );
200 $rev->setPageId( $title->getArticleID() );
201 $rev->setUser( $dummyRev->getUser() );
202 $rev->setComment( $dummyRev->getComment() );
203 $rev->setTimestamp( $dummyRev->getTimestamp() );
204
205 $rev->setContent( SlotRecord::MAIN, $content );
206
207 $rev = new Revision( $rev );
208
209 /** @var MockObject|WikiPage $page */
210 $page = $this->getMockBuilder( WikiPage::class )
211 ->setMethods( [ 'getRevision', 'getLatest' ] )
212 ->setConstructorArgs( [ $title ] )
213 ->getMock();
214
215 $page->method( 'getRevision' )
216 ->willReturn( $rev );
217 $page->method( 'getLatest' )
218 ->willReturn( $rev->getId() );
219
220 $article = Article::newFromWikiPage( $page, RequestContext::getMain() );
221 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
222 $article->view();
223
224 $output = $article->getContext()->getOutput();
225 $this->assertContains( 'Structured Output', $this->getHtml( $output ) );
226 $this->assertNotContains( 'Dummy', $this->getHtml( $output ) );
227 }
228
229 public function testViewOfOldRevision() {
230 $revisions = [];
231 $page = $this->getPage( __METHOD__, [ 1 => 'Test A', 2 => 'Test B' ], $revisions );
232 $idA = $revisions[1]->getId();
233
234 $article = new Article( $page->getTitle(), $idA );
235 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
236 $article->view();
237
238 $output = $article->getContext()->getOutput();
239 $this->assertContains( 'Test A', $this->getHtml( $output ) );
240 $this->assertContains( 'id="mw-revision-info"', $output->getSubtitle() );
241 $this->assertContains( 'id="mw-revision-nav"', $output->getSubtitle() );
242
243 $this->assertNotContains( 'id="revision-info-current"', $output->getSubtitle() );
244 $this->assertNotContains( 'Test B', $this->getHtml( $output ) );
245 }
246
247 public function testViewOfCurrentRevision() {
248 $revisions = [];
249 $page = $this->getPage( __METHOD__, [ 1 => 'Test A', 2 => 'Test B' ], $revisions );
250 $idB = $revisions[2]->getId();
251
252 $article = new Article( $page->getTitle(), $idB );
253 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
254 $article->view();
255
256 $output = $article->getContext()->getOutput();
257 $this->assertContains( 'Test B', $this->getHtml( $output ) );
258 $this->assertContains( 'id="mw-revision-info-current"', $output->getSubtitle() );
259 $this->assertContains( 'id="mw-revision-nav"', $output->getSubtitle() );
260 }
261
262 public function testViewOfMissingRevision() {
263 $revisions = [];
264 $page = $this->getPage( __METHOD__, [ 1 => 'Test A' ], $revisions );
265 $badId = $revisions[1]->getId() + 100;
266
267 $article = new Article( $page->getTitle(), $badId );
268 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
269 $article->view();
270
271 $output = $article->getContext()->getOutput();
272 $this->assertContains( 'missing-revision: ' . $badId, $this->getHtml( $output ) );
273
274 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
275 }
276
277 public function testViewOfDeletedRevision() {
278 $revisions = [];
279 $page = $this->getPage( __METHOD__, [ 1 => 'Test A', 2 => 'Test B' ], $revisions );
280 $idA = $revisions[1]->getId();
281
282 $revDelList = new RevDelRevisionList(
283 RequestContext::getMain(), $page->getTitle(), [ $idA ]
284 );
285 $revDelList->setVisibility( [
286 'value' => [ RevisionRecord::DELETED_TEXT => 1 ],
287 'comment' => "Testing",
288 ] );
289
290 $article = new Article( $page->getTitle(), $idA );
291 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
292 $article->view();
293
294 $output = $article->getContext()->getOutput();
295 $this->assertContains( '(rev-deleted-text-permission)', $this->getHtml( $output ) );
296
297 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
298 $this->assertNotContains( 'Test B', $this->getHtml( $output ) );
299 }
300
301 public function testViewMissingPage() {
302 $page = $this->getPage( __METHOD__ );
303
304 $article = new Article( $page->getTitle() );
305 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
306 $article->view();
307
308 $output = $article->getContext()->getOutput();
309 $this->assertContains( '(noarticletextanon)', $this->getHtml( $output ) );
310 }
311
312 public function testViewDeletedPage() {
313 $page = $this->getPage( __METHOD__, [ 1 => 'Test A', 2 => 'Test B' ] );
314 $page->doDeleteArticle( 'Test' );
315
316 $article = new Article( $page->getTitle() );
317 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
318 $article->view();
319
320 $output = $article->getContext()->getOutput();
321 $this->assertContains( 'moveddeleted', $this->getHtml( $output ) );
322 $this->assertContains( 'logentry-delete-delete', $this->getHtml( $output ) );
323 $this->assertContains( '(noarticletextanon)', $this->getHtml( $output ) );
324
325 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
326 $this->assertNotContains( 'Test B', $this->getHtml( $output ) );
327 }
328
329 public function testViewMessagePage() {
330 $title = Title::makeTitle( NS_MEDIAWIKI, 'Mainpage' );
331 $page = $this->getPage( $title );
332
333 $article = new Article( $page->getTitle() );
334 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
335 $article->view();
336
337 $output = $article->getContext()->getOutput();
338 $this->assertContains(
339 wfMessage( 'mainpage' )->inContentLanguage()->parse(),
340 $this->getHtml( $output )
341 );
342 $this->assertNotContains( '(noarticletextanon)', $this->getHtml( $output ) );
343 }
344
345 public function testViewMissingUserPage() {
346 $user = $this->getTestUser()->getUser();
347 $user->addToDatabase();
348
349 $title = Title::makeTitle( NS_USER, $user->getName() );
350
351 $page = $this->getPage( $title );
352
353 $article = new Article( $page->getTitle() );
354 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
355 $article->view();
356
357 $output = $article->getContext()->getOutput();
358 $this->assertContains( '(noarticletextanon)', $this->getHtml( $output ) );
359 $this->assertNotContains( '(userpage-userdoesnotexist-view)', $this->getHtml( $output ) );
360 }
361
362 public function testViewUserPageOfNonexistingUser() {
363 $user = User::newFromName( 'Testing ' . __METHOD__ );
364
365 $title = Title::makeTitle( NS_USER, $user->getName() );
366
367 $page = $this->getPage( $title );
368
369 $article = new Article( $page->getTitle() );
370 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
371 $article->view();
372
373 $output = $article->getContext()->getOutput();
374 $this->assertContains( '(noarticletextanon)', $this->getHtml( $output ) );
375 $this->assertContains( '(userpage-userdoesnotexist-view:', $this->getHtml( $output ) );
376 }
377
378 public function testArticleViewHeaderHook() {
379 $page = $this->getPage( __METHOD__, [ 1 => 'Test A' ] );
380
381 $article = new Article( $page->getTitle(), 0 );
382 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
383
384 $this->setTemporaryHook(
385 'ArticleViewHeader',
386 function ( Article $articlePage, &$outputDone, &$useParserCache ) use ( $article ) {
387 $this->assertSame( $article, $articlePage, '$articlePage' );
388
389 $outputDone = new ParserOutput( 'Hook Text' );
390 $outputDone->setTitleText( 'Hook Title' );
391
392 $articlePage->getContext()->getOutput()->addParserOutput( $outputDone );
393 }
394 );
395
396 $article->view();
397
398 $output = $article->getContext()->getOutput();
399 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
400 $this->assertContains( 'Hook Text', $this->getHtml( $output ) );
401 $this->assertSame( 'Hook Title', $output->getPageTitle() );
402 }
403
404 public function testArticleContentViewCustomHook() {
405 $page = $this->getPage( __METHOD__, [ 1 => 'Test A' ] );
406
407 $article = new Article( $page->getTitle(), 0 );
408 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
409
410 // use ArticleViewHeader hook to bypass the parser cache
411 $this->setTemporaryHook(
412 'ArticleViewHeader',
413 function ( Article $articlePage, &$outputDone, &$useParserCache ) use ( $article ) {
414 $useParserCache = false;
415 }
416 );
417
418 $this->setTemporaryHook(
419 'ArticleContentViewCustom',
420 function ( Content $content, Title $title, OutputPage $output ) use ( $page ) {
421 $this->assertSame( $page->getTitle(), $title, '$title' );
422 $this->assertSame( 'Test A', $content->getNativeData(), '$content' );
423
424 $output->addHTML( 'Hook Text' );
425 return false;
426 }
427 );
428
429 $this->hideDeprecated(
430 'ArticleContentViewCustom hook (used in hook-ArticleContentViewCustom-closure)'
431 );
432
433 $article->view();
434
435 $output = $article->getContext()->getOutput();
436 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
437 $this->assertContains( 'Hook Text', $this->getHtml( $output ) );
438 }
439
440 public function testArticleRevisionViewCustomHook() {
441 $page = $this->getPage( __METHOD__, [ 1 => 'Test A' ] );
442
443 $article = new Article( $page->getTitle(), 0 );
444 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
445
446 // use ArticleViewHeader hook to bypass the parser cache
447 $this->setTemporaryHook(
448 'ArticleViewHeader',
449 function ( Article $articlePage, &$outputDone, &$useParserCache ) use ( $article ) {
450 $useParserCache = false;
451 }
452 );
453
454 $this->setTemporaryHook(
455 'ArticleRevisionViewCustom',
456 function ( RevisionRecord $rev, Title $title, $oldid, OutputPage $output ) use ( $page ) {
457 $content = $rev->getContent( SlotRecord::MAIN );
458
459 $this->assertSame( $page->getTitle(), $title, '$title' );
460 $this->assertSame( 'Test A', $content->getNativeData(), '$content' );
461
462 $output->addHTML( 'Hook Text' );
463 return false;
464 }
465 );
466
467 $article->view();
468
469 $output = $article->getContext()->getOutput();
470 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
471 $this->assertContains( 'Hook Text', $this->getHtml( $output ) );
472 }
473
474 public function testArticleAfterFetchContentObjectHook() {
475 $page = $this->getPage( __METHOD__, [ 1 => 'Test A' ] );
476
477 $article = new Article( $page->getTitle(), 0 );
478 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
479
480 // use ArticleViewHeader hook to bypass the parser cache
481 $this->setTemporaryHook(
482 'ArticleViewHeader',
483 function ( Article $articlePage, &$outputDone, &$useParserCache ) use ( $article ) {
484 $useParserCache = false;
485 }
486 );
487
488 $this->setTemporaryHook(
489 'ArticleAfterFetchContentObject',
490 function ( Article &$articlePage, Content &$content ) use ( $page, $article ) {
491 $this->assertSame( $article, $articlePage, '$articlePage' );
492 $this->assertSame( 'Test A', $content->getNativeData(), '$content' );
493
494 $content = new WikitextContent( 'Hook Text' );
495 }
496 );
497
498 $this->hideDeprecated(
499 'ArticleAfterFetchContentObject hook'
500 . ' (used in hook-ArticleAfterFetchContentObject-closure)'
501 );
502
503 $article->view();
504
505 $output = $article->getContext()->getOutput();
506 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
507 $this->assertContains( 'Hook Text', $this->getHtml( $output ) );
508 }
509
510 public function testShowMissingArticleHook() {
511 $page = $this->getPage( __METHOD__ );
512
513 $article = new Article( $page->getTitle() );
514 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
515
516 $this->setTemporaryHook(
517 'ShowMissingArticle',
518 function ( Article $articlePage ) use ( $article ) {
519 $this->assertSame( $article, $articlePage, '$articlePage' );
520
521 $articlePage->getContext()->getOutput()->addHTML( 'Hook Text' );
522 }
523 );
524
525 $article->view();
526
527 $output = $article->getContext()->getOutput();
528 $this->assertContains( '(noarticletextanon)', $this->getHtml( $output ) );
529 $this->assertContains( 'Hook Text', $this->getHtml( $output ) );
530 }
531
532 }