From 29d4e01f10381b6d85004d4d036d52795af536b4 Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Tue, 11 Oct 2022 13:46:32 -0700 Subject: [PATCH 1/5] Tests: Add phpunit-watcher for convenience --- composer.json | 5 ++++- package.json | 4 ++-- phpunit-watcher.yml.dist | 13 +++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 phpunit-watcher.yml.dist diff --git a/composer.json b/composer.json index bc93d2e8..cb0a6bca 100644 --- a/composer.json +++ b/composer.json @@ -11,8 +11,9 @@ }, "config": { "sort-packages": true, + "process-timeout": 0, "platform": { - "php": "5.6.20" + "php": "7.2" }, "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true, @@ -36,6 +37,7 @@ "phpunit/phpunit": "^5", "roots/wordpress-core-installer": "^1.100", "roots/wordpress-full": "^6.0", + "spatie/phpunit-watcher": "^1.23", "wp-coding-standards/wpcs": "^2.3", "wp-phpunit/wp-phpunit": "^6.0", "yoast/phpunit-polyfills": "^1.0" @@ -44,6 +46,7 @@ "lint": "phpcs", "lint-php8": "phpcs -p --standard=PHPCompatibilityWP --runtime-set testVersion 8.0- --extensions=php --ignore='vendor/,wordpress/,node_modules/' .", "test": "phpunit", + "test:watch": "phpunit-watcher watch", "format": "phpcbf" } } diff --git a/package.json b/package.json index e12720c6..31114a52 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,8 @@ "format:php": "composer format", "format:js": "npm run lint:js -- --fix", "format:css": "npm run lint:css -- --fix", - "test": "npm-run-all test:*", - "test:php": "npm run composer test", + "test": "npm run composer test", + "test:watch": "npm run composer test:watch", "composer": "wp-env run phpunit composer --working-dir=/var/www/html/wp-content/plugins/two-factor", "preinstall": "composer install" }, diff --git a/phpunit-watcher.yml.dist b/phpunit-watcher.yml.dist new file mode 100644 index 00000000..22ee1024 --- /dev/null +++ b/phpunit-watcher.yml.dist @@ -0,0 +1,13 @@ +watch: + directories: + - ./ + exclude: + - assets + fileMask: '*.php' + ignoreDotFiles: true + ignoreVCS: true + ignoreVCSIgnored: true + +notifications: + passingTests: false + failingTests: false From 48b90b674558519a3c5ca4e0742e7a366bd3154c Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Wed, 12 Oct 2022 11:01:55 +0300 Subject: [PATCH 2/5] Set the timeout only when needed Co-Authored-By: Ian Dunn --- composer.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index cb0a6bca..99390915 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,6 @@ }, "config": { "sort-packages": true, - "process-timeout": 0, "platform": { "php": "7.2" }, @@ -46,7 +45,10 @@ "lint": "phpcs", "lint-php8": "phpcs -p --standard=PHPCompatibilityWP --runtime-set testVersion 8.0- --extensions=php --ignore='vendor/,wordpress/,node_modules/' .", "test": "phpunit", - "test:watch": "phpunit-watcher watch", + "test:watch": [ + "Composer\\Config::disableProcessTimeout", + "phpunit-watcher watch" + ], "format": "phpcbf" } } From 0ad89db9d98c2304de2f5653804975924665f090 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Wed, 12 Oct 2022 11:08:42 +0300 Subject: [PATCH 3/5] Bump the version of runner to enable php8 compat checks --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5f0220ca..ea2267bd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,7 +20,7 @@ jobs: - name: Setup PHP and Composer uses: shivammathur/setup-php@v2 with: - php-version: '5.6' + php-version: '7.2' tools: composer:v2 - name: Install NPM dependencies From dab968b114f2e2205bd7d64dccb1716e5be9604a Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Wed, 12 Oct 2022 10:20:56 -0700 Subject: [PATCH 4/5] Tests: Update to PHPUnit 9.5 Fixture methods were updated per https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#shared-fixtures --- .github/workflows/test.yml | 2 +- composer.json | 4 +- tests/class-two-factor-core.php | 34 +++++++--------- .../class-two-factor-backup-codes.php | 36 ++++++++--------- tests/providers/class-two-factor-dummy.php | 14 +++---- tests/providers/class-two-factor-email.php | 40 ++++++++----------- tests/providers/class-two-factor-fido-u2f.php | 28 ++++++------- tests/providers/class-two-factor-totp.php | 26 ++++++------ 8 files changed, 87 insertions(+), 97 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ea2267bd..7fea4080 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/composer.json b/composer.json index 99390915..cb5117f7 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "config": { "sort-packages": true, "platform": { - "php": "7.2" + "php": "7.4" }, "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true, @@ -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", diff --git a/tests/class-two-factor-core.php b/tests/class-two-factor-core.php index ac1a8345..124da8ec 100644 --- a/tests/class-two-factor-core.php +++ b/tests/class-two-factor-core.php @@ -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 */ @@ -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(); } /** @@ -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 ); @@ -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 ); @@ -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 ); @@ -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( @@ -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 ) ); } @@ -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', @@ -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. @@ -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', diff --git a/tests/providers/class-two-factor-backup-codes.php b/tests/providers/class-two-factor-backup-codes.php index b7860632..e43e5055 100644 --- a/tests/providers/class-two-factor-backup-codes.php +++ b/tests/providers/class-two-factor-backup-codes.php @@ -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(); } @@ -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() ); } /** @@ -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 ); } /** @@ -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]; @@ -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 ) ); } @@ -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 ) ); @@ -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 ) ); @@ -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] ); @@ -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] ) ); @@ -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( '

', $buffer ); - $this->assertContains( '