Unit Tests: one $factory to rule them all, and it shall be static.

Using more than one instance of `WP_UnitTest_Factory` causes all kinds of craziness, due to out-of-sync internal generator sequences. Since we want to use `setUpBeforeClass`, we were creating ad hoc instances. To avoid that, we were injecting one `static` instance via Dependency Injection in `wpSetUpBeforeClass`. All tests should really use the `static` instance, so we will remove the instance prop `$factory`.

Replace `$this->factory` with `self::$factory` over 2000 times.
Rewrite all of the tests that were hard-coding dynamic values. 

#YOLOFriday



git-svn-id: https://develop.svn.wordpress.org/trunk@35225 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor
2015-10-16 21:04:12 +00:00
parent 84272ff8cd
commit e70ebea219
169 changed files with 2631 additions and 2616 deletions

View File

@@ -222,11 +222,11 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
// test the default roles and caps
function test_all_roles_and_caps() {
$users = array(
'administrator' => $this->factory->user->create_and_get( array( 'role' => 'administrator' ) ),
'editor' => $this->factory->user->create_and_get( array( 'role' => 'editor' ) ),
'author' => $this->factory->user->create_and_get( array( 'role' => 'author' ) ),
'contributor' => $this->factory->user->create_and_get( array( 'role' => 'contributor' ) ),
'subscriber' => $this->factory->user->create_and_get( array( 'role' => 'subscriber' ) ),
'administrator' => self::$factory->user->create_and_get( array( 'role' => 'administrator' ) ),
'editor' => self::$factory->user->create_and_get( array( 'role' => 'editor' ) ),
'author' => self::$factory->user->create_and_get( array( 'role' => 'author' ) ),
'contributor' => self::$factory->user->create_and_get( array( 'role' => 'contributor' ) ),
'subscriber' => self::$factory->user->create_and_get( array( 'role' => 'subscriber' ) ),
);
$caps = $this->getCapsAndRoles();
@@ -258,11 +258,11 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
// special case for the link manager
function test_link_manager_caps() {
$users = array(
'administrator' => $this->factory->user->create_and_get( array( 'role' => 'administrator' ) ),
'editor' => $this->factory->user->create_and_get( array( 'role' => 'editor' ) ),
'author' => $this->factory->user->create_and_get( array( 'role' => 'author' ) ),
'contributor' => $this->factory->user->create_and_get( array( 'role' => 'contributor' ) ),
'subscriber' => $this->factory->user->create_and_get( array( 'role' => 'subscriber' ) ),
'administrator' => self::$factory->user->create_and_get( array( 'role' => 'administrator' ) ),
'editor' => self::$factory->user->create_and_get( array( 'role' => 'editor' ) ),
'author' => self::$factory->user->create_and_get( array( 'role' => 'author' ) ),
'contributor' => self::$factory->user->create_and_get( array( 'role' => 'contributor' ) ),
'subscriber' => self::$factory->user->create_and_get( array( 'role' => 'subscriber' ) ),
);
$caps = array(
'manage_links' => array( 'administrator', 'editor' ),
@@ -305,7 +305,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
}
$caps = $this->getCapsAndRoles();
$user = $this->factory->user->create_and_get( array( 'role' => 'administrator' ) );
$user = self::$factory->user->create_and_get( array( 'role' => 'administrator' ) );
grant_super_admin( $user->ID );
$this->assertTrue( is_super_admin( $user->ID ) );
@@ -321,7 +321,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
// a role that doesn't exist
function test_bogus_role() {
$user = $this->factory->user->create_and_get( array( 'role' => 'invalid_role' ) );
$user = self::$factory->user->create_and_get( array( 'role' => 'invalid_role' ) );
// make sure the user is valid
$this->assertTrue( $user->exists(), "User does not exist" );
@@ -339,7 +339,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
// a user with multiple roles
function test_user_subscriber_contributor() {
$user = $this->factory->user->create_and_get( array( 'role' => 'subscriber' ) );
$user = self::$factory->user->create_and_get( array( 'role' => 'subscriber' ) );
// make sure the user is valid
$this->assertTrue( $user->exists(), "User does not exist" );
@@ -372,7 +372,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
$this->_flush_roles();
$this->assertTrue( $wp_roles->is_role( $role_name ) );
$user = $this->factory->user->create_and_get( array( 'role' => $role_name ) );
$user = self::$factory->user->create_and_get( array( 'role' => $role_name ) );
// make sure the user is valid
$this->assertTrue( $user->exists(), "User does not exist" );
@@ -409,7 +409,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
$this->_flush_roles();
$this->assertTrue( $wp_roles->is_role( $role_name ) );
$user = $this->factory->user->create_and_get( array( 'role' => $role_name ) );
$user = self::$factory->user->create_and_get( array( 'role' => $role_name ) );
// make sure the user is valid
$this->assertTrue( $user->exists(), "User does not exist" );
@@ -446,7 +446,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
$this->assertTrue( $wp_roles->is_role($role_name) );
// assign a user to that role
$id = $this->factory->user->create( array( 'role' => $role_name ) );
$id = self::$factory->user->create( array( 'role' => $role_name ) );
// now add a cap to the role
$wp_roles->add_cap($role_name, 'sweep_floor');
@@ -484,7 +484,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
$this->assertTrue( $wp_roles->is_role($role_name) );
// assign a user to that role
$id = $this->factory->user->create( array( 'role' => $role_name ) );
$id = self::$factory->user->create( array( 'role' => $role_name ) );
// now remove a cap from the role
$wp_roles->remove_cap($role_name, 'polish_doorknobs');
@@ -513,8 +513,8 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
// add an extra capability to a user
// there are two contributors
$id_1 = $this->factory->user->create( array( 'role' => 'contributor' ) );
$id_2 = $this->factory->user->create( array( 'role' => 'contributor' ) );
$id_1 = self::$factory->user->create( array( 'role' => 'contributor' ) );
$id_2 = self::$factory->user->create( array( 'role' => 'contributor' ) );
// user 1 has an extra capability
$user_1 = new WP_User($id_1);
@@ -550,8 +550,8 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
// add an extra capability to a user then remove it
// there are two contributors
$id_1 = $this->factory->user->create( array( 'role' => 'contributor' ) );
$id_2 = $this->factory->user->create( array( 'role' => 'contributor' ) );
$id_1 = self::$factory->user->create( array( 'role' => 'contributor' ) );
$id_2 = self::$factory->user->create( array( 'role' => 'contributor' ) );
// user 1 has an extra capability
$user_1 = new WP_User($id_1);
@@ -581,7 +581,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
// make sure the user_level is correctly set and changed with the user's role
// user starts as an author
$id = $this->factory->user->create( array( 'role' => 'author' ) );
$id = self::$factory->user->create( array( 'role' => 'author' ) );
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
@@ -604,7 +604,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
function test_user_remove_all_caps() {
// user starts as an author
$id = $this->factory->user->create( array( 'role' => 'author' ) );
$id = self::$factory->user->create( array( 'role' => 'author' ) );
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
@@ -646,19 +646,19 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
// simple tests for some common meta capabilities
// Make our author
$author = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
$author = new WP_User( self::$factory->user->create( array( 'role' => 'author' ) ) );
// make a post
$post = $this->factory->post->create( array( 'post_author' => $author->ID, 'post_type' => 'post' ) );
$post = self::$factory->post->create( array( 'post_author' => $author->ID, 'post_type' => 'post' ) );
// the author of the post
$this->assertTrue($author->exists(), "Problem getting user $author->ID");
// add some other users
$admin = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$author_2 = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
$editor = new WP_User( $this->factory->user->create( array( 'role' => 'editor' ) ) );
$contributor = new WP_User( $this->factory->user->create( array( 'role' => 'contributor' ) ) );
$admin = new WP_User( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
$author_2 = new WP_User( self::$factory->user->create( array( 'role' => 'author' ) ) );
$editor = new WP_User( self::$factory->user->create( array( 'role' => 'editor' ) ) );
$contributor = new WP_User( self::$factory->user->create( array( 'role' => 'contributor' ) ) );
// administrators, editors and the post owner can edit it
$this->assertTrue($admin->has_cap('edit_post', $post));
@@ -732,11 +732,11 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
*/
function test_authorless_post( $status ) {
// Make a post without an author
$post = $this->factory->post->create( array( 'post_author' => 0, 'post_type' => 'post', 'post_status' => $status ) );
$post = self::$factory->post->create( array( 'post_author' => 0, 'post_type' => 'post', 'post_status' => $status ) );
// Add an editor and contributor
$editor = $this->factory->user->create_and_get( array( 'role' => 'editor' ) );
$contributor = $this->factory->user->create_and_get( array( 'role' => 'contributor' ) );
$editor = self::$factory->user->create_and_get( array( 'role' => 'editor' ) );
$contributor = self::$factory->user->create_and_get( array( 'role' => 'contributor' ) );
// editor can edit, view, and trash
$this->assertTrue( $editor->has_cap( 'edit_post', $post ) );
@@ -753,11 +753,11 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
* @ticket 16714
*/
function test_create_posts_caps() {
$author = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
$admin = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$author_2 = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
$editor = new WP_User( $this->factory->user->create( array( 'role' => 'editor' ) ) );
$contributor = new WP_User( $this->factory->user->create( array( 'role' => 'contributor' ) ) );
$author = new WP_User( self::$factory->user->create( array( 'role' => 'author' ) ) );
$admin = new WP_User( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
$author_2 = new WP_User( self::$factory->user->create( array( 'role' => 'author' ) ) );
$editor = new WP_User( self::$factory->user->create( array( 'role' => 'editor' ) ) );
$contributor = new WP_User( self::$factory->user->create( array( 'role' => 'contributor' ) ) );
// create_posts isn't a real cap.
$this->assertFalse($admin->has_cap('create_posts'));
@@ -818,19 +818,19 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
// simple tests for some common meta capabilities
// Make our author
$author = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
$author = new WP_User( self::$factory->user->create( array( 'role' => 'author' ) ) );
// make a page
$page = $this->factory->post->create( array( 'post_author' => $author->ID, 'post_type' => 'page' ) );
$page = self::$factory->post->create( array( 'post_author' => $author->ID, 'post_type' => 'page' ) );
// the author of the page
$this->assertTrue($author->exists(), "Problem getting user " . $author->ID);
// add some other users
$admin = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$author_2 = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
$editor = new WP_User( $this->factory->user->create( array( 'role' => 'editor' ) ) );
$contributor = new WP_User( $this->factory->user->create( array( 'role' => 'contributor' ) ) );
$admin = new WP_User( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
$author_2 = new WP_User( self::$factory->user->create( array( 'role' => 'author' ) ) );
$editor = new WP_User( self::$factory->user->create( array( 'role' => 'editor' ) ) );
$contributor = new WP_User( self::$factory->user->create( array( 'role' => 'contributor' ) ) );
// administrators, editors and the post owner can edit it
$this->assertTrue($admin->has_cap('edit_page', $page));
@@ -853,7 +853,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
* @ticket 21786
*/
function test_negative_caps() {
$author = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
$author = new WP_User( self::$factory->user->create( array( 'role' => 'author' ) ) );
$author->add_cap( 'foo', false );
$this->assertTrue ( isset( $author->caps['foo'] ) );
$author->remove_cap( 'foo' );
@@ -864,7 +864,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
* @ticket 18932
*/
function test_set_role_same_role() {
$user = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$user = new WP_User( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
$caps = $user->caps;
$this->assertNotEmpty( $user->caps );
$user->set_role( 'administrator' );
@@ -875,7 +875,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
function test_current_user_can_for_blog() {
global $wpdb;
$user = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$user = new WP_User( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
$old_uid = get_current_user_id();
wp_set_current_user( $user->ID );
@@ -890,7 +890,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
$this->assertFalse( current_user_can_for_blog( 12345, 'edit_posts' ) );
$wpdb->suppress_errors( $suppress );
$blog_id = $this->factory->blog->create( array( 'user_id' => $user->ID ) );
$blog_id = self::$factory->blog->create( array( 'user_id' => $user->ID ) );
$this->assertTrue( current_user_can_for_blog( $blog_id, 'edit_posts' ) );
$this->assertFalse( current_user_can_for_blog( $blog_id, 'foo_the_bar' ) );
@@ -904,7 +904,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
}
$orig_blog_id = get_current_blog_id();
$blog_id = $this->factory->blog->create();
$blog_id = self::$factory->blog->create();
$this->_nullify_current_user();
@@ -932,7 +932,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
* @ticket 28374
*/
function test_current_user_edit_caps() {
$user = new WP_User( $this->factory->user->create( array( 'role' => 'contributor' ) ) );
$user = new WP_User( self::$factory->user->create( array( 'role' => 'contributor' ) ) );
wp_set_current_user( $user->ID );
$user->add_cap( 'publish_posts' );
@@ -945,10 +945,10 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
}
function test_subscriber_cant_edit_posts() {
$user = new WP_User( $this->factory->user->create( array( 'role' => 'subscriber' ) ) );
$user = new WP_User( self::$factory->user->create( array( 'role' => 'subscriber' ) ) );
wp_set_current_user( $user->ID );
$post = $this->factory->post->create( array( 'post_author' => 1 ) );
$post = self::$factory->post->create( array( 'post_author' => 1 ) );
$this->assertFalse( current_user_can( 'edit_post', $post ) );
$this->assertFalse( current_user_can( 'edit_post', $post + 1 ) );
@@ -960,8 +960,8 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
return;
}
$user = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$other_user = new WP_User( $this->factory->user->create( array( 'role' => 'subscriber' ) ) );
$user = new WP_User( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
$other_user = new WP_User( self::$factory->user->create( array( 'role' => 'subscriber' ) ) );
wp_set_current_user( $user->ID );
@@ -974,7 +974,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
return;
}
$user = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$user = new WP_User( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
wp_set_current_user( $user->ID );
@@ -987,9 +987,9 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
return;
}
$user = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$user = new WP_User( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
$user->add_cap( 'manage_network_users' );
$other_user = new WP_User( $this->factory->user->create( array( 'role' => 'subscriber' ) ) );
$other_user = new WP_User( self::$factory->user->create( array( 'role' => 'subscriber' ) ) );
wp_set_current_user( $user->ID );
@@ -1002,9 +1002,9 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
return;
}
$user = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$user = new WP_User( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
$user->add_cap( 'manage_network_users' );
$super_admin = new WP_User( $this->factory->user->create( array( 'role' => 'subscriber' ) ) );
$super_admin = new WP_User( self::$factory->user->create( array( 'role' => 'subscriber' ) ) );
grant_super_admin( $super_admin->ID );
wp_set_current_user( $user->ID );
@@ -1017,11 +1017,11 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
*/
function test_require_edit_others_posts_if_post_type_doesnt_exist() {
register_post_type( 'existed' );
$post_id = $this->factory->post->create( array( 'post_type' => 'existed' ) );
$post_id = self::$factory->post->create( array( 'post_type' => 'existed' ) );
_unregister_post_type( 'existed' );
$subscriber_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
$editor_id = $this->factory->user->create( array( 'role' => 'editor' ) );
$subscriber_id = self::$factory->user->create( array( 'role' => 'subscriber' ) );
$editor_id = self::$factory->user->create( array( 'role' => 'editor' ) );
$this->setExpectedIncorrectUsage( 'map_meta_cap' );
foreach ( array( 'delete_post', 'edit_post', 'read_post', 'publish_post' ) as $cap ) {
@@ -1046,10 +1046,10 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
$cpt = get_post_type_object( 'page_capability' );
$admin = $this->factory->user->create_and_get( array( 'role' => 'administrator' ) );
$editor = $this->factory->user->create_and_get( array( 'role' => 'editor' ) );
$author = $this->factory->user->create_and_get( array( 'role' => 'author' ) );
$contributor = $this->factory->user->create_and_get( array( 'role' => 'contributor' ) );
$admin = self::$factory->user->create_and_get( array( 'role' => 'administrator' ) );
$editor = self::$factory->user->create_and_get( array( 'role' => 'editor' ) );
$author = self::$factory->user->create_and_get( array( 'role' => 'author' ) );
$contributor = self::$factory->user->create_and_get( array( 'role' => 'contributor' ) );
$this->assertEquals( 'edit_pages', $cpt->cap->edit_posts );
$this->assertTrue( user_can( $admin->ID, $cpt->cap->edit_posts ) );
@@ -1057,7 +1057,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
$this->assertFalse( user_can( $author->ID, $cpt->cap->edit_posts ) );
$this->assertFalse( user_can( $contributor->ID, $cpt->cap->edit_posts ) );
$admin_post = $this->factory->post->create_and_get( array(
$admin_post = self::$factory->post->create_and_get( array(
'post_author' => $admin->ID,
'post_type' => 'page_capability',
) );
@@ -1067,7 +1067,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
$this->assertFalse( user_can( $author->ID, 'edit_post', $admin_post->ID ) );
$this->assertFalse( user_can( $contributor->ID, 'edit_post', $admin_post->ID ) );
$author_post = $this->factory->post->create_and_get( array(
$author_post = self::$factory->post->create_and_get( array(
'post_author' => $author->ID,
'post_type' => 'page_capability',
) );