Skip to content

Commit

Permalink
Fix RemoveUploaded within WP_XMLRPC_UnitTestCase
Browse files Browse the repository at this point in the history
Because the parent set_up was a trait, it was being bypassed, so the
uploads were not scanned and everything was being deleted.
  • Loading branch information
lipemat committed Jul 27, 2024
1 parent eccae02 commit c964ae4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
20 changes: 13 additions & 7 deletions includes/src/Traits/RemoveUploaded.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
*
*/
trait RemoveUploaded {
protected static $ignore_files = [];
/**
* @var string[]|null
*/
protected static ?array $ignore_files = null;


public function set_up() {
parent::set_up();
if ( [] === static::$ignore_files ) {
if ( null === static::$ignore_files ) {
// Only scan the directory once per test run.
static::$ignore_files = $this->scan_user_uploads();
}
Expand All @@ -38,13 +41,17 @@ public function tear_down(): void {
*
* This method works in tandem with the `set_up()` and `rmdir()` methods:
* - `set_up()` scans the `uploads` directory before every test, and stores
* its contents inside of the `$ignore_files` property.
* its contents inside the `$ignore_files` property.
* - `rmdir()` and its helper methods only delete files that are not listed
* in the `$ignore_files` property. If called during `tear_down()` in tests,
* this will only delete files added during the previously run test.
*/
public function remove_added_uploads(): void {
$uploads = wp_upload_dir();
if ( null === static::$ignore_files ) {
static::$ignore_files = $this->scan_user_uploads();
}

Files::instance()->rmdir( $uploads['basedir'], static::$ignore_files );
}

Expand All @@ -54,16 +61,15 @@ public function remove_added_uploads(): void {
*
* @since 4.0.0
*
* @return array List of file paths.
* @return string[] List of file paths.
*/
public function scan_user_uploads(): array {
static $files = [];
if ( ! empty( $files ) ) {
if ( [] !== $files ) {
return $files;
}

$uploads = wp_upload_dir();
$files = Files::instance()->files_in_dir( $uploads['basedir'] );
return $files;
return Files::instance()->files_in_dir( $uploads['basedir'] );
}
}
6 changes: 4 additions & 2 deletions includes/testcase-xmlrpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
require_once ABSPATH . WPINC . '/class-wp-xmlrpc-server.php';

abstract class WP_XMLRPC_UnitTestCase extends WP_UnitTestCase {
use RemoveUploaded;
use RemoveUploaded {
set_up as protected remove_uploaded_set_up;
}

/**
* @var wp_xmlrpc_server
*/
protected $myxmlrpcserver;

public function set_up() {
parent::set_up();
$this->remove_uploaded_set_up();

add_filter( 'pre_option_enable_xmlrpc', '__return_true' );

Expand Down

0 comments on commit c964ae4

Please sign in to comment.