Skip to content

Commit 6dea035

Browse files
authored
Delete dirsize_cache transient before getting WP disk size (#559)
* Delete dirsize_cache transient before getting WP disk size * Fix PHP lint test for PHP 5.6 * Test PHP 5.6 on WP 6.2 * Update WP installed script * Update .travis.yml * Migrate deprecated phpunit.xml * Fix PHPUnit tests
1 parent 696a522 commit 6dea035

8 files changed

+155
-82
lines changed

.travis.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ matrix:
2121
# Lock file has phpunit 7. Remove it and install phpunit 5 for php 5.6.
2222
- composer remove --dev phpunit/phpunit && composer require --dev phpunit/phpunit ^5
2323
- composer install -o
24-
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
24+
- bash bin/install-wp-tests.sh wordpress_test root '' localhost 6.2
2525
# We are only running phpcs on php 5.6. @todo fix with php 7 and 8.
2626
- yarn run php-codesniffer
2727
-
@@ -48,8 +48,8 @@ matrix:
4848
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
4949

5050
script:
51-
- find . -name composer -prune -o -name node_modules -prune -o -name '*.php' -exec php -lf {} \; > /dev/null
52-
- vendor/phpunit/phpunit/phpunit --debug
51+
- find . \( -name composer -o -name node_modules -o -name phpunit -o -name phpunit-polyfills -o -name code-unit-reverse-lookup \) -prune -o -name '*.php' -exec php -lf {} \; > /dev/null
52+
- vendor/phpunit/phpunit/phpunit --debug --verbose
5353
- yarn run js-lint
5454
# Remove dev scripts.
5555
- composer install -o --no-dev
@@ -60,8 +60,11 @@ deploy:
6060
skip_cleanup: true
6161
on:
6262
tags: true
63+
php: 5.6
6364
- provider: releases
6465
api_key: "${GITHUB_TOKEN}"
6566
file: "boldgrid-backup.zip"
67+
skip_cleanup: true
6668
on:
6769
tags: true
70+
php: 5.6

admin/class-boldgrid-backup-admin-filelist.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public function get_size() {
9595

9696
$size = 0;
9797

98+
// Delete transient "dirsize_cache" that WordPress sets when using recurse_dirsize(); used for Site Health info, our REST endpoint, and here.
99+
delete_transient( 'dirsize_cache' );
100+
98101
foreach ( $this->filelist_filter as $file ) {
99102
$file_path = ABSPATH . $file;
100103

admin/class-boldgrid-backup-admin-test.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,13 +693,12 @@ public function get_disk_space( $get_wp_size = true ) {
693693
* Get the WordPress total file size.
694694
*
695695
* @since 1.0
696-
* @access private
697696
*
698697
* @see get_filtered_filelist
699698
*
700699
* @return int|bool The total size for the WordPress file system in bytes, or FALSE on error.
701700
*/
702-
private function get_wp_size() {
701+
public function get_wp_size() {
703702
// Save time, use transients.
704703
$transient = get_transient( 'boldgrid_backup_wp_size' );
705704

bin/install-wp-tests.sh

Lines changed: 98 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
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

36
if [ $# -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
69
fi
710

@@ -10,27 +13,86 @@ DB_USER=$2
1013
DB_PASS=$3
1114
DB_HOST=${4-localhost}
1215
WP_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

1754
set -ex
1855

1956
install_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

3698
install_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

58127
install_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]};

phpunit.xml

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
<phpunit
2-
bootstrap="tests/bootstrap.php"
3-
backupGlobals="false"
4-
colors="true"
5-
convertErrorsToExceptions="true"
6-
convertNoticesToExceptions="true"
7-
convertWarningsToExceptions="true"
8-
>
9-
<testsuites>
10-
<testsuite name="tests">
11-
<directory suffix=".php">./tests/</directory>
12-
</testsuite>
13-
</testsuites>
14-
<filter>
15-
<whitelist>
16-
<directory>./</directory>
17-
</whitelist>
18-
</filter>
19-
<groups>
20-
<exclude>
21-
<group>ajax</group>
22-
</exclude>
23-
</groups>
24-
<php>
25-
<ini name="display_errors" value="true"/>
26-
</php>
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" backupGlobals="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory>./</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="tests">
10+
<directory suffix=".php">./tests/</directory>
11+
</testsuite>
12+
</testsuites>
13+
<groups>
14+
<exclude>
15+
<group>ajax</group>
16+
</exclude>
17+
</groups>
18+
<php>
19+
<ini name="display_errors" value="true"/>
20+
</php>
2721
</phpunit>

rest/class-boldgrid-backup-rest-test.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,21 @@ public function get_item( $request ) {
152152

153153
$preflight_test = new Boldgrid_Backup_Admin_Test( $this->core );
154154

155-
$disk_space = $preflight_test->get_disk_space( false );
156-
$disk_abspath = get_dirsize( ABSPATH );
157-
$disk_db = $preflight_test->get_database_size();
155+
$disk_space = $preflight_test->get_disk_space( false );
156+
$disk_wp = $preflight_test->get_wp_size();
157+
$disk_db = $preflight_test->get_database_size();
158158

159159
$settings = array(
160160
'passed' => $preflight_test->run_functionality_tests(),
161161
'php_version' => phpversion(),
162162
'wordpress_version' => $wp_version,
163163
'abspath' => ABSPATH,
164-
'disk_abspath' => $disk_abspath,
164+
'disk_abspath' => $disk_wp,
165165
'disk_database' => $disk_db,
166166
'disk_total' => $disk_space[0],
167167
'disk_used' => $disk_space[1],
168168
'disk_free' => $disk_space[2],
169-
'disk_post_backup' => $disk_space[2] - $disk_abspath - $disk_db,
169+
'disk_post_backup' => $disk_space[2] - $disk_wp - $disk_db,
170170
);
171171

172172
return $this->prepare_item_for_response( $settings, $request );

tests/admin/test-class-boldgrid-backup-admin-auto-updates.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -188,25 +188,25 @@ public function test_maybe_update_plugin() {
188188
*/
189189
public function test_maybe_update_theme() {
190190
// Test does not update if this theme hasn't been configured yet.
191-
$twentytwenty_test_settings = $this->default_test_settings;
192-
update_option( 'boldgrid_backup_settings', $twentytwenty_test_settings );
191+
$twentytwentytwo_test_settings = $this->default_test_settings;
192+
update_option( 'boldgrid_backup_settings', $twentytwentytwo_test_settings );
193193
$auto_updates = new Boldgrid_backup_Admin_Auto_Updates();
194-
$this->assertFalse( $auto_updates->maybe_update_theme( 'twentytwenty' ) );
194+
$this->assertFalse( $auto_updates->maybe_update_theme( 'twentytwentytwo' ) );
195195

196196
// Test does not update if this theme has updates disabled.
197-
$twentytwenty_test_settings['auto_update']['themes']['twentytwenty'] = '0';
198-
update_option( 'boldgrid_backup_settings', $twentytwenty_test_settings );
197+
$twentytwentytwo_test_settings['auto_update']['themes']['twentytwentytwo'] = '0';
198+
update_option( 'boldgrid_backup_settings', $twentytwentytwo_test_settings );
199199
$auto_updates->set_settings();
200-
$this->assertFalse( $auto_updates->maybe_update_theme( 'twentytwenty' ) );
200+
$this->assertFalse( $auto_updates->maybe_update_theme( 'twentytwentytwo' ) );
201201

202202
// Test that updates do occur if enabled.
203-
$twentytwenty_test_settings['auto_update']['themes']['twentytwenty'] = '1';
204-
update_option( 'boldgrid_backup_settings', $twentytwenty_test_settings );
203+
$twentytwentytwo_test_settings['auto_update']['themes']['twentytwentytwo'] = '1';
204+
update_option( 'boldgrid_backup_settings', $twentytwentytwo_test_settings );
205205
$auto_updates->set_settings();
206-
$this->assertTrue( $auto_updates->maybe_update_theme( 'twentytwenty' ) );
206+
$this->assertTrue( $auto_updates->maybe_update_theme( 'twentytwentytwo' ) );
207207

208208
$themes = new \Boldgrid\Library\Library\Theme\Themes();
209-
$theme = $themes->getFromStylesheet( 'twentytwenty' );
209+
$theme = $themes->getFromStylesheet( 'twentytwentytwo' );
210210
$theme->setUpdateData();
211211
$days = $theme->updateData->days; //phpcs:ignore WordPress.NamingConventions.ValidVariableName
212212

@@ -216,22 +216,22 @@ public function test_maybe_update_theme() {
216216
->getMock();
217217
$mock_auto_updates->method( 'is_premium_done' )
218218
->will( $this->returnValue( false ) );
219-
$this->assertTrue( $mock_auto_updates->maybe_update_theme( 'twentytwenty' ) );
219+
$this->assertTrue( $mock_auto_updates->maybe_update_theme( 'twentytwentytwo' ) );
220220

221221
// Test updates will occur if within the day's option with premium active.
222222
$mock_auto_updates = $this->getMockBuilder( Boldgrid_backup_Admin_Auto_Updates::class )
223223
->setMethods( array( 'is_premium_done' ) )
224224
->getMock();
225225
$mock_auto_updates->method( 'is_premium_done' )
226226
->will( $this->returnValue( true ) );
227-
$this->assertTrue( $mock_auto_updates->maybe_update_theme( 'twentytwenty' ) );
227+
$this->assertTrue( $mock_auto_updates->maybe_update_theme( 'twentytwentytwo' ) );
228228

229229
// Test updates will not occur if not within the day's option with premium active.
230-
$twentytwenty_test_settings['auto_update']['days'] = (int) $days + 10;
231-
$twentytwenty_test_settings['auto_update']['timely-updates-enabled'] = true;
232-
update_option( 'boldgrid_backup_settings', $twentytwenty_test_settings );
230+
$twentytwentytwo_test_settings['auto_update']['days'] = (int) $days + 10;
231+
$twentytwentytwo_test_settings['auto_update']['timely-updates-enabled'] = true;
232+
update_option( 'boldgrid_backup_settings', $twentytwentytwo_test_settings );
233233
$mock_auto_updates->set_settings();
234-
$this->assertFalse( $mock_auto_updates->maybe_update_theme( 'twentytwenty' ) );
234+
$this->assertFalse( $mock_auto_updates->maybe_update_theme( 'twentytwentytwo' ) );
235235
}
236236

237237
/**
@@ -265,8 +265,8 @@ public function test_auto_update_themes() {
265265
->will( $this->returnValue( true ) );
266266
$themes = new \Boldgrid\Library\Library\Theme\Themes();
267267
foreach ( $themes->get() as $theme ) {
268-
if ( 'twentytwenty' === $theme->stylesheet ) {
269-
$this->assertTrue( $mock_auto_updates->auto_update_themes( true, (object) array( 'theme' => 'twentytwenty' ) ) );
268+
if ( 'twentytwentytwo' === $theme->stylesheet ) {
269+
$this->assertTrue( $mock_auto_updates->auto_update_themes( true, (object) array( 'theme' => 'twentytwentytwo' ) ) );
270270
}
271271
}
272272

@@ -277,8 +277,8 @@ public function test_auto_update_themes() {
277277
->will( $this->returnValue( false ) );
278278
$themes = new \Boldgrid\Library\Library\Theme\Themes();
279279
foreach ( $themes->get() as $theme ) {
280-
if ( 'twentytwenty' === $theme->stylesheet ) {
281-
$this->assertFalse( $mock_auto_updates->auto_update_themes( true, (object) array( 'theme' => 'twentytwenty' ) ) );
280+
if ( 'twentytwentytwo' === $theme->stylesheet ) {
281+
$this->assertFalse( $mock_auto_updates->auto_update_themes( true, (object) array( 'theme' => 'twentytwentytwo' ) ) );
282282
}
283283
}
284284
}

tests/rest/class-boldgrid-backup-rest-case.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ public function set_up() {
8585

8686
do_action( 'rest_api_init' );
8787

88-
$this->editor_id = $this->factory->user->create( array(
88+
$this->editor_id = $this->factory()->user->create( array(
8989
'role' => 'editor',
9090
'display_name' => 'test_editor',
9191
) );
9292

93-
$this->admin_id = $this->factory->user->create( array(
93+
$this->admin_id = $this->factory()->user->create( array(
9494
'role' => 'administrator',
9595
'display_name' => 'test_admin',
9696
) );

0 commit comments

Comments
 (0)