Skip to content
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.

Flexibly run tests #397

Merged
merged 7 commits into from
Aug 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@
language: php

php:
- "5.2"
- "5.3"
- "5.4"
- "5.5"
- 5.2
- 5.3
- 5.4
- 5.5

env:
- WP_VERSION=latest WP_MULTISITE=0
- WP_VERSION=latest WP_MULTISITE=1

# Clones WordPress and configures our testing environment.
before_script:
- export PLUGIN_SLUG=$(basename $(pwd))
- git clone --depth=1 git://develop.git.wordpress.org/ /tmp/wordpress
- mkdir "/tmp/wordpress/src/wp-content/plugins/$PLUGIN_SLUG"
- cp -r . "/tmp/wordpress/src/wp-content/plugins/$PLUGIN_SLUG/"
- cd /tmp/wordpress
- mysql -e "CREATE DATABASE wordpress_tests;" -uroot
- cp wp-tests-config-sample.php wp-tests-config.php
- sed -i "s/youremptytestdbnamehere/wordpress_tests/" wp-tests-config.php
- sed -i "s/yourusernamehere/travis/" wp-tests-config.php
- sed -i "s/yourpasswordhere//" wp-tests-config.php
- cd "/tmp/wordpress/src/wp-content/plugins/$PLUGIN_SLUG"
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION

script: phpunit
78 changes: 78 additions & 0 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env bash

if [ $# -lt 3 ]; then
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version]"
exit 1
fi

DB_NAME=$1
DB_USER=$2
DB_PASS=$3
DB_HOST=${4-localhost}
WP_VERSION=${5-latest}

WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib}
WP_CORE_DIR=/tmp/wordpress/

set -ex

install_wp() {
mkdir -p $WP_CORE_DIR

if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
else
local ARCHIVE_NAME="wordpress-$WP_VERSION"
fi

wget -nv -O /tmp/wordpress.tar.gz http://wordpress.org/${ARCHIVE_NAME}.tar.gz
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR

wget -nv -O $WP_CORE_DIR/wp-content/db.php https://raw.github.com/markoheijnen/wp-mysqli/master/db.php
}

install_test_suite() {
# portable in-place argument for both GNU sed and Mac OSX sed
if [[ $(uname -s) == 'Darwin' ]]; then
local ioption='-i .bak'
else
local ioption='-i'
fi

# set up testing suite
mkdir -p $WP_TESTS_DIR
cd $WP_TESTS_DIR
svn co --quiet http://develop.svn.wordpress.org/trunk/tests/phpunit/includes/

wget -nv -O wp-tests-config.php http://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" wp-tests-config.php
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" wp-tests-config.php
sed $ioption "s/yourusernamehere/$DB_USER/" wp-tests-config.php
sed $ioption "s/yourpasswordhere/$DB_PASS/" wp-tests-config.php
sed $ioption "s|localhost|${DB_HOST}|" wp-tests-config.php
}

install_db() {
# parse DB_HOST for port or socket references
local PARTS=(${DB_HOST//\:/ })
local DB_HOSTNAME=${PARTS[0]};
local DB_SOCK_OR_PORT=${PARTS[1]};
local EXTRA=""

if ! [ -z $DB_HOSTNAME ] ; then
if [[ "$DB_SOCK_OR_PORT" =~ ^[0-9]+$ ]] ; then
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
EXTRA=" --socket=$DB_SOCK_OR_PORT"
elif ! [ -z $DB_HOSTNAME ] ; then
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
fi
fi

# create database
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
}

install_wp
install_test_suite
install_db
38 changes: 22 additions & 16 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
<?php
/**
* Bootstrap the plugin unit testing environment.
*
* @package WordPress
* @subpackage JSON API
*/
* Bootstrap the plugin unit testing environment.
*
* @package WordPress
* @subpackage JSON API
*/

// Activates this plugin in WordPress so it can be tested.
$GLOBALS['wp_tests_options'] = array(
'active_plugins' => array( basename( dirname( dirname( __FILE__ ) ) ) . '/plugin.php' ),
);
// Support for:
// 1. `WP_DEVELOP_DIR` environment variable
// 2. Plugin installed inside of WordPress.org developer checkout
// 3. Tests checked out to /tmp
if( false !== getenv( 'WP_DEVELOP_DIR' ) ) {
$test_root = getenv( 'WP_DEVELOP_DIR' );
} else if ( file_exists( '../../../../tests/phpunit/includes/bootstrap.php' ) ) {
$test_root = '../../../..';
} else if ( file_exists( '/tmp/wordpress-tests-lib/includes/bootstrap.php' ) ) {
$test_root = '/tmp/wordpress-tests-lib';
}

// If the develop repo location is defined (as WP_DEVELOP_DIR), use that
// location. Otherwise, we'll just assume that this plugin is installed in a
// WordPress develop SVN checkout.
require $test_root . '/includes/functions.php';

if( false !== getenv( 'WP_DEVELOP_DIR' ) ) {
require getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit/includes/bootstrap.php';
} else {
require '../../../../tests/phpunit/includes/bootstrap.php';
function _manually_load_plugin() {
require dirname( __FILE__ ) . '/../plugin.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

require $test_root . '/includes/bootstrap.php';

// Helper classes
require_once dirname( __FILE__ ) . '/test-json-testcase.php';
3 changes: 1 addition & 2 deletions tests/test-json-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class WP_Test_JSON_Plugin extends WP_UnitTestCase {
* The plugin should be installed and activated.
*/
function test_plugin_activated() {
$directory = basename( dirname( dirname( __FILE__ ) ) );
$this->assertTrue( is_plugin_active( $directory . '/plugin.php' ) );
$this->assertTrue( class_exists( 'WP_JSON_Posts' ) );
}

/**
Expand Down
16 changes: 12 additions & 4 deletions tests/test-json-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public function setUp() {
$this->endpoint = new WP_JSON_Users( $this->fake_server );
}

protected function allow_user_to_create_users( $user ) {
if ( is_multisite() ) {
update_site_option( 'site_admins', array( $user->user_login ) );
} else {
$user->set_role( 'administrator' );
}
}

public function test_get_current_user() {
$response = $this->endpoint->get_current_user();
$this->assertNotInstanceOf( 'WP_Error', $response );
Expand Down Expand Up @@ -85,7 +93,7 @@ protected function check_get_user_response( $response, $user_obj, $context = 'vi
}

public function test_create_user() {
$this->user_obj->set_role( 'administrator' );
$this->allow_user_to_create_users( $this->user_obj );
$data = array(
'username' => 'test_user',
'password' => 'test_password',
Expand All @@ -112,7 +120,7 @@ public function test_create_user() {
}

public function test_create_user_missing_params() {
$this->user_obj->set_role( 'administrator' );
$this->allow_user_to_create_users( $this->user_obj );
$data = array(
'username' => 'test_user',
);
Expand All @@ -121,7 +129,7 @@ public function test_create_user_missing_params() {
}

public function test_delete_user() {
$this->user_obj->set_role( 'administrator' );
$this->allow_user_to_create_users( $this->user_obj );

// Test with a new user, rather than ourselves, to avoid any
// complications with doing so. We should check this separately though.
Expand All @@ -139,7 +147,7 @@ public function test_delete_user() {
}

public function test_delete_user_reassign() {
$this->user_obj->set_role( 'administrator' );
$this->allow_user_to_create_users( $this->user_obj );

// Test with a new user, rather than ourselves, to avoid any
// complications with doing so. We should check this separately though.
Expand Down