1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
<?php
use MediaWiki\Context\RequestContext;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
use MediaWiki\Title\Title;
/**
* @covers \UserNotLoggedIn
* @author Addshore
* @author Dreamy Jazz
* @group Database
*/
class UserNotLoggedInTest extends MediaWikiIntegrationTestCase {
use TempUserTestTrait;
/** @dataProvider provideConstruction */
public function testConstruction( $userIsTemp, $expectedReasonMsgKey ) {
if ( $userIsTemp ) {
$this->enableAutoCreateTempUser();
$user = $this->getServiceContainer()->getTempUserCreator()->create( null, new FauxRequest() )->getUser();
RequestContext::getMain()->setUser( $user );
}
$e = new UserNotLoggedIn();
$this->assertEquals( 'exception-nologin', $e->title );
$this->assertEquals( $expectedReasonMsgKey, $e->msg );
$this->assertEquals( [], $e->params );
}
public static function provideConstruction() {
return [
'User is not a temporary account' => [ false, 'exception-nologin-text' ],
'User is a temporary account' => [ true, 'exception-nologin-text-for-temp-user' ],
];
}
public function testConstructionForTempAccountWithAlwaysRedirectToLoginPageSet() {
$this->enableAutoCreateTempUser();
$user = $this->getServiceContainer()->getTempUserCreator()->create( null, new FauxRequest() )->getUser();
RequestContext::getMain()->setUser( $user );
$e = new UserNotLoggedIn( 'exception-nologin-text', 'exception-nologin', [], true );
$this->assertEquals( 'exception-nologin', $e->title );
$this->assertEquals( 'exception-nologin-text', $e->msg );
$this->assertEquals( [], $e->params );
}
public function testConstructionForReasonMsgWithoutTemporaryAccountEquivalent() {
$this->enableAutoCreateTempUser();
$user = $this->getServiceContainer()->getTempUserCreator()->create( null, new FauxRequest() )->getUser();
RequestContext::getMain()->setUser( $user );
$e = new UserNotLoggedIn( 'changeemail-no-info' );
$this->assertEquals( 'changeemail-no-info', $e->msg );
}
/** @dataProvider provideTemporaryAccountsEnabled */
public function testReportForRedirectToLoginPage( $temporaryAccountsEnabled ) {
if ( $temporaryAccountsEnabled ) {
$this->enableAutoCreateTempUser();
} else {
$this->disableAutoCreateTempUser();
}
RequestContext::getMain()->setTitle( Title::newFromText( 'Preferences', NS_SPECIAL ) );
$e = new UserNotLoggedIn();
$e->report();
$redirectUrl = RequestContext::getMain()->getOutput()->getRedirect();
$parsedUrlParts = $this->getServiceContainer()->getUrlUtils()->parse( $redirectUrl );
$this->assertNotNull( $parsedUrlParts );
$this->assertArrayEquals(
[
'title' => 'Special:UserLogin',
'returntoquery' => '',
'returnto' => 'Special:Preferences',
'warning' => 'exception-nologin-text',
],
wfCgiToArray( $parsedUrlParts['query'] ),
false,
true
);
}
public static function provideTemporaryAccountsEnabled() {
return [
'Temporary accounts disabled' => [ false ],
'Temporary accounts enabled' => [ true ],
];
}
public function testReportForRedirectToAccountCreationPage() {
$this->enableAutoCreateTempUser();
$user = $this->getServiceContainer()->getTempUserCreator()->create( null, new FauxRequest() )->getUser();
RequestContext::getMain()->setUser( $user );
RequestContext::getMain()->setTitle( Title::newFromText( 'Preferences', NS_SPECIAL ) );
$e = new UserNotLoggedIn();
$e->report();
$redirectUrl = RequestContext::getMain()->getOutput()->getRedirect();
$parsedUrlParts = $this->getServiceContainer()->getUrlUtils()->parse( $redirectUrl );
$this->assertNotNull( $parsedUrlParts );
$this->assertArrayEquals(
[
'title' => 'Special:CreateAccount',
'returntoquery' => '',
'returnto' => 'Special:Preferences',
'warning' => 'exception-nologin-text-for-temp-user',
],
wfCgiToArray( $parsedUrlParts['query'] ),
false,
true
);
}
}
|