Skip to content
This repository was archived by the owner on Jun 15, 2022. It is now read-only.

Commit a7322a3

Browse files
Merge pull request #9 from liquidweb/fix/database-table-not-being-created
Ensure database table is being created on plugin installation.
2 parents 6ac4258 + 77022c5 commit a7322a3

File tree

9 files changed

+374
-1
lines changed

9 files changed

+374
-1
lines changed

.travis.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
sudo: false
2+
dist: trusty
3+
4+
language: php
5+
6+
notifications:
7+
email:
8+
on_success: never
9+
on_failure: change
10+
11+
branches:
12+
only:
13+
- master
14+
15+
cache:
16+
directories:
17+
- $HOME/.composer/cache
18+
19+
matrix:
20+
include:
21+
- php: 7.1
22+
env: WP_VERSION=latest
23+
- php: 7.0
24+
env: WP_VERSION=latest
25+
- php: 5.6
26+
env: WP_VERSION=latest
27+
- php: 5.6
28+
env: WP_VERSION=trunk
29+
- php: 5.6
30+
env: WP_TRAVISCI=phpcs
31+
- php: 5.3
32+
env: WP_VERSION=latest
33+
dist: precise
34+
35+
before_script:
36+
- export PATH="$HOME/.composer/vendor/bin:$PATH"
37+
- |
38+
if [ -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ]; then
39+
phpenv config-rm xdebug.ini
40+
else
41+
echo "xdebug.ini does not exist"
42+
fi
43+
- |
44+
if [[ ! -z "$WP_VERSION" ]] ; then
45+
bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
46+
composer global require "phpunit/phpunit=4.8.*|5.7.*"
47+
fi
48+
- |
49+
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
50+
composer global require wp-coding-standards/wpcs
51+
phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs
52+
fi
53+
54+
script:
55+
- |
56+
if [[ ! -z "$WP_VERSION" ]] ; then
57+
phpunit
58+
WP_MULTISITE=1 phpunit
59+
fi
60+
- |
61+
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
62+
phpcs
63+
fi

bin/install-wp-tests.sh

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
fi
26+
}
27+
28+
if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
29+
WP_TESTS_TAG="branches/$WP_VERSION"
30+
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
31+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
32+
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
33+
WP_TESTS_TAG="tags/${WP_VERSION%??}"
34+
else
35+
WP_TESTS_TAG="tags/$WP_VERSION"
36+
fi
37+
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
38+
WP_TESTS_TAG="trunk"
39+
else
40+
# http serves a single offer, whereas https serves multiple. we only want one
41+
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
42+
grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
43+
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
44+
if [[ -z "$LATEST_VERSION" ]]; then
45+
echo "Latest WordPress version could not be found"
46+
exit 1
47+
fi
48+
WP_TESTS_TAG="tags/$LATEST_VERSION"
49+
fi
50+
51+
set -ex
52+
53+
install_wp() {
54+
55+
if [ -d $WP_CORE_DIR ]; then
56+
return;
57+
fi
58+
59+
mkdir -p $WP_CORE_DIR
60+
61+
if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
62+
mkdir -p $TMPDIR/wordpress-nightly
63+
download https://wordpress.org/nightly-builds/wordpress-latest.zip $TMPDIR/wordpress-nightly/wordpress-nightly.zip
64+
unzip -q $TMPDIR/wordpress-nightly/wordpress-nightly.zip -d $TMPDIR/wordpress-nightly/
65+
mv $TMPDIR/wordpress-nightly/wordpress/* $WP_CORE_DIR
66+
else
67+
if [ $WP_VERSION == 'latest' ]; then
68+
local ARCHIVE_NAME='latest'
69+
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then
70+
# https serves multiple offers, whereas http serves single.
71+
download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json
72+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
73+
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
74+
LATEST_VERSION=${WP_VERSION%??}
75+
else
76+
# otherwise, scan the releases and get the most up to date minor version of the major release
77+
local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'`
78+
LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1)
79+
fi
80+
if [[ -z "$LATEST_VERSION" ]]; then
81+
local ARCHIVE_NAME="wordpress-$WP_VERSION"
82+
else
83+
local ARCHIVE_NAME="wordpress-$LATEST_VERSION"
84+
fi
85+
else
86+
local ARCHIVE_NAME="wordpress-$WP_VERSION"
87+
fi
88+
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz
89+
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
90+
fi
91+
92+
download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
93+
}
94+
95+
install_test_suite() {
96+
# portable in-place argument for both GNU sed and Mac OSX sed
97+
if [[ $(uname -s) == 'Darwin' ]]; then
98+
local ioption='-i .bak'
99+
else
100+
local ioption='-i'
101+
fi
102+
103+
# set up testing suite if it doesn't yet exist
104+
if [ ! -d $WP_TESTS_DIR ]; then
105+
# set up testing suite
106+
mkdir -p $WP_TESTS_DIR
107+
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
108+
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
109+
fi
110+
111+
if [ ! -f wp-tests-config.php ]; then
112+
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
113+
# remove all forward slashes in the end
114+
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
115+
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
116+
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
117+
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
118+
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
119+
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
120+
fi
121+
122+
}
123+
124+
install_db() {
125+
126+
if [ ${SKIP_DB_CREATE} = "true" ]; then
127+
return 0
128+
fi
129+
130+
# parse DB_HOST for port or socket references
131+
local PARTS=(${DB_HOST//\:/ })
132+
local DB_HOSTNAME=${PARTS[0]};
133+
local DB_SOCK_OR_PORT=${PARTS[1]};
134+
local EXTRA=""
135+
136+
if ! [ -z $DB_HOSTNAME ] ; then
137+
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
138+
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
139+
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
140+
EXTRA=" --socket=$DB_SOCK_OR_PORT"
141+
elif ! [ -z $DB_HOSTNAME ] ; then
142+
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
143+
fi
144+
fi
145+
146+
# create database
147+
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
148+
}
149+
150+
install_wp
151+
install_test_suite
152+
install_db

phpcs.xml.dist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="WordPress Coding Standards for Plugins">
3+
<description>Generally-applicable sniffs for WordPress plugins</description>
4+
5+
<rule ref="WordPress-Core" />
6+
<rule ref="WordPress-Docs" />
7+
8+
<!-- Check all PHP files in directory tree by default. -->
9+
<arg name="extensions" value="php"/>
10+
<file>.</file>
11+
12+
<!-- Show progress and sniff codes in all reports -->
13+
<arg value="ps"/>
14+
15+
<exclude-pattern>*/node_modules/*</exclude-pattern>
16+
<exclude-pattern>*/vendor/*</exclude-pattern>
17+
</ruleset>

phpunit.xml.dist

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<phpunit
3+
bootstrap="tests/bootstrap.php"
4+
backupGlobals="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
>
10+
<testsuites>
11+
<testsuite>
12+
<directory prefix="test-" suffix=".php">./tests/</directory>
13+
<exclude>tests/test-sample.php</exclude>
14+
</testsuite>
15+
</testsuites>
16+
</phpunit>

tests/bootstrap.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* PHPUnit bootstrap file
4+
*
5+
* @package Woocommerce_Order_Tables
6+
*/
7+
8+
$_tests_dir = getenv( 'WP_TESTS_DIR' );
9+
10+
if ( ! $_tests_dir ) {
11+
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
12+
}
13+
14+
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
15+
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL;
16+
exit( 1 );
17+
}
18+
19+
// Give access to tests_add_filter() function.
20+
require_once $_tests_dir . '/includes/functions.php';
21+
22+
/**
23+
* Manually load the plugin being tested.
24+
*/
25+
function _manually_load_plugin() {
26+
require dirname( dirname( __FILE__ ) ) . '/wc-custom-order-table.php';
27+
}
28+
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
29+
30+
// Start up the WP testing environment.
31+
require $_tests_dir . '/includes/bootstrap.php';
32+
require __DIR__ . '/testcase.php';

tests/test-installation.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Tests for the plugin installation.
4+
*
5+
* @package Woocommerce_Order_Tables
6+
* @author Liquid Web
7+
*/
8+
9+
class InstallationTest extends TestCase {
10+
11+
public function setUp() {
12+
self::drop_orders_table();
13+
}
14+
15+
public function test_table_is_created_on_plugin_activation() {
16+
$this->assertFalse(
17+
self::orders_table_exists(),
18+
'The wp_woocommerce_orders table should not exist at the beginning of this test.'
19+
);
20+
21+
self::reactivate_plugin();
22+
23+
$this->assertTrue(
24+
self::orders_table_exists(),
25+
'Upon activation, the table should be created.'
26+
);
27+
}
28+
}

tests/test-sample.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Class SampleTest
4+
*
5+
* @package Woocommerce_Order_Tables
6+
*/
7+
8+
/**
9+
* Sample test case.
10+
*/
11+
class SampleTest extends WP_UnitTestCase {
12+
13+
/**
14+
* A single example test.
15+
*/
16+
function test_sample() {
17+
// Replace this with some actual testing code.
18+
$this->assertTrue( true );
19+
}
20+
}

tests/testcase.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Base test case for WooCommerce Custom Order Tables.
4+
*
5+
* @package Woocommerce_Order_Tables
6+
* @author Liquid Web
7+
*/
8+
9+
class TestCase extends WP_UnitTestCase {
10+
11+
/**
12+
* Determine if the custom orders table exists.
13+
*
14+
* @global $wpdb
15+
*/
16+
protected static function orders_table_exists() {
17+
global $wpdb;
18+
19+
return (bool) $wpdb->get_var( $wpdb->prepare(
20+
'SELECT COUNT(*) FROM information_schema.tables WHERE table_name = %s LIMIT 1',
21+
$wpdb->prefix . 'woocommerce_orders'
22+
) );
23+
}
24+
25+
/**
26+
* Drop the wp_woocommerce_orders table.
27+
*
28+
* @global $wpdb
29+
*/
30+
protected static function drop_orders_table() {
31+
global $wpdb;
32+
33+
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_orders" );
34+
}
35+
36+
/**
37+
* Emulate deactivating, then subsequently reactivating the plugin.
38+
*/
39+
protected static function reactivate_plugin() {
40+
$plugin = plugin_basename( dirname( __DIR__ ) . '/wc-custom-order-table.php' );
41+
42+
do_action( 'deactivate_' . $plugin, false );
43+
do_action( 'activate_' . $plugin, false );
44+
}
45+
}

wc-custom-order-table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
function wc_custom_order_table_install() {
3434
$installer = new WC_Custom_Order_Table_Install();
35-
register_activation_hook( __FILE__, array( $installer, 'activate' ) );
35+
$installer->activate();
3636
}
3737

3838
register_activation_hook( __FILE__, 'wc_custom_order_table_install' );

0 commit comments

Comments
 (0)