Merge "ApiDiscussionToolsTrait: PageInfo & Compare don't need HTML for editing"

This commit is contained in:
jenkins-bot 2022-11-22 08:51:57 +00:00 committed by Gerrit Code Review
commit 7d06b61347
4 changed files with 153 additions and 54 deletions

View file

@ -9,7 +9,9 @@ use DerivativeRequest;
use IContextSource;
use MediaWiki\Extension\VisualEditor\ParsoidClient;
use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory;
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\RevisionRecord;
use ParserOptions;
use Title;
use TitleValue;
use Wikimedia\Parsoid\Utils\DOMCompat;
@ -27,9 +29,15 @@ trait ApiDiscussionToolsTrait {
* @return ContentThreadItemSet
*/
protected function parseRevision( RevisionRecord $revision ): ContentThreadItemSet {
$response = $this->requestRestbasePageHtml( $revision );
$parsoidOutputAccess = MediaWikiServices::getInstance()->getParsoidOutputAccess();
$status = $parsoidOutputAccess->getParserOutput(
$revision->getPage(),
ParserOptions::newFromAnon(),
$revision
);
$html = $status->getValue()->getText();
$doc = DOMUtils::parseHTML( $response['body'] );
$doc = DOMUtils::parseHTML( $html );
$container = DOMCompat::getBody( $doc );
CommentUtils::unwrapParsoidSections( $container );
@ -146,6 +154,9 @@ trait ApiDiscussionToolsTrait {
}
/**
* @warning (T323357) - Calling this method writes to stash, so it should be called
* only when we are fetching page HTML for editing.
*
* @param RevisionRecord $revision
* @return array
*/

View file

@ -1,52 +0,0 @@
<?php
namespace MediaWiki\Extension\DiscussionTools\Tests;
use MediaWiki\Extension\DiscussionTools\ApiDiscussionToolsPageInfo;
use MediaWiki\MediaWikiServices;
use Wikimedia\TestingAccessWrapper;
/**
* @group medium
* @covers \MediaWiki\Extension\DiscussionTools\ApiDiscussionToolsPageInfo
*/
class ApiDiscussionToolsPageInfoTest extends IntegrationTestCase {
/**
* @dataProvider provideGetThreadItemsHtml
*/
public function testGetThreadItemsHtml(
string $name, string $title, string $dom, string $expected, string $config, string $data
): void {
$dom = static::getHtml( $dom );
$expectedPath = $expected;
$expected = static::getJson( $expected );
$config = static::getJson( $config );
$data = static::getJson( $data );
$doc = static::createDocument( $dom );
$container = static::getThreadContainer( $doc );
$this->setupEnv( $config, $data );
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
$threadItemSet = static::createParser( $data )->parse( $container, $title );
$pageInfo = TestingAccessWrapper::newFromClass( ApiDiscussionToolsPageInfo::class );
$threadItemsHtml = $pageInfo->getThreadItemsHtml( $threadItemSet );
// Optionally write updated content to the JSON files
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
static::overwriteJsonFile( $expectedPath, $threadItemsHtml );
}
static::assertEquals( $expected, $threadItemsHtml, $name );
$processedThreads = [];
}
public function provideGetThreadItemsHtml(): array {
return static::getJson( '../cases/threaditemshtml.json' );
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace MediaWiki\Extension\DiscussionTools\Tests;
use ApiTestCase;
use Title;
/**
* @group medium
* @group Database
* @covers \MediaWiki\Extension\DiscussionTools\ApiDiscussionToolsCompare
*/
class ApiDiscussionToolsCompareTest extends ApiTestCase {
/**
* @covers \MediaWiki\Extension\DiscussionTools\ApiDiscussionToolsCompare::execute
*/
public function testExecuteApiDiscussionToolsCompare() {
$title = Title::newFromText( 'Talk:' . __METHOD__ );
$page = $this->getNonexistingTestPage( $title );
$this->editPage( $page, "== Test ==\n\nadd DT pageinfo content\n" );
$rev1 = $page->getLatest();
$this->editPage( $page, ':adding another edit' );
$rev2 = $page->getLatest();
$params = [
'action' => 'discussiontoolscompare',
'fromrev' => $rev1,
'torev' => $rev2,
];
$result = $this->doApiRequestWithToken( $params );
$this->assertNotEmpty( $result[0]['discussiontoolscompare'] );
$this->assertArrayHasKey( 'fromrevid', $result[0]['discussiontoolscompare'] );
$this->assertSame( $rev1, $result[0]['discussiontoolscompare']['fromrevid'] );
$this->assertArrayHasKey( 'torevid', $result[0]['discussiontoolscompare'] );
$this->assertSame( $rev2, $result[0]['discussiontoolscompare']['torevid'] );
$this->assertArrayHasKey( 'removedcomments', $result[0]['discussiontoolscompare'] );
$this->assertArrayHasKey( 'addedcomments', $result[0]['discussiontoolscompare'] );
}
}

View file

@ -0,0 +1,95 @@
<?php
namespace MediaWiki\Extension\DiscussionTools\Tests;
use ApiTestCase;
use MediaWiki\Extension\DiscussionTools\ApiDiscussionToolsPageInfo;
use MediaWiki\MediaWikiServices;
use Wikimedia\TestingAccessWrapper;
/**
* @group medium
* @group Database
* @covers \MediaWiki\Extension\DiscussionTools\ApiDiscussionToolsPageInfo
*/
class ApiDiscussionToolsPageInfoTest extends ApiTestCase {
use TestUtils;
/**
* Setup the MW environment
*
* @param array $config
* @param array $data
*/
protected function setupEnv( array $config, array $data ): void {
$this->setMwGlobals( $config );
$this->setMwGlobals( [
'wgArticlePath' => $config['wgArticlePath'],
'wgNamespaceAliases' => $config['wgNamespaceIds'],
'wgMetaNamespace' => strtr( $config['wgFormattedNamespaces'][NS_PROJECT], ' ', '_' ),
'wgMetaNamespaceTalk' => strtr( $config['wgFormattedNamespaces'][NS_PROJECT_TALK], ' ', '_' ),
// TODO: Move this to $config
'wgLocaltimezone' => $data['localTimezone'],
// Data used for the tests assumes there are no variants for English.
// Language variants are tested using other languages.
'wgUsePigLatinVariant' => false,
] );
$this->setUserLang( $config['wgContentLanguage'] );
$this->setContentLang( $config['wgContentLanguage'] );
}
/**
* @dataProvider provideGetThreadItemsHtml
*/
public function testGetThreadItemsHtml(
string $name, string $title, string $dom, string $expected, string $config, string $data
): void {
$dom = static::getHtml( $dom );
$expectedPath = $expected;
$expected = static::getJson( $expected );
$config = static::getJson( $config );
$data = static::getJson( $data );
$doc = static::createDocument( $dom );
$container = static::getThreadContainer( $doc );
$this->setupEnv( $config, $data );
$title = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( $title );
$threadItemSet = static::createParser( $data )->parse( $container, $title );
$pageInfo = TestingAccessWrapper::newFromClass( ApiDiscussionToolsPageInfo::class );
$threadItemsHtml = $pageInfo->getThreadItemsHtml( $threadItemSet );
// Optionally write updated content to the JSON files
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
static::overwriteJsonFile( $expectedPath, $threadItemsHtml );
}
static::assertEquals( $expected, $threadItemsHtml, $name );
}
public function provideGetThreadItemsHtml(): array {
return static::getJson( '../cases/threaditemshtml.json' );
}
/**
* @covers \MediaWiki\Extension\DiscussionTools\ApiDiscussionToolsPageInfo::execute
*/
public function testExecuteApiDiscussionToolsPageInfo() {
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'add DT pageinfo content' );
$params = [
'action' => 'discussiontoolspageinfo',
'page' => $page->getTitle()->getText(),
];
$result = $this->doApiRequestWithToken( $params );
$this->assertNotEmpty( $result[0]['discussiontoolspageinfo'] );
$this->assertArrayHasKey( 'transcludedfrom', $result[0]['discussiontoolspageinfo'] );
}
}