Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion tests/unit/ElpUploadHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand Down Expand Up @@ -170,6 +189,7 @@ private function create_extraction_folder() {

wp_mkdir_p( $path );
file_put_contents( $path . 'index.html', '<!doctype html>' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
$this->cleanup_paths[] = $path;

return array( $hash, $path );
}
Expand All @@ -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 );

Expand Down
23 changes: 17 additions & 6 deletions tests/unit/RestApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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();
}

Expand Down Expand Up @@ -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', '<html></html>' );
$this->cleanup_paths[] = $old_folder;
$this->cleanup_paths[] = $file_path;

update_post_meta( $attachment_id, '_exelearning_extracted', $old_hash );

Expand All @@ -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'] );
}

/**
Expand Down Expand Up @@ -1842,6 +1852,7 @@ public function test_cleanup_old_extraction_with_folder() {

wp_mkdir_p( $folder );
file_put_contents( $folder . 'test.html', '<html></html>' );
$this->cleanup_paths[] = $folder;

update_post_meta( $attachment_id, '_exelearning_extracted', $hash );

Expand Down
20 changes: 20 additions & 0 deletions tests/unit/ShortcodesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand Down
Loading