3 use MediaWiki\Context\RequestContext
;
4 use MediaWiki\Request\FauxRequest
;
5 use MediaWiki\Tests\User\TempUser\TempUserTestTrait
;
6 use MediaWiki\Title\Title
;
9 * @covers \UserNotLoggedIn
14 class UserNotLoggedInTest
extends MediaWikiIntegrationTestCase
{
16 use TempUserTestTrait
;
18 /** @dataProvider provideConstruction */
19 public function testConstruction( $userIsTemp, $expectedReasonMsgKey ) {
21 $this->enableAutoCreateTempUser();
22 $user = $this->getServiceContainer()->getTempUserCreator()->create( null, new FauxRequest() )->getUser();
23 RequestContext
::getMain()->setUser( $user );
25 $e = new UserNotLoggedIn();
26 $this->assertEquals( 'exception-nologin', $e->title
);
27 $this->assertEquals( $expectedReasonMsgKey, $e->msg
);
28 $this->assertEquals( [], $e->params
);
31 public static function provideConstruction() {
33 'User is not a temporary account' => [ false, 'exception-nologin-text' ],
34 'User is a temporary account' => [ true, 'exception-nologin-text-for-temp-user' ],
38 public function testConstructionForTempAccountWithAlwaysRedirectToLoginPageSet() {
39 $this->enableAutoCreateTempUser();
40 $user = $this->getServiceContainer()->getTempUserCreator()->create( null, new FauxRequest() )->getUser();
41 RequestContext
::getMain()->setUser( $user );
42 $e = new UserNotLoggedIn( 'exception-nologin-text', 'exception-nologin', [], true );
43 $this->assertEquals( 'exception-nologin', $e->title
);
44 $this->assertEquals( 'exception-nologin-text', $e->msg
);
45 $this->assertEquals( [], $e->params
);
48 public function testConstructionForReasonMsgWithoutTemporaryAccountEquivalent() {
49 $this->enableAutoCreateTempUser();
50 $user = $this->getServiceContainer()->getTempUserCreator()->create( null, new FauxRequest() )->getUser();
51 RequestContext
::getMain()->setUser( $user );
52 $e = new UserNotLoggedIn( 'changeemail-no-info' );
53 $this->assertEquals( 'changeemail-no-info', $e->msg
);
56 /** @dataProvider provideTemporaryAccountsEnabled */
57 public function testReportForRedirectToLoginPage( $temporaryAccountsEnabled ) {
58 if ( $temporaryAccountsEnabled ) {
59 $this->enableAutoCreateTempUser();
61 $this->disableAutoCreateTempUser();
63 RequestContext
::getMain()->setTitle( Title
::newFromText( 'Preferences', NS_SPECIAL
) );
64 $e = new UserNotLoggedIn();
66 $redirectUrl = RequestContext
::getMain()->getOutput()->getRedirect();
67 $parsedUrlParts = $this->getServiceContainer()->getUrlUtils()->parse( $redirectUrl );
68 $this->assertNotNull( $parsedUrlParts );
69 $this->assertArrayEquals(
71 'title' => 'Special:UserLogin',
72 'returntoquery' => '',
73 'returnto' => 'Special:Preferences',
74 'warning' => 'exception-nologin-text',
76 wfCgiToArray( $parsedUrlParts['query'] ),
82 public static function provideTemporaryAccountsEnabled() {
84 'Temporary accounts disabled' => [ false ],
85 'Temporary accounts enabled' => [ true ],
89 public function testReportForRedirectToAccountCreationPage() {
90 $this->enableAutoCreateTempUser();
91 $user = $this->getServiceContainer()->getTempUserCreator()->create( null, new FauxRequest() )->getUser();
92 RequestContext
::getMain()->setUser( $user );
93 RequestContext
::getMain()->setTitle( Title
::newFromText( 'Preferences', NS_SPECIAL
) );
94 $e = new UserNotLoggedIn();
96 $redirectUrl = RequestContext
::getMain()->getOutput()->getRedirect();
97 $parsedUrlParts = $this->getServiceContainer()->getUrlUtils()->parse( $redirectUrl );
98 $this->assertNotNull( $parsedUrlParts );
99 $this->assertArrayEquals(
101 'title' => 'Special:CreateAccount',
102 'returntoquery' => '',
103 'returnto' => 'Special:Preferences',
104 'warning' => 'exception-nologin-text-for-temp-user',
106 wfCgiToArray( $parsedUrlParts['query'] ),