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