Merge "docs: Fix typo"
[mediawiki.git] / tests / phpunit / includes / editpage / PreloadedContentBuilderTest.php
blob7d65bec146a046d48c1b95d782f6d22825831aba
1 <?php
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;
11 /**
12 * @covers \MediaWiki\EditPage\PreloadedContentBuilder
13 * @group Database
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 );
26 $this->preloadedContentBuilder = $services->getPreloadedContentBuilder();
29 public static function provideCases() {
30 // title, preload, preloadParams, section, pages, expectedContent
32 yield 'Non-existent page, no preload' =>
33 [ 'Does-not-exist-asdfasdf', null, [], null, [],
34 "" ];
35 yield 'Non-existent page, preload' =>
36 [ 'Does-not-exist-asdfasdf', 'Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
37 "Preload" ];
38 yield 'Non-existent page, preload with parameters' =>
39 [ 'Does-not-exist-asdfasdf', 'Template:Preloadparams', [ 'a', 'b' ], null, [ 'Template:Preloadparams' => 'Preload $1 $2' ],
40 "Preload a b" ];
42 yield 'Existing page content is ignored (it is not our responsibility)' =>
43 [ 'Exists', null, [], null, [ 'Exists' => 'Hello' ],
44 "" ];
45 yield 'Existing page content is ignored (it is not our responsibility), preload' =>
46 [ 'Exists', 'Template:Preload', [], null, [ 'Exists' => 'Hello', 'Template:Preload' => 'Preload' ],
47 "Preload" ];
49 yield 'Preload section' =>
50 [ 'Exists', 'Template:Preload', [], 'new', [ 'Exists' => 'Hello', 'Template:Preload' => 'Preload' ],
51 "Preload" ];
52 yield 'Default preload section' =>
53 [ 'Exists', null, [], 'new', [ 'Exists' => 'Hello', 'MediaWiki:Addsection-preload' => 'Preloadsection' ],
54 "Preloadsection" ];
56 yield 'Non-existent page in MediaWiki: namespace is prefilled with message' =>
57 [ 'MediaWiki:View', null, [], null, [],
58 "View" ];
59 yield 'Non-existent page in MediaWiki: namespace for non-existent message' =>
60 [ 'MediaWiki:Does-not-exist-asdfasdf', null, [], null, [],
61 "" ];
62 yield 'Non-existent page in MediaWiki: namespace does not support preload' =>
63 [ 'MediaWiki:View', 'Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
64 "View" ];
65 yield 'Non-existent message supports preload' =>
66 [ 'MediaWiki:Does-not-exist-asdfasdf', 'Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
67 "Preload" ];
69 yield 'JSON page in MediaWiki: namespace is prefilled with empty JSON' =>
70 [ 'MediaWiki:Foo.json', null, [], null, [],
71 "{}" ];
73 yield 'Preload using Special:MyLanguage' =>
74 [ 'Does-not-exist-asdfasdf', 'Special:MyLanguage/Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
75 "Preload" ];
77 yield 'Preload using a localisation message' =>
78 [ 'Does-not-exist-asdfasdf', 'MediaWiki:View', [], null, [],
79 "View" ];
80 yield 'Preload using a page in mediawiki namespace' =>
81 [ 'Does-not-exist-asdfasdf', 'MediaWiki:For-preloading', [], null, [ 'MediaWiki:For-preloading' => '<noinclude>Noinclude</noinclude><includeonly>Includeonly</includeonly>' ],
82 "Includeonly" ];
84 yield 'Preload over redirect' =>
85 [ 'Does-not-exist-asdfasdf', 'Template:Preload2', [], null, [ 'Template:Preload' => 'Preload', 'Template:Preload2' => '#REDIRECT[[Template:Preload]]' ],
86 "Preload" ];
89 /**
90 * @dataProvider provideCases
92 public function testGetPreloadedContent( $title, $preload, $preloadParams, $section, $pages, $expectedContent ) {
93 foreach ( $pages as $page => $content ) {
94 $this->editPage( $page, $content );
97 $content = $this->preloadedContentBuilder->getPreloadedContent(
98 Title::newFromText( $title )->toPageIdentity(),
99 new UltimateAuthority( $this->getTestUser()->getUser() ),
100 $preload,
101 $preloadParams,
102 $section
105 $this->assertEquals( $expectedContent, $content->serialize() );