Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scan the test classes with PHPStan #112

Merged
merged 6 commits into from
Oct 10, 2023
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
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
12 changes: 7 additions & 5 deletions tests/acceptance/SwitchFromEnglishCest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php
<?php declare(strict_types = 1);

namespace UserSwitching\Tests;

/**
* Acceptance tests for switching from a user who uses English to a user who doesn't
*/

class SwitchFromEnglishCest {
public function _before( AcceptanceTester $I ) {
final class SwitchFromEnglishCest {
public function _before( \AcceptanceTester $I ): void {
$I->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' );
Expand All @@ -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' );
Expand Down
26 changes: 14 additions & 12 deletions tests/acceptance/SwitchOffCest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php
<?php declare(strict_types = 1);

namespace UserSwitching\Tests;

/**
* Acceptance tests for switching off.
*/

class SwitchOffCest {
public function _before( AcceptanceTester $I ) {
final class SwitchOffCest {
public function _before( \AcceptanceTester $I ): void {
$I->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();
Expand All @@ -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();
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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] );
Expand All @@ -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
Expand All @@ -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',
Expand All @@ -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',
Expand Down
12 changes: 7 additions & 5 deletions tests/acceptance/SwitchToEnglishCest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php
<?php declare(strict_types = 1);

namespace UserSwitching\Tests;

/**
* Acceptance tests for switching from a user who doesn't use English to a user who does
*/

class SwitchToEnglishCest {
public function _before( AcceptanceTester $I ) {
final class SwitchToEnglishCest {
public function _before( \AcceptanceTester $I ): void {
$I->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' );
Expand All @@ -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' );
Expand Down
14 changes: 8 additions & 6 deletions tests/acceptance/SwitchUserCest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php
<?php declare(strict_types = 1);

namespace UserSwitching\Tests;

/**
* Acceptance tests for switching users.
*/

class SwitchUserCest {
public function _before( AcceptanceTester $I ) {
final class SwitchUserCest {
public function _before( \AcceptanceTester $I ): void {
$I->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' );

Expand All @@ -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' );

Expand Down
42 changes: 21 additions & 21 deletions tests/integration/Test.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
<?php

declare(strict_types = 1);
<?php declare(strict_types = 1);

namespace UserSwitching\Tests;

abstract class Test extends \Codeception\TestCase\WPTestCase {

/**
* @var array<string, \WP_User>
*/
protected static $users = array();
protected static $users = [];

/**
* @var array<string, \WP_User>
*/
protected static $testers = array();
protected static $testers = [];

/**
* @var array<int, string>
*/
protected $sessions = array();
protected $sessions = [];

/**
* @return void
Expand Down Expand Up @@ -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 ] );
}

}
23 changes: 8 additions & 15 deletions tests/integration/authenticationTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types = 1);
<?php declare(strict_types = 1);

namespace UserSwitching\Tests;

Expand All @@ -9,9 +7,8 @@
/**
* @covers \user_switching::authenticate_old_user
*/
class AuthenticationTest extends Test {

public function testValidCookiePassesAuthentication() {
final class AuthenticationTest extends Test {
public function testValidCookiePassesAuthentication(): void {
$expiry = time() + 172800;

$auth_cookie = wp_generate_auth_cookie( self::$testers['editor']->ID, $expiry, 'auth' );
Expand All @@ -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' );
Expand All @@ -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' );
Expand All @@ -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'] ) );
}

}
Loading