diff --git a/tests/phpunit/includes/functions.php b/tests/phpunit/includes/functions.php index be9519239b..0e644ff824 100644 --- a/tests/phpunit/includes/functions.php +++ b/tests/phpunit/includes/functions.php @@ -48,13 +48,47 @@ function _test_filter_build_unique_id($tag, $function, $priority) { } } +function _delete_all_data() { + global $wpdb; + + foreach ( array( + $wpdb->posts, + $wpdb->postmeta, + $wpdb->comments, + $wpdb->commentmeta, + $wpdb->term_relationships, + $wpdb->termmeta + ) as $table ) { + $wpdb->query( "DELETE FROM {$table}" ); + } + + foreach ( array( + $wpdb->terms, + $wpdb->term_taxonomy + ) as $table ) { + $wpdb->query( "DELETE FROM {$table} WHERE term_id != 1" ); + } + + $wpdb->query( "UPDATE {$wpdb->term_taxonomy} SET count = 0" ); + + $wpdb->query( "DELETE FROM {$wpdb->users} WHERE ID != 1" ); + $wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE user_id != 1" ); +} + function _delete_all_posts() { global $wpdb; - $all_posts = $wpdb->get_col("SELECT ID from {$wpdb->posts}"); - if ($all_posts) { - foreach ($all_posts as $id) - wp_delete_post( $id, true ); + $all_posts = $wpdb->get_results( "SELECT ID, post_type from {$wpdb->posts}", ARRAY_A ); + if ( ! $all_posts ) { + return; + } + + foreach ( $all_posts as $data ) { + if ( 'attachment' === $data['post_type'] ) { + wp_delete_attachment( $data['ID'], true ); + } else { + wp_delete_post( $data['ID'], true ); + } } } diff --git a/tests/phpunit/includes/testcase-canonical.php b/tests/phpunit/includes/testcase-canonical.php index efdbf56548..64d7c22287 100644 --- a/tests/phpunit/includes/testcase-canonical.php +++ b/tests/phpunit/includes/testcase-canonical.php @@ -15,6 +15,14 @@ class WP_Canonical_UnitTestCase extends WP_UnitTestCase { */ public $structure = '/%year%/%monthnum%/%day%/%postname%/'; + public static function wpSetUpBeforeClass( $factory ) { + self::generate_shared_fixtures( $factory ); + } + + public static function wpTearDownAfterClass() { + self::delete_shared_fixtures(); + } + public function setUp() { parent::setUp(); @@ -134,20 +142,6 @@ class WP_Canonical_UnitTestCase extends WP_UnitTestCase { * @since 4.1.0 */ public static function delete_shared_fixtures() { - self::delete_user( self::$author_id ); - - foreach ( self::$post_ids as $pid ) { - wp_delete_post( $pid, true ); - } - - foreach ( self::$comment_ids as $cid ) { - wp_delete_comment( $cid, true ); - } - - foreach ( self::$term_ids as $tid => $tax ) { - wp_delete_term( $tid, $tax ); - } - self::$author_id = null; self::$post_ids = array(); self::$comment_ids = array(); diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index 6eed90af72..d278887f6f 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -61,6 +61,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { $c = self::get_called_class(); if ( ! method_exists( $c, 'wpSetUpBeforeClass' ) ) { + self::commit_transaction(); return; } @@ -72,8 +73,12 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { public static function tearDownAfterClass() { parent::tearDownAfterClass(); + _delete_all_data(); + self::flush_cache(); + $c = self::get_called_class(); if ( ! method_exists( $c, 'wpTearDownAfterClass' ) ) { + self::commit_transaction(); return; } @@ -158,7 +163,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { function clean_up_global_scope() { $_GET = array(); $_POST = array(); - $this->flush_cache(); + self::flush_cache(); } /** @@ -243,7 +248,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { } } - function flush_cache() { + static function flush_cache() { global $wp_object_cache; $wp_object_cache->group_ops = array(); $wp_object_cache->stats = array(); @@ -445,7 +450,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { $_SERVER['REQUEST_URI'] = $req; unset($_SERVER['PATH_INFO']); - $this->flush_cache(); + self::flush_cache(); unset($GLOBALS['wp_query'], $GLOBALS['wp_the_query']); $GLOBALS['wp_the_query'] = new WP_Query(); $GLOBALS['wp_query'] = $GLOBALS['wp_the_query']; diff --git a/tests/phpunit/tests/admin/includesComment.php b/tests/phpunit/tests/admin/includesComment.php index ceb6e9956c..72cdc8fa70 100644 --- a/tests/phpunit/tests/admin/includesComment.php +++ b/tests/phpunit/tests/admin/includesComment.php @@ -41,17 +41,6 @@ class Tests_Admin_IncludesComment extends WP_UnitTestCase { ) ); } - /** - * Delete the post and comments for the tests. - */ - public static function wpTearDownAfterClass() { - foreach ( self::$comment_ids as $comment_id ) { - wp_delete_comment( $comment_id, true ); - } - - wp_delete_post( self::$post_id, true ); - } - /** * Verify that both the comment date and author must match for a comment to exist. */ diff --git a/tests/phpunit/tests/admin/includesFile.php b/tests/phpunit/tests/admin/includesFile.php index f4ee0f1375..c9b4a11c0c 100644 --- a/tests/phpunit/tests/admin/includesFile.php +++ b/tests/phpunit/tests/admin/includesFile.php @@ -6,10 +6,6 @@ */ class Tests_Admin_includesFile extends WP_UnitTestCase { - function setUp() { - parent::setUp(); - } - /** * @ticket 20449 */ diff --git a/tests/phpunit/tests/admin/includesListTable.php b/tests/phpunit/tests/admin/includesListTable.php index 9dac76fdcf..b6696e482b 100644 --- a/tests/phpunit/tests/admin/includesListTable.php +++ b/tests/phpunit/tests/admin/includesListTable.php @@ -4,10 +4,10 @@ * @group admin */ class Tests_Admin_includesListTable extends WP_UnitTestCase { - protected static $top; - protected static $children; - protected static $grandchildren; - protected static $post_ids; + protected static $top = array(); + protected static $children = array(); + protected static $grandchildren = array(); + protected static $post_ids = array(); function setUp() { parent::setUp(); @@ -64,12 +64,6 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase { } } - public static function wpTearDownAfterClass() { - foreach ( self::$post_ids as $post_id ) { - wp_delete_post( $post_id, true ); - } - } - /** * @ticket 15459 */ diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index 5c3ef0eac9..5e74c05507 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -22,14 +22,6 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase { self::$post_id = $factory->post->create(); } - public static function wpTearDownAfterClass() { - foreach ( self::$user_ids as $id ) { - self::delete_user( $id ); - } - - wp_delete_post( self::$post_id, true ); - } - function test__wp_translate_postdata_cap_checks_contributor() { wp_set_current_user( self::$contributor_id ); diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index 8b17eeece6..a7d86980b4 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -26,12 +26,6 @@ class Tests_AdminBar extends WP_UnitTestCase { 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 */ diff --git a/tests/phpunit/tests/ajax/Autosave.php b/tests/phpunit/tests/ajax/Autosave.php index beb9227c8b..d6eac7c753 100644 --- a/tests/phpunit/tests/ajax/Autosave.php +++ b/tests/phpunit/tests/ajax/Autosave.php @@ -35,14 +35,6 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase { self::$post = get_post( self::$post_id ); } - public static function wpTearDownAfterClass() { - foreach ( self::$user_ids as $user_id ) { - self::delete_user( $user_id ); - } - - wp_delete_post( self::$post_id, true ); - } - /** * Set up the test fixture */ diff --git a/tests/phpunit/tests/ajax/DeleteComment.php b/tests/phpunit/tests/ajax/DeleteComment.php index e8f3ae1cdd..f88453a805 100644 --- a/tests/phpunit/tests/ajax/DeleteComment.php +++ b/tests/phpunit/tests/ajax/DeleteComment.php @@ -34,14 +34,6 @@ class Tests_Ajax_DeleteComment extends WP_Ajax_UnitTestCase { self::$comments = array_map( 'get_comment', $comment_ids ); } - public static function wpTearDownAfterClass() { - wp_delete_post( self::$post_id, true ); - - foreach ( self::$comments as $c ) { - wp_delete_comment( $c->ID, true ); - } - } - /** * Clear the POST actions in between requests */ diff --git a/tests/phpunit/tests/ajax/GetComments.php b/tests/phpunit/tests/ajax/GetComments.php index c1ca2a1ab0..81342f75d7 100644 --- a/tests/phpunit/tests/ajax/GetComments.php +++ b/tests/phpunit/tests/ajax/GetComments.php @@ -35,15 +35,6 @@ class Tests_Ajax_GetComments extends WP_Ajax_UnitTestCase { self::$no_comment_post = $factory->post->create_and_get(); } - public static function wpTearDownAfterClass() { - foreach ( self::$comment_ids as $comment_id ) { - wp_delete_comment( $comment_id, true ); - } - - wp_delete_post( self::$comment_post->ID, true ); - wp_delete_post( self::$no_comment_post->ID, true ); - } - /** * Get comments as a privilged user (administrator) * Expects test to pass diff --git a/tests/phpunit/tests/ajax/ReplytoComment.php b/tests/phpunit/tests/ajax/ReplytoComment.php index df10c744f1..33a0650472 100644 --- a/tests/phpunit/tests/ajax/ReplytoComment.php +++ b/tests/phpunit/tests/ajax/ReplytoComment.php @@ -35,15 +35,6 @@ class Tests_Ajax_ReplytoComment extends WP_Ajax_UnitTestCase { self::$draft_post = $factory->post->create_and_get( array( 'post_status' => 'draft' ) ); } - public static function wpTearDownAfterClass() { - foreach ( self::$comment_ids as $comment_id ) { - wp_delete_comment( $comment_id, true ); - } - - wp_delete_post( self::$comment_post->ID, true ); - wp_delete_post( self::$draft_post->ID, true ); - } - public function tearDown() { remove_filter( 'query', array( $this, '_block_comments' ) ); parent::tearDown(); diff --git a/tests/phpunit/tests/ajax/TagSearch.php b/tests/phpunit/tests/ajax/TagSearch.php index 75a3af8c27..c8a621116a 100644 --- a/tests/phpunit/tests/ajax/TagSearch.php +++ b/tests/phpunit/tests/ajax/TagSearch.php @@ -31,12 +31,6 @@ class Tests_Ajax_TagSearch extends WP_Ajax_UnitTestCase { } } - public static function wpTearDownAfterClass() { - foreach ( self::$term_ids as $t ) { - wp_delete_term( $t, 'post_tag' ); - } - } - /** * Test as an admin */ diff --git a/tests/phpunit/tests/auth.php b/tests/phpunit/tests/auth.php index ea696a6967..94d77388f1 100644 --- a/tests/phpunit/tests/auth.php +++ b/tests/phpunit/tests/auth.php @@ -26,10 +26,6 @@ class Tests_Auth extends WP_UnitTestCase { self::$wp_hasher = new PasswordHash( 8, true ); } - public static function wpTearDownAfterClass() { - self::delete_user( self::$user_id ); - } - function setUp() { parent::setUp(); diff --git a/tests/phpunit/tests/canonical.php b/tests/phpunit/tests/canonical.php index 112022ad3c..29cc98859d 100644 --- a/tests/phpunit/tests/canonical.php +++ b/tests/phpunit/tests/canonical.php @@ -9,13 +9,6 @@ * @group query */ class Tests_Canonical extends WP_Canonical_UnitTestCase { - public static function wpSetUpBeforeClass( $factory ) { - self::generate_shared_fixtures( $factory ); - } - - public static function wpTearDownAfterClass() { - self::delete_shared_fixtures(); - } public function setUp() { parent::setUp(); diff --git a/tests/phpunit/tests/canonical/category.php b/tests/phpunit/tests/canonical/category.php index 36a5adfb58..daaca74467 100644 --- a/tests/phpunit/tests/canonical/category.php +++ b/tests/phpunit/tests/canonical/category.php @@ -22,16 +22,6 @@ class Tests_Canonical_Category extends WP_Canonical_UnitTestCase { wp_set_post_categories( self::$posts[1], self::$cats[1] ); } - public static function wpTearDownAfterClass() { - foreach ( self::$posts as $post_id ) { - wp_delete_post( $post_id, true ); - } - - foreach ( self::$cats as $cat ) { - wp_delete_term( $cat, 'category' ); - } - } - /** * @dataProvider data_canonical_category */ diff --git a/tests/phpunit/tests/canonical/noRewrite.php b/tests/phpunit/tests/canonical/noRewrite.php index d869717b6b..d3b0d63949 100644 --- a/tests/phpunit/tests/canonical/noRewrite.php +++ b/tests/phpunit/tests/canonical/noRewrite.php @@ -10,13 +10,6 @@ require_once dirname( dirname( __FILE__ ) ) . '/canonical.php'; class Tests_Canonical_NoRewrite extends WP_Canonical_UnitTestCase { // These test cases are run against the test handler in WP_Canonical - public static function wpSetUpBeforeClass( $factory ) { - self::generate_shared_fixtures( $factory ); - } - - public static function wpTearDownAfterClass() { - self::delete_shared_fixtures(); - } public function setUp() { global $wp_rewrite; diff --git a/tests/phpunit/tests/canonical/pageOnFront.php b/tests/phpunit/tests/canonical/pageOnFront.php index 3b04ca8f22..15b7abcfa5 100644 --- a/tests/phpunit/tests/canonical/pageOnFront.php +++ b/tests/phpunit/tests/canonical/pageOnFront.php @@ -6,13 +6,6 @@ * @group query */ class Tests_Canonical_PageOnFront extends WP_Canonical_UnitTestCase { - public static function wpSetUpBeforeClass( $factory ) { - self::generate_shared_fixtures( $factory ); - } - - public static function wpTearDownAfterClass() { - self::delete_shared_fixtures(); - } function setUp() { parent::setUp(); diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index c2fc51b9d8..e3c8081d4e 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -25,12 +25,6 @@ class Tests_Comment extends WP_UnitTestCase { ) ); } - public static function wpTearDownAfterClass() { - wp_delete_post( self::$post_id, true ); - - self::delete_user( self::$user_id ); - } - function test_wp_update_comment() { $post = self::factory()->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post' ) ); $post2 = self::factory()->post->create_and_get( array( 'post_title' => 'some-post-2', 'post_type' => 'post' ) ); diff --git a/tests/phpunit/tests/comment/getCommentAuthorEmailLink.php b/tests/phpunit/tests/comment/getCommentAuthorEmailLink.php index 82a73079ab..aa80dcc837 100644 --- a/tests/phpunit/tests/comment/getCommentAuthorEmailLink.php +++ b/tests/phpunit/tests/comment/getCommentAuthorEmailLink.php @@ -28,10 +28,6 @@ class Tests_Comment_GetCommentAuthorEmailLink extends WP_UnitTestCase { ) ); } - public static function wpTearDownAfterClass() { - wp_delete_comment( self::$comment->comment_ID, true ); - } - public function test_global_comment_with_default_parameters() { $expected = 'foo@example.org'; diff --git a/tests/phpunit/tests/comment/getCommentAuthorUrlLink.php b/tests/phpunit/tests/comment/getCommentAuthorUrlLink.php index dd59edca15..96ead72496 100644 --- a/tests/phpunit/tests/comment/getCommentAuthorUrlLink.php +++ b/tests/phpunit/tests/comment/getCommentAuthorUrlLink.php @@ -13,12 +13,6 @@ class Tests_Comment_GetCommentAuthorUrlLink extends WP_UnitTestCase { self::$comments = array_map( 'get_comment', $comment_ids ); } - public static function wpTearDownAfterClass() { - foreach ( self::$comments as $comment ) { - wp_delete_comment( $comment, true ); - } - } - protected function parseCommentAuthorUrl( $comment, $linktext = '' ) { if ( empty( $linktext ) ) { $linktext = rtrim( preg_replace( '#http://(www\.)?#', '', $comment->comment_author_url ), '/' ); diff --git a/tests/phpunit/tests/comment/getCommentLink.php b/tests/phpunit/tests/comment/getCommentLink.php index fac5fa0d7f..b9e32e2884 100644 --- a/tests/phpunit/tests/comment/getCommentLink.php +++ b/tests/phpunit/tests/comment/getCommentLink.php @@ -44,14 +44,6 @@ class Tests_Comment_GetCommentLink extends WP_UnitTestCase { } - public static function wpTearDownAfterClass() { - foreach ( self::$comments as $c ) { - wp_delete_comment( $c, true ); - } - - wp_delete_post( self::$p, true ); - } - /** * @ticket 34068 */ diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 219e2d1933..1ab4f7f668 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -13,10 +13,6 @@ class Tests_Comment_Query extends WP_UnitTestCase { self::$post_id = $factory->post->create(); } - public static function wpTearDownAfterClass() { - wp_delete_post( self::$post_id, true ); - } - function setUp() { parent::setUp(); } diff --git a/tests/phpunit/tests/comment/wpComment.php b/tests/phpunit/tests/comment/wpComment.php index 5769fa3ee7..5e3566d346 100644 --- a/tests/phpunit/tests/comment/wpComment.php +++ b/tests/phpunit/tests/comment/wpComment.php @@ -22,11 +22,6 @@ class Tests_Term_WpComment extends WP_UnitTestCase { self::$comment_id = self::factory()->comment->create(); } - public static function wpTearDownAfterClass() { - wp_delete_comment( 1, true ); - wp_delete_comment( self::$comment_id, true ); - } - /** * @ticket 37738 */ diff --git a/tests/phpunit/tests/customize/section.php b/tests/phpunit/tests/customize/section.php index 1192de96f2..6369174664 100644 --- a/tests/phpunit/tests/customize/section.php +++ b/tests/phpunit/tests/customize/section.php @@ -13,12 +13,6 @@ class Tests_WP_Customize_Section extends WP_UnitTestCase { self::$user_ids[] = self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); } - public static function wpTearDownAfterClass() { - foreach ( self::$user_ids as $id ) { - self::delete_user( $id ); - } - } - /** * @var WP_Customize_Manager */ diff --git a/tests/phpunit/tests/feed/atom.php b/tests/phpunit/tests/feed/atom.php index 9e9bed11a5..74fdf7a147 100644 --- a/tests/phpunit/tests/feed/atom.php +++ b/tests/phpunit/tests/feed/atom.php @@ -44,22 +44,6 @@ class Tests_Feeds_Atom extends WP_UnitTestCase { } - /** - * Destroy the user we created and related posts. - */ - public static function wpTearDownAfterClass() { - // Delete our user - self::delete_user( self::$user_id ); - - // Delete all of our posts - foreach ( self::$posts as $post ) { - wp_delete_post( $post, true ); - } - - // Delete our taxonomy - wp_delete_category( self::$category->term_id ); - } - /** * Setup. */ diff --git a/tests/phpunit/tests/feed/rss2.php b/tests/phpunit/tests/feed/rss2.php index edd42a543b..45b607d176 100644 --- a/tests/phpunit/tests/feed/rss2.php +++ b/tests/phpunit/tests/feed/rss2.php @@ -43,22 +43,6 @@ class Tests_Feeds_RSS2 extends WP_UnitTestCase { } } - /** - * Destroy the user we created and related posts. - */ - public static function wpTearDownAfterClass() { - // Delete our user - self::delete_user( self::$user_id ); - - // Delete all of our posts - foreach ( self::$posts as $post ) { - wp_delete_post( $post, true ); - } - - // Delete our taxonomy - wp_delete_category( self::$category->term_id ); - } - /** * Setup. */ diff --git a/tests/phpunit/tests/functions/getArchives.php b/tests/phpunit/tests/functions/getArchives.php index 35c01da7da..c90ab979ed 100644 --- a/tests/phpunit/tests/functions/getArchives.php +++ b/tests/phpunit/tests/functions/getArchives.php @@ -23,12 +23,6 @@ class Tests_Get_Archives extends WP_UnitTestCase { self::$post_ids = $factory->post->create_many( 8, array( 'post_type' => 'post', 'post_author' => '1' ) ); } - public static function wpTearDownAfterClass() { - foreach ( self::$post_ids as $post_id ) { - wp_delete_post( $post_id, true ); - } - } - function test_wp_get_archives_default() { $expected['default'] = "