2 use MediaWiki\MediaWikiServices
;
3 use MediaWiki\Storage\MutableRevisionRecord
;
4 use MediaWiki\Storage\RevisionRecord
;
5 use PHPUnit\Framework\MockObject\MockObject
;
8 * @covers \Article::view()
10 class ArticleViewTest
extends MediaWikiTestCase
{
12 protected function setUp() {
15 $this->setUserLang( 'qqx' );
18 private function getHtml( OutputPage
$output ) {
19 return preg_replace( '/<!--.*?-->/s', '', $output->getHTML() );
23 * @param string|Title $title
24 * @param Content[]|string[] $revisionContents Content of the revisions to create
25 * (as Content or string).
26 * @param RevisionRecord[] &$revisions will be filled with the RevisionRecord for $content.
31 private function getPage( $title, array $revisionContents = [], array &$revisions = [] ) {
32 if ( is_string( $title ) ) {
33 $title = Title
::makeTitle( $this->getDefaultWikitextNS(), $title );
36 $page = WikiPage
::factory( $title );
38 $user = $this->getTestUser()->getUser();
40 foreach ( $revisionContents as $key => $cont ) {
41 if ( is_string( $cont ) ) {
42 $cont = new WikitextContent( $cont );
45 $u = $page->newPageUpdater( $user );
46 $u->setContent( 'main', $cont );
47 $rev = $u->saveRevision( CommentStoreComment
::newUnsavedComment( 'Rev ' . $key ) );
49 $revisions[ $key ] = $rev;
56 * @covers Article::getOldId()
57 * @covers Article::getRevIdFetched()
59 public function testGetOldId() {
61 $page = $this->getPage( __METHOD__
, [ 1 => 'Test A', 2 => 'Test B' ], $revisions );
63 $idA = $revisions[1]->getId();
64 $idB = $revisions[2]->getId();
66 // oldid in constructor
67 $article = new Article( $page->getTitle(), $idA );
68 $this->assertSame( $idA, $article->getOldID() );
69 $article->getRevisionFetched();
70 $this->assertSame( $idA, $article->getRevIdFetched() );
72 // oldid 0 in constructor
73 $article = new Article( $page->getTitle(), 0 );
74 $this->assertSame( 0, $article->getOldID() );
75 $article->getRevisionFetched();
76 $this->assertSame( $idB, $article->getRevIdFetched() );
79 $article = new Article( $page->getTitle() );
80 $context = new RequestContext();
81 $context->setRequest( new FauxRequest( [ 'oldid' => $idA ] ) );
82 $article->setContext( $context );
83 $this->assertSame( $idA, $article->getOldID() );
84 $article->getRevisionFetched();
85 $this->assertSame( $idA, $article->getRevIdFetched() );
88 $article = new Article( $page->getTitle() );
89 $context = new RequestContext();
90 $context->setRequest( new FauxRequest( [] ) );
91 $article->setContext( $context );
92 $this->assertSame( 0, $article->getOldID() );
93 $article->getRevisionFetched();
94 $this->assertSame( $idB, $article->getRevIdFetched() );
97 public function testView() {
98 $page = $this->getPage( __METHOD__
, [ 1 => 'Test A', 2 => 'Test B' ] );
100 $article = new Article( $page->getTitle(), 0 );
101 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
104 $output = $article->getContext()->getOutput();
105 $this->assertContains( 'Test B', $this->getHtml( $output ) );
106 $this->assertNotContains( 'id="mw-revision-info"', $this->getHtml( $output ) );
107 $this->assertNotContains( 'id="mw-revision-nav"', $this->getHtml( $output ) );
110 public function testViewCached() {
111 $page = $this->getPage( __METHOD__
, [ 1 => 'Test A', 2 => 'Test B' ] );
113 $po = new ParserOutput( 'Cached Text' );
115 $article = new Article( $page->getTitle(), 0 );
116 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
118 $cache = MediaWikiServices
::getInstance()->getParserCache();
119 $cache->save( $po, $page, $article->getParserOptions() );
123 $output = $article->getContext()->getOutput();
124 $this->assertContains( 'Cached Text', $this->getHtml( $output ) );
125 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
126 $this->assertNotContains( 'Test B', $this->getHtml( $output ) );
130 * @covers Article::getRedirectTarget()
132 public function testViewRedirect() {
133 $target = Title
::makeTitle( $this->getDefaultWikitextNS(), 'Test_Target' );
134 $redirectText = '#REDIRECT [[' . $target->getPrefixedText() . ']]';
136 $page = $this->getPage( __METHOD__
, [ $redirectText ] );
138 $article = new Article( $page->getTitle(), 0 );
139 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
142 $this->assertNotNull(
143 $article->getRedirectTarget()->getPrefixedDBkey()
146 $target->getPrefixedDBkey(),
147 $article->getRedirectTarget()->getPrefixedDBkey()
150 $output = $article->getContext()->getOutput();
151 $this->assertContains( 'class="redirectText"', $this->getHtml( $output ) );
152 $this->assertContains(
153 '>' . htmlspecialchars( $target->getPrefixedText() ) . '<',
154 $this->getHtml( $output )
158 public function testViewNonText() {
159 $dummy = $this->getPage( __METHOD__
, [ 'Dummy' ] );
160 $dummyRev = $dummy->getRevision()->getRevisionRecord();
161 $title = $dummy->getTitle();
163 /** @var MockObject|ContentHandler $mockHandler */
164 $mockHandler = $this->getMockBuilder( ContentHandler
::class )
167 'isParserCacheSupported',
169 'unserializeContent',
173 ->setConstructorArgs( [ 'NotText', [ 'application/frobnitz' ] ] )
176 $mockHandler->method( 'isParserCacheSupported' )
177 ->willReturn( false );
179 $this->setTemporaryHook(
180 'ContentHandlerForModelID',
181 function ( $id, &$handler ) use ( $mockHandler ) {
182 $handler = $mockHandler;
186 /** @var MockObject|Content $content */
187 $content = $this->getMock( Content
::class );
188 $content->method( 'getParserOutput' )
189 ->willReturn( new ParserOutput( 'Structured Output' ) );
190 $content->method( 'getModel' )
191 ->willReturn( 'NotText' );
192 $content->method( 'getNativeData' )
193 ->willReturn( [ (object)[ 'x' => 'stuff' ] ] );
194 $content->method( 'copy' )
195 ->willReturn( $content );
197 $rev = new MutableRevisionRecord( $title );
198 $rev->setId( $dummyRev->getId() );
199 $rev->setPageId( $title->getArticleID() );
200 $rev->setUser( $dummyRev->getUser() );
201 $rev->setComment( $dummyRev->getComment() );
202 $rev->setTimestamp( $dummyRev->getTimestamp() );
204 $rev->setContent( 'main', $content );
206 $rev = new Revision( $rev );
208 /** @var MockObject|WikiPage $page */
209 $page = $this->getMockBuilder( WikiPage
::class )
210 ->setMethods( [ 'getRevision', 'getLatest' ] )
211 ->setConstructorArgs( [ $title ] )
214 $page->method( 'getRevision' )
215 ->willReturn( $rev );
216 $page->method( 'getLatest' )
217 ->willReturn( $rev->getId() );
219 $article = Article
::newFromWikiPage( $page, RequestContext
::getMain() );
220 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
223 $output = $article->getContext()->getOutput();
224 $this->assertContains( 'Structured Output', $this->getHtml( $output ) );
225 $this->assertNotContains( 'Dummy', $this->getHtml( $output ) );
228 public function testViewOfOldRevision() {
230 $page = $this->getPage( __METHOD__
, [ 1 => 'Test A', 2 => 'Test B' ], $revisions );
231 $idA = $revisions[1]->getId();
233 $article = new Article( $page->getTitle(), $idA );
234 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
237 $output = $article->getContext()->getOutput();
238 $this->assertContains( 'Test A', $this->getHtml( $output ) );
239 $this->assertContains( 'id="mw-revision-info"', $output->getSubtitle() );
240 $this->assertContains( 'id="mw-revision-nav"', $output->getSubtitle() );
242 $this->assertNotContains( 'id="revision-info-current"', $output->getSubtitle() );
243 $this->assertNotContains( 'Test B', $this->getHtml( $output ) );
246 public function testViewOfCurrentRevision() {
248 $page = $this->getPage( __METHOD__
, [ 1 => 'Test A', 2 => 'Test B' ], $revisions );
249 $idB = $revisions[2]->getId();
251 $article = new Article( $page->getTitle(), $idB );
252 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
255 $output = $article->getContext()->getOutput();
256 $this->assertContains( 'Test B', $this->getHtml( $output ) );
257 $this->assertContains( 'id="mw-revision-info-current"', $output->getSubtitle() );
258 $this->assertContains( 'id="mw-revision-nav"', $output->getSubtitle() );
261 public function testViewOfMissingRevision() {
263 $page = $this->getPage( __METHOD__
, [ 1 => 'Test A' ], $revisions );
264 $badId = $revisions[1]->getId() +
100;
266 $article = new Article( $page->getTitle(), $badId );
267 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
270 $output = $article->getContext()->getOutput();
271 $this->assertContains( 'missing-revision: ' . $badId, $this->getHtml( $output ) );
273 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
276 public function testViewOfDeletedRevision() {
278 $page = $this->getPage( __METHOD__
, [ 1 => 'Test A', 2 => 'Test B' ], $revisions );
279 $idA = $revisions[1]->getId();
281 $revDelList = new RevDelRevisionList(
282 RequestContext
::getMain(), $page->getTitle(), [ $idA ]
284 $revDelList->setVisibility( [
285 'value' => [ RevisionRecord
::DELETED_TEXT
=> 1 ],
286 'comment' => "Testing",
289 $article = new Article( $page->getTitle(), $idA );
290 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
293 $output = $article->getContext()->getOutput();
294 $this->assertContains( '(rev-deleted-text-permission)', $this->getHtml( $output ) );
296 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
297 $this->assertNotContains( 'Test B', $this->getHtml( $output ) );
300 public function testViewMissingPage() {
301 $page = $this->getPage( __METHOD__
);
303 $article = new Article( $page->getTitle() );
304 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
307 $output = $article->getContext()->getOutput();
308 $this->assertContains( '(noarticletextanon)', $this->getHtml( $output ) );
311 public function testViewDeletedPage() {
312 $page = $this->getPage( __METHOD__
, [ 1 => 'Test A', 2 => 'Test B' ] );
313 $page->doDeleteArticle( 'Test' );
315 $article = new Article( $page->getTitle() );
316 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
319 $output = $article->getContext()->getOutput();
320 $this->assertContains( 'moveddeleted', $this->getHtml( $output ) );
321 $this->assertContains( 'logentry-delete-delete', $this->getHtml( $output ) );
322 $this->assertContains( '(noarticletextanon)', $this->getHtml( $output ) );
324 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
325 $this->assertNotContains( 'Test B', $this->getHtml( $output ) );
328 public function testViewMessagePage() {
329 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Mainpage' );
330 $page = $this->getPage( $title );
332 $article = new Article( $page->getTitle() );
333 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
336 $output = $article->getContext()->getOutput();
337 $this->assertContains(
338 wfMessage( 'mainpage' )->inContentLanguage()->parse(),
339 $this->getHtml( $output )
341 $this->assertNotContains( '(noarticletextanon)', $this->getHtml( $output ) );
344 public function testViewMissingUserPage() {
345 $user = $this->getTestUser()->getUser();
346 $user->addToDatabase();
348 $title = Title
::makeTitle( NS_USER
, $user->getName() );
350 $page = $this->getPage( $title );
352 $article = new Article( $page->getTitle() );
353 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
356 $output = $article->getContext()->getOutput();
357 $this->assertContains( '(noarticletextanon)', $this->getHtml( $output ) );
358 $this->assertNotContains( '(userpage-userdoesnotexist-view)', $this->getHtml( $output ) );
361 public function testViewUserPageOfNonexistingUser() {
362 $user = User
::newFromName( 'Testing ' . __METHOD__
);
364 $title = Title
::makeTitle( NS_USER
, $user->getName() );
366 $page = $this->getPage( $title );
368 $article = new Article( $page->getTitle() );
369 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
372 $output = $article->getContext()->getOutput();
373 $this->assertContains( '(noarticletextanon)', $this->getHtml( $output ) );
374 $this->assertContains( '(userpage-userdoesnotexist-view:', $this->getHtml( $output ) );
377 public function testArticleViewHeaderHook() {
378 $page = $this->getPage( __METHOD__
, [ 1 => 'Test A' ] );
380 $article = new Article( $page->getTitle(), 0 );
381 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
383 $this->setTemporaryHook(
385 function ( Article
$articlePage, &$outputDone, &$useParserCache ) use ( $article ) {
386 $this->assertSame( $article, $articlePage, '$articlePage' );
388 $outputDone = new ParserOutput( 'Hook Text' );
389 $outputDone->setTitleText( 'Hook Title' );
391 $articlePage->getContext()->getOutput()->addParserOutput( $outputDone );
397 $output = $article->getContext()->getOutput();
398 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
399 $this->assertContains( 'Hook Text', $this->getHtml( $output ) );
400 $this->assertSame( 'Hook Title', $output->getPageTitle() );
403 public function testArticleContentViewCustomHook() {
404 $page = $this->getPage( __METHOD__
, [ 1 => 'Test A' ] );
406 $article = new Article( $page->getTitle(), 0 );
407 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
409 // use ArticleViewHeader hook to bypass the parser cache
410 $this->setTemporaryHook(
412 function ( Article
$articlePage, &$outputDone, &$useParserCache ) use ( $article ) {
413 $useParserCache = false;
417 $this->setTemporaryHook(
418 'ArticleContentViewCustom',
419 function ( Content
$content, Title
$title, OutputPage
$output ) use ( $page ) {
420 $this->assertSame( $page->getTitle(), $title, '$title' );
421 $this->assertSame( 'Test A', $content->getNativeData(), '$content' );
423 $output->addHTML( 'Hook Text' );
428 $this->hideDeprecated(
429 'ArticleContentViewCustom hook (used in hook-ArticleContentViewCustom-closure)'
434 $output = $article->getContext()->getOutput();
435 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
436 $this->assertContains( 'Hook Text', $this->getHtml( $output ) );
439 public function testArticleRevisionViewCustomHook() {
440 $page = $this->getPage( __METHOD__
, [ 1 => 'Test A' ] );
442 $article = new Article( $page->getTitle(), 0 );
443 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
445 // use ArticleViewHeader hook to bypass the parser cache
446 $this->setTemporaryHook(
448 function ( Article
$articlePage, &$outputDone, &$useParserCache ) use ( $article ) {
449 $useParserCache = false;
453 $this->setTemporaryHook(
454 'ArticleRevisionViewCustom',
455 function ( RevisionRecord
$rev, Title
$title, $oldid, OutputPage
$output ) use ( $page ) {
456 $content = $rev->getContent( 'main' );
458 $this->assertSame( $page->getTitle(), $title, '$title' );
459 $this->assertSame( 'Test A', $content->getNativeData(), '$content' );
461 $output->addHTML( 'Hook Text' );
468 $output = $article->getContext()->getOutput();
469 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
470 $this->assertContains( 'Hook Text', $this->getHtml( $output ) );
473 public function testArticleAfterFetchContentObjectHook() {
474 $page = $this->getPage( __METHOD__
, [ 1 => 'Test A' ] );
476 $article = new Article( $page->getTitle(), 0 );
477 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
479 // use ArticleViewHeader hook to bypass the parser cache
480 $this->setTemporaryHook(
482 function ( Article
$articlePage, &$outputDone, &$useParserCache ) use ( $article ) {
483 $useParserCache = false;
487 $this->setTemporaryHook(
488 'ArticleAfterFetchContentObject',
489 function ( Article
&$articlePage, Content
&$content ) use ( $page, $article ) {
490 $this->assertSame( $article, $articlePage, '$articlePage' );
491 $this->assertSame( 'Test A', $content->getNativeData(), '$content' );
493 $content = new WikitextContent( 'Hook Text' );
497 $this->hideDeprecated(
498 'ArticleAfterFetchContentObject hook'
499 . ' (used in hook-ArticleAfterFetchContentObject-closure)'
504 $output = $article->getContext()->getOutput();
505 $this->assertNotContains( 'Test A', $this->getHtml( $output ) );
506 $this->assertContains( 'Hook Text', $this->getHtml( $output ) );
509 public function testShowMissingArticleHook() {
510 $page = $this->getPage( __METHOD__
);
512 $article = new Article( $page->getTitle() );
513 $article->getContext()->getOutput()->setTitle( $page->getTitle() );
515 $this->setTemporaryHook(
516 'ShowMissingArticle',
517 function ( Article
$articlePage ) use ( $article ) {
518 $this->assertSame( $article, $articlePage, '$articlePage' );
520 $articlePage->getContext()->getOutput()->addHTML( 'Hook Text' );
526 $output = $article->getContext()->getOutput();
527 $this->assertContains( '(noarticletextanon)', $this->getHtml( $output ) );
528 $this->assertContains( 'Hook Text', $this->getHtml( $output ) );