/
hm-dev.phpunit.php
332 lines (249 loc) · 8.65 KB
/
hm-dev.phpunit.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
if ( ! @include_once('PHPUnit/Autoload.php') )
return;
class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
var $url = 'http://example.org/';
var $plugin_slug = null;
function _setUp() {
global $wpdb;
$wpdb->suppress_errors = false;
$wpdb->show_errors = true;
$wpdb->db_connect();
ini_set('display_errors', 1 );
$this->start_transaction();
add_filter( 'gp_get_option_uri', array( $this, 'url_filter') );
$this->activate_tested_plugin();
}
function activate_tested_plugin() {
if ( !$this->plugin_slug ) {
return;
}
require_once ABSPATH . '/wp-admin/includes/plugin.php';
if ( file_exists( WP_PLUGIN_DIR . '/' . $this->plugin_slug . '.php' ) )
activate_plugin( $this->plugin_slug . '.php' );
elseif ( file_exists( WP_PLUGIN_DIR . '/' . $this->plugin_slug . '/' . $this->plugin_slug . '.php' ) )
activate_plugin( $this->plugin_slug . '/' . $this->plugin_slug . '.php' );
else
throw new WP_Tests_Exception( "Couldn't find a plugin with slug $this->plugin_slug" );
}
function url_filter( $url ) {
return $this->url;
}
function _tearDown() {
global $wpdb;
$wpdb->query( 'ROLLBACK' );
remove_filter( 'gp_get_option_uri', array( $this, 'url_filter') );
}
function clean_up_global_scope() {
wp_cache_flush();
$_GET = array();
$_POST = array();
}
function start_transaction() {
global $wpdb;
$wpdb->query( 'SET autocommit = 0;' );
$wpdb->query( 'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE;' );
$wpdb->query( 'START TRANSACTION;' );
}
function force_innodb( $schema ) {
foreach( $schema as &$sql ) {
$sql = str_replace( ');', ') TYPE=InnoDB;', $sql );
}
return $schema;
}
function assertWPError( $actual, $message = '' ) {
$this->assertTrue( is_wp_error( $actual ), $message );
}
function assertNotWPError( $actual, $message = '' ) {
$this->assertNotInstanceOf( 'WP_Error', $actual, $message );
}
function assertEqualFields( $object, $fields ) {
foreach( $fields as $field_name => $field_value ) {
if ( $object->$field_name != $field_value ) {
$this->fail();
}
}
}
/**
* Assert that a zip archive contains the array
* of filenames
*
* @access public
* @param string path to zip file
* @param array of filenames to check for
* @return null
*/
function assertArchiveContains( $zip_file, $filepaths, $root = ABSPATH ) {
$extracted = $this->pclzip_extract_as_string( $zip_file );
$files = array();
foreach( $filepaths as $filepath )
$filenames[] = str_ireplace( trailingslashit( $root ), '', $filepath );
foreach( $extracted as $fileInfo )
$files[] = untrailingslashit( $fileInfo['filename'] );
foreach( $filenames as $filename )
$this->assertContains( untrailingslashit( $filename ), $files );
}
/**
* Assert that a zip archive doesn't contain any of the files
* in the array of filenames
*
* @access public
* @param string path to zip file
* @param array of filenames to check for
* @return null
*/
function assertArchiveNotContains( $zip_file, $filenames ) {
$extracted = $this->pclzip_extract_as_string( $zip_file );
$files = array();
foreach( $extracted as $fileInfo )
$files[] = $fileInfo['filename'];
foreach( $filenames as $filename )
$this->assertNotContains( $filename, $files );
}
/**
* Assert that a zip archive contains the
* correct number of files
*
* @access public
* @param string path to zip file
* @param int the number of files the archive should contain
* @return null
*/
function assertArchiveFileCount( $zip_file, $file_count ) {
$extracted = $this->pclzip_extract_as_string( $zip_file );
$this->assertEquals( count( array_filter( (array) $extracted ) ), $file_count );
}
function assertURL( $url, $message = '' ) {
$this->assertStringStartsWith( 'http', $url, $message );
}
function assertURLReponseCode( $url, $code, $message = '' ) {
$r = wp_remote_request( $url, array( 'timeout' => 30 ) );
$r_code = wp_remote_retrieve_response_code( $r );
$this->assertEquals( $code, $r_code );
}
function assertURLContains( $url, $pattern, $message = '' ) {
$r = wp_remote_request( $url, array( 'timeout' => 30 ) );
$body = wp_remote_retrieve_body( $r );
$this->assertContains( $pattern, $body, $message );
}
function assertDiscardWhitespace( $expected, $actual ) {
$this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual ) );
}
function assertImageRGBAtPoint( $image_path, $point, $color ) {
$im = imagecreatefrompng( $image_path );
$rgb = imagecolorat($im, $point[0], $point[1]);
$colors = imagecolorsforindex($im, $rgb);
$colors = array( $colors['red'], $colors['green'], $colors['blue'] );
$this->assertEquals( $colors, $color );
}
function assertImageAlphaAtPoint( $image_path, $point, $alpha ) {
$im = imagecreatefrompng( $image_path );
$rgb = imagecolorat($im, $point[0], $point[1]);
$colors = imagecolorsforindex($im, $rgb);
$this->assertEquals( $colors['alpha'], $alpha );
}
function checkAtLeastPHPVersion( $version ) {
if ( version_compare( PHP_VERSION, $version, '<' ) ) {
$this->markTestSkipped();
}
}
function go_to( $url ) {
// note: the WP and WP_Query classes like to silently fetch parameters
// from all over the place (globals, GET, etc), which makes it tricky
// to run them more than once without very carefully clearing everything
$_GET = $_POST = array();
$this->clean_up_global_scope();
foreach (array('query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow') as $v) {
if ( isset( $GLOBALS[$v] ) ) unset( $GLOBALS[$v] );
}
$parts = parse_url($url);
if (isset($parts['scheme'])) {
$req = $parts['path'];
if (isset($parts['query'])) {
$req .= '?' . $parts['query'];
// parse the url query vars into $_GET
parse_str($parts['query'], $_GET);
} else {
$parts['query'] = '';
}
}
else {
$req = $url;
}
$_SERVER['REQUEST_URI'] = $req;
unset($_SERVER['PATH_INFO']);
wp_cache_flush();
unset($GLOBALS['wp_query'], $GLOBALS['wp_the_query']);
$GLOBALS['wp_the_query'] = new WP_Query();
$GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
$GLOBALS['wp'] = new WP();
// clean out globals to stop them polluting wp and wp_query
foreach ($GLOBALS['wp']->public_query_vars as $v) {
unset($GLOBALS[$v]);
}
foreach ($GLOBALS['wp']->private_query_vars as $v) {
unset($GLOBALS[$v]);
}
$GLOBALS['wp']->main($parts['query']);
}
private function pclzip_extract_as_string( $zip_file ) {
require_once( ABSPATH . 'wp-admin/includes/class-pclzip.php' );
if ( ini_get( 'mbstring.func_overload' ) && function_exists( 'mb_internal_encoding' ) ) {
$previous_encoding = mb_internal_encoding();
mb_internal_encoding( 'ISO-8859-1' );
}
$archive = new PclZip( $zip_file );
$extracted = $archive->extract( PCLZIP_OPT_EXTRACT_AS_STRING );
if ( isset( $previous_encoding ) )
mb_internal_encoding( $previous_encoding );
return $extracted ?: array();
}
}
function hmdev_phpunit_load_all_test_files() {
if( ! empty( $_POST['tests'] ) ) {
foreach( (array) $_POST['tests'] as $test_file )
if( strpos( $test_file, '.php' ) ) {
include_once( WP_PLUGIN_DIR . '/' . $test_file );
} else {
$files = wptest_get_all_test_files( WP_PLUGIN_DIR . '/' . $test_file );
foreach ($files as $file) {
require_once($file);
}
}
} else {
$plugins = get_plugins();
foreach( $plugins as $plugin_path => $plugin ) {
if( is_plugin_active( $plugin_path ) ) {
foreach( get_plugin_files( $plugin_path ) as $file )
if( strpos( $file, '/tests/' ) && end( explode( '.', $file) ) == 'php' )
include_once( WP_PLUGIN_DIR . '/' . $file );
}
}
if ( is_dir( get_template_directory() . '/tests/' ) ) {
foreach ( glob( get_template_directory() . '/tests/*.php' ) as $file )
include_once( $file );
}
if ( is_dir( WPMU_PLUGIN_DIR ) ) {
foreach ( glob( WPMU_PLUGIN_DIR . '/*/tests/*.php' ) as $file ) {
include_once( $file );
}
}
}
}
function hmdev_phpunit_get_all_test_cases() {
$test_classes = array();
$all_classes = get_declared_classes();
// only classes that extend WPTestCase and have names that don't start with _ are included
foreach ($all_classes as $class)
if ($class{0} != '_' && hmdev_phpunit_test_is_descendent('PHPUnit_Framework_TestCase', $class) && $class != 'WP_UnitTestCase' )
$test_classes[] = $class;
return $test_classes;
}
function hmdev_phpunit_test_is_descendent($parent, $class) {
$ancestor = strtolower(get_parent_class($class));
while ($ancestor) {
if ($ancestor == strtolower($parent)) return true;
$ancestor = strtolower(get_parent_class($ancestor));
}
return false;
}