| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Unit\User; |
| 4 |
|
| 5 |
use MediaWiki\User\ActorCache; |
| 6 |
use MediaWiki\User\UserIdentity; |
| 7 |
use MediaWiki\User\UserIdentityValue; |
| 8 |
use MediaWikiUnitTestCase; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class ActorCacheTest extends MediaWikiUnitTestCase { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
private function assertCacheContains( ActorCache $cache, int $actorId, UserIdentity $actor ) { |
| 21 |
$this->assertSame( $actor, |
| 22 |
$cache->getActor( ActorCache::KEY_ACTOR_ID, $actorId ) ); |
| 23 |
$this->assertSame( $actor, |
| 24 |
$cache->getActor( ActorCache::KEY_USER_NAME, $actor->getName() ) ); |
| 25 |
$this->assertSame( $actor, |
| 26 |
$cache->getActor( ActorCache::KEY_USER_ID, $actor->getId( $actor->getWikiId() ) ) ); |
| 27 |
$this->assertSame( $actorId, |
| 28 |
$cache->getActorId( ActorCache::KEY_ACTOR_ID, 1 ) ); |
| 29 |
$this->assertSame( $actorId, |
| 30 |
$cache->getActorId( ActorCache::KEY_USER_NAME, $actor->getName() ) ); |
| 31 |
$this->assertSame( $actorId, |
| 32 |
$cache->getActorId( ActorCache::KEY_USER_ID, $actor->getId( $actor->getWikiId() ) ) ); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private function assertCacheNotContains( ActorCache $cache, int $actorId, string $actorName, int $userId ) { |
| 42 |
$this->assertNull( $cache->getActor( ActorCache::KEY_ACTOR_ID, $actorId ) ); |
| 43 |
$this->assertNull( $cache->getActor( ActorCache::KEY_USER_NAME, $actorName ) ); |
| 44 |
$this->assertNull( $cache->getActor( ActorCache::KEY_USER_ID, $userId ) ); |
| 45 |
$this->assertNull( $cache->getActorId( ActorCache::KEY_ACTOR_ID, $actorId ) ); |
| 46 |
$this->assertNull( $cache->getActorId( ActorCache::KEY_USER_NAME, $actorName ) ); |
| 47 |
$this->assertNull( $cache->getActorId( ActorCache::KEY_USER_ID, $userId ) ); |
| 48 |
} |
| 49 |
|
| 50 |
public static function provideGetActor() { |
| 51 |
yield 'local' => [ |
| 52 |
'actor' => new UserIdentityValue( 10, 'Hello' ), |
| 53 |
]; |
| 54 |
yield 'foreign' => [ |
| 55 |
'actor' => new UserIdentityValue( 10, 'Hello', 'acmewiki' ), |
| 56 |
]; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function testGetActor( UserIdentity $actor ) { |
| 63 |
$cache = new ActorCache( 5 ); |
| 64 |
$cache->add( 1, $actor ); |
| 65 |
$this->assertCacheContains( $cache, 1, $actor ); |
| 66 |
} |
| 67 |
|
| 68 |
public static function provideRemove() { |
| 69 |
yield 'Same actor' => [ |
| 70 |
'addedActor' => new UserIdentityValue( 10, 'Hello' ), |
| 71 |
'removedActor' => new UserIdentityValue( 10, 'Hello' ) |
| 72 |
]; |
| 73 |
yield 'Different name' => [ |
| 74 |
'addedActor' => new UserIdentityValue( 10, 'Hello' ), |
| 75 |
'removedActor' => new UserIdentityValue( 10, 'Goodbye' ) |
| 76 |
]; |
| 77 |
yield 'Different user ID' => [ |
| 78 |
'addedActor' => new UserIdentityValue( 10, 'Hello' ), |
| 79 |
'removedActor' => new UserIdentityValue( 11, 'Hello' ) |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function testRemove( UserIdentity $added, UserIdentity $removed ) { |
| 87 |
$cache = new ActorCache( 5 ); |
| 88 |
$cache->add( 1, $added ); |
| 89 |
$this->assertCacheContains( $cache, 1, $added ); |
| 90 |
$cache->remove( $removed ); |
| 91 |
$this->assertCacheNotContains( $cache, 1, $added->getName(), $added->getId() ); |
| 92 |
$this->assertCacheNotContains( $cache, 1, $removed->getName(), $removed->getId() ); |
| 93 |
} |
| 94 |
|
| 95 |
public function testEvict() { |
| 96 |
$cache = new ActorCache( 1 ); |
| 97 |
$actor1 = new UserIdentityValue( 10, 'Hello' ); |
| 98 |
$cache->add( 2, new UserIdentityValue( 12, 'Goodbye' ) ); |
| 99 |
$cache->add( 1, $actor1 ); |
| 100 |
$this->assertCacheContains( $cache, 1, $actor1 ); |
| 101 |
$this->assertCacheNotContains( $cache, 2, 'Goodbye', 12 ); |
| 102 |
} |
| 103 |
|
| 104 |
public function testDoesNotCacheAnonUserId() { |
| 105 |
$cache = new ActorCache( 5 ); |
| 106 |
$actor = new UserIdentityValue( 0, '127.0.0.1' ); |
| 107 |
$cache->add( 1, $actor ); |
| 108 |
$this->assertNull( $cache->getActor( ActorCache::KEY_USER_ID, 0 ) ); |
| 109 |
$this->assertNull( $cache->getActorId( ActorCache::KEY_USER_ID, 0 ) ); |
| 110 |
$this->assertSame( $actor, $cache->getActor( ActorCache::KEY_USER_NAME, '127.0.0.1' ) ); |
| 111 |
$this->assertSame( 1, $cache->getActorId( ActorCache::KEY_USER_NAME, '127.0.0.1' ) ); |
| 112 |
$this->assertSame( $actor, $cache->getActor( ActorCache::KEY_ACTOR_ID, 1 ) ); |
| 113 |
$this->assertSame( 1, $cache->getActorId( ActorCache::KEY_ACTOR_ID, 1 ) ); |
| 114 |
} |
| 115 |
} |