Filesystem API: Introduce filters for before/after unzipping archives.

This introduces the following new filters which wrap the process of unzipping an archive:
- `pre_unzip_file` - Filters archive unzipping to allow an override with a custom process.
- `unzip_file` - Filters the result of unzipping an archive.

Both filters pass the following:
- `string $file` - Full path and filename of ZIP archive.
- `string $to` - Full path on the filesystem to extract archive to.
- `string[] $needed_dirs` - A full list of required folders that need to be created.
- `float|false $required_space` - The space required to unzip the file and copy its contents, with a 10% buffer.

Props dfavor, azaozz, oglekler, afragen, costdev.
Fixes #37719.

git-svn-id: https://develop.svn.wordpress.org/trunk@56689 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Colin Stewart
2023-09-25 22:14:11 +00:00
parent 8b63003223
commit 6558db1756
3 changed files with 217 additions and 6 deletions

View File

@@ -0,0 +1,76 @@
<?php
/**
* Tests _unzip_file_pclzip().
*
* @group file.php
*
* @covers ::_unzip_file_pclzip
*/
class Tests_Filesystem_UnzipFilePclzip extends WP_UnitTestCase {
/**
* The test data directory.
*
* @var string $test_data_dir
*/
private static $test_data_dir;
/**
* Sets up the filesystem and test data directory property
* before any tests run.
*/
public static function set_up_before_class() {
parent::set_up_before_class();
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();
self::$test_data_dir = DIR_TESTDATA . '/filesystem/';
}
/**
* Tests that _unzip_file_pclzip() applies "pre_unzip_file" filters.
*
* @ticket 37719
*/
public function test_should_apply_pre_unzip_file_filters() {
$filter = new MockAction();
add_filter( 'pre_unzip_file', array( $filter, 'filter' ) );
// Prepare test environment.
$unzip_destination = self::$test_data_dir . 'archive/';
mkdir( $unzip_destination );
_unzip_file_pclzip( self::$test_data_dir . 'archive.zip', $unzip_destination );
// Cleanup test environment.
$this->rmdir( $unzip_destination );
$this->delete_folders( $unzip_destination );
$this->assertSame( 1, $filter->get_call_count() );
}
/**
* Tests that _unzip_file_pclzip() applies "unzip_file" filters.
*
* @ticket 37719
*/
public function test_should_apply_unzip_file_filters() {
$filter = new MockAction();
add_filter( 'unzip_file', array( $filter, 'filter' ) );
// Prepare test environment.
$unzip_destination = self::$test_data_dir . 'archive/';
mkdir( $unzip_destination );
_unzip_file_pclzip( self::$test_data_dir . 'archive.zip', $unzip_destination );
// Cleanup test environment.
$this->rmdir( $unzip_destination );
$this->delete_folders( $unzip_destination );
$this->assertSame( 1, $filter->get_call_count() );
}
}

View File

@@ -0,0 +1,84 @@
<?php
/**
* Tests _unzip_file_ziparchive().
*
* @group file.php
*
* @covers ::_unzip_file_ziparchive
*/
class Tests_Filesystem_UnzipFileZiparchive extends WP_UnitTestCase {
/**
* The test data directory.
*
* @var string $test_data_dir
*/
private static $test_data_dir;
/**
* Sets up the filesystem and test data directory property
* before any tests run.
*/
public static function set_up_before_class() {
parent::set_up_before_class();
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();
self::$test_data_dir = DIR_TESTDATA . '/filesystem/';
}
/**
* Tests that _unzip_file_ziparchive() applies "pre_unzip_file" filters.
*
* @ticket 37719
*/
public function test_should_apply_pre_unzip_file_filters() {
if ( ! class_exists( 'ZipArchive' ) ) {
$this->markTestSkipped( 'This test requires the ZipArchive class.' );
}
$filter = new MockAction();
add_filter( 'pre_unzip_file', array( $filter, 'filter' ) );
// Prepare test environment.
$unzip_destination = self::$test_data_dir . 'archive/';
mkdir( $unzip_destination );
_unzip_file_ziparchive( self::$test_data_dir . 'archive.zip', $unzip_destination );
// Cleanup test environment.
$this->rmdir( $unzip_destination );
$this->delete_folders( $unzip_destination );
$this->assertSame( 1, $filter->get_call_count() );
}
/**
* Tests that _unzip_file_ziparchive() applies "unzip_file" filters.
*
* @ticket 37719
*/
public function test_should_apply_unzip_file_filters() {
if ( ! class_exists( 'ZipArchive' ) ) {
$this->markTestSkipped( 'This test requires the ZipArchive class.' );
}
$filter = new MockAction();
add_filter( 'unzip_file', array( $filter, 'filter' ) );
// Prepare test environment.
$unzip_destination = self::$test_data_dir . 'archive/';
mkdir( $unzip_destination );
_unzip_file_ziparchive( self::$test_data_dir . 'archive.zip', $unzip_destination );
// Cleanup test environment.
$this->rmdir( $unzip_destination );
$this->delete_folders( $unzip_destination );
$this->assertSame( 1, $filter->get_call_count() );
}
}