| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\EditPage; |
| 4 |
|
| 5 |
use File; |
| 6 |
use FileRepo; |
| 7 |
use MediaWiki\Config\HashConfig; |
| 8 |
use MediaWiki\EditPage\IntroMessageBuilder; |
| 9 |
use MediaWiki\MainConfigNames; |
| 10 |
use MediaWiki\Page\ProperPageIdentity; |
| 11 |
use MediaWiki\Permissions\UltimateAuthority; |
| 12 |
use MediaWiki\Title\Title; |
| 13 |
use MediaWiki\User\TempUser\TempUserCreator; |
| 14 |
use MediaWiki\User\User; |
| 15 |
use MediaWiki\User\UserFactory; |
| 16 |
use MediaWiki\User\UserIdentityValue; |
| 17 |
use MediaWikiIntegrationTestCase; |
| 18 |
use MockMessageLocalizer; |
| 19 |
use RepoGroup; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class IntroMessageBuilderTest extends MediaWikiIntegrationTestCase { |
| 26 |
|
| 27 |
|
| 28 |
private $introMessageBuilder; |
| 29 |
|
| 30 |
protected function setUp(): void { |
| 31 |
$services = $this->getServiceContainer(); |
| 32 |
|
| 33 |
$config = new HashConfig( [ |
| 34 |
MainConfigNames::AllowUserCss => true, |
| 35 |
MainConfigNames::AllowUserJs => true, |
| 36 |
] ); |
| 37 |
|
| 38 |
$repoGroup = $this->createMock( RepoGroup::class ); |
| 39 |
$repoGroup |
| 40 |
->method( 'findFile' ) |
| 41 |
->willReturnCallback( function ( ProperPageIdentity $title ) { |
| 42 |
if ( $title->getDBkey() === 'Shared.png' || $title->getDBkey() === 'Shared-with-desc.png' ) { |
| 43 |
$file = $this->createMock( File::class ); |
| 44 |
$file->method( 'isLocal' )->willReturn( false ); |
| 45 |
$file->method( 'getDescriptionUrl' )->willReturn( 'https://example.com/' ); |
| 46 |
$repo = $this->createMock( FileRepo::class ); |
| 47 |
$repo->method( 'getDisplayName' )->willReturn( '' ); |
| 48 |
$file->method( 'getRepo' )->willReturn( $repo ); |
| 49 |
return $file; |
| 50 |
} |
| 51 |
return null; |
| 52 |
} ); |
| 53 |
|
| 54 |
$tempUserCreator = $this->createMock( TempUserCreator::class ); |
| 55 |
$tempUserCreator |
| 56 |
->method( 'isAutoCreateAction' ) |
| 57 |
->willReturn( false ); |
| 58 |
|
| 59 |
$userFactory = $this->createMock( UserFactory::class ); |
| 60 |
$userFactory |
| 61 |
->method( 'newFromName' ) |
| 62 |
->willReturnCallback( static function ( $name ) { |
| 63 |
$user = new User; |
| 64 |
$user->load(); |
| 65 |
$user->mName = $name; |
| 66 |
$user->mId = in_array( $name, [ 'Alice', 'Bob' ] ) ? 1 : 0; |
| 67 |
$user->mDataLoaded = true; |
| 68 |
return $user; |
| 69 |
} ); |
| 70 |
|
| 71 |
$this->introMessageBuilder = new IntroMessageBuilder( |
| 72 |
$config, |
| 73 |
$services->getLinkRenderer(), |
| 74 |
$services->getPermissionManager(), |
| 75 |
$services->getUserNameUtils(), |
| 76 |
$tempUserCreator, |
| 77 |
$userFactory, |
| 78 |
$services->getRestrictionStore(), |
| 79 |
$services->getDatabaseBlockStore(), |
| 80 |
$services->getReadOnlyMode(), |
| 81 |
$services->getSpecialPageFactory(), |
| 82 |
$repoGroup, |
| 83 |
$services->getNamespaceInfo(), |
| 84 |
$services->getSkinFactory(), |
| 85 |
$services->getConnectionProvider(), |
| 86 |
$services->getUrlUtils() |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
public static function provideCases() { |
| 91 |
|
| 92 |
$errorClass = 'cdx-message--error'; |
| 93 |
$warningClass = 'cdx-message--warning'; |
| 94 |
yield 'Main namespace has no default message' => |
| 95 |
[ 'Hello', null, 'Alice', null, [ 'Hello' => '' ], |
| 96 |
[], null ]; |
| 97 |
|
| 98 |
yield 'Logged-out warning' => |
| 99 |
[ 'Hello', null, null, null, [ 'Hello' => '' ], |
| 100 |
[ "anoneditwarning" ], $warningClass ]; |
| 101 |
|
| 102 |
|
| 103 |
yield 'User JavaScript requires alert as well as code-specific message' => |
| 104 |
[ 'User:Bob/common.js', null, 'Alice', null, [ 'User:Bob/common.js' => '' ], |
| 105 |
[ "userjsdangerous", "editpage-code-message" ], $errorClass ]; |
| 106 |
|
| 107 |
yield 'Inform users that their JS is public and suggest guidelines' => |
| 108 |
[ 'User:Bob/common.js', null, 'Bob', null, [ 'User:Bob/common.js' => '' ], |
| 109 |
[ "userjsispublic", "userjsdangerous", "editpage-code-message", "userjsyoucanpreview" ], $errorClass ]; |
| 110 |
|
| 111 |
yield 'MediaWiki: namespace JSON requires alert' => |
| 112 |
[ 'MediaWiki:Map.json', null, 'Alice', null, [], |
| 113 |
[ "editinginterface", "newarticletext" ], $errorClass ]; |
| 114 |
|
| 115 |
yield 'MediaWiki: namespace message requires alert' => |
| 116 |
[ 'MediaWiki:Does-not-exist-asdfasdf', null, 'Alice', null, [], |
| 117 |
[ "editinginterface", "newarticletext" ], $errorClass ]; |
| 118 |
|
| 119 |
yield 'Translateable MediaWiki: namespace message links to Translatewiki' => |
| 120 |
[ 'MediaWiki:View', null, 'Alice', null, [], |
| 121 |
[ "editinginterface", "translateinterface", "newarticletext" ], $errorClass ]; |
| 122 |
|
| 123 |
|
| 124 |
yield 'Neither shared not local file exists' => |
| 125 |
[ 'File:Missing.png', null, 'Alice', null, [], |
| 126 |
[ "newarticletext" ], "mw-newarticletext" ]; |
| 127 |
|
| 128 |
yield 'Shared file exists, local description does not exist' => |
| 129 |
[ 'File:Shared.png', null, 'Alice', null, [], |
| 130 |
[ "sharedupload-desc-create", "newarticletext" ], "mw-sharedupload-desc-create" ]; |
| 131 |
|
| 132 |
yield 'Shared file exists, local description exists' => |
| 133 |
[ 'File:Shared-with-desc.png', null, 'Alice', null, [ 'File:Shared-with-desc.png' => 'Test' ], |
| 134 |
[ "sharedupload-desc-edit" ], "mw-sharedupload-desc-edit" ]; |
| 135 |
|
| 136 |
|
| 137 |
yield 'User does not exist' => |
| 138 |
[ 'User:Foo', null, 'Alice', null, [], |
| 139 |
[ "userpage-userdoesnotexist", "newarticletext" ], "mw-newarticletext" ]; |
| 140 |
|
| 141 |
yield 'User exists' => |
| 142 |
[ 'User:Bob', null, 'Alice', null, [ 'User:Bob' => '' ], |
| 143 |
[], null ]; |
| 144 |
|
| 145 |
yield 'IP user exists, I guess' => |
| 146 |
[ 'User:1.2.3.4', null, 'Alice', null, [ 'User:1.2.3.4' => '' ], |
| 147 |
[], null ]; |
| 148 |
|
| 149 |
|
| 150 |
yield 'Default edit intro for missing page' => |
| 151 |
[ 'Does-not-exist-asdfasdf', null, 'Alice', null, [], |
| 152 |
[ "newarticletext" ], "mw-newarticletext" ]; |
| 153 |
|
| 154 |
yield 'The "editintro" parameter replaces the default edit intro' => |
| 155 |
[ 'Does-not-exist-asdfasdf', null, 'Alice', 'Template:Editintro', [ 'Template:Editintro' => '(editintro)' ], |
| 156 |
[ "editintro" ], null ]; |
| 157 |
|
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public function testGetIntroMessages( $title, $oldid, $user, $editIntro, $pages, $expectedMessages, $expectedWrap ) { |
| 165 |
foreach ( $pages as $page => $content ) { |
| 166 |
$this->editPage( $page, $content ); |
| 167 |
} |
| 168 |
|
| 169 |
if ( $user ) { |
| 170 |
$userObj = UserIdentityValue::newRegistered( 1, $user ); |
| 171 |
} else { |
| 172 |
$userObj = UserIdentityValue::newAnonymous( '1.2.3.4' ); |
| 173 |
} |
| 174 |
|
| 175 |
$parameters = [ |
| 176 |
|
| 177 |
[ 'editnotice-notext', 'editpage-head-copy-warn' ], |
| 178 |
new MockMessageLocalizer( 'qqx' ), |
| 179 |
Title::newFromText( $title )->toPageIdentity(), |
| 180 |
null, |
| 181 |
new UltimateAuthority( $userObj ), |
| 182 |
$editIntro, |
| 183 |
null, |
| 184 |
false |
| 185 |
]; |
| 186 |
|
| 187 |
$result = $this->introMessageBuilder->getIntroMessages( IntroMessageBuilder::LESS_FRAMES, ...$parameters ); |
| 188 |
$resultLessFrames = implode( '', $result ); |
| 189 |
$result = $this->introMessageBuilder->getIntroMessages( IntroMessageBuilder::MORE_FRAMES, ...$parameters ); |
| 190 |
$resultMoreFrames = implode( '', $result ); |
| 191 |
|
| 192 |
|
| 193 |
preg_match_all( '/[(](.+?)[):]/', $resultLessFrames, $matches, PREG_PATTERN_ORDER ); |
| 194 |
$this->assertEquals( $expectedMessages, $matches[1], 'Messages (less frames)' ); |
| 195 |
if ( $expectedWrap !== null ) { |
| 196 |
$this->assertStringNotContainsString( $expectedWrap, $resultLessFrames, 'No frames (less frames)' ); |
| 197 |
} |
| 198 |
|
| 199 |
preg_match_all( '/[(](.+?)[):]/', $resultMoreFrames, $matches, PREG_PATTERN_ORDER ); |
| 200 |
$this->assertEquals( $expectedMessages, $matches[1], 'Messages (more frames)' ); |
| 201 |
if ( $expectedWrap !== null ) { |
| 202 |
$this->assertStringContainsString( $expectedWrap, $resultMoreFrames, 'Frames (more frames)' ); |
| 203 |
} |
| 204 |
|
| 205 |
if ( $expectedWrap == null ) { |
| 206 |
$this->assertEquals( $resultLessFrames, $resultMoreFrames, 'No frames' ); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
} |