REST page metadata endpoints: handle supressed data gracefully
Why: - Revision meta-data output was failingfor revisions with suppressed user or comment What: - Handle suppressed user and comment gracefully - add regression test Bug: T386368 Bug: T387397 Change-Id: Ic6d3fc89d24030f5c3fd422637816de9976fc709 (cherry picked from commit 8c53a15ba0fae2677aa8e28055f06ef557595be6)
This commit is contained in:
parent
1a623d1487
commit
548d5eb8de
2 changed files with 106 additions and 2 deletions
|
|
@ -299,6 +299,9 @@ class PageContentHelper {
|
|||
$restrictions[] = 'userhidden';
|
||||
}
|
||||
|
||||
$publicUser = $revision->getUser();
|
||||
$publicComment = $revision->getComment();
|
||||
|
||||
return [
|
||||
'title' => $title->getPrefixedDBkey(),
|
||||
'page_id' => $page->getId(),
|
||||
|
|
@ -312,9 +315,9 @@ class PageContentHelper {
|
|||
|
||||
'namespace' => $page->getNamespace(),
|
||||
'user_id' => $revision->getUser( RevisionRecord::RAW )->getId(),
|
||||
'user_text' => $revision->getUser( RevisionRecord::FOR_PUBLIC )->getName(),
|
||||
'user_text' => $publicUser ? $publicUser->getName() : null,
|
||||
'comment' => $publicComment ? $publicComment->text : null,
|
||||
'timestamp' => wfTimestampOrNull( TS_ISO_8601, $revision->getTimestamp() ),
|
||||
'comment' => $revision->getComment()->text,
|
||||
'tags' => $tags,
|
||||
'restrictions' => $restrictions,
|
||||
'page_language' => $title->getPageLanguage()->getCode(),
|
||||
|
|
|
|||
|
|
@ -3,19 +3,24 @@
|
|||
namespace MediaWiki\Tests\Rest\Handler\Helper;
|
||||
|
||||
use Exception;
|
||||
use MediaWiki\CommentStore\CommentStoreComment;
|
||||
use MediaWiki\Config\ServiceOptions;
|
||||
use MediaWiki\MainConfigNames;
|
||||
use MediaWiki\Page\ExistingPageRecord;
|
||||
use MediaWiki\Page\PageIdentityValue;
|
||||
use MediaWiki\Permissions\Authority;
|
||||
use MediaWiki\Rest\Handler\Helper\PageContentHelper;
|
||||
use MediaWiki\Rest\HttpException;
|
||||
use MediaWiki\Rest\LocalizedHttpException;
|
||||
use MediaWiki\Rest\Response;
|
||||
use MediaWiki\Revision\MutableRevisionRecord;
|
||||
use MediaWiki\Revision\RevisionRecord;
|
||||
use MediaWiki\Revision\SlotRecord;
|
||||
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
|
||||
use MediaWiki\Title\Title;
|
||||
use MediaWiki\User\UserIdentityValue;
|
||||
use MediaWikiIntegrationTestCase;
|
||||
use Wikimedia\TestingAccessWrapper;
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\Rest\Handler\Helper\PageContentHelper
|
||||
|
|
@ -344,4 +349,100 @@ class PageContentHelperTest extends MediaWikiIntegrationTestCase {
|
|||
$this->assertEquals( $expected, $data );
|
||||
}
|
||||
|
||||
public function provideConstructRestbaseCompatibleMetadata() {
|
||||
$pageName = 'User:Morg';
|
||||
$page = PageIdentityValue::localIdentity( 7, NS_USER, 'Morg' );
|
||||
$user = UserIdentityValue::newRegistered( 444, 'Morg' );
|
||||
$comment = CommentStoreComment::newUnsavedComment( 'just an edit' );
|
||||
$timestamp = '20220102112233';
|
||||
|
||||
$rev = new MutableRevisionRecord( $page );
|
||||
$rev->setId( 123 );
|
||||
$rev->setUser( $user );
|
||||
$rev->setComment( $comment );
|
||||
$rev->setTimestamp( $timestamp );
|
||||
|
||||
$expected = [
|
||||
'title' => $pageName,
|
||||
'page_id' => 7,
|
||||
'rev' => 123,
|
||||
'tid' => 'DUMMY',
|
||||
'namespace' => NS_USER,
|
||||
'user_id' => 444,
|
||||
'user_text' => 'Morg',
|
||||
'comment' => $comment->text,
|
||||
'timestamp' => wfTimestampOrNull( TS_ISO_8601, $timestamp ),
|
||||
'tags' => [],
|
||||
'restrictions' => [],
|
||||
'page_language' => 'en',
|
||||
'redirect' => false
|
||||
];
|
||||
|
||||
yield [
|
||||
$pageName,
|
||||
$rev,
|
||||
$expected
|
||||
];
|
||||
|
||||
// Construct a revision with a hidden comment
|
||||
$rev = new MutableRevisionRecord( $page );
|
||||
$rev->setId( 123 );
|
||||
$rev->setUser( $user );
|
||||
$rev->setComment( $comment );
|
||||
$rev->setTimestamp( $timestamp );
|
||||
$rev->setVisibility( RevisionRecord::DELETED_COMMENT );
|
||||
|
||||
$expectedHiddenComment = [
|
||||
'comment' => null,
|
||||
'restrictions' => [ 'commenthidden' ],
|
||||
] + $expected;
|
||||
|
||||
yield [
|
||||
$pageName,
|
||||
$rev,
|
||||
$expectedHiddenComment
|
||||
];
|
||||
|
||||
// Construct a revision with a suppressed user
|
||||
$rev = new MutableRevisionRecord( $page );
|
||||
$rev->setId( 123 );
|
||||
$rev->setUser( $user );
|
||||
$rev->setComment( $comment );
|
||||
$rev->setTimestamp( $timestamp );
|
||||
$rev->setVisibility( RevisionRecord::DELETED_USER );
|
||||
|
||||
$expectedHiddenComment = [
|
||||
'user_text' => null,
|
||||
'restrictions' => [ 'userhidden' ],
|
||||
] + $expected;
|
||||
|
||||
yield [
|
||||
$pageName,
|
||||
$rev,
|
||||
$expectedHiddenComment
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideConstructRestbaseCompatibleMetadata
|
||||
*/
|
||||
public function testConstructRestbaseCompatibleMetadata(
|
||||
string $pageName,
|
||||
RevisionRecord $revision,
|
||||
array $expected
|
||||
) {
|
||||
$helper = $this->newHelper( [ 'title' => $pageName ] );
|
||||
|
||||
$helperAccess = TestingAccessWrapper::newFromObject( $helper );
|
||||
$helperAccess->pageIdentity = $revision->getPage();
|
||||
$helperAccess->targetRevision = $revision;
|
||||
|
||||
$data = $helper->constructRestbaseCompatibleMetadata();
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue