| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\EditPage; |
| 4 |
|
| 5 |
use MediaWiki\EditPage\PreloadedContentBuilder; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\Permissions\UltimateAuthority; |
| 8 |
use MediaWiki\Title\Title; |
| 9 |
use MediaWikiIntegrationTestCase; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class PreloadedContentBuilderTest extends MediaWikiIntegrationTestCase { |
| 16 |
|
| 17 |
|
| 18 |
private $preloadedContentBuilder; |
| 19 |
|
| 20 |
protected function setUp(): void { |
| 21 |
$services = $this->getServiceContainer(); |
| 22 |
|
| 23 |
|
| 24 |
$this->overrideConfigValue( MainConfigNames::UseDatabaseMessages, true ); |
| 25 |
|
| 26 |
$this->preloadedContentBuilder = $services->getPreloadedContentBuilder(); |
| 27 |
} |
| 28 |
|
| 29 |
public static function provideCases() { |
| 30 |
|
| 31 |
|
| 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" ]; |
| 41 |
|
| 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" ]; |
| 48 |
|
| 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" ]; |
| 55 |
|
| 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" ]; |
| 68 |
|
| 69 |
yield 'JSON page in MediaWiki: namespace is prefilled with empty JSON' => |
| 70 |
[ 'MediaWiki:Foo.json', null, [], null, [], |
| 71 |
"{}" ]; |
| 72 |
|
| 73 |
yield 'Preload using Special:MyLanguage' => |
| 74 |
[ 'Does-not-exist-asdfasdf', 'Special:MyLanguage/Template:Preload', [], null, [ 'Template:Preload' => 'Preload' ], |
| 75 |
"Preload" ]; |
| 76 |
|
| 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" ]; |
| 83 |
|
| 84 |
yield 'Preload over redirect' => |
| 85 |
[ 'Does-not-exist-asdfasdf', 'Template:Preload2', [], null, [ 'Template:Preload' => 'Preload', 'Template:Preload2' => '#REDIRECT[[Template:Preload]]' ], |
| 86 |
"Preload" ]; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function testGetPreloadedContent( $title, $preload, $preloadParams, $section, $pages, $expectedContent ) { |
| 93 |
foreach ( $pages as $page => $content ) { |
| 94 |
$this->editPage( $page, $content ); |
| 95 |
} |
| 96 |
|
| 97 |
$content = $this->preloadedContentBuilder->getPreloadedContent( |
| 98 |
Title::newFromText( $title )->toPageIdentity(), |
| 99 |
new UltimateAuthority( $this->getTestUser()->getUser() ), |
| 100 |
$preload, |
| 101 |
$preloadParams, |
| 102 |
$section |
| 103 |
); |
| 104 |
|
| 105 |
$this->assertEquals( $expectedContent, $content->serialize() ); |
| 106 |
} |
| 107 |
|
| 108 |
} |