Skip to content

Commit

Permalink
fix: test if gzip extension is available (#308)
Browse files Browse the repository at this point in the history
If "gzip" PHP extension is not loaded and there is no substitute in
place, gzencode() does not exist and generation of compressed files will
fail.
Add a simple function_exists() test to prevent this.
  • Loading branch information
stklcode committed Oct 6, 2024
1 parent 7a84373 commit a424ae4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
7 changes: 5 additions & 2 deletions inc/class-cachify-hdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ public static function is_gzip_enabled() {
/**
* Filter that allows to enable/disable gzip file creation
*
* @param bool $create_gzip_files Whether to create gzip files. Default is `true`
* @param bool $create_gzip_files Whether to create gzip files. Default is `true`, if gzip is available.
*/
return apply_filters( 'cachify_create_gzip_files', true );
return apply_filters(
'cachify_create_gzip_files',
function_exists( 'gzencode' )
);
}

/**
Expand Down
15 changes: 10 additions & 5 deletions tests/test-cachify-hdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,24 @@ public function test_stringify_method() {
* Test GZip availability.
*/
public function test_is_gzip_enabled() {
self::assertTrue( Cachify_HDD::is_gzip_enabled(), 'GZip should be enabled by default' );
$gzip_available = function_exists( 'gzencode' );
self::assertSame(
$gzip_available,
Cachify_HDD::is_gzip_enabled(),
'GZip should be ' . ( $gzip_available ? 'enabled' : 'disabled' ) . ' by default'
);

$capture = null;
add_filter(
'cachify_create_gzip_files',
function ( $original ) use ( &$capture ) {
function ( $original ) use ( &$capture, $gzip_available ) {
$capture = $original;

return false;
return ! $gzip_available;
}
);
self::assertFalse( Cachify_HDD::is_gzip_enabled(), 'GZip should be disabled by filter' );
self::assertTrue( $capture, 'Filter was not applied' );
self::assertSame( ! $gzip_available, Cachify_HDD::is_gzip_enabled(), 'GZip should be changed by filter' );
self::assertSame( $gzip_available, $capture, 'Filter was not applied' );
}

/**
Expand Down

0 comments on commit a424ae4

Please sign in to comment.