Skip to content

Commit

Permalink
Merge pull request #178 from Automattic/basic-tests
Browse files Browse the repository at this point in the history
Unit tests scaffold
  • Loading branch information
danielbachhuber committed Mar 7, 2014
2 parents 1b55132 + e0172e7 commit 1d89694
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 7 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
@@ -0,0 +1,15 @@
language: php

php:
- 5.3
- 5.5

env:
- WP_VERSION=latest WP_MULTISITE=0
- WP_VERSION=3.8 WP_MULTISITE=0
- WP_VERSION=3.3 WP_MULTISITE=0

before_script:
- 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
@@ -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
25 changes: 18 additions & 7 deletions co-authors-plus.php
Expand Up @@ -744,26 +744,37 @@ function has_author_terms( $post_id ) {

/**
* Add one or more co-authors as bylines for a post
*
* @param int
* @param array
* @param bool
*/
function add_coauthors( $post_id, $coauthors, $append = false ) {
public function add_coauthors( $post_id, $coauthors, $append = false ) {
global $current_user;

$post_id = (int) $post_id;
$insert = false;

// if an array isn't returned, create one and populate with default author
if ( !is_array( $coauthors ) || 0 == count( $coauthors ) || empty( $coauthors ) ) {
// Best way to persist order
if ( $append ) {
$existing_coauthors = wp_list_pluck( get_coauthors( $post_id ), 'user_login' );
} else {
$existing_coauthors = array();
}

// A co-author is always required
if ( empty( $coauthors ) ) {
$coauthors = array( $current_user->user_login );
}

// Add each co-author to the post meta
foreach( array_unique( $coauthors ) as $key => $author_name ){
$coauthors = array_unique( array_merge( $existing_coauthors, $coauthors ) );
foreach( $coauthors as &$author_name ){

$author = $this->get_coauthor_by( 'user_nicename', $author_name );
$term = $this->update_author_term( $author );
$coauthors[$key] = $term->slug;
$author_name = $term->slug;
}
return wp_set_post_terms( $post_id, $coauthors, $this->coauthor_taxonomy, $append );
return wp_set_post_terms( $post_id, $coauthors, $this->coauthor_taxonomy, false );
}

/**
Expand Down
14 changes: 14 additions & 0 deletions phpunit.xml
@@ -0,0 +1,14 @@
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
1 change: 1 addition & 0 deletions readme.txt
Expand Up @@ -58,6 +58,7 @@ Bug fixes and minor enhancements
* Manage co-authors from Quick Edit. Props [mpatek](https://github.com/mpatek).
* Updated Spanish translation, courtesy of [sergiomajluf](https://github.com/sergiomajluf).
* Packages a composer.json file for those using Composer.
* Beginnings of unit test coverage for core features.

= 3.0.7 (Jan. 27, 2014) =
* Better support for installing Co-Authors Plus as a symlinked directory. [Follow these instructions](http://kaspars.net/blog/wordpress/plugins-via-symlinks) to filter `plugins_url`.
Expand Down
15 changes: 15 additions & 0 deletions tests/bootstrap.php
@@ -0,0 +1,15 @@
<?php

$_tests_dir = getenv('WP_TESTS_DIR');
if ( !$_tests_dir ) $_tests_dir = '/tmp/wordpress-tests-lib';

require_once $_tests_dir . '/includes/functions.php';

function _manually_load_plugin() {
require dirname( __FILE__ ) . '/../co-authors-plus.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

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

require dirname( __FILE__ ) . '/coauthorsplus-testcase.php';
25 changes: 25 additions & 0 deletions tests/coauthorsplus-testcase.php
@@ -0,0 +1,25 @@
<?php

/**
* Base unit test class for Co-Authors Plus
*/

class CoAuthorsPlus_TestCase extends WP_UnitTestCase {

public function setUp() {
parent::setUp();

$this->author1 = $this->factory->user->create( array( 'role' => 'author', 'user_login' => 'author1' ) );
$this->editor1 = $this->factory->user->create( array( 'role' => 'editor', 'user_login' => 'author2' ) );

$post = array(
'post_author' => $this->author1,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
);

$this->author1_post1 = wp_insert_post( $post );
}

}
28 changes: 28 additions & 0 deletions tests/test-manage-coauthors.php
@@ -0,0 +1,28 @@
<?php

class Test_Manage_CoAuthors extends CoAuthorsPlus_TestCase {

/**
* Test assigning a Co-Author to a post
*/
public function test_add_coauthor_to_post() {
global $coauthors_plus;

$coauthors = get_coauthors( $this->author1_post1 );
$this->assertEquals( 1, count( $coauthors ) );

// append = true, should preserve order
$editor1 = get_user_by( 'id', $this->editor1 );
$coauthors_plus->add_coauthors( $this->author1_post1, array( $editor1->user_login ), true );
$coauthors = get_coauthors( $this->author1_post1 );
$this->assertEquals( array( $this->author1, $this->editor1 ), wp_list_pluck( $coauthors, 'ID' ) );

// append = false, overrides existing authors
$coauthors_plus->add_coauthors( $this->author1_post1, array( $editor1->user_login ), false );
$coauthors = get_coauthors( $this->author1_post1 );
$this->assertEquals( array( $this->editor1 ), wp_list_pluck( $coauthors, 'ID' ) );

}


}

0 comments on commit 1d89694

Please sign in to comment.