3 namespace MediaWiki\Auth
;
5 use Psr\Log\LoggerInterface
;
6 use Wikimedia\TestingAccessWrapper
;
8 class EmailNotificationSecondaryAuthenticationProviderTest
extends \PHPUnit_Framework_TestCase
{
9 public function testConstructor() {
10 $config = new \
HashConfig( [
11 'EnableEmail' => true,
12 'EmailAuthentication' => true,
15 $provider = new EmailNotificationSecondaryAuthenticationProvider();
16 $provider->setConfig( $config );
17 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
18 $this->assertTrue( $providerPriv->sendConfirmationEmail
);
20 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
21 'sendConfirmationEmail' => false,
23 $provider->setConfig( $config );
24 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
25 $this->assertFalse( $providerPriv->sendConfirmationEmail
);
29 * @dataProvider provideGetAuthenticationRequests
30 * @param string $action
31 * @param AuthenticationRequest[] $expected
33 public function testGetAuthenticationRequests( $action, $expected ) {
34 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
35 'sendConfirmationEmail' => true,
37 $this->assertSame( $expected, $provider->getAuthenticationRequests( $action, [] ) );
40 public function provideGetAuthenticationRequests() {
42 [ AuthManager
::ACTION_LOGIN
, [] ],
43 [ AuthManager
::ACTION_CREATE
, [] ],
44 [ AuthManager
::ACTION_LINK
, [] ],
45 [ AuthManager
::ACTION_CHANGE
, [] ],
46 [ AuthManager
::ACTION_REMOVE
, [] ],
50 public function testBeginSecondaryAuthentication() {
51 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
52 'sendConfirmationEmail' => true,
54 $this->assertEquals( AuthenticationResponse
::newAbstain(),
55 $provider->beginSecondaryAuthentication( \User
::newFromName( 'Foo' ), [] ) );
58 public function testBeginSecondaryAccountCreation() {
59 $authManager = new AuthManager( new \
FauxRequest(), new \
HashConfig() );
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' )
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' )
83 $userNotExpectsConfirmation->expects( $this->never() )->method( 'sendConfirmationMail' );
85 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
86 'sendConfirmationEmail' => false,
88 $provider->setManager( $authManager );
89 $provider->beginSecondaryAccountCreation( $userNotExpectsConfirmation, $creator, [] );
91 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
92 'sendConfirmationEmail' => true,
94 $provider->setManager( $authManager );
95 $provider->beginSecondaryAccountCreation( $userWithoutEmail, $creator, [] );
96 $provider->beginSecondaryAccountCreation( $userExpectsConfirmation, $creator, [] );
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, [] );
104 // test disable flag used by other providers
105 $authManager->setAuthenticationSessionData( 'no-email', true );
106 $provider->setManager( $authManager );
107 $provider->beginSecondaryAccountCreation( $userNotExpectsConfirmation, $creator, [] );