diff --git a/.github/workflows/acceptance-tests.yml b/.github/workflows/acceptance-tests.yml index b8158c63..b68919ba 100644 --- a/.github/workflows/acceptance-tests.yml +++ b/.github/workflows/acceptance-tests.yml @@ -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' diff --git a/tests/_support/AcceptanceTester.php b/tests/_support/AcceptanceTester.php index 68097631..304ab4a8 100644 --- a/tests/_support/AcceptanceTester.php +++ b/tests/_support/AcceptanceTester.php @@ -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', @@ -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 ) ); } diff --git a/tests/acceptance/SwitchFromEnglishCest.php b/tests/acceptance/SwitchFromEnglishCest.php index 48501cf0..778905fd 100644 --- a/tests/acceptance/SwitchFromEnglishCest.php +++ b/tests/acceptance/SwitchFromEnglishCest.php @@ -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 */ @@ -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' ); diff --git a/tests/acceptance/SwitchToEnglishCest.php b/tests/acceptance/SwitchToEnglishCest.php index 3db08e0a..c9c43e66 100644 --- a/tests/acceptance/SwitchToEnglishCest.php +++ b/tests/acceptance/SwitchToEnglishCest.php @@ -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 */ @@ -22,7 +24,17 @@ 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' ); @@ -30,7 +42,7 @@ public function SwitchFromItalianAdminToEnglishAuthorAndBack( \AcceptanceTester $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.' ); } diff --git a/user-switching.php b/user-switching.php index 63d0ad8a..960c2488 100644 --- a/user-switching.php +++ b/user-switching.php @@ -1039,6 +1039,8 @@ 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' ), @@ -1046,6 +1048,10 @@ public static function switch_back_message( WP_User $user ): string { $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)',