diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 853a419..a0ee43d 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -6,8 +6,11 @@ parameters: level: 9 paths: - user-switching.php + - tests/acceptance + - tests/integration scanDirectories: - tests/_support/ + - vendor/lucatume/wp-browser/src/includes/factory/ excludePaths: analyse: - tests/integration/Supports/ diff --git a/tests/acceptance/SwitchFromEnglishCest.php b/tests/acceptance/SwitchFromEnglishCest.php index 7edbd76..48501cf 100644 --- a/tests/acceptance/SwitchFromEnglishCest.php +++ b/tests/acceptance/SwitchFromEnglishCest.php @@ -1,10 +1,12 @@ -comment( 'As an administrator of a site which uses more than one language' ); $I->comment( 'I need to be able to switch to user accounts that use a different language' ); $I->comment( 'And see the output of User Switching in my original language' ); @@ -17,7 +19,7 @@ public function _before( AcceptanceTester $I ) { ] ); } - public function SwitchFromEnglishAdminToItalianAuthorAndBack( AcceptanceTester $I ) { + public function SwitchFromEnglishAdminToItalianAuthorAndBack( \AcceptanceTester $I ): void { $I->loginAsAdmin(); $I->switchToUser( 'autore' ); $I->canSeeThePageInLanguage( 'it-IT' ); diff --git a/tests/acceptance/SwitchOffCest.php b/tests/acceptance/SwitchOffCest.php index 4228fc8..374f4bb 100644 --- a/tests/acceptance/SwitchOffCest.php +++ b/tests/acceptance/SwitchOffCest.php @@ -1,16 +1,18 @@ -comment( 'As an administrator' ); $I->comment( 'I need to be able to switch off' ); $I->comment( 'In order to view the site without logging out completely' ); } - public function SwitchOffFromDashboardAndBackFromFrontEnd( AcceptanceTester $I ) { + public function SwitchOffFromDashboardAndBackFromFrontEnd( \AcceptanceTester $I ): void { $I->loginAsAdmin(); $I->amOnAdminPage( '/' ); $I->switchOff(); @@ -22,7 +24,7 @@ public function SwitchOffFromDashboardAndBackFromFrontEnd( AcceptanceTester $I ) $I->amLoggedInAs( 'admin' ); } - public function SwitchOffFromDashboardAndBackFromLoginScreen( AcceptanceTester $I ) { + public function SwitchOffFromDashboardAndBackFromLoginScreen( \AcceptanceTester $I ): void { $I->loginAsAdmin(); $I->amOnAdminPage( '/' ); $I->switchOff(); @@ -36,7 +38,7 @@ public function SwitchOffFromDashboardAndBackFromLoginScreen( AcceptanceTester $ $I->amLoggedInAs( 'admin' ); } - public function SwitchOffFromPublishedPostEditingScreen( AcceptanceTester $I ) { + public function SwitchOffFromPublishedPostEditingScreen( \AcceptanceTester $I ): void { $I->loginAsAdmin(); $id = $I->havePostInDatabase( [ 'post_status' => 'publish', @@ -49,7 +51,7 @@ public function SwitchOffFromPublishedPostEditingScreen( AcceptanceTester $I ) { $I->amLoggedOut(); } - public function SwitchOffFromDraftPostEditingScreen( AcceptanceTester $I ) { + public function SwitchOffFromDraftPostEditingScreen( \AcceptanceTester $I ): void { $I->loginAsAdmin(); $id = $I->havePostInDatabase( [ 'post_status' => 'draft', @@ -62,7 +64,7 @@ public function SwitchOffFromDraftPostEditingScreen( AcceptanceTester $I ) { $I->amLoggedOut(); } - public function SwitchOffFromTermEditingScreen( AcceptanceTester $I ) { + public function SwitchOffFromTermEditingScreen( \AcceptanceTester $I ): void { $I->loginAsAdmin(); $term = $I->haveTermInDatabase( 'hello', 'category' ); $I->amOnAdminPage( '/term.php?taxonomy=category&tag_ID=' . $term[0] ); @@ -79,7 +81,7 @@ public function SwitchOffFromTermEditingScreen( AcceptanceTester $I ) { $I->amLoggedOut(); } - public function SwitchOffFromUserEditingScreen( AcceptanceTester $I ) { + public function SwitchOffFromUserEditingScreen( \AcceptanceTester $I ): void { $I->loginAsAdmin(); $id = $I->haveUserInDatabase( 'example', 'editor' ); // https://github.com/lucatume/wp-browser/pull/586 @@ -90,7 +92,7 @@ public function SwitchOffFromUserEditingScreen( AcceptanceTester $I ) { $I->amLoggedOut(); } - public function SwitchOffFromApprovedCommentEditingScreen( AcceptanceTester $I ) { + public function SwitchOffFromApprovedCommentEditingScreen( \AcceptanceTester $I ): void { $I->loginAsAdmin(); $postId = $I->havePostInDatabase( [ 'post_status' => 'publish', @@ -105,7 +107,7 @@ public function SwitchOffFromApprovedCommentEditingScreen( AcceptanceTester $I ) $I->amLoggedOut(); } - public function SwitchOffFromUnapprovedCommentEditingScreen( AcceptanceTester $I ) { + public function SwitchOffFromUnapprovedCommentEditingScreen( \AcceptanceTester $I ): void { $I->loginAsAdmin(); $postId = $I->havePostInDatabase( [ 'post_status' => 'publish', diff --git a/tests/acceptance/SwitchToEnglishCest.php b/tests/acceptance/SwitchToEnglishCest.php index 947bbc2..3db08e0 100644 --- a/tests/acceptance/SwitchToEnglishCest.php +++ b/tests/acceptance/SwitchToEnglishCest.php @@ -1,10 +1,12 @@ -comment( 'As an administrator of a site which uses more than one language' ); $I->comment( 'I need to be able to switch between users' ); $I->comment( 'And see the output of User Switching in my original language' ); @@ -20,7 +22,7 @@ public function _before( AcceptanceTester $I ) { ] ); } - public function SwitchFromItalianAdminToEnglishAuthorAndBack( AcceptanceTester $I ) { + public function SwitchFromItalianAdminToEnglishAuthorAndBack( \AcceptanceTester $I ): void { $I->loginAs( 'admin_it', 'admin_it' ); $I->switchToUser( 'author_en' ); $I->canSeeThePageInLanguage( 'en-US' ); diff --git a/tests/acceptance/SwitchUserCest.php b/tests/acceptance/SwitchUserCest.php index 8cd77f7..ecd39ce 100644 --- a/tests/acceptance/SwitchUserCest.php +++ b/tests/acceptance/SwitchUserCest.php @@ -1,16 +1,18 @@ -comment( 'As an administrator' ); $I->comment( 'I need to be able to switch between users' ); $I->comment( 'In order to access different user accounts' ); } - public function SwitchToEditorThenBackFromFrontEnd( AcceptanceTester $I ) { + public function SwitchToEditorThenBackFromFrontEnd( \AcceptanceTester $I ): void { $I->loginAsAdmin(); $I->haveUserInDatabase( 'editor', 'editor' ); @@ -25,7 +27,7 @@ public function SwitchToEditorThenBackFromFrontEnd( AcceptanceTester $I ) { $I->amLoggedInAs( 'admin' ); } - public function SwitchToEditorThenBackFromAdminArea( AcceptanceTester $I ) { + public function SwitchToEditorThenBackFromAdminArea( \AcceptanceTester $I ): void { $I->loginAsAdmin(); $I->haveUserInDatabase( 'editor', 'editor' ); diff --git a/tests/integration/Test.php b/tests/integration/Test.php index 9bd861e..60c0fa8 100644 --- a/tests/integration/Test.php +++ b/tests/integration/Test.php @@ -1,25 +1,22 @@ - */ - protected static $users = array(); + protected static $users = []; /** * @var array */ - protected static $testers = array(); + protected static $testers = []; /** * @var array */ - protected $sessions = array(); + protected $sessions = []; /** * @return void @@ -57,48 +54,51 @@ public static function wpSetUpBeforeClass( \WP_UnitTest_Factory $factory ) { add_filter( 'user_switching_send_auth_cookies', '__return_false' ); } - /** - * @return void - */ - public function _before() { + public function _before(): void { add_action( 'set_auth_cookie', array( $this, 'action_set_auth_cookie' ), 10, 6 ); - add_action( 'set_logged_in_cookie', array( $this, 'action_set_logged_in_cookie' ), 10, 6 ); + add_action( 'set_logged_in_cookie', array( $this, 'action_set_logged_in_cookie' ), 10 ); add_action( 'clear_auth_cookie', array( $this, 'action_clear_auth_cookie' ) ); - add_action( 'set_user_switching_cookie', array( $this, 'action_set_user_switching_cookie' ), 10, 5 ); - add_action( 'set_olduser_cookie', array( $this, 'action_set_olduser_cookie' ), 10, 5 ); + add_action( 'set_user_switching_cookie', array( $this, 'action_set_user_switching_cookie' ), 10 ); + add_action( 'set_olduser_cookie', array( $this, 'action_set_olduser_cookie' ), 10 ); add_action( 'clear_olduser_cookie', array( $this, 'action_clear_olduser_cookie' ) ); } - public function action_set_auth_cookie( $cookie, $expire, $expiration, $user_id, $scheme, $token ) { + final public function action_set_auth_cookie( + string $cookie, + int $expire, + int $expiration, + int $user_id, + string $scheme, + string $token + ): void { $_COOKIE[ SECURE_AUTH_COOKIE ] = $cookie; $_COOKIE[ AUTH_COOKIE ] = $cookie; $this->sessions[ $user_id ] = $token; } - public function action_set_logged_in_cookie( $cookie, $expire, $expiration, $user_id, $scheme, $token ) { + final public function action_set_logged_in_cookie( string $cookie ): void { $_COOKIE[ LOGGED_IN_COOKIE ] = $cookie; } - public function action_clear_auth_cookie() { + final public function action_clear_auth_cookie(): void { unset( $_COOKIE[ LOGGED_IN_COOKIE ] ); unset( $_COOKIE[ SECURE_AUTH_COOKIE ] ); unset( $_COOKIE[ AUTH_COOKIE ] ); } - public function action_set_user_switching_cookie( $cookie, $expiration, $user_id, $scheme, $token ) { + final public function action_set_user_switching_cookie( string $cookie ): void { $_COOKIE[ USER_SWITCHING_COOKIE ] = $cookie; $_COOKIE[ USER_SWITCHING_SECURE_COOKIE ] = $cookie; } - public function action_set_olduser_cookie( $cookie, $expiration, $user_id, $scheme, $token ) { + final public function action_set_olduser_cookie( string $cookie ): void { $_COOKIE[ USER_SWITCHING_OLDUSER_COOKIE ] = $cookie; } - public function action_clear_olduser_cookie() { + final public function action_clear_olduser_cookie(): void { unset( $_COOKIE[ USER_SWITCHING_COOKIE ] ); unset( $_COOKIE[ USER_SWITCHING_SECURE_COOKIE ] ); unset( $_COOKIE[ USER_SWITCHING_OLDUSER_COOKIE ] ); } - } diff --git a/tests/integration/authenticationTest.php b/tests/integration/authenticationTest.php index 0c40416..1e3747f 100644 --- a/tests/integration/authenticationTest.php +++ b/tests/integration/authenticationTest.php @@ -1,6 +1,4 @@ -ID, $expiry, 'auth' ); @@ -20,14 +17,14 @@ public function testValidCookiePassesAuthentication() { self::assertFalse( user_switching::authenticate_old_user( self::$testers['admin'] ) ); } - public function testExpiredCookieDoesNotPassAuthentication() { + public function testExpiredCookieDoesNotPassAuthentication(): void { $auth_cookie = wp_generate_auth_cookie( self::$testers['editor']->ID, time() - 1000, 'auth' ); $_COOKIE[ USER_SWITCHING_COOKIE ] = json_encode( array( $auth_cookie ) ); self::assertFalse( user_switching::authenticate_old_user( self::$testers['editor'] ) ); self::assertFalse( user_switching::authenticate_old_user( self::$testers['admin'] ) ); } - public function testValidCookieWithIncorrectSchemeDoesNotPassAuthentication() { + public function testValidCookieWithIncorrectSchemeDoesNotPassAuthentication(): void { $expiry = time() + 172800; $logged_in_cookie = wp_generate_auth_cookie( self::$testers['editor']->ID, $expiry, 'logged_in' ); @@ -40,16 +37,13 @@ public function testValidCookieWithIncorrectSchemeDoesNotPassAuthentication() { self::assertFalse( user_switching::authenticate_old_user( self::$testers['admin'] ) ); } - public function testMalformedCookieDoesNotPassAuthentication() { + public function testMalformedCookieDoesNotPassAuthentication(): void { $_COOKIE[ USER_SWITCHING_COOKIE ] = 'hello'; self::assertFalse( user_switching::authenticate_old_user( self::$testers['editor'] ) ); self::assertFalse( user_switching::authenticate_old_user( self::$testers['admin'] ) ); } - /** - * @testdox A non-JSON encoded cookie does not pass authentication - */ - public function testANonJsonEncodedCookieDoesNotPassAuthentication() { + public function testANonJsonEncodedCookieDoesNotPassAuthentication(): void { $expiry = time() + 172800; $auth_cookie = wp_generate_auth_cookie( self::$testers['editor']->ID, $expiry, 'auth' ); @@ -58,10 +52,9 @@ public function testANonJsonEncodedCookieDoesNotPassAuthentication() { self::assertFalse( user_switching::authenticate_old_user( self::$testers['admin'] ) ); } - public function testNoCookieDoesNotPassAuthentication() { + public function testNoCookieDoesNotPassAuthentication(): void { unset( $_COOKIE[ USER_SWITCHING_COOKIE ] ); self::assertFalse( user_switching::authenticate_old_user( self::$testers['editor'] ) ); self::assertFalse( user_switching::authenticate_old_user( self::$testers['admin'] ) ); } - } diff --git a/tests/integration/capabilitiesTest.php b/tests/integration/capabilitiesTest.php index e49f282..edd0316 100644 --- a/tests/integration/capabilitiesTest.php +++ b/tests/integration/capabilitiesTest.php @@ -1,6 +1,4 @@ -> */ - public function data_roles() { + public function data_roles(): array { $roles = [ 'admin' => [ 'admin', @@ -51,7 +48,7 @@ public function data_roles() { return $roles; } - public function testAllRolesAreTested() { + public function testAllRolesAreTested(): void { $tested_roles = array_column( $this->data_roles(), 0 ); self::assertSame( array_keys( self::$testers ), $tested_roles ); @@ -60,9 +57,8 @@ public function testAllRolesAreTested() { /** * @dataProvider data_roles - * @testdox User with role of $role can or cannot switch according to role */ - public function testUserCanOrCannotSwitchAccordingToRole( $role, $can_switch ) { + public function testUserCanOrCannotSwitchAccordingToRole( string $role, bool $can_switch ): void { foreach ( self::$users as $user_role => $user ) { if ( self::$testers[ $role ]->ID === $user->ID ) { # No user can switch to themselves: @@ -80,7 +76,7 @@ public function testUserCanOrCannotSwitchAccordingToRole( $role, $can_switch ) { self::assertSame( $can_switch, user_can( self::$testers[ $role ]->ID, 'switch_off' ) ); } - public function testAbilityToSwitchUsersCanBeGrantedToUser() { + public function testAbilityToSwitchUsersCanBeGrantedToUser(): void { # Editors cannot switch to other users: $can_already_switch = user_can( self::$testers['editor']->ID, 'switch_to_user', self::$users['admin']->ID ); @@ -100,7 +96,7 @@ public function testAbilityToSwitchUsersCanBeGrantedToUser() { self::assertTrue( $can_switch_off ); } - public function testAbilityToSwitchUsersCanBeGrantedToRole() { + public function testAbilityToSwitchUsersCanBeGrantedToRole(): void { # Editors cannot switch to other users: $can_already_switch = user_can( self::$testers['editor']->ID, 'switch_to_user', self::$users['admin']->ID ); @@ -126,7 +122,7 @@ public function testAbilityToSwitchUsersCanBeGrantedToRole() { /** * @group ms-excluded */ - public function testAbilityToSwitchUsersCanBeDeniedFromUser() { + public function testAbilityToSwitchUsersCanBeDeniedFromUser(): void { # Admins can switch to other users: $can_already_switch = user_can( self::$testers['admin']->ID, 'switch_to_user', self::$users['author']->ID ); @@ -149,7 +145,7 @@ public function testAbilityToSwitchUsersCanBeDeniedFromUser() { /** * @group ms-excluded */ - public function testAbilityToSwitchUsersCanBeDeniedFromRole() { + public function testAbilityToSwitchUsersCanBeDeniedFromRole(): void { # Admins can switch to other users: $can_already_switch = user_can( self::$testers['admin']->ID, 'switch_to_user', self::$users['author']->ID ); @@ -176,7 +172,7 @@ public function testAbilityToSwitchUsersCanBeDeniedFromRole() { * @group multisite * @group ms-required */ - public function testAbilityToSwitchUsersCanBeGrantedToAdministratorRoleOnMultisite() { + public function testAbilityToSwitchUsersCanBeGrantedToAdministratorRoleOnMultisite(): void { # Admins on Multisite cannot switch to other users: $can_already_switch = user_can( self::$testers['admin']->ID, 'switch_to_user', self::$users['author']->ID ); @@ -203,7 +199,7 @@ public function testAbilityToSwitchUsersCanBeGrantedToAdministratorRoleOnMultisi * @group multisite * @group ms-required */ - public function testAbilityToSwitchUsersCanBeGrantedToAdministratorUserOnMultisite() { + public function testAbilityToSwitchUsersCanBeGrantedToAdministratorUserOnMultisite(): void { # Admins on Multisite cannot switch to other users: $can_already_switch = user_can( self::$testers['admin']->ID, 'switch_to_user', self::$users['author']->ID ); @@ -225,10 +221,8 @@ public function testAbilityToSwitchUsersCanBeGrantedToAdministratorUserOnMultisi /** * @dataProvider data_roles - * @testdox User with role of $role cannot switch to no user */ - public function testSwitchingToNoUserIsNotAllowed( $role ) { + public function testSwitchingToNoUserIsNotAllowed( string $role ): void { self::assertFalse( user_can( self::$testers[ $role ]->ID, 'switch_to_user', 0 ) ); } - } diff --git a/tests/integration/pluginTest.php b/tests/integration/pluginTest.php index fb598e4..55a03df 100644 --- a/tests/integration/pluginTest.php +++ b/tests/integration/pluginTest.php @@ -1,16 +1,14 @@ - */ private $readme_data; - public function testStableTagMatchesVersion() { + public function testStableTagMatchesVersion(): void { $readme_data = $this->get_readme(); if ( null === $readme_data ) { self::fail( 'There is no readme file' ); @@ -24,7 +22,7 @@ public function testStableTagMatchesVersion() { /** * @return ?array */ - private function get_readme() { + private function get_readme(): ?array { if ( ! isset( $this->readme_data ) ) { $file = dirname( dirname( __DIR__ ) ) . '/readme.md'; @@ -49,5 +47,4 @@ private function get_readme() { return $this->readme_data; } - } diff --git a/tests/integration/sessionsTest.php b/tests/integration/sessionsTest.php index e04babd..8f21555 100644 --- a/tests/integration/sessionsTest.php +++ b/tests/integration/sessionsTest.php @@ -1,18 +1,15 @@ -get_all() ); self::assertNotNull( $admin_manager->get( $admin_token ) ); } - } diff --git a/tests/integration/switchingTest.php b/tests/integration/switchingTest.php index 2beb005..4c7e016 100644 --- a/tests/integration/switchingTest.php +++ b/tests/integration/switchingTest.php @@ -1,34 +1,31 @@ -go_to( $url ); self::assertSame( user_switching::current_url(), $url ); @@ -221,23 +217,20 @@ public function testCurrentUrl() { * @param int $user_id * @param int|false $old_user_id */ - public function _action_switch_user( $user_id, $old_user_id ) { + public function _action_switch_user( $user_id, $old_user_id ): void { $this->test_switching_user_id = $user_id; $this->test_switching_old_user_id = $old_user_id; } - public function _action_switch_off( $old_user_id ) { + public function _action_switch_off( int $old_user_id ): void { $this->test_switching_user_id = false; $this->test_switching_old_user_id = $old_user_id; } - /** - * @return int - */ - public function _filter_auth_cookie_expiration( $length, $user_id, $remember ) { + public function _filter_auth_cookie_expiration( int $length, int $user_id, bool $remember ): int { $this->test_switching_auth_cookie_user_id = $user_id; $this->test_switching_auth_cookie_remember = $remember; + return $length; } - } diff --git a/tests/phpstan/stubs.php b/tests/phpstan/stubs.php index 3a58cd1..02c4188 100644 --- a/tests/phpstan/stubs.php +++ b/tests/phpstan/stubs.php @@ -41,7 +41,6 @@ function bp_get_button( array $args ) {} function buddypress() {} class BuddyPress { - /** * @var array */ @@ -59,7 +58,6 @@ function bbp_get_user_id() {} function bbp_get_user_profile_url( int $user_id ) {} class WooCommerce { - /** * @var \WC_Session */