Skip to content

Commit 6cab5e4

Browse files
committed
Add single ClassicPress Unit Test
1 parent 1f4f887 commit 6cab5e4

File tree

5 files changed

+321
-1
lines changed

5 files changed

+321
-1
lines changed

.github/workflows/cp-phpunit.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: ClassicPress Unit Testing
2+
on:
3+
push:
4+
pull_request:
5+
jobs:
6+
phpunit:
7+
runs-on: ubuntu-latest
8+
services:
9+
mysql:
10+
image: mariadb:10.4
11+
env:
12+
MYSQL_ROOT_PASSWORD: root
13+
ports:
14+
- 3306:3306
15+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=10s --health-retries=10
16+
strategy:
17+
matrix:
18+
php-versions: ['7.4']
19+
include:
20+
- cp-version: latest
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v3
24+
- name: Setup PHP
25+
uses: shivammathur/setup-php@v2
26+
with:
27+
php-version: ${{ matrix.php-versions }}
28+
coverage: none
29+
tools: composer, phpunit-polyfills
30+
extensions: mysql
31+
- name: Install Composer dependencies for PHP
32+
uses: "ramsey/composer-install@v2"
33+
- name: Setup Test Environment
34+
run: bash ./bin/install-cp-tests.sh cpc_test root root 127.0.0.1 ${{ matrix.cp-version }}
35+
- name: Unit Testing
36+
run: ./vendor/bin/phpunit
37+
env:
38+
PHP_VERSION: ${{ matrix.php-versions }}

.github/workflows/phpunit.yml renamed to .github/workflows/wp-phpunit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Unit Testing
1+
name: WordPress Unit Testing
22
on:
33
push:
44
pull_request:

bin/install-cp-tests.sh

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
#!/usr/bin/env bash
2+
# See https://raw.githubusercontent.com/wp-cli/scaffold-command/master/templates/install-wp-tests.sh
3+
4+
if [ $# -lt 3 ]; then
5+
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [cp-version] [skip-database-creation]"
6+
exit 1
7+
fi
8+
9+
DB_NAME=$1
10+
DB_USER=$2
11+
DB_PASS=$3
12+
DB_HOST=${4-localhost}
13+
CP_VERSION=${5-latest}
14+
SKIP_DB_CREATE=${6-false}
15+
16+
TMPDIR=${TMPDIR-/tmp}
17+
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
18+
CP_TESTS_DIR=${CP_TESTS_DIR-$TMPDIR/classicpress-tests-lib}
19+
CP_CORE_DIR=${CP_CORE_DIR-$TMPDIR/classicpress}
20+
21+
# Remove trailing slashes
22+
CP_TESTS_DIR=$(echo "$CP_TESTS_DIR" | sed 's:/\+$::')
23+
CP_CORE_DIR=$(echo "$CP_CORE_DIR" | sed 's:/\+$::')
24+
25+
download() {
26+
if [ `which curl` ]; then
27+
curl -L -s "$1" -o "$2"
28+
elif [ `which wget` ]; then
29+
wget -nv -O "$2" "$1"
30+
fi
31+
}
32+
33+
set -ex
34+
35+
# $CP_VERSION may be one of the following:
36+
# 'latest' - latest stable release
37+
# '1.2.3' or '1.2.3-rc1' etc - any released version number
38+
# 'git+abc123' - use the specific commit 'abc123' from the *development* repo
39+
40+
CP_RELEASE=y
41+
if [[ "$CP_VERSION" == latest ]]; then
42+
# Find the version number of the latest release
43+
download \
44+
https://api.github.com/repos/ClassicPress/ClassicPress-release/releases/latest \
45+
"$TMPDIR/cp-latest.json"
46+
CP_VERSION="$(grep -Po '"tag_name":\s*"[^"]+"' "$TMPDIR/cp-latest.json" | cut -d'"' -f4)"
47+
48+
if [ -z "$CP_VERSION" ]; then
49+
echo "ClassicPress version not detected correctly!"
50+
cat "$TMPDIR/cp-latest.json"
51+
exit 1
52+
fi
53+
elif [[ "$CP_VERSION" = git-* ]]; then
54+
# Use a specific commit from the development repo
55+
CP_RELEASE=n
56+
CP_VERSION=${CP_VERSION#git-}
57+
elif ! [[ "$CP_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-|$) ]]; then
58+
# Anything else needs to be a release version number
59+
echo "ClassicPress version number not supported: $CP_VERSION"
60+
exit 1
61+
fi
62+
63+
if [ $CP_RELEASE = y ]; then
64+
# Remote URLs for release and dev packages/files
65+
CP_BUILD_ZIP_URL="https://github.com/ClassicPress/ClassicPress-release/archive/$CP_VERSION.zip"
66+
CP_DEV_ZIP_URL="https://github.com/ClassicPress/ClassicPress/archive/$CP_VERSION+dev.zip"
67+
CP_DEV_FILE_URL="https://raw.githubusercontent.com/ClassicPress/ClassicPress/$CP_VERSION+dev"
68+
# Local paths
69+
CP_BUILD_ZIP_PATH="$TMPDIR/classicpress-release-$CP_VERSION.zip"
70+
CP_DEV_ZIP_PATH="$TMPDIR/classicpress-dev-$CP_VERSION.zip"
71+
CP_DEV_PATH="$TMPDIR/classicpress-dev-$CP_VERSION"
72+
else
73+
# Remote URLs for dev packages/files (no release build)
74+
CP_DEV_ZIP_URL="https://github.com/ClassicPress/ClassicPress/archive/$CP_VERSION.zip"
75+
CP_DEV_FILE_URL="https://raw.githubusercontent.com/ClassicPress/ClassicPress/$CP_VERSION"
76+
# Local paths
77+
CP_DEV_ZIP_PATH="$TMPDIR/classicpress-dev-$CP_VERSION.zip"
78+
CP_DEV_PATH="$TMPDIR/classicpress-dev-$CP_VERSION"
79+
fi
80+
81+
install_cp() {
82+
if [ -d $CP_CORE_DIR ]; then
83+
return;
84+
fi
85+
86+
mkdir -p $CP_CORE_DIR
87+
88+
if [ $CP_RELEASE = y ]; then
89+
download "$CP_BUILD_ZIP_URL" "$CP_BUILD_ZIP_PATH"
90+
unzip -q "$CP_BUILD_ZIP_PATH" -d "$CP_CORE_DIR"
91+
else
92+
download "$CP_DEV_ZIP_URL" "$CP_DEV_ZIP_PATH"
93+
unzip -q "$CP_DEV_ZIP_PATH" -d "$CP_CORE_DIR"
94+
fi
95+
clean_github_download "$CP_CORE_DIR" true
96+
97+
download \
98+
https://raw.github.com/markoheijnen/wp-mysqli/master/db.php \
99+
"$CP_CORE_DIR/wp-content/db.php"
100+
101+
# Hello Dolly is still used in some tests.
102+
download \
103+
"$CP_DEV_FILE_URL/src/wp-content/plugins/hello.php" \
104+
"$CP_CORE_DIR/wp-content/plugins/hello.php"
105+
}
106+
107+
clean_github_download() {
108+
# GitHub downloads extract with a single folder inside, named based on the
109+
# version downloaded. Get rid of this.
110+
dir="$1"
111+
remove_src_dir="$2"
112+
mv "$dir" "$dir-old"
113+
mv "$dir-old/ClassicPress-"* "$dir"
114+
rmdir "$dir-old"
115+
if [ -d "$dir/src" ] && [ "$remove_src_dir" = true ]; then
116+
# Development build - get rid of the 'src' directory too.
117+
mv "$dir" "$dir-old"
118+
mv "$dir-old/src" "$dir"
119+
rm -rf "$dir-old"
120+
fi
121+
}
122+
123+
install_test_suite() {
124+
# portable in-place argument for both GNU sed and Mac OSX sed
125+
if [[ $(uname -s) == 'Darwin' ]]; then
126+
local ioption='-i .bak'
127+
else
128+
local ioption='-i'
129+
fi
130+
131+
# set up testing suite if it doesn't yet exist
132+
if [ ! -d "$CP_TESTS_DIR" ]; then
133+
mkdir -p "$CP_TESTS_DIR"
134+
download "$CP_DEV_ZIP_URL" "$CP_DEV_ZIP_PATH"
135+
unzip -q "$CP_DEV_ZIP_PATH" -d "$CP_DEV_PATH"
136+
clean_github_download "$CP_DEV_PATH" false
137+
cp -ar \
138+
"$CP_DEV_PATH/tests/phpunit/includes" \
139+
"$CP_DEV_PATH/tests/phpunit/data" \
140+
"$CP_TESTS_DIR/"
141+
fi
142+
143+
if [ ! -f wp-tests-config.php ]; then
144+
download \
145+
"$CP_DEV_FILE_URL/wp-tests-config-sample.php" \
146+
"$CP_TESTS_DIR/wp-tests-config.php"
147+
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$CP_CORE_DIR/':" "$CP_TESTS_DIR"/wp-tests-config.php
148+
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$CP_TESTS_DIR"/wp-tests-config.php
149+
sed $ioption "s/yourusernamehere/$DB_USER/" "$CP_TESTS_DIR"/wp-tests-config.php
150+
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$CP_TESTS_DIR"/wp-tests-config.php
151+
sed $ioption "s|localhost|${DB_HOST}|" "$CP_TESTS_DIR"/wp-tests-config.php
152+
fi
153+
154+
}
155+
156+
install_db() {
157+
158+
if [ ${SKIP_DB_CREATE} = "true" ]; then
159+
return 0
160+
fi
161+
162+
# parse DB_HOST for port or socket references
163+
local PARTS=(${DB_HOST//\:/ })
164+
local DB_HOSTNAME=${PARTS[0]};
165+
local DB_SOCK_OR_PORT=${PARTS[1]};
166+
local EXTRA=""
167+
168+
if ! [ -z $DB_HOSTNAME ] ; then
169+
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
170+
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
171+
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
172+
EXTRA=" --socket=$DB_SOCK_OR_PORT"
173+
elif ! [ -z $DB_HOSTNAME ] ; then
174+
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
175+
fi
176+
fi
177+
178+
# create database
179+
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
180+
}
181+
182+
install_e2e_site() {
183+
184+
if [[ ${RUN_E2E} == 1 ]]; then
185+
186+
# Script Variables
187+
CONFIG_DIR="./tests/e2e-tests/config/travis"
188+
CP_CORE_DIR="$HOME/classicpress"
189+
CC_PLUGIN_DIR="$CP_CORE_DIR/wp-content/plugins/classic-commerce"
190+
NGINX_DIR="$HOME/nginx"
191+
PHP_FPM_BIN="$HOME/.phpenv/versions/$TRAVIS_PHP_VERSION/sbin/php-fpm"
192+
PHP_FPM_CONF="$NGINX_DIR/php-fpm.conf"
193+
CP_SITE_URL="http://localhost:8080"
194+
BRANCH=$TRAVIS_BRANCH
195+
REPO=$TRAVIS_REPO_SLUG
196+
CP_DB_DATA="$HOME/build/$REPO/tests/e2e-tests/data/e2e-db.sql"
197+
WORKING_DIR="$PWD"
198+
199+
if [ "$TRAVIS_PULL_REQUEST_BRANCH" != "" ]; then
200+
BRANCH=$TRAVIS_PULL_REQUEST_BRANCH
201+
REPO=$TRAVIS_PULL_REQUEST_SLUG
202+
fi
203+
204+
set -ev
205+
npm install
206+
export NODE_CONFIG_DIR="./tests/e2e-tests/config"
207+
208+
# Set up nginx to run the server
209+
mkdir -p "$CP_CORE_DIR"
210+
mkdir -p "$NGINX_DIR"
211+
mkdir -p "$NGINX_DIR/sites-enabled"
212+
mkdir -p "$NGINX_DIR/var"
213+
214+
cp "$CONFIG_DIR/travis_php-fpm.conf" "$PHP_FPM_CONF"
215+
216+
# Start php-fpm
217+
"$PHP_FPM_BIN" --fpm-config "$PHP_FPM_CONF"
218+
219+
# Copy the default nginx config files.
220+
cp "$CONFIG_DIR/travis_nginx.conf" "$NGINX_DIR/nginx.conf"
221+
cp "$CONFIG_DIR/travis_fastcgi.conf" "$NGINX_DIR/fastcgi.conf"
222+
cp "$CONFIG_DIR/travis_default-site.conf" "$NGINX_DIR/sites-enabled/default-site.conf"
223+
224+
# Start nginx.
225+
nginx -c "$NGINX_DIR/nginx.conf"
226+
227+
# Set up ClassicPress using wp-cli
228+
cd "$CP_CORE_DIR"
229+
230+
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
231+
# Note, `wp core download` does not work when running the tests with a
232+
# development version of ClassicPress, so we'll substitute the latest
233+
# build instead!
234+
if [ -z "$CP_BUILD_ZIP_URL" ]; then
235+
CP_BUILD_ZIP_URL="https://www.classicpress.net/latest.zip"
236+
fi
237+
php wp-cli.phar core download "$CP_BUILD_ZIP_URL"
238+
php wp-cli.phar core config --dbname=$DB_NAME --dbuser=$DB_USER --dbpass=$DB_PASS --dbhost=$DB_HOST --dbprefix=wp_ --extra-php <<PHP
239+
/* Change WP_MEMORY_LIMIT to increase the memory limit for public pages. */
240+
define('WP_MEMORY_LIMIT', '256M');
241+
define('SCRIPT_DEBUG', true);
242+
PHP
243+
php wp-cli.phar core install --url="$CP_SITE_URL" --title="Example" --admin_user=admin --admin_password=password [email protected] --path=$CP_CORE_DIR --skip-email
244+
php wp-cli.phar db import $CP_DB_DATA
245+
php wp-cli.phar search-replace "http://local.wordpress.test" "$CP_SITE_URL"
246+
php wp-cli.phar theme install twentytwelve --activate
247+
248+
# Instead of installing WC from a GH zip, rather used the checked out branch?
249+
# php wp-cli.phar plugin install https://github.com/$REPO/archive/$BRANCH.zip --activate
250+
echo "CREATING Classic Commerce PLUGIN DIR AT $CC_PLUGIN_DIR"
251+
mkdir $CC_PLUGIN_DIR
252+
echo "COPYING CHECKED OUT BRANCH TO $CC_PLUGIN_DIR"
253+
cp -R "$TRAVIS_BUILD_DIR" "$CP_CORE_DIR/wp-content/plugins/"
254+
ls "$CP_CORE_DIR/wp-content/plugins/classic-commerce/"
255+
256+
# Compile assets and installing dependencies
257+
echo "COMPILING ASSETS IN $CC_PLUGIN_DIR"
258+
cd $CC_PLUGIN_DIR
259+
npm install
260+
composer install
261+
grunt e2e-build
262+
263+
echo "ACTIVATING Classic Commerce PLUGIN"
264+
php wp-cli.phar plugin activate classic-commerce
265+
echo "RUNNING Classic Commerce UPDATE ROUTINE"
266+
php wp-cli.phar wc update
267+
268+
echo "DONE INSTALLING E2E SUITE."
269+
cd "$WORKING_DIR"
270+
echo "WORKING DIR: $WORKING_DIR"
271+
fi
272+
}
273+
274+
install_cp
275+
install_test_suite
276+
install_db
277+
install_e2e_site

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"lint": "./vendor/bin/phpcs -n -q",
3131
"phpunit": "./vendor/bin/phpunit",
3232
"setup-local-tests": "bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 latest",
33+
"setup-cp-tests": "bash bin/install-cp-tests.sh cpc_test root root 127.0.0.1 latest",
3334
"test": [
3435
"composer install",
3536
"bin/install-wp-tests.sh webmention-test root webmention-test test-db latest true",

tests/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
1919
}
2020

21+
if ( ! is_dir( $_tests_dir ) ) {
22+
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/classicpress-tests-lib';
23+
}
24+
2125
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
2226
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2327
exit( 1 );

0 commit comments

Comments
 (0)