MediaWiki  master
EmailNotificationSecondaryAuthenticationProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Auth;
4 
7 
11 class EmailNotificationSecondaryAuthenticationProviderTest extends \PHPUnit\Framework\TestCase {
12  public function testConstructor() {
13  $config = new \HashConfig( [
14  'EnableEmail' => true,
15  'EmailAuthentication' => true,
16  ] );
17 
19  $provider->setConfig( $config );
20  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
21  $this->assertTrue( $providerPriv->sendConfirmationEmail );
22 
24  'sendConfirmationEmail' => false,
25  ] );
26  $provider->setConfig( $config );
27  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
28  $this->assertFalse( $providerPriv->sendConfirmationEmail );
29  }
30 
36  public function testGetAuthenticationRequests( $action, $expected ) {
38  'sendConfirmationEmail' => true,
39  ] );
40  $this->assertSame( $expected, $provider->getAuthenticationRequests( $action, [] ) );
41  }
42 
44  return [
50  ];
51  }
52 
55  'sendConfirmationEmail' => true,
56  ] );
57  $this->assertEquals( AuthenticationResponse::newAbstain(),
58  $provider->beginSecondaryAuthentication( \User::newFromName( 'Foo' ), [] ) );
59  }
60 
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 
89  'sendConfirmationEmail' => false,
90  ] );
91  $provider->setManager( $authManager );
92  $provider->beginSecondaryAccountCreation( $userNotExpectsConfirmation, $creator, [] );
93 
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 }
Config $config
Definition: MediaWiki.php:43
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
string $action
Cache what action this request is.
Definition: MediaWiki.php:48
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:68
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
const ACTION_CHANGE
Change a user&#39;s credentials.
This serves as the entry point to the authentication system.
Definition: AuthManager.php:83
const ACTION_LINK
Link an existing user to a third-party account.
Definition: AuthManager.php:95
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
const ACTION_REMOVE
Remove a user&#39;s credentials.
const ACTION_CREATE
Create a new user.
Definition: AuthManager.php:90
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:585
const ACTION_LOGIN
Log in with an existing (not necessarily local) user.
Definition: AuthManager.php:85
Handles email notification / email address confirmation for account creation.
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28