MediaWiki REL1_31
TokenTest.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Session;
4
6use Wikimedia\TestingAccessWrapper;
7
13
14 public function testBasics() {
15 $token = $this->getMockBuilder( Token::class )
16 ->setMethods( [ 'toStringAtTimestamp' ] )
17 ->setConstructorArgs( [ 'sekret', 'salty', true ] )
18 ->getMock();
19 $token->expects( $this->any() )->method( 'toStringAtTimestamp' )
20 ->will( $this->returnValue( 'faketoken+\\' ) );
21
22 $this->assertSame( 'faketoken+\\', $token->toString() );
23 $this->assertSame( 'faketoken+\\', (string)$token );
24 $this->assertTrue( $token->wasNew() );
25
26 $token = new Token( 'sekret', 'salty', false );
27 $this->assertFalse( $token->wasNew() );
28 }
29
30 public function testToStringAtTimestamp() {
31 $token = TestingAccessWrapper::newFromObject( new Token( 'sekret', 'salty', false ) );
32
33 $this->assertSame(
34 'd9ade0c7d4349e9df9094e61c33a5a0d5644fde2+\\',
35 $token->toStringAtTimestamp( 1447362018 )
36 );
37 $this->assertSame(
38 'ee2f7a2488dea9176c224cfb400d43be5644fdea+\\',
39 $token->toStringAtTimestamp( 1447362026 )
40 );
41 }
42
43 public function testGetTimestamp() {
44 $this->assertSame(
45 1447362018, Token::getTimestamp( 'd9ade0c7d4349e9df9094e61c33a5a0d5644fde2+\\' )
46 );
47 $this->assertSame(
48 1447362026, Token::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be5644fdea+\\' )
49 );
50 $this->assertNull( Token::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be5644fdea-\\' ) );
51 $this->assertNull( Token::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be+\\' ) );
52
53 $this->assertNull( Token::getTimestamp( 'ee2f7a2488dea9x76c224cfb400d43be5644fdea+\\' ) );
54 }
55
56 public function testMatch() {
57 $token = TestingAccessWrapper::newFromObject( new Token( 'sekret', 'salty', false ) );
58
59 $test = $token->toStringAtTimestamp( time() - 10 );
60 $this->assertTrue( $token->match( $test ) );
61 $this->assertTrue( $token->match( $test, 12 ) );
62 $this->assertFalse( $token->match( $test, 8 ) );
63
64 $this->assertFalse( $token->match( 'ee2f7a2488dea9176c224cfb400d43be5644fdea-\\' ) );
65 }
66
67}
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition COPYING.txt:326
Session MediaWiki\Session\Token.
Definition TokenTest.php:12
Value object representing a CSRF token.
Definition Token.php:32
static getTimestamp( $token)
Decode the timestamp from a token string.
Definition Token.php:61