Skip to content

Commit

Permalink
Build/Test Tools: Avoid using wp_delete_user() in PHPUnit tests unl…
Browse files Browse the repository at this point in the history
…ess explicitly acknowledging or ignoring Multisite.

`wp_delete_user()` does not actually delete the entire user when using WordPress Multisite. Therefore tests should typically use the test helper method to fully delete the user, unless explicitly ignoring Multisite or testing the `wp_delete_user()` function while taking Multisite behavior into account.

Fixes #61851.


git-svn-id: https://develop.svn.wordpress.org/trunk@58876 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
felixarntz committed Aug 9, 2024
1 parent defaa76 commit 4333979
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
10 changes: 5 additions & 5 deletions tests/phpunit/tests/admin/includesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function test_submenu_position( $position, $expected_position ) {
wp_set_current_user( $current_user );

// Clean up the temporary user.
wp_delete_user( $admin_user );
self::delete_user( $admin_user );

// Verify the menu was inserted at the expected position.
$this->assertSame( 'custom-position', $submenu[ $parent ][ $expected_position ][2] );
Expand Down Expand Up @@ -204,7 +204,7 @@ public function test_submenu_helpers_position( $position, $expected_position ) {
}

// Clean up the temporary user.
wp_delete_user( $admin_user );
self::delete_user( $admin_user );

foreach ( $actual_positions as $test => $actual_position ) {
// Verify the menu was inserted at the expected position.
Expand Down Expand Up @@ -295,7 +295,7 @@ public function test_position_when_parent_slug_child_slug_are_the_same() {

// Clean up the temporary user.
wp_set_current_user( $current_user );
wp_delete_user( $admin_user );
self::delete_user( $admin_user );

// Verify the menu was inserted at the expected position.
$this->assertSame( 'main_slug', $submenu['main_slug'][0][2] );
Expand Down Expand Up @@ -326,7 +326,7 @@ public function test_passing_string_as_position_fires_doing_it_wrong_submenu() {

// Clean up the temporary user.
wp_set_current_user( $current_user );
wp_delete_user( $admin_user );
self::delete_user( $admin_user );

// Verify the menu was inserted at the expected position.
$this->assertSame( 'submenu_page_1', $submenu['main_slug'][1][2] );
Expand Down Expand Up @@ -355,7 +355,7 @@ public function test_passing_float_as_position_does_not_override_int() {

// Clean up the temporary user.
wp_set_current_user( $current_user );
wp_delete_user( $admin_user );
self::delete_user( $admin_user );

// Verify the menus were inserted.
$this->assertSame( 'main_slug_1', $menu[1][2] );
Expand Down
6 changes: 1 addition & 5 deletions tests/phpunit/tests/link/getDashboardUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
}

public static function wpTearDownAfterClass() {
if ( is_multisite() ) {
wpmu_delete_user( self::$user_id );
} else {
wp_delete_user( self::$user_id );
}
self::delete_user( self::$user_id );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/rest-api/rest-sidebars-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public static function wpSetUpBeforeClass( $factory ) {
}

public static function wpTearDownAfterClass() {
wp_delete_user( self::$admin_id );
wp_delete_user( self::$author_id );
self::delete_user( self::$admin_id );
self::delete_user( self::$author_id );
}

public function set_up() {
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/user/queryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public function test_query_cache_delete_user() {

$this->assertSameSets( $expected, $found, 'Find author in returned values' );

wp_delete_user( $user_id );
self::delete_user( $user_id );

$q2 = new WP_User_Query(
array(
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/xmlrpc/wp/getUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function test_valid_user() {
$this->assertSame( $user_data['user_login'], $result['username'] );
$this->assertContains( $user_data['role'], $result['roles'] );

wp_delete_user( $user_id );
self::delete_user( $user_id );
}

public function test_no_fields() {
Expand Down

0 comments on commit 4333979

Please sign in to comment.