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
This commit is contained in:
Scott Taylor 2015-10-17 20:48:33 +00:00
parent 8506f48d6b
commit c173dc5595
5 changed files with 58 additions and 47 deletions

View File

@ -6,16 +6,37 @@
* @group admin * @group admin
*/ */
class Tests_AdminBar extends WP_UnitTestCase { 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() { public static function setUpBeforeClass() {
require_once( ABSPATH . WPINC . '/class-wp-admin-bar.php' ); 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 * @ticket 21117
*/ */
function test_content_post_type() { 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 ) ); register_post_type( 'content', array( 'show_in_admin_bar' => true ) );
@ -34,7 +55,7 @@ class Tests_AdminBar extends WP_UnitTestCase {
* @ticket 21117 * @ticket 21117
*/ */
function test_merging_existing_meta_values() { 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; $admin_bar = new WP_Admin_Bar;
@ -42,16 +63,17 @@ class Tests_AdminBar extends WP_UnitTestCase {
'id' => 'test-node', 'id' => 'test-node',
'meta' => array( 'class' => 'test-class' ), '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( $admin_bar->add_node( array(
'id' => 'test-node', 'id' => 'test-node',
'meta' => array( 'some-meta' => 'value' ), 'meta' => array( 'some-meta' => 'value' ),
) ); ) );
$node = $admin_bar->get_node( 'test-node' ); $node2 = $admin_bar->get_node( 'test-node' );
$this->assertEquals( array( 'class' => 'test-class', 'some-meta' => 'value' ), $node->meta ); $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' ); $this->markTestSkipped( 'Test does not run in multisite' );
} }
$nobody = self::factory()->user->create( array( 'role' => '' ) ); $this->assertFalse( user_can( self::$no_role_id, 'read' ) );
$this->assertFalse( user_can( $nobody, 'read' ) );
wp_set_current_user( $nobody ); wp_set_current_user( self::$no_role_id );
$wp_admin_bar = $this->get_standard_admin_bar(); $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_my_account->href );
$this->assertFalse( $node_user_info->href ); $this->assertFalse( $node_user_info->href );
$this->assertNull( $node_edit_profile ); $this->assertNull( $node_edit_profile );
} }
/** /**
@ -92,10 +112,9 @@ class Tests_AdminBar extends WP_UnitTestCase {
$this->markTestSkipped( 'Test does not run in multisite' ); $this->markTestSkipped( 'Test does not run in multisite' );
} }
$editor = self::factory()->user->create( array( 'role' => 'editor' ) ); $this->assertTrue( user_can( self::$editor_id, 'read' ) );
$this->assertTrue( user_can( $editor, 'read' ) );
wp_set_current_user( $editor ); wp_set_current_user( self::$editor_id );
$wp_admin_bar = $this->get_standard_admin_bar(); $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' ); $this->markTestSkipped( 'Test only runs in multisite' );
} }
$admin = self::factory()->user->create( array( 'role' => 'administrator' ) ); $blog_id = self::factory()->blog->create( array(
$editor = self::factory()->user->create( array( 'role' => 'editor' ) ); 'user_id' => self::$admin_id,
$this->assertTrue( user_can( $admin, 'read' ) );
$this->assertTrue( user_can( $editor, 'read' ) );
$new_blog_id = self::factory()->blog->create( array(
'user_id' => $admin,
) ); ) );
$this->assertTrue( is_user_member_of_blog( $admin, $new_blog_id ) ); $this->assertTrue( user_can( self::$admin_id, 'read' ) );
$this->assertFalse( is_user_member_of_blog( $editor, $new_blog_id ) ); $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(); $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' ); $node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
// get primary blog // get primary blog
$primary = get_active_blog_for_user( $editor ); $primary = get_active_blog_for_user( self::$editor_id );
$this->assertInternalType( 'object', $primary ); $this->assertInternalType( 'object', $primary );
// No Site menu as the user isn't a member of this blog // 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 ); $this->assertEquals( $primary_profile_url, $node_edit_profile->href );
restore_current_blog(); restore_current_blog();
} }
/** /**
@ -179,29 +194,26 @@ class Tests_AdminBar extends WP_UnitTestCase {
$this->markTestSkipped( 'Test only runs in multisite' ); $this->markTestSkipped( 'Test only runs in multisite' );
} }
$admin = self::factory()->user->create( array( 'role' => 'administrator' ) ); $this->assertTrue( user_can( self::$admin_id, 'read' ) );
$nobody = self::factory()->user->create( array( 'role' => '' ) ); $this->assertFalse( user_can( self::$no_role_id, 'read' ) );
$this->assertTrue( user_can( $admin, 'read' ) ); $blog_id = self::factory()->blog->create( array(
$this->assertFalse( user_can( $nobody, 'read' ) ); 'user_id' => self::$admin_id,
$new_blog_id = self::factory()->blog->create( array(
'user_id' => $admin,
) ); ) );
$this->assertTrue( is_user_member_of_blog( $admin, $new_blog_id ) ); $this->assertTrue( is_user_member_of_blog( self::$admin_id, $blog_id ) );
$this->assertFalse( is_user_member_of_blog( $nobody, $new_blog_id ) ); $this->assertFalse( is_user_member_of_blog( self::$no_role_id, $blog_id ) );
$this->assertTrue( is_user_member_of_blog( $nobody, get_current_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 // 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->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(); $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' ); $node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
// get primary blog // get primary blog
$primary = get_active_blog_for_user( $nobody ); $primary = get_active_blog_for_user( self::$no_role_id );
$this->assertNull( $primary ); $this->assertNull( $primary );
// No Site menu as the user isn't a member of this site // 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 ); $this->assertEquals( $user_profile_url, $node_edit_profile->href );
restore_current_blog(); restore_current_blog();
} }
protected function get_standard_admin_bar() { protected function get_standard_admin_bar() {

View File

@ -674,7 +674,7 @@ class Tests_Multisite_Site extends WP_UnitTestCase {
$post2 = self::factory()->post->create(); $post2 = self::factory()->post->create();
$this->assertEquals( 2, get_blog_details()->post_count ); $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 ); $this->assertEquals( 1, get_blog_details()->post_count );
} }

View File

@ -78,7 +78,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
$last_changed = wp_cache_get( 'last_changed', 'posts' ); $last_changed = wp_cache_get( 'last_changed', 'posts' );
// This should bump last_changed. // 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 ); $old_changed_float = $this->_microtime_to_float( $last_changed );
$new_changed_float = $this->_microtime_to_float( wp_cache_get( 'last_changed', 'posts' ) ); $new_changed_float = $this->_microtime_to_float( wp_cache_get( 'last_changed', 'posts' ) );
$this->assertGreaterThan( $old_changed_float, $new_changed_float ); $this->assertGreaterThan( $old_changed_float, $new_changed_float );

View File

@ -387,7 +387,7 @@ class Tests_Term extends WP_UnitTestCase {
$this->assertTrue( wp_delete_term( $term_id, 'post_tag' ) ); $this->assertTrue( wp_delete_term( $term_id, 'post_tag' ) );
foreach ( $posts as $post_id ) foreach ( $posts as $post_id )
$this->assertTrue( (bool) wp_delete_post( $post_id, true ) ); $this->assertTrue( (bool) wp_delete_post( $post_id ) );
} }
/** /**