Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 3 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jun 08, 2012
Gregory Cornelius Change how tests are bootstrapped and plugins are activated.
Always install fresh.
Remove the SHORTINT / eval() hack and instead activate plugins during install.
762b5b2
Gregory Cornelius Use is_subclass_of() to find tests. df94b1c
Gregory Cornelius Discover tests in wp-tests directory of all active plugins. 2e42960
View
5 all.php
@@ -14,8 +14,9 @@
class all {
public static function suite() {
$suite = new PHPUnit_Framework_TestSuite();
+
foreach( get_declared_classes() as $class ) {
- if ( preg_match( '/^WP_Test_/', $class ) ) {
+ if ( is_subclass_of( $class, 'WP_UnitTestCase' ) ) {
$suite->addTestSuite( $class );
}
}
@@ -23,4 +24,4 @@ public static function suite() {
}
}
-chdir( $old_cwd );
+chdir( $old_cwd );
View
26 bin/install.php
@@ -9,7 +9,8 @@
$config_file_path = $argv[1];
define( 'WP_INSTALLING', true );
-require_once $config_file_path;
+require_once $config_file_path . '/unittests-config.php';
+require_once $config_file_path . '/lib/functions.php';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['HTTP_HOST'] = WP_TESTS_DOMAIN;
@@ -20,25 +21,15 @@
require_once ABSPATH . '/wp-admin/includes/upgrade.php';
require_once ABSPATH . '/wp-includes/wp-db.php';
-define( 'WP_TESTS_DB_VERSION_FILE', ABSPATH . '.wp-tests-db-version' );
-
$wpdb->suppress_errors();
$wpdb->hide_errors();
-$installed = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'" );
-if ( $installed && file_exists( WP_TESTS_DB_VERSION_FILE ) ) {
- $install_db_version = file_get_contents( WP_TESTS_DB_VERSION_FILE );
- $db_version = get_option( 'db_version' );
- if ( $db_version == $install_db_version ) {
- return;
- }
-}
$wpdb->query( 'SET storage_engine = INNODB;' );
$wpdb->query( 'DROP DATABASE IF EXISTS '.DB_NAME.";" );
$wpdb->query( 'CREATE DATABASE '.DB_NAME.";" );
$wpdb->select( DB_NAME, $wpdb->dbh );
-echo "Installing\n";
+echo "Installing WordPress...\n";
wp_install( WP_TESTS_TITLE, 'admin', WP_TESTS_EMAIL, true, '', 'a' );
if ( defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE ) {
@@ -53,7 +44,14 @@
$result = populate_network(1, WP_TESTS_DOMAIN, WP_TESTS_EMAIL, WP_TESTS_NETWORK_TITLE, ABSPATH, WP_TESTS_SUBDOMAIN_INSTALL);
system( 'php '.escapeshellarg( dirname( __FILE__ ) . '/ms-install.php' ) . ' ' . escapeshellarg( $config_file_path ) );
-
+
+ if ( isset( $wp_test_ms_plugins ) && is_array( $wp_test_ms_plugins ) ) {
+ echo "Installing network plugins...\n";
+ wptest_install_plugins($wp_test_ms_plugins);
+ }
}
-file_put_contents( WP_TESTS_DB_VERSION_FILE, get_option('db_version') );
+if ( isset( $wp_tests_plugins ) && is_array( $wp_tests_plugins ) ) {
+ echo "Installing site plugins...\n";
+ wptest_install_plugins($wp_tests_plugins);
+}
View
38 init.php
@@ -7,7 +7,11 @@
require_once 'PHPUnit/Autoload.php';
-$config_file_path = dirname( __FILE__ ) . '/unittests-config.php';
+define('WP_TESTS_PATH', dirname( __FILE__ ) );
+
+require WP_TESTS_PATH . '/lib/functions.php';
+
+$config_file_path = WP_TESTS_PATH . '/unittests-config.php';
/*
* Globalize some WordPress variables, because PHPUnit loads this file inside a function
@@ -26,33 +30,13 @@
$_SERVER['HTTP_HOST'] = WP_TESTS_DOMAIN;
$PHP_SELF = $GLOBALS['PHP_SELF'] = $_SERVER['PHP_SELF'] = '/index.php';
-system( 'php '.escapeshellarg( dirname( __FILE__ ) . '/bin/install.php' ) . ' ' . escapeshellarg( $config_file_path ) );
-
-// Stop most of WordPress from being loaded.
-define('SHORTINIT', true);
+// install WordPress
+system( 'php '.escapeshellarg( WP_TESTS_PATH . '/bin/install.php' ) . ' ' . escapeshellarg( WP_TESTS_PATH ) );
// Load the basics part of WordPress.
require_once ABSPATH . '/wp-settings.php';
-// Preset WordPress options defined in bootstarp file.
-// Used to activate theme and plugins.
-if(isset($GLOBALS['wp_tests_options'])) {
- function wp_tests_options( $value ) {
- $key = substr( current_filter(), strlen( 'pre_option_' ) );
- return $GLOBALS['wp_tests_options'][$key];
- }
-
- foreach ( array_keys( $GLOBALS['wp_tests_options'] ) as $key ) {
- add_filter( 'pre_option_'.$key, 'wp_tests_options' );
- }
-}
-
-// Load the rest of wp-settings.php, start from where we left off.
-$wp_settings_content = file_get_contents(ABSPATH.'/wp-settings.php');
-$shortinit_phrase = "if ( SHORTINIT )\n\treturn false;\n";
-$offset = strpos($wp_settings_content, $shortinit_phrase)+strlen($shortinit_phrase);
-eval(substr($wp_settings_content, $offset));
-unset($wp_settings_content, $offset, $shortinit_phrase);
-
-require dirname( __FILE__ ) . '/lib/testcase.php';
-require dirname( __FILE__ ) . '/lib/exceptions.php';
+require WP_TESTS_PATH . '/lib/testcase.php';
+require WP_TESTS_PATH . '/lib/exceptions.php';
+
+wp_test_load_plugin_tests();
View
61 lib/functions.php
@@ -0,0 +1,61 @@
+<?php
+
+function wptest_install_plugins($plugins) {
+ foreach($plugins as $plugin) {
+ activate_plugin($plugin);
+ }
+}
+
+
+function wp_test_load_plugin_tests() {
+ $plugins = wp_get_active_and_valid_plugins();
+ if(is_multisite()) {
+ $plugins = array_merge($plugins, wp_get_active_network_plugins());
+ }
+ foreach($plugins as $plugin) {
+ $dirname = dirname($plugin);
+ if($dirname != WP_PLUGIN_DIR) {
+ wp_test_load_tests($dirname);
+ }
+ }
+}
+
+/**
+ * Load theme tests.
+ **/
+function wptest_load_theme_tests() {
+ // unfinished, obviously.
+}
+
+/**
+ * Load scripts that should contain test cases from a directory inside of $dir
+ * @todo support an array of possible test directories
+ *
+ * @param string $dir
+ * @param string $test_dir
+ **/
+function wp_test_load_tests( $dir, $test_dirs = array('wp-tests'), $starts_with = 'test_') {
+ if(!is_dir($dir) || $dir[0] == '.') return false;
+
+ foreach($test_dirs as $test_dir) {
+ if($test_dir[0] == '.') continue;
+
+ $tests_path = realpath(rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $test_dir);
+
+ if($dh = opendir($tests_path)) {
+
+ // read through the test directory looking for tests
+ while (($file = readdir($dh)) !== false) {
+ $path = realpath($tests_path . DIRECTORY_SEPARATOR . $file);
+
+ $fileparts = pathinfo($file);
+
+ // add .php files starting with 'test'
+ if (is_file($path) && strpos($fileparts['basename'], $starts_with) === 0 && $fileparts['extension'] == 'php') {
+ require_once $path;
+ }
+ }
+ closedir($dh);
+ }
+ }
+}
View
12 test_hello.php
@@ -0,0 +1,12 @@
+<?php
+
+class WP_Test_Hello_Dolly extends WP_UnitTestCase {
+
+ function test_hello_dolly() {
+ ob_start();
+ hello_dolly();
+ $hello_dolly_output = ob_get_clean();
+ $this->assertContains( "id='dolly'", $hello_dolly_output );
+ }
+}
+?>
View
15 unittests-config-sample.php
@@ -20,6 +20,21 @@
define( 'WP_TESTS_SUBDOMAIN_INSTALL', true );
$base = '/';
+define( 'WP_TESTS_STYLESHEET', '');
+define( 'WP_TESTS_TEMPLATE', '');
+
+
+// Plugins to activate on install. Plugins will be activated on all sites in a
+// MultiSite install.
+$wp_tests_plugins = array(
+ 'hello.php',
+);
+
+// Plugins to network activate.
+$wp_tests_ms_plugins = array(
+
+);
+
/* Cron tries to make an HTTP request to the blog, which always fails, because tests are run in CLI mode only */
define( 'DISABLE_WP_CRON', true );

No commit comments for this range

Something went wrong with that request. Please try again.