Rename JsonUnserial… to JsonDeserial…
[mediawiki.git] / tests / phpunit / includes / editpage / PreloadedContentBuilderTest.php
blob0272fa822aeef4adbd78157578ad5c6733136e66
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 );
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, [],
35 "" ];
36 yield 'Non-existent page, preload' =>
37 [ 'Does-not-exist-asdfasdf', 'Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
38 "Preload" ];
39 yield 'Non-existent page, preload with parameters' =>
40 [ 'Does-not-exist-asdfasdf', 'Template:Preloadparams', [ 'a', 'b' ], null, [ 'Template:Preloadparams' => 'Preload $1 $2' ],
41 "Preload a b" ];
43 yield 'Existing page content is ignored (it is not our responsibility)' =>
44 [ 'Exists', null, [], null, [ 'Exists' => 'Hello' ],
45 "" ];
46 yield 'Existing page content is ignored (it is not our responsibility), preload' =>
47 [ 'Exists', 'Template:Preload', [], null, [ 'Exists' => 'Hello', 'Template:Preload' => 'Preload' ],
48 "Preload" ];
50 yield 'Preload section' =>
51 [ 'Exists', 'Template:Preload', [], 'new', [ 'Exists' => 'Hello', 'Template:Preload' => 'Preload' ],
52 "Preload" ];
53 yield 'Default preload section' =>
54 [ 'Exists', null, [], 'new', [ 'Exists' => 'Hello', 'MediaWiki:Addsection-preload' => 'Preloadsection' ],
55 "Preloadsection" ];
57 yield 'Non-existent page in MediaWiki: namespace is prefilled with message' =>
58 [ 'MediaWiki:View', null, [], null, [],
59 "View" ];
60 yield 'Non-existent page in MediaWiki: namespace for non-existent message' =>
61 [ 'MediaWiki:Does-not-exist-asdfasdf', null, [], null, [],
62 "" ];
63 yield 'Non-existent page in MediaWiki: namespace does not support preload' =>
64 [ 'MediaWiki:View', 'Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
65 "View" ];
66 yield 'Non-existent message supports preload' =>
67 [ 'MediaWiki:Does-not-exist-asdfasdf', 'Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
68 "Preload" ];
70 yield 'JSON page in MediaWiki: namespace is prefilled with empty JSON' =>
71 [ 'MediaWiki:Foo.json', null, [], null, [],
72 "{}" ];
74 yield 'Preload using Special:MyLanguage' =>
75 [ 'Does-not-exist-asdfasdf', 'Special:MyLanguage/Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ],
76 "Preload" ];
78 yield 'Preload using a localisation message' =>
79 [ 'Does-not-exist-asdfasdf', 'MediaWiki:View', [], null, [],
80 "View" ];
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>' ],
83 "Includeonly" ];
85 yield 'Preload over redirect' =>
86 [ 'Does-not-exist-asdfasdf', 'Template:Preload2', [], null, [ 'Template:Preload' => 'Preload', 'Template:Preload2' => '#REDIRECT[[Template:Preload]]' ],
87 "Preload" ];
90 /**
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() ),
101 $preload,
102 $preloadParams,
103 $section
106 $this->assertEquals( $expectedContent, $content->serialize() );