diff --git a/composer.json b/composer.json index 29466a9..cb119f5 100644 --- a/composer.json +++ b/composer.json @@ -14,9 +14,10 @@ "require-dev": { "phpcompatibility/phpcompatibility-wp": "2.1.4", "phpunit/phpunit": "9.6.13", - "yoast/phpunit-polyfills": "2.0.0", + "yoast/phpunit-polyfills": "^3.0", "automattic/vipwpcs": "^3.0", - "sirbrillig/phpcs-variable-analysis": "^2.11" + "sirbrillig/phpcs-variable-analysis": "^2.11", + "wp-phpunit/wp-phpunit": "^6.6" }, "scripts": { "cs": "@php ./vendor/bin/phpcs -p -s -v -n . --standard=\"phpcs.xml.dist\" --extensions=php --ignore=\"/vendor/*,/node_modules/*\"", diff --git a/composer.lock b/composer.lock index 45fbf62..130de7a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d63fb105d5efd1d85f8bb3c3539fa238", + "content-hash": "5f0f3371efc8cc5c94fa51e79d154d57", "packages": [ { "name": "composer/installers", @@ -2591,31 +2591,81 @@ ], "time": "2024-03-25T16:39:00+00:00" }, + { + "name": "wp-phpunit/wp-phpunit", + "version": "6.6.2", + "source": { + "type": "git", + "url": "https://github.com/wp-phpunit/wp-phpunit.git", + "reference": "7a1d3a2150033a3d3e19de40aa5b2ef2fee36bc3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/wp-phpunit/wp-phpunit/zipball/7a1d3a2150033a3d3e19de40aa5b2ef2fee36bc3", + "reference": "7a1d3a2150033a3d3e19de40aa5b2ef2fee36bc3", + "shasum": "" + }, + "type": "library", + "autoload": { + "files": [ + "__loaded.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-2.0-or-later" + ], + "authors": [ + { + "name": "Evan Mattson", + "email": "me@aaemnnost.tv" + }, + { + "name": "WordPress Community", + "homepage": "https://wordpress.org/about/" + } + ], + "description": "WordPress core PHPUnit library", + "homepage": "https://github.com/wp-phpunit", + "keywords": [ + "phpunit", + "test", + "wordpress" + ], + "support": { + "docs": "https://github.com/wp-phpunit/docs", + "issues": "https://github.com/wp-phpunit/issues", + "source": "https://github.com/wp-phpunit/wp-phpunit" + }, + "time": "2024-07-17T01:13:44+00:00" + }, { "name": "yoast/phpunit-polyfills", - "version": "2.0.0", + "version": "3.0.0", "source": { "type": "git", "url": "https://github.com/Yoast/PHPUnit-Polyfills.git", - "reference": "c758753e8f9dac251fed396a73c8305af3f17922" + "reference": "19e6d5fb8aad31f731f774f9646a10c64a8843d2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Yoast/PHPUnit-Polyfills/zipball/c758753e8f9dac251fed396a73c8305af3f17922", - "reference": "c758753e8f9dac251fed396a73c8305af3f17922", + "url": "https://api.github.com/repos/Yoast/PHPUnit-Polyfills/zipball/19e6d5fb8aad31f731f774f9646a10c64a8843d2", + "reference": "19e6d5fb8aad31f731f774f9646a10c64a8843d2", "shasum": "" }, "require": { - "php": ">=5.6", - "phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0" + "php": ">=7.0", + "phpunit/phpunit": "^6.4.4 || ^7.0 || ^8.0 || ^9.0 || ^11.0" }, "require-dev": { - "yoast/yoastcs": "^2.3.0" + "php-parallel-lint/php-console-highlighter": "^1.0.0", + "php-parallel-lint/php-parallel-lint": "^1.4.0", + "yoast/yoastcs": "^3.1.0" }, "type": "library", "extra": { "branch-alias": { - "dev-main": "2.x-dev" + "dev-main": "3.x-dev" } }, "autoload": { @@ -2647,9 +2697,10 @@ ], "support": { "issues": "https://github.com/Yoast/PHPUnit-Polyfills/issues", + "security": "https://github.com/Yoast/PHPUnit-Polyfills/security/policy", "source": "https://github.com/Yoast/PHPUnit-Polyfills" }, - "time": "2023-06-06T20:28:24+00:00" + "time": "2024-09-07T00:24:25+00:00" } ], "aliases": [], diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c002c84..8a4535a 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -9,7 +9,8 @@ // Require composer dependencies. require_once dirname( __DIR__ ) . '/vendor/autoload.php'; -$_tests_dir = getenv( 'WP_TESTS_DIR' ); +$_wp_tests_dir = getenv( 'WP_TESTS_DIR' ); +$_tests_dir = $_wp_tests_dir ? $_wp_tests_dir : getenv( 'WP_PHPUNIT__DIR' ); if ( ! $_tests_dir ) { $_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib'; @@ -38,9 +39,8 @@ function _manually_load_plugin() { tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); -// Add TestCase classes. -require_once __DIR__ . '/workflow-test-case.php'; -require_once __DIR__ . '/rest-test-case.php'; +// Start up the WP testing environment. +require "{$_tests_dir}/includes/bootstrap.php"; // Allow wp_mail() in tests from a valid domain name tests_add_filter( @@ -50,5 +50,6 @@ function () { } ); -// Start up the WP testing environment. -require "{$_tests_dir}/includes/bootstrap.php"; +// Add TestCase classes. +require_once __DIR__ . '/workflow-test-case.php'; +require_once __DIR__ . '/rest-test-case.php'; diff --git a/tests/modules/custom-status/meta/test-required-metadata-id-handler.php b/tests/modules/custom-status/meta/test-required-metadata-id-handler.php index b5df937..292136f 100644 --- a/tests/modules/custom-status/meta/test-required-metadata-id-handler.php +++ b/tests/modules/custom-status/meta/test-required-metadata-id-handler.php @@ -9,35 +9,17 @@ use VIPWorkflow\Modules\CustomStatus; use VIPWorkflow\Modules\CustomStatus\Meta\RequiredMetadataIdHandler; -use VIPWorkflow\Modules\Shared\PHP\OptionsUtilities; -use WP_UnitTestCase; - -class RequiredMetadataIdHandlerTest extends WP_UnitTestCase { - - /** - * Before each test, ensure default custom statuses are available and reset all module options. - */ - protected function setUp(): void { - parent::setUp(); - - // Reset all module options - OptionsUtilities::reset_all_module_options(); - - // Normally custom statuses are installed on 'admin_init', which is only run when a page is accessed - // in the admin web interface. Manually install them here. This avoid issues when a test creates or deletes - // a status and it's the only status existing, which can cause errors due to status restrictions. - CustomStatus::setup_install(); - } +class RequiredMetadataIdHandlerTest extends WorkflowTestCase { public function test_remove_deleted_metadata_from_required_metadata() { - $meta_id = 1; + $meta_id = 1; $custom_status_term = CustomStatus::add_custom_status( [ - 'name' => 'Test Custom Status', - 'slug' => 'test-custom-status', - 'description' => 'Test Description.', + 'name' => 'Test Custom Status', + 'slug' => 'test-custom-status', + 'description' => 'Test Description.', 'required_metadata_ids' => [ $meta_id ], ] ); - $term_id = $custom_status_term->term_id; + $term_id = $custom_status_term->term_id; RequiredMetadataIdHandler::remove_deleted_metadata_from_required_metadata( $meta_id ); @@ -46,7 +28,5 @@ public function test_remove_deleted_metadata_from_required_metadata() { $this->assertEquals( 'Test Custom Status', $updated_term->name ); $this->assertEquals( 'Test Description.', $updated_term->description ); $this->assertEmpty( $updated_term->meta['required_metadata_ids'] ); - - CustomStatus::delete_custom_status( $term_id ); } } diff --git a/tests/modules/custom-status/meta/test-required-user-id-handler.php b/tests/modules/custom-status/meta/test-required-user-id-handler.php index b860235..03d8cd9 100644 --- a/tests/modules/custom-status/meta/test-required-user-id-handler.php +++ b/tests/modules/custom-status/meta/test-required-user-id-handler.php @@ -9,35 +9,17 @@ use VIPWorkflow\Modules\CustomStatus; use VIPWorkflow\Modules\CustomStatus\Meta\RequiredUserIdHandler; -use VIPWorkflow\Modules\Shared\PHP\OptionsUtilities; -use WP_UnitTestCase; - -class RequiredUserIdHandlerTest extends WP_UnitTestCase { - - /** - * Before each test, ensure default custom statuses are available and reset all module options. - */ - protected function setUp(): void { - parent::setUp(); - - // Reset all module options - OptionsUtilities::reset_all_module_options(); - - // Normally custom statuses are installed on 'admin_init', which is only run when a page is accessed - // in the admin web interface. Manually install them here. This avoid issues when a test creates or deletes - // a status and it's the only status existing, which can cause errors due to status restrictions. - CustomStatus::setup_install(); - } +class RequiredUserIdHandlerTest extends WorkflowTestCase { public function test_remove_deleted_user_from_required_users_no_reassigned_user() { - $deleted_user_id = 1; + $deleted_user_id = 1; $custom_status_term = CustomStatus::add_custom_status( [ - 'name' => 'Test Custom Status', - 'slug' => 'test-custom-status', - 'description' => 'Test Description.', + 'name' => 'Test Custom Status', + 'slug' => 'test-custom-status', + 'description' => 'Test Description.', 'required_user_ids' => [ $deleted_user_id ], ] ); - $term_id = $custom_status_term->term_id; + $term_id = $custom_status_term->term_id; RequiredUserIdHandler::remove_deleted_user_from_required_users( $deleted_user_id, null ); @@ -46,20 +28,18 @@ public function test_remove_deleted_user_from_required_users_no_reassigned_user( $this->assertEquals( 'Test Custom Status', $updated_term->name ); $this->assertEquals( 'Test Description.', $updated_term->description ); $this->assertEmpty( $updated_term->meta['required_user_ids'] ); - - CustomStatus::delete_custom_status( $term_id ); } public function test_remove_deleted_user_from_required_users_with_reassigned_user() { - $deleted_user_id = 1; + $deleted_user_id = 1; $reassigned_user_id = 2; $custom_status_term = CustomStatus::add_custom_status( [ - 'name' => 'Test Custom Status', - 'slug' => 'test-custom-status', - 'description' => 'Test Description.', + 'name' => 'Test Custom Status', + 'slug' => 'test-custom-status', + 'description' => 'Test Description.', 'required_user_ids' => [ $deleted_user_id ], ] ); - $term_id = $custom_status_term->term_id; + $term_id = $custom_status_term->term_id; RequiredUserIdHandler::remove_deleted_user_from_required_users( $deleted_user_id, $reassigned_user_id ); @@ -69,7 +49,5 @@ public function test_remove_deleted_user_from_required_users_with_reassigned_use $this->assertEquals( 'Test Description.', $updated_term->description ); $this->assertCount( 1, $updated_term->meta['required_user_ids'] ); $this->assertEquals( $reassigned_user_id, $updated_term->meta['required_user_ids'][0] ); - - CustomStatus::delete_custom_status( $term_id ); } } diff --git a/tests/modules/custom-status/rest/test-custom-status-endpoint.php b/tests/modules/custom-status/rest/test-custom-status-endpoint.php index 2211b54..3d9bad7 100644 --- a/tests/modules/custom-status/rest/test-custom-status-endpoint.php +++ b/tests/modules/custom-status/rest/test-custom-status-endpoint.php @@ -17,34 +17,21 @@ */ class CustomStatusRestApiTest extends RestTestCase { - /** - * Before each test, ensure default custom statuses are available and reset all module options. - */ - protected function setUp(): void { - parent::setUp(); - - // Reset all module options - OptionsUtilities::reset_all_module_options(); - - // Normally custom statuses are installed on 'admin_init', which is only run when a page is accessed - // in the admin web interface. Manually install them here. This avoid issues when a test creates or deletes - // a status and it's the only status existing, which can cause errors due to status restrictions. - CustomStatus::setup_install(); - } - public function test_create_custom_status_with_optional_fields() { $editorial_metadata_term = EditorialMetadata::insert_editorial_metadata_term( [ 'name' => 'Test Metadata 1', 'description' => 'A test metadata for testing', 'type' => 'text', ] ); - $admin_user_id = self::create_user( 'test-admin', [ 'role' => 'administrator' ] ); + $admin_user_id = $this->factory()->user->create( [ + 'role' => 'administrator', + ] ); $request = new WP_REST_Request( 'POST', sprintf( '/%s/%s', VIP_WORKFLOW_REST_NAMESPACE, 'custom-status' ) ); $request->set_body_params( [ - 'name' => 'test-status', - 'description' => 'A test status for testing', - 'required_user_ids' => [ $admin_user_id ], + 'name' => 'test-status', + 'description' => 'A test status for testing', + 'required_user_ids' => [ $admin_user_id ], 'required_metadata_ids' => [ $editorial_metadata_term->term_id ], ] ); @@ -68,9 +55,6 @@ public function test_create_custom_status_with_optional_fields() { $this->assertEquals( $admin_user_id, $created_term->meta['required_user_ids'][0] ); $this->assertCount( 1, $created_term->meta['required_metadata_ids'] ); $this->assertEquals( $editorial_metadata_term->term_id, $created_term->meta['required_metadata_ids'][0] ); - - CustomStatus::delete_custom_status( $term_id ); - EditorialMetadata::delete_editorial_metadata_term( $editorial_metadata_term->term_id ); } public function test_create_custom_status() { @@ -94,19 +78,19 @@ public function test_create_custom_status() { $created_term = CustomStatus::get_custom_status_by( 'id', $term_id ); $this->assertEquals( 'test-status', $created_term->name ); - - CustomStatus::delete_custom_status( $term_id ); } public function test_update_custom_status() { $custom_status_term = CustomStatus::add_custom_status( [ - 'name' => 'Test Custom Status', - 'slug' => 'test-custom-status', - 'description' => 'Test Description.', + 'name' => 'Test Custom Status', + 'slug' => 'test-custom-status', + 'description' => 'Test Description.', ] ); $term_id = $custom_status_term->term_id; - $editor_user_id = self::create_user( 'test-editor', [ 'role' => 'editor' ] ); + $editor_user_id = $this->factory()->user->create( [ + 'role' => 'editor', + ] ); $request = new WP_REST_Request( 'PUT', sprintf( '/%s/%s/%d', VIP_WORKFLOW_REST_NAMESPACE, 'custom-status', $term_id ) ); $request->set_body_params( [ @@ -129,8 +113,6 @@ public function test_update_custom_status() { $this->assertEquals( 'Test Description 2!', $updated_term->description ); $this->assertCount( 1, $updated_term->meta['required_user_ids'] ); $this->assertEquals( $editor_user_id, $updated_term->meta['required_user_ids'][0] ); - - CustomStatus::delete_custom_status( $term_id ); } public function test_delete_custom_status() { @@ -193,9 +175,5 @@ public function test_reorder_custom_status() { $this->assertEquals( $term3->term_id, $reordered_custom_statuses[0]->term_id ); $this->assertEquals( $term1->term_id, $reordered_custom_statuses[1]->term_id ); $this->assertEquals( $term2->term_id, $reordered_custom_statuses[2]->term_id ); - - CustomStatus::delete_custom_status( $term1->term_id ); - CustomStatus::delete_custom_status( $term2->term_id ); - CustomStatus::delete_custom_status( $term3->term_id ); } } diff --git a/tests/modules/custom-status/test-custom-status-transitions.php b/tests/modules/custom-status/test-custom-status-transitions.php index 2b5b6ae..c610ef4 100644 --- a/tests/modules/custom-status/test-custom-status-transitions.php +++ b/tests/modules/custom-status/test-custom-status-transitions.php @@ -15,7 +15,10 @@ class CustomStatusTransitionsTest extends WorkflowTestCase { public function test_transition_restrictions_as_privileged_user() { - $admin_user_id = self::create_user( 'test-admin', [ 'role' => 'administrator' ] ); + $admin_user_id = $this->factory()->user->create( [ + 'role' => 'administrator', + ] ); + wp_set_current_user( $admin_user_id ); // Setup statuses, with the second status requiring admin user permissions @@ -62,18 +65,13 @@ public function test_transition_restrictions_as_privileged_user() { $this->assertEquals( $post_id, $transtion_to_status_3_result ); $this->assertEquals( 'status-3', get_post_status( $post_id ) ); - - // Cleanup - CustomStatus::delete_custom_status( $status_1->term_id ); - CustomStatus::delete_custom_status( $status_2_restricted->term_id ); - CustomStatus::delete_custom_status( $status_3->term_id ); - - wp_set_current_user( null ); - wp_delete_user( $admin_user_id ); } public function test_transition_restrictions_as_unprivileged_user() { - $author_user_id = self::create_user( 'test-unprivileged-author', [ 'role' => 'author' ] ); + $author_user_id = $this->factory()->user->create( [ + 'role' => 'author', + ] ); + wp_set_current_user( $author_user_id ); // Setup statuses, with the second status requiring admin user permissions @@ -83,7 +81,10 @@ public function test_transition_restrictions_as_unprivileged_user() { 'slug' => 'status-1', ] ); - $admin_user_id = self::create_user( 'test-admin', [ 'role' => 'administrator' ] ); + $admin_user_id = $this->factory()->user->create( [ + 'role' => 'administrator', + ] ); + $status_2_restricted = CustomStatus::add_custom_status( [ 'name' => 'Status 2 (restricted)', 'position' => -2, @@ -121,10 +122,5 @@ public function test_transition_restrictions_as_unprivileged_user() { $this->assertInstanceOf( 'WP_Error', $transtion_to_status_3_result ); $this->assertEquals( 'status-2-restricted', get_post_status( $post_id ) ); - - // Cleanup - CustomStatus::delete_custom_status( $status_1->term_id ); - CustomStatus::delete_custom_status( $status_2_restricted->term_id ); - CustomStatus::delete_custom_status( $status_3->term_id ); } } diff --git a/tests/modules/editorial-metadata/test-editorial-metadata-endpoint.php b/tests/modules/editorial-metadata/test-editorial-metadata-endpoint.php index 035e584..38f736e 100644 --- a/tests/modules/editorial-metadata/test-editorial-metadata-endpoint.php +++ b/tests/modules/editorial-metadata/test-editorial-metadata-endpoint.php @@ -41,8 +41,6 @@ public function test_create_editorial_metadata() { $this->assertEquals( 'A test metadata for testing', $created_term->description ); $this->assertEquals( 'text', $created_term->meta['type'] ); $this->assertEquals( 'vw_editorial_meta_text_' . $term_id, $created_term->meta['postmeta_key'] ); - - EditorialMetadata::delete_editorial_metadata_term( $term_id ); } public function test_update_editorial_metadata() { @@ -74,8 +72,6 @@ public function test_update_editorial_metadata() { $this->assertEquals( 'Test Description 2!', $updated_term->description ); $this->assertEquals( 'text', $updated_term->meta['type'] ); $this->assertEquals( 'vw_editorial_meta_text_' . $term_id, $updated_term->meta['postmeta_key'] ); - - EditorialMetadata::delete_editorial_metadata_term( $term_id ); } public function test_delete_editorial_metadata() { diff --git a/tests/modules/notifications/test-notifications.php b/tests/modules/notifications/test-notifications.php index 236b41d..8abfc94 100644 --- a/tests/modules/notifications/test-notifications.php +++ b/tests/modules/notifications/test-notifications.php @@ -12,30 +12,26 @@ use VIPWorkflow\Modules\Settings; use VIPWorkflow\Modules\Shared\PHP\OptionsUtilities; use WP_Error; -use WP_UnitTestCase; -class NotificationsTest extends WP_UnitTestCase { +class NotificationsTest extends WorkflowTestCase { protected function tearDown(): void { parent::tearDown(); reset_phpmailer_instance(); - - // Reset all module options - OptionsUtilities::reset_all_module_options(); } public function test_validate_get_notification_footer() { $expected_result = "\r\n--------------------\r\nYou are receiving this email because a notification was configured via the VIP Workflow Plugin.\r\n"; - $result = Notifications::get_notification_footer(); + $result = Notifications::get_notification_footer(); $this->assertTrue( $result === $expected_result ); } public function test_send_emails() { $recipients = [ 'test1@gmail.com', 'test2@gmail.com', 'test3@gmail.com' ]; - $subject = 'Test Subject'; - $body = 'Test Body'; + $subject = 'Test Subject'; + $body = 'Test Body'; Notifications::send_emails( $recipients, $subject, $body ); diff --git a/tests/rest-test-case.php b/tests/rest-test-case.php index 494d890..e0dd1c8 100644 --- a/tests/rest-test-case.php +++ b/tests/rest-test-case.php @@ -7,7 +7,6 @@ namespace VIPWorkflow\Tests; -use PHPUnit\Framework\TestCase; use WP_REST_Request; use WP_REST_Server; @@ -25,11 +24,9 @@ protected function setUp(): void { parent::setUp(); // Create an administrative user for tests to use - $this->administrator_user_id = $this->create_user( 'admin-rest-user', [ - 'user_pass' => wp_generate_password(), - 'user_email' => 'admin-rest-user@example.com', - 'role' => 'administrator', - ]); + $this->administrator_user_id = $this->factory()->user->create( [ + 'role' => 'administrator', + ] ); // Create a new REST server $this->server = new WP_REST_Server(); diff --git a/tests/workflow-test-case.php b/tests/workflow-test-case.php index d4c851a..15fa360 100644 --- a/tests/workflow-test-case.php +++ b/tests/workflow-test-case.php @@ -7,60 +7,23 @@ namespace VIPWorkflow\Tests; -use PHPUnit\Framework\TestCase; +use VIPWorkflow\Modules\CustomStatus; +use VIPWorkflow\Modules\Shared\PHP\OptionsUtilities; +use WP_UnitTestCase; /** * Extension of TestCase with helper methods */ -class WorkflowTestCase extends TestCase { - protected $user_ids_to_cleanup = []; - +class WorkflowTestCase extends WP_UnitTestCase { /** * Before each test, register REST endpoints. */ protected function setUp(): void { - parent::setUp(); - - // Always ensure we're starting as an unauthenticated user unless specifically set - wp_set_current_user( null ); - } - - /** - * After each test, reset REST endpoints. - */ - protected function tearDown(): void { - // Remove any users created during this test - foreach ( $this->user_ids_to_cleanup as $user_id ) { - if ( is_multisite() ) { - // Ensure user is fully deleted in multisite tests - wpmu_delete_user( $user_id ); - } else { - wp_delete_user( $user_id ); - } - } - - $this->user_ids_to_cleanup = []; + // Reset module options at the start of each test, which allows setup_install() to install a default + // set of custom statuses. Tested code expects some default statuses to always be available. + OptionsUtilities::reset_all_module_options(); + CustomStatus::setup_install(); - parent::tearDown(); - } - - protected function create_user( $username, $args = [] ) { - $default_args = [ - 'user_login' => $username, - 'user_pass' => 'password', - 'display_name' => $username, - 'user_email' => sprintf( '%s@example.com', $username ), - 'role' => 'editor', - ]; - - $user_id = wp_insert_user( array_merge( $default_args, $args ) ); - - if ( is_wp_error( $user_id ) ) { - throw new \Exception( esc_html( $user_id->get_error_message() ) ); - } - - $this->user_ids_to_cleanup[] = $user_id; - - return $user_id; + parent::setUp(); } }