blob: cd765320233f5f6f81d161f037e60b0c1c145760 (
plain) (
blame)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
<?php
/**
* Admin ajax functions to be tested
*/
require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
/**
* Testing ajax compression test functionality
*
* @package WordPress
* @subpackage UnitTests
* @since 3.4.0
* @group ajax
* @runTestsInSeparateProcesses
*/
class Tests_Ajax_CompressionTest extends WP_Ajax_UnitTestCase {
/**
* Test as a logged out user
*/
public function test_logged_out() {
$this->logout();
// Set up a default request
$_GET['test'] = 1;
// Make the request
$this->setExpectedException( 'WPAjaxDieStopException', '0' );
$this->_handleAjax( 'wp-compression-test' );
}
/**
* Fetch the test text
*/
public function test_text() {
// Become an administrator
$this->_setRole( 'administrator' );
// Set up a default request
$_GET['test'] = 1;
// Make the request
try {
$this->_handleAjax( 'wp-compression-test' );
} catch ( WPAjaxDieContinueException $e ) {
unset( $e );
}
// Ensure we found the right match
$this->assertContains( 'wpCompressionTest', $this->_last_response );
}
/**
* Fetch the test text (gzdeflate)
*/
public function test_gzdeflate() {
if ( !function_exists( 'gzdeflate' ) ) {
$this->markTestSkipped( 'gzdeflate function not available' );
}
// Become an administrator
$this->_setRole( 'administrator' );
// Set up a default request
$_GET['test'] = 2;
$_SERVER['HTTP_ACCEPT_ENCODING'] = 'deflate';
// Make the request
try {
$this->_handleAjax( 'wp-compression-test' );
} catch ( WPAjaxDieContinueException $e ) {
unset( $e );
}
// Ensure we found the right match
$this->assertContains( 'wpCompressionTest', gzinflate( $this->_last_response ) );
}
/**
* Fetch the test text (gzencode)
*/
public function test_gzencode() {
if ( !function_exists('gzencode') ) {
$this->markTestSkipped( 'gzencode function not available' );
}
// Become an administrator
$this->_setRole( 'administrator' );
// Set up a default request
$_GET['test'] = 2;
$_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
// Make the request
try {
$this->_handleAjax( 'wp-compression-test' );
} catch ( WPAjaxDieContinueException $e ) {
unset( $e );
}
// Ensure we found the right match
$this->assertContains( 'wpCompressionTest', $this->_gzdecode( $this->_last_response ) );
}
/**
* Fetch the test text (unknown encoding)
*/
public function test_unknown_encoding() {
// Become an administrator
$this->_setRole( 'administrator' );
// Set up a default request
$_GET['test'] = 2;
$_SERVER['HTTP_ACCEPT_ENCODING'] = 'unknown';
// Make the request
$this->setExpectedException( 'WPAjaxDieStopException', '-1' );
$this->_handleAjax( 'wp-compression-test' );
}
/**
* Set the 'can_compress_scripts' site option to true
*/
public function test_set_yes() {
// Become an administrator
$this->_setRole( 'administrator' );
// Set up a default request
$_GET['test'] = 'yes';
// Set the option to false
update_site_option( 'can_compress_scripts', 0 );
// Make the request
try {
$this->_handleAjax( 'wp-compression-test' );
} catch ( WPAjaxDieStopException $e ) {
unset( $e );
}
// Check the site option
$this->assertEquals( 1, get_site_option( 'can_compress_scripts' ) );
}
/**
* Set the 'can_compress_scripts' site option to false
*/
public function test_set_no() {
// Become an administrator
$this->_setRole( 'administrator' );
// Set up a default request
$_GET['test'] = 'no';
// Set the option to true
update_site_option( 'can_compress_scripts', 1 );
// Make the request
try {
$this->_handleAjax( 'wp-compression-test' );
} catch ( WPAjaxDieStopException $e ) {
unset( $e );
}
// Check the site option
$this->assertEquals( 0, get_site_option( 'can_compress_scripts' ) );
}
/**
* Undo gzencode. This is ugly, but there's no stock gzdecode() function.
* @param string $encoded_data
* @return string
*/
protected function _gzdecode( $encoded_data ) {
// Save the encoded data to a temp file
$file = wp_tempnam( 'gzdecode' );
file_put_contents( $file, $encoded_data );
// Flush it to the output buffer and delete the temp file
ob_start();
readgzfile( $file );
unlink( $file );
// Save the data stop buffering
$data = ob_get_clean();
ob_end_clean();
// Done
return $data;
}
}
|