Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/acceptance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ jobs:
# Latest WordPress on PHP 8.4
- wp: '6.8'
php: '8.4'
# Testing
- wp: '6.5'
php: '7.4'
- wp: '6.4'
php: '7.4'
- wp: '6.3'
php: '7.4'
- wp: '6.2'
php: '7.4'
- wp: '6.1'
php: '7.4'
# Oldest supported WordPress
- wp: '6.0'
php: '7.4'
Expand Down
16 changes: 12 additions & 4 deletions tests/_support/AcceptanceTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ public function switchOff() {

/**
* Switch back to the original user
*
* @param string $user_login
*/
public function switchBackTo( $user_login ) {
public function switchBackTo( string $user_login, string $lang = 'en-US' ) {
$display_name = $this->grabFromDatabase(
$this->grabUsersTableName(),
'display_name',
Expand All @@ -104,8 +102,18 @@ public function switchBackTo( $user_login ) {
// Nothing.
}

switch ( $lang ) {
case 'it-IT':
$text = 'Torna a %s';
break;
case 'en-US':
default:
$text = 'Switch back to %s';
break;
}

$this->click( sprintf(
'Switch back to %s',
$text,
$display_name
) );
}
Expand Down
14 changes: 13 additions & 1 deletion tests/acceptance/SwitchFromEnglishCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace UserSwitching\Tests;

use Codeception\Scenario;

/**
* Acceptance tests for switching from a user who uses English to a user who doesn't
*/
Expand All @@ -19,7 +21,17 @@ public function _before( \AcceptanceTester $I ): void {
] );
}

public function SwitchFromEnglishAdminToItalianAuthorAndBack( \AcceptanceTester $I ): void {
public function SwitchFromEnglishAdminToItalianAuthorAndBack( \AcceptanceTester $I, Scenario $scenario ): void {
require dirname( __DIR__, 2 ) . '/vendor/wordpress/wordpress/wp-includes/version.php';

/** @var string $wp_version */

$I->comment( sprintf( 'Running test on WordPress version %s', $wp_version ) );

if ( version_compare( $wp_version, '6.2', '<' ) ) {
$scenario->skip( 'This test requires WordPress 6.2 or later.' );
}

$I->loginAsAdmin();
$I->switchToUser( 'autore' );
$I->canSeeThePageInLanguage( 'it-IT' );
Expand Down
16 changes: 14 additions & 2 deletions tests/acceptance/SwitchToEnglishCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace UserSwitching\Tests;

use Codeception\Scenario;

/**
* Acceptance tests for switching from a user who doesn't use English to a user who does
*/
Expand All @@ -22,15 +24,25 @@ public function _before( \AcceptanceTester $I ): void {
] );
}

public function SwitchFromItalianAdminToEnglishAuthorAndBack( \AcceptanceTester $I ): void {
public function SwitchFromItalianAdminToEnglishAuthorAndBack( \AcceptanceTester $I, Scenario $scenario ): void {
require dirname( __DIR__, 2 ) . '/vendor/wordpress/wordpress/wp-includes/version.php';

/** @var string $wp_version */

$I->comment( sprintf( 'Running test on WordPress version %s', $wp_version ) );

if ( version_compare( $wp_version, '6.2', '<' ) ) {
$scenario->skip( 'This test requires WordPress 6.2 or later.' );
}

$I->loginAs( 'admin_it', 'admin_it' );
$I->switchToUser( 'author_en' );
$I->canSeeThePageInLanguage( 'en-US' );
$I->seeAdminSuccessNotice( 'Cambiato a Author EN.' );
$I->canSeeTheElementInLanguage( '#user_switching p', 'it-IT' );

$I->amOnAdminPage( '/' );
$I->switchBackTo( 'admin_it' );
$I->switchBackTo( 'admin_it', 'it-IT' );
$I->canSeeThePageInLanguage( 'it-IT' );
$I->seeAdminSuccessNotice( 'Tornato a Admin IT.' );
}
Expand Down
6 changes: 6 additions & 0 deletions user-switching.php
Original file line number Diff line number Diff line change
Expand Up @@ -1039,13 +1039,19 @@ public static function switched_to_message( WP_User $user ): string {
* @return string The message.
*/
public static function switch_back_message( WP_User $user ): string {
$switched_locale = switch_to_locale( get_user_locale( $user ) );

$message = sprintf(
/* Translators: 1: user display name; 2: username; */
__( 'Switch back to %1$s (%2$s)', 'user-switching' ),
$user->display_name,
$user->user_login
);

if ( $switched_locale ) {
restore_previous_locale();
}

// Removes the user login from this message without invalidating existing translations
return str_replace( sprintf(
' (%s)',
Expand Down
Loading