Separate MediaWiki unit and integration tests
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / auth / EmailNotificationSecondaryAuthenticationProviderTest.php
1 <?php
2
3 namespace MediaWiki\Auth;
4
5 use Psr\Log\LoggerInterface;
6 use Wikimedia\Rdbms\IDatabase;
7 use Wikimedia\Rdbms\LoadBalancer;
8 use Wikimedia\TestingAccessWrapper;
9
10 /**
11 * @covers \MediaWiki\Auth\EmailNotificationSecondaryAuthenticationProvider
12 */
13 class EmailNotificationSecondaryAuthenticationProviderTest extends \MediaWikiUnitTestCase {
14
15 protected function setUp() {
16 parent::setUp();
17
18 $lbMock = $this->createMock( LoadBalancer::class );
19 $dbMock = $this->getMockBuilder( IDatabase::class )
20 ->disableOriginalConstructor()
21 ->getMockForAbstractClass();
22
23 $lbMock->expects( $this->any() )
24 ->method( 'getConnection' )
25 ->willReturn( $dbMock );
26 $dbMock->expects( $this->any() )
27 ->method( 'onTransactionCommitOrIdle' )
28 ->willReturnCallback( function ( callable $callback ) {
29 $callback();
30 } );
31
32 $lbMockFactory = function () use ( $lbMock ): LoadBalancer {
33 return $lbMock;
34 };
35
36 $this->overrideMwServices( [ 'DBLoadBalancer' => $lbMockFactory ] );
37 }
38
39 public function testConstructor() {
40 $config = new \HashConfig( [
41 'EnableEmail' => true,
42 'EmailAuthentication' => true,
43 ] );
44
45 $provider = new EmailNotificationSecondaryAuthenticationProvider();
46 $provider->setConfig( $config );
47 $providerPriv = TestingAccessWrapper::newFromObject( $provider );
48 $this->assertTrue( $providerPriv->sendConfirmationEmail );
49
50 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
51 'sendConfirmationEmail' => false,
52 ] );
53 $provider->setConfig( $config );
54 $providerPriv = TestingAccessWrapper::newFromObject( $provider );
55 $this->assertFalse( $providerPriv->sendConfirmationEmail );
56 }
57
58 /**
59 * @dataProvider provideGetAuthenticationRequests
60 * @param string $action
61 * @param AuthenticationRequest[] $expected
62 */
63 public function testGetAuthenticationRequests( $action, $expected ) {
64 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
65 'sendConfirmationEmail' => true,
66 ] );
67 $this->assertSame( $expected, $provider->getAuthenticationRequests( $action, [] ) );
68 }
69
70 public function provideGetAuthenticationRequests() {
71 return [
72 [ AuthManager::ACTION_LOGIN, [] ],
73 [ AuthManager::ACTION_CREATE, [] ],
74 [ AuthManager::ACTION_LINK, [] ],
75 [ AuthManager::ACTION_CHANGE, [] ],
76 [ AuthManager::ACTION_REMOVE, [] ],
77 ];
78 }
79
80 public function testBeginSecondaryAuthentication() {
81 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
82 'sendConfirmationEmail' => true,
83 ] );
84 $this->assertEquals( AuthenticationResponse::newAbstain(),
85 $provider->beginSecondaryAuthentication( \User::newFromName( 'Foo' ), [] ) );
86 }
87
88 public function testBeginSecondaryAccountCreation() {
89 $authManager = new AuthManager( new \FauxRequest(), new \HashConfig() );
90
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' )
105 ->willReturnSelf();
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' )
112 ->willReturnSelf();
113 $userNotExpectsConfirmation->expects( $this->never() )->method( 'sendConfirmationMail' );
114
115 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
116 'sendConfirmationEmail' => false,
117 ] );
118 $provider->setManager( $authManager );
119 $provider->beginSecondaryAccountCreation( $userNotExpectsConfirmation, $creator, [] );
120
121 $provider = new EmailNotificationSecondaryAuthenticationProvider( [
122 'sendConfirmationEmail' => true,
123 ] );
124 $provider->setManager( $authManager );
125 $provider->beginSecondaryAccountCreation( $userWithoutEmail, $creator, [] );
126 $provider->beginSecondaryAccountCreation( $userExpectsConfirmation, $creator, [] );
127
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, [] );
133
134 // test disable flag used by other providers
135 $authManager->setAuthenticationSessionData( 'no-email', true );
136 $provider->setManager( $authManager );
137 $provider->beginSecondaryAccountCreation( $userNotExpectsConfirmation, $creator, [] );
138 }
139 }