Exclude redirects from Special:Fewestrevisions
[lhc/web/wiklou.git] / tests / phpunit / includes / session / TokenTest.php
1 <?php
2
3 namespace MediaWiki\Session;
4
5 use MediaWikiTestCase;
6 use Wikimedia\TestingAccessWrapper;
7
8 /**
9 * @group Session
10 * @covers MediaWiki\Session\Token
11 */
12 class TokenTest extends MediaWikiTestCase {
13
14 public function testBasics() {
15 $token = $this->getMockBuilder( Token::class )
16 ->setMethods( [ 'toStringAtTimestamp' ] )
17 ->setConstructorArgs( [ 'sekret', 'salty', true ] )
18 ->getMock();
19 $token->expects( $this->any() )->method( 'toStringAtTimestamp' )
20 ->will( $this->returnValue( 'faketoken+\\' ) );
21
22 $this->assertSame( 'faketoken+\\', $token->toString() );
23 $this->assertSame( 'faketoken+\\', (string)$token );
24 $this->assertTrue( $token->wasNew() );
25
26 $token = new Token( 'sekret', 'salty', false );
27 $this->assertFalse( $token->wasNew() );
28 }
29
30 public function testToStringAtTimestamp() {
31 $token = TestingAccessWrapper::newFromObject( new Token( 'sekret', 'salty', false ) );
32
33 $this->assertSame(
34 'd9ade0c7d4349e9df9094e61c33a5a0d5644fde2+\\',
35 $token->toStringAtTimestamp( 1447362018 )
36 );
37 $this->assertSame(
38 'ee2f7a2488dea9176c224cfb400d43be5644fdea+\\',
39 $token->toStringAtTimestamp( 1447362026 )
40 );
41 }
42
43 public function testGetTimestamp() {
44 $this->assertSame(
45 1447362018, Token::getTimestamp( 'd9ade0c7d4349e9df9094e61c33a5a0d5644fde2+\\' )
46 );
47 $this->assertSame(
48 1447362026, Token::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be5644fdea+\\' )
49 );
50 $this->assertNull( Token::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be5644fdea-\\' ) );
51 $this->assertNull( Token::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be+\\' ) );
52
53 $this->assertNull( Token::getTimestamp( 'ee2f7a2488dea9x76c224cfb400d43be5644fdea+\\' ) );
54 }
55
56 public function testMatch() {
57 $token = TestingAccessWrapper::newFromObject( new Token( 'sekret', 'salty', false ) );
58
59 $test = $token->toStringAtTimestamp( time() - 10 );
60 $this->assertTrue( $token->match( $test ) );
61 $this->assertTrue( $token->match( $test, 12 ) );
62 $this->assertFalse( $token->match( $test, 8 ) );
63
64 $this->assertFalse( $token->match( 'ee2f7a2488dea9176c224cfb400d43be5644fdea-\\' ) );
65 }
66
67 }