3 namespace MediaWiki\Session
;
9 * @covers MediaWiki\Session\Token
11 class TokenTest
extends MediaWikiTestCase
{
13 public function testBasics() {
14 $token = $this->getMockBuilder( Token
::class )
15 ->setMethods( [ 'toStringAtTimestamp' ] )
16 ->setConstructorArgs( [ 'sekret', 'salty', true ] )
18 $token->expects( $this->any() )->method( 'toStringAtTimestamp' )
19 ->will( $this->returnValue( 'faketoken+\\' ) );
21 $this->assertSame( 'faketoken+\\', $token->toString() );
22 $this->assertSame( 'faketoken+\\', (string)$token );
23 $this->assertTrue( $token->wasNew() );
25 $token = new Token( 'sekret', 'salty', false );
26 $this->assertFalse( $token->wasNew() );
29 public function testToStringAtTimestamp() {
30 $token = \TestingAccessWrapper
::newFromObject( new Token( 'sekret', 'salty', false ) );
33 'd9ade0c7d4349e9df9094e61c33a5a0d5644fde2+\\',
34 $token->toStringAtTimestamp( 1447362018 )
37 'ee2f7a2488dea9176c224cfb400d43be5644fdea+\\',
38 $token->toStringAtTimestamp( 1447362026 )
42 public function testGetTimestamp() {
44 1447362018, Token
::getTimestamp( 'd9ade0c7d4349e9df9094e61c33a5a0d5644fde2+\\' )
47 1447362026, Token
::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be5644fdea+\\' )
49 $this->assertNull( Token
::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be5644fdea-\\' ) );
50 $this->assertNull( Token
::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be+\\' ) );
52 $this->assertNull( Token
::getTimestamp( 'ee2f7a2488dea9x76c224cfb400d43be5644fdea+\\' ) );
55 public function testMatch() {
56 $token = \TestingAccessWrapper
::newFromObject( new Token( 'sekret', 'salty', false ) );
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 ) );
63 $this->assertFalse( $token->match( 'ee2f7a2488dea9176c224cfb400d43be5644fdea-\\' ) );