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