diff --git a/tests/unit/ElpUploadHandlerTest.php b/tests/unit/ElpUploadHandlerTest.php index cc78ec8..36e546c 100644 --- a/tests/unit/ElpUploadHandlerTest.php +++ b/tests/unit/ElpUploadHandlerTest.php @@ -19,6 +19,14 @@ class ElpUploadHandlerTest extends WP_UnitTestCase { */ private $handler; + /** + * Paths removed in tear_down(), so a failing test cannot leak them into the + * shared uploads directory of the next run. + * + * @var string[] + */ + private $cleanup_paths = array(); + /** * Set up test fixtures. */ @@ -27,6 +35,17 @@ public function set_up() { $this->handler = new ExeLearning_Elp_Upload_Handler(); } + /** + * Tear down test fixtures. + */ + public function tear_down() { + foreach ( $this->cleanup_paths as $path ) { + ExeLearning_Styles_Service::recursive_delete( $path ); + } + $this->cleanup_paths = array(); + parent::tear_down(); + } + /** * Test process_elp_upload ignores non-elpx files. */ @@ -170,6 +189,7 @@ private function create_extraction_folder() { wp_mkdir_p( $path ); file_put_contents( $path . 'index.html', '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents + $this->cleanup_paths[] = $path; return array( $hash, $path ); } @@ -196,7 +216,7 @@ public function test_delete_extracted_folder_no_metadata() { public function test_delete_extracted_folder_nonexistent_dir() { list( , $bystander ) = $this->create_extraction_folder(); $attachment_id = $this->factory->attachment->create(); - $missing_hash = str_repeat( 'f', 40 ); + $missing_hash = sha1( uniqid( 'exe-missing-', true ) ); update_post_meta( $attachment_id, '_exelearning_extracted', $missing_hash ); diff --git a/tests/unit/RestApiTest.php b/tests/unit/RestApiTest.php index 61afa14..2cd3225 100644 --- a/tests/unit/RestApiTest.php +++ b/tests/unit/RestApiTest.php @@ -19,6 +19,14 @@ class RestApiTest extends WP_UnitTestCase { */ private $rest_api; + /** + * Paths removed in tear_down(), so a failing test cannot leak them into the + * shared uploads directory of the next run. + * + * @var string[] + */ + private $cleanup_paths = array(); + /** * Set up test fixtures. */ @@ -38,6 +46,10 @@ public function set_up() { public function tear_down() { global $wp_rest_server; $wp_rest_server = null; + foreach ( $this->cleanup_paths as $path ) { + ExeLearning_Styles_Service::recursive_delete( $path ); + } + $this->cleanup_paths = array(); parent::tear_down(); } @@ -1361,6 +1373,8 @@ public function test_save_elp_file_cleanup_with_old_extraction() { $old_folder = $upload_dir['basedir'] . '/exelearning/' . $old_hash . '/'; wp_mkdir_p( $old_folder ); file_put_contents( $old_folder . 'index.html', '' ); + $this->cleanup_paths[] = $old_folder; + $this->cleanup_paths[] = $file_path; update_post_meta( $attachment_id, '_exelearning_extracted', $old_hash ); @@ -1380,14 +1394,10 @@ public function test_save_elp_file_cleanup_with_old_extraction() { $result = $this->rest_api->save_elp_file( $request ); + unset( $_FILES['file'] ); + // Reprocessing fails (invalid ZIP), so old folder should be preserved. $this->assertTrue( is_dir( $old_folder ) ); - - // Clean up test directory. - unlink( $old_folder . 'index.html' ); - rmdir( $old_folder ); - unlink( $file_path ); - unset( $_FILES['file'] ); } /** @@ -1842,6 +1852,7 @@ public function test_cleanup_old_extraction_with_folder() { wp_mkdir_p( $folder ); file_put_contents( $folder . 'test.html', '' ); + $this->cleanup_paths[] = $folder; update_post_meta( $attachment_id, '_exelearning_extracted', $hash ); diff --git a/tests/unit/ShortcodesTest.php b/tests/unit/ShortcodesTest.php index bb9353d..41cb8b4 100644 --- a/tests/unit/ShortcodesTest.php +++ b/tests/unit/ShortcodesTest.php @@ -19,6 +19,14 @@ class ShortcodesTest extends WP_UnitTestCase { */ private $shortcodes; + /** + * Paths removed in tear_down(), so a failing test cannot leak them into the + * shared uploads directory of the next run. + * + * @var string[] + */ + private $cleanup_paths = array(); + /** * Set up test fixtures. */ @@ -27,6 +35,17 @@ public function set_up() { $this->shortcodes = new ExeLearning_Shortcodes(); } + /** + * Tear down test fixtures. + */ + public function tear_down() { + foreach ( $this->cleanup_paths as $path ) { + ExeLearning_Styles_Service::recursive_delete( $path ); + } + $this->cleanup_paths = array(); + parent::tear_down(); + } + /** * Test shortcode is registered. */ @@ -317,6 +336,7 @@ private function create_previewable_attachment( $hash, $with_screenshot = false $dir = trailingslashit( $upload_dir['basedir'] ) . 'exelearning/' . $hash; wp_mkdir_p( $dir ); file_put_contents( $dir . '/screenshot.png', 'PNG' ); // phpcs:ignore + $this->cleanup_paths[] = $dir; } return $attachment_id;