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