1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
<?php
namespace MediaWiki\Tests\Integration\User\TempUser;
use MediaWiki\Permissions\SimpleAuthority;
use MediaWiki\Tests\MockDatabase;
use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
use MediaWiki\User\TempUser\Pattern;
use MediaWiki\User\UserIdentityValue;
use Wikimedia\Rdbms\IExpression;
use Wikimedia\TestingAccessWrapper;
use Wikimedia\Timestamp\ConvertibleTimestamp;
/**
* @covers \MediaWiki\User\TempUser\RealTempUserConfig
* @group Database
*/
class RealTempUserConfigTest extends \MediaWikiIntegrationTestCase {
use TempUserTestTrait;
public static function provideIsAutoCreateAction() {
return [
'disabled' => [
'enabled' => false,
'configOverrides' => [],
'action' => 'edit',
'expected' => false
],
'disabled by action' => [
'enabled' => true,
'configOverrides' => [ 'actions' => [] ],
'action' => 'edit',
'expected' => false
],
'enabled' => [
'enabled' => true,
'configOverrides' => [],
'action' => 'edit',
'expected' => true
],
// Create isn't an action in the ActionFactory sense, but is is an
// action in PermissionManager
'create' => [
'enabled' => true,
'configOverrides' => [],
'action' => 'create',
'expected' => true
],
'unknown action' => [
'enabled' => true,
'configOverrides' => [],
'action' => 'foo',
'expected' => false
],
];
}
/**
* @dataProvider provideIsAutoCreateAction
* @param bool $enabled
* @param array $configOverrides
* @param string $action
* @param bool $expected
*/
public function testIsAutoCreateAction( $enabled, $configOverrides, $action, $expected ) {
if ( $enabled ) {
$this->enableAutoCreateTempUser( $configOverrides );
} else {
$this->disableAutoCreateTempUser( $configOverrides );
}
$tuc = $this->getServiceContainer()->getTempUserConfig();
$this->assertSame( $expected, $tuc->isAutoCreateAction( $action ) );
}
public static function provideShouldAutoCreate() {
return [
'enabled' => [
'enabled' => true,
'id' => 0,
'rights' => [ 'createaccount' ],
'action' => 'edit',
'expected' => true
],
'disabled by config' => [
'enabled' => false,
'id' => 0,
'rights' => [ 'createaccount' ],
'action' => 'edit',
'expected' => false
],
'logged in' => [
'enabled' => true,
'id' => 1,
'rights' => [ 'createaccount' ],
'action' => 'edit',
'expected' => false
],
'no createaccount right' => [
'enabled' => true,
'id' => 0,
'rights' => [ 'edit' ],
'action' => 'edit',
'expected' => false
],
'wrong action' => [
'enabled' => true,
'id' => 0,
'rights' => [ 'createaccount' ],
'action' => 'upload',
'expected' => false
],
];
}
/**
* @dataProvider provideShouldAutoCreate
* @param bool $enabled
* @param int $id
* @param string[] $rights
* @param string $action
* @param bool $expected
*/
public function testShouldAutoCreate( $enabled, $id, $rights, $action, $expected ) {
if ( $enabled ) {
$this->enableAutoCreateTempUser();
} else {
$this->disableAutoCreateTempUser();
}
$tuc = $this->getServiceContainer()->getTempUserConfig();
$user = new SimpleAuthority(
new UserIdentityValue( $id, $id ? 'Test' : '127.0.0.1' ),
$rights
);
$this->assertSame( $expected, $tuc->shouldAutoCreate( $user, $action ) );
}
public static function provideIsTempName() {
return [
'disabled' => [
'enabled' => false,
'name' => '~Some user',
'expected' => false,
],
'default mismatch' => [
'enabled' => true,
'name' => 'Test',
'expected' => false,
],
'default match' => [
'enabled' => true,
'name' => '~Some user',
'expected' => true,
],
];
}
/**
* @dataProvider provideIsTempName
* @param bool $enabled
* @param string $name
* @param bool $expected
*/
public function testIsTempName( $enabled, $name, $expected ) {
if ( $enabled ) {
$this->enableAutoCreateTempUser();
} else {
$this->disableAutoCreateTempUser();
}
$tuc = $this->getServiceContainer()->getTempUserConfig();
$this->assertSame( $expected, $tuc->isTempName( $name ) );
}
/** @dataProvider provideGetPlaceholderName */
public function testGetPlaceholderName( $useYear, $expected ) {
$this->enableAutoCreateTempUser( [ 'serialProvider' => [ 'type' => 'local', 'useYear' => $useYear ] ] );
ConvertibleTimestamp::setFakeTime( '20210101000000' );
$this->assertSame( $expected, $this->getServiceContainer()->getTempUserConfig()->getPlaceholderName() );
}
public static function provideGetPlaceholderName() {
return [
'no year' => [ false, '~*' ],
'with year' => [ true, '~2021-*' ],
];
}
public static function provideIsReservedName() {
return [
'no matchPattern when disabled' => [
'enabled' => false,
'configOverrides' => [ 'reservedPattern' => null ],
'name' => '~39',
'expected' => false,
],
'matchPattern match' => [
'enabled' => true,
'configOverrides' => [],
'name' => '~39',
'expected' => true,
],
'genPattern match' => [
'enabled' => true,
'configOverrides' => [ 'matchPattern' => null ],
'name' => '~39',
'expected' => true,
],
'reservedPattern match with enabled=false' => [
'enabled' => false,
'configOverrides' => [ 'reservedPattern' => '~$1' ],
'name' => '~Foo*',
'expected' => true
]
];
}
/**
* @dataProvider provideIsReservedName
* @param bool $enabled
* @param array $configOverrides
* @param string $name
* @param bool $expected
*/
public function testIsReservedName( $enabled, $configOverrides, $name, $expected ) {
if ( $enabled ) {
$this->enableAutoCreateTempUser( $configOverrides );
} else {
$this->disableAutoCreateTempUser( $configOverrides );
}
$tuc = $this->getServiceContainer()->getTempUserConfig();
$this->assertSame( $expected, $tuc->isReservedName( $name ) );
}
public function testGetMatchPatterns() {
$this->enableAutoCreateTempUser( [ 'matchPattern' => [ '*$1', '~$1' ] ] );
$tuc = $this->getServiceContainer()->getTempUserConfig();
$this->assertCount( 2, $tuc->getMatchPatterns() );
$actualPatterns = array_map( static function ( Pattern $pattern ) {
return TestingAccessWrapper::newFromObject( $pattern )->pattern;
}, $tuc->getMatchPatterns() );
$this->assertArrayEquals( [ '*$1', '~$1' ], $actualPatterns );
}
public function testGetMatchPattern() {
$this->hideDeprecated( 'MediaWiki\User\TempUser\RealTempUserConfig::getMatchPattern' );
$this->enableAutoCreateTempUser( [ 'matchPattern' => [ '*$1', '~$1' ] ] );
$tuc = $this->getServiceContainer()->getTempUserConfig();
$this->assertSame( '*$1', TestingAccessWrapper::newFromObject( $tuc->getMatchPattern() )->pattern );
}
public function testGetMatchCondition() {
$db = new MockDatabase;
$this->enableAutoCreateTempUser( [ 'matchPattern' => [ '*$1', '~$1' ] ] );
$tuc = $this->getServiceContainer()->getTempUserConfig();
$this->assertEquals(
"(foo LIKE '*%' ESCAPE '`' OR foo LIKE '~%' ESCAPE '`')",
$tuc->getMatchCondition( $db, 'foo', IExpression::LIKE )->toSql( $db ),
'LIKE allows any of the patterns'
);
$this->assertEquals(
"(foo NOT LIKE '*%' ESCAPE '`' AND foo NOT LIKE '~%' ESCAPE '`')",
$tuc->getMatchCondition( $db, 'foo', IExpression::NOT_LIKE )->toSql( $db ),
'NOT LIKE disallows all of the patterns'
);
$this->enableAutoCreateTempUser( [ 'matchPattern' => [ '*$1' ] ] );
$tuc = $this->getServiceContainer()->getTempUserConfig();
$this->assertEquals(
"foo LIKE '*%' ESCAPE '`'",
$tuc->getMatchCondition( $db, 'foo', IExpression::LIKE )->toSql( $db ),
'With a single pattern, parentheses are omitted'
);
$this->assertEquals(
"foo NOT LIKE '*%' ESCAPE '`'",
$tuc->getMatchCondition( $db, 'foo', IExpression::NOT_LIKE )->toSql( $db ),
'With a single pattern, parentheses are omitted'
);
}
}
|