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
|
<?php
use MediaWiki\Block\BlockCache;
use MediaWiki\Block\BlockCacheKey;
use MediaWiki\Block\SystemBlock;
use MediaWiki\Request\FauxRequest;
use MediaWiki\User\UserIdentityValue;
/**
* @covers \MediaWiki\Block\BlockCache
* @covers \MediaWiki\Block\BlockCacheEntry
* @covers \MediaWiki\Block\BlockCacheKey
*/
class BlockCacheTest extends MediaWikiUnitTestCase {
public function testGet() {
// The exact semantics of partial keys and evictions in BlockCache are
// implementation details, not really part of the public contract.
// But we have some tests for them here to confirm that the code as
// implemented at least reflects the developer's intention.
$cache = new BlockCache;
$req1 = new FauxRequest();
$req2 = new FauxRequest();
$user1 = new UserIdentityValue( 1, 'User 1' );
$user2 = new UserIdentityValue( 2, 'User 2' );
$anon = new UserIdentityValue( 0, '127.0.0.1' );
$keyR1U1 = new BlockCacheKey( $req1, $user1, false );
$keyR2U1 = new BlockCacheKey( $req2, $user1, false );
$keyR2U2 = new BlockCacheKey( $req2, $user2, false );
$keyR2U2P = new BlockCacheKey( $req2, $user2, true );
$keyU1 = new BlockCacheKey( null, $user1, false );
$keyU2P = new BlockCacheKey( null, $user2, true );
$keyA1 = new BlockCacheKey( null, $anon, false );
$block1 = new SystemBlock();
$block2 = new SystemBlock();
$block3 = new SystemBlock();
$block4 = new SystemBlock();
$block5 = new SystemBlock();
// Empty cache
$this->assertNull( $cache->get( $keyR1U1 ) );
$this->assertNull( $cache->get( $keyR2U1 ) );
$this->assertNull( $cache->get( $keyR2U2 ) );
$this->assertNull( $cache->get( $keyR2U2P ) );
$this->assertNull( $cache->get( $keyU1 ) );
$this->assertNull( $cache->get( $keyA1 ) );
// Set a request block to absent
$cache->set( $keyR1U1, false );
$this->assertFalse( $cache->get( $keyR1U1 ) );
$this->assertNull( $cache->get( $keyR2U1 ) );
$this->assertNull( $cache->get( $keyR2U2 ) );
$this->assertNull( $cache->get( $keyR2U2P ) );
$this->assertNull( $cache->get( $keyU1 ) );
$this->assertNull( $cache->get( $keyA1 ) );
// Set another request block with a different request
$cache->set( $keyR2U1, $block1 );
$this->assertNull( $cache->get( $keyR1U1 ) );
$this->assertSame( $block1, $cache->get( $keyR2U1 ) );
$this->assertNull( $cache->get( $keyR2U2 ) );
$this->assertNull( $cache->get( $keyR2U2P ) );
$this->assertNull( $cache->get( $keyU1 ) );
$this->assertNull( $cache->get( $keyA1 ) );
// Set another request block with a different user
$cache->set( $keyR2U2, $block2 );
$this->assertNull( $cache->get( $keyR1U1 ) );
$this->assertNull( $cache->get( $keyR2U1 ) );
$this->assertSame( $block2, $cache->get( $keyR2U2 ) );
$this->assertNull( $cache->get( $keyR2U2P ) );
$this->assertNull( $cache->get( $keyU1 ) );
$this->assertNull( $cache->get( $keyA1 ) );
// Set another request block with a different fromPrimary flag
$cache->set( $keyR2U2P, $block3 );
$this->assertNull( $cache->get( $keyR1U1 ) );
$this->assertNull( $cache->get( $keyR2U1 ) );
$this->assertSame( $block3, $cache->get( $keyR2U2 ) );
$this->assertSame( $block3, $cache->get( $keyR2U2P ) );
$this->assertNull( $cache->get( $keyU1 ) );
$this->assertNull( $cache->get( $keyA1 ) );
// Set a user block with no request
$cache->set( $keyU1, $block4 );
$this->assertNull( $cache->get( $keyR1U1 ) );
$this->assertNull( $cache->get( $keyR2U1 ) );
$this->assertSame( $block3, $cache->get( $keyR2U2 ) );
$this->assertSame( $block3, $cache->get( $keyR2U2P ) );
$this->assertSame( $block4, $cache->get( $keyU1 ) );
$this->assertNull( $cache->get( $keyA1 ) );
// Set an anonymous block with no request
$cache->set( $keyA1, $block5 );
$this->assertNull( $cache->get( $keyR1U1 ) );
$this->assertNull( $cache->get( $keyR2U1 ) );
$this->assertSame( $block3, $cache->get( $keyR2U2 ) );
$this->assertSame( $block3, $cache->get( $keyR2U2P ) );
$this->assertSame( $block4, $cache->get( $keyU1 ) );
$this->assertSame( $block5, $cache->get( $keyA1 ) );
// The weak reference causes the request cache entry to disappear when
// the request is destroyed
$req2 = null;
$this->assertNull( $cache->get( $keyR2U2P ) );
// An non-request block does not match a destroyed weak reference
$this->assertNull( $cache->get( $keyU2P ) );
}
}
|