Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / session / TokenTest.php
blobca001f3654bde5b35b21f0eedf063ba42efea64d
1 <?php
3 namespace MediaWiki\Session;
5 use MediaWikiTestCase;
7 /**
8 * @group 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 ] )
17 ->getMock();
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 ) );
32 $this->assertSame(
33 'd9ade0c7d4349e9df9094e61c33a5a0d5644fde2+\\',
34 $token->toStringAtTimestamp( 1447362018 )
36 $this->assertSame(
37 'ee2f7a2488dea9176c224cfb400d43be5644fdea+\\',
38 $token->toStringAtTimestamp( 1447362026 )
42 public function testGetTimestamp() {
43 $this->assertSame(
44 1447362018, Token::getTimestamp( 'd9ade0c7d4349e9df9094e61c33a5a0d5644fde2+\\' )
46 $this->assertSame(
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-\\' ) );