3 namespace MediaWiki\Auth
;
5 use Psr\Log\LoggerInterface
;
6 use Wikimedia\TestingAccessWrapper
;
9 * @covers \MediaWiki\Auth\EmailNotificationSecondaryAuthenticationProvider
11 class EmailNotificationSecondaryAuthenticationProviderTest
extends \PHPUnit\Framework\TestCase
{
12 public function testConstructor() {
13 $config = new \
HashConfig( [
14 'EnableEmail' => true,
15 'EmailAuthentication' => true,
18 $provider = new EmailNotificationSecondaryAuthenticationProvider();
19 $provider->setConfig( $config );
20 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
21 $this->assertTrue( $providerPriv->sendConfirmationEmail
);
23 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
24 'sendConfirmationEmail' => false,
26 $provider->setConfig( $config );
27 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
28 $this->assertFalse( $providerPriv->sendConfirmationEmail
);
32 * @dataProvider provideGetAuthenticationRequests
33 * @param string $action
34 * @param AuthenticationRequest[] $expected
36 public function testGetAuthenticationRequests( $action, $expected ) {
37 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
38 'sendConfirmationEmail' => true,
40 $this->assertSame( $expected, $provider->getAuthenticationRequests( $action, [] ) );
43 public function provideGetAuthenticationRequests() {
45 [ AuthManager
::ACTION_LOGIN
, [] ],
46 [ AuthManager
::ACTION_CREATE
, [] ],
47 [ AuthManager
::ACTION_LINK
, [] ],
48 [ AuthManager
::ACTION_CHANGE
, [] ],
49 [ AuthManager
::ACTION_REMOVE
, [] ],
53 public function testBeginSecondaryAuthentication() {
54 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
55 'sendConfirmationEmail' => true,
57 $this->assertEquals( AuthenticationResponse
::newAbstain(),
58 $provider->beginSecondaryAuthentication( \User
::newFromName( 'Foo' ), [] ) );
61 public function testBeginSecondaryAccountCreation() {
62 $authManager = new AuthManager( new \
FauxRequest(), new \
HashConfig() );
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' )
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' )
86 $userNotExpectsConfirmation->expects( $this->never() )->method( 'sendConfirmationMail' );
88 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
89 'sendConfirmationEmail' => false,
91 $provider->setManager( $authManager );
92 $provider->beginSecondaryAccountCreation( $userNotExpectsConfirmation, $creator, [] );
94 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
95 'sendConfirmationEmail' => true,
97 $provider->setManager( $authManager );
98 $provider->beginSecondaryAccountCreation( $userWithoutEmail, $creator, [] );
99 $provider->beginSecondaryAccountCreation( $userExpectsConfirmation, $creator, [] );
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, [] );
107 // test disable flag used by other providers
108 $authManager->setAuthenticationSessionData( 'no-email', true );
109 $provider->setManager( $authManager );
110 $provider->beginSecondaryAccountCreation( $userNotExpectsConfirmation, $creator, [] );