diff --git a/tests/phpunit/includes/factory.php b/tests/phpunit/includes/factory.php index 40a5ab7417..e9c4743834 100644 --- a/tests/phpunit/includes/factory.php +++ b/tests/phpunit/includes/factory.php @@ -401,11 +401,17 @@ abstract class WP_UnitTest_Factory_For_Thing { } class WP_UnitTest_Generator_Sequence { - var $next; - var $template_string; + static $incr = -1; + public $next; + public $template_string; - function __construct( $template_string = '%s', $start = 1 ) { - $this->next = $start; + function __construct( $template_string = '%s', $start = null ) { + if ( $start ) { + $this->next = $start; + } else { + self::$incr++; + $this->next = self::$incr; + } $this->template_string = $template_string; } diff --git a/tests/phpunit/includes/testcase-canonical.php b/tests/phpunit/includes/testcase-canonical.php index a82c8c5e0d..24e651721f 100644 --- a/tests/phpunit/includes/testcase-canonical.php +++ b/tests/phpunit/includes/testcase-canonical.php @@ -134,11 +134,7 @@ class WP_Canonical_UnitTestCase extends WP_UnitTestCase { * @since 4.1.0 */ public static function delete_shared_fixtures() { - if ( is_multisite() ) { - wpmu_delete_user( self::$author_id ); - } else { - wp_delete_user( self::$author_id ); - } + self::delete_user( self::$author_id ); foreach ( self::$post_ids as $pid ) { wp_delete_post( $pid, true ); diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index 8874a1c6c2..84d4db256a 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -5,11 +5,6 @@ */ class Tests_Admin_includesPost extends WP_UnitTestCase { - function tearDown() { - wp_set_current_user( 0 ); - parent::tearDown(); - } - function test__wp_translate_postdata_cap_checks_contributor() { $contributor_id = self::factory()->user->create( array( 'role' => 'contributor' ) ); $editor_id = self::factory()->user->create( array( 'role' => 'editor' ) ); diff --git a/tests/phpunit/tests/ajax/Autosave.php b/tests/phpunit/tests/ajax/Autosave.php index 4b2b26c6ec..148c8962e8 100644 --- a/tests/phpunit/tests/ajax/Autosave.php +++ b/tests/phpunit/tests/ajax/Autosave.php @@ -40,15 +40,6 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase { $this->_post = get_post( $post_id ); } - /** - * Tear down the test fixture. - * Reset the current user - */ - public function tearDown() { - wp_set_current_user( 0 ); - parent::tearDown(); - } - /** * Test autosaving a post * @return void diff --git a/tests/phpunit/tests/ajax/CustomizeMenus.php b/tests/phpunit/tests/ajax/CustomizeMenus.php index 87191c6a36..025d29f6a6 100644 --- a/tests/phpunit/tests/ajax/CustomizeMenus.php +++ b/tests/phpunit/tests/ajax/CustomizeMenus.php @@ -28,14 +28,6 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase { $wp_customize = $this->wp_customize; } - /** - * Tear down the test fixture. - */ - public function tearDown() { - wp_set_current_user( 0 ); - parent::tearDown(); - } - /** * Helper to keep it DRY * diff --git a/tests/phpunit/tests/attachment/slashes.php b/tests/phpunit/tests/attachment/slashes.php index 724e6a9204..fb0ec4cfbd 100644 --- a/tests/phpunit/tests/attachment/slashes.php +++ b/tests/phpunit/tests/attachment/slashes.php @@ -9,7 +9,6 @@ class Tests_Attachment_Slashes extends WP_UnitTestCase { function setUp() { parent::setUp(); $this->author_id = self::factory()->user->create( array( 'role' => 'editor' ) ); - $this->old_current_user = get_current_user_id(); wp_set_current_user( $this->author_id ); // it is important to test with both even and odd numbered slashes as @@ -23,12 +22,6 @@ class Tests_Attachment_Slashes extends WP_UnitTestCase { $this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\'; } - function tearDown() { - wp_set_current_user( $this->old_current_user ); - wp_delete_user( $this->author_id ); - parent::tearDown(); - } - /** * Tests the model function that expects slashed data * diff --git a/tests/phpunit/tests/basic.php b/tests/phpunit/tests/basic.php index 78cf0fb480..d824d3395d 100644 --- a/tests/phpunit/tests/basic.php +++ b/tests/phpunit/tests/basic.php @@ -5,21 +5,6 @@ * @group testsuite */ class Tests_Basic extends WP_UnitTestCase { - var $val; - - function setUp() { - parent::setUp(); - $this->val = true; - } - - function tearDown() { - $this->val = false; - parent::tearDown(); - } - - function test_true() { - $this->assertTrue($this->val); - } function test_license() { $license = file_get_contents( ABSPATH . 'license.txt' ); diff --git a/tests/phpunit/tests/cache.php b/tests/phpunit/tests/cache.php index bc671ecd86..87028c5ce2 100644 --- a/tests/phpunit/tests/cache.php +++ b/tests/phpunit/tests/cache.php @@ -4,7 +4,7 @@ * @group cache */ class Tests_Cache extends WP_UnitTestCase { - var $cache = NULL; + var $cache = null; function setUp() { parent::setUp(); diff --git a/tests/phpunit/tests/comment-submission.php b/tests/phpunit/tests/comment-submission.php index 96ba7a299e..548b77b609 100644 --- a/tests/phpunit/tests/comment-submission.php +++ b/tests/phpunit/tests/comment-submission.php @@ -10,13 +10,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase { require_once ABSPATH . WPINC . '/class-phpass.php'; } - function tearDown() { - wp_set_current_user( 0 ); - parent::tearDown(); - } - public function test_submitting_comment_to_invalid_post_returns_error() { - $error = 'comment_id_not_found'; $this->assertSame( 0, did_action( $error ) ); diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index cf55e07db9..7a6846bcf6 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -15,7 +15,7 @@ class Tests_Comment extends WP_UnitTestCase { } public static function wpTearDownAfterClass() { - wp_delete_post( self::$post_id ); + wp_delete_post( self::$post_id, true ); self::delete_user( self::$user_id ); } diff --git a/tests/phpunit/tests/comment/slashes.php b/tests/phpunit/tests/comment/slashes.php index 6a87ab510c..69aba67b25 100644 --- a/tests/phpunit/tests/comment/slashes.php +++ b/tests/phpunit/tests/comment/slashes.php @@ -10,7 +10,6 @@ class Tests_Comment_Slashes extends WP_UnitTestCase { parent::setUp(); // we need an admin user to bypass comment flood protection $this->author_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); - $this->old_current_user = get_current_user_id(); wp_set_current_user( $this->author_id ); // it is important to test with both even and odd numbered slashes as @@ -24,11 +23,6 @@ class Tests_Comment_Slashes extends WP_UnitTestCase { $this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\'; } - function tearDown() { - wp_set_current_user( $this->old_current_user ); - parent::tearDown(); - } - /** * Tests the extended model function that expects slashed data * diff --git a/tests/phpunit/tests/link.php b/tests/phpunit/tests/link.php index 272b4cdcd1..54c1bd0316 100644 --- a/tests/phpunit/tests/link.php +++ b/tests/phpunit/tests/link.php @@ -4,12 +4,6 @@ */ class Tests_Link extends WP_UnitTestCase { - function tearDown() { - global $wp_rewrite; - $wp_rewrite->init(); - parent::tearDown(); - } - function _get_pagenum_link_cb( $url ) { return $url . '/WooHoo'; } diff --git a/tests/phpunit/tests/meta/slashes.php b/tests/phpunit/tests/meta/slashes.php index a5da86f428..9aca03e2fa 100644 --- a/tests/phpunit/tests/meta/slashes.php +++ b/tests/phpunit/tests/meta/slashes.php @@ -10,7 +10,7 @@ class Tests_Meta_Slashes extends WP_UnitTestCase { parent::setUp(); $this->author_id = self::factory()->user->create( array( 'role' => 'editor' ) ); $this->post_id = self::factory()->post->create(); - $this->old_current_user = get_current_user_id(); + wp_set_current_user( $this->author_id ); $this->slash_1 = 'String with 1 slash \\'; @@ -22,11 +22,6 @@ class Tests_Meta_Slashes extends WP_UnitTestCase { $this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\'; } - function tearDown() { - wp_set_current_user( $this->old_current_user ); - parent::tearDown(); - } - /** * Tests the controller function that expects slashed data * diff --git a/tests/phpunit/tests/multisite/getSpaceAllowed.php b/tests/phpunit/tests/multisite/getSpaceAllowed.php index 6470f38592..d387f47ed2 100644 --- a/tests/phpunit/tests/multisite/getSpaceAllowed.php +++ b/tests/phpunit/tests/multisite/getSpaceAllowed.php @@ -18,7 +18,6 @@ class Tests_Multisite_Get_Space_Allowed extends WP_UnitTestCase { self::$original_site_blog_upload_space = get_site_option( 'blog_upload_space' ); self::$original_blog_upload_space = get_option( 'blog_upload_space' ); - } public function setUp() { diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index 8afdbe8fb6..9680b3edc1 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 ); + wp_delete_post( $post2, true ); $this->assertEquals( 1, get_blog_details()->post_count ); } diff --git a/tests/phpunit/tests/option/userSettings.php b/tests/phpunit/tests/option/userSettings.php index 4da33e5659..53c1e9dd6d 100644 --- a/tests/phpunit/tests/option/userSettings.php +++ b/tests/phpunit/tests/option/userSettings.php @@ -14,7 +14,6 @@ class Tests_User_Settings extends WP_UnitTestCase { function tearDown() { unset( $GLOBALS['_updated_user_settings'] ); - delete_user_option( $this->user_id, 'user-settings' ); parent::tearDown(); } diff --git a/tests/phpunit/tests/post/getPages.php b/tests/phpunit/tests/post/getPages.php index 5391815aa0..c9f336f955 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 ); + wp_delete_post( $pages[0]->ID, true ); $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/post/meta.php b/tests/phpunit/tests/post/meta.php index 7ca095f816..82c5c122d5 100644 --- a/tests/phpunit/tests/post/meta.php +++ b/tests/phpunit/tests/post/meta.php @@ -32,12 +32,6 @@ class Tests_Post_Meta extends WP_UnitTestCase { $this->post_id_2 = wp_insert_post($post); } - function tearDown() { - wp_delete_post($this->post_id); - wp_delete_post($this->post_id_2); - parent::tearDown(); - } - function test_unique_postmeta() { // Add a unique post meta item $this->assertInternalType( 'integer', add_post_meta($this->post_id, 'unique', 'value', true) ); diff --git a/tests/phpunit/tests/post/slashes.php b/tests/phpunit/tests/post/slashes.php index f42114105d..ed144c0a8b 100644 --- a/tests/phpunit/tests/post/slashes.php +++ b/tests/phpunit/tests/post/slashes.php @@ -9,7 +9,7 @@ class Tests_Post_Slashes extends WP_UnitTestCase { function setUp() { parent::setUp(); $this->author_id = self::factory()->user->create( array( 'role' => 'editor' ) ); - $this->old_current_user = get_current_user_id(); + wp_set_current_user( $this->author_id ); // it is important to test with both even and odd numbered slashes as @@ -23,11 +23,6 @@ class Tests_Post_Slashes extends WP_UnitTestCase { $this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\'; } - function tearDown() { - wp_set_current_user( $this->old_current_user ); - parent::tearDown(); - } - /** * Tests the controller function that expects slashed data * diff --git a/tests/phpunit/tests/post/thumbnails.php b/tests/phpunit/tests/post/thumbnails.php index 39013a602d..822154f1ff 100644 --- a/tests/phpunit/tests/post/thumbnails.php +++ b/tests/phpunit/tests/post/thumbnails.php @@ -17,8 +17,8 @@ class Tests_Post_Thumbnail_Template extends WP_UnitTestCase { } public static function wpTearDownAfterClass() { - wp_delete_post( self::$post->ID ); - wp_delete_attachment( self::$attachment_id ); + wp_delete_post( self::$post->ID, true ); + wp_delete_attachment( self::$attachment_id, true ); } function test_has_post_thumbnail() { diff --git a/tests/phpunit/tests/query/conditionals.php b/tests/phpunit/tests/query/conditionals.php index 8a351e7c73..2f99998f3d 100644 --- a/tests/phpunit/tests/query/conditionals.php +++ b/tests/phpunit/tests/query/conditionals.php @@ -27,13 +27,6 @@ class Tests_Query_Conditionals extends WP_UnitTestCase { $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); } - function tearDown() { - global $wp_rewrite; - $wp_rewrite->init(); - - parent::tearDown(); - } - function test_home() { $this->go_to('/'); $this->assertQueryTrue('is_home'); diff --git a/tests/phpunit/tests/query/setupPostdata.php b/tests/phpunit/tests/query/setupPostdata.php index 73ec7bddfc..e99a042149 100644 --- a/tests/phpunit/tests/query/setupPostdata.php +++ b/tests/phpunit/tests/query/setupPostdata.php @@ -23,21 +23,6 @@ class Tests_Query_SetupPostdata extends WP_UnitTestCase { } } - public function tearDown() { - parent::tearDown(); - return; - - foreach ( $this->global_keys as $global_key ) { - if ( ! is_null( $this->global_data[ $global_key ] ) ) { - $GLOBALS[ $global_key ] = $this->global_data[ $global_key ]; - } else { - unset( $GLOBALS[ $global_key ] ); - } - } - - $this->global_data = array(); - } - public function test_id() { $p = self::factory()->post->create_and_get(); setup_postdata( $p ); diff --git a/tests/phpunit/tests/shortcode.php b/tests/phpunit/tests/shortcode.php index a2ff424d4f..65da789c25 100644 --- a/tests/phpunit/tests/shortcode.php +++ b/tests/phpunit/tests/shortcode.php @@ -20,8 +20,9 @@ class Tests_Shortcode extends WP_UnitTestCase { function tearDown() { global $shortcode_tags; - foreach ( $this->shortcodes as $shortcode ) + foreach ( $this->shortcodes as $shortcode ) { unset( $shortcode_tags[ $shortcode ] ); + } parent::tearDown(); } diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index ca5b79a065..513c913ee6 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -12,7 +12,9 @@ class Tests_Term extends WP_UnitTestCase { } public static function wpTearDownAfterClass() { - array_map( 'wp_delete_post', self::$post_ids ); + foreach ( self::$post_ids as $post_id ) { + wp_delete_post( $post_id, true ); + } } /** diff --git a/tests/phpunit/tests/term/slashes.php b/tests/phpunit/tests/term/slashes.php index 9970a25050..c01a7554a3 100644 --- a/tests/phpunit/tests/term/slashes.php +++ b/tests/phpunit/tests/term/slashes.php @@ -9,7 +9,7 @@ class Tests_Term_Slashes extends WP_Ajax_UnitTestCase { function setUp() { parent::setUp(); $this->author_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); - $this->old_current_user = get_current_user_id(); + wp_set_current_user( $this->author_id ); $this->slash_1 = 'String with 1 slash \\'; @@ -21,11 +21,6 @@ class Tests_Term_Slashes extends WP_Ajax_UnitTestCase { $this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\'; } - function tearDown() { - wp_set_current_user( $this->old_current_user ); - parent::tearDown(); - } - /** * Tests the model function that expects slashed data * diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php index 82c99ef983..8b2054a34c 100644 --- a/tests/phpunit/tests/user.php +++ b/tests/phpunit/tests/user.php @@ -244,7 +244,11 @@ class Tests_User extends WP_UnitTestCase { ); foreach ( $roles as $role => $level ) { - $user_id = self::factory()->user->create( array( 'role' => $role ) ); + $user_id = self::factory()->user->create( array( + 'user_email' => 'user_19265_' . $role . '@burritos.com', + 'user_login' => 'user_19265_' . $role, + 'role' => $role + ) ); $user = new WP_User( $user_id ); $this->assertTrue( isset( $user->user_level ) ); @@ -775,6 +779,11 @@ class Tests_User extends WP_UnitTestCase { 'user_nicename' => str_repeat( 'a', 50 ), ) ); + $user1 = new WP_User( $u1 ); + + $expected = str_repeat( 'a', 50 ); + $this->assertSame( $expected, $user1->user_nicename ); + $user_login = str_repeat( 'a', 55 ); $u = wp_insert_user( array( 'user_login' => $user_login, @@ -783,19 +792,29 @@ class Tests_User extends WP_UnitTestCase { ) ); $this->assertNotEmpty( $u ); - $user = new WP_User( $u ); + $user2 = new WP_User( $u ); $expected = str_repeat( 'a', 48 ) . '-2'; - $this->assertSame( $expected, $user->user_nicename ); + $this->assertSame( $expected, $user2->user_nicename ); } /** * @ticket 33793 */ public function test_wp_insert_user_should_not_truncate_to_a_duplicate_user_nicename_when_suffix_has_more_than_one_character() { - $users = self::factory()->user->create_many( 4, array( + $user_ids = self::factory()->user->create_many( 4, array( 'user_nicename' => str_repeat( 'a', 50 ), ) ); + foreach ( $user_ids as $i => $user_id ) { + $user = new WP_User( $user_id ); + if ( 0 === $i ) { + $expected = str_repeat( 'a', 50 ); + } else { + $expected = str_repeat( 'a', 48 ) . '-' . ( $i + 1 ); + } + $this->assertSame( $expected, $user->user_nicename ); + } + $user_login = str_repeat( 'a', 55 ); $u = wp_insert_user( array( 'user_login' => $user_login, diff --git a/tests/phpunit/tests/user/countUserPosts.php b/tests/phpunit/tests/user/countUserPosts.php index 2ae112943e..bc91ceb7b0 100644 --- a/tests/phpunit/tests/user/countUserPosts.php +++ b/tests/phpunit/tests/user/countUserPosts.php @@ -27,10 +27,11 @@ class Tests_User_CountUserPosts extends WP_UnitTestCase { 'post_author' => 12345, 'post_type' => 'wptests_pt', ) ) ); - self::$post_ids = array_merge( self::$post_ids, $factory->post->create_many( 1, array( + + self::$post_ids[] = $factory->post->create( array( 'post_author' => 12345, 'post_type' => 'wptests_pt', - ) ) ); + ) ); } public static function wpTearDownAfterClass() { diff --git a/tests/phpunit/tests/user/listAuthors.php b/tests/phpunit/tests/user/listAuthors.php index 86f1a3b395..fcafee70fc 100644 --- a/tests/phpunit/tests/user/listAuthors.php +++ b/tests/phpunit/tests/user/listAuthors.php @@ -4,7 +4,7 @@ * @group user */ class Tests_User_ListAuthors extends WP_UnitTestCase { - static $users = array(); + static $user_ids = array(); static $fred_id; static $posts = array(); static $user_urls = array(); @@ -24,13 +24,13 @@ class Tests_User_ListAuthors extends WP_UnitTestCase { 'html' => true ); */ public static function wpSetUpBeforeClass( $factory ) { - self::$users[] = $factory->user->create( array( 'user_login' => 'zack', 'display_name' => 'zack', 'role' => 'author', 'first_name' => 'zack', 'last_name' => 'moon' ) ); - self::$users[] = $factory->user->create( array( 'user_login' => 'bob', 'display_name' => 'bob', 'role' => 'author', 'first_name' => 'bob', 'last_name' => 'reno' ) ); - self::$users[] = $factory->user->create( array( 'user_login' => 'paul', 'display_name' => 'paul', 'role' => 'author', 'first_name' => 'paul', 'last_name' => 'norris' ) ); + self::$user_ids[] = $factory->user->create( array( 'user_login' => 'zack', 'display_name' => 'zack', 'role' => 'author', 'first_name' => 'zack', 'last_name' => 'moon' ) ); + self::$user_ids[] = $factory->user->create( array( 'user_login' => 'bob', 'display_name' => 'bob', 'role' => 'author', 'first_name' => 'bob', 'last_name' => 'reno' ) ); + self::$user_ids[] = $factory->user->create( array( 'user_login' => 'paul', 'display_name' => 'paul', 'role' => 'author', 'first_name' => 'paul', 'last_name' => 'norris' ) ); self::$fred_id = $factory->user->create( array( 'user_login' => 'fred', 'role' => 'author' ) ); $count = 0; - foreach ( self::$users as $userid ) { + foreach ( self::$user_ids as $userid ) { $count = $count + 1; for ( $i = 0; $i < $count; $i++ ) { self::$posts[] = $factory->post->create( array( 'post_type' => 'post', 'post_author' => $userid ) ); @@ -41,7 +41,9 @@ class Tests_User_ListAuthors extends WP_UnitTestCase { } public static function wpTearDownAfterClass() { - foreach ( array_merge( self::$users, array( self::$fred_id ) ) as $user_id ) { + self::$user_ids[] = self::$fred_id; + + foreach ( self::$user_ids as $user_id ) { self::delete_user( $user_id ); } @@ -96,17 +98,17 @@ class Tests_User_ListAuthors extends WP_UnitTestCase { } function test_wp_list_authors_feed() { - $url0 = get_author_feed_link( self::$users[0] ); - $url1 = get_author_feed_link( self::$users[1] ); - $url2 = get_author_feed_link( self::$users[2] ); + $url0 = get_author_feed_link( self::$user_ids[0] ); + $url1 = get_author_feed_link( self::$user_ids[1] ); + $url2 = get_author_feed_link( self::$user_ids[2] ); $expected['feed'] = '