Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2e Tests #119

Merged
merged 13 commits into from
Jan 14, 2025
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
181 changes: 181 additions & 0 deletions .github/workflows/cypress-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
name: E2E Test

env:
COMPOSER_VERSION: "2"
COMPOSER_CACHE: "${{ github.workspace }}/.composer-cache"
NODE_VERSION: "20"
NODE_CACHE: "${{ github.workspace }}/node_modules_cache"

on:
schedule:
- cron: '0 0 * * *'
push:
branches:
- develop
- trunk
pull_request:
branches:
- develop
- '[0-9].[0-9x]*' # Version branches: 4.x.x, 4.1.x, 5.x

jobs:
cypress_local:
name: ES ${{ matrix.esVersion }} - ${{ matrix.core.name }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
esVersion: ['7.10.1', '8.12.2']
core:
- {name: 'WP latest', version: ''}
- {name: 'WP minimum', version: '6.0'}

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Prepare npm cache
uses: actions/cache@v4
with:
path: ${{ env.NODE_CACHE }}
key: npm-${{ env.NODE_VERSION }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
npm-${{ env.NODE_VERSION }}-
- name: Prepare composer cache
uses: actions/cache@v4
with:
path: ${{ env.COMPOSER_CACHE }}
key: composer-${{ env.COMPOSER_VERSION }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
composer-${{ env.COMPOSER_VERSION }}-

- name: Set PHP version
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: :php-psr
tools: cs2pr
coverage: none

- name: composer install
run: composer install

- name: "Install node v${{ env.NODE_VERSION }}"
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}

- name: Install dependencies
run: npm ci --include=dev

- name: Set up WP environment with Elasticsearch
run: ES_VERSION=${{ matrix.esVersion }} npm run env:start

- name: Check ES response
run: curl --connect-timeout 5 --max-time 10 --retry 5 --retry-max-time 40 --retry-all-errors http://localhost:8890

- name: Build asset
run: npm run build

- name: Set up database
run: npm run cypress:setup -- --wp-version=${{ matrix.core.version }}

- name: Test
run: npm run cypress:run

- name: Make artifacts available
uses: actions/upload-artifact@v4
if: failure()
with:
name: cypress-artifact-${{ matrix.esVersion }}-${{ matrix.core.name }}
retention-days: 2
path: |
${{ github.workspace }}/tests/cypress/screenshots/
${{ github.workspace }}/tests/cypress/videos/
${{ github.workspace }}/tests/cypress/logs/

- name: Stop Elasticsearch
if: always()
run: npm run es:stop

cypress_epio:
name: EP.io - ${{ matrix.core.name }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
core:
- {name: 'WP latest', version: ''}
- {name: 'WP minimum', version: '6.0'}

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Prepare npm cache
uses: actions/cache@v4
with:
path: ${{ env.NODE_CACHE }}
key: npm-${{ env.NODE_VERSION }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
npm-${{ env.NODE_VERSION }}-
- name: Prepare composer cache
uses: actions/cache@v4
with:
path: ${{ env.COMPOSER_CACHE }}
key: composer-${{ env.COMPOSER_VERSION }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
composer-${{ env.COMPOSER_VERSION }}-

- name: Set PHP version
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: :php-psr
tools: cs2pr
coverage: none

- name: composer install
run: composer install

- name: "Install node v${{ env.NODE_VERSION }}"
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}

- name: Install dependencies
run: npm ci --include=dev

- name: Set up WP environment
run: |
npm run env start
npm run env:install-tests-cli

- name: Build asset
run: npm run build

- name: Set up database
env:
EPIO_HOST: ${{ vars.EPIO_HOST }}
EPIO_INDEX_PREFIX: ${{ vars.EPIO_INDEX_PREFIX }}
run: npm run cypress:setup -- --ep-host="$EPIO_HOST" --es-shield='${{ secrets.EP_CREDENTIALS }}' --ep-index-prefix="$EPIO_INDEX_PREFIX" --wp-version=${{ matrix.core.version }}

- name: Test
run: npm run cypress:run

- name: Make artifacts available
uses: actions/upload-artifact@v4
if: failure()
with:
name: cypress-artifact-epio-${{ matrix.core.name }}
retention-days: 2
path: |
${{ github.workspace }}/tests/cypress/screenshots/
${{ github.workspace }}/tests/cypress/videos/
${{ github.workspace }}/tests/cypress/logs/

- name: Delete Elasticsearch indices
if: always()
run: |
./bin/wp-env-cli tests-wordpress "wp --allow-root plugin activate elasticpress elasticpress-labs"
./bin/wp-env-cli tests-wordpress "wp --allow-root elasticpress-tests delete-all-indices"
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test
name: Unit Test

env:
COMPOSER_VERSION: "2"
Expand Down
22 changes: 22 additions & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"config": {
"WP_DEBUG": true,
"WP_DEBUG_DISPLAY": true,
"WP_DISABLE_FATAL_ERROR_HANDLER": true
},
"env": {
"tests": {
"themes": [
"https://downloads.wordpress.org/theme/twentytwentyone.zip"
],
"plugins": [
"https://downloads.wordpress.org/plugin/elasticpress.zip"
],
"mappings": {
"wp-content/plugins/elasticpress-labs": ".",
".htaccess": "./tests/cypress/wordpress-files/.htaccess",
"wp-content/mu-plugins/unique-index-name.php": "./tests/cypress/wordpress-files/test-mu-plugins/unique-index-name.php"
}
}
}
}
47 changes: 47 additions & 0 deletions bin/install-es-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash

if [ $# -lt 1 ]; then
echo "usage: $0 start or $0 stop"
exit 1
fi
ACTION=$1
if [ $ACTION != "start" ] && [ $ACTION != "stop" ]; then
echo "usage: $0 start or $0 stop"
exit 1
fi

TMPDIR=${TMPDIR-/tmp}
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
EP_ES_DOCKER_DIR=${EP_ES_DOCKER_DIR-$TMPDIR/ep-es-docker}

# Check if git is installed
check_git_installed() {
if ! command -v git > /dev/null; then
echo "Error: git is not installed. Please install git and try again."
exit 1
fi
}

maybe_install() {
if [ -d $EP_ES_DOCKER_DIR ]; then
echo "Elasticsearch Docker already installed."
return
fi

check_git_installed
git clone --depth 1 --branch develop https://github.com/10up/ElasticPress/ $TMPDIR/elasticpress-develop

rm -r $EP_ES_DOCKER_DIR
mv $TMPDIR/elasticpress-develop/bin/es-docker $EP_ES_DOCKER_DIR
rm -r $TMPDIR/elasticpress-develop
}

maybe_install

if [ $ACTION == "start" ]; then
cd $EP_ES_DOCKER_DIR
docker compose build --build-arg ES_VERSION=${ES_VERSION-8.16.1} && docker compose up -d
elif [ $ACTION == "stop" ]; then
cd $EP_ES_DOCKER_DIR
docker compose down
fi
7 changes: 7 additions & 0 deletions bin/install-wp-cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

echo "Installing WP-CLI in $1"

./bin/wp-env-cli $1 curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
./bin/wp-env-cli $1 chmod +x wp-cli.phar
./bin/wp-env-cli $1 mv wp-cli.phar /usr/local/bin/wp
86 changes: 86 additions & 0 deletions bin/setup-cypress-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/bash

EP_HOST=""
EP_CREDENTIALS=""
EP_INDEX_PREFIX=""
WP_VERSION=""
WC_VERSION=""
DISPLAY_HELP=0

for opt in "$@"; do
case $opt in
-h=*|--ep-host=*)
EP_HOST="${opt#*=}"
;;
-s=*|--es-shield=*)
EP_CREDENTIALS="${opt#*=}"
;;
-u=*|--ep-index-prefix=*)
EP_INDEX_PREFIX="${opt#*=}"
;;
-wp=*|--wp-version=*)
WP_VERSION="${opt#*=}"
;;
-wc=*|--wc-version=*)
WC_VERSION="${opt#*=}"
;;
-h|--help|*)
DISPLAY_HELP=1
;;
esac
done

PLUGIN_NAME=$(basename "$PWD")

if [ $DISPLAY_HELP -eq 1 ]; then
echo "This script will setup the environment for the Cypress tests"
echo "Usage: ${0##*/} [OPTIONS...]"
echo
echo "Optional parameters:"
echo "-h=*, --ep-host=* The remote Elasticsearch Host URL."
echo "-s=*, --es-shield=* The Elasticsearch credentials, used in the ES_SHIELD constant."
echo "-u=*, --ep-index-prefix=* The Elasticsearch credentials, used in the EP_INDEX_PREFIX constant."
echo "-W=*, --wp-version=* WordPress Core version."
echo "-w=*, --wc-version=* WooCommerce version."
echo "-h|--help Display this help screen"
exit
fi

# Set twentytwentyone as the active theme here, as 2025 won't work with WP 5.6
./bin/wp-env-cli tests-wordpress "wp --allow-root theme activate twentytwentyone"

if [ ! -z $WP_VERSION ]; then
./bin/wp-env-cli tests-wordpress "wp --allow-root core update --version=${WP_VERSION} --force"
./bin/wp-env-cli tests-wordpress "wp --allow-root core update-db"
fi

./bin/wp-env-cli tests-wordpress "wp --allow-root plugin activate elasticpress-labs"

if [ -z $EP_HOST ]; then
# Determine what kind of env we're in
if [ "$(uname | tr '[:upper:]' '[:lower:]')" = "darwin" ]; then
echo "Running tests on $(uname)"
EP_HOST="http://host.docker.internal:8890/"
elif grep -qi microsoft /proc/version; then
echo "Running tests on Windows"
EP_HOST="http://host.docker.internal:8890/"
else
echo "Running tests on $(uname)"
# 172.17.0.1 is the IP Address of host when using Linux
EP_HOST="http://172.17.0.1:8890/"
fi
fi
./bin/wp-env-cli tests-wordpress "wp --allow-root config set EP_HOST ${EP_HOST}"

if [ ! -z $EP_CREDENTIALS ]; then
./bin/wp-env-cli tests-wordpress "wp --allow-root config set EP_CREDENTIALS ${EP_CREDENTIALS}"
fi

if [ ! -z $EP_INDEX_PREFIX ]; then
./bin/wp-env-cli tests-wordpress "wp --allow-root config set EP_INDEX_PREFIX ${EP_INDEX_PREFIX}"
fi

./bin/wp-env-cli tests-wordpress "wp --allow-root elasticpress sync --setup --yes --show-errors"

./bin/wp-env-cli tests-wordpress "wp --allow-root option set posts_per_page 5"
./bin/wp-env-cli tests-wordpress "wp --allow-root user meta update admin edit_post_per_page 5"
Loading
Loading