wiki.techinc.nl/tests/phpunit/includes/editpage/PreloadedContentBuilderTest.php
Bartosz Dziewoński 6a20dc29ae editpage: Split off producing edit intro messages and preloaded content
Introduce two new classes, containing code split off from EditPage:
* IntroMessageBuilder (edit notices and other intro messages)
* PreloadedContentBuilder (initial text of new pages / sections)

I'm doing both of these features in one change, because they share a
lot of code. They are meant to be used by alternative editors to
support all of the features of the MediaWiki edit form. This isn't
everything you need yet (we should at least do this for the edit
checkboxes too), but it's a step.

Bug: T201613
Change-Id: If0b05710cb52a977bf4e85947d72d68683a0a29e
2023-05-16 20:51:00 +02:00

102 lines
3.7 KiB
PHP

<?php
namespace MediaWiki\Tests\EditPage;
use MediaWiki\MainConfigNames;
use MediaWiki\Permissions\UltimateAuthority;
use MediaWiki\Title\Title;
use MediaWikiIntegrationTestCase;
/**
* @covers \MediaWiki\EditPage\PreloadedContentBuilder
* @group Database
*/
class PreloadedContentBuilderTest extends MediaWikiIntegrationTestCase {
protected function setUp(): void {
$services = $this->getServiceContainer();
// Needed for the 'Default preload section' test case due to use of wfMessage()
$this->overrideConfigValue( MainConfigNames::UseDatabaseMessages, true );
$services->resetServiceForTesting( 'MessageCache' );
$this->preloadedContentBuilder = $services->getPreloadedContentBuilder();
}
public static function provideCases() {
// title, preload, preloadParams, section, pages, expectedContent
yield 'Non-existent page, no preload' =>
[ 'Does-not-exist-asdfasdf', null, [], null, [],
"" ];
yield 'Non-existent page, preload' =>
[ 'Does-not-exist-asdfasdf', 'Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
"Preload" ];
yield 'Non-existent page, preload with parameters' =>
[ 'Does-not-exist-asdfasdf', 'Template:Preloadparams', [ 'a', 'b' ], null, [ 'Template:Preloadparams' => 'Preload $1 $2' ],
"Preload a b" ];
yield 'Existing page content is ignored (it is not our responsibility)' =>
[ 'Exists', null, [], null, [ 'Exists' => 'Hello' ],
"" ];
yield 'Existing page content is ignored (it is not our responsibility), preload' =>
[ 'Exists', 'Template:Preload', [], null, [ 'Exists' => 'Hello', 'Template:Preload' => 'Preload' ],
"Preload" ];
yield 'Preload section' =>
[ 'Exists', 'Template:Preload', [], 'new', [ 'Exists' => 'Hello', 'Template:Preload' => 'Preload' ],
"Preload" ];
yield 'Default preload section' =>
[ 'Exists', null, [], 'new', [ 'Exists' => 'Hello', 'MediaWiki:Addsection-preload' => 'Preloadsection' ],
"Preloadsection" ];
yield 'Non-existent page in MediaWiki: namespace is prefilled with message' =>
[ 'MediaWiki:View', null, [], null, [],
"View" ];
yield 'Non-existent page in MediaWiki: namespace for non-existent message' =>
[ 'MediaWiki:Does-not-exist-asdfasdf', null, [], null, [],
"" ];
yield 'Non-existent page in MediaWiki: namespace does not support preload' =>
[ 'MediaWiki:View', 'Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
"View" ];
yield 'Non-existent message supports preload' =>
[ 'MediaWiki:Does-not-exist-asdfasdf', 'Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
"Preload" ];
yield 'JSON page in MediaWiki: namespace is prefilled with empty JSON' =>
[ 'MediaWiki:Foo.json', null, [], null, [],
"{}" ];
yield 'Preload using Special:MyLanguage' =>
[ 'Does-not-exist-asdfasdf', 'Special:MyLanguage/Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
"Preload" ];
yield 'Preload using a localisation message' =>
[ 'Does-not-exist-asdfasdf', 'MediaWiki:View', [], null, [],
"View" ];
yield 'Preload over redirect' =>
[ 'Does-not-exist-asdfasdf', 'Template:Preload2', [], null, [ 'Template:Preload' => 'Preload', 'Template:Preload2' => '#REDIRECT[[Template:Preload]]' ],
"Preload" ];
}
/**
* @dataProvider provideCases
*/
public function testGetPreloadedContent( $title, $preload, $preloadParams, $section, $pages, $expectedContent ) {
foreach ( $pages as $page => $content ) {
$this->editPage( $page, $content );
}
$content = $this->preloadedContentBuilder->getPreloadedContent(
Title::newFromText( $title )->toPageIdentity(),
new UltimateAuthority( $this->getTestUser()->getUser() ),
$preload,
$preloadParams,
$section
);
$this->assertEquals( $expectedContent, $content->serialize() );
}
}