From 1656c1a74ab289f4792fc2ec8088cafda64f3977 Mon Sep 17 00:00:00 2001 From: David Shanske Date: Fri, 3 Jan 2025 04:43:48 +0000 Subject: [PATCH] Add CP Unit Testing and updated plugin header and readme header --- .github/workflows/cp-phpunit.yml | 42 +++ .../workflows/{phpunit.yml => wp-phpunit.yml} | 5 +- bin/install-cp-tests.sh | 277 ++++++++++++++++++ composer.json | 3 +- indieauth.php | 3 + languages/indieauth.pot | 4 +- readme.md | 4 +- readme.txt | 4 +- 8 files changed, 332 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/cp-phpunit.yml rename .github/workflows/{phpunit.yml => wp-phpunit.yml} (89%) create mode 100755 bin/install-cp-tests.sh diff --git a/.github/workflows/cp-phpunit.yml b/.github/workflows/cp-phpunit.yml new file mode 100644 index 0000000..861eff0 --- /dev/null +++ b/.github/workflows/cp-phpunit.yml @@ -0,0 +1,42 @@ +name: ClassicPress Unit Testing +on: + push: + pull_request: +jobs: + phpcs: + runs-on: ubuntu-latest + services: + mysql: + image: mariadb:10.4 + env: + MYSQL_ROOT_PASSWORD: root + ports: + - 3306:3306 + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=10s --health-retries=10 + strategy: + fail-fast: false + matrix: + php-versions: ['7.4', '8.3' ] + include: + - cp-version: latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + coverage: none + tools: composer, phpunit-polyfills + extensions: mysql + - name: Install Composer dependencies for PHP + uses: "ramsey/composer-install@v2" + - name: Setup Test Environment + run: | + sudo apt update + sudo apt install subversion + composer setup-cp-tests + - name: Unit Testing + run: composer phpunit + env: + PHP_VERSION: ${{ matrix.php-versions }} diff --git a/.github/workflows/phpunit.yml b/.github/workflows/wp-phpunit.yml similarity index 89% rename from .github/workflows/phpunit.yml rename to .github/workflows/wp-phpunit.yml index 6d75779..272d89e 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/wp-phpunit.yml @@ -29,7 +29,10 @@ jobs: - name: Install Composer dependencies for PHP uses: "ramsey/composer-install@v2" - name: Setup Test Environment - run: composer setup-local-tests + run: | + sudo apt update + sudo apt install subversion + composer setup-wp-tests - name: Unit Testing run: composer phpunit env: diff --git a/bin/install-cp-tests.sh b/bin/install-cp-tests.sh new file mode 100755 index 0000000..8821d5b --- /dev/null +++ b/bin/install-cp-tests.sh @@ -0,0 +1,277 @@ +#!/usr/bin/env bash +# See https://raw.githubusercontent.com/wp-cli/scaffold-command/master/templates/install-wp-tests.sh + +if [ $# -lt 3 ]; then + echo "usage: $0 [db-host] [cp-version] [skip-database-creation]" + exit 1 +fi + +DB_NAME=$1 +DB_USER=$2 +DB_PASS=$3 +DB_HOST=${4-localhost} +CP_VERSION=${5-latest} +SKIP_DB_CREATE=${6-false} + +TMPDIR=${TMPDIR-/tmp} +TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") +CP_TESTS_DIR=${CP_TESTS_DIR-$TMPDIR/classicpress-tests-lib} +CP_CORE_DIR=${CP_CORE_DIR-$TMPDIR/classicpress} + +# Remove trailing slashes +CP_TESTS_DIR=$(echo "$CP_TESTS_DIR" | sed 's:/\+$::') +CP_CORE_DIR=$(echo "$CP_CORE_DIR" | sed 's:/\+$::') + +download() { + if [ `which curl` ]; then + curl -L -s "$1" -o "$2" + elif [ `which wget` ]; then + wget -nv -O "$2" "$1" + fi +} + +set -ex + +# $CP_VERSION may be one of the following: +# 'latest' - latest stable release +# '1.2.3' or '1.2.3-rc1' etc - any released version number +# 'git+abc123' - use the specific commit 'abc123' from the *development* repo + +CP_RELEASE=y +if [[ "$CP_VERSION" == latest ]]; then + # Find the version number of the latest release + download \ + https://api.github.com/repos/ClassicPress/ClassicPress-release/releases/latest \ + "$TMPDIR/cp-latest.json" + CP_VERSION="$(grep -Po '"tag_name":\s*"[^"]+"' "$TMPDIR/cp-latest.json" | cut -d'"' -f4)" + + if [ -z "$CP_VERSION" ]; then + echo "ClassicPress version not detected correctly!" + cat "$TMPDIR/cp-latest.json" + exit 1 + fi +elif [[ "$CP_VERSION" = git-* ]]; then + # Use a specific commit from the development repo + CP_RELEASE=n + CP_VERSION=${CP_VERSION#git-} +elif ! [[ "$CP_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-|$) ]]; then + # Anything else needs to be a release version number + echo "ClassicPress version number not supported: $CP_VERSION" + exit 1 +fi + +if [ $CP_RELEASE = y ]; then + # Remote URLs for release and dev packages/files + CP_BUILD_ZIP_URL="https://github.com/ClassicPress/ClassicPress-release/archive/$CP_VERSION.zip" + CP_DEV_ZIP_URL="https://github.com/ClassicPress/ClassicPress/archive/$CP_VERSION+dev.zip" + CP_DEV_FILE_URL="https://raw.githubusercontent.com/ClassicPress/ClassicPress/$CP_VERSION+dev" + # Local paths + CP_BUILD_ZIP_PATH="$TMPDIR/classicpress-release-$CP_VERSION.zip" + CP_DEV_ZIP_PATH="$TMPDIR/classicpress-dev-$CP_VERSION.zip" + CP_DEV_PATH="$TMPDIR/classicpress-dev-$CP_VERSION" +else + # Remote URLs for dev packages/files (no release build) + CP_DEV_ZIP_URL="https://github.com/ClassicPress/ClassicPress/archive/$CP_VERSION.zip" + CP_DEV_FILE_URL="https://raw.githubusercontent.com/ClassicPress/ClassicPress/$CP_VERSION" + # Local paths + CP_DEV_ZIP_PATH="$TMPDIR/classicpress-dev-$CP_VERSION.zip" + CP_DEV_PATH="$TMPDIR/classicpress-dev-$CP_VERSION" +fi + +install_cp() { + if [ -d $CP_CORE_DIR ]; then + return; + fi + + mkdir -p $CP_CORE_DIR + + if [ $CP_RELEASE = y ]; then + download "$CP_BUILD_ZIP_URL" "$CP_BUILD_ZIP_PATH" + unzip -q "$CP_BUILD_ZIP_PATH" -d "$CP_CORE_DIR" + else + download "$CP_DEV_ZIP_URL" "$CP_DEV_ZIP_PATH" + unzip -q "$CP_DEV_ZIP_PATH" -d "$CP_CORE_DIR" + fi + clean_github_download "$CP_CORE_DIR" true + + download \ + https://raw.github.com/markoheijnen/wp-mysqli/master/db.php \ + "$CP_CORE_DIR/wp-content/db.php" + + # Hello Dolly is still used in some tests. + download \ + "$CP_DEV_FILE_URL/src/wp-content/plugins/hello.php" \ + "$CP_CORE_DIR/wp-content/plugins/hello.php" +} + +clean_github_download() { + # GitHub downloads extract with a single folder inside, named based on the + # version downloaded. Get rid of this. + dir="$1" + remove_src_dir="$2" + mv "$dir" "$dir-old" + mv "$dir-old/ClassicPress-"* "$dir" + rmdir "$dir-old" + if [ -d "$dir/src" ] && [ "$remove_src_dir" = true ]; then + # Development build - get rid of the 'src' directory too. + mv "$dir" "$dir-old" + mv "$dir-old/src" "$dir" + rm -rf "$dir-old" + fi +} + +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 [ ! -d "$CP_TESTS_DIR" ]; then + mkdir -p "$CP_TESTS_DIR" + download "$CP_DEV_ZIP_URL" "$CP_DEV_ZIP_PATH" + unzip -q "$CP_DEV_ZIP_PATH" -d "$CP_DEV_PATH" + clean_github_download "$CP_DEV_PATH" false + cp -ar \ + "$CP_DEV_PATH/tests/phpunit/includes" \ + "$CP_DEV_PATH/tests/phpunit/data" \ + "$CP_TESTS_DIR/" + fi + + if [ ! -f wp-tests-config.php ]; then + download \ + "$CP_DEV_FILE_URL/wp-tests-config-sample.php" \ + "$CP_TESTS_DIR/wp-tests-config.php" + sed $ioption "s:dirname( __FILE__ ) . '/src/':'$CP_CORE_DIR/':" "$CP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$CP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/yourusernamehere/$DB_USER/" "$CP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/yourpasswordhere/$DB_PASS/" "$CP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s|localhost|${DB_HOST}|" "$CP_TESTS_DIR"/wp-tests-config.php + fi + +} + +install_db() { + + if [ ${SKIP_DB_CREATE} = "true" ]; then + return 0 + fi + + # 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_e2e_site() { + + if [[ ${RUN_E2E} == 1 ]]; then + + # Script Variables + CONFIG_DIR="./tests/e2e-tests/config/travis" + CP_CORE_DIR="$HOME/classicpress" + CC_PLUGIN_DIR="$CP_CORE_DIR/wp-content/plugins/classic-commerce" + NGINX_DIR="$HOME/nginx" + PHP_FPM_BIN="$HOME/.phpenv/versions/$TRAVIS_PHP_VERSION/sbin/php-fpm" + PHP_FPM_CONF="$NGINX_DIR/php-fpm.conf" + CP_SITE_URL="http://localhost:8080" + BRANCH=$TRAVIS_BRANCH + REPO=$TRAVIS_REPO_SLUG + CP_DB_DATA="$HOME/build/$REPO/tests/e2e-tests/data/e2e-db.sql" + WORKING_DIR="$PWD" + + if [ "$TRAVIS_PULL_REQUEST_BRANCH" != "" ]; then + BRANCH=$TRAVIS_PULL_REQUEST_BRANCH + REPO=$TRAVIS_PULL_REQUEST_SLUG + fi + + set -ev + npm install + export NODE_CONFIG_DIR="./tests/e2e-tests/config" + + # Set up nginx to run the server + mkdir -p "$CP_CORE_DIR" + mkdir -p "$NGINX_DIR" + mkdir -p "$NGINX_DIR/sites-enabled" + mkdir -p "$NGINX_DIR/var" + + cp "$CONFIG_DIR/travis_php-fpm.conf" "$PHP_FPM_CONF" + + # Start php-fpm + "$PHP_FPM_BIN" --fpm-config "$PHP_FPM_CONF" + + # Copy the default nginx config files. + cp "$CONFIG_DIR/travis_nginx.conf" "$NGINX_DIR/nginx.conf" + cp "$CONFIG_DIR/travis_fastcgi.conf" "$NGINX_DIR/fastcgi.conf" + cp "$CONFIG_DIR/travis_default-site.conf" "$NGINX_DIR/sites-enabled/default-site.conf" + + # Start nginx. + nginx -c "$NGINX_DIR/nginx.conf" + + # Set up ClassicPress using wp-cli + cd "$CP_CORE_DIR" + + curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar + # Note, `wp core download` does not work when running the tests with a + # development version of ClassicPress, so we'll substitute the latest + # build instead! + if [ -z "$CP_BUILD_ZIP_URL" ]; then + CP_BUILD_ZIP_URL="https://www.classicpress.net/latest.zip" + fi + php wp-cli.phar core download "$CP_BUILD_ZIP_URL" + php wp-cli.phar core config --dbname=$DB_NAME --dbuser=$DB_USER --dbpass=$DB_PASS --dbhost=$DB_HOST --dbprefix=wp_ --extra-php <