Debian Codegraph @

tests/phpunit/unit/includes/block/BlockCacheTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Block\BlockCache;
4 use MediaWiki\Block\BlockCacheKey;
5 use MediaWiki\Block\SystemBlock;
6 use MediaWiki\Request\FauxRequest;
7 use MediaWiki\User\UserIdentityValue;
8
9 /**
10 * @covers \MediaWiki\Block\BlockCache
11 * @covers \MediaWiki\Block\BlockCacheEntry
12 * @covers \MediaWiki\Block\BlockCacheKey
13 */
14 class BlockCacheTest extends MediaWikiUnitTestCase {
15 public function testGet() {
16 // The exact semantics of partial keys and evictions in BlockCache are
17 // implementation details, not really part of the public contract.
18 // But we have some tests for them here to confirm that the code as
19 // implemented at least reflects the developer's intention.
20
21 $cache = new BlockCache;
22
23 $req1 = new FauxRequest();
24 $req2 = new FauxRequest();
25
26 $user1 = new UserIdentityValue( 1, 'User 1' );
27 $user2 = new UserIdentityValue( 2, 'User 2' );
28 $anon = new UserIdentityValue( 0, '127.0.0.1' );
29
30 $keyR1U1 = new BlockCacheKey( $req1, $user1, false );
31 $keyR2U1 = new BlockCacheKey( $req2, $user1, false );
32 $keyR2U2 = new BlockCacheKey( $req2, $user2, false );
33 $keyR2U2P = new BlockCacheKey( $req2, $user2, true );
34 $keyU1 = new BlockCacheKey( null, $user1, false );
35 $keyU2P = new BlockCacheKey( null, $user2, true );
36 $keyA1 = new BlockCacheKey( null, $anon, false );
37
38 $block1 = new SystemBlock();
39 $block2 = new SystemBlock();
40 $block3 = new SystemBlock();
41 $block4 = new SystemBlock();
42 $block5 = new SystemBlock();
43
44 // Empty cache
45 $this->assertNull( $cache->get( $keyR1U1 ) );
46 $this->assertNull( $cache->get( $keyR2U1 ) );
47 $this->assertNull( $cache->get( $keyR2U2 ) );
48 $this->assertNull( $cache->get( $keyR2U2P ) );
49 $this->assertNull( $cache->get( $keyU1 ) );
50 $this->assertNull( $cache->get( $keyA1 ) );
51
52 // Set a request block to absent
53 $cache->set( $keyR1U1, false );
54 $this->assertFalse( $cache->get( $keyR1U1 ) );
55 $this->assertNull( $cache->get( $keyR2U1 ) );
56 $this->assertNull( $cache->get( $keyR2U2 ) );
57 $this->assertNull( $cache->get( $keyR2U2P ) );
58 $this->assertNull( $cache->get( $keyU1 ) );
59 $this->assertNull( $cache->get( $keyA1 ) );
60
61 // Set another request block with a different request
62 $cache->set( $keyR2U1, $block1 );
63 $this->assertNull( $cache->get( $keyR1U1 ) );
64 $this->assertSame( $block1, $cache->get( $keyR2U1 ) );
65 $this->assertNull( $cache->get( $keyR2U2 ) );
66 $this->assertNull( $cache->get( $keyR2U2P ) );
67 $this->assertNull( $cache->get( $keyU1 ) );
68 $this->assertNull( $cache->get( $keyA1 ) );
69
70 // Set another request block with a different user
71 $cache->set( $keyR2U2, $block2 );
72 $this->assertNull( $cache->get( $keyR1U1 ) );
73 $this->assertNull( $cache->get( $keyR2U1 ) );
74 $this->assertSame( $block2, $cache->get( $keyR2U2 ) );
75 $this->assertNull( $cache->get( $keyR2U2P ) );
76 $this->assertNull( $cache->get( $keyU1 ) );
77 $this->assertNull( $cache->get( $keyA1 ) );
78
79 // Set another request block with a different fromPrimary flag
80 $cache->set( $keyR2U2P, $block3 );
81 $this->assertNull( $cache->get( $keyR1U1 ) );
82 $this->assertNull( $cache->get( $keyR2U1 ) );
83 $this->assertSame( $block3, $cache->get( $keyR2U2 ) );
84 $this->assertSame( $block3, $cache->get( $keyR2U2P ) );
85 $this->assertNull( $cache->get( $keyU1 ) );
86 $this->assertNull( $cache->get( $keyA1 ) );
87
88 // Set a user block with no request
89 $cache->set( $keyU1, $block4 );
90 $this->assertNull( $cache->get( $keyR1U1 ) );
91 $this->assertNull( $cache->get( $keyR2U1 ) );
92 $this->assertSame( $block3, $cache->get( $keyR2U2 ) );
93 $this->assertSame( $block3, $cache->get( $keyR2U2P ) );
94 $this->assertSame( $block4, $cache->get( $keyU1 ) );
95 $this->assertNull( $cache->get( $keyA1 ) );
96
97 // Set an anonymous block with no request
98 $cache->set( $keyA1, $block5 );
99 $this->assertNull( $cache->get( $keyR1U1 ) );
100 $this->assertNull( $cache->get( $keyR2U1 ) );
101 $this->assertSame( $block3, $cache->get( $keyR2U2 ) );
102 $this->assertSame( $block3, $cache->get( $keyR2U2P ) );
103 $this->assertSame( $block4, $cache->get( $keyU1 ) );
104 $this->assertSame( $block5, $cache->get( $keyA1 ) );
105
106 // The weak reference causes the request cache entry to disappear when
107 // the request is destroyed
108 $req2 = null;
109 $this->assertNull( $cache->get( $keyR2U2P ) );
110
111 // An non-request block does not match a destroyed weak reference
112 $this->assertNull( $cache->get( $keyU2P ) );
113 }
114 }