phpunit: Randomize and improve default test page names

UTPage is badly named, because it doesn't give any information as to
what test caused the page to be created. It also sort of encourages test
authors to rely on this "UTPage" page being created by the framework for
them.

Both these things are dangerous, or at least very questionable. Use a
random page title instead, but include the caller name in case someone
needs to investigate where a test page is coming from.

Do the same for summary and content, too.

In getExistingTestPage, add a check to make sure that the page was
created successfully. Do not use assert* to avoid adding assertions
extraneous to the test.

addCoreDBData is not changed because that method will be removed
entirely (T342428).

Fix tests that are now failing:
- ParsoidOutputAccessTest was relying on the content of
  getExistingTestPage to be UTContent.
- HTMLHandlerTestTrait did not account for spaces in the page name (also
  change the signature to reflect the fact that WikiPage is always
  passed in).
- HtmlInputTransformHelperTest was relying on the fake test page to be
  there.
- PoolWorkArticleViewTest is leaving pages behind, and for some reason
  that's making SpecialRecentchangesTest fail.

Bug: T341344
Change-Id: I9c2dc1cf1f184c8062864756d2747ee56e886086
This commit is contained in:
Daimona Eaytoy 2023-08-09 17:43:50 +02:00 committed by Krinkle
parent 65dacd5748
commit 77d4c2c454
5 changed files with 68 additions and 18 deletions

View file

@ -308,24 +308,34 @@ abstract class MediaWikiIntegrationTestCase extends PHPUnit\Framework\TestCase {
' method should return true. Use @group Database or $this->tablesUsed.' );
}
$title = ( $title === null ) ? 'UTPage' : $title;
$title = is_string( $title ) ? Title::newFromText( $title ) : $title;
$caller = $this->getCallerName();
if ( !$title instanceof Title ) {
if ( $title === null ) {
static $counter;
$counter = $counter === null ? random_int( 10, 1000 ) : ++$counter;
$title = "Test page $counter $caller";
}
$title = Title::newFromText( $title );
}
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
if ( !$page->exists() ) {
$user = static::getTestSysop()->getUser();
$page->doUserEditContent(
$status = $page->doUserEditContent(
ContentHandler::makeContent(
'UTContent',
"Test content for $caller",
$title,
// Regardless of how the wiki is configure or what extensions are present,
// force this page to be a wikitext one.
CONTENT_MODEL_WIKITEXT
),
$user,
'UTPageSummary',
"Summary for $caller",
EDIT_NEW | EDIT_SUPPRESS_RC
);
if ( !$status->isGood() ) {
throw new RuntimeException( "Could not create test page: $status" );
}
}
return $page;
@ -347,18 +357,33 @@ abstract class MediaWikiIntegrationTestCase extends PHPUnit\Framework\TestCase {
' method should return true. Use @group Database or $this->tablesUsed.' );
}
$title = ( $title === null ) ? 'UTPage-' . rand( 0, 100000 ) : $title;
$title = is_string( $title ) ? Title::newFromText( $title ) : $title;
$caller = $this->getCallerName();
if ( !$title instanceof Title ) {
if ( $title === null ) {
$title = 'Test page ' . $caller . ' ' . wfRandomString();
}
$title = Title::newFromText( $title );
}
$wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
$page = $wikiPageFactory->newFromTitle( $title );
if ( $page->exists() ) {
$this->deletePage( $page );
$this->deletePage( $page, "Deleting for $caller" );
}
return $page;
}
/**
* Retuns the calling method, in a format suitable for page titles etc.
*
* @return string
*/
private function getCallerName(): string {
$classWithoutNamespace = ( new ReflectionClass( $this ) )->getShortName();
return $classWithoutNamespace . '-' . debug_backtrace()[2]['function'];
}
/**
* Determine config overrides, taking into account the local system's actual settings and
* avoiding interference with any custom overrides.

View file

@ -12,6 +12,7 @@ use Psr\Log\NullLogger;
* @group Database
*/
class PoolWorkArticleViewTest extends MediaWikiIntegrationTestCase {
protected $tablesUsed = [ 'page' ];
/**
* @param LoggerInterface|null $logger

View file

@ -7,6 +7,7 @@ use MediaWiki\Edit\ParsoidOutputStash;
use MediaWiki\Edit\SimpleParsoidOutputStash;
use MediaWiki\Parser\Parsoid\ParsoidRenderID;
use MediaWiki\Rest\RequestData;
use WikiPage;
/**
* This trait is used in PageHTMLHandlerTest.php & RevisionHTMLHandlerTest.php
@ -25,7 +26,7 @@ trait HTMLHandlerTestTrait {
}
/**
* @param string $page
* @param WikiPage $page
* @param array $queryParams
* @param array $config
*
@ -33,13 +34,13 @@ trait HTMLHandlerTestTrait {
* @throws \Exception
*/
private function executePageHTMLRequest(
string $page,
WikiPage $page,
array $queryParams = [],
array $config = []
): array {
$handler = $this->newHandler();
$request = new RequestData( [
'pathParams' => [ 'title' => $page ],
'pathParams' => [ 'title' => $page->getTitle()->getPrefixedDBkey() ],
'queryParams' => $queryParams,
] );
$result = $this->executeHandler( $handler,

View file

@ -5,6 +5,7 @@ namespace MediaWiki\Tests\Rest\Handler\Helper;
use BufferingStatsdDataFactory;
use Exception;
use Liuggio\StatsdClient\Factory\StatsdDataFactory;
use LogicException;
use MediaWiki\Edit\SelserContext;
use MediaWiki\MainConfigNames;
use MediaWiki\MainConfigSchema;
@ -28,6 +29,7 @@ use NullStatsdDataFactory;
use ParserOptions;
use ParserOutput;
use PHPUnit\Framework\MockObject\MockObject;
use TextContent;
use Wikimedia\Message\MessageValue;
use Wikimedia\Parsoid\Core\ClientError;
use Wikimedia\Parsoid\Core\PageBundle;
@ -285,7 +287,7 @@ class HtmlInputTransformHelperTest extends MediaWikiIntegrationTestCase {
yield 'should use selser, return original wikitext because the HTML didn\'t change' => [
$body,
$params,
[ 'UTContent' ], // Returns original wikitext, because HTML didn't change.
null, // Returns original wikitext, because HTML didn't change.
];
// Should fall back to non-selective serialization. //////////////////
@ -674,6 +676,10 @@ class HtmlInputTransformHelperTest extends MediaWikiIntegrationTestCase {
}
/**
* @param array $body
* @param array $params
* @param string|string[]|null $expectedText Null means use the original content.
* @param array $expectedHeaders
* @dataProvider provideRequests()
* @covers \MediaWiki\Rest\Handler\Helper\HtmlInputTransformHelper
* @covers \MediaWiki\Parser\Parsoid\HtmlToContentTransform
@ -681,10 +687,14 @@ class HtmlInputTransformHelperTest extends MediaWikiIntegrationTestCase {
public function testResponse( $body, $params, $expectedText, array $expectedHeaders = [] ) {
if ( !empty( $params['oldid'] ) ) {
// If an oldid is set, run the test with an actual existing revision ID
$page = $this->getExistingTestPage()->getTitle();
$originalContent = __METHOD__ . ' original content';
$page = $this->getNonexistingTestPage();
$this->editPage( $page, new WikitextContent( $originalContent ) );
$page = $page->getTitle();
$params['oldid'] = $page->getLatestRevID();
} else {
$page = PageIdentityValue::localIdentity( 7, NS_MAIN, $body['pageName'] ?? 'HtmlInputTransformHelperTest' );
$originalContent = '';
}
$stats = new BufferingStatsdDataFactory( '' );
@ -704,6 +714,7 @@ class HtmlInputTransformHelperTest extends MediaWikiIntegrationTestCase {
$body->rewind();
$text = $body->getContents();
$expectedText ??= $originalContent;
foreach ( (array)$expectedText as $exp ) {
$this->assertStringContainsString( $exp, $text );
}
@ -761,7 +772,7 @@ class HtmlInputTransformHelperTest extends MediaWikiIntegrationTestCase {
$selserContext,
1, // will be replaced by the actual revid
$unchangedPB, // Expect selser, since HTML didn't change.
'UTContent', // Loaded from default test page revision. Selser should preserve it.
null, // Selser should preserve the original content.
];
// should use wikitext from fake revision ////////////////////
@ -791,7 +802,7 @@ class HtmlInputTransformHelperTest extends MediaWikiIntegrationTestCase {
* @param SelserContext|null $stashed
* @param RevisionRecord|int|null $rev
* @param ParsoidRenderID|PageBundle|ParserOutput|null $originalRendering
* @param string|string[] $expectedText
* @param string|string[]|null $expectedText Null means use the original content
*
* @throws HttpException
* @throws \MWException
@ -800,13 +811,22 @@ class HtmlInputTransformHelperTest extends MediaWikiIntegrationTestCase {
public function testSetOriginal( ?SelserContext $stashed, $rev, $originalRendering, $expectedText ) {
if ( is_int( $rev ) && $rev > 0 ) {
// If a revision ID is given, run the test with an actual existing revision ID
$page = $this->getExistingTestPage();
$originalContent = __METHOD__ . ' original content';
$page = $this->getNonexistingTestPage();
$this->editPage( $page, new WikitextContent( $originalContent ) );
$page = $page->getTitle();
$revId = $page->getLatestRevID() ?: 0;
$rev = $revId;
} elseif ( $rev instanceof RevisionRecord ) {
$originalContentObj = $rev->getContent( SlotRecord::MAIN );
if ( !$originalContentObj instanceof TextContent ) {
throw new LogicException( 'Not implemented' );
}
$originalContent = $originalContentObj->getText();
$page = $rev->getPage();
$revId = $rev->getId() ?: 0;
} else {
$originalContent = '';
$page = PageIdentityValue::localIdentity( 7, NS_MAIN, 'HtmlInputTransformHelperTest' );
$revId = 0;
}
@ -838,6 +858,7 @@ class HtmlInputTransformHelperTest extends MediaWikiIntegrationTestCase {
$body->rewind();
$text = $body->getContents();
$expectedText ??= $originalContent;
foreach ( (array)$expectedText as $exp ) {
$this->assertStringContainsString( $exp, $text );
}

View file

@ -682,7 +682,9 @@ class ParsoidOutputAccessTest extends MediaWikiIntegrationTestCase {
$services = $this->getServiceContainer();
$parserOutputAccess = $services->getParsoidOutputAccess();
$page = $this->getExistingTestPage();
$content = 'Test content for ' . __METHOD__;
$page = Title::makeTitle( NS_MAIN, 'TestGetParserOutputWithLanguageOverride' );
$this->editPage( $page, $content );
$status = $parserOutputAccess->getParserOutput( $page, $parserOptions );
@ -690,7 +692,7 @@ class ParsoidOutputAccessTest extends MediaWikiIntegrationTestCase {
// assert dummy content in parsoid output HTML
$html = $status->getValue()->getRawText();
$this->assertStringContainsString( 'UTContent', $html );
$this->assertStringContainsString( $content, $html );
if ( $parserOptions->getTargetLanguage() !== null ) {
$targetLanguage = $parserOptions->getTargetLanguage()->getCode();