Skip to content
Open
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
80 changes: 80 additions & 0 deletions Tests/Integration/inc/functions/imagifyGetMimeTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Imagify\Tests\Integration\Functions;

use Imagify\Tests\Integration\TestCase;

class Test_ImagifyGetMimeTypes extends TestCase {
protected $useApi = false;

public function testShouldReturnAllMimeTypesByDefault() {
$this->assertSame(
[
'jpg|jpeg|jpe' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
'pdf' => 'application/pdf',
],
imagify_get_mime_types()
);
}

public function testShouldReturnImageMimeTypesOnly() {
$this->assertSame(
[
'jpg|jpeg|jpe' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
],
imagify_get_mime_types( 'image' )
);
}

public function testShouldReturnNotImageMimeTypesOnly() {
$this->assertSame(
[ 'pdf' => 'application/pdf' ],
imagify_get_mime_types( 'not-image' )
);
}

public function testShouldApplyMimeTypesFilter() {
$callback = function ( $mimes ) {
$mimes['avif'] = 'image/avif';
unset( $mimes['pdf'] );

return $mimes;
};

add_filter( 'imagify_get_mime_types', $callback );

$mimes = imagify_get_mime_types();

remove_filter( 'imagify_get_mime_types', $callback );

$this->assertSame( 'image/avif', $mimes['avif'] );
$this->assertArrayNotHasKey( 'pdf', $mimes );
}

public function testShouldDiscardMalformedFilteredMimeTypes() {
$callback = function ( $mimes ) {
$mimes['bad-bool'] = true;
$mimes['bad-array'] = [ 'image/png' ];
$mimes['not-mime'] = 'image';

return $mimes;
};

add_filter( 'imagify_get_mime_types', $callback );

$mimes = imagify_get_mime_types();

remove_filter( 'imagify_get_mime_types', $callback );

$this->assertArrayNotHasKey( 'bad-bool', $mimes );
$this->assertArrayNotHasKey( 'bad-array', $mimes );
$this->assertArrayNotHasKey( 'not-mime', $mimes );
$this->assertSame( 'image/jpeg', $mimes['jpg|jpeg|jpe'] );
}
}
80 changes: 80 additions & 0 deletions Tests/Unit/inc/functions/imagifyGetMimeTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Imagify\Tests\Unit\Functions;

use Brain\Monkey\Filters;
use Imagify\Tests\Unit\TestCase;

class Test_ImagifyGetMimeTypes extends TestCase {
/**
* Test should return image mime types and PDF when no type is given.
*/
public function testShouldReturnAllMimeTypesByDefault() {
$this->assertSame(
[
'jpg|jpeg|jpe' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
'pdf' => 'application/pdf',
],
imagify_get_mime_types()
);
}

/**
* Test should return only image mime types for the image type.
*/
public function testShouldReturnImageMimeTypesOnly() {
$this->assertSame(
[
'jpg|jpeg|jpe' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
],
imagify_get_mime_types( 'image' )
);
}

/**
* Test should return only PDF for the not-image type.
*/
public function testShouldReturnNotImageMimeTypesOnly() {
$this->assertSame(
[ 'pdf' => 'application/pdf' ],
imagify_get_mime_types( 'not-image' )
);
}

/**
* Test should pass the mime types through the imagify_get_mime_types filter.
*/
public function testShouldApplyMimeTypesFilter() {
Filters\expectApplied( 'imagify_get_mime_types' )
->once()
->andReturn( [ 'avif' => 'image/avif' ] );

$this->assertSame( [ 'avif' => 'image/avif' ], imagify_get_mime_types() );
}

/**
* Test should discard malformed entries returned by the filter.
*/
public function testShouldDiscardMalformedFilteredMimeTypes() {
Filters\expectApplied( 'imagify_get_mime_types' )
->once()
->andReturn(
[
'avif' => 'image/avif',
'bad-bool' => true,
'bad-array' => [ 'image/png' ],
'not-mime' => 'image',
0 => 'image/gif',
'' => 'image/jpeg',
]
);

$this->assertSame( [ 'avif' => 'image/avif' ], imagify_get_mime_types() );
}
}
18 changes: 17 additions & 1 deletion inc/functions/attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,23 @@ function imagify_get_mime_types( $type = null ) {
$mimes['pdf'] = 'application/pdf';
}

return $mimes;
/**
* Filter the mime types which could be optimized by Imagify.
*
* @since 2.3.1
*
* @param array $mimes The mime types, as extension => mime type pairs.
*/
$mimes = wpm_apply_filters_typed( 'array', 'imagify_get_mime_types', $mimes );

// Keep only well-formed extension => mime type pairs.
return array_filter(
$mimes,
function ( $mime, $ext ) {
return is_string( $ext ) && '' !== $ext && is_string( $mime ) && preg_match( '@^[a-z-]+/[a-z0-9.+-]+$@', $mime );
},
ARRAY_FILTER_USE_BOTH
);
}

/**
Expand Down
Loading