This commit is contained in:
2024-05-20 15:37:46 +03:00
commit 00b7dbd0b7
10404 changed files with 3285853 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
<?php
/**
* PHPUnit bootstrap file
*
* @package Ewww_Image_Optimizer
*/
$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
$_tests_dir = '/tmp/wordpress-tests-lib';
}
// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';
/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( dirname( __FILE__ ) ) . '/ewww-image-optimizer.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
if ( ! defined( 'EIO_PHPUNIT' ) ) {
define( 'EIO_PHPUNIT', true );
}
if ( ! empty( $_SERVER['HOME'] ) ) {
if ( is_file( $_SERVER['HOME'] . '/Documents/GitHub/PHPUnit-Polyfills/phpunitpolyfills-autoload.php' ) ) {
require( $_SERVER['HOME'] . '/Documents/GitHub/PHPUnit-Polyfills/phpunitpolyfills-autoload.php' );
}
}
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';

View File

@@ -0,0 +1,108 @@
<?php
/**
* Class EWWWIO_AGR_Tests
*
* @link https://ewww.io
* @package Ewww_Image_Optimizer
*/
/**
* AGR (animated GIF resizing) test cases.
*/
class EWWWIO_AGR_Tests extends WP_UnitTestCase {
/**
* The location of the test GIF image.
*
* @var string $test_gif
*/
public static $test_gif = '';
/**
* Downloads test images.
*/
public static function set_up_before_class() {
self::$test_gif = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/rain.gif' );
ewwwio()->set_defaults();
update_option( 'ewww_image_optimizer_gif_level', 10 );
ewwwio()->local->install_tools();
}
/**
* Initializes the plugin and installs the ewwwio_images table.
*/
function set_up() {
parent::set_up();
remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
ewww_image_optimizer_install_table();
add_filter( 'query', array( $this, '_create_temporary_tables' ) );
}
/**
* Test that GD is active and Imagick is not -- otherwise our tests are bogus.
*/
function test_gd_active() {
$this->assertNotEmpty( \ewwwio()->gd_support() );
$this->assertFalse( \ewwwio()->imagick_support() );
}
/**
* Test local (gifsicle) AGR.
*/
function test_local_agr() {
$upload_gif = self::$test_gif . '.gif';
copy( self::$test_gif, $upload_gif );
$id = $this->factory->attachment->create_upload_object( $upload_gif );
$meta = wp_get_attachment_metadata( $id );
list( $file_path, $upload_path ) = ewww_image_optimizer_attachment_path( $meta, $id );
$thumb_path = trailingslashit( dirname( $file_path ) ) . wp_basename( $meta['sizes']['thumbnail']['file'] );
$this->assertTrue( ewww_image_optimizer_is_animated( $thumb_path ) );
unlink( $upload_gif );
}
/**
* Test API-based AGR.
*/
function test_api_agr() {
$upload_gif = self::$test_gif . '.gif';
copy( self::$test_gif, $upload_gif );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$id = $this->factory->attachment->create_upload_object( $upload_gif );
$meta = wp_get_attachment_metadata( $id );
list( $file_path, $upload_path ) = ewww_image_optimizer_attachment_path( $meta, $id );
$thumb_path = trailingslashit( dirname( $file_path ) ) . wp_basename( $meta['sizes']['thumbnail']['file'] );
$this->assertTrue( ewww_image_optimizer_is_animated( $thumb_path ) );
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
unlink( $upload_gif );
}
/**
* Cleans up ewwwio_images table.
*/
function tear_down() {
global $wpdb;
remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
$wpdb->query( "DROP TABLE IF EXISTS $wpdb->ewwwio_images" );
add_filter( 'query', array( $this, '_drop_temporary_tables' ) );
delete_option( 'ewww_image_optimizer_version' );
delete_option( 'ewww_image_optimizer_cloud_key' );
delete_site_option( 'ewww_image_optimizer_version' );
delete_site_option( 'ewww_image_optimizer_cloud_key' );
parent::tear_down();
}
/**
* Cleans up the temp images.
*/
public static function tear_down_after_class() {
if ( ewwwio_is_file( self::$test_gif ) ) {
unlink( self::$test_gif );
}
ewww_image_optimizer_remove_binaries();
}
}

View File

@@ -0,0 +1,362 @@
<?php
/**
* Class EWWWIO_Convert_Tests
*
* @link https://ewww.io
* @package Ewww_Image_Optimizer
*/
/**
* Conversion test cases.
*/
class EWWWIO_Convert_Tests extends WP_UnitTestCase {
/**
* The location of the test JPG image.
*
* @var string $test_jpg
*/
public static $test_jpg = '';
/**
* The location of the test PNG image.
*
* @var string $test_png
*/
public static $test_png = '';
/**
* The location of the test GIF image.
*
* @var string $test_gif
*/
public static $test_gif = '';
/**
* Downloads test images.
*/
public static function set_up_before_class() {
$wp_upload_dir = wp_upload_dir();
$temp_upload_dir = trailingslashit( $wp_upload_dir['basedir'] ) . 'testing/';
wp_mkdir_p( $temp_upload_dir );
$test_jpg = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/DCClogo.jpg' );
rename( $test_jpg, $temp_upload_dir . wp_basename( $test_jpg ) );
self::$test_jpg = $temp_upload_dir . wp_basename( $test_jpg );
$test_png = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/common-loon.png' );
rename( $test_png, $temp_upload_dir . wp_basename( $test_png ) );
self::$test_png = $temp_upload_dir . wp_basename( $test_png );
$test_gif = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/xhtml11.gif' );
rename( $test_gif, $temp_upload_dir . wp_basename( $test_gif ) );
self::$test_gif = $temp_upload_dir . wp_basename( $test_gif );
ewwwio()->set_defaults();
update_option( 'ewww_image_optimizer_jpg_level', 10 );
update_option( 'ewww_image_optimizer_gif_level', 10 );
update_option( 'ewww_image_optimizer_webp', true );
update_option( 'ewww_image_optimizer_png_level', 40 );
update_site_option( 'ewww_image_optimizer_webp', true );
update_site_option( 'ewww_image_optimizer_png_level', 40 );
ewwwio()->local->install_tools();
ewww_image_optimizer_install_pngout();
ewww_image_optimizer_install_svgcleaner();
update_option( 'ewww_image_optimizer_webp', '' );
update_option( 'ewww_image_optimizer_png_level', 10 );
update_site_option( 'ewww_image_optimizer_webp', '' );
update_site_option( 'ewww_image_optimizer_png_level', 10 );
}
/**
* Initializes the plugin and installs the ewwwio_images table.
*/
function set_up() {
parent::set_up();
remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
ewww_image_optimizer_install_table();
add_filter( 'query', array( $this, '_create_temporary_tables' ) );
}
/**
* Copies the test JPG to a temp file, optimizes it, and returns the results.
*
* @return array The results of the ewww_image_optimizer() function.
*/
protected function optimize_jpg( $original = false ) {
if ( ! $original ) {
$original = self::$test_jpg;
}
global $ewww_force;
$ewww_force = 1;
$filename = $original . ".jpg";
copy( $original, $filename );
$results = ewww_image_optimizer( $filename, 1, false, false, true );
return $results;
}
/**
* Copies the test PNG to a temp file, optimizes it, and returns the results.
*
* @return array The results of the ewww_image_optimizer() function.
*/
protected function optimize_png( $original = false ) {
if ( ! $original ) {
$original = self::$test_png;
}
global $ewww_force;
$ewww_force = 1;
$filename = $original . ".png";
copy( $original, $filename );
$results = ewww_image_optimizer( $filename, 1, false, false, true );
return $results;
}
/**
* Copies the test GIF to a temp file, optimizes it, and returns the results.
*
* @return array The results of the ewww_image_optimizer() function.
*/
protected function optimize_gif( $original = false ) {
if ( ! $original ) {
$original = self::$test_gif;
}
global $ewww_force;
$ewww_force = 1;
$filename = $original . ".gif";
copy( $original, $filename );
$results = ewww_image_optimizer( $filename, 1, false, false, true );
return $results;
}
/**
* Test JPG to PNG conversion.
*/
function test_convert_jpg_to_png() {
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_jpg_level', 10 );
update_option( 'ewww_image_optimizer_png_level', 10 );
update_option( 'ewww_image_optimizer_jpg_to_png', true );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_jpg_level', 10 );
update_site_option( 'ewww_image_optimizer_png_level', 10 );
update_site_option( 'ewww_image_optimizer_jpg_to_png', true );
$results = $this->optimize_jpg();
$this->assertEquals( 'image/png', ewww_image_optimizer_mimetype( $results[0], 'i' ) );
unlink( $results[0] );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_option( 'ewww_image_optimizer_jpg_level', 20 );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_jpg_level', 20 );
$results = $this->optimize_jpg();
$this->assertEquals( 'image/png', ewww_image_optimizer_mimetype( $results[0], 'i' ) );
unlink( $results[0] );
update_option( 'ewww_image_optimizer_jpg_to_png', '' );
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_jpg_to_png', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
}
/**
* Test PNG to JPG conversion.
*/
function test_convert_png_to_jpg() {
update_option( 'ewww_image_optimizer_png_level', 10 );
update_option( 'ewww_image_optimizer_jpg_level', 10 );
update_option( 'ewww_image_optimizer_disable_pngout', true );
update_option( 'ewww_image_optimizer_optipng_level', 2 );
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_png_to_jpg', true );
update_site_option( 'ewww_image_optimizer_png_level', 10 );
update_site_option( 'ewww_image_optimizer_jpg_level', 10 );
update_site_option( 'ewww_image_optimizer_disable_pngout', true );
update_site_option( 'ewww_image_optimizer_optipng_level', 2 );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_png_to_jpg', true );
$results = $this->optimize_png();
update_option( 'ewww_image_optimizer_png_to_jpg', '' );
update_site_option( 'ewww_image_optimizer_png_to_jpg', '' );
$this->assertEquals( 'image/jpeg', ewww_image_optimizer_mimetype( $results[0], 'i' ) );
unlink( $results[0] );
}
/**
* Test PNG to JPG conversion with alpha.
*/
function test_convert_png_to_jpg_alpha() {
update_option( 'ewww_image_optimizer_png_level', 10 );
update_option( 'ewww_image_optimizer_jpg_level', 10 );
update_option( 'ewww_image_optimizer_disable_pngout', true );
update_option( 'ewww_image_optimizer_optipng_level', 2 );
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_png_to_jpg', true );
update_option( 'ewww_image_optimizer_jpg_background', '' );
update_site_option( 'ewww_image_optimizer_png_level', 10 );
update_site_option( 'ewww_image_optimizer_jpg_level', 10 );
update_site_option( 'ewww_image_optimizer_disable_pngout', true );
update_site_option( 'ewww_image_optimizer_optipng_level', 2 );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_png_to_jpg', true );
update_site_option( 'ewww_image_optimizer_jpg_background', '' );
// No background, conversion will fail.
$test_png = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/books.png' );
rename( $test_png, dirname( self::$test_png ) . wp_basename( $test_png ) );
$test_png = dirname( self::$test_png ) . wp_basename( $test_png );
$results = $this->optimize_png( $test_png );
$this->assertEquals( 'image/png', ewww_image_optimizer_mimetype( $results[0], 'i' ) );
unlink( $results[0] );
// Set background, conversion will succeed.
update_option( 'ewww_image_optimizer_jpg_background', 'ffffff' );
update_site_option( 'ewww_image_optimizer_jpg_background', 'ffffff' );
$results = $this->optimize_png( $test_png );
$this->assertEquals( 'image/jpeg', ewww_image_optimizer_mimetype( $results[0], 'i' ) );
unlink( $results[0] );
// No background, conversion will fail, using API.
update_option( 'ewww_image_optimizer_png_level', 20 );
update_option( 'ewww_image_optimizer_jpg_background', '' );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_png_level', 20 );
update_site_option( 'ewww_image_optimizer_jpg_background', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$results = $this->optimize_png( $test_png );
$this->assertEquals( 'image/png', ewww_image_optimizer_mimetype( $results[0], 'i' ) );
unlink( $results[0] );
// Set background, conversion will succeed, using API.
update_option( 'ewww_image_optimizer_jpg_background', 'ffffff' );
update_site_option( 'ewww_image_optimizer_jpg_background', 'ffffff' );
$results = $this->optimize_png( $test_png );
$this->assertEquals( 'image/jpeg', ewww_image_optimizer_mimetype( $results[0], 'i' ) );
unlink( $results[0] );
update_option( 'ewww_image_optimizer_png_to_jpg', '' );
update_option( 'ewww_image_optimizer_jpg_background', '' );
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_png_to_jpg', '' );
update_site_option( 'ewww_image_optimizer_jpg_background', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
unlink( $test_png );
}
/**
* Tests autoconvert by uploading a PNG attachment.
*/
function test_autoconvert_png_local() {
update_option( 'ewww_image_optimizer_png_level', 10 );
update_site_option( 'ewww_image_optimizer_png_level', 10 );
$upload_png = self::$test_png . '.png';
copy( self::$test_png, $upload_png );
$id = $this->factory->attachment->create_upload_object( $upload_png );
$meta = wp_get_attachment_metadata( $id );
list( $file_path, $upload_path ) = ewww_image_optimizer_attachment_path( $meta, $id );
$this->assertEquals( 'image/jpeg', ewww_image_optimizer_mimetype( $file_path, 'i' ) );
$test_png = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/books.png' );
$upload_png = $test_png . '.png';
copy( $test_png, $upload_png );
$id = $this->factory->attachment->create_upload_object( $upload_png );
$meta = wp_get_attachment_metadata( $id );
list( $file_path, $upload_path ) = ewww_image_optimizer_attachment_path( $meta, $id );
$this->assertEquals( 'image/png', ewww_image_optimizer_mimetype( $file_path, 'i' ) );
unlink( $test_png );
}
/**
* Tests attachment conversion by uploading a PNG attachment and then converting it.
*/
function test_convert_png_attachment() {
ewww_image_optimizer_set_option( 'ewww_image_optimizer_png_level', 0 );
define( 'EWWW_IMAGE_OPTIMIZER_DISABLE_AUTOCONVERT', true );
$upload_png = self::$test_png . '.png';
copy( self::$test_png, $upload_png );
$id = $this->factory->attachment->create_upload_object( $upload_png );
$meta = wp_get_attachment_metadata( $id );
list( $file_path, $upload_path ) = ewww_image_optimizer_attachment_path( $meta, $id );
$this->assertEquals( 'image/png', ewww_image_optimizer_mimetype( $file_path, 'i' ) );
global $ewww_new_image;
$ewww_new_image = true;
ewww_image_optimizer_set_option( 'ewww_image_optimizer_png_level', 10 );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_png_to_jpg', true );
$meta = ewww_image_optimizer_resize_from_meta_data( $meta, $id, false, true );
list( $file_path, $upload_path ) = ewww_image_optimizer_attachment_path( $meta, $id );
$this->assertEquals( 'image/jpeg', ewww_image_optimizer_mimetype( $file_path, 'i' ) );
$base_dir = trailingslashit( dirname( $file_path ) );
foreach ( $meta['sizes'] as $size => $data ) {
$image_path = $base_dir . wp_basename( $data['file'] );
$this->assertEquals( 'image/jpeg', ewww_image_optimizer_mimetype( $image_path, 'i' ) );
}
ewww_image_optimizer_set_option( 'ewww_image_optimizer_png_to_jpg', '' );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_disable_autoconvert', '' );
}
/**
* Test GIF to PNG conversion.
*/
function test_convert_gif_to_png() {
update_option( 'ewww_image_optimizer_gif_level', 10 );
update_option( 'ewww_image_optimizer_png_level', 10 );
update_option( 'ewww_image_optimizer_gif_to_png', true );
update_site_option( 'ewww_image_optimizer_gif_level', 10 );
update_site_option( 'ewww_image_optimizer_png_level', 10 );
update_site_option( 'ewww_image_optimizer_gif_to_png', true );
$results = $this->optimize_gif();
$this->assertEquals( 'image/png', ewww_image_optimizer_mimetype( $results[0], 'i' ) );
unlink( $results[0] );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$results = $this->optimize_gif();
$this->assertEquals( 'image/png', ewww_image_optimizer_mimetype( $results[0], 'i' ) );
unlink( $results[0] );
update_option( 'ewww_image_optimizer_gif_to_png', '' );
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_gif_to_png', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
}
/**
* Cleans up ewwwio_images table.
*/
function tear_down() {
global $wpdb;
remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
$wpdb->query( "DROP TABLE IF EXISTS $wpdb->ewwwio_images" );
add_filter( 'query', array( $this, '_drop_temporary_tables' ) );
delete_option( 'ewww_image_optimizer_version' );
delete_option( 'ewww_image_optimizer_cloud_key' );
delete_site_option( 'ewww_image_optimizer_version' );
delete_site_option( 'ewww_image_optimizer_cloud_key' );
parent::tear_down();
}
/**
* Cleans up the temp images.
*/
public static function tear_down_after_class() {
if ( ewwwio_is_file( self::$test_jpg ) ) {
unlink( self::$test_jpg );
}
if ( ewwwio_is_file( self::$test_png ) ) {
unlink( self::$test_png );
}
if ( ewwwio_is_file( self::$test_gif ) ) {
unlink( self::$test_gif );
}
ewww_image_optimizer_remove_binaries();
}
}

View File

@@ -0,0 +1,587 @@
<?php
/**
* Class EWWWIO_Optimize_Tests
*
* @link https://ewww.io
* @package Ewww_Image_Optimizer
*/
/**
* Optimization test cases.
*/
class EWWWIO_Optimize_Tests extends WP_UnitTestCase {
/**
* The location of the test JPG image.
*
* @var string $test_jpg
*/
public static $test_jpg = '';
/**
* The location of the test PNG image.
*
* @var string $test_png
*/
public static $test_png = '';
/**
* The location of the test GIF image.
*
* @var string $test_gif
*/
public static $test_gif = '';
/**
* The location of the test PDF image.
*
* @var string $test_pdf
*/
public static $test_pdf = '';
/**
* The location of the test SVG image.
*
* @var string $test_svg
*/
public static $test_svg = '';
/**
* Downloads test images.
*/
public static function set_up_before_class() {
$wp_upload_dir = wp_upload_dir();
$temp_upload_dir = trailingslashit( $wp_upload_dir['basedir'] ) . 'testing/';
wp_mkdir_p( $temp_upload_dir );
$test_jpg = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/20170314_174658.jpg' );
rename( $test_jpg, $temp_upload_dir . wp_basename( $test_jpg ) );
self::$test_jpg = $temp_upload_dir . wp_basename( $test_jpg );
$test_png = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/books.png' );
rename( $test_png, $temp_upload_dir . wp_basename( $test_png ) );
self::$test_png = $temp_upload_dir . wp_basename( $test_png );
$test_gif = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/gifsiclelogo.gif' );
rename( $test_gif, $temp_upload_dir . wp_basename( $test_gif ) );
self::$test_gif = $temp_upload_dir . wp_basename( $test_gif );
$test_pdf = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/tomtempleartist-bio-2008.pdf' );
rename( $test_pdf, $temp_upload_dir . wp_basename( $test_pdf ) );
self::$test_pdf = $temp_upload_dir . wp_basename( $test_pdf );
$test_svg = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/image-x-generic.svg' );
rename( $test_svg, $temp_upload_dir . wp_basename( $test_svg ) );
self::$test_svg = $temp_upload_dir . wp_basename( $test_svg );
ewwwio()->set_defaults();
update_option( 'ewww_image_optimizer_jpg_level', 10 );
update_option( 'ewww_image_optimizer_gif_level', 10 );
update_option( 'ewww_image_optimizer_webp', true );
update_option( 'ewww_image_optimizer_png_level', 40 );
update_site_option( 'ewww_image_optimizer_webp', true );
update_site_option( 'ewww_image_optimizer_png_level', 40 );
ewwwio()->local->install_tools();
ewww_image_optimizer_install_pngout();
ewww_image_optimizer_install_svgcleaner();
update_option( 'ewww_image_optimizer_webp', '' );
update_option( 'ewww_image_optimizer_png_level', 10 );
update_site_option( 'ewww_image_optimizer_webp', '' );
update_site_option( 'ewww_image_optimizer_png_level', 10 );
}
/**
* Initializes the plugin and installs the ewwwio_images table.
*/
function set_up() {
parent::set_up();
remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
ewww_image_optimizer_install_table();
add_filter( 'query', array( $this, '_create_temporary_tables' ) );
}
/**
* Copies the test JPG to a temp file, optimizes it, and returns the results.
*
* @return array The results of the ewww_image_optimizer() function.
*/
protected function optimize_jpg() {
global $ewww_force;
$ewww_force = 1;
$filename = self::$test_jpg . ".jpg";
copy( self::$test_jpg, $filename );
$results = ewww_image_optimizer( $filename );
return $results;
}
/**
* Copies the test PNG to a temp file, optimizes it, and returns the results.
*
* @return array The results of the ewww_image_optimizer() function.
*/
protected function optimize_png() {
global $ewww_force;
$ewww_force = 1;
$filename = self::$test_png . ".png";
copy( self::$test_png, $filename );
$results = ewww_image_optimizer( $filename );
return $results;
}
/**
* Copies the test GIF to a temp file, optimizes it, and returns the results.
*
* @return array The results of the ewww_image_optimizer() function.
*/
protected function optimize_gif() {
global $ewww_force;
$ewww_force = 1;
$filename = self::$test_gif . ".gif";
copy( self::$test_gif, $filename );
$results = ewww_image_optimizer( $filename );
return $results;
}
/**
* Copies the test PDF to a temp file, optimizes it, and returns the results.
*
* @return array The results of the ewww_image_optimizer() function.
*/
protected function optimize_pdf() {
global $ewww_force;
$ewww_force = 1;
$filename = self::$test_pdf . ".pdf";
copy( self::$test_pdf, $filename );
$results = ewww_image_optimizer( $filename );
return $results;
}
/**
* Copies the test SVG to a temp file, optimizes it, and returns the results.
*
* @return array The results of the ewww_image_optimizer() function.
*/
protected function optimize_svg() {
global $ewww_force;
$ewww_force = 1;
$filename = self::$test_svg . ".svg";
copy( self::$test_svg, $filename );
$results = ewww_image_optimizer( $filename );
return $results;
}
/**
* Test default JPG optimization with WebP.
*/
function test_optimize_jpg_10() {
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_jpg_level', 10 );
update_option( 'ewww_image_optimizer_webp', true );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_jpg_level', 10 );
update_site_option( 'ewww_image_optimizer_webp', true );
$results = $this->optimize_jpg();
update_option( 'ewww_image_optimizer_webp', '' );
update_site_option( 'ewww_image_optimizer_webp', '' );
$this->assertEquals( 1348499, filesize( $results[0] ) );
unlink( $results[0] );
$this->assertEquals( 200048, filesize( $results[0] . '.webp' ) );
if ( ewwwio_is_file( $results[0] . '.webp' ) ) {
unlink( $results[0] . '.webp' );
}
}
/**
* Test lossless JPG and keeps meta with WebP and autorotation tests.
*/
function test_optimize_jpg_10_keep_meta() {
update_option( 'ewww_image_optimizer_metadata_remove', '' );
update_option( 'ewww_image_optimizer_jpg_level', 10 );
update_option( 'ewww_image_optimizer_webp', true );
update_site_option( 'ewww_image_optimizer_metadata_remove', '' );
update_site_option( 'ewww_image_optimizer_jpg_level', 10 );
update_site_option( 'ewww_image_optimizer_webp', true );
$results = $this->optimize_jpg();
update_option( 'ewww_image_optimizer_webp', '' );
update_site_option( 'ewww_image_optimizer_webp', '' );
// size post opt.
$this->assertEquals( 1368047, filesize( $results[0] ) );
// orientation pre-rotation.
$this->assertEquals( ewww_image_optimizer_get_orientation( self::$test_jpg, 'image/jpeg' ), 8 );
// orientation post-rotation should always be 1, no matter the image.
$this->assertEquals( ewww_image_optimizer_get_orientation( $results[0], 'image/jpeg' ), 1 );
unlink( $results[0] );
// size of webp with meta.
$this->assertEquals( 219630, filesize( $results[0] . '.webp' ) );
if ( ewwwio_is_file( $results[0] . '.webp' ) ) {
unlink( $results[0] . '.webp' );
}
}
/**
* Test Max Lossless JPG optimization with WebP (API level 20).
*/
function test_optimize_jpg_20() {
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_jpg_level', 20 );
update_option( 'ewww_image_optimizer_webp', true );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_jpg_level', 20 );
update_site_option( 'ewww_image_optimizer_webp', true );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$results = $this->optimize_jpg();
update_option( 'ewww_image_optimizer_webp', '' );
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_webp', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
$this->assertEquals( 1339854, filesize( $results[0] ) );
unlink( $results[0] );
$this->assertEquals( 187866, filesize( $results[0] . '.webp' ) );
if ( ewwwio_is_file( $results[0] . '.webp' ) ) {
unlink( $results[0] . '.webp' );
}
}
/**
* Test lossless JPG via API and keeps meta with WebP and autorotation check.
*/
function test_optimize_jpg_20_keep_meta() {
update_option( 'ewww_image_optimizer_metadata_remove', '' );
update_option( 'ewww_image_optimizer_jpg_level', 20 );
update_option( 'ewww_image_optimizer_webp', true );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_metadata_remove', '' );
update_site_option( 'ewww_image_optimizer_jpg_level', 20 );
update_site_option( 'ewww_image_optimizer_webp', true );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$results = $this->optimize_jpg();
update_option( 'ewww_image_optimizer_webp', '' );
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_webp', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
// size post opt.
$this->assertEquals( 1359406, filesize( $results[0] ) );
// orientation pre-rotation.
$this->assertEquals( ewww_image_optimizer_get_orientation( $results[0], 'image/jpeg' ), 1 );
unlink( $results[0] );
// size of webp with meta.
$this->assertEquals( 207452, filesize( $results[0] . '.webp' ) );
if ( ewwwio_is_file( $results[0] . '.webp' ) ) {
unlink( $results[0] . '.webp' );
}
}
/**
* Test Regular Lossy JPG optimization (API level 30).
*/
function test_optimize_jpg_30() {
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_jpg_level', 30 );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_jpg_level', 30 );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$results = $this->optimize_jpg();
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
$this->assertEquals( 344098, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test Max Lossy JPG optimization (API level 40).
*/
function test_optimize_jpg_40() {
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_jpg_level', 40 );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_jpg_level', 40 );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$results = $this->optimize_jpg();
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
$this->assertEquals( 310924, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test lossless PNG with optipng.
*/
function test_optimize_png_10_optipng() {
update_option( 'ewww_image_optimizer_png_level', 10 );
update_option( 'ewww_image_optimizer_disable_pngout', true );
update_option( 'ewww_image_optimizer_optipng_level', 2 );
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_webp', true );
update_site_option( 'ewww_image_optimizer_png_level', 10 );
update_site_option( 'ewww_image_optimizer_disable_pngout', true );
update_site_option( 'ewww_image_optimizer_optipng_level', 2 );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_webp', true );
$results = $this->optimize_png();
update_option( 'ewww_image_optimizer_webp', '' );
update_site_option( 'ewww_image_optimizer_webp', '' );
$this->assertEquals( 188043, filesize( $results[0] ) );
unlink( $results[0] );
$this->assertLessThanOrEqual( 138000, filesize( $results[0] . '.webp' ) );
if ( ewwwio_is_file( $results[0] . '.webp' ) ) {
unlink( $results[0] . '.webp' );
}
}
/**
* Test lossless PNG with optipng, keeping metadata.
*/
function test_optimize_png_10_optipng_keep_meta() {
update_option( 'ewww_image_optimizer_png_level', 10 );
update_option( 'ewww_image_optimizer_disable_pngout', true );
update_option( 'ewww_image_optimizer_optipng_level', 2 );
update_option( 'ewww_image_optimizer_metadata_remove', '' );
update_site_option( 'ewww_image_optimizer_png_level', 10 );
update_site_option( 'ewww_image_optimizer_disable_pngout', true );
update_site_option( 'ewww_image_optimizer_optipng_level', 2 );
update_site_option( 'ewww_image_optimizer_metadata_remove', '' );
$results = $this->optimize_png();
$this->assertLessThanOrEqual( 190775, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test lossless PNG with optipng and PNGOUT.
*/
function test_optimize_png_10_optipng_pngout() {
update_option( 'ewww_image_optimizer_png_level', 10 );
update_option( 'ewww_image_optimizer_disable_pngout', '' );
update_option( 'ewww_image_optimizer_optipng_level', 2 );
update_option( 'ewww_image_optimizer_pngout_level', 1 );
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_png_level', 10 );
update_site_option( 'ewww_image_optimizer_disable_pngout', '' );
update_site_option( 'ewww_image_optimizer_optipng_level', 2 );
update_site_option( 'ewww_image_optimizer_pngout_level', 1 );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
$results = $this->optimize_png();
$this->assertEquals( 180779, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test lossy local PNG with optipng.
*/
function test_optimize_png_40_optipng() {
update_option( 'ewww_image_optimizer_png_level', 40 );
update_option( 'ewww_image_optimizer_disable_pngout', true );
update_option( 'ewww_image_optimizer_optipng_level', 2 );
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_png_level', 40 );
update_site_option( 'ewww_image_optimizer_disable_pngout', true );
update_site_option( 'ewww_image_optimizer_optipng_level', 2 );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
$results = $this->optimize_png();
$this->assertLessThanOrEqual( 39000, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test "better" lossless PNG with API, no meta.
*/
function test_optimize_png_20() {
update_option( 'ewww_image_optimizer_png_level', 20 );
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_png_level', 20 );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$results = $this->optimize_png();
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
$this->assertLessThanOrEqual( 175000, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test regular lossy PNG with API.
*/
function test_optimize_png_40() {
update_option( 'ewww_image_optimizer_png_level', 40 );
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_png_level', 40 );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$results = $this->optimize_png();
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
$this->assertLessThanOrEqual( 39100, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test max lossy PNG with API.
*/
function test_optimize_png_50() {
update_option( 'ewww_image_optimizer_png_level', 50 );
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_png_level', 50 );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$results = $this->optimize_png();
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
$this->assertEquals( 32267, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test lossless GIF.
*/
function test_optimize_gif_10() {
update_option( 'ewww_image_optimizer_gif_level', 10 );
update_site_option( 'ewww_image_optimizer_gif_level', 10 );
$results = $this->optimize_gif();
$this->assertEquals( 8900, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test lossless GIF via API.
*/
function test_optimize_gif_10_api() {
update_option( 'ewww_image_optimizer_gif_level', 10 );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_option( 'ewww_image_optimizer_webp', true );
update_site_option( 'ewww_image_optimizer_gif_level', 10 );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_webp', true );
$results = $this->optimize_gif();
update_option( 'ewww_image_optimizer_webp', '' );
update_site_option( 'ewww_image_optimizer_webp', '' );
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
$this->assertEquals( 8900, filesize( $results[0] ) );
unlink( $results[0] );
$this->assertEquals( 8014, filesize( $results[0] . '.webp' ) );
if ( ewwwio_is_file( $results[0] . '.webp' ) ) {
unlink( $results[0] . '.webp' );
}
}
/**
* Test lossless PDF via API.
*/
function test_optimize_pdf_10() {
update_option( 'ewww_image_optimizer_pdf_level', 10 );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_pdf_level', 10 );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$results = $this->optimize_pdf();
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
$this->assertEquals( 144907, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test lossy PDF via API.
*/
function test_optimize_pdf_20() {
update_option( 'ewww_image_optimizer_pdf_level', 20 );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_pdf_level', 20 );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$results = $this->optimize_pdf();
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
$this->assertLessThan( 129000, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test minimal SVG locally.
*/
function test_optimize_svg_01() {
update_option( 'ewww_image_optimizer_svg_level', 1 );
update_site_option( 'ewww_image_optimizer_svg_level', 1 );
$results = $this->optimize_svg();
$this->assertEquals( 10792, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test default SVG locally.
*/
function test_optimize_svg_10() {
update_option( 'ewww_image_optimizer_svg_level', 10 );
update_site_option( 'ewww_image_optimizer_svg_level', 10 );
$results = $this->optimize_svg();
$this->assertEquals( 9518, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test minimal SVG via API.
*/
function test_optimize_svg_01_api() {
update_option( 'ewww_image_optimizer_svg_level', 1 );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_svg_level', 1 );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$results = $this->optimize_svg();
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
$this->assertEquals( 10792, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Test default SVG via API.
*/
function test_optimize_svg_10_api() {
update_option( 'ewww_image_optimizer_svg_level', 10 );
update_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
update_site_option( 'ewww_image_optimizer_svg_level', 10 );
update_site_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
$results = $this->optimize_svg();
update_option( 'ewww_image_optimizer_cloud_key', '' );
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
$this->assertEquals( 9518, filesize( $results[0] ) );
unlink( $results[0] );
}
/**
* Cleans up ewwwio_images table.
*/
function tear_down() {
global $wpdb;
remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
$wpdb->query( "DROP TABLE IF EXISTS $wpdb->ewwwio_images" );
add_filter( 'query', array( $this, '_drop_temporary_tables' ) );
delete_option( 'ewww_image_optimizer_version' );
delete_option( 'ewww_image_optimizer_cloud_key' );
delete_site_option( 'ewww_image_optimizer_version' );
delete_site_option( 'ewww_image_optimizer_cloud_key' );
parent::tear_down();
}
/**
* Cleans up the temp images.
*/
public static function tear_down_after_class() {
if ( ewwwio_is_file( self::$test_jpg ) ) {
unlink( self::$test_jpg );
}
if ( ewwwio_is_file( self::$test_png ) ) {
unlink( self::$test_png );
}
if ( ewwwio_is_file( self::$test_gif ) ) {
unlink( self::$test_gif );
}
if ( ewwwio_is_file( self::$test_pdf ) ) {
unlink( self::$test_pdf );
}
ewww_image_optimizer_remove_binaries();
}
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* Class EWWWIO_Option_Tests
*
* @link https://ewww.io
* @package Ewww_Image_Optimizer
*/
// TODO: run tests for some of the fancier sanitation stuff like folders to optimize and webp paths.
/**
* Option test cases.
*/
class EWWWIO_Option_Tests extends WP_UnitTestCase {
/**
* Test the jpg background funtion to ensure proper formatting.
*/
function test_jpg_background() {
ewww_image_optimizer_set_option( 'ewww_image_optimizer_jpg_background', '#fff333' );
$this->assertEquals( 'fff333', ewww_image_optimizer_jpg_background() );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_jpg_background', '' );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_jpg_background', '#fff' );
$this->assertEquals( '', ewww_image_optimizer_jpg_background() );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_jpg_background', '' );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_jpg_background', 'black' );
$this->assertEquals( '', ewww_image_optimizer_jpg_background() );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_jpg_background', '' );
}
/**
* Test the jpg/webp quality sanitizer.
*/
function test_jpg_quality() {
ewww_image_optimizer_set_option( 'ewww_image_optimizer_jpg_quality', 1000 );
$this->assertEquals( '', ewww_image_optimizer_jpg_quality() );
$this->assertEquals( 82, (int) apply_filters( 'jpeg_quality', 82, 'image/webp' ) );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_jpg_quality', '' );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_jpg_quality', 75 );
$this->assertEquals( 75, ewww_image_optimizer_jpg_quality() );
$this->assertEquals( 75, (int) apply_filters( 'jpeg_quality', 82, 'image/webp' ) );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_jpg_quality', '' );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_jpg_quality', 'spinach' );
$this->assertEquals( '', ewww_image_optimizer_jpg_quality() );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_jpg_quality', '' );
}
}

View File

@@ -0,0 +1,164 @@
<?php
/**
* Class EWWWIO_Resize_Tests
*
* @link https://ewww.io
* @package Ewww_Image_Optimizer
*/
/**
* Resizing test cases.
*/
class EWWWIO_Resize_Tests extends WP_UnitTestCase {
/**
* The location of the test JPG image.
*
* @var string $test_jpg
*/
public static $test_jpg = '';
/**
* The location of the test PNG image.
*
* @var string $test_png
*/
public static $test_png = '';
/**
* The location of the test GIF image.
*
* @var string $test_gif
*/
public static $test_gif = '';
/**
* Downloads test images.
*/
public static function set_up_before_class() {
self::$test_jpg = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/20170314_174658.jpg' );
copy( self::$test_jpg, self::$test_jpg . '.jpg' );
self::$test_jpg .= '.jpg';
ewwwio()->set_defaults();
update_option( 'ewww_image_optimizer_jpg_level', '10' );
ewwwio()->local->install_tools();
}
/**
* Initializes the plugin and installs the ewwwio_images table.
*/
function set_up() {
parent::set_up();
remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
ewww_image_optimizer_install_table();
add_filter( 'query', array( $this, '_create_temporary_tables' ) );
}
/**
* Creates a JPG attachment while resizing is enabled (no cropping) using jpegtran.
*/
function test_scale_jpg_local() {
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_jpg_level', 10 );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_jpg_level', 10 );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_maxotherwidth', 1024 );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_maxotherheight', 1024 );
$id = $this->factory->attachment->create_upload_object( self::$test_jpg );
$meta = wp_get_attachment_metadata( $id );
list( $file_path, $upload_path ) = ewww_image_optimizer_attachment_path( $meta, $id );
list( $width, $height ) = wp_getimagesize( $file_path );
$this->assertEquals( 576, $width );
$this->assertEquals( 1024, $height );
}
/**
* Creates a JPG attachment while resizing is enabled (crop-mode) using jpegtran.
*/
function test_crop_jpg_local() {
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_jpg_level', 10 );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_jpg_level', 10 );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_maxotherwidth', 1024 );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_maxotherheight', 1024 );
add_filter( 'ewww_image_optimizer_crop_image', '__return_true' );
$id = $this->factory->attachment->create_upload_object( self::$test_jpg );
remove_filter( 'ewww_image_optimizer_crop_image', '__return_true' );
$meta = wp_get_attachment_metadata( $id );
list( $file_path, $upload_path ) = ewww_image_optimizer_attachment_path( $meta, $id );
list( $width, $height ) = wp_getimagesize( $file_path );
$this->assertEquals( 1024, $width );
$this->assertEquals( 1024, $height );
}
/**
* Creates a JPG attachment while resizing is enabled (no cropping) using API.
*/
function test_scale_jpg_cloud() {
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_jpg_level', 20 );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_jpg_level', 20 );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_maxotherwidth', 1024 );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_maxotherheight', 1024 );
$id = $this->factory->attachment->create_upload_object( self::$test_jpg );
$meta = wp_get_attachment_metadata( $id );
list( $file_path, $upload_path ) = ewww_image_optimizer_attachment_path( $meta, $id );
list( $width, $height ) = wp_getimagesize( $file_path );
$this->assertEquals( 576, $width );
$this->assertEquals( 1024, $height );
}
/**
* Creates a JPG attachment while resizing is enabled (crop-mode) using API.
*/
function test_crop_jpg_cloud() {
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_jpg_level', 20 );
update_site_option( 'ewww_image_optimizer_metadata_remove', true );
update_site_option( 'ewww_image_optimizer_jpg_level', 20 );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_cloud_key', 'abc123' );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_maxotherwidth', 1024 );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_maxotherheight', 1024 );
add_filter( 'ewww_image_optimizer_crop_image', '__return_true' );
$id = $this->factory->attachment->create_upload_object( self::$test_jpg );
remove_filter( 'ewww_image_optimizer_crop_image', '__return_true' );
$meta = wp_get_attachment_metadata( $id );
list( $file_path, $upload_path ) = ewww_image_optimizer_attachment_path( $meta, $id );
list( $width, $height ) = wp_getimagesize( $file_path );
$this->assertEquals( 1024, $width );
$this->assertEquals( 1024, $height );
}
/**
* Cleans up ewwwio_images table.
*/
function tear_down() {
global $wpdb;
remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
$wpdb->query( "DROP TABLE IF EXISTS $wpdb->ewwwio_images" );
add_filter( 'query', array( $this, '_drop_temporary_tables' ) );
delete_option( 'ewww_image_optimizer_version' );
delete_option( 'ewww_image_optimizer_cloud_key' );
delete_site_option( 'ewww_image_optimizer_version' );
delete_site_option( 'ewww_image_optimizer_cloud_key' );
parent::tear_down();
}
/**
* Cleans up the temp images.
*/
public static function tear_down_after_class() {
if ( ewwwio_is_file( self::$test_jpg ) ) {
unlink( self::$test_jpg );
}
if ( ewwwio_is_file( self::$test_png ) ) {
unlink( self::$test_png );
}
if ( ewwwio_is_file( self::$test_gif ) ) {
unlink( self::$test_gif );
}
}
}

View File

@@ -0,0 +1,132 @@
<?php
/**
* Class EWWWIO_Table_Tests
*
* @link https://ewww.io
* @package Ewww_Image_Optimizer
*/
/**
* Custom table test cases.
*/
class EWWWIO_Table_Tests extends WP_UnitTestCase {
/**
* The location of the test GIF image.
*
* @var string $test_gif
*/
public static $test_gif = '';
/**
* Downloads test images.
*/
public static function set_up_before_class() {
$wp_upload_dir = wp_upload_dir();
$temp_upload_dir = trailingslashit( $wp_upload_dir['basedir'] ) . 'testing/';
wp_mkdir_p( $temp_upload_dir );
$test_gif = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/gifsiclelogo.gif' );
rename( $test_gif, $temp_upload_dir . wp_basename( $test_gif ) );
self::$test_gif = $temp_upload_dir . wp_basename( $test_gif );
ewwwio()->set_defaults();
update_option( 'ewww_image_optimizer_gif_level', 10 );
update_site_option( 'ewww_image_optimizer_gif_level', 10 );
ewwwio()->local->install_tools();
}
/**
* Initializes the plugin and installs the ewwwio_images table.
*/
function set_up() {
parent::set_up();
remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
ewww_image_optimizer_install_table();
add_filter( 'query', array( $this, '_create_temporary_tables' ) );
}
/**
* Copies the test GIF to a temp file, optimizes it, and returns the results.
*
* @return array The results of the ewww_image_optimizer() function.
*/
protected function optimize_gif( $original = false ) {
if ( ! $original ) {
$original = self::$test_gif;
}
global $ewww_force;
$ewww_force = 1;
$filename = $original . ".gif";
copy( $original, $filename );
$results = ewww_image_optimizer( $filename, 1 );
return $results;
}
/**
* Test updating records in table.
*/
function test_ewwwio_images_table() {
global $wpdb;
global $ewww_image;
$orig_size = filesize( self::$test_gif );
$results = $this->optimize_gif();
$ewww_image = false;
$opt_file = $results[0];
$results_msg = ewww_image_optimizer_check_table( $opt_file, $orig_size );
$this->assertEmpty( $results_msg );
$ewww_image = false;
$file_size = ewww_image_optimizer_filesize( $opt_file );
$results_msg = ewww_image_optimizer_check_table( $opt_file, $file_size );
$this->assertStringStartsWith( 'Reduced by', $results_msg );
$second_results_msg = ewww_image_optimizer_update_table( $opt_file, $file_size - 500, $orig_size );
$this->assertStringStartsWith( 'Reduced by', $second_results_msg );
$this->assertNotEquals( $results_msg, $second_results_msg );
$ewww_image = false;
$results_msg = ewww_image_optimizer_check_table( $opt_file, $file_size - 500 );
$this->assertStringStartsWith( $second_results_msg, $results_msg );
$record = ewww_image_optimizer_find_already_optimized( $opt_file );
unset( $record['id'] );
$record['image_size'] = $file_size;
$record['path'] = ewww_image_optimizer_relativize_path( $opt_file );
$wpdb->insert( $wpdb->ewwwio_images, $record );
$record['image_size'] = 0;
$record['results'] = '';
$wpdb->insert( $wpdb->ewwwio_images, $record );
$ewww_image = false;
$results_msg = ewww_image_optimizer_check_table( $opt_file, $file_size );
$this->assertStringStartsWith( 'Reduced by', $results_msg );
unlink( $results[0] );
}
/**
* Cleans up ewwwio_images table.
*/
function tear_down() {
global $wpdb;
remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
$wpdb->query( "DROP TABLE IF EXISTS $wpdb->ewwwio_images" );
add_filter( 'query', array( $this, '_drop_temporary_tables' ) );
delete_option( 'ewww_image_optimizer_version' );
delete_site_option( 'ewww_image_optimizer_version' );
parent::tear_down();
}
/**
* Cleans up the temp images.
*/
public static function tear_down_after_class() {
if ( ewwwio_is_file( self::$test_gif ) ) {
unlink( self::$test_gif );
}
ewww_image_optimizer_remove_binaries();
}
}

View File

@@ -0,0 +1,150 @@
<?php
/**
* Class EWWWIO_Utility_Tests
*
* @link https://ewww.io
* @package Ewww_Image_Optimizer
*/
/**
* Utility test cases.
*/
class EWWWIO_Utility_Tests extends WP_UnitTestCase {
/**
* Test our image results function to ensure proper formatting.
*/
function test_byte_format() {
$this->assertEquals( 'reduced by 90.0% (90 b)', strtolower( ewww_image_optimizer_image_results( 100, 10 ) ) );
$this->assertEquals( 'reduced by 29.8% (29.2 kb)', strtolower( ewww_image_optimizer_image_results( 100235, 70384 ) ) );
$this->assertEquals( 'reduced by 36.8% (1.1 mb)', strtolower( ewww_image_optimizer_image_results( 3202350, 2023840 ) ) );
}
/**
* Test the checksum function to be sure all our binaries are in the list.
*/
function test_sha256sum() {
$binaries = scandir( EWWW_IMAGE_OPTIMIZER_BINARY_PATH );
foreach ( $binaries as $binary ) {
$binary = trailingslashit( EWWW_IMAGE_OPTIMIZER_BINARY_PATH ) . $binary;
if ( ! ewwwio_is_file( $binary ) ) {
continue;
}
$this->assertTrue( ewwwio()->local->check_integrity( $binary ) );
}
}
/**
* Test the mimetype function to be sure all our binaries validate.
*/
function test_mimetype() {
$binaries = scandir( EWWW_IMAGE_OPTIMIZER_BINARY_PATH );
ewww_image_optimizer_set_option( 'ewww_image_optimizer_debug', true );
foreach ( $binaries as $binary ) {
$binary = trailingslashit( EWWW_IMAGE_OPTIMIZER_BINARY_PATH ) . $binary;
if ( ! ewwwio_is_file( $binary ) ) {
continue;
}
$this->assertTrue( (bool) ewwwio()->mimetype( $binary, 'b' ), $binary . ":\n" . str_replace( '<br>', "\n", EWWW\Base::$debug_data ) );
}
}
/**
* Tests that shell commands get escaped properly (replaces spaces in binary names).
*/
function test_shellcmdesc() {
$this->assertEquals( ewwwio()->escapeshellcmd( 'jpeg tran' ), 'jpeg\ tran' );
}
/**
* Tests that shell args get escaped properly (quotes and such).
*/
function test_shellargesc() {
$this->assertEquals( ewwwio()->escapeshellarg( "file'name" ), "'file'\\''name'" );
}
/**
* Tests that GIF animation is detected properly.
*/
function test_animated() {
$wp_upload_dir = wp_upload_dir();
$test_gif = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/gifsiclelogo.gif' );
rename( $test_gif, $wp_upload_dir['basedir'] . wp_basename( $test_gif ) );
$test_gif = $wp_upload_dir['basedir'] . wp_basename( $test_gif );
$this->assertTrue( ewww_image_optimizer_is_animated( $test_gif ) );
unlink( $test_gif );
}
/**
* Tests that PNG transparency is detected properly.
*/
function test_transparency() {
$wp_upload_dir = wp_upload_dir();
$test_png = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/books.png' );
rename( $test_png, $wp_upload_dir['basedir'] . wp_basename( $test_png ) );
$test_png = $wp_upload_dir['basedir'] . wp_basename( $test_png );
$this->assertTrue( ewww_image_optimizer_png_alpha( $test_png ) );
unlink( $test_png );
}
/**
* Test that EWWW IO plugin images are ignored using the filter function.
*/
function test_skipself() {
$test_image = EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'images/test.png';
$this->assertTrue( ewww_image_optimizer_ignore_self( false, $test_image ) );
}
/**
* Test relative path functions.
*/
function test_relative_paths() {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_RELATIVE' ) ) {
define( 'EWWW_IMAGE_OPTIMIZER_RELATIVE', true );
}
$test_image = trailingslashit( ABSPATH ) . 'images/test.png';
$relative_test_image_path = ewww_image_optimizer_relativize_path( $test_image );
$this->assertEquals( 'ABSPATHimages/test.png', $relative_test_image_path );
$replaced_test_image = ewww_image_optimizer_absolutize_path( $relative_test_image_path );
$this->assertEquals( $test_image, $replaced_test_image );
}
/**
* Test local copy of Cloudflare IP range list.
*/
function test_cf_ip_ranges() {
$latest_ips = wp_remote_get( 'https://www.cloudflare.com/ips-v4' );
$latest_ips = explode( "\n", $latest_ips['body'] );
$cf_ips = array(
'173.245.48.0/20',
'103.21.244.0/22',
'103.22.200.0/22',
'103.31.4.0/22',
'141.101.64.0/18',
'108.162.192.0/18',
'190.93.240.0/20',
'188.114.96.0/20',
'197.234.240.0/22',
'198.41.128.0/17',
'162.158.0.0/15',
'104.16.0.0/13',
'104.24.0.0/14',
'172.64.0.0/13',
'131.0.72.0/22',
);
foreach( $latest_ips as $key => $range ) {
if ( empty( $range ) ) {
continue;
}
$this->assertEquals( $range, $cf_ips[ $key ] );
}
}
/**
* Run syntax checks for requires in WP-Admin.
*/
function test_admin_init() {
ewwwio()->admin_init();
$this->assertTrue( true );
}
}