2011-06-29 22:09:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-01-23 11:01:00 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
|
|
|
|
use MediaWiki\MainConfigSchema;
|
|
|
|
|
use MediaWiki\Page\ParserOutputAccess;
|
|
|
|
|
use MediaWiki\Parser\Parsoid\ParsoidOutputAccess;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-01-23 11:01:00 +00:00
|
|
|
|
2020-11-15 15:57:24 +00:00
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class ArticleTest extends \MediaWikiIntegrationTestCase {
|
2011-06-29 22:09:51 +00:00
|
|
|
|
2019-10-22 11:45:16 +00:00
|
|
|
protected $tablesUsed = [
|
|
|
|
|
'revision',
|
|
|
|
|
'recentchanges',
|
|
|
|
|
];
|
|
|
|
|
|
2023-01-23 11:01:00 +00:00
|
|
|
/**
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @param User|null $user
|
|
|
|
|
*
|
|
|
|
|
* @return Article
|
|
|
|
|
*/
|
|
|
|
|
private function newArticle( Title $title, User $user = null ): Article {
|
|
|
|
|
if ( !$user ) {
|
|
|
|
|
$user = $this->getTestUser()->getUser();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$context = new RequestContext();
|
|
|
|
|
$article = new Article( $title );
|
|
|
|
|
$context->setUser( $user );
|
|
|
|
|
$context->setTitle( $title );
|
|
|
|
|
$article->setContext( $context );
|
|
|
|
|
|
|
|
|
|
return $article;
|
2011-07-03 17:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-24 10:54:02 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Article::__get
|
|
|
|
|
* @covers Article::__set
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testGetOrSetOnNewProperty() {
|
2023-01-23 11:01:00 +00:00
|
|
|
$article = new Article( Title::newMainPage() );
|
|
|
|
|
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
$this->filterDeprecated(
|
|
|
|
|
'/Accessing Article::\$ext_someNewProperty/'
|
2020-03-19 03:23:43 +00:00
|
|
|
);
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
$this->filterDeprecated(
|
|
|
|
|
'/Setting Article::\$ext_someNewProperty/'
|
2020-03-19 03:23:43 +00:00
|
|
|
);
|
2023-01-23 11:01:00 +00:00
|
|
|
$article->ext_someNewProperty = 12;
|
|
|
|
|
$this->assertEquals( 12, $article->ext_someNewProperty,
|
2011-07-03 17:51:11 +00:00
|
|
|
"Article get/set magic on new field" );
|
2023-01-23 11:01:00 +00:00
|
|
|
$this->assertEquals( 12, $article->getPage()->ext_someNewProperty,
|
2020-03-19 03:23:43 +00:00
|
|
|
"Article get/set magic on new field" );
|
2023-01-23 11:01:00 +00:00
|
|
|
$article->ext_someNewProperty = -8;
|
|
|
|
|
$this->assertEquals( -8, $article->ext_someNewProperty,
|
2011-07-03 17:51:11 +00:00
|
|
|
"Article get/set magic on update to new field" );
|
2023-01-23 11:01:00 +00:00
|
|
|
$this->assertEquals( -8, $article->getPage()->ext_someNewProperty,
|
2020-03-19 03:23:43 +00:00
|
|
|
"Article get/set magic on new field" );
|
2011-07-03 17:51:11 +00:00
|
|
|
}
|
2020-09-07 09:30:43 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Article::__sleep
|
|
|
|
|
*/
|
|
|
|
|
public function testSerialization_fails() {
|
2023-01-23 11:01:00 +00:00
|
|
|
$article = new Article( Title::newMainPage() );
|
|
|
|
|
|
2020-09-07 09:30:43 +00:00
|
|
|
$this->expectException( LogicException::class );
|
2023-01-23 11:01:00 +00:00
|
|
|
serialize( $article );
|
2020-09-07 09:30:43 +00:00
|
|
|
}
|
2020-11-15 15:57:24 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests that missing article page shows parser contents
|
|
|
|
|
* of the well-known system message for NS_MEDIAWIKI pages
|
|
|
|
|
* @covers Article::showMissingArticle
|
|
|
|
|
*/
|
|
|
|
|
public function testMissingArticleMessage() {
|
|
|
|
|
// Use a well-known system message
|
2021-01-19 19:57:24 +00:00
|
|
|
$title = Title::makeTitle( NS_MEDIAWIKI, 'Uploadedimage' );
|
2023-01-23 11:01:00 +00:00
|
|
|
$article = $this->newArticle( $title );
|
|
|
|
|
|
2020-11-15 15:57:24 +00:00
|
|
|
$article->showMissingArticle();
|
|
|
|
|
$output = $article->getContext()->getOutput();
|
|
|
|
|
$this->assertStringContainsString(
|
|
|
|
|
Message::newFromKey( 'uploadedimage' )->parse(),
|
|
|
|
|
$output->getHTML()
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-05-25 15:39:48 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test if patrol footer is possible to show
|
|
|
|
|
* @covers Article::showPatrolFooter
|
|
|
|
|
* @dataProvider provideShowPatrolFooter
|
|
|
|
|
*/
|
|
|
|
|
public function testShowPatrolFooter( $group, $title, $editPageText, $isEditedBySameUser, $expectedResult ) {
|
2019-10-22 11:45:16 +00:00
|
|
|
$testPage = $this->getNonexistingTestPage( $title );
|
2021-05-25 15:39:48 +00:00
|
|
|
$user1 = $this->getTestUser( $group )->getUser();
|
|
|
|
|
$user2 = $this->getTestUser()->getUser();
|
|
|
|
|
if ( $editPageText !== null ) {
|
|
|
|
|
$editedUser = $isEditedBySameUser ? $user1 : $user2;
|
2019-10-22 11:45:16 +00:00
|
|
|
$editIsGood = $this->editPage( $testPage, $editPageText, '', NS_MAIN, $editedUser )->isGood();
|
2021-11-21 16:23:11 +00:00
|
|
|
$this->assertTrue( $editIsGood, 'edited a page' );
|
2021-05-25 15:39:48 +00:00
|
|
|
}
|
2019-10-22 11:45:16 +00:00
|
|
|
|
2023-01-23 11:01:00 +00:00
|
|
|
$article = $this->newArticle( $title, $user1 );
|
2021-05-25 15:39:48 +00:00
|
|
|
$this->assertSame( $expectedResult, $article->showPatrolFooter() );
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideShowPatrolFooter() {
|
2021-05-25 15:39:48 +00:00
|
|
|
yield 'UserAllowedRevExist' => [
|
|
|
|
|
'sysop',
|
|
|
|
|
Title::makeTitle( NS_MAIN, 'Page1' ),
|
|
|
|
|
'EditPage1',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield 'UserNotAllowedRevExist' => [
|
|
|
|
|
null,
|
|
|
|
|
Title::makeTitle( NS_MAIN, 'Page2' ),
|
|
|
|
|
'EditPage2',
|
|
|
|
|
false,
|
|
|
|
|
false
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield 'UserAllowedNoRev' => [
|
|
|
|
|
'sysop',
|
|
|
|
|
Title::makeTitle( NS_MAIN, 'Page3' ),
|
|
|
|
|
null,
|
|
|
|
|
false,
|
|
|
|
|
false
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield 'UserAllowedRevExistBySameUser' => [
|
|
|
|
|
'sysop',
|
|
|
|
|
Title::makeTitle( NS_MAIN, 'Page4' ),
|
|
|
|
|
'EditPage4',
|
|
|
|
|
true,
|
|
|
|
|
false
|
|
|
|
|
];
|
|
|
|
|
}
|
2019-10-22 11:45:16 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show patrol footer even if the page was moved (T162871).
|
|
|
|
|
*
|
|
|
|
|
* @covers Article::showPatrolFooter
|
|
|
|
|
*/
|
|
|
|
|
public function testShowPatrolFooterMovedPage() {
|
|
|
|
|
$oldTitle = Title::makeTitle( NS_USER, 'NewDraft' );
|
|
|
|
|
$newTitle = Title::makeTitle( NS_MAIN, 'NewDraft' );
|
|
|
|
|
$editor = $this->getTestUser()->getUser();
|
|
|
|
|
|
|
|
|
|
$editIsGood = $this->editPage( $oldTitle, 'Content', '', NS_USER, $editor )->isGood();
|
|
|
|
|
$this->assertTrue( $editIsGood, 'edited a page' );
|
|
|
|
|
|
|
|
|
|
$status = $this->getServiceContainer()
|
|
|
|
|
->getMovePageFactory()
|
|
|
|
|
->newMovePage( $oldTitle, $newTitle )
|
|
|
|
|
->move( $this->getTestUser()->getUser() );
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
|
|
|
|
|
$sysop = $this->getTestUser( 'sysop' )->getUser();
|
2023-01-23 11:01:00 +00:00
|
|
|
$article = $this->newArticle( $newTitle, $sysop );
|
2019-10-22 11:45:16 +00:00
|
|
|
|
|
|
|
|
$this->assertTrue( $article->showPatrolFooter() );
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 11:01:00 +00:00
|
|
|
/**
|
|
|
|
|
* Ensure that content that is present in the parser cache will be used.
|
|
|
|
|
*
|
|
|
|
|
* @covers Article::generateContentOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testUsesCachedOutput() {
|
|
|
|
|
$title = $this->getExistingTestPage()->getTitle();
|
|
|
|
|
|
|
|
|
|
$parserOutputAccess = $this->createNoOpMock( ParserOutputAccess::class, [ 'getCachedParserOutput' ] );
|
|
|
|
|
$parserOutputAccess->method( 'getCachedParserOutput' )
|
|
|
|
|
->willReturn( new ParserOutput( 'Kittens' ) );
|
|
|
|
|
|
|
|
|
|
$parsoidOutputAccess = $this->createNoOpMock( ParsoidOutputAccess::class );
|
|
|
|
|
|
|
|
|
|
$this->setService( 'ParserOutputAccess', $parserOutputAccess );
|
|
|
|
|
$this->setService( 'ParsoidOutputAccess', $parsoidOutputAccess );
|
|
|
|
|
|
|
|
|
|
$article = $this->newArticle( $title );
|
|
|
|
|
$article->view();
|
|
|
|
|
$this->assertStringContainsString( 'Kittens', $article->getContext()->getOutput()->getHTML() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Ensure that content that is present in the parser cache will be used.
|
|
|
|
|
*
|
|
|
|
|
* @covers Article::generateContentOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testOutputIsCached() {
|
|
|
|
|
$this->overrideConfigValue(
|
|
|
|
|
MainConfigNames::ParsoidCacheConfig,
|
|
|
|
|
[ 'WarmParsoidParserCache' => true ]
|
|
|
|
|
+ MainConfigSchema::getDefaultValue( MainConfigNames::ParsoidCacheConfig )
|
|
|
|
|
);
|
|
|
|
|
$title = $this->getExistingTestPage()->getTitle();
|
|
|
|
|
|
|
|
|
|
$parserOutputAccess = $this->createNoOpMock(
|
|
|
|
|
ParserOutputAccess::class,
|
|
|
|
|
[ 'getCachedParserOutput', 'getParserOutput', ]
|
|
|
|
|
);
|
|
|
|
|
$parserOutputAccess->method( 'getCachedParserOutput' )
|
|
|
|
|
->willReturn( null );
|
|
|
|
|
$parserOutputAccess
|
|
|
|
|
->expects( $this->once() ) // This is the key assertion in this test case.
|
|
|
|
|
->method( 'getParserOutput' )
|
2023-05-12 10:26:57 +00:00
|
|
|
->with(
|
|
|
|
|
$this->anything(),
|
|
|
|
|
$this->callback( function ( ParserOptions $parserOptions ) {
|
|
|
|
|
$this->assertSame( 'page-view', $parserOptions->getRenderReason() );
|
|
|
|
|
return true;
|
|
|
|
|
} ),
|
|
|
|
|
$this->anything(),
|
|
|
|
|
$this->callback( function ( $options ) {
|
|
|
|
|
$this->assertTrue( (bool)( $options & ParserOutputAccess::OPT_NO_CHECK_CACHE ),
|
|
|
|
|
"The cache is not checked again" );
|
|
|
|
|
$this->assertTrue( (bool)( $options & ParserOutputAccess::OPT_LINKS_UPDATE ),
|
|
|
|
|
"WikiPage::triggerOpportunisticLinksUpdate is attempted" );
|
|
|
|
|
return true;
|
|
|
|
|
} )
|
|
|
|
|
)
|
2023-01-23 11:01:00 +00:00
|
|
|
->willReturn( Status::newGood( new ParserOutput( 'Old Kittens' ) ) );
|
|
|
|
|
|
|
|
|
|
$parsoidOutputAccess = $this->createNoOpMock(
|
|
|
|
|
ParsoidOutputAccess::class,
|
|
|
|
|
[ 'getParserOutput', 'supportsContentModel' ]
|
|
|
|
|
);
|
|
|
|
|
$parsoidOutputAccess->method( 'supportsContentModel' )
|
|
|
|
|
->willReturn( true );
|
|
|
|
|
$parsoidOutputAccess
|
|
|
|
|
->expects( $this->once() ) // This is the key assertion in this test case.
|
|
|
|
|
->method( 'getParserOutput' )
|
|
|
|
|
->willReturn( Status::newGood( new ParserOutput( 'New Kittens' ) ) );
|
|
|
|
|
|
|
|
|
|
$this->setService( 'ParserOutputAccess', $parserOutputAccess );
|
|
|
|
|
$this->setService( 'ParsoidOutputAccess', $parsoidOutputAccess );
|
|
|
|
|
|
|
|
|
|
$article = $this->newArticle( $title );
|
|
|
|
|
$article->view();
|
|
|
|
|
|
|
|
|
|
$this->runJobs( [ 'minJobs' => 1, 'maxJobs' => 1 ], [ 'type' => 'parsoidCachePrewarm' ] );
|
|
|
|
|
|
|
|
|
|
// This is just a sanity check, not the key assertion.
|
|
|
|
|
$this->assertStringContainsString( 'Old Kittens', $article->getContext()->getOutput()->getHTML() );
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-29 22:09:51 +00:00
|
|
|
}
|