Skip to content

Commit

Permalink
First pass at some tests for AMP_Img_Converter
Browse files Browse the repository at this point in the history
Some tests fail :)
  • Loading branch information
mjangda committed Sep 30, 2015
1 parent daa4757 commit cac5e1f
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
language: php

notifications:
on_success: never
on_failure: change

php:
- 5.3
- 5.5

env:
- WP_VERSION=latest WP_MULTISITE=0

matrix:
include:
- php: 5.3
env: WP_VERSION=latest WP_MULTISITE=1

before_script:
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION

script: phpunit
98 changes: 98 additions & 0 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/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/includes}
WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/}

set -ex

download() {
if [ `which curl` ]; then
curl -s "$1" > "$2";
elif [ `which wget` ]; then
wget -nv -O "$2" "$1"
fi
}

install_wp() {

if [ -d $WP_CORE_DIR ]; then
return;
fi

mkdir -p $WP_CORE_DIR

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

download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR

download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/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 if it doesn't yet exist
if [ ! "$(ls -A $WP_TESTS_DIR)" ]; then
# set up testing suite
mkdir -p $WP_TESTS_DIR
svn co --quiet http://develop.svn.wordpress.org/trunk/tests/phpunit/includes/ $WP_TESTS_DIR
fi

cd $WP_TESTS_DIR

if [ ! -f wp-tests-config.php ]; then
download https://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php $(dirname ${WP_TESTS_DIR})/wp-tests-config.php
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" $(dirname ${WP_TESTS_DIR})/wp-tests-config.php
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" $(dirname ${WP_TESTS_DIR})/wp-tests-config.php
sed $ioption "s/yourusernamehere/$DB_USER/" $(dirname ${WP_TESTS_DIR})/wp-tests-config.php
sed $ioption "s/yourpasswordhere/$DB_PASS/" $(dirname ${WP_TESTS_DIR})/wp-tests-config.php
sed $ioption "s|localhost|${DB_HOST}|" $(dirname ${WP_TESTS_DIR})/wp-tests-config.php
fi

}

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 [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; 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
14 changes: 14 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -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>
15 changes: 15 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -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( dirname( __FILE__ ) ) . '/amp.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

require $_tests_dir . '/includes/bootstrap.php';
44 changes: 44 additions & 0 deletions tests/test-amp-img-converter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

class AMP_Img_Converter_Test extends WP_UnitTestCase {
private $converter;

function setUp() {
$this->converter = new AMP_Img_Converter;
}

function test_no_images() {
$content = '<p>Lorem Ipsum Demet Delorit.</p>';
$converted = $this->converter->convert( $content );
$this->assertEquals( $content, $converted );
}

function test_image_with_self_closing_tag() {
$content = '<img src="http://placehold.it/350x150" width="350" height="150" alt="Placeholder!" />';
$expected = '<amp-img src="http://placehold.it/350x150" width="350" height="150" alt="Placeholder!"></amp-img>';
$converted = $this->converter->convert( $content );
$this->assertEquals( $expected, $converted );
}

function test_image_with_no_end_tag() {
$content = '<img src="http://placehold.it/350x150" width="350" height="150" alt="Placeholder!">';
$expected = '<amp-img src="http://placehold.it/350x150" width="350" height="150" alt="Placeholder!"></amp-img>';
$converted = $this->converter->convert( $content );
$this->assertEquals( $expected, $converted );
}

function test_image_with_end_tag() {
$content = '<img src="http://placehold.it/350x150" width="350" height="150" alt="Placeholder!"></img>';
$expected = '<amp-img src="http://placehold.it/350x150" width="350" height="150" alt="Placeholder!"></amp-img>';
$converted = $this->converter->convert( $content );
$this->assertEquals( $expected, $converted );
}

function test_image_with_blacklisted_attribute() {
$content = '<img src="http://placehold.it/350x150" style="border: 1px solid red;" />';
$expected = '<amp-img src="http://placehold.it/350x150"></amp-img>';
$converted = $this->converter->convert( $content );
$this->assertEquals( $expected, $converted );
}
}

0 comments on commit cac5e1f

Please sign in to comment.