Skip to content

Commit 596c78d

Browse files
authored
Playwright and phpunit tests [TMZ-733] (#502)
1 parent c8994cc commit 596c78d

35 files changed

+6422
-271
lines changed

.buildignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ webpack.config.js
2424
.eslintignore
2525
.eslintrc.js
2626
.npmrc
27+
28+
tests/
29+
test-results/
30+
tsconfig.json
31+
.wp-env.json

.github/workflows/phpunit.yml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
name: PHPUnit
2+
permissions:
3+
contents: read
4+
5+
on:
6+
push:
7+
paths-ignore:
8+
- '**.md'
9+
- '**.txt'
10+
- '.github/config.json'
11+
- 'bin/**'
12+
- '.gitignore'
13+
- 'docs/**'
14+
branches:
15+
- 'main'
16+
- '3.*'
17+
pull_request:
18+
paths-ignore:
19+
- '**.md'
20+
- '**.txt'
21+
- '.github/config.json'
22+
- 'bin/**'
23+
- '.gitignore'
24+
- 'docs/**'
25+
merge_group:
26+
27+
# This allows a subsequently queued workflow run to interrupt previous runs
28+
concurrency:
29+
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}'
30+
cancel-in-progress: true
31+
32+
jobs:
33+
file-diff:
34+
runs-on: ubuntu-latest
35+
name: File Diff
36+
if: startsWith( github.repository, 'elementor/' )
37+
outputs:
38+
php_diff: ${{ steps.php_diff_files.outputs.diff }}
39+
steps:
40+
- name: Checkout source code
41+
uses: actions/checkout@v4
42+
- name: Check PHP files diff
43+
id: php_diff_files
44+
uses: technote-space/get-diff-action@v6
45+
with:
46+
PATTERNS: |
47+
**/*.php
48+
**/*.twig
49+
composer.+(json|lock)
50+
.github/**/*.yml
51+
install-wp-tests.sh
52+
test:
53+
runs-on: ubuntu-22.04
54+
needs: [ 'file-diff' ]
55+
if: ${{ github.event.pull_request.title == null || needs.file-diff.outputs.php_diff }}
56+
strategy:
57+
fail-fast: false
58+
matrix:
59+
wordpress_versions: ['nightly', 'latest', '6.6', '6.5']
60+
php_versions: ['7.4', '8.0', '8.1', '8.2', '8.3']
61+
name: PHPUnit - WordPress ${{ matrix.wordpress_versions }} - PHP version ${{ matrix.php_versions }}
62+
env:
63+
WP_TESTS_DIR: /tmp/wordpress-tests-lib
64+
WP_TESTS_ELEMENTOR_DIR: /tmp/elementor/elementor.php
65+
WP_TESTS_HELLOPLUS_DIR: /tmp/hello-plus/hello-plus.php
66+
THEME_FILE: functions.php
67+
COVERAGE: ${{ matrix.php_versions >= 8.3 && matrix.wordpress_versions == 'latest' && github.event_name == 'push' }}
68+
services:
69+
mysql:
70+
image: mysql:5.7
71+
env:
72+
MYSQL_ROOT_PASSWORD: root
73+
ports:
74+
- 3306
75+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
76+
77+
steps:
78+
- name: Checkout source code
79+
uses: actions/checkout@v4
80+
with:
81+
fetch-depth: 0
82+
- name: Setup PHP
83+
uses: shivammathur/setup-php@v2
84+
with:
85+
php-version: ${{ matrix.php_versions }}
86+
coverage: none
87+
- name: Install Dependencies
88+
run: |
89+
bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:${{ job.services.mysql.ports['3306'] }} ${{ matrix.wordpress_versions }} true
90+
composer update --no-interaction
91+
- name: Copy theme folder to /tmp/wordpress/wp-content/themes/
92+
run: |
93+
cp -a $GITHUB_WORKSPACE /tmp/wordpress/wp-content/themes/
94+
echo "Copied theme folder to /tmp/wordpress/wp-content/themes/$(basename $GITHUB_WORKSPACE)"
95+
- name: Run Tests with Coverage (latest PHP & WP)
96+
if: ${{ env.COVERAGE != 'false' }}
97+
run: |
98+
composer run coverage
99+
- name: Run Tests without Coverage
100+
if: ${{ env.COVERAGE == 'false' }}
101+
run: |
102+
composer run test
103+
104+
test-result:
105+
needs: test
106+
if: ${{ always() }} # Will be run even if 'test' matrix will be skipped
107+
runs-on: ubuntu-22.04
108+
name: PHPUnit - Test Results
109+
steps:
110+
- name: Test status
111+
run: echo "Test status is - ${{ needs.test.result }}"
112+
- name: Check test matrix status
113+
if: ${{ needs.test.result != 'success' && needs.test.result != 'skipped' }}
114+
run: exit 1

.github/workflows/playwright.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: Playwright Tests
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
permissions:
8+
contents: read
9+
actions: write
10+
11+
jobs:
12+
playwright-tests:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
19+
- name: Setup Node.js
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version: '20'
23+
cache: 'npm'
24+
registry-url: 'https://npm.pkg.github.com'
25+
scope: '@elementor'
26+
27+
- name: Setup PHP
28+
uses: shivammathur/setup-php@v2
29+
with:
30+
php-version: '8.1'
31+
tools: composer
32+
coverage: none
33+
34+
- name: Install Composer dependencies
35+
run: composer install --no-dev --optimize-autoloader
36+
37+
- name: Install npm dependencies
38+
run: npm ci
39+
env:
40+
NODE_AUTH_TOKEN: ${{ secrets.CLOUD_DEVOPS_TOKEN }}
41+
42+
- name: Start wp-env
43+
run: npm run wp-env:start
44+
45+
- name: Setup Playwright
46+
run: npm run test:setup:playwright
47+
48+
- name: Setup Chromium
49+
run: npm run test:setup:chromium
50+
51+
- name: Run Playwright tests
52+
id: playwright-tests
53+
run: |
54+
npm run test:playwright
55+
echo "exit_code=$?" >> $GITHUB_OUTPUT
56+
continue-on-error: false
57+
58+
- name: Check test results
59+
if: steps.playwright-tests.outcome == 'failure'
60+
run: |
61+
echo "❌ Playwright tests failed!"
62+
exit 1
63+
64+
- name: Stop wp-env
65+
if: always()
66+
run: npm run wp-env:stop

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ assets/js/
1717
*.zip
1818
.npmrc
1919
.env
20+
tmp/
21+
.phpunit.result.cache
22+
!tests/phpunit/hello-elementor

.wp-env.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"core": null,
3+
"phpVersion": "8.0",
4+
"plugins": [
5+
"https://downloads.wordpress.org/plugin/elementor.latest-stable.zip"
6+
],
7+
"themes": [
8+
"./"
9+
],
10+
"mappings": {
11+
"hello-elementor-config": "./tests/wp-env/config"
12+
},
13+
"config": {
14+
"ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS": true,
15+
"SCRIPT_DEBUG": false,
16+
"WP_DEBUG": false
17+
}
18+
}

bin/install-wp-tests-local.sh

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#!/usr/bin/env bash
2+
3+
WORKING_DIR=$(pwd)
4+
5+
# Remove old tmp folder
6+
rm -rf "$WORKING_DIR/tmp"
7+
8+
# Ask for some parameters to install the test env.
9+
echo "Choose a database Name for tests [elementor-tests]:"
10+
read -r DB_NAME
11+
echo "What is your database username [admin]?"
12+
read -r DB_USER
13+
echo "What is your database password [admin]?"
14+
read -r DB_PASS
15+
echo "What is your database host (specify port if needed) [127.0.0.1:PORT]?"
16+
read -r DB_HOST
17+
echo "Choose WordPress version for testing [latest]:"
18+
read -r WP_VERSION
19+
20+
DB_NAME=${DB_NAME:-"elementor-tests"}
21+
DB_USER=${DB_USER:-"admin"}
22+
DB_PASS=${DB_PASS:-"admin"}
23+
DB_HOST=${DB_HOST:-"127.0.0.1"}
24+
WP_VERSION=${WP_VERSION:-"latest"}
25+
26+
WP_TESTS_UTILS_DIR=${WP_TESTS_UTILS_DIR-$WORKING_DIR/tmp/wordpress-tests-lib}
27+
WP_CORE_DIR=${WP_CORE_DIR-$WORKING_DIR/tmp/wordpress/}
28+
ELEMENTOR_PLUGIN_DIR=${ELEMENTOR_PLUGIN_DIR-$WORKING_DIR/tmp}
29+
HELLOPLUS_PLUGIN_DIR=${HELLOPLUS_PLUGIN_DIR-$WORKING_DIR/tmp}
30+
HELLOTHEME_THEME_DIR=${HELLOTHEME_THEME_DIR-$WP_CORE_DIR/wp-content/themes/hello-theme}
31+
32+
# Download util
33+
download() {
34+
if [ `which curl` ]; then
35+
curl --location -s "$1" > "$2";
36+
elif [ `which wget` ]; then
37+
wget -nv -O "$2" "$1"
38+
fi
39+
}
40+
41+
## Determine the WP_TEST_TAG.
42+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then
43+
WP_TESTS_TAG="tags/$WP_VERSION"
44+
else
45+
# http serves a single offer, whereas https serves multiple. we only want one
46+
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
47+
grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
48+
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
49+
if [[ -z "$LATEST_VERSION" ]]; then
50+
echo "Latest WordPress version could not be found"
51+
exit 1
52+
fi
53+
WP_TESTS_TAG="tags/$LATEST_VERSION"
54+
fi
55+
56+
set -ex
57+
58+
check_for_svn() {
59+
if [ ! `which svn` ]; then
60+
echo 'Please install "svn" and re run this script.'
61+
echo 'Mac users: `brew install svn`'
62+
exit 1
63+
fi
64+
}
65+
66+
install_wp() {
67+
if [ -d "$WP_CORE_DIR" ]; then
68+
return;
69+
fi
70+
71+
mkdir -p "$WP_CORE_DIR"
72+
73+
if [ $WP_VERSION == 'latest' ]; then
74+
local ARCHIVE_NAME='latest'
75+
else
76+
local ARCHIVE_NAME="wordpress-$WP_VERSION"
77+
fi
78+
79+
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
80+
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C "$WP_CORE_DIR"
81+
82+
download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php "$WP_CORE_DIR/wp-content/db.php"
83+
}
84+
85+
install_test_suite() {
86+
# portable in-place argument for both GNU sed and Mac OSX sed
87+
if [[ $(uname -s) == 'Darwin' ]]; then
88+
local ioption='-i .bak'
89+
else
90+
local ioption='-i'
91+
fi
92+
93+
# set up testing suite if it doesn't yet exist
94+
if [ ! -d "$WP_TESTS_UTILS_DIR" ]; then
95+
# set up testing suite
96+
mkdir -p "$WP_TESTS_UTILS_DIR"/includes
97+
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ "$WP_TESTS_UTILS_DIR/includes/"
98+
fi
99+
100+
cd "$WP_TESTS_UTILS_DIR"
101+
102+
if [ ! -f wp-tests-config.php ]; then
103+
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_UTILS_DIR/wp-tests-config.php"
104+
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" "$WP_TESTS_UTILS_DIR"/wp-tests-config.php
105+
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_UTILS_DIR"/wp-tests-config.php
106+
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_UTILS_DIR"/wp-tests-config.php
107+
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_UTILS_DIR"/wp-tests-config.php
108+
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_UTILS_DIR"/wp-tests-config.php
109+
fi
110+
}
111+
112+
install_db() {
113+
# parse DB_HOST for port or socket references
114+
local PARTS=(${DB_HOST//\:/ })
115+
local DB_HOSTNAME=${PARTS[0]};
116+
local DB_SOCK_OR_PORT=${PARTS[1]};
117+
local EXTRA=""
118+
119+
if ! [ -z $DB_HOSTNAME ] ; then
120+
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
121+
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
122+
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
123+
EXTRA=" --socket=$DB_SOCK_OR_PORT"
124+
elif ! [ -z $DB_HOSTNAME ] ; then
125+
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
126+
fi
127+
fi
128+
129+
# create database
130+
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
131+
}
132+
133+
install_elementor_plugin() {
134+
download https://downloads.wordpress.org/plugin/elementor.latest-stable.zip /tmp/elementor.zip
135+
# Using double-quotes to wrap the unzip path so directory names with spaces will not cause problems
136+
unzip -q /tmp/elementor.zip -d "$ELEMENTOR_PLUGIN_DIR"
137+
}
138+
139+
install_helloplus_plugin() {
140+
download https://downloads.wordpress.org/plugin/hello-plus.latest-stable.zip /tmp/hello-plus.zip
141+
# Using double-quotes to wrap the unzip path so directory names with spaces will not cause problems
142+
unzip -q /tmp/hello-plus.zip -d "$HELLOPLUS_PLUGIN_DIR"
143+
}
144+
145+
symlink_theme() {
146+
rm -rf "$HELLOTHEME_THEME_DIR"
147+
148+
ln -s "$WORKING_DIR" "$HELLOTHEME_THEME_DIR"
149+
}
150+
151+
check_for_svn
152+
install_wp
153+
install_test_suite
154+
install_elementor_plugin
155+
install_helloplus_plugin
156+
symlink_theme
157+
install_db

0 commit comments

Comments
 (0)