diff options
author | jenkins-bot <jenkins-bot@gerrit.wikimedia.org> | 2024-09-12 11:18:07 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@wikimedia.org> | 2024-09-12 11:18:07 +0000 |
commit | bc055b6ca41a46efb8162edb8ac2166724ed68e4 (patch) | |
tree | 2f09116d54790a28d17349ccbef8c5295c00c924 | |
parent | 2efc9ea4d3bf8e83f5f86e979d5d51597e3c5162 (diff) | |
parent | ab89fff633843b4493de6a58e014a461e02b73f9 (diff) | |
download | mediawikicore-bc055b6ca41a46efb8162edb8ac2166724ed68e4.tar.gz mediawikicore-bc055b6ca41a46efb8162edb8ac2166724ed68e4.zip |
Merge "Customise Special:UserLogout success message for temporary account"
-rw-r--r-- | includes/specialpage/SpecialPageFactory.php | 3 | ||||
-rw-r--r-- | includes/specials/SpecialUserLogout.php | 30 | ||||
-rw-r--r-- | languages/i18n/en.json | 1 | ||||
-rw-r--r-- | languages/i18n/qqq.json | 5 | ||||
-rw-r--r-- | resources/src/mediawiki.page.ready/ready.js | 7 | ||||
-rw-r--r-- | tests/phpunit/includes/specials/SpecialUserLogoutTest.php | 47 |
6 files changed, 87 insertions, 6 deletions
diff --git a/includes/specialpage/SpecialPageFactory.php b/includes/specialpage/SpecialPageFactory.php index fbd78719cddd..511fe56f709c 100644 --- a/includes/specialpage/SpecialPageFactory.php +++ b/includes/specialpage/SpecialPageFactory.php @@ -459,6 +459,9 @@ class SpecialPageFactory { ], 'Userlogout' => [ 'class' => SpecialUserLogout::class, + 'services' => [ + 'TempUserConfig', + ], ], 'CreateAccount' => [ 'class' => SpecialCreateAccount::class, diff --git a/includes/specials/SpecialUserLogout.php b/includes/specials/SpecialUserLogout.php index a6dad05854dd..d67b0e60d775 100644 --- a/includes/specials/SpecialUserLogout.php +++ b/includes/specials/SpecialUserLogout.php @@ -26,6 +26,7 @@ use MediaWiki\Session\SessionManager; use MediaWiki\SpecialPage\FormSpecialPage; use MediaWiki\SpecialPage\SpecialPage; use MediaWiki\Status\Status; +use MediaWiki\User\TempUser\TempUserConfig; /** * Implements Special:Userlogout @@ -39,8 +40,11 @@ class SpecialUserLogout extends FormSpecialPage { */ private $oldUserName; - public function __construct() { + private TempUserConfig $tempUserConfig; + + public function __construct( TempUserConfig $tempUserConfig ) { parent::__construct( 'Userlogout' ); + $this->tempUserConfig = $tempUserConfig; } public function doesWrites() { @@ -125,7 +129,17 @@ class SpecialUserLogout extends FormSpecialPage { $this->getRequest()->getValues( 'returnto', 'returntoquery' ) ); $out = $this->getOutput(); - $out->addWikiMsg( 'logouttext', $loginURL ); + + $messageKey = 'logouttext'; + if ( + ( isset( $this->oldUserName ) && $this->tempUserConfig->isTempName( $this->oldUserName ) ) || + $this->getRequest()->getCheck( 'wasTempUser' ) + ) { + // Generates the message key logouttext-for-temporary-account which is used to customise the success + // message for a temporary account. + $messageKey .= '-for-temporary-account'; + } + $out->addWikiMsg( $messageKey, $loginURL ); $out->returnToMain(); } @@ -137,6 +151,18 @@ class SpecialUserLogout extends FormSpecialPage { public function requiresUnblock() { return false; } + + public function getDescription() { + // Set the page title as "templogout" if the user is (or just was) logged in to a temporary account + if ( + $this->getUser()->isTemp() || + ( isset( $this->oldUserName ) && $this->tempUserConfig->isTempName( $this->oldUserName ) ) || + $this->getRequest()->getCheck( 'wasTempUser' ) + ) { + return $this->msg( 'templogout' ); + } + return parent::getDescription(); + } } /** diff --git a/languages/i18n/en.json b/languages/i18n/en.json index 994427875f91..4c4cdfe8bc0f 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -353,6 +353,7 @@ "virus-scanfailed": "scan failed (code $1)", "virus-unknownscanner": "unknown antivirus:", "logouttext": "<strong>You are now logged out.</strong>\n\nNote that some pages may continue to be displayed as if you were still logged in, until you clear your browser cache.", + "logouttext-for-temporary-account": "<strong>You are now logged out of your temporary account.</strong>\n\nNote that some pages may continue to be displayed as if you were still logged in, until you clear your browser cache.", "logging-out-notify": "You are being logged out, please wait.", "logout-failed": "Cannot log out now: $1", "cannotlogoutnow-title": "Cannot log out now", diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json index 484c3f4d1f74..cf3a8e27140a 100644 --- a/languages/i18n/qqq.json +++ b/languages/i18n/qqq.json @@ -615,7 +615,8 @@ "virus-badscanner": "Used as error message. Parameters:\n* $1 - virus scanner name which is defined in the variable [[mw:Special:MyLanguage/Manual:$wgAntivirus|$wgAntivirus]].", "virus-scanfailed": "Used as error message. \"scan\" stands for \"virus scan\". Parameters:\n* $1 - exit code of virus scanner", "virus-unknownscanner": "Used as error message. This message is followed by the virus scanner name.", - "logouttext": "Log out message. Parameters:\n* $1 - (Unused) an URL to [[Special:Userlogin]] containing <code>returnto</code> and <code>returntoquery</code> parameters", + "logouttext": "Log out message. If the user just logged out of a temporary account, the {{msg-mw|logouttext-for-temporary-account}} message is used instead. Parameters:\n* $1 - (Unused) an URL to [[Special:Userlogin]] containing <code>returnto</code> and <code>returntoquery</code> parameters", + "logouttext-for-temporary-account": "Log out message used when the user just logged out of a temporary account. Otherwise the {{msg-mw|logouttext}} message is used instead. Parameters:\n* $1 - (Unused) an URL to [[Special:Userlogin]] containing <code>returnto</code> and <code>returntoquery</code> parameters", "logging-out-notify": "The message when the user is being logged out.", "logout-failed": "Message when log out fails in notification popup. Parameters:\n* $1 - Error message", "cannotlogoutnow-title": "Error page title shown when logging out is not possible.", @@ -653,7 +654,7 @@ "logout": "Used as link text in your personal toolbox (upper right side).\n\nSee also:\n* {{msg-mw|Logout}}\n* {{msg-mw|Accesskey-pt-logout}}\n* {{msg-mw|Tooltip-pt-logout}}\n{{Identical|Log out}}", "userlogout": "{{doc-special|UserLogout|unlisted=1}}\n{{Identical|Log out}}", "userlogout-summary": "{{ignored}}", - "templogout": "Used as log out link text in your personal toolbox (upper right side) for temp users.", + "templogout": "Used as log out link text in your personal toolbox (upper right side) for temp users and also as the page title for [[Special:UserLogout]] when the user is (or was just) logged in to a temporary account.", "notloggedin": "This message is displayed in the standard skin when not logged in. The message is placed above the login link in the top right corner of pages.\n\n{{Identical|Not logged in}}", "userlogin-noaccount": "In the [[Special:Userlogin]] form, this is the text prior to button inviting user to join project.\n{{Identical|Do not have an account}}", "userlogin-joinproject": "Text of button inviting user to create an account.\n\nSee example: [[Special:UserLogin]]", diff --git a/resources/src/mediawiki.page.ready/ready.js b/resources/src/mediawiki.page.ready/ready.js index 9a778917a5f5..94617994c41e 100644 --- a/resources/src/mediawiki.page.ready/ready.js +++ b/resources/src/mediawiki.page.ready/ready.js @@ -192,6 +192,13 @@ $( () => { { tag: 'logout', autoHide: false } ); var api = new mw.Api(); + if ( mw.user.isTemp() ) { + // Indicate to the success page that the user was previously a temporary account, so that the success + // message can be customised appropriately. + const url = new URL( href ); + url.searchParams.append( 'wasTempUser', 1 ); + href = url; + } api.postWithToken( 'csrf', { action: 'logout' } ).then( diff --git a/tests/phpunit/includes/specials/SpecialUserLogoutTest.php b/tests/phpunit/includes/specials/SpecialUserLogoutTest.php index f019650e3ca8..690f4ea7798a 100644 --- a/tests/phpunit/includes/specials/SpecialUserLogoutTest.php +++ b/tests/phpunit/includes/specials/SpecialUserLogoutTest.php @@ -4,6 +4,7 @@ use MediaWiki\Context\RequestContext; use MediaWiki\Request\FauxRequest; use MediaWiki\SpecialPage\SpecialPage; use MediaWiki\Specials\SpecialUserLogout; +use MediaWiki\Tests\User\TempUser\TempUserTestTrait; /** * @covers \MediaWiki\Specials\SpecialUserLogout @@ -11,13 +12,15 @@ use MediaWiki\Specials\SpecialUserLogout; */ class SpecialUserLogoutTest extends SpecialPageTestBase { + use TempUserTestTrait; + /** * Returns a new instance of the special page under test. * * @return SpecialPage */ protected function newSpecialPage() { - return new SpecialUserLogout(); + return new SpecialUserLogout( $this->getServiceContainer()->getTempUserConfig() ); } public function testUserLogoutComplete() { @@ -38,11 +41,51 @@ class SpecialUserLogoutTest extends SpecialPageTestBase { $oldNameInHook = $oldName; } ); - $this->executeSpecialPage( '', $fauxRequest, 'qqx', $user->getUser() ); + + [ $html ] = $this->executeSpecialPage( '', $fauxRequest, 'qqx', $user->getUser(), true ); + // Check that the page title and page content are as expected for a normal user logout + $this->assertStringContainsString( '(logouttext:', $html ); + $this->assertStringContainsString( '(userlogout)', $html ); + $this->assertEquals( $oldName, $oldNameInHook, 'old name in UserLogoutComplete hook was incorrect' ); } + + public function testExecuteForTemporaryAccount() { + $this->enableAutoCreateTempUser(); + $user = $this->getServiceContainer()->getTempUserCreator()->create( null, new FauxRequest() )->getUser(); + + $session = RequestContext::getMain()->getRequest()->getSession(); + $session->setUser( $user ); + $fauxRequest = new FauxRequest( [ 'wpEditToken' => $session->getToken( 'logoutToken' ) ], true, $session ); + + [ $html ] = $this->executeSpecialPage( '', $fauxRequest, 'qqx', $user, true ); + // Check that the page title and page content are as expected for the temporary account logout + $this->assertStringContainsString( '(logouttext-for-temporary-account:', $html ); + $this->assertStringContainsString( '(templogout)', $html ); + } + + public function testViewForTemporaryAccountAfterApiLogout() { + $user = $this->getServiceContainer()->getUserFactory()->newAnonymous( '1.2.3.4' ); + + $fauxRequest = new FauxRequest( [ 'wasTempUser' => 1 ] ); + + [ $html ] = $this->executeSpecialPage( '', $fauxRequest, 'qqx', $user, true ); + // Check that the page title and page content are as expected for the temporary account logout + $this->assertStringContainsString( '(logouttext-for-temporary-account:', $html ); + $this->assertStringContainsString( '(templogout)', $html ); + } + + public function testViewForTemporaryAccount() { + $this->enableAutoCreateTempUser(); + $user = $this->getServiceContainer()->getTempUserCreator()->create( null, new FauxRequest() )->getUser(); + + [ $html ] = $this->executeSpecialPage( '', null, 'qqx', $user, true ); + // Check that the page title is as expected for a temporary account and that the submit button is present + $this->assertStringContainsString( '(templogout)', $html ); + $this->assertStringContainsString( '(htmlform-submit)', $html ); + } } |