aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/auth/RememberMeAuthenticationRequestTest.php
blob: 99a6537152939917e7f601973c83557eca70e777 (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

namespace MediaWiki\Tests\Auth;

use MediaWiki\Auth\RememberMeAuthenticationRequest;
use Wikimedia\TestingAccessWrapper;

/**
 * @group AuthManager
 * @covers \MediaWiki\Auth\RememberMeAuthenticationRequest
 */
class RememberMeAuthenticationRequestTest extends AuthenticationRequestTestCase {

	public static function provideGetFieldInfo() {
		return [
			[ [ 1 ] ],
			[ [ null ] ],
		];
	}

	public function testGetFieldInfo_2() {
		$req = new RememberMeAuthenticationRequest();
		$reqWrapper = TestingAccessWrapper::newFromObject( $req );

		$reqWrapper->expiration = 30 * 24 * 3600;
		$this->assertNotEmpty( $req->getFieldInfo() );

		$reqWrapper->expiration = null;
		$this->assertSame( [], $req->getFieldInfo() );
	}

	public function testNoChoice() {
		$req = new RememberMeAuthenticationRequest(
			RememberMeAuthenticationRequest::ALWAYS_REMEMBER
		);
		$reqWrapper = TestingAccessWrapper::newFromObject( $req );
		$this->assertSame( [], $req->getFieldInfo() );
		$this->assertNotNull( $reqWrapper->expiration );

		$req = new RememberMeAuthenticationRequest(
			RememberMeAuthenticationRequest::NEVER_REMEMBER
		);
		$reqWrapper = TestingAccessWrapper::newFromObject( $req );
		$this->assertSame( [], $req->getFieldInfo() );
		$this->assertNull( $reqWrapper->expiration );
	}

	public function testInvalid() {
		$this->expectException( '\UnexpectedValueException' );
		new RememberMeAuthenticationRequest( 'invalid value' );
	}

	protected function getInstance( array $args = [] ) {
		if ( isset( $args[1] ) ) {
			$req = new RememberMeAuthenticationRequest( $args[1] );
		} else {
			$req = new RememberMeAuthenticationRequest();
		}
		$reqWrapper = TestingAccessWrapper::newFromObject( $req );
		$reqWrapper->expiration = $args[0];
		return $req;
	}

	public static function provideLoadFromSubmission() {
		return [
			'Empty request' => [
				[ 30 * 24 * 3600 ],
				[],
				[ 'expiration' => 30 * 24 * 3600, 'rememberMe' => false ]
			],
			'RememberMe present' => [
				[ 30 * 24 * 3600 ],
				[ 'rememberMe' => '' ],
				[ 'expiration' => 30 * 24 * 3600, 'rememberMe' => true ]
			],
			'RememberMe present but session provider cannot remember' => [
				[ null ],
				[ 'rememberMe' => '' ],
				false
			],
			'Empty request (CHOOSE_REMEMBER)' => [
				[ 30 * 24 * 3600, RememberMeAuthenticationRequest::CHOOSE_REMEMBER ],
				[],
				[ 'expiration' => 30 * 24 * 3600, 'rememberMe' => false ]
			],
			'RememberMe present (CHOOSE_REMEMBER)' => [
				[ 30 * 24 * 3600, RememberMeAuthenticationRequest::CHOOSE_REMEMBER ],
				[ 'rememberMe' => '' ],
				[ 'expiration' => 30 * 24 * 3600, 'rememberMe' => true ]
			],
			'RememberMe present but session provider cannot remember (CHOOSE_REMEMBER)' => [
				[ null, RememberMeAuthenticationRequest::CHOOSE_REMEMBER ],
				[ 'rememberMe' => '' ],
				false
			],
			'Empty request (FORCE_CHOOSE_REMEMBER)' => [
				[ 30 * 24 * 3600, RememberMeAuthenticationRequest::FORCE_CHOOSE_REMEMBER ],
				[],
				[ 'expiration' => 30 * 24 * 3600, 'rememberMe' => false, 'skippable' => false ]
			],
			'RememberMe present (FORCE_CHOOSE_REMEMBER)' => [
				[ 30 * 24 * 3600, RememberMeAuthenticationRequest::FORCE_CHOOSE_REMEMBER ],
				[ 'rememberMe' => '' ],
				[ 'expiration' => 30 * 24 * 3600, 'rememberMe' => true, 'skippable' => false ]
			],
			'RememberMe present but session provider cannot remember (FORCE_CHOOSE_REMEMBER)' => [
				[ null, RememberMeAuthenticationRequest::FORCE_CHOOSE_REMEMBER ],
				[ 'rememberMe' => '', 'skippable' => false ],
				false
			],
		];
	}
}