Skip to content

Commit

Permalink
Tests: Update to PHPUnit 9.5
Browse files Browse the repository at this point in the history
  • Loading branch information
iandunn committed Oct 12, 2022
1 parent 5506c1f commit 923fef3
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Setup PHP and Composer
uses: shivammathur/setup-php@v2
with:
php-version: '7.2'
php-version: '7.4'
tools: composer:v2

- name: Install NPM dependencies
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"config": {
"sort-packages": true,
"platform": {
"php": "7.2"
"php": "7.4"
},
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
Expand All @@ -33,7 +33,7 @@
"php-coveralls/php-coveralls": "^2.5",
"phpcompatibility/php-compatibility": "10.x-dev as 9.99.99",
"phpcompatibility/phpcompatibility-wp": "dev-master",
"phpunit/phpunit": "^5",
"phpunit/phpunit": "^9.5",
"roots/wordpress-core-installer": "^1.100",
"roots/wordpress-full": "^6.0",
"spatie/phpunit-watcher": "^1.23",
Expand Down
34 changes: 15 additions & 19 deletions tests/class-two-factor-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class Test_ClassTwoFactorCore extends WP_UnitTestCase {

/**
* Original User ID set in setup.
* Original User ID set in set_up().
*
* @var int
*/
Expand All @@ -23,23 +23,19 @@ class Test_ClassTwoFactorCore extends WP_UnitTestCase {
/**
* Set up error handling before test suite.
*
* @see WP_UnitTestCase::setUpBeforeClass()
* @see WP_UnitTestCase_Base::set_up_before_class()
*/
public static function setUpBeforeClass() {
parent::setUpBeforeClass();

public static function wpSetUpBeforeClass() {
set_error_handler( array( 'Test_ClassTwoFactorCore', 'error_handler' ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
}

/**
* Clean up error settings after test suite.
*
* @see WP_UnitTestCase::setUpBeforeClass()
* @see WP_UnitTestCase_Base::tear_down_after_class()
*/
public static function tearDownAfterClass() {
public static function wpTearDownAfterClass() {
restore_error_handler();

parent::tearDownAfterClass();
}

/**
Expand Down Expand Up @@ -68,7 +64,7 @@ public static function error_handler( $errno, $errstr ) {
* @return WP_User
*/
public function get_dummy_user( $meta_key = array( 'Two_Factor_Dummy' => 'Two_Factor_Dummy' ) ) {
$user = new WP_User( $this->factory->user->create() );
$user = new WP_User( self::factory()->user->create() );
$this->old_user_id = get_current_user_id();
wp_set_current_user( $user->ID );

Expand Down Expand Up @@ -163,7 +159,7 @@ public function test_get_enabled_providers_for_user_not_logged_in() {
* @covers Two_Factor_Core::get_enabled_providers_for_user
*/
public function test_get_enabled_providers_for_user_logged_in() {
$user = new WP_User( $this->factory->user->create() );
$user = new WP_User( self::factory()->user->create() );
$old_user_id = get_current_user_id();
wp_set_current_user( $user->ID );

Expand Down Expand Up @@ -221,7 +217,7 @@ public function test_get_available_providers_for_user_not_logged_in() {
* @covers Two_Factor_Core::get_available_providers_for_user
*/
public function test_get_available_providers_for_user_logged_in() {
$user = new WP_User( $this->factory->user->create() );
$user = new WP_User( self::factory()->user->create() );
$old_user_id = get_current_user_id();
wp_set_current_user( $user->ID );

Expand Down Expand Up @@ -254,9 +250,9 @@ public function test_is_user_using_two_factor_not_logged_in() {
* @covers Two_Factor_Core::login_url
*/
public function test_login_url() {
$this->assertContains( 'wp-login.php', Two_Factor_Core::login_url() );
$this->assertStringContainsString( 'wp-login.php', Two_Factor_Core::login_url() );

$this->assertContains(
$this->assertStringContainsString(
'paramencoded=%2F%3D1',
Two_Factor_Core::login_url(
array(
Expand All @@ -272,7 +268,7 @@ public function test_login_url() {
* @covers Two_Factor_Core::is_user_api_login_enabled
*/
public function test_user_api_login_is_disabled_by_default() {
$user_id = $this->factory->user->create();
$user_id = self::factory()->user->create();

$this->assertFalse( Two_Factor_Core::is_user_api_login_enabled( $user_id ) );
}
Expand All @@ -283,8 +279,8 @@ public function test_user_api_login_is_disabled_by_default() {
* @covers Two_Factor_Core::is_user_api_login_enabled
*/
public function test_user_api_login_can_be_enabled_via_filter() {
$user_id_default = $this->factory->user->create();
$user_id_enabled = $this->factory->user->create();
$user_id_default = self::factory()->user->create();
$user_id_enabled = self::factory()->user->create();

add_filter(
'two_factor_user_api_login_enable',
Expand Down Expand Up @@ -324,7 +320,7 @@ public function test_is_api_request() {
* @covers Two_Factor_Core::filter_authenticate
*/
public function test_filter_authenticate() {
$user_default = new WP_User( $this->factory->user->create() );
$user_default = new WP_User( self::factory()->user->create() );
$user_2fa_enabled = $this->get_dummy_user(); // User with a dummy two-factor method enabled.

// TODO: Get Two_Factor_Core away from static methods to allow mocking this.
Expand All @@ -348,7 +344,7 @@ public function test_filter_authenticate() {
* @covers Two_Factor_Core::collect_auth_cookie_tokens
*/
public function test_can_distroy_auth_sessions() {
$user_id = $this->factory->user->create(
$user_id = self::factory()->user->create(
array(
'user_login' => 'username',
'user_pass' => 'password',
Expand Down
36 changes: 18 additions & 18 deletions tests/providers/class-two-factor-backup-codes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class Tests_Two_Factor_Backup_Codes extends WP_UnitTestCase {
/**
* Set up a test case.
*
* @see WP_UnitTestCase::setup()
* @see WP_UnitTestCase_Base::set_up()
*/
public function setUp() {
parent::setUp();
public function set_up() {
parent::set_up();
$this->provider = Two_Factor_Backup_Codes::get_instance();
}

Expand All @@ -45,7 +45,7 @@ public function test_get_instance() {
* @covers Two_Factor_Backup_Codes::get_label
*/
public function test_get_label() {
$this->assertContains( 'Backup Verification Codes', $this->provider->get_label() );
$this->assertStringContainsString( 'Backup Verification Codes', $this->provider->get_label() );
}

/**
Expand All @@ -58,7 +58,7 @@ public function test_authentication_page() {
$this->provider->authentication_page( false );
$contents = ob_get_clean();

$this->assertContains( 'Enter a backup verification code.', $contents );
$this->assertStringContainsString( 'Enter a backup verification code.', $contents );
}

/**
Expand All @@ -67,7 +67,7 @@ public function test_authentication_page() {
* @covers Two_Factor_Backup_Codes::validate_authentication
*/
public function test_validate_authentication() {
$user = new WP_User( $this->factory->user->create() );
$user = new WP_User( self::factory()->user->create() );
$code = $this->provider->generate_codes( $user, array( 'number' => 1 ) );
$_POST['two-factor-backup-code'] = $code[0];

Expand All @@ -82,7 +82,7 @@ public function test_validate_authentication() {
* @covers Two_Factor_Backup_Codes::is_available_for_user
*/
public function test_is_available_for_user_false() {
$user = new WP_User( $this->factory->user->create() );
$user = new WP_User( self::factory()->user->create() );

$this->assertFalse( $this->provider->is_available_for_user( $user ) );
}
Expand All @@ -93,7 +93,7 @@ public function test_is_available_for_user_false() {
* @covers Two_Factor_Backup_Codes::is_available_for_user
*/
public function test_is_available_for_user() {
$user = new WP_User( $this->factory->user->create() );
$user = new WP_User( self::factory()->user->create() );
$codes = $this->provider->generate_codes( $user );

$this->assertTrue( $this->provider->is_available_for_user( $user ) );
Expand All @@ -107,7 +107,7 @@ public function test_is_available_for_user() {
* @covers Two_Factor_Backup_Codes::codes_remaining_for_user
*/
public function test_generate_codes_and_validate_codes() {
$user = new WP_User( $this->factory->user->create() );
$user = new WP_User( self::factory()->user->create() );
$codes = $this->provider->generate_codes( $user );
foreach ( $codes as $code ) {
$this->assertTrue( $this->provider->validate_code( $user, $code ) );
Expand All @@ -122,7 +122,7 @@ public function test_generate_codes_and_validate_codes() {
* @covers Two_Factor_Backup_Codes::validate_code
*/
public function test_generate_code_and_validate_code_false_revalidate() {
$user = new WP_User( $this->factory->user->create() );
$user = new WP_User( self::factory()->user->create() );
$codes = $this->provider->generate_codes( $user, array( 'number' => 1 ) );
$validate = $this->provider->validate_code( $user, $codes[0] );

Expand All @@ -136,10 +136,10 @@ public function test_generate_code_and_validate_code_false_revalidate() {
* @covers Two_Factor_Backup_Codes::validate_code
*/
public function test_generate_code_and_validate_code_false_different_users() {
$user = new WP_User( $this->factory->user->create() );
$user = new WP_User( self::factory()->user->create() );
$codes = $this->provider->generate_codes( $user, array( 'number' => 1 ) );

$user2 = new WP_User( $this->factory->user->create() );
$user2 = new WP_User( self::factory()->user->create() );
$codes2 = $this->provider->generate_codes( $user2, array( 'number' => 1 ) );

$this->assertFalse( $this->provider->validate_code( $user2, $codes[0] ) );
Expand All @@ -151,17 +151,17 @@ public function test_generate_code_and_validate_code_false_different_users() {
* @covers Two_Factor_Backup_Codes::user_options
*/
public function test_user_options() {
$user = new WP_User( $this->factory->user->create() );
$user = new WP_User( self::factory()->user->create() );
$nonce = wp_create_nonce( 'two-factor-backup-codes-generate-json-' . $user->ID );

ob_start();
$this->provider->user_options( $user );
$buffer = ob_get_clean();

$this->assertContains( '<p id="two-factor-backup-codes">', $buffer );
$this->assertContains( '<div class="two-factor-backup-codes-wrapper" style="display:none;">', $buffer );
$this->assertContains( "user_id: '{$user->ID}'", $buffer );
$this->assertContains( "nonce: '{$nonce}'", $buffer );
$this->assertStringContainsString( '<p id="two-factor-backup-codes">', $buffer );
$this->assertStringContainsString( '<div class="two-factor-backup-codes-wrapper" style="display:none;">', $buffer );
$this->assertStringContainsString( "user_id: '{$user->ID}'", $buffer );
$this->assertStringContainsString( "nonce: '{$nonce}'", $buffer );
}

/**
Expand All @@ -172,7 +172,7 @@ public function test_user_options() {
* @covers Two_Factor_Backup_Codes::codes_remaining_for_user
*/
public function test_delete_code() {
$user = new WP_User( $this->factory->user->create() );
$user = new WP_User( self::factory()->user->create() );

$this->provider->generate_codes( $user, array( 'number' => 1 ) );
$this->assertEquals( 1, $this->provider->codes_remaining_for_user( $user ) );
Expand Down
14 changes: 7 additions & 7 deletions tests/providers/class-two-factor-dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class Tests_Two_Factor_Dummy extends WP_UnitTestCase {
/**
* Set up a test case.
*
* @see WP_UnitTestCase::setup()
* @see WP_UnitTestCase_Base::set_up()
*/
public function setUp() {
parent::setUp();
public function set_up() {
parent::set_up();

$this->provider = Two_Factor_Dummy::get_instance();
}
Expand All @@ -49,7 +49,7 @@ public function test_get_instance() {
*/
public function test_get_label() {

$this->assertContains( 'Dummy Method', $this->provider->get_label() );
$this->assertStringContainsString( 'Dummy Method', $this->provider->get_label() );

}

Expand All @@ -64,9 +64,9 @@ public function test_authentication_page() {
$this->provider->authentication_page( false );
$contents = ob_get_clean();

$this->assertContains( 'Are you really you?', $contents );
$this->assertContains( '<p class="submit">', $contents );
$this->assertContains( 'Yup', $contents );
$this->assertStringContainsString( 'Are you really you?', $contents );
$this->assertStringContainsString( '<p class="submit">', $contents );
$this->assertStringContainsString( 'Yup', $contents );

}

Expand Down
Loading

0 comments on commit 923fef3

Please sign in to comment.