7 use Psr\Log\AbstractLogger;
8 use Psr\Log\LoggerInterface;
9 use Psr\Log\NullLogger;
10 use Wikimedia\TestingAccessWrapper;
18 $cache = new \HashBagOStuff();
20 ->setMethods( [
'log' ] )
21 ->getMockForAbstractClass();
24 [ [
'count' => 123,
'seconds' => 456 ] ],
25 [
'type' =>
'foo',
'cache' =>
$cache ]
27 $throttler->setLogger( $logger );
28 $throttlerPriv = TestingAccessWrapper::newFromObject( $throttler );
29 $this->assertSame( [ [
'count' => 123,
'seconds' => 456 ] ], $throttlerPriv->conditions );
30 $this->assertSame(
'foo', $throttlerPriv->type );
31 $this->assertSame(
$cache, $throttlerPriv->cache );
32 $this->assertSame( $logger, $throttlerPriv->logger );
34 $throttler =
new Throttler( [ [
'count' => 123,
'seconds' => 456 ] ] );
35 $throttler->setLogger(
new NullLogger() );
36 $throttlerPriv = TestingAccessWrapper::newFromObject( $throttler );
37 $this->assertSame( [ [
'count' => 123,
'seconds' => 456 ] ], $throttlerPriv->conditions );
38 $this->assertSame(
'custom', $throttlerPriv->type );
42 $this->
setMwGlobals( [
'wgPasswordAttemptThrottle' => [ [
'count' => 321,
43 'seconds' => 654 ] ] ] );
45 $throttler->setLogger(
new NullLogger() );
46 $throttlerPriv = TestingAccessWrapper::newFromObject( $throttler );
47 $this->assertSame( [ [
'count' => 321,
'seconds' => 654 ] ], $throttlerPriv->conditions );
48 $this->assertSame(
'password', $throttlerPriv->type );
53 new Throttler( [], [
'foo' => 1,
'bar' => 2,
'baz' => 3 ] );
54 $this->fail(
'Expected exception not thrown' );
55 }
catch ( \InvalidArgumentException $ex ) {
56 $this->assertSame(
'unrecognized parameters: foo, bar, baz', $ex->getMessage() );
65 $throttler->setLogger(
new NullLogger() );
66 $throttlerPriv = TestingAccessWrapper::newFromObject( $throttler );
67 $this->assertSame( $normalized, $throttlerPriv->conditions );
77 [
'count' => 1,
'seconds' => 2 ],
78 [ [
'count' => 1,
'seconds' => 2 ] ],
81 [ [
'count' => 1,
'seconds' => 2 ], [
'count' => 2,
'seconds' => 3 ] ],
82 [ [
'count' => 1,
'seconds' => 2 ], [
'count' => 2,
'seconds' => 3 ] ],
89 $this->assertSame( [], $priv->normalizeThrottleConditions(
null ) );
90 $this->assertSame( [], $priv->normalizeThrottleConditions(
'bad' ) );
94 $cache = new \HashBagOStuff();
96 [
'count' => 2,
'seconds' => 10, ],
97 [
'count' => 4,
'seconds' => 15,
'allIPs' =>
true ],
98 ], [
'cache' =>
$cache ] );
99 $throttler->setLogger(
new NullLogger() );
101 $result = $throttler->increase(
'SomeUser',
'1.2.3.4' );
102 $this->assertFalse(
$result,
'should not throttle' );
104 $result = $throttler->increase(
'SomeUser',
'1.2.3.4' );
105 $this->assertFalse(
$result,
'should not throttle' );
107 $result = $throttler->increase(
'SomeUser',
'1.2.3.4' );
108 $this->assertSame( [
'throttleIndex' => 0,
'count' => 2,
'wait' => 10 ],
$result );
110 $result = $throttler->increase(
'OtherUser',
'1.2.3.4' );
111 $this->assertFalse(
$result,
'should not throttle' );
113 $result = $throttler->increase(
'SomeUser',
'2.3.4.5' );
114 $this->assertFalse(
$result,
'should not throttle' );
116 $result = $throttler->increase(
'SomeUser',
'3.4.5.6' );
117 $this->assertFalse(
$result,
'should not throttle' );
119 $result = $throttler->increase(
'SomeUser',
'3.4.5.6' );
120 $this->assertSame( [
'throttleIndex' => 1,
'count' => 4,
'wait' => 15 ],
$result );
124 $cache = new \HashBagOStuff();
125 $throttler =
new Throttler( [ [
'count' => 0,
'seconds' => 10 ] ], [
'cache' =>
$cache ] );
126 $throttler->setLogger(
new NullLogger() );
128 $result = $throttler->increase(
'SomeUser',
'1.2.3.4' );
129 $this->assertFalse(
$result,
'should not throttle, count=0 is ignored' );
131 $result = $throttler->increase(
'SomeUser',
'1.2.3.4' );
132 $this->assertFalse(
$result,
'should not throttle, count=0 is ignored' );
134 $result = $throttler->increase(
'SomeUser',
'1.2.3.4' );
135 $this->assertFalse(
$result,
'should not throttle, count=0 is ignored' );
139 $cache = new \HashBagOStuff();
140 $throttler1 =
new Throttler( [ [
'count' => 1,
'seconds' => 10 ] ],
141 [
'cache' =>
$cache,
'type' =>
'foo' ] );
142 $throttler2 =
new Throttler( [ [
'count' => 1,
'seconds' => 10 ] ],
143 [
'cache' =>
$cache,
'type' =>
'foo' ] );
144 $throttler3 =
new Throttler( [ [
'count' => 1,
'seconds' => 10 ] ],
145 [
'cache' =>
$cache,
'type' =>
'bar' ] );
146 $throttler1->setLogger(
new NullLogger() );
147 $throttler2->setLogger(
new NullLogger() );
148 $throttler3->setLogger(
new NullLogger() );
150 $throttled = [
'throttleIndex' => 0,
'count' => 1,
'wait' => 10 ];
152 $result = $throttler1->increase(
'SomeUser',
'1.2.3.4' );
153 $this->assertFalse(
$result,
'should not throttle' );
155 $result = $throttler1->increase(
'SomeUser',
'1.2.3.4' );
156 $this->assertEquals( $throttled,
$result,
'should throttle' );
158 $result = $throttler2->increase(
'SomeUser',
'1.2.3.4' );
159 $this->assertEquals( $throttled,
$result,
'should throttle, same namespace' );
161 $result = $throttler3->increase(
'SomeUser',
'1.2.3.4' );
162 $this->assertFalse(
$result,
'should not throttle, different namespace' );
167 ->setMethods( [
'add' ] )->getMock();
168 $throttler =
new Throttler( [ [
'count' => 3,
'seconds' => 10 ] ], [
'cache' =>
$cache ] );
169 $throttler->setLogger(
new NullLogger() );
171 $cache->expects( $this->once() )->method(
'add' )->with( $this->
anything(), 1, 10 );
172 $throttler->increase(
'SomeUser' );
179 $throttler =
new Throttler( [ [
'count' => 3,
'seconds' => 10 ] ] );
180 $throttler->setLogger(
new NullLogger() );
181 $throttler->increase();
185 $cache = new \HashBagOStuff();
186 $throttler =
new Throttler( [ [
'count' => 1,
'seconds' => 10 ] ], [
'cache' =>
$cache ] );
189 ->setMethods( [
'log' ] )
190 ->getMockForAbstractClass();
191 $logger->expects( $this->never() )->method(
'log' );
192 $throttler->setLogger( $logger );
193 $result = $throttler->increase(
'SomeUser',
'1.2.3.4' );
194 $this->assertFalse(
$result,
'should not throttle' );
197 ->setMethods( [
'log' ] )
198 ->getMockForAbstractClass();
199 $logger->expects( $this->once() )->method(
'log' )->with( $this->
anything(), $this->
anything(), [
200 'throttle' =>
'custom',
202 'ipKey' =>
'1.2.3.4',
203 'username' =>
'SomeUser',
208 $throttler->setLogger( $logger );
209 $result = $throttler->increase(
'SomeUser',
'1.2.3.4',
'foo' );
210 $this->assertSame( [
'throttleIndex' => 0,
'count' => 1,
'wait' => 10 ],
$result );
214 $cache = new \HashBagOStuff();
215 $throttler =
new Throttler( [ [
'count' => 1,
'seconds' => 10 ] ], [
'cache' =>
$cache ] );
216 $throttler->setLogger(
new NullLogger() );
218 $result = $throttler->increase(
'SomeUser',
'1.2.3.4' );
219 $this->assertFalse(
$result,
'should not throttle' );
221 $result = $throttler->increase(
'SomeUser',
'1.2.3.4' );
222 $this->assertSame( [
'throttleIndex' => 0,
'count' => 1,
'wait' => 10 ],
$result );
224 $result = $throttler->increase(
'OtherUser',
'1.2.3.4' );
225 $this->assertFalse(
$result,
'should not throttle' );
227 $result = $throttler->increase(
'OtherUser',
'1.2.3.4' );
228 $this->assertSame( [
'throttleIndex' => 0,
'count' => 1,
'wait' => 10 ],
$result );
230 $throttler->clear(
'SomeUser',
'1.2.3.4' );
232 $result = $throttler->increase(
'SomeUser',
'1.2.3.4' );
233 $this->assertFalse(
$result,
'should not throttle' );
235 $result = $throttler->increase(
'OtherUser',
'1.2.3.4' );
236 $this->assertSame( [
'throttleIndex' => 0,
'count' => 1,
'wait' => 10 ],
$result );