11#! /usr/bin/env bash
2+ # WordPress scaffolding for tests of the W3 Total Cache WordPress plugin by BoldGrid.
3+ # @link https://make.wordpress.org/cli/handbook/misc/plugin-unit-tests/
4+ # @link https://github.com/wp-cli/sample-plugin/blob/master/bin/install-wp-tests.sh
25
36if [ $# -lt 3 ]; then
4- echo " usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version]"
7+ echo " usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation] "
58 exit 1
69fi
710
@@ -10,27 +13,86 @@ DB_USER=$2
1013DB_PASS=$3
1114DB_HOST=${4-localhost}
1215WP_VERSION=${5-latest}
16+ SKIP_DB_CREATE=${6-false}
1317
14- WP_TESTS_DIR=${WP_TESTS_DIR-/ tmp/ wordpress-tests-lib}
15- WP_CORE_DIR=${WP_CORE_DIR-/ tmp/ wordpress/ }
18+ TMPDIR=${TMPDIR-/ tmp}
19+ TMPDIR=$( echo $TMPDIR | sed -e " s/\/$//" )
20+ WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR / wordpress-tests-lib}
21+ WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR / wordpress/ }
22+
23+ download () {
24+ if [ ` which curl` ]; then
25+ curl -s " $1 " > " $2 " ;
26+ elif [ ` which wget` ]; then
27+ wget -nv -O " $2 " " $1 "
28+ fi
29+ }
30+
31+ if [[ $WP_VERSION =~ ^[0-9]+\. [0-9]+$ ]]; then
32+ WP_TESTS_TAG=" branches/$WP_VERSION "
33+ elif [[ $WP_VERSION =~ [0-9]+\. [0-9]+\. [0-9]+ ]]; then
34+ if [[ $WP_VERSION =~ [0-9]+\. [0-9]+\. [0] ]]; then
35+ # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
36+ WP_TESTS_TAG=" tags/${WP_VERSION% ??} "
37+ else
38+ WP_TESTS_TAG=" tags/$WP_VERSION "
39+ fi
40+ elif [[ $WP_VERSION == ' nightly' || $WP_VERSION == ' trunk' ]]; then
41+ WP_TESTS_TAG=" trunk"
42+ else
43+ # http serves a single offer, whereas https serves multiple. we only want one
44+ download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
45+ grep ' [0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
46+ LATEST_VERSION=$( grep -o ' "version":"[^"]*' /tmp/wp-latest.json | sed ' s/"version":"//' )
47+ if [[ -z " $LATEST_VERSION " ]]; then
48+ echo " Latest WordPress version could not be found"
49+ exit 1
50+ fi
51+ WP_TESTS_TAG=" tags/$LATEST_VERSION "
52+ fi
1653
1754set -ex
1855
1956install_wp () {
57+
58+ if [ -d $WP_CORE_DIR ]; then
59+ return ;
60+ fi
61+
2062 mkdir -p $WP_CORE_DIR
2163
22- if [ $WP_VERSION == ' latest' ]; then
23- local ARCHIVE_NAME=' latest'
64+ if [[ $WP_VERSION == ' nightly' || $WP_VERSION == ' trunk' ]]; then
65+ mkdir -p $TMPDIR /wordpress-nightly
66+ download https://wordpress.org/nightly-builds/wordpress-latest.zip $TMPDIR /wordpress-nightly/wordpress-nightly.zip
67+ unzip -q $TMPDIR /wordpress-nightly/wordpress-nightly.zip -d $TMPDIR /wordpress-nightly/
68+ mv $TMPDIR /wordpress-nightly/wordpress/* $WP_CORE_DIR
2469 else
25- local ARCHIVE_NAME=" wordpress-$WP_VERSION "
70+ if [ $WP_VERSION == ' latest' ]; then
71+ local ARCHIVE_NAME=' latest'
72+ elif [[ $WP_VERSION =~ [0-9]+\. [0-9]+ ]]; then
73+ # https serves multiple offers, whereas http serves single.
74+ download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR /wp-latest.json
75+ if [[ $WP_VERSION =~ [0-9]+\. [0-9]+\. [0] ]]; then
76+ # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
77+ LATEST_VERSION=${WP_VERSION% ??}
78+ else
79+ # otherwise, scan the releases and get the most up to date minor version of the major release
80+ local VERSION_ESCAPED=` echo $WP_VERSION | sed ' s/\./\\\\./g' `
81+ LATEST_VERSION=$( grep -o ' "version":"' $VERSION_ESCAPED ' [^"]*' $TMPDIR /wp-latest.json | sed ' s/"version":"//' | head -1)
82+ fi
83+ if [[ -z " $LATEST_VERSION " ]]; then
84+ local ARCHIVE_NAME=" wordpress-$WP_VERSION "
85+ else
86+ local ARCHIVE_NAME=" wordpress-$LATEST_VERSION "
87+ fi
88+ else
89+ local ARCHIVE_NAME=" wordpress-$WP_VERSION "
90+ fi
91+ download https://wordpress.org/${ARCHIVE_NAME} .tar.gz $TMPDIR /wordpress.tar.gz
92+ tar --strip-components=1 -zxmf $TMPDIR /wordpress.tar.gz -C $WP_CORE_DIR
2693 fi
2794
28- # Install the WordPress files.
29- # Unzip quietly (-qq) so the automated tests are not flooded with the unzip output.
30- wget -nv -O /tmp/wordpress-latest.zip https://wordpress.org/nightly-builds/wordpress-latest.zip
31- unzip -qq /tmp/wordpress-latest.zip -d /tmp
32-
33- wget -nv -O $WP_CORE_DIR /wp-content/db.php https://raw.github.com/markoheijnen/wp-mysqli/master/db.php
95+ download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR /wp-content/db.php
3496}
3597
3698install_test_suite () {
@@ -41,21 +103,33 @@ install_test_suite() {
41103 local ioption=' -i'
42104 fi
43105
44- # set up testing suite
45- mkdir -p $WP_TESTS_DIR
46- cd $WP_TESTS_DIR
47- # As each new version of WP is released, the branch should be updated in the 2 lines below.
48- svn co --quiet https://develop.svn.wordpress.org/branches/5.9/tests/phpunit/includes/
49- wget -nv -O wp-tests-config.php https://develop.svn.wordpress.org/branches/5.9/wp-tests-config-sample.php
50- sed $ioption " s:dirname( __FILE__ ) . '/build/':'$WP_CORE_DIR ':" " $WP_TESTS_DIR " /wp-tests-config.php
51- sed $ioption " s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR ':" " $WP_TESTS_DIR " /wp-tests-config.php
52- sed $ioption " s/youremptytestdbnamehere/$DB_NAME /" wp-tests-config.php
53- sed $ioption " s/yourusernamehere/$DB_USER /" wp-tests-config.php
54- sed $ioption " s/yourpasswordhere/$DB_PASS /" wp-tests-config.php
55- sed $ioption " s|localhost|${DB_HOST} |" wp-tests-config.php
106+ # set up testing suite if it doesn't yet exist
107+ if [ ! -d $WP_TESTS_DIR ]; then
108+ # set up testing suite
109+ mkdir -p $WP_TESTS_DIR
110+ svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG} /tests/phpunit/includes/ $WP_TESTS_DIR /includes
111+ svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG} /tests/phpunit/data/ $WP_TESTS_DIR /data
112+ fi
113+
114+ if [ ! -f wp-tests-config.php ]; then
115+ download https://develop.svn.wordpress.org/${WP_TESTS_TAG} /wp-tests-config-sample.php " $WP_TESTS_DIR " /wp-tests-config.php
116+ # remove all forward slashes in the end
117+ WP_CORE_DIR=$( echo $WP_CORE_DIR | sed " s:/\+$::" )
118+ sed $ioption " s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR /':" " $WP_TESTS_DIR " /wp-tests-config.php
119+ sed $ioption " s/youremptytestdbnamehere/$DB_NAME /" " $WP_TESTS_DIR " /wp-tests-config.php
120+ sed $ioption " s/yourusernamehere/$DB_USER /" " $WP_TESTS_DIR " /wp-tests-config.php
121+ sed $ioption " s/yourpasswordhere/$DB_PASS /" " $WP_TESTS_DIR " /wp-tests-config.php
122+ sed $ioption " s|localhost|${DB_HOST} |" " $WP_TESTS_DIR " /wp-tests-config.php
123+ fi
124+
56125}
57126
58127install_db () {
128+
129+ if [ ${SKIP_DB_CREATE} = " true" ]; then
130+ return 0
131+ fi
132+
59133 # parse DB_HOST for port or socket references
60134 local PARTS=(${DB_HOST// \: / } )
61135 local DB_HOSTNAME=${PARTS[0]} ;
0 commit comments