| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\User; |
| 4 |
|
| 5 |
use CannotCreateActorException; |
| 6 |
use InvalidArgumentException; |
| 7 |
use MediaWiki\DAO\WikiAwareEntity; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\MediaWikiServices; |
| 10 |
use MediaWiki\Tests\User\TempUser\TempUserTestTrait; |
| 11 |
use MediaWiki\User\ActorStore; |
| 12 |
use MediaWiki\User\User; |
| 13 |
use MediaWiki\User\UserIdentity; |
| 14 |
use MediaWiki\User\UserIdentityValue; |
| 15 |
use MediaWiki\User\UserSelectQueryBuilder; |
| 16 |
use stdClass; |
| 17 |
use Wikimedia\Assert\PreconditionException; |
| 18 |
use Wikimedia\Rdbms\IDatabase; |
| 19 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class ActorStoreTest extends ActorStoreTestBase { |
| 26 |
use TempUserTestTrait; |
| 27 |
|
| 28 |
public static function provideGetActorById() { |
| 29 |
yield 'getActorById, registered' => [ |
| 30 |
42, |
| 31 |
new UserIdentityValue( 24, 'TestUser' ), |
| 32 |
]; |
| 33 |
yield 'getActorById, anon' => [ |
| 34 |
43, |
| 35 |
new UserIdentityValue( 0, self::IP ), |
| 36 |
]; |
| 37 |
yield 'getActorById, non-existent' => [ |
| 38 |
4321231, |
| 39 |
null, |
| 40 |
]; |
| 41 |
yield 'getActorById, zero' => [ |
| 42 |
0, |
| 43 |
null, |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function testGetActorById( $argument, ?UserIdentity $expected ) { |
| 51 |
$actor = $this->getStore()->getActorById( $argument, $this->getDb() ); |
| 52 |
if ( $expected ) { |
| 53 |
$this->assertNotNull( $actor ); |
| 54 |
$this->assertSameActors( $expected, $actor ); |
| 55 |
|
| 56 |
|
| 57 |
$cachedActor = $this->getStore()->getActorById( $argument, $this->getDb() ); |
| 58 |
$this->assertSame( $actor, $cachedActor ); |
| 59 |
} else { |
| 60 |
$this->assertNull( $actor ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
public static function provideGetActorByMethods() { |
| 65 |
yield 'getUserIdentityByName, registered' => [ |
| 66 |
'getUserIdentityByName', |
| 67 |
'TestUser', |
| 68 |
new UserIdentityValue( 24, 'TestUser' ), |
| 69 |
]; |
| 70 |
yield 'getUserIdentityByName, non-existent' => [ |
| 71 |
'getUserIdentityByName', |
| 72 |
'TestUser_I_Do_Not_Exist', |
| 73 |
null, |
| 74 |
]; |
| 75 |
yield 'getUserIdentityByName, anon' => [ |
| 76 |
'getUserIdentityByName', |
| 77 |
self::IP, |
| 78 |
new UserIdentityValue( 0, self::IP ), |
| 79 |
]; |
| 80 |
yield 'getUserIdentityByName, anon, non-canonicalized IP' => [ |
| 81 |
'getUserIdentityByName', |
| 82 |
strtolower( self::IP ), |
| 83 |
new UserIdentityValue( 0, self::IP ), |
| 84 |
]; |
| 85 |
yield 'getUserIdentityByName, user name 0' => [ |
| 86 |
'getUserIdentityByName', |
| 87 |
'0', |
| 88 |
new UserIdentityValue( 26, '0' ), |
| 89 |
]; |
| 90 |
yield 'getUserIdentityByName, empty' => [ |
| 91 |
'getUserIdentityByName', |
| 92 |
'', |
| 93 |
null, |
| 94 |
]; |
| 95 |
yield 'getUserIdentityByName, ip range' => [ |
| 96 |
'getUserIdentityByName', |
| 97 |
'1.1.1.1/1', |
| 98 |
null, |
| 99 |
]; |
| 100 |
yield 'getUserIdentityByUserId, registered' => [ |
| 101 |
'getUserIdentityByUserId', |
| 102 |
24, |
| 103 |
new UserIdentityValue( 24, 'TestUser' ), |
| 104 |
]; |
| 105 |
yield 'getUserIdentityByUserId, non-exitent' => [ |
| 106 |
'getUserIdentityByUserId', |
| 107 |
2412312, |
| 108 |
null, |
| 109 |
]; |
| 110 |
yield 'getUserIdentityByUserId, zero' => [ |
| 111 |
'getUserIdentityByUserId', |
| 112 |
0, |
| 113 |
null, |
| 114 |
]; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function testGetActorByMethods( string $method, $argument, ?UserIdentity $expected ) { |
| 121 |
$actor = $this->getStore()->$method( $argument ); |
| 122 |
if ( $expected ) { |
| 123 |
$this->assertNotNull( $actor ); |
| 124 |
$this->assertSameActors( $expected, $actor ); |
| 125 |
|
| 126 |
|
| 127 |
$cachedActor = $this->getStore()->$method( $argument ); |
| 128 |
$this->assertSame( $actor, $cachedActor ); |
| 129 |
} else { |
| 130 |
$this->assertNull( $actor ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
public static function provideUserIdentityValues() { |
| 135 |
yield [ new UserIdentityValue( 24, 'TestUser' ) ]; |
| 136 |
yield [ new UserIdentityValue( 0, self::IP ) ]; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function testSequentialCacheRetrieval( UserIdentity $expected ) { |
| 144 |
if ( $expected->getId() === 0 ) { |
| 145 |
$this->disableAutoCreateTempUser(); |
| 146 |
} |
| 147 |
|
| 148 |
$actorId = $this->getStore()->findActorId( $expected, $this->getDb() ); |
| 149 |
|
| 150 |
$cachedActorId = $this->getStore()->findActorId( $expected, $this->getDb() ); |
| 151 |
$this->assertSame( $actorId, $cachedActorId ); |
| 152 |
|
| 153 |
$cachedActorId = $this->getStore()->acquireActorId( $expected, $this->getDb() ); |
| 154 |
$this->assertSame( $actorId, $cachedActorId ); |
| 155 |
|
| 156 |
$cached = $this->getStore()->getActorById( $actorId, $this->getDb() ); |
| 157 |
$this->assertNotNull( $cached ); |
| 158 |
$this->assertSameActors( $expected, $cached ); |
| 159 |
|
| 160 |
$cached = $this->getStore()->getUserIdentityByName( $expected->getName() ); |
| 161 |
$this->assertNotNull( $cached ); |
| 162 |
$this->assertSameActors( $expected, $cached ); |
| 163 |
|
| 164 |
$userId = $expected->getId(); |
| 165 |
if ( $userId ) { |
| 166 |
$cached = $this->getStore()->getUserIdentityByUserId( $userId ); |
| 167 |
$this->assertNotNull( $cached ); |
| 168 |
$this->assertSameActors( $expected, $cached ); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
public function testGetActorByIdRealUser() { |
| 173 |
$user = $this->getTestUser()->getUser(); |
| 174 |
$actor = $this->getStore()->getActorById( $user->getActorId(), $this->getDb() ); |
| 175 |
$this->assertSameActors( $user, $actor ); |
| 176 |
} |
| 177 |
|
| 178 |
public function testgetUserIdentityByNameRealUser() { |
| 179 |
$user = $this->getTestUser()->getUser(); |
| 180 |
$actor = $this->getStore()->getUserIdentityByName( $user->getName() ); |
| 181 |
$this->assertSameActors( $user, $actor ); |
| 182 |
} |
| 183 |
|
| 184 |
public function testGetUserIdentityByUserIdRealUser() { |
| 185 |
$user = $this->getTestUser()->getUser(); |
| 186 |
$actor = $this->getStore()->getUserIdentityByUserId( $user->getId() ); |
| 187 |
$this->assertSameActors( $user, $actor ); |
| 188 |
} |
| 189 |
|
| 190 |
public static function provideNewActorFromRow() { |
| 191 |
yield 'full row' => [ |
| 192 |
UserIdentity::LOCAL, |
| 193 |
(object)[ 'actor_id' => 42, 'actor_name' => 'TestUser', 'actor_user' => 24 ], |
| 194 |
new UserIdentityValue( 24, 'TestUser' ), |
| 195 |
]; |
| 196 |
yield 'full row, strings' => [ |
| 197 |
UserIdentity::LOCAL, |
| 198 |
(object)[ 'actor_id' => '42', 'actor_name' => 'TestUser', 'actor_user' => '24' ], |
| 199 |
new UserIdentityValue( 24, 'TestUser' ), |
| 200 |
]; |
| 201 |
yield 'null user' => [ |
| 202 |
UserIdentity::LOCAL, |
| 203 |
(object)[ 'actor_id' => '42', 'actor_name' => 'TestUser', 'actor_user' => null ], |
| 204 |
new UserIdentityValue( 0, 'TestUser' ), |
| 205 |
]; |
| 206 |
yield 'zero user' => [ |
| 207 |
UserIdentity::LOCAL, |
| 208 |
(object)[ 'actor_id' => '42', 'actor_name' => 'TestUser', 'actor_user' => 0 ], |
| 209 |
new UserIdentityValue( 0, 'TestUser' ), |
| 210 |
]; |
| 211 |
yield 'cross-wiki' => [ |
| 212 |
'acmewiki', |
| 213 |
(object)[ 'actor_id' => 42, 'actor_name' => 'TestUser', 'actor_user' => 24 ], |
| 214 |
new UserIdentityValue( 24, 'TestUser', 'acmewiki' ), |
| 215 |
]; |
| 216 |
yield 'user name 0' => [ |
| 217 |
UserIdentity::LOCAL, |
| 218 |
(object)[ 'actor_id' => '46', 'actor_name' => '0', 'actor_user' => 26 ], |
| 219 |
new UserIdentityValue( 26, '0' ), |
| 220 |
]; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function testNewActorFromRow( $wikiId, stdClass $row, UserIdentity $expected ) { |
| 227 |
$actor = $this->getStore( $wikiId )->newActorFromRow( $row ); |
| 228 |
$this->assertSameActors( $expected, $actor, $wikiId ); |
| 229 |
} |
| 230 |
|
| 231 |
public static function provideNewActorFromRow_exception() { |
| 232 |
yield 'null actor' => [ |
| 233 |
(object)[ 'actor_id' => '0', 'actor_name' => 'TestUser', 'actor_user' => 0 ], |
| 234 |
]; |
| 235 |
yield 'zero actor' => [ |
| 236 |
(object)[ 'actor_id' => null, 'actor_name' => 'TestUser', 'actor_user' => 0 ], |
| 237 |
]; |
| 238 |
yield 'empty name' => [ |
| 239 |
(object)[ 'actor_id' => '10', 'actor_name' => '', 'actor_user' => 15 ], |
| 240 |
]; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public function testNewActorFromRow_exception( stdClass $row ) { |
| 247 |
$this->expectException( InvalidArgumentException::class ); |
| 248 |
$this->getStore()->newActorFromRow( $row ); |
| 249 |
} |
| 250 |
|
| 251 |
public static function provideNewActorFromRowFields() { |
| 252 |
yield 'full row' => [ |
| 253 |
UserIdentity::LOCAL, |
| 254 |
42, |
| 255 |
'TestUser', |
| 256 |
24, |
| 257 |
new UserIdentityValue( 24, 'TestUser' ), |
| 258 |
]; |
| 259 |
yield 'full row, strings' => [ |
| 260 |
UserIdentity::LOCAL, |
| 261 |
'42', |
| 262 |
'TestUser', |
| 263 |
'24', |
| 264 |
new UserIdentityValue( 24, 'TestUser' ), |
| 265 |
]; |
| 266 |
yield 'null user' => [ |
| 267 |
UserIdentity::LOCAL, |
| 268 |
42, |
| 269 |
'TestUser', |
| 270 |
null, |
| 271 |
new UserIdentityValue( 0, 'TestUser' ), |
| 272 |
]; |
| 273 |
yield 'zero user' => [ |
| 274 |
UserIdentity::LOCAL, |
| 275 |
42, |
| 276 |
'TestUser', |
| 277 |
0, |
| 278 |
new UserIdentityValue( 0, 'TestUser' ), |
| 279 |
]; |
| 280 |
yield 'user name 0' => [ |
| 281 |
UserIdentity::LOCAL, |
| 282 |
46, |
| 283 |
'0', |
| 284 |
26, |
| 285 |
new UserIdentityValue( 26, '0' ), |
| 286 |
]; |
| 287 |
yield 'cross-wiki' => [ |
| 288 |
'acmewiki', |
| 289 |
42, |
| 290 |
'TestUser', |
| 291 |
0, |
| 292 |
new UserIdentityValue( 0, 'TestUser', 'acmewiki' ), |
| 293 |
]; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
public function testNewActorFromRowFields( $wikiId, $actorId, $name, $userId, UserIdentity $expected ) { |
| 300 |
$actor = $this->getStore( $wikiId )->newActorFromRowFields( $userId, $name, $actorId ); |
| 301 |
$this->assertSameActors( $expected, $actor, $wikiId ); |
| 302 |
} |
| 303 |
|
| 304 |
public static function provideNewActorFromRowFields_exception() { |
| 305 |
yield 'empty name' => [ |
| 306 |
42, |
| 307 |
'', |
| 308 |
24, |
| 309 |
]; |
| 310 |
yield 'null name' => [ |
| 311 |
42, |
| 312 |
null, |
| 313 |
24, |
| 314 |
]; |
| 315 |
yield 'null actor' => [ |
| 316 |
null, |
| 317 |
'TestUser', |
| 318 |
null, |
| 319 |
]; |
| 320 |
yield 'zero actor' => [ |
| 321 |
0, |
| 322 |
'TestUser', |
| 323 |
null, |
| 324 |
]; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public function testNewActorFromRowFields_exception( $actorId, $name, $userId ) { |
| 331 |
$this->expectException( InvalidArgumentException::class ); |
| 332 |
$this->getStore()->newActorFromRowFields( $userId, $name, $actorId ); |
| 333 |
} |
| 334 |
|
| 335 |
public static function provideFindActorId() { |
| 336 |
yield 'anon, local' => [ |
| 337 |
static function () { |
| 338 |
return new UserIdentityValue( 0, self::IP ); |
| 339 |
}, |
| 340 |
43, |
| 341 |
]; |
| 342 |
yield 'anon, non-canonical, local' => [ |
| 343 |
static function () { |
| 344 |
return new UserIdentityValue( 0, strtolower( self::IP ) ); |
| 345 |
}, |
| 346 |
43, |
| 347 |
]; |
| 348 |
yield 'registered, local' => [ |
| 349 |
static function () { |
| 350 |
return new UserIdentityValue( 24, 'TestUser' ); |
| 351 |
}, |
| 352 |
42, |
| 353 |
]; |
| 354 |
yield 'registered, zero user name' => [ |
| 355 |
static function () { |
| 356 |
return new UserIdentityValue( 26, '0' ); |
| 357 |
}, |
| 358 |
46, |
| 359 |
]; |
| 360 |
yield 'anon, non-existent, local' => [ |
| 361 |
static function () { |
| 362 |
return new UserIdentityValue( 0, '127.1.2.3' ); |
| 363 |
}, |
| 364 |
null, |
| 365 |
]; |
| 366 |
yield 'registered, non-existent, local' => [ |
| 367 |
static function () { |
| 368 |
return new UserIdentityValue( 51, 'DoNotExist' ); |
| 369 |
}, |
| 370 |
null, |
| 371 |
]; |
| 372 |
yield 'external, local' => [ |
| 373 |
static function () { |
| 374 |
return new UserIdentityValue( 0, 'acme>TestUser' ); |
| 375 |
}, |
| 376 |
45, |
| 377 |
]; |
| 378 |
yield 'anon User, local' => [ |
| 379 |
static function ( MediaWikiServices $serviceContainer ) { |
| 380 |
return $serviceContainer->getUserFactory()->newAnonymous( self::IP ); |
| 381 |
}, |
| 382 |
43, |
| 383 |
]; |
| 384 |
yield 'anon User, non-canonical, local' => [ |
| 385 |
static function ( MediaWikiServices $serviceContainer ) { |
| 386 |
return $serviceContainer->getUserFactory()->newAnonymous( strtolower( self::IP ) ); |
| 387 |
}, |
| 388 |
43, |
| 389 |
]; |
| 390 |
yield 'anon User, non-existent, local' => [ |
| 391 |
static function ( MediaWikiServices $serviceContainer ) { |
| 392 |
return $serviceContainer->getUserFactory()->newAnonymous( '127.1.2.3' ); |
| 393 |
}, |
| 394 |
null, |
| 395 |
]; |
| 396 |
yield 'anon, foreign' => [ |
| 397 |
static function () { |
| 398 |
return new UserIdentityValue( 0, self::IP, 'acmewiki' ); |
| 399 |
}, |
| 400 |
43, |
| 401 |
'acmewiki', |
| 402 |
]; |
| 403 |
yield 'registered, foreign' => [ |
| 404 |
static function () { |
| 405 |
return new UserIdentityValue( 24, 'TestUser', 'acmewiki' ); |
| 406 |
}, |
| 407 |
42, |
| 408 |
'acmewiki', |
| 409 |
]; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public function testFindActorId( callable $actorCallback, $expected, $wikiId = WikiAwareEntity::LOCAL ) { |
| 416 |
$actor = $actorCallback( $this->getServiceContainer() ); |
| 417 |
if ( $wikiId ) { |
| 418 |
$this->executeWithForeignStore( |
| 419 |
$wikiId, |
| 420 |
function ( ActorStore $store ) use ( $expected, $actor ) { |
| 421 |
$this->assertSame( $expected, $store->findActorId( $actor, $this->getDb() ) ); |
| 422 |
|
| 423 |
if ( $actor instanceof User ) { |
| 424 |
$this->assertSame( $expected ?: 0, $actor->getActorId() ); |
| 425 |
} |
| 426 |
} |
| 427 |
); |
| 428 |
} else { |
| 429 |
$this->assertSame( $expected, $this->getStore()->findActorId( $actor, $this->getDb() ) ); |
| 430 |
|
| 431 |
if ( $actor instanceof User ) { |
| 432 |
$this->assertSame( $expected ?: 0, $actor->getActorId() ); |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
public function testFindActorId_wikiMismatch() { |
| 438 |
$this->markTestSkipped(); |
| 439 |
$this->expectException( PreconditionException::class ); |
| 440 |
$this->getStore()->findActorId( |
| 441 |
new UserIdentityValue( 0, self::IP, 'acmewiki' ), |
| 442 |
$this->getDb() |
| 443 |
); |
| 444 |
} |
| 445 |
|
| 446 |
public static function provideFindActorIdByName() { |
| 447 |
yield 'anon' => [ |
| 448 |
self::IP, |
| 449 |
43, |
| 450 |
]; |
| 451 |
yield 'anon, non-canonical' => [ |
| 452 |
strtolower( self::IP ), |
| 453 |
43, |
| 454 |
]; |
| 455 |
yield 'registered' => [ |
| 456 |
'TestUser', |
| 457 |
42, |
| 458 |
]; |
| 459 |
yield 'registered, unnormalized' => [ |
| 460 |
'testUser', |
| 461 |
42, |
| 462 |
]; |
| 463 |
yield 'registered, 0 user name' => [ |
| 464 |
'0', |
| 465 |
46, |
| 466 |
]; |
| 467 |
yield 'external, local' => [ |
| 468 |
'acme>TestUser', |
| 469 |
45, |
| 470 |
]; |
| 471 |
yield 'anon, non-existent' => [ |
| 472 |
'127.1.2.3', |
| 473 |
null, |
| 474 |
]; |
| 475 |
yield 'registered, non-existent' => [ |
| 476 |
'DoNotExist', |
| 477 |
null, |
| 478 |
]; |
| 479 |
yield 'invalid' => [ |
| 480 |
'#', |
| 481 |
null, |
| 482 |
]; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
public function testFindActorIdByName( $name, $expected ) { |
| 489 |
$this->assertSame( $expected, $this->getStore()->findActorIdByName( $name, $this->getDb() ) ); |
| 490 |
} |
| 491 |
|
| 492 |
public static function provideAcquireActorId() { |
| 493 |
yield 'anon' => [ static function () { |
| 494 |
return new UserIdentityValue( 0, '127.3.2.1' ); |
| 495 |
} ]; |
| 496 |
yield 'registered' => [ static function () { |
| 497 |
return new UserIdentityValue( 15, 'MyUser' ); |
| 498 |
} ]; |
| 499 |
yield 'User object' => [ static function ( $serviceContainer ) { |
| 500 |
return $serviceContainer->getUserFactory()->newAnonymous( '127.4.3.2' ); |
| 501 |
} ]; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
public function testAcquireActorId( callable $userCallback ) { |
| 508 |
|
| 509 |
$user = $userCallback( $this->getServiceContainer() ); |
| 510 |
if ( $user->getId() === 0 ) { |
| 511 |
$this->disableAutoCreateTempUser(); |
| 512 |
} |
| 513 |
$actorId = $this->getStore()->acquireActorId( $user, $this->getDb() ); |
| 514 |
$this->assertTrue( $actorId > 0 ); |
| 515 |
|
| 516 |
if ( $user instanceof User ) { |
| 517 |
$this->assertSame( $actorId, $user->getActorId() ); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
public static function provideAcquireActorId_foreign() { |
| 522 |
yield 'anon' => [ |
| 523 |
'userCallback' => static function () { |
| 524 |
return new UserIdentityValue( 0, '127.3.2.1', 'acmewiki' ); |
| 525 |
}, |
| 526 |
'method' => 'acquireActorId' |
| 527 |
]; |
| 528 |
yield 'registered' => [ |
| 529 |
'userCallback' => static function () { |
| 530 |
return new UserIdentityValue( 15, 'MyUser', 'acmewiki' ); |
| 531 |
}, |
| 532 |
'method' => 'acquireActorId' |
| 533 |
]; |
| 534 |
yield 'anon, new' => [ |
| 535 |
'userCallback' => static function () { |
| 536 |
return new UserIdentityValue( 0, '128.9.8.7', 'acmewiki' ); |
| 537 |
}, |
| 538 |
'method' => 'createNewActor' |
| 539 |
]; |
| 540 |
yield 'registered, new' => [ |
| 541 |
'userCallback' => static function () { |
| 542 |
return new UserIdentityValue( 15, 'New MyUser', 'acmewiki' ); |
| 543 |
}, |
| 544 |
'method' => 'createNewActor' |
| 545 |
]; |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
public function testAcquireActorId_foreign( callable $userCallback, string $method ) { |
| 552 |
|
| 553 |
$user = $userCallback( $this->getServiceContainer() ); |
| 554 |
if ( $user->getId( 'acmewiki' ) === 0 ) { |
| 555 |
$this->disableAutoCreateTempUser(); |
| 556 |
} |
| 557 |
$this->executeWithForeignStore( |
| 558 |
'acmewiki', |
| 559 |
function ( ActorStore $store ) use ( $user, $method ) { |
| 560 |
$actorId = $store->$method( $user, $this->getDb() ); |
| 561 |
$this->assertTrue( $actorId > 0 ); |
| 562 |
if ( $user instanceof User ) { |
| 563 |
$this->assertSame( $actorId, $user->getActorId() ); |
| 564 |
} |
| 565 |
} |
| 566 |
); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
public function testAcquireActorId_foreign_withDBConnection( callable $userCallback, string $method ) { |
| 573 |
|
| 574 |
$user = $userCallback( $this->getServiceContainer() ); |
| 575 |
if ( $user->getId( 'acmewiki' ) === 0 ) { |
| 576 |
$this->disableAutoCreateTempUser(); |
| 577 |
} |
| 578 |
$this->executeWithForeignStore( |
| 579 |
'acmewiki', |
| 580 |
function ( ActorStore $store, IDatabase $dbw ) use ( $user, $method ) { |
| 581 |
$actorId = $store->$method( $user, $dbw ); |
| 582 |
$this->assertTrue( $actorId > 0 ); |
| 583 |
if ( $user instanceof User ) { |
| 584 |
$this->assertSame( $actorId, $user->getActorId() ); |
| 585 |
} |
| 586 |
} |
| 587 |
); |
| 588 |
} |
| 589 |
|
| 590 |
public static function provideAcquireActorId_canNotCreate() { |
| 591 |
yield 'usable name' => [ |
| 592 |
'actor' => new UserIdentityValue( 0, 'MyFancyUser' ), |
| 593 |
'method' => 'acquireActorId' |
| 594 |
]; |
| 595 |
yield 'empty name' => [ |
| 596 |
'actor' => new UserIdentityValue( 15, '' ), |
| 597 |
'method' => 'acquireActorId' |
| 598 |
]; |
| 599 |
yield 'usable name, new' => [ |
| 600 |
'actor' => new UserIdentityValue( 0, 'MyFancyUser' ), |
| 601 |
'method' => 'createNewActor' |
| 602 |
]; |
| 603 |
yield 'empty name, new' => [ |
| 604 |
'actor' => new UserIdentityValue( 15, '' ), |
| 605 |
'method' => 'createNewActor' |
| 606 |
]; |
| 607 |
yield 'usable name, replace' => [ |
| 608 |
'actor' => new UserIdentityValue( 0, 'MyFancyUser' ), |
| 609 |
'method' => 'acquireSystemActorId' |
| 610 |
]; |
| 611 |
yield 'empty name, replace' => [ |
| 612 |
'actor' => new UserIdentityValue( 15, '' ), |
| 613 |
'method' => 'acquireSystemActorId' |
| 614 |
]; |
| 615 |
yield 'existing non-anon, replace' => [ |
| 616 |
'actor' => new UserIdentityValue( 25, 'TestUser' ), |
| 617 |
'method' => 'acquireSystemActorId' |
| 618 |
]; |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
public function testAcquireActorId_canNotCreate( UserIdentityValue $actor, string $method ) { |
| 625 |
|
| 626 |
$this->setNullLogger( 'rdbms' ); |
| 627 |
$this->expectException( CannotCreateActorException::class ); |
| 628 |
$this->getStore()->$method( $actor, $this->getDb() ); |
| 629 |
} |
| 630 |
|
| 631 |
public function testAcquireNowActorId_existing() { |
| 632 |
|
| 633 |
$this->setNullLogger( 'rdbms' ); |
| 634 |
$this->expectException( CannotCreateActorException::class ); |
| 635 |
$this->getStore()->createNewActor( new UserIdentityValue( 24, 'TestUser' ), $this->getDb() ); |
| 636 |
} |
| 637 |
|
| 638 |
public function testAcquireActorId_autocreateTempIP() { |
| 639 |
$this->enableAutoCreateTempUser(); |
| 640 |
$this->expectException( CannotCreateActorException::class ); |
| 641 |
$this->getStore()->createNewActor( new UserIdentityValue( 0, '127.3.2.1' ), $this->getDb() ); |
| 642 |
} |
| 643 |
|
| 644 |
public function testAcquireActorId_autocreateTempIPallowed() { |
| 645 |
$this->enableAutoCreateTempUser(); |
| 646 |
$actorId = $this->getStoreForImport()->createNewActor( new UserIdentityValue( 0, '127.3.2.1' ), $this->getDb() ); |
| 647 |
$this->assertTrue( $actorId > 0 ); |
| 648 |
} |
| 649 |
|
| 650 |
public static function provideAcquireActorId_existing() { |
| 651 |
yield 'anon' => [ |
| 652 |
new UserIdentityValue( 0, self::IP ), |
| 653 |
43, |
| 654 |
]; |
| 655 |
yield 'registered' => [ |
| 656 |
new UserIdentityValue( 24, 'TestUser' ), |
| 657 |
42, |
| 658 |
]; |
| 659 |
yield 'registered, 0 user name' => [ |
| 660 |
new UserIdentityValue( 26, '0' ), |
| 661 |
46, |
| 662 |
]; |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
public function testAcquireActorId_existing( UserIdentityValue $actor, int $expected ) { |
| 669 |
if ( $actor->getId() === 0 ) { |
| 670 |
$this->disableAutoCreateTempUser(); |
| 671 |
} |
| 672 |
$this->assertSame( $expected, $this->getStore()->acquireActorId( $actor, $this->getDb() ) ); |
| 673 |
} |
| 674 |
|
| 675 |
public function testAcquireActorId_domain_mismatch() { |
| 676 |
$this->expectException( InvalidArgumentException::class ); |
| 677 |
$this->getStore( 'fancywiki' )->acquireActorId( |
| 678 |
new UserIdentityValue( 15, 'Test', 'fancywiki' ), |
| 679 |
$this->getDb() |
| 680 |
); |
| 681 |
} |
| 682 |
|
| 683 |
public function testcreateNewActor_domain_mismatch() { |
| 684 |
$this->expectException( InvalidArgumentException::class ); |
| 685 |
$this->getStore( 'fancywiki' )->createNewActor( |
| 686 |
new UserIdentityValue( 15, 'Test', 'fancywiki' ), |
| 687 |
$this->getDb() |
| 688 |
); |
| 689 |
} |
| 690 |
|
| 691 |
public function testAcquireSystemActorId_domain_mismatch() { |
| 692 |
$this->expectException( InvalidArgumentException::class ); |
| 693 |
$this->getStore( 'fancywiki' )->acquireSystemActorId( |
| 694 |
new UserIdentityValue( 15, 'Test', 'fancywiki' ), |
| 695 |
$this->getDb() |
| 696 |
); |
| 697 |
} |
| 698 |
|
| 699 |
public function testAcquireActorId_wikiMismatch() { |
| 700 |
$this->markTestSkipped(); |
| 701 |
$this->expectException( PreconditionException::class ); |
| 702 |
$this->getStore()->acquireActorId( |
| 703 |
new UserIdentityValue( 0, self::IP, 'acmewiki' ), |
| 704 |
$this->getDb() |
| 705 |
); |
| 706 |
} |
| 707 |
|
| 708 |
public function testAcquireActorId_clearCacheOnRollback() { |
| 709 |
$this->disableAutoCreateTempUser(); |
| 710 |
$rolledBackActor = new UserIdentityValue( 0, '127.0.0.10' ); |
| 711 |
$store = $this->getStore(); |
| 712 |
$this->getDb()->startAtomic( __METHOD__ ); |
| 713 |
$rolledBackActorId = $store->acquireActorId( $rolledBackActor, $this->getDb() ); |
| 714 |
$this->assertTrue( $rolledBackActorId > 0 ); |
| 715 |
$foundActorId = $store->findActorId( $rolledBackActor, $this->getDb() ); |
| 716 |
$this->assertSame( $rolledBackActorId, $foundActorId ); |
| 717 |
$this->getDb()->rollback( __METHOD__ ); |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
$anotherActor = new UserIdentityValue( 0, '127.0.0.11' ); |
| 722 |
$anotherActorId = $this->getStore()->acquireActorId( $anotherActor, $this->getDb() ); |
| 723 |
|
| 724 |
|
| 725 |
$foundActorIdAfterRollback = $store->findActorId( $rolledBackActor, $this->getDb() ); |
| 726 |
$this->assertNull( $foundActorIdAfterRollback ); |
| 727 |
|
| 728 |
|
| 729 |
$newActorId = $store->acquireActorId( $rolledBackActor, $this->getDb() ); |
| 730 |
$this->assertTrue( $newActorId > 0 ); |
| 731 |
$this->assertNotSame( $newActorId, $rolledBackActorId ); |
| 732 |
|
| 733 |
|
| 734 |
$this->assertSameActors( $anotherActor, $store->getActorById( $anotherActorId, $this->getDb() ) ); |
| 735 |
} |
| 736 |
|
| 737 |
public function testUserRenameUpdatesActor() { |
| 738 |
$user = $this->getMutableTestUser()->getUser(); |
| 739 |
$oldName = $user->getName(); |
| 740 |
|
| 741 |
$store = $this->getStore(); |
| 742 |
$actorId = $store->findActorId( $user, $this->getDb() ); |
| 743 |
$this->assertTrue( $actorId > 0 ); |
| 744 |
|
| 745 |
$user->setName( 'NewName' ); |
| 746 |
$user->saveSettings(); |
| 747 |
|
| 748 |
$this->assertSameActors( $user, $store->getActorById( $actorId, $this->getDb() ) ); |
| 749 |
$this->assertSameActors( $user, $store->getUserIdentityByName( 'NewName' ) ); |
| 750 |
$this->assertSameActors( $user, $store->getUserIdentityByUserId( $user->getId() ) ); |
| 751 |
$this->assertNull( $store->getUserIdentityByName( $oldName ) ); |
| 752 |
} |
| 753 |
|
| 754 |
public function testAcquireSystemActorId() { |
| 755 |
$this->disableAutoCreateTempUser(); |
| 756 |
$store = $this->getStore(); |
| 757 |
$originalActor = new UserIdentityValue( 0, '129.0.0.1' ); |
| 758 |
$actorId = $store->createNewActor( $originalActor, $this->getDb() ); |
| 759 |
$this->assertTrue( $actorId > 0, 'Acquired new actor ID' ); |
| 760 |
|
| 761 |
$updatedActor = new UserIdentityValue( 56789, '129.0.0.1' ); |
| 762 |
$this->assertSame( $actorId, $store->acquireSystemActorId( $updatedActor, $this->getDb() ) ); |
| 763 |
$this->assertSame( 56789, $store->getActorById( $actorId, $this->getDb() )->getId() ); |
| 764 |
|
| 765 |
$this->assertSame( 56789, $this->getStore()->getActorById( $actorId, $this->getDb() )->getId() ); |
| 766 |
} |
| 767 |
|
| 768 |
public function testAcquireSystemActorId_replaceReserved() { |
| 769 |
$this->overrideConfigValue( |
| 770 |
MainConfigNames::ReservedUsernames, |
| 771 |
[ 'RESERVED' ] |
| 772 |
); |
| 773 |
$store = $this->getStore(); |
| 774 |
$originalActor = new UserIdentityValue( 0, 'RESERVED' ); |
| 775 |
$actorId = $store->createNewActor( $originalActor, $this->getDb() ); |
| 776 |
$this->assertTrue( $actorId > 0, 'Acquired new actor ID' ); |
| 777 |
|
| 778 |
$updatedActor = new UserIdentityValue( 80, 'RESERVED' ); |
| 779 |
$this->assertSame( $actorId, $store->acquireSystemActorId( $updatedActor, $this->getDb() ) ); |
| 780 |
$this->assertSame( 80, $store->getActorById( $actorId, $this->getDb() )->getId() ); |
| 781 |
|
| 782 |
$this->assertSame( 80, $this->getStore()->getActorById( $actorId, $this->getDb() )->getId() ); |
| 783 |
} |
| 784 |
|
| 785 |
public function testAcquireSystemActorId_assignsNew() { |
| 786 |
$store = $this->getStore(); |
| 787 |
|
| 788 |
$newActor = new UserIdentityValue( 456789, 'Foo' ); |
| 789 |
$newActorId = $store->acquireSystemActorId( $newActor, $this->getDb() ); |
| 790 |
$this->assertTrue( $newActorId > 0 ); |
| 791 |
$this->assertSame( 456789, $store->getActorById( $newActorId, $this->getDb() )->getId() ); |
| 792 |
|
| 793 |
$this->assertSame( 456789, $this->getStore()->getActorById( $newActorId, $this->getDb() )->getId() ); |
| 794 |
} |
| 795 |
|
| 796 |
public function testDelete() { |
| 797 |
$store = $this->getStore(); |
| 798 |
$actor = new UserIdentityValue( 9999, 'DeleteTest' ); |
| 799 |
$actorId = $store->acquireActorId( $actor, $this->getDb() ); |
| 800 |
$this->assertTrue( $store->deleteActor( $actor, $this->getDb() ) ); |
| 801 |
|
| 802 |
$this->assertNull( $store->findActorId( $actor, $this->getDb() ) ); |
| 803 |
$this->assertNull( $store->getUserIdentityByUserId( $actor->getId() ) ); |
| 804 |
$this->assertNull( $store->getUserIdentityByName( $actor->getName() ) ); |
| 805 |
$this->assertNull( $store->getActorById( $actorId, $this->getDb() ) ); |
| 806 |
} |
| 807 |
|
| 808 |
public function testDeleteDoesNotExist() { |
| 809 |
$this->assertFalse( |
| 810 |
$this->getStore()->deleteActor( new UserIdentityValue( 9998, 'DoesNotExist' ), $this->getDb() ) |
| 811 |
); |
| 812 |
} |
| 813 |
|
| 814 |
public function testGetUnknownActor() { |
| 815 |
$store = $this->getStore(); |
| 816 |
$unknownActor = $store->getUnknownActor(); |
| 817 |
$this->assertInstanceOf( UserIdentity::class, $unknownActor ); |
| 818 |
$this->assertSame( ActorStore::UNKNOWN_USER_NAME, $unknownActor->getName() ); |
| 819 |
$this->assertSameActors( $unknownActor, $store->getUnknownActor() ); |
| 820 |
} |
| 821 |
|
| 822 |
public static function provideNormalizeUserName() { |
| 823 |
yield [ strtolower( self::IP ), self::IP ]; |
| 824 |
yield [ 'acme>test', 'acme>test' ]; |
| 825 |
yield [ 'test_this', 'Test this' ]; |
| 826 |
yield [ 'foo#bar', null ]; |
| 827 |
yield [ 'foo|bar', null ]; |
| 828 |
yield [ '_', null ]; |
| 829 |
yield [ 'test', 'Test' ]; |
| 830 |
yield [ '', null ]; |
| 831 |
yield [ '0', '0' ]; |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
public function testNormalizeUserName( $name, $expected ) { |
| 838 |
$store = $this->getStore(); |
| 839 |
$this->assertSame( $expected, $store->normalizeUserName( $name ) ); |
| 840 |
} |
| 841 |
|
| 842 |
public function testNewSelectQueryBuilderWithoutDB() { |
| 843 |
$store = $this->getStore(); |
| 844 |
$queryBuilder = $store->newSelectQueryBuilder(); |
| 845 |
$this->assertInstanceOf( UserSelectQueryBuilder::class, $queryBuilder ); |
| 846 |
} |
| 847 |
|
| 848 |
public function testNewSelectQueryBuilderWithDB() { |
| 849 |
$store = $this->getStore(); |
| 850 |
$queryBuilder = $store->newSelectQueryBuilder( $this->getDb() ); |
| 851 |
$this->assertInstanceOf( UserSelectQueryBuilder::class, $queryBuilder ); |
| 852 |
} |
| 853 |
|
| 854 |
public function testNewSelectQueryBuilderWithQueryFlags() { |
| 855 |
$store = $this->getStore(); |
| 856 |
$queryBuilder = $store->newSelectQueryBuilder( IDBAccessObject::READ_NORMAL ); |
| 857 |
$this->assertInstanceOf( UserSelectQueryBuilder::class, $queryBuilder ); |
| 858 |
} |
| 859 |
} |