From 429c7b8ddc3c773d3ca4b3de862ed390e5055fb3 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Mon, 4 Jan 2021 23:32:49 +0000 Subject: [PATCH] Tests: Increase use of shared fixtures in capability checks. See #51802. git-svn-id: https://develop.svn.wordpress.org/trunk@49932 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/user/author.php | 33 +++++++++-------------- tests/phpunit/tests/user/capabilities.php | 16 +++++------ 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/tests/phpunit/tests/user/author.php b/tests/phpunit/tests/user/author.php index 472e7a2b73..0cae9b2bcb 100644 --- a/tests/phpunit/tests/user/author.php +++ b/tests/phpunit/tests/user/author.php @@ -15,9 +15,10 @@ class Tests_User_Author_Template extends WP_UnitTestCase { public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { self::$author_id = $factory->user->create( array( - 'role' => 'author', - 'user_login' => 'test_author', - 'description' => 'test_author', + 'role' => 'author', + 'user_login' => 'test_author', + 'display_name' => 'Test Author', + 'description' => 'test_author', ) ); @@ -48,13 +49,13 @@ class Tests_User_Author_Template extends WP_UnitTestCase { $user = new WP_User( self::$author_id ); $this->assertSame( $user->display_name, $author_name ); - $this->assertSame( 'test_author', $author_name ); + $this->assertSame( 'Test Author', $author_name ); } function test_get_the_author_meta() { $this->assertSame( 'test_author', get_the_author_meta( 'login' ) ); $this->assertSame( 'test_author', get_the_author_meta( 'user_login' ) ); - $this->assertSame( 'test_author', get_the_author_meta( 'display_name' ) ); + $this->assertSame( 'Test Author', get_the_author_meta( 'display_name' ) ); $this->assertSame( 'test_author', trim( get_the_author_meta( 'description' ) ) ); $this->assertSame( 'test_author', get_the_author_meta( 'user_description' ) ); @@ -110,12 +111,7 @@ class Tests_User_Author_Template extends WP_UnitTestCase { * @ticket 30355 */ public function test_get_the_author_posts_link_no_permalinks() { - $author = self::factory()->user->create_and_get( - array( - 'display_name' => 'Foo', - 'user_nicename' => 'bar', - ) - ); + $author = get_userdata( self::$author_id ); $GLOBALS['authordata'] = $author->data; @@ -124,8 +120,8 @@ class Tests_User_Author_Template extends WP_UnitTestCase { $url = sprintf( 'http://%1$s/?author=%2$s', WP_TESTS_DOMAIN, $author->ID ); $this->assertContains( $url, $link ); - $this->assertContains( 'Posts by Foo', $link ); - $this->assertContains( '>Foo', $link ); + $this->assertContains( 'Posts by Test Author', $link ); + $this->assertContains( '>Test Author', $link ); unset( $GLOBALS['authordata'] ); } @@ -136,12 +132,7 @@ class Tests_User_Author_Template extends WP_UnitTestCase { public function test_get_the_author_posts_link_with_permalinks() { $this->set_permalink_structure( '/%postname%/' ); - $author = self::factory()->user->create_and_get( - array( - 'display_name' => 'Foo', - 'user_nicename' => 'bar', - ) - ); + $author = get_userdata( self::$author_id ); $GLOBALS['authordata'] = $author; @@ -152,8 +143,8 @@ class Tests_User_Author_Template extends WP_UnitTestCase { $this->set_permalink_structure( '' ); $this->assertContains( $url, $link ); - $this->assertContains( 'Posts by Foo', $link ); - $this->assertContains( '>Foo', $link ); + $this->assertContains( 'Posts by Test Author', $link ); + $this->assertContains( '>Test Author', $link ); unset( $GLOBALS['authordata'] ); } diff --git a/tests/phpunit/tests/user/capabilities.php b/tests/phpunit/tests/user/capabilities.php index 2459884cea..55394e7c52 100644 --- a/tests/phpunit/tests/user/capabilities.php +++ b/tests/phpunit/tests/user/capabilities.php @@ -1004,7 +1004,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase { */ function test_user_add_cap() { // There are two contributors. - $id_1 = self::factory()->user->create( array( 'role' => 'contributor' ) ); + $id_1 = self::$users['contributor']->ID; $id_2 = self::factory()->user->create( array( 'role' => 'contributor' ) ); // User 1 has an extra capability. @@ -1043,7 +1043,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase { */ function test_user_remove_cap() { // There are two contributors. - $id_1 = self::factory()->user->create( array( 'role' => 'contributor' ) ); + $id_1 = self::$users['contributor']->ID; $id_2 = self::factory()->user->create( array( 'role' => 'contributor' ) ); // User 1 has an extra capability. @@ -1075,7 +1075,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase { */ function test_user_level_update() { // User starts as an author. - $id = self::factory()->user->create( array( 'role' => 'author' ) ); + $id = self::$users['author']->ID; $user = new WP_User( $id ); $this->assertTrue( $user->exists(), "Problem getting user $id" ); @@ -1098,7 +1098,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase { function test_user_remove_all_caps() { // User starts as an author. - $id = self::factory()->user->create( array( 'role' => 'author' ) ); + $id = self::$users['author']->ID; $user = new WP_User( $id ); $this->assertTrue( $user->exists(), "Problem getting user $id" ); @@ -1153,10 +1153,10 @@ class Tests_User_Capabilities extends WP_UnitTestCase { $this->assertTrue( $author->exists(), "Problem getting user $author->ID" ); // Add some other users. - $admin = new WP_User( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + $admin = self::$users['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' ) ) ); + $editor = self::$users['editor']; + $contributor = self::$users['contributor']; // Administrators, editors and the post owner can edit it. $this->assertTrue( $admin->has_cap( 'edit_post', $post ) ); @@ -1900,7 +1900,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase { * @ticket 35488 */ function test_wp_logout_should_clear_current_user() { - $user_id = self::factory()->user->create(); + $user_id = self::$users['author']->ID; wp_set_current_user( $user_id ); wp_logout();