Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / exception / UserNotLoggedInTest.php
blob935643493e2d2fe61a7f29dbf92979659c54c78b
1 <?php
3 use MediaWiki\Context\RequestContext;
4 use MediaWiki\Request\FauxRequest;
5 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
6 use MediaWiki\Title\Title;
8 /**
9 * @covers \UserNotLoggedIn
10 * @author Addshore
11 * @author Dreamy Jazz
12 * @group Database
14 class UserNotLoggedInTest extends MediaWikiIntegrationTestCase {
16 use TempUserTestTrait;
18 /** @dataProvider provideConstruction */
19 public function testConstruction( $userIsTemp, $expectedReasonMsgKey ) {
20 if ( $userIsTemp ) {
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() {
32 return [
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();
60 } else {
61 $this->disableAutoCreateTempUser();
63 RequestContext::getMain()->setTitle( Title::newFromText( 'Preferences', NS_SPECIAL ) );
64 $e = new UserNotLoggedIn();
65 $e->report();
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'] ),
77 false,
78 true
82 public static function provideTemporaryAccountsEnabled() {
83 return [
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();
95 $e->report();
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'] ),
107 false,
108 true