Skip to content

Commit

Permalink
Change all tests to extend WP_UnitTestCase, use built-in factory meth…
Browse files Browse the repository at this point in the history
…ods, remove unnecessary per-test cleanup
  • Loading branch information
alecgeatches committed Oct 9, 2024
1 parent f6a362e commit c69cb67
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 191 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/*\"",
Expand Down
71 changes: 61 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand All @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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 );
}
}
44 changes: 11 additions & 33 deletions tests/modules/custom-status/meta/test-required-user-id-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

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

Expand All @@ -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 );
}
}
46 changes: 12 additions & 34 deletions tests/modules/custom-status/rest/test-custom-status-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ],
] );

Expand All @@ -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() {
Expand All @@ -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( [
Expand All @@ -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() {
Expand Down Expand Up @@ -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 );
}
}
Loading

0 comments on commit c69cb67

Please sign in to comment.