3 namespace MediaWiki\Auth
;
5 use Psr\Log\LoggerInterface
;
6 use Wikimedia\Rdbms\IDatabase
;
7 use Wikimedia\Rdbms\LoadBalancer
;
8 use Wikimedia\TestingAccessWrapper
;
11 * @covers \MediaWiki\Auth\EmailNotificationSecondaryAuthenticationProvider
13 class EmailNotificationSecondaryAuthenticationProviderTest
extends \MediaWikiUnitTestCase
{
15 protected function setUp() {
18 $lbMock = $this->createMock( LoadBalancer
::class );
19 $dbMock = $this->getMockBuilder( IDatabase
::class )
20 ->disableOriginalConstructor()
21 ->getMockForAbstractClass();
23 $lbMock->expects( $this->any() )
24 ->method( 'getConnection' )
25 ->willReturn( $dbMock );
26 $dbMock->expects( $this->any() )
27 ->method( 'onTransactionCommitOrIdle' )
28 ->willReturnCallback( function ( callable
$callback ) {
32 $lbMockFactory = function () use ( $lbMock ): LoadBalancer
{
36 $this->overrideMwServices( [ 'DBLoadBalancer' => $lbMockFactory ] );
39 public function testConstructor() {
40 $config = new \
HashConfig( [
41 'EnableEmail' => true,
42 'EmailAuthentication' => true,
45 $provider = new EmailNotificationSecondaryAuthenticationProvider();
46 $provider->setConfig( $config );
47 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
48 $this->assertTrue( $providerPriv->sendConfirmationEmail
);
50 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
51 'sendConfirmationEmail' => false,
53 $provider->setConfig( $config );
54 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
55 $this->assertFalse( $providerPriv->sendConfirmationEmail
);
59 * @dataProvider provideGetAuthenticationRequests
60 * @param string $action
61 * @param AuthenticationRequest[] $expected
63 public function testGetAuthenticationRequests( $action, $expected ) {
64 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
65 'sendConfirmationEmail' => true,
67 $this->assertSame( $expected, $provider->getAuthenticationRequests( $action, [] ) );
70 public function provideGetAuthenticationRequests() {
72 [ AuthManager
::ACTION_LOGIN
, [] ],
73 [ AuthManager
::ACTION_CREATE
, [] ],
74 [ AuthManager
::ACTION_LINK
, [] ],
75 [ AuthManager
::ACTION_CHANGE
, [] ],
76 [ AuthManager
::ACTION_REMOVE
, [] ],
80 public function testBeginSecondaryAuthentication() {
81 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
82 'sendConfirmationEmail' => true,
84 $this->assertEquals( AuthenticationResponse
::newAbstain(),
85 $provider->beginSecondaryAuthentication( \User
::newFromName( 'Foo' ), [] ) );
88 public function testBeginSecondaryAccountCreation() {
89 $authManager = new AuthManager( new \
FauxRequest(), new \
HashConfig() );
91 $creator = $this->getMockBuilder( \User
::class )->getMock();
92 $userWithoutEmail = $this->getMockBuilder( \User
::class )->getMock();
93 $userWithoutEmail->expects( $this->any() )->method( 'getEmail' )->willReturn( '' );
94 $userWithoutEmail->expects( $this->any() )->method( 'getInstanceForUpdate' )->willReturnSelf();
95 $userWithoutEmail->expects( $this->never() )->method( 'sendConfirmationMail' );
96 $userWithEmailError = $this->getMockBuilder( \User
::class )->getMock();
97 $userWithEmailError->expects( $this->any() )->method( 'getEmail' )->willReturn( 'foo@bar.baz' );
98 $userWithEmailError->expects( $this->any() )->method( 'getInstanceForUpdate' )->willReturnSelf();
99 $userWithEmailError->expects( $this->any() )->method( 'sendConfirmationMail' )
100 ->willReturn( \Status
::newFatal( 'fail' ) );
101 $userExpectsConfirmation = $this->getMockBuilder( \User
::class )->getMock();
102 $userExpectsConfirmation->expects( $this->any() )->method( 'getEmail' )
103 ->willReturn( 'foo@bar.baz' );
104 $userExpectsConfirmation->expects( $this->any() )->method( 'getInstanceForUpdate' )
106 $userExpectsConfirmation->expects( $this->once() )->method( 'sendConfirmationMail' )
107 ->willReturn( \Status
::newGood() );
108 $userNotExpectsConfirmation = $this->getMockBuilder( \User
::class )->getMock();
109 $userNotExpectsConfirmation->expects( $this->any() )->method( 'getEmail' )
110 ->willReturn( 'foo@bar.baz' );
111 $userNotExpectsConfirmation->expects( $this->any() )->method( 'getInstanceForUpdate' )
113 $userNotExpectsConfirmation->expects( $this->never() )->method( 'sendConfirmationMail' );
115 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
116 'sendConfirmationEmail' => false,
118 $provider->setManager( $authManager );
119 $provider->beginSecondaryAccountCreation( $userNotExpectsConfirmation, $creator, [] );
121 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
122 'sendConfirmationEmail' => true,
124 $provider->setManager( $authManager );
125 $provider->beginSecondaryAccountCreation( $userWithoutEmail, $creator, [] );
126 $provider->beginSecondaryAccountCreation( $userExpectsConfirmation, $creator, [] );
128 // test logging of email errors
129 $logger = $this->getMockForAbstractClass( LoggerInterface
::class );
130 $logger->expects( $this->once() )->method( 'warning' );
131 $provider->setLogger( $logger );
132 $provider->beginSecondaryAccountCreation( $userWithEmailError, $creator, [] );
134 // test disable flag used by other providers
135 $authManager->setAuthenticationSessionData( 'no-email', true );
136 $provider->setManager( $authManager );
137 $provider->beginSecondaryAccountCreation( $userNotExpectsConfirmation, $creator, [] );