11#! /usr/bin/env bash
22
3+ # See https://raw.githubusercontent.com/wp-cli/scaffold-command/master/templates/install-wp-tests.sh
4+
5+ # Set up colors for output
6+ RED=" \033[0;31m"
7+ GREEN=" \033[0;32m"
8+ YELLOW=" \033[0;33m"
9+ CYAN=" \033[0;36m"
10+ RESET=" \033[0m"
11+
312if [ $# -lt 3 ]; then
4- echo " usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
13+ echo -e " ${YELLOW} Usage: ${RESET} $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
514 exit 1
615fi
716
@@ -15,31 +24,67 @@ SKIP_DB_CREATE=${6-false}
1524TMPDIR=${TMPDIR-/ tmp}
1625TMPDIR=$( echo $TMPDIR | sed -e " s/\/$//" )
1726WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR / wordpress-tests-lib}
27+ WP_TESTS_FILE=" $WP_TESTS_DIR " /includes/functions.php
1828WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR / wordpress}
29+ WP_CORE_FILE=" $WP_CORE_DIR " /wp-settings.php
1930
2031download () {
21- if [ ` which curl` ]; then
22- curl -s " $1 " > " $2 " ;
23- elif [ ` which wget` ]; then
32+ if command -v curl > /dev/null 2>&1 ; then
33+ curl -L -s " $1 " > " $2 " ;
34+ return $?
35+ elif command -v wget > /dev/null 2>&1 ; then
2436 wget -nv -O " $2 " " $1 "
37+ return $?
2538 else
26- echo " Error: Neither curl nor wget is installed."
39+ echo -e " ${RED} Error: Neither curl nor wget is installed.${RESET} "
2740 exit 1
2841 fi
2942}
3043
31- # Check if svn is installed
32- check_svn_installed () {
33- if ! command -v svn > /dev/null; then
34- echo " Error: svn is not installed. Please install svn and try again."
35- exit 1
36- fi
44+ check_for_updates () {
45+ local remote_url=" https://raw.githubusercontent.com/wp-cli/scaffold-command/main/templates/install-wp-tests.sh"
46+ local tmp_script=" $TMPDIR /install-wp-tests.sh.latest"
47+
48+ if ! download " $remote_url " " $tmp_script " ; then
49+ echo -e " ${YELLOW} Warning: Failed to download the latest version of the script for update check.${RESET} "
50+ return
51+ fi
52+
53+ if [ ! -f " $tmp_script " ] || [ ! -s " $tmp_script " ]; then
54+ echo -e " ${YELLOW} Warning: Downloaded script is missing or empty, cannot check for updates.${RESET} "
55+ rm -f " $tmp_script "
56+ return
57+ fi
58+
59+ local local_hash=" "
60+ local remote_hash=" "
61+
62+ if command -v shasum > /dev/null; then
63+ local_hash=$( shasum -a 256 " $0 " | awk ' {print $1}' )
64+ remote_hash=$( shasum -a 256 " $tmp_script " | awk ' {print $1}' )
65+ elif command -v sha256sum > /dev/null; then
66+ local_hash=$( sha256sum " $0 " | awk ' {print $1}' )
67+ remote_hash=$( sha256sum " $tmp_script " | awk ' {print $1}' )
68+ else
69+ echo -e " ${YELLOW} Warning: Could not find shasum or sha256sum to check for script updates.${RESET} "
70+ rm " $tmp_script "
71+ return
72+ fi
73+
74+ rm " $tmp_script "
75+
76+ if [ " $local_hash " != " $remote_hash " ]; then
77+ echo -e " ${YELLOW} Warning: A newer version of this script is available at $remote_url ${RESET} "
78+ fi
3779}
80+ # Allow disabling the update check by setting WP_INSTALL_TESTS_SKIP_UPDATE_CHECK=true in the environment.
81+ if [ " ${WP_INSTALL_TESTS_SKIP_UPDATE_CHECK:- false} " != " true" ]; then
82+ check_for_updates
83+ fi
3884
3985if [[ $WP_VERSION =~ ^[0-9]+\. [0-9]+\- (beta| RC)[0-9]+$ ]]; then
4086 WP_BRANCH=${WP_VERSION% \- * }
4187 WP_TESTS_TAG=" branches/$WP_BRANCH "
42-
4388elif [[ $WP_VERSION =~ ^[0-9]+\. [0-9]+$ ]]; then
4489 WP_TESTS_TAG=" branches/$WP_VERSION "
4590elif [[ $WP_VERSION =~ [0-9]+\. [0-9]+\. [0-9]+ ]]; then
@@ -54,30 +99,35 @@ elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
5499else
55100 # http serves a single offer, whereas https serves multiple. we only want one
56101 download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
57- grep ' [0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
58- LATEST_VERSION=$( grep -o ' "version":"[^"]*' /tmp/wp-latest.json | sed ' s/"version":"//' )
102+ LATEST_VERSION=$( grep -oE ' "version":"[^"]*' /tmp/wp-latest.json | head -n 1 | sed ' s/"version":"//' )
59103 if [[ -z " $LATEST_VERSION " ]]; then
60- echo " Latest WordPress version could not be found"
104+ echo -e " ${RED} Error: Latest WordPress version could not be found. ${RESET} "
61105 exit 1
62106 fi
107+ # The version-check endpoint returns major.minor (e.g., 6.9), but GitHub tags include the patch version (e.g., 6.9.0)
108+ if [[ $LATEST_VERSION =~ ^[0-9]+\. [0-9]+$ ]]; then
109+ LATEST_VERSION=" ${LATEST_VERSION} .0"
110+ fi
63111 WP_TESTS_TAG=" tags/$LATEST_VERSION "
64112fi
113+
65114set -ex
66115
67116install_wp () {
68117
69- if [ -d $WP_CORE_DIR ]; then
118+ if [ -f $WP_CORE_FILE ]; then
119+ echo -e " ${CYAN} WordPress is already installed.${RESET} "
70120 return ;
71121 fi
72122
123+ echo -e " ${CYAN} Installing WordPress...${RESET} "
124+
125+ rm -rf $WP_CORE_DIR
73126 mkdir -p $WP_CORE_DIR
74127
75128 if [[ $WP_VERSION == ' nightly' || $WP_VERSION == ' trunk' ]]; then
76- mkdir -p $TMPDIR /wordpress-trunk
77- rm -rf $TMPDIR /wordpress-trunk/*
78- check_svn_installed
79- svn export --quiet https://core.svn.wordpress.org/trunk $TMPDIR /wordpress-trunk/wordpress
80- mv $TMPDIR /wordpress-trunk/wordpress/* $WP_CORE_DIR
129+ download https://github.com/WordPress/wordpress/archive/refs/heads/master.tar.gz $TMPDIR /wordpress.tar.gz
130+ tar --strip-components=1 -zxmf $TMPDIR /wordpress.tar.gz -C $WP_CORE_DIR
81131 else
82132 if [ $WP_VERSION == ' latest' ]; then
83133 local ARCHIVE_NAME=' latest'
@@ -103,8 +153,7 @@ install_wp() {
103153 download https://wordpress.org/${ARCHIVE_NAME} .tar.gz $TMPDIR /wordpress.tar.gz
104154 tar --strip-components=1 -zxmf $TMPDIR /wordpress.tar.gz -C $WP_CORE_DIR
105155 fi
106-
107- download https://raw.githubusercontent.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR /wp-content/db.php
156+ echo -e " ${GREEN} WordPress installed successfully.${RESET} "
108157}
109158
110159install_test_suite () {
@@ -115,18 +164,68 @@ install_test_suite() {
115164 local ioption=' -i'
116165 fi
117166
118- # set up testing suite if it doesn't yet exist
119- if [ ! -d $WP_TESTS_DIR ]; then
167+ # set up testing suite if it doesn't yet exist or only partially exists
168+ if [ ! -f $WP_TESTS_FILE ]; then
169+ echo -e " ${CYAN} Installing test suite...${RESET} "
120170 # set up testing suite
171+ rm -rf $WP_TESTS_DIR
121172 mkdir -p $WP_TESTS_DIR
122- rm -rf $WP_TESTS_DIR /{includes,data}
123- check_svn_installed
124- svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG} /tests/phpunit/includes/ $WP_TESTS_DIR /includes
125- svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG} /tests/phpunit/data/ $WP_TESTS_DIR /data
173+
174+ if [[ $WP_TESTS_TAG == ' trunk' ]]; then
175+ ref=trunk
176+ archive_url=" https://github.com/WordPress/wordpress-develop/archive/refs/heads/${ref} .tar.gz"
177+ elif [[ $WP_TESTS_TAG == branches/* ]]; then
178+ ref=${WP_TESTS_TAG# branches/ }
179+ archive_url=" https://github.com/WordPress/wordpress-develop/archive/refs/heads/${ref} .tar.gz"
180+ else
181+ ref=${WP_TESTS_TAG# tags/ }
182+ archive_url=" https://github.com/WordPress/wordpress-develop/archive/refs/tags/${ref} .tar.gz"
183+ fi
184+
185+ if [ -z " $ref " ]; then
186+ echo -e " ${RED} Error:${RESET} Unable to determine git reference from WP_TESTS_TAG: $WP_TESTS_TAG "
187+ exit 1
188+ fi
189+
190+ download " ${archive_url} " " $TMPDIR /wordpress-develop.tar.gz"
191+
192+ # Validate that the tarball was downloaded correctly before extracting
193+ if [ ! -s " $TMPDIR /wordpress-develop.tar.gz" ]; then
194+ echo -e " ${RED} Error:${RESET} Downloaded test suite archive is missing or empty: $TMPDIR /wordpress-develop.tar.gz"
195+ exit 1
196+ fi
197+
198+ if ! tar -tzf " $TMPDIR /wordpress-develop.tar.gz" > /dev/null 2>&1 ; then
199+ echo -e " ${RED} Error:${RESET} Downloaded test suite archive is not a valid tar.gz file: $TMPDIR /wordpress-develop.tar.gz"
200+ exit 1
201+ fi
202+
203+ tar -zxmf " $TMPDIR /wordpress-develop.tar.gz" -C " $TMPDIR "
204+ mv " $TMPDIR /wordpress-develop-${ref} /tests/phpunit/includes" " $WP_TESTS_DIR " /
205+ mv " $TMPDIR /wordpress-develop-${ref} /tests/phpunit/data" " $WP_TESTS_DIR " /
206+ rm -rf " $TMPDIR /wordpress-develop-${ref} "
207+ rm " $TMPDIR /wordpress-develop.tar.gz"
208+ echo -e " ${GREEN} Test suite installed.${RESET} "
209+ else
210+ echo -e " ${CYAN} Test suite is already installed.${RESET} "
126211 fi
127212
128- if [ ! -f wp-tests-config.php ]; then
129- download https://develop.svn.wordpress.org/${WP_TESTS_TAG} /wp-tests-config-sample.php " $WP_TESTS_DIR " /wp-tests-config.php
213+ if [ ! -f " $WP_TESTS_DIR " /wp-tests-config.php ]; then
214+ echo -e " ${CYAN} Configuring test suite...${RESET} "
215+ if [[ $WP_TESTS_TAG == ' trunk' ]]; then
216+ ref=trunk
217+ elif [[ $WP_TESTS_TAG == branches/* ]]; then
218+ ref=${WP_TESTS_TAG# branches/ }
219+ else
220+ ref=${WP_TESTS_TAG# tags/ }
221+ fi
222+
223+ if [ -z " $ref " ]; then
224+ echo -e " ${RED} Error:${RESET} Unable to determine git reference from WP_TESTS_TAG: $WP_TESTS_TAG "
225+ exit 1
226+ fi
227+
228+ download https://raw.githubusercontent.com/WordPress/wordpress-develop/${ref} /wp-tests-config-sample.php " $WP_TESTS_DIR " /wp-tests-config.php
130229 # remove all forward slashes in the end
131230 WP_CORE_DIR=$( echo $WP_CORE_DIR | sed " s:/\+$::" )
132231 sed $ioption " s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR /':" " $WP_TESTS_DIR " /wp-tests-config.php
@@ -135,6 +234,9 @@ install_test_suite() {
135234 sed $ioption " s/yourusernamehere/$DB_USER /" " $WP_TESTS_DIR " /wp-tests-config.php
136235 sed $ioption " s/yourpasswordhere/$DB_PASS /" " $WP_TESTS_DIR " /wp-tests-config.php
137236 sed $ioption " s|localhost|${DB_HOST} |" " $WP_TESTS_DIR " /wp-tests-config.php
237+ echo -e " ${GREEN} Test suite configured.${RESET} "
238+ else
239+ echo -e " ${CYAN} Test suite is already configured.${RESET} "
138240 fi
139241
140242}
@@ -143,22 +245,32 @@ recreate_db() {
143245 shopt -s nocasematch
144246 if [[ $1 =~ ^(y| yes)$ ]]
145247 then
146- mysqladmin drop $DB_NAME -f --user=" $DB_USER " --password=" $DB_PASS " $EXTRA
248+ echo -e " ${CYAN} Recreating the database ($DB_NAME )...${RESET} "
249+ if command -v mariadb-admin > /dev/null 2>&1 ; then
250+ mariadb-admin drop $DB_NAME -f --user=" $DB_USER " --password=" $DB_PASS " $EXTRA
251+ else
252+ mysqladmin drop $DB_NAME -f --user=" $DB_USER " --password=" $DB_PASS " $EXTRA
253+ fi
147254 create_db
148- echo " Recreated the database ($DB_NAME ). "
255+ echo -e " ${GREEN} Database ($DB_NAME ) recreated. ${RESET} "
149256 else
150- echo " Leaving the existing database ($DB_NAME ) in place."
257+ echo -e " ${YELLOW} Leaving the existing database ($DB_NAME ) in place.${RESET} "
151258 fi
152259 shopt -u nocasematch
153260}
154261
155262create_db () {
156- mysqladmin create $DB_NAME --user=" $DB_USER " --password=" $DB_PASS " $EXTRA
263+ if command -v mariadb-admin > /dev/null 2>&1 ; then
264+ mariadb-admin create $DB_NAME --user=" $DB_USER " --password=" $DB_PASS " $EXTRA
265+ else
266+ mysqladmin create $DB_NAME --user=" $DB_USER " --password=" $DB_PASS " $EXTRA
267+ fi
157268}
158269
159270install_db () {
160271
161272 if [ ${SKIP_DB_CREATE} = " true" ]; then
273+ echo -e " ${YELLOW} Skipping database creation.${RESET} "
162274 return 0
163275 fi
164276
@@ -179,16 +291,24 @@ install_db() {
179291 fi
180292
181293 # create database
182- if [ $( mysql --user=" $DB_USER " --password=" $DB_PASS " $EXTRA --execute=' show databases;' | grep ^$DB_NAME $) ]
294+ if command -v mariadb > /dev/null 2>&1 ; then
295+ local DB_CLIENT=' mariadb'
296+ else
297+ local DB_CLIENT=' mysql'
298+ fi
299+ if $DB_CLIENT --user=" $DB_USER " --password=" $DB_PASS " $EXTRA --execute=' show databases;' | grep -q " ^$DB_NAME $" ;
183300 then
184- echo " Reinstalling will delete the existing test database ($DB_NAME )"
301+ echo -e " ${YELLOW} Reinstalling will delete the existing test database ($DB_NAME )${RESET} "
185302 read -p ' Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB
186303 recreate_db $DELETE_EXISTING_DB
187304 else
305+ echo -e " ${CYAN} Creating database ($DB_NAME )...${RESET} "
188306 create_db
307+ echo -e " ${GREEN} Database ($DB_NAME ) created.${RESET} "
189308 fi
190309}
191310
192311install_wp
193312install_test_suite
194313install_db
314+ echo -e " ${GREEN} Done.${RESET} "
0 commit comments