From 54a09a7ec99c59b8e640bd5aacebfdbf03bb02cc Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Fri, 22 Dec 2023 15:20:34 +0000 Subject: [PATCH] Build/Test: Add Tests for `wp_scheduled_delete`. Props pbearne. Fixes #59938. git-svn-id: https://develop.svn.wordpress.org/trunk@57224 602fd350-edb4-49c9-b593-d223f7449a82 --- .../tests/functions/wpScheduledDelete.php | 173 ++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 tests/phpunit/tests/functions/wpScheduledDelete.php diff --git a/tests/phpunit/tests/functions/wpScheduledDelete.php b/tests/phpunit/tests/functions/wpScheduledDelete.php new file mode 100644 index 0000000000..38044d44a1 --- /dev/null +++ b/tests/phpunit/tests/functions/wpScheduledDelete.php @@ -0,0 +1,173 @@ +post->create( + array( + 'post_type' => 'page', + 'post_status' => 'trash', + ) + ); + add_post_meta( self::$page_id, '_wp_trash_meta_time', time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS + 1 ) ); + add_post_meta( self::$page_id, '_wp_trash_meta_status', 'published' ); + + $this->assertNotEmpty( get_post( self::$page_id ) ); + + wp_scheduled_delete(); + + $this->assertEmpty( get_post( self::$page_id ) ); + } + + /** + * Don't delete old trashed post/pages if status not trash. + * Remove the trash meta status. + * + * @ticket 59938 + * + */ + public function test_wp_scheduled_delete_not_trash() { + self::$page_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'published', + ) + ); + add_post_meta( self::$page_id, '_wp_trash_meta_time', time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS + 1 ) ); + add_post_meta( self::$page_id, '_wp_trash_meta_status', 'published' ); + + $this->assertNotEmpty( get_post( self::$page_id ) ); + + wp_scheduled_delete(); + + $this->assertNotEmpty( get_post( self::$page_id ) ); + $this->assertEmpty( get_post_meta( self::$page_id, '_wp_trash_meta_time', true ) ); + $this->assertEmpty( get_post_meta( self::$page_id, '_wp_trash_meta_status', true ) ); + } + + + /** + * Don't delete old trashed post/pages if old enough. + * + * @ticket 59938 + * + */ + public function test_wp_scheduled_delete_not_old() { + self::$page_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'trash', + ) + ); + add_post_meta( self::$page_id, '_wp_trash_meta_time', time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ) ); + add_post_meta( self::$page_id, '_wp_trash_meta_status', 'published' ); + + $this->assertNotEmpty( get_post( self::$page_id ) ); + + wp_scheduled_delete(); + + $this->assertNotEmpty( get_post( self::$page_id ) ); + $this->assertNotEmpty( get_post_meta( self::$page_id, '_wp_trash_meta_time', true ) ); + $this->assertNotEmpty( get_post_meta( self::$page_id, '_wp_trash_meta_status', true ) ); + } + + /** + * Delete old trashed comments. + * + * @ticket 59938 + * + */ + public function test_wp_scheduled_delete_comment() { + self::$comment_id = self::factory()->comment->create( + array( + 'comment_approved' => 'trash', + ) + ); + add_comment_meta( self::$comment_id, '_wp_trash_meta_time', time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS + 1 ) ); + add_post_meta( self::$comment_id, '_wp_trash_meta_status', 'published' ); + + $this->assertNotEmpty( get_comment( self::$comment_id ) ); + + wp_scheduled_delete(); + + $this->assertEmpty( get_comment( self::$comment_id ) ); + } + + /** + * Don't delete old trashed comments if status not trash. + * Remove the trash meta status. + * + * @ticket 59938 + * + */ + public function test_wp_scheduled_delete_not_trash_comment() { + self::$comment_id = self::factory()->comment->create( + array( + 'comment_approved' => '1', + ) + ); + add_comment_meta( self::$comment_id, '_wp_trash_meta_time', time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS + 1 ) ); + add_comment_meta( self::$comment_id, '_wp_trash_meta_status', 'published' ); + + $this->assertNotEmpty( get_comment( self::$comment_id ) ); + + wp_scheduled_delete(); + + $this->assertNotEmpty( get_comment( self::$comment_id ) ); + $this->assertEmpty( get_comment_meta( self::$comment_id, '_wp_trash_meta_time', true ) ); + $this->assertEmpty( get_comment_meta( self::$comment_id, '_wp_trash_meta_status', true ) ); + } + + + /** + * Don't delete old trashed comments if old enough. + * + * @ticket 59938 + * + */ + public function test_wp_scheduled_delete_not_old_comment() { + self::$comment_id = self::factory()->comment->create( + array( + 'comment_approved' => 'trash', + ) + ); + add_comment_meta( self::$comment_id, '_wp_trash_meta_time', time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ) ); + add_comment_meta( self::$comment_id, '_wp_trash_meta_status', 'published' ); + + $this->assertNotEmpty( get_comment( self::$comment_id ) ); + + wp_scheduled_delete(); + + $this->assertNotEmpty( get_comment( self::$comment_id ) ); + $this->assertNotEmpty( get_comment_meta( self::$comment_id, '_wp_trash_meta_time', true ) ); + $this->assertNotEmpty( get_comment_meta( self::$comment_id, '_wp_trash_meta_status', true ) ); + } +}