diff options
author | Dreamy Jazz <wpgbrown@wikimedia.org> | 2024-09-24 01:26:14 +0100 |
---|---|---|
committer | Dreamy Jazz <wpgbrown@wikimedia.org> | 2024-09-24 21:42:17 +0000 |
commit | 0fa81768c2a960ae9b7df28518f9efa8fe8b08fa (patch) | |
tree | e3b44e5d394631272095d8150a036544241a514e /tests/phpunit/includes/exception/UserNotLoggedInTest.php | |
parent | 4affdf161bf532e6279d04a9ae5c7a7a0c1a8f11 (diff) | |
download | mediawikicore-0fa81768c2a960ae9b7df28518f9efa8fe8b08fa.tar.gz mediawikicore-0fa81768c2a960ae9b7df28518f9efa8fe8b08fa.zip |
Send temporary accounts to Special:CreateAccount on ::requireNamed
Why:
* When code throws a UserNotLoggedIn error for a temporary account,
this takes them to Special:UserLogin and asks them to log in to
an account
* Users using temporary accounts are less likely to have a named
account, so should instead be sent to Special:CreateAccount
(as described in T358586)
* When doing this, the warning message should be customised to
talk about the user needing to create an account (and not
log in)
What:
* Update UserNotLoggedIn to redirect to Special:CreateAccount
when the user is logged in to a temporary account
** The code which constructs the exception object can override
this and force the redirect to always be to Special:UserLogin.
* When the redirect is to Special:CreateAccount, allow customising
the message shown by adding a '-for-temp-user' suffix to the
message key in UserNotLoggedIn.
** This suffix is only added if the resulting message exists,
so that code can choose whether they need a message that is
different to the one on Special:UserLogin.
* Update SpecialPage::requiredNamedUser to allow special pages to
force the redirect to always be to Special:UserLogin
** This is made use of in Special:ChangeEmail and
Special:ConfirmEmail, as these special pages only make sense
for use with an existing account.
* Update and add message keys to allow a different message for
Special:CreateAccount.
* Update and add tests for these changes.
Bug: T358586
Change-Id: Ie0dd06f51b1e2b85eef6be377f673a02732ce604
Diffstat (limited to 'tests/phpunit/includes/exception/UserNotLoggedInTest.php')
-rw-r--r-- | tests/phpunit/includes/exception/UserNotLoggedInTest.php | 97 |
1 files changed, 96 insertions, 1 deletions
diff --git a/tests/phpunit/includes/exception/UserNotLoggedInTest.php b/tests/phpunit/includes/exception/UserNotLoggedInTest.php index 3e71b6997d16..935643493e2d 100644 --- a/tests/phpunit/includes/exception/UserNotLoggedInTest.php +++ b/tests/phpunit/includes/exception/UserNotLoggedInTest.php @@ -1,16 +1,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 { - public function testConstruction() { + 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 + ); + } } |