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