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 zlib 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 bf0a4a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions inc/class-cachify-hdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public static function is_available() {
* @since 2.4.0
*/
public static function is_gzip_enabled() {
if ( ! function_exists( 'gzencode' ) ) {
// GZip is not available on the system.
return false;
}

/**
* Filter that allows to enable/disable gzip file creation
*
Expand Down
11 changes: 10 additions & 1 deletion tests/test-cachify-hdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ 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' );
if ( ! function_exists( 'gzencode' ) ) {
self::assertFalse( Cachify_HDD::is_gzip_enabled(), 'GZip should be disabled, if not available' );

// Define gzencode function for testing the hook.
function gzencode( $data, $level = -1 ) {
return $data;
}
}

self::assertTrue( Cachify_HDD::is_gzip_enabled(), 'GZip should be enabled if available' );

$capture = null;
add_filter(
Expand Down

0 comments on commit bf0a4a3

Please sign in to comment.