aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/unit/includes/auth/AbstractSecondaryAuthenticationProviderTest.php
blob: f9795292fece722ece6e8e15680d0326a6f55619 (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
<?php

namespace MediaWiki\Tests\Unit\Auth;

use BadMethodCallException;
use MediaWiki\Auth\AbstractSecondaryAuthenticationProvider;
use MediaWiki\Auth\AuthenticationRequest;
use MediaWiki\Auth\AuthenticationResponse;
use MediaWiki\Auth\AuthManager;
use MediaWiki\User\User;
use MediaWikiUnitTestCase;
use StatusValue;

/**
 * @group AuthManager
 * @covers \MediaWiki\Auth\AbstractSecondaryAuthenticationProvider
 */
class AbstractSecondaryAuthenticationProviderTest extends MediaWikiUnitTestCase {
	public function testAbstractSecondaryAuthenticationProvider() {
		$user = $this->createMock( User::class );

		$provider = $this->getMockForAbstractClass( AbstractSecondaryAuthenticationProvider::class );

		try {
			$provider->continueSecondaryAuthentication( $user, [] );
			$this->fail( 'Expected exception not thrown' );
		} catch ( BadMethodCallException $ex ) {
		}

		try {
			$provider->continueSecondaryAccountCreation( $user, $user, [] );
			$this->fail( 'Expected exception not thrown' );
		} catch ( BadMethodCallException $ex ) {
		}

		$req = $this->getMockForAbstractClass( AuthenticationRequest::class );

		$this->assertTrue( $provider->providerAllowsPropertyChange( 'foo' ) );
		$this->assertEquals(
			StatusValue::newGood( 'ignored' ),
			$provider->providerAllowsAuthenticationDataChange( $req )
		);
		$this->assertEquals(
			StatusValue::newGood(),
			$provider->testForAccountCreation( $user, $user, [] )
		);
		$this->assertEquals(
			StatusValue::newGood(),
			$provider->testUserForCreation( $user, AuthManager::AUTOCREATE_SOURCE_SESSION )
		);
		$this->assertEquals(
			StatusValue::newGood(),
			$provider->testUserForCreation( $user, false )
		);

		$provider->providerChangeAuthenticationData( $req );
		$provider->autoCreatedAccount( $user, AuthManager::AUTOCREATE_SOURCE_SESSION );

		$res = AuthenticationResponse::newPass();
		$provider->postAuthentication( $user, $res );
		$provider->postAccountCreation( $user, $user, $res );
	}

	public function testProviderRevokeAccessForUser() {
		$reqs = [];
		for ( $i = 0; $i < 3; $i++ ) {
			$reqs[$i] = $this->createMock( AuthenticationRequest::class );
		}
		$userName = 'TestProviderRevokeAccessForUser';

		$provider = $this->getMockBuilder( AbstractSecondaryAuthenticationProvider::class )
			->onlyMethods( [ 'providerChangeAuthenticationData' ] )
			->getMockForAbstractClass();
		$provider->expects( $this->once() )->method( 'getAuthenticationRequests' )
			->with(
				$this->identicalTo( AuthManager::ACTION_REMOVE ),
				$this->identicalTo( [ 'username' => $userName ] )
			)
			->willReturn( $reqs );
		$provider->expects( $this->exactly( 3 ) )->method( 'providerChangeAuthenticationData' )
			->willReturnCallback( function ( $req ) use ( $userName ) {
				$this->assertSame( $userName, $req->username );
			} );

		$provider->providerRevokeAccessForUser( $userName );

		foreach ( $reqs as $i => $req ) {
			$this->assertNotNull( $req->username, "#$i" );
		}
	}
}