Merge "Convert CREDITS to list of all known git contributors"
[lhc/web/wiklou.git] / tests / phpunit / includes / auth / EmailNotificationSecondaryAuthenticationProviderTest.php
1 <?php
2
3 namespace MediaWiki\Auth;
4
5 use Psr\Log\LoggerInterface;
6
7 class EmailNotificationSecondaryAuthenticationProviderTest extends \PHPUnit_Framework_TestCase {
8 public function testConstructor() {
9 $config = new \HashConfig( [
10 'EnableEmail' => true,
11 'EmailAuthentication' => true,
12 ] );
13
14 $provider = new EmailNotificationSecondaryAuthenticationProvider();
15 $provider->setConfig( $config );
16 $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
17 $this->assertTrue( $providerPriv->sendConfirmationEmail );
18
19 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
20 'sendConfirmationEmail' => false,
21 ] );
22 $provider->setConfig( $config );
23 $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
24 $this->assertFalse( $providerPriv->sendConfirmationEmail );
25 }
26
27 /**
28 * @dataProvider provideGetAuthenticationRequests
29 * @param string $action
30 * @param AuthenticationRequest[] $expected
31 */
32 public function testGetAuthenticationRequests( $action, $expected ) {
33 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
34 'sendConfirmationEmail' => true,
35 ] );
36 $this->assertSame( $expected, $provider->getAuthenticationRequests( $action, [] ) );
37 }
38
39 public function provideGetAuthenticationRequests() {
40 return [
41 [ AuthManager::ACTION_LOGIN, [] ],
42 [ AuthManager::ACTION_CREATE, [] ],
43 [ AuthManager::ACTION_LINK, [] ],
44 [ AuthManager::ACTION_CHANGE, [] ],
45 [ AuthManager::ACTION_REMOVE, [] ],
46 ];
47 }
48
49 public function testBeginSecondaryAuthentication() {
50 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
51 'sendConfirmationEmail' => true,
52 ] );
53 $this->assertEquals( AuthenticationResponse::newAbstain(),
54 $provider->beginSecondaryAuthentication( \User::newFromName( 'Foo' ), [] ) );
55 }
56
57 public function testBeginSecondaryAccountCreation() {
58 $authManager = new AuthManager( new \FauxRequest(), new \HashConfig() );
59
60 $creator = $this->getMock( 'User' );
61 $userWithoutEmail = $this->getMock( 'User' );
62 $userWithoutEmail->expects( $this->any() )->method( 'getEmail' )->willReturn( '' );
63 $userWithoutEmail->expects( $this->any() )->method( 'getInstanceForUpdate' )->willReturnSelf();
64 $userWithoutEmail->expects( $this->never() )->method( 'sendConfirmationMail' );
65 $userWithEmailError = $this->getMock( 'User' );
66 $userWithEmailError->expects( $this->any() )->method( 'getEmail' )->willReturn( 'foo@bar.baz' );
67 $userWithEmailError->expects( $this->any() )->method( 'getInstanceForUpdate' )->willReturnSelf();
68 $userWithEmailError->expects( $this->any() )->method( 'sendConfirmationMail' )
69 ->willReturn( \Status::newFatal( 'fail' ) );
70 $userExpectsConfirmation = $this->getMock( 'User' );
71 $userExpectsConfirmation->expects( $this->any() )->method( 'getEmail' )
72 ->willReturn( 'foo@bar.baz' );
73 $userExpectsConfirmation->expects( $this->any() )->method( 'getInstanceForUpdate' )
74 ->willReturnSelf();
75 $userExpectsConfirmation->expects( $this->once() )->method( 'sendConfirmationMail' )
76 ->willReturn( \Status::newGood() );
77 $userNotExpectsConfirmation = $this->getMock( 'User' );
78 $userNotExpectsConfirmation->expects( $this->any() )->method( 'getEmail' )
79 ->willReturn( 'foo@bar.baz' );
80 $userNotExpectsConfirmation->expects( $this->any() )->method( 'getInstanceForUpdate' )
81 ->willReturnSelf();
82 $userNotExpectsConfirmation->expects( $this->never() )->method( 'sendConfirmationMail' );
83
84 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
85 'sendConfirmationEmail' => false,
86 ] );
87 $provider->setManager( $authManager );
88 $provider->beginSecondaryAccountCreation( $userNotExpectsConfirmation, $creator, [] );
89
90 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
91 'sendConfirmationEmail' => true,
92 ] );
93 $provider->setManager( $authManager );
94 $provider->beginSecondaryAccountCreation( $userWithoutEmail, $creator, [] );
95 $provider->beginSecondaryAccountCreation( $userExpectsConfirmation, $creator, [] );
96
97 // test logging of email errors
98 $logger = $this->getMockForAbstractClass( LoggerInterface::class );
99 $logger->expects( $this->once() )->method( 'warning' );
100 $provider->setLogger( $logger );
101 $provider->beginSecondaryAccountCreation( $userWithEmailError, $creator, [] );
102
103 // test disable flag used by other providers
104 $authManager->setAuthenticationSessionData( 'no-email', true );
105 $provider->setManager( $authManager );
106 $provider->beginSecondaryAccountCreation( $userNotExpectsConfirmation, $creator, [] );
107 }
108 }