| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\RequestContext; |
| 4 |
use MediaWiki\Request\FauxRequest; |
| 5 |
use MediaWiki\Tests\User\TempUser\TempUserTestTrait; |
| 6 |
use MediaWiki\Title\Title; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class UserNotLoggedInTest extends MediaWikiIntegrationTestCase { |
| 15 |
|
| 16 |
use TempUserTestTrait; |
| 17 |
|
| 18 |
|
| 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 ); |
| 24 |
} |
| 25 |
$e = new UserNotLoggedIn(); |
| 26 |
$this->assertEquals( 'exception-nologin', $e->title ); |
| 27 |
$this->assertEquals( $expectedReasonMsgKey, $e->msg ); |
| 28 |
$this->assertEquals( [], $e->params ); |
| 29 |
} |
| 30 |
|
| 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' ], |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 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 ); |
| 46 |
} |
| 47 |
|
| 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 ); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
public function testReportForRedirectToLoginPage( $temporaryAccountsEnabled ) { |
| 58 |
if ( $temporaryAccountsEnabled ) { |
| 59 |
$this->enableAutoCreateTempUser(); |
| 60 |
} else { |
| 61 |
$this->disableAutoCreateTempUser(); |
| 62 |
} |
| 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( |
| 70 |
[ |
| 71 |
'title' => 'Special:UserLogin', |
| 72 |
'returntoquery' => '', |
| 73 |
'returnto' => 'Special:Preferences', |
| 74 |
'warning' => 'exception-nologin-text', |
| 75 |
], |
| 76 |
wfCgiToArray( $parsedUrlParts['query'] ), |
| 77 |
false, |
| 78 |
true |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
public static function provideTemporaryAccountsEnabled() { |
| 83 |
return [ |
| 84 |
'Temporary accounts disabled' => [ false ], |
| 85 |
'Temporary accounts enabled' => [ true ], |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 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( |
| 100 |
[ |
| 101 |
'title' => 'Special:CreateAccount', |
| 102 |
'returntoquery' => '', |
| 103 |
'returnto' => 'Special:Preferences', |
| 104 |
'warning' => 'exception-nologin-text-for-temp-user', |
| 105 |
], |
| 106 |
wfCgiToArray( $parsedUrlParts['query'] ), |
| 107 |
false, |
| 108 |
true |
| 109 |
); |
| 110 |
} |
| 111 |
} |