3 namespace MediaWiki\Tests\EditPage
;
5 use MediaWiki\EditPage\PreloadedContentBuilder
;
6 use MediaWiki\MainConfigNames
;
7 use MediaWiki\Permissions\UltimateAuthority
;
8 use MediaWiki\Title\Title
;
9 use MediaWikiIntegrationTestCase
;
12 * @covers \MediaWiki\EditPage\PreloadedContentBuilder
15 class PreloadedContentBuilderTest
extends MediaWikiIntegrationTestCase
{
17 /** @var PreloadedContentBuilder */
18 private $preloadedContentBuilder;
20 protected function setUp(): void
{
21 $services = $this->getServiceContainer();
23 // Needed for the 'Default preload section' test case due to use of wfMessage()
24 $this->overrideConfigValue( MainConfigNames
::UseDatabaseMessages
, true );
25 $services->resetServiceForTesting( 'MessageCache' );
27 $this->preloadedContentBuilder
= $services->getPreloadedContentBuilder();
30 public static function provideCases() {
31 // title, preload, preloadParams, section, pages, expectedContent
33 yield
'Non-existent page, no preload' =>
34 [ 'Does-not-exist-asdfasdf', null, [], null, [],
36 yield
'Non-existent page, preload' =>
37 [ 'Does-not-exist-asdfasdf', 'Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
39 yield
'Non-existent page, preload with parameters' =>
40 [ 'Does-not-exist-asdfasdf', 'Template:Preloadparams', [ 'a', 'b' ], null, [ 'Template:Preloadparams' => 'Preload $1 $2' ],
43 yield
'Existing page content is ignored (it is not our responsibility)' =>
44 [ 'Exists', null, [], null, [ 'Exists' => 'Hello' ],
46 yield
'Existing page content is ignored (it is not our responsibility), preload' =>
47 [ 'Exists', 'Template:Preload', [], null, [ 'Exists' => 'Hello', 'Template:Preload' => 'Preload' ],
50 yield
'Preload section' =>
51 [ 'Exists', 'Template:Preload', [], 'new', [ 'Exists' => 'Hello', 'Template:Preload' => 'Preload' ],
53 yield
'Default preload section' =>
54 [ 'Exists', null, [], 'new', [ 'Exists' => 'Hello', 'MediaWiki:Addsection-preload' => 'Preloadsection' ],
57 yield
'Non-existent page in MediaWiki: namespace is prefilled with message' =>
58 [ 'MediaWiki:View', null, [], null, [],
60 yield
'Non-existent page in MediaWiki: namespace for non-existent message' =>
61 [ 'MediaWiki:Does-not-exist-asdfasdf', null, [], null, [],
63 yield
'Non-existent page in MediaWiki: namespace does not support preload' =>
64 [ 'MediaWiki:View', 'Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
66 yield
'Non-existent message supports preload' =>
67 [ 'MediaWiki:Does-not-exist-asdfasdf', 'Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
70 yield
'JSON page in MediaWiki: namespace is prefilled with empty JSON' =>
71 [ 'MediaWiki:Foo.json', null, [], null, [],
74 yield
'Preload using Special:MyLanguage' =>
75 [ 'Does-not-exist-asdfasdf', 'Special:MyLanguage/Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
78 yield
'Preload using a localisation message' =>
79 [ 'Does-not-exist-asdfasdf', 'MediaWiki:View', [], null, [],
81 yield
'Preload using a page in mediawiki namespace' =>
82 [ 'Does-not-exist-asdfasdf', 'MediaWiki:For-preloading', [], null, [ 'MediaWiki:For-preloading' => '<noinclude>Noinclude</noinclude><includeonly>Includeonly</includeonly>' ],
85 yield
'Preload over redirect' =>
86 [ 'Does-not-exist-asdfasdf', 'Template:Preload2', [], null, [ 'Template:Preload' => 'Preload', 'Template:Preload2' => '#REDIRECT[[Template:Preload]]' ],
91 * @dataProvider provideCases
93 public function testGetPreloadedContent( $title, $preload, $preloadParams, $section, $pages, $expectedContent ) {
94 foreach ( $pages as $page => $content ) {
95 $this->editPage( $page, $content );
98 $content = $this->preloadedContentBuilder
->getPreloadedContent(
99 Title
::newFromText( $title )->toPageIdentity(),
100 new UltimateAuthority( $this->getTestUser()->getUser() ),
106 $this->assertEquals( $expectedContent, $content->serialize() );