-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest-utility.php
150 lines (138 loc) · 4.59 KB
/
test-utility.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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 );
}
}