From c173dc55950d3fffd9625b8036a4e3c6da02a03f Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sat, 17 Oct 2015 20:48:33 +0000 Subject: [PATCH] Unit Tests: better fixtures for `Tests_AdminBar`. Don't force-delete some posts: some filter callbacks are no-ops on Multisite if the post is vanquished. See #30017, #33968. git-svn-id: https://develop.svn.wordpress.org/trunk@35246 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/adminbar.php | 97 ++++++++++++++------------ tests/phpunit/tests/multisite/site.php | 2 +- tests/phpunit/tests/post.php | 2 +- tests/phpunit/tests/post/getPages.php | 2 +- tests/phpunit/tests/term.php | 2 +- 5 files changed, 58 insertions(+), 47 deletions(-) diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index cf83adc96c..a2f61f23df 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -6,16 +6,37 @@ * @group admin */ class Tests_AdminBar extends WP_UnitTestCase { + protected static $editor_id; + protected static $admin_id; + protected static $no_role_id; + protected static $post_id; + protected static $blog_id; + + protected static $user_ids = array(); public static function setUpBeforeClass() { require_once( ABSPATH . WPINC . '/class-wp-admin-bar.php' ); + + parent::setUpBeforeClass(); + } + + public static function wpSetUpBeforeClass( $factory ) { + self::$user_ids[] = self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) ); + self::$user_ids[] = self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); + self::$user_ids[] = self::$no_role_id = $factory->user->create( array( 'role' => '' ) ); + } + + public static function wpTearDownAfterClass() { + foreach ( self::$user_ids as $id ) { + self::delete_user( $id ); + } } /** * @ticket 21117 */ function test_content_post_type() { - wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) ); + wp_set_current_user( self::$editor_id ); register_post_type( 'content', array( 'show_in_admin_bar' => true ) ); @@ -34,7 +55,7 @@ class Tests_AdminBar extends WP_UnitTestCase { * @ticket 21117 */ function test_merging_existing_meta_values() { - wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) ); + wp_set_current_user( self::$editor_id ); $admin_bar = new WP_Admin_Bar; @@ -42,16 +63,17 @@ class Tests_AdminBar extends WP_UnitTestCase { 'id' => 'test-node', 'meta' => array( 'class' => 'test-class' ), ) ); - $node = $admin_bar->get_node( 'test-node' ); - $this->assertEquals( array( 'class' => 'test-class' ), $node->meta ); + + $node1 = $admin_bar->get_node( 'test-node' ); + $this->assertEquals( array( 'class' => 'test-class' ), $node1->meta ); $admin_bar->add_node( array( 'id' => 'test-node', 'meta' => array( 'some-meta' => 'value' ), ) ); - $node = $admin_bar->get_node( 'test-node' ); - $this->assertEquals( array( 'class' => 'test-class', 'some-meta' => 'value' ), $node->meta ); + $node2 = $admin_bar->get_node( 'test-node' ); + $this->assertEquals( array( 'class' => 'test-class', 'some-meta' => 'value' ), $node2->meta ); } /** @@ -62,10 +84,9 @@ class Tests_AdminBar extends WP_UnitTestCase { $this->markTestSkipped( 'Test does not run in multisite' ); } - $nobody = self::factory()->user->create( array( 'role' => '' ) ); - $this->assertFalse( user_can( $nobody, 'read' ) ); + $this->assertFalse( user_can( self::$no_role_id, 'read' ) ); - wp_set_current_user( $nobody ); + wp_set_current_user( self::$no_role_id ); $wp_admin_bar = $this->get_standard_admin_bar(); @@ -81,7 +102,6 @@ class Tests_AdminBar extends WP_UnitTestCase { $this->assertFalse( $node_my_account->href ); $this->assertFalse( $node_user_info->href ); $this->assertNull( $node_edit_profile ); - } /** @@ -92,10 +112,9 @@ class Tests_AdminBar extends WP_UnitTestCase { $this->markTestSkipped( 'Test does not run in multisite' ); } - $editor = self::factory()->user->create( array( 'role' => 'editor' ) ); - $this->assertTrue( user_can( $editor, 'read' ) ); + $this->assertTrue( user_can( self::$editor_id, 'read' ) ); - wp_set_current_user( $editor ); + wp_set_current_user( self::$editor_id ); $wp_admin_bar = $this->get_standard_admin_bar(); @@ -125,22 +144,19 @@ class Tests_AdminBar extends WP_UnitTestCase { $this->markTestSkipped( 'Test only runs in multisite' ); } - $admin = self::factory()->user->create( array( 'role' => 'administrator' ) ); - $editor = self::factory()->user->create( array( 'role' => 'editor' ) ); - - $this->assertTrue( user_can( $admin, 'read' ) ); - $this->assertTrue( user_can( $editor, 'read' ) ); - - $new_blog_id = self::factory()->blog->create( array( - 'user_id' => $admin, + $blog_id = self::factory()->blog->create( array( + 'user_id' => self::$admin_id, ) ); - $this->assertTrue( is_user_member_of_blog( $admin, $new_blog_id ) ); - $this->assertFalse( is_user_member_of_blog( $editor, $new_blog_id ) ); + $this->assertTrue( user_can( self::$admin_id, 'read' ) ); + $this->assertTrue( user_can( self::$editor_id, 'read' ) ); - wp_set_current_user( $editor ); + $this->assertTrue( is_user_member_of_blog( self::$admin_id, $blog_id ) ); + $this->assertFalse( is_user_member_of_blog( self::$editor_id, $blog_id ) ); - switch_to_blog( $new_blog_id ); + wp_set_current_user( self::$editor_id ); + + switch_to_blog( $blog_id ); $wp_admin_bar = $this->get_standard_admin_bar(); @@ -150,7 +166,7 @@ class Tests_AdminBar extends WP_UnitTestCase { $node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' ); // get primary blog - $primary = get_active_blog_for_user( $editor ); + $primary = get_active_blog_for_user( self::$editor_id ); $this->assertInternalType( 'object', $primary ); // No Site menu as the user isn't a member of this blog @@ -167,7 +183,6 @@ class Tests_AdminBar extends WP_UnitTestCase { $this->assertEquals( $primary_profile_url, $node_edit_profile->href ); restore_current_blog(); - } /** @@ -179,29 +194,26 @@ class Tests_AdminBar extends WP_UnitTestCase { $this->markTestSkipped( 'Test only runs in multisite' ); } - $admin = self::factory()->user->create( array( 'role' => 'administrator' ) ); - $nobody = self::factory()->user->create( array( 'role' => '' ) ); + $this->assertTrue( user_can( self::$admin_id, 'read' ) ); + $this->assertFalse( user_can( self::$no_role_id, 'read' ) ); - $this->assertTrue( user_can( $admin, 'read' ) ); - $this->assertFalse( user_can( $nobody, 'read' ) ); - - $new_blog_id = self::factory()->blog->create( array( - 'user_id' => $admin, + $blog_id = self::factory()->blog->create( array( + 'user_id' => self::$admin_id, ) ); - $this->assertTrue( is_user_member_of_blog( $admin, $new_blog_id ) ); - $this->assertFalse( is_user_member_of_blog( $nobody, $new_blog_id ) ); - $this->assertTrue( is_user_member_of_blog( $nobody, get_current_blog_id() ) ); + $this->assertTrue( is_user_member_of_blog( self::$admin_id, $blog_id ) ); + $this->assertFalse( is_user_member_of_blog( self::$no_role_id, $blog_id ) ); + $this->assertTrue( is_user_member_of_blog( self::$no_role_id, get_current_blog_id() ) ); // Remove `$nobody` from the current blog, so they're not a member of any blog - $removed = remove_user_from_blog( $nobody, get_current_blog_id() ); + $removed = remove_user_from_blog( self::$no_role_id, get_current_blog_id() ); $this->assertTrue( $removed ); - $this->assertFalse( is_user_member_of_blog( $nobody, get_current_blog_id() ) ); + $this->assertFalse( is_user_member_of_blog( self::$no_role_id, get_current_blog_id() ) ); - wp_set_current_user( $nobody ); + wp_set_current_user( self::$no_role_id ); - switch_to_blog( $new_blog_id ); + switch_to_blog( $blog_id ); $wp_admin_bar = $this->get_standard_admin_bar(); @@ -211,7 +223,7 @@ class Tests_AdminBar extends WP_UnitTestCase { $node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' ); // get primary blog - $primary = get_active_blog_for_user( $nobody ); + $primary = get_active_blog_for_user( self::$no_role_id ); $this->assertNull( $primary ); // No Site menu as the user isn't a member of this site @@ -227,7 +239,6 @@ class Tests_AdminBar extends WP_UnitTestCase { $this->assertEquals( $user_profile_url, $node_edit_profile->href ); restore_current_blog(); - } protected function get_standard_admin_bar() { diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index 9680b3edc1..8afdbe8fb6 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -674,7 +674,7 @@ class Tests_Multisite_Site extends WP_UnitTestCase { $post2 = self::factory()->post->create(); $this->assertEquals( 2, get_blog_details()->post_count ); - wp_delete_post( $post2, true ); + wp_delete_post( $post2 ); $this->assertEquals( 1, get_blog_details()->post_count ); } diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index df1ab0a6f6..ad72a2238e 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -508,7 +508,7 @@ class Tests_Post extends WP_UnitTestCase { $this->assertEquals($future_date, $this->_next_schedule_for_post('publish_future_post', $id)); // now delete the post and make sure the cron entry is removed - wp_delete_post($id); + wp_delete_post( $id ); $this->assertFalse($this->_next_schedule_for_post('publish_future_post', $id)); } diff --git a/tests/phpunit/tests/post/getPages.php b/tests/phpunit/tests/post/getPages.php index c9f336f955..5391815aa0 100644 --- a/tests/phpunit/tests/post/getPages.php +++ b/tests/phpunit/tests/post/getPages.php @@ -78,7 +78,7 @@ class Tests_Post_getPages extends WP_UnitTestCase { $last_changed = wp_cache_get( 'last_changed', 'posts' ); // This should bump last_changed. - wp_delete_post( $pages[0]->ID, true ); + wp_delete_post( $pages[0]->ID ); $old_changed_float = $this->_microtime_to_float( $last_changed ); $new_changed_float = $this->_microtime_to_float( wp_cache_get( 'last_changed', 'posts' ) ); $this->assertGreaterThan( $old_changed_float, $new_changed_float ); diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 513c913ee6..0d005f38d3 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -387,7 +387,7 @@ class Tests_Term extends WP_UnitTestCase { $this->assertTrue( wp_delete_term( $term_id, 'post_tag' ) ); foreach ( $posts as $post_id ) - $this->assertTrue( (bool) wp_delete_post( $post_id, true ) ); + $this->assertTrue( (bool) wp_delete_post( $post_id ) ); } /**