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