File: TokenTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (69 lines) | stat: -rw-r--r-- 2,156 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

namespace MediaWiki\Tests\Session;

use MediaWiki\Session\Token;
use MediaWikiUnitTestCase;
use Wikimedia\TestingAccessWrapper;

/**
 * @group Session
 * @covers \MediaWiki\Session\Token
 */
class TokenTest extends MediaWikiUnitTestCase {

	public function testBasics() {
		$token = $this->getMockBuilder( Token::class )
			->onlyMethods( [ 'toStringAtTimestamp' ] )
			->setConstructorArgs( [ 'sekret', 'salty', true ] )
			->getMock();
		$token->method( 'toStringAtTimestamp' )
			->willReturn( 'faketoken+\\' );

		$this->assertSame( 'faketoken+\\', $token->toString() );
		$this->assertSame( 'faketoken+\\', (string)$token );
		$this->assertTrue( $token->wasNew() );

		$token = new Token( 'sekret', 'salty', false );
		$this->assertFalse( $token->wasNew() );
	}

	public function testToStringAtTimestamp() {
		$token = TestingAccessWrapper::newFromObject( new Token( 'sekret', 'salty', false ) );

		$this->assertSame(
			'd9ade0c7d4349e9df9094e61c33a5a0d5644fde2+\\',
			$token->toStringAtTimestamp( 1447362018 )
		);
		$this->assertSame(
			'ee2f7a2488dea9176c224cfb400d43be5644fdea+\\',
			$token->toStringAtTimestamp( 1447362026 )
		);
	}

	public function testGetTimestamp() {
		$this->assertSame(
			1447362018, Token::getTimestamp( 'd9ade0c7d4349e9df9094e61c33a5a0d5644fde2+\\' )
		);
		$this->assertSame(
			1447362026, Token::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be5644fdea+\\' )
		);
		$this->assertNull( Token::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be5644fdea-\\' ) );
		$this->assertNull( Token::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be+\\' ) );

		$this->assertNull( Token::getTimestamp( 'ee2f7a2488dea9x76c224cfb400d43be5644fdea+\\' ) );
	}

	public function testMatch() {
		$token = TestingAccessWrapper::newFromObject( new Token( 'sekret', 'salty', false ) );

		$this->assertFalse( $token->match( null ) );

		$test = $token->toStringAtTimestamp( time() - 10 );
		$this->assertTrue( $token->match( $test ) );
		$this->assertTrue( $token->match( $test, 12 ) );
		$this->assertFalse( $token->match( $test, 8 ) );

		$this->assertFalse( $token->match( 'ee2f7a2488dea9176c224cfb400d43be5644fdea-\\' ) );
	}
}