@@ -233,4 +233,95 @@ public function test_cleanup_command_handles_invalid_sleep_parameter() {
233233 // Should still complete successfully (invalid sleep defaults to 0).
234234 $ this ->assertStringContainsString ( 'Success: No orphaned issues found. ' , $ output );
235235 }
236+
237+ /**
238+ * Test the cleanup command only removes orphaned issues and preserves valid issues.
239+ */
240+ public function test_cleanup_command_preserves_valid_issues_and_removes_orphaned_issues () {
241+ // Set up scannable post types to include 'post' so valid posts aren't considered orphaned.
242+ update_option ( 'edac_post_types ' , [ 'post ' , 'page ' ] );
243+
244+ global $ wpdb ;
245+ $ table_name = $ wpdb ->prefix . 'accessibility_checker ' ;
246+
247+ // Create a real post.
248+ $ real_post_id = wp_insert_post ( [
249+ 'post_title ' => 'Test Post ' ,
250+ 'post_content ' => 'Test content ' ,
251+ 'post_status ' => 'publish ' ,
252+ 'post_type ' => 'post ' ,
253+ ] );
254+
255+ // Create an issue for the real post (should NOT be deleted).
256+ $ wpdb ->insert ( // phpcs:ignore WordPress.DB -- using direct query for testing.
257+ $ table_name ,
258+ [
259+ 'postid ' => $ real_post_id ,
260+ 'siteid ' => get_current_blog_id (),
261+ 'type ' => 'post ' ,
262+ 'rule ' => 'empty_paragraph_tag ' ,
263+ 'ruletype ' => 'warning ' ,
264+ 'object ' => '<p></p> ' ,
265+ 'recordcheck ' => 1 ,
266+ 'user ' => get_current_user_id (),
267+ 'ignre ' => 0 ,
268+ 'ignre_user ' => null ,
269+ 'ignre_date ' => null ,
270+ 'ignre_comment ' => null ,
271+ 'ignre_global ' => 0 ,
272+ ]
273+ );
274+
275+ // Create an issue for a non-existent post ID (should be deleted).
276+ $ orphaned_post_id = 999999 ;
277+ $ wpdb ->insert ( // phpcs:ignore WordPress.DB -- using direct query for testing.
278+ $ table_name ,
279+ [
280+ 'postid ' => $ orphaned_post_id ,
281+ 'siteid ' => get_current_blog_id (),
282+ 'type ' => 'post ' ,
283+ 'rule ' => 'empty_paragraph_tag ' ,
284+ 'ruletype ' => 'warning ' ,
285+ 'object ' => '<p></p> ' ,
286+ 'recordcheck ' => 1 ,
287+ 'user ' => get_current_user_id (),
288+ 'ignre ' => 0 ,
289+ 'ignre_user ' => null ,
290+ 'ignre_date ' => null ,
291+ 'ignre_comment ' => null ,
292+ 'ignre_global ' => 0 ,
293+ ]
294+ );
295+
296+ // Verify the table has 2 items to start.
297+ $ total_issues_before = $ wpdb ->get_var ( "SELECT COUNT(*) FROM $ table_name " ); // phpcs:ignore WordPress.DB -- Querying for testing purposes.
298+ $ this ->assertEquals ( 2 , (int ) $ total_issues_before );
299+
300+ // Run the cleanup command.
301+ ob_start ();
302+ $ this ->cleanup_orphaned_issues ->__invoke ( [], [] );
303+ $ output = ob_get_clean ();
304+
305+ // Verify that it finds one orphaned issue.
306+ $ this ->assertStringContainsString ( 'Log: Found 1 orphaned post IDs ' , $ output );
307+ $ this ->assertStringContainsString ( "Log: - Deleting issues for post ID: $ orphaned_post_id " , $ output );
308+ $ this ->assertStringContainsString ( 'Success: Orphaned issues cleanup complete. 1 post(s) processed. ' , $ output );
309+
310+ // Verify that there is still 1 item left in the table (the valid one).
311+ $ total_issues_after = $ wpdb ->get_var ( "SELECT COUNT(*) FROM $ table_name " ); // phpcs:ignore WordPress.DB -- Querying for testing purposes.
312+ $ this ->assertEquals ( 1 , (int ) $ total_issues_after );
313+
314+ // Verify the remaining issue is for the real post.
315+ $ remaining_issue = $ wpdb ->get_row ( "SELECT * FROM $ table_name WHERE postid = $ real_post_id " ); // phpcs:ignore WordPress.DB -- Querying for testing purposes.
316+ $ this ->assertNotNull ( $ remaining_issue );
317+ $ this ->assertEquals ( $ real_post_id , (int ) $ remaining_issue ->postid );
318+
319+ // Verify the orphaned issue was deleted.
320+ $ orphaned_issue = $ wpdb ->get_row ( "SELECT * FROM $ table_name WHERE postid = $ orphaned_post_id " ); // phpcs:ignore WordPress.DB -- Querying for testing purposes.
321+ $ this ->assertNull ( $ orphaned_issue );
322+
323+ // Clean up the test post and option.
324+ wp_delete_post ( $ real_post_id , true );
325+ delete_option ( 'edac_post_types ' );
326+ }
236327}
0 commit comments