Skip to content

Commit 5197fd0

Browse files
Merge pull request #24 from Automattic/tools_required_permissions_callback
Tools required permissions callback
2 parents 1917bb2 + a566998 commit 5197fd0

15 files changed

+3377
-32
lines changed

.circleci/config.yml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
workflows:
2+
version: 2
3+
main:
4+
jobs:
5+
- php80-build
6+
- php81-build
7+
- php82-build
8+
- php83-build
9+
10+
version: 2
11+
12+
job-references:
13+
mysql_image: &mysql_image circleci/mysql:8.0
14+
15+
setup_environment: &setup_environment
16+
name: 'Setup Environment Variables'
17+
command: |
18+
echo "export PATH=$HOME/.composer/vendor/bin:$PATH" >> $BASH_ENV
19+
source /home/circleci/.bashrc
20+
21+
install_dependencies: &install_dependencies
22+
name: 'Install Dependencies'
23+
command: |
24+
sudo apt-get update && sudo apt-get install subversion
25+
sudo -E docker-php-ext-install mysqli
26+
sudo sh -c "printf '\ndeb http://ftp.us.debian.org/debian sid main\n' >> /etc/apt/sources.list"
27+
sudo apt-get update && sudo apt-get install mysql-client-5.7
28+
29+
php_job: &php_job
30+
environment:
31+
- WP_TESTS_DIR: '/tmp/wordpress-tests-lib'
32+
- WP_CORE_DIR: '/tmp/wordpress/'
33+
steps:
34+
- checkout
35+
- run: *setup_environment
36+
- run: *install_dependencies
37+
- run:
38+
name: 'Run Tests'
39+
command: |
40+
composer global require "phpunit/phpunit=5.7.*"
41+
composer global require wp-coding-standards/wpcs
42+
phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs
43+
phpcs
44+
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
45+
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 latest
46+
phpunit
47+
WP_MULTISITE=1 phpunit
48+
49+
jobs:
50+
php56-build:
51+
<<: *php_job
52+
docker:
53+
- image: circleci/php:5.6
54+
- image: *mysql_image
55+
steps:
56+
- checkout
57+
- run: *setup_environment
58+
- run: *install_dependencies
59+
- run:
60+
name: 'Run Tests'
61+
command: |
62+
composer global require "phpunit/phpunit=5.7.*"
63+
composer global require wp-coding-standards/wpcs
64+
phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs
65+
phpcs
66+
SKIP_DB_CREATE=false
67+
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
68+
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 latest $SKIP_DB_CREATE
69+
phpunit
70+
WP_MULTISITE=1 phpunit
71+
SKIP_DB_CREATE=true
72+
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
73+
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 trunk $SKIP_DB_CREATE
74+
phpunit
75+
WP_MULTISITE=1 phpunit
76+
SKIP_DB_CREATE=true
77+
78+
php70-build:
79+
<<: *php_job
80+
docker:
81+
- image: circleci/php:7.0
82+
- image: *mysql_image
83+
84+
php71-build:
85+
<<: *php_job
86+
docker:
87+
- image: circleci/php:7.1
88+
- image: *mysql_image
89+
90+
php72-build:
91+
<<: *php_job
92+
docker:
93+
- image: circleci/php:7.2
94+
- image: *mysql_image
95+
96+
php73-build:
97+
<<: *php_job
98+
docker:
99+
- image: circleci/php:7.3
100+
- image: *mysql_image
101+
102+
php74-build:
103+
<<: *php_job
104+
docker:
105+
- image: circleci/php:7.4
106+
- image: *mysql_image

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.DS_Store
44
/build/
55
/wordpress-mcp.zip
6+
/.phpunit.result.cache

bin/install-wp-tests.sh

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
#!/usr/bin/env bash
2+
3+
if [ $# -lt 3 ]; then
4+
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
5+
exit 1
6+
fi
7+
8+
DB_NAME=$1
9+
DB_USER=$2
10+
DB_PASS=$3
11+
DB_HOST=${4-localhost}
12+
WP_VERSION=${5-latest}
13+
SKIP_DB_CREATE=${6-false}
14+
15+
TMPDIR=${TMPDIR-/tmp}
16+
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
17+
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
18+
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress}
19+
20+
download() {
21+
if [ `which curl` ]; then
22+
curl -s "$1" > "$2";
23+
elif [ `which wget` ]; then
24+
wget -nv -O "$2" "$1"
25+
else
26+
echo "Error: Neither curl nor wget is installed."
27+
exit 1
28+
fi
29+
}
30+
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
37+
}
38+
39+
if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then
40+
WP_BRANCH=${WP_VERSION%\-*}
41+
WP_TESTS_TAG="branches/$WP_BRANCH"
42+
43+
elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
44+
WP_TESTS_TAG="branches/$WP_VERSION"
45+
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
46+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
47+
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
48+
WP_TESTS_TAG="tags/${WP_VERSION%??}"
49+
else
50+
WP_TESTS_TAG="tags/$WP_VERSION"
51+
fi
52+
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
53+
WP_TESTS_TAG="trunk"
54+
else
55+
# http serves a single offer, whereas https serves multiple. we only want one
56+
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":"//')
59+
if [[ -z "$LATEST_VERSION" ]]; then
60+
echo "Latest WordPress version could not be found"
61+
exit 1
62+
fi
63+
WP_TESTS_TAG="tags/$LATEST_VERSION"
64+
fi
65+
set -ex
66+
67+
install_wp() {
68+
69+
if [ -d $WP_CORE_DIR ]; then
70+
return;
71+
fi
72+
73+
mkdir -p $WP_CORE_DIR
74+
75+
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
81+
else
82+
if [ $WP_VERSION == 'latest' ]; then
83+
local ARCHIVE_NAME='latest'
84+
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then
85+
# https serves multiple offers, whereas http serves single.
86+
download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json
87+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
88+
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
89+
LATEST_VERSION=${WP_VERSION%??}
90+
else
91+
# otherwise, scan the releases and get the most up to date minor version of the major release
92+
local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'`
93+
LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1)
94+
fi
95+
if [[ -z "$LATEST_VERSION" ]]; then
96+
local ARCHIVE_NAME="wordpress-$WP_VERSION"
97+
else
98+
local ARCHIVE_NAME="wordpress-$LATEST_VERSION"
99+
fi
100+
else
101+
local ARCHIVE_NAME="wordpress-$WP_VERSION"
102+
fi
103+
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz
104+
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
105+
fi
106+
107+
download https://raw.githubusercontent.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
108+
}
109+
110+
install_test_suite() {
111+
# portable in-place argument for both GNU sed and Mac OSX sed
112+
if [[ $(uname -s) == 'Darwin' ]]; then
113+
local ioption='-i.bak'
114+
else
115+
local ioption='-i'
116+
fi
117+
118+
# set up testing suite if it doesn't yet exist
119+
if [ ! -d $WP_TESTS_DIR ]; then
120+
# set up testing suite
121+
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
126+
fi
127+
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
130+
# remove all forward slashes in the end
131+
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
132+
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
133+
sed $ioption "s:__DIR__ . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
134+
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
135+
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
136+
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
137+
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
138+
fi
139+
140+
}
141+
142+
recreate_db() {
143+
shopt -s nocasematch
144+
if [[ $1 =~ ^(y|yes)$ ]]
145+
then
146+
mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA
147+
create_db
148+
echo "Recreated the database ($DB_NAME)."
149+
else
150+
echo "Leaving the existing database ($DB_NAME) in place."
151+
fi
152+
shopt -u nocasematch
153+
}
154+
155+
create_db() {
156+
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
157+
}
158+
159+
install_db() {
160+
161+
if [ ${SKIP_DB_CREATE} = "true" ]; then
162+
return 0
163+
fi
164+
165+
# parse DB_HOST for port or socket references
166+
local PARTS=(${DB_HOST//\:/ })
167+
local DB_HOSTNAME=${PARTS[0]};
168+
local DB_SOCK_OR_PORT=${PARTS[1]};
169+
local EXTRA=""
170+
171+
if ! [ -z $DB_HOSTNAME ] ; then
172+
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
173+
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
174+
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
175+
EXTRA=" --socket=$DB_SOCK_OR_PORT"
176+
elif ! [ -z $DB_HOSTNAME ] ; then
177+
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
178+
fi
179+
fi
180+
181+
# create database
182+
if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ]
183+
then
184+
echo "Reinstalling will delete the existing test database ($DB_NAME)"
185+
read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB
186+
recreate_db $DELETE_EXISTING_DB
187+
else
188+
create_db
189+
fi
190+
}
191+
192+
install_wp
193+
install_test_suite
194+
install_db

composer.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "ovidiugalatan/wordpress-mcp",
3+
"description": "WordPress MCP Plugin",
4+
"type": "wordpress-plugin",
5+
"require-dev": {
6+
"phpunit/phpunit": "^9.0",
7+
"yoast/phpunit-polyfills": "^1.0"
8+
},
9+
"config": {
10+
"allow-plugins": {
11+
"dealerdirect/phpcodesniffer-composer-installer": true
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)