-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest-class-boldgrid-backup-admin-core.php
More file actions
386 lines (323 loc) · 9.96 KB
/
test-class-boldgrid-backup-admin-core.php
File metadata and controls
386 lines (323 loc) · 9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/**
* File: test-class-boldgrid-backup-admin-core.php
*
* @link https://www.boldgrid.com
* @since xxx
*
* @package Boldgrid_Backup
* @subpackage Boldgrid_Backup/tests/admin
* @copyright BoldGrid
* @version $Id$
* @author BoldGrid <support@boldgrid.com>
*
* phpcs:disable WordPress.VIP
*/
/**
* Class: Test_Boldgrid_Backup_Admin_Core
*
* @since xxx
*/
class Test_Boldgrid_Backup_Admin_Core extends WP_UnitTestCase {
/**
* Assert that a given dir in an archive has files and folders.
*
* This is a very generic test, doesn't need to be exact, as the WordPress core files will change
* over time.
*
* For example, we may run this and say:
* Make sure wp-admin folder has over 10 files totalling over 10000 bytes, and there's at least
* 3 folders.
*
* @since xxx
*
* @param string $filepath Path to the zip file.
* @param string $dir The dir within the zip to check.
* @param int $min_file_count The minimum number of files that need to be in the directory.
* @param int $min_file_size The minimum file size of all files in the directory.
* @param int $min_dir_count The minimum number of folders that need to be in the directory.
*/
public function assertDirNotEmpty( $filepath, $dir = '.', $min_file_count, $min_file_size, $min_dir_count ) {
$abspath = $this->zip->browse( $filepath, $dir );
$file_count = 0;
$file_size = 0;
$dir_count = 0;
foreach ( $abspath as $file ) {
if ( $file['folder'] ) {
$dir_count++;
} else {
$file_count++;
$file_size += $file['size'];
}
}
/*
* Debug. This is how you can see the actual counts / sizes in question.
phpunit_error_log( array(
'$dir' => $dir,
'$file_count' => $file_count,
'$file_size' => $file_size,
'$dir_count' => $dir_count,
) );
*/
$this->assertTrue( $file_count >= $min_file_count && $file_size >= $min_file_size && $dir_count >= $min_dir_count );
}
/**
* An instance core.
*
* @since xxx
* @var Boldgrid_Backup_Admin_Core
*/
public $core;
/**
* An array of info about a backup.
*
* This is the $info that is returned when a backup file is made.
*
* @since xxx
* @var array
*/
public $info;
/**
* The name of a view that we will be testing.
*
* @since 1.12.4
* @var string
*/
public $view_name;
/**
* An instance of pcl_zip.
*
* @since xxx
* @var Boldgrid_Backup_Admin_Compressor_Pcl_Zip
*/
public $zip;
/**
* Drop a table.
*
* @since xxx
*
* @param string $table Name of the table to delete.
*/
public function dropTable( $table ) {
global $wpdb;
$wpdb->query( 'DROP TABLE IF EXISTS ' . $table ); // phpcs:ignore
}
/**
* Create a wp-config file.
*
* This file doesn't actually exist within the testing environment, and needs to exist for the
* restore via cli script.
*
* @since xxx
*/
public function createWpconfig() {
$wpconfig = '
define( \'DB_NAME\', \'' . DB_NAME . '\' );
define( \'DB_USER\', \'' . DB_USER . '\' );
define( \'DB_PASSWORD\', \'' . DB_PASSWORD . '\' );
define( \'DB_HOST\', \'' . DB_HOST . '\' );
define( \'DB_CHARSET\', \'' . DB_CHARSET . '\' );
define( \'DB_COLLATE\', \'\' );
';
file_put_contents( trailingslashit( ABSPATH ) . 'wp-config.php', $wpconfig ); // phpcs:ignore
}
/**
* Delete a few files, drop a few tables, and make sure the delete / drop worked as expected.
*
* This method is intended to be ran before / after a restore method is executed. It is used to
* delete files / drop tables, and after a restore, it makes sure those files and tables are
* back.
*
* @since xxx.
*
* @param string $action Action.
*/
public function deleteBasic( $action = 'delete' ) {
global $wpdb;
$files_to_delete = [
trailingslashit( ABSPATH ) . 'wp-load.php',
trailingslashit( ABSPATH ) . 'wp-includes/theme.php',
trailingslashit( ABSPATH ) . 'wp-includes/rest-api/class-wp-rest-request.php',
];
$tables_to_drop = [
'wptests_commentmeta',
'wptests_comments',
];
switch ( $action ) {
case 'delete':
foreach ( $files_to_delete as $file ) {
$this->assertTrue( file_exists( $file ) );
unlink( $file );
$this->assertFalse( file_exists( $file ) );
}
foreach ( $tables_to_drop as $table ) {
$tables = $this->getTables();
$this->assertTrue( in_array( $table, $tables, true ) );
$this->dropTable( $table );
$tables = $this->getTables();
$this->assertFalse( in_array( $table, $tables, true ) );
}
// Delete the view we created. We should now have 0 views.
$sql = 'DROP VIEW ' . $this->view_name;
$wpdb->query( $sql ); // phpcs:ignore
$views = $this->core->db_get->get_by_type( 'VIEW' );
$this->assertTrue( 0 === count( $views ) );
break;
case 'restore':
foreach ( $files_to_delete as $file ) {
$this->assertTrue( file_exists( $file ) );
}
$tables = $this->getTables();
foreach ( $tables_to_drop as $table ) {
$this->assertTrue( in_array( $table, $tables, true ) );
}
// Ensure our view was restored.
$views = $this->core->db_get->get_by_type( 'VIEW' );
$this->assertTrue( 1 === count( $views ) );
break;
}
}
/**
* Get an array of our db tables.
*
* @since xxx
*
* @return array
*/
public function getTables() {
global $wpdb;
$tables = [];
$sql_tables = $wpdb->get_results( 'SHOW TABLES' );
foreach ( $sql_tables as $table ) {
// Get the name of the table.
$vars = get_object_vars( $table );
$table_name = reset( $vars );
$tables[] = $table_name;
}
return $tables;
}
/**
* Setup.
*
* @since xxx
*/
public function setUp() {
global $wpdb;
if ( ! defined( 'BOLDGRID_BACKUP_VERSION' ) ) {
$file = dirname( dirname( dirname( __FILE__ ) ) ) . '/boldgrid-backup.php';
define( 'BOLDGRID_BACKUP_VERSION', implode( get_file_data( $file, array( 'Version' ), 'plugin' ) ) );
}
$this->fake_restore_helper = $this->getMockBuilder( Boldgrid_Backup_Admin_Restore_Helper::class )
->setMethods( [ 'is_compressor_type' ] )
->getMock();
$this->fake_restore_helper->method( 'is_compressor_type' )->willReturn( 'PclZip' );
$this->core = apply_filters( 'boldgrid_backup_get_core', null );
$this->zip = new Boldgrid_Backup_Admin_Compressor_Pcl_Zip( $this->core );
$this->view_name = $wpdb->prefix . 'view1';
}
/**
* Test archive_files.
*
* @since xxx
*/
public function test_archive_files() {
global $wpdb;
// Delete our latest_backup variable.
delete_option( 'boldgrid_backup_latest_backup' );
$this->assertFalse( get_option( 'boldgrid_backup_latest_backup' ) );
/*
* Basic test.
*
* This is a generic backup test (IE backup all files and folders and tables).
*/
/*
* Before backing up, create a view to ensure it's backed up / restored properly.
*
* @link https://wordpress.org/support/topic/backup-fails-because-of-some-database-error/
*/
$sql = 'CREATE OR REPLACE VIEW ' . $this->view_name . ' AS SELECT * FROM ' . $wpdb->prefix . 'options WHERE `option_id` = 10';
$wpdb->query( $sql ); // phpcs:ignore
$views = $this->core->db_get->get_by_type( 'VIEW' );
$this->assertTrue( 1 === count( $views ) );
$this->info = $this->core->archive_files( true );
// Ensure a backup was made and we have a filepath.
$this->assertTrue( ! empty( $this->info['filepath'] ) );
// Ensure the $this->info returned matches the data stored in the boldgrid_backup_latest_backup option.
$this->assertTrue( get_option( 'boldgrid_backup_latest_backup' ) === $this->info );
// Ensure we have files and folders (IE they're not empty and the backup process recursed).
$this->assertDirNotEmpty( $this->info['filepath'], '.', 10, 10000, 3 );
$this->assertDirNotEmpty( $this->info['filepath'], 'wp-includes', 10, 10000, 3 );
$this->assertDirNotEmpty( $this->info['filepath'], 'wp-includes/rest-api', 3, 10000, 3 );
$this->assertDirNotEmpty( $this->info['filepath'], 'wp-admin', 10, 10000, 3 );
// Ensure there is exactly 1 .sql in the backup.
$sqls = $this->zip->get_sqls( $this->info['filepath'] );
$this->assertTrue( 1 === count( $sqls ) );
}
/**
* Test restore_archive_file.
*
* @since xxx
*/
public function test_restore_archive_file() {
/*
* Test: Basic test.
*
* Create a basic backup. Delete some stuff. Restore the backup. Make sure everything worked.
*/
// Create a backup if don't already have one.
if ( empty( $this->info ) ) {
$this->info = $this->core->archive_files( true );
}
$this->deleteBasic( 'delete' );
// Pad necessary $_POST vars.
$_POST['restore_now'] = 1;
$_POST['archive_key'] = 0;
$_POST['archive_filename'] = basename( $this->info['filepath'] );
$this->core->restore_archive_file();
$this->deleteBasic( 'restore' );
}
/**
* Test restore_cli.
*
* There is not restore_cli method. This method tests the cron command generated to restore a
* backup file.
*
* @since xxx
*/
public function test_restore_cli() {
$this->createWpconfig();
if ( empty( $this->info ) ) {
$this->info = $this->core->archive_files( true );
}
$this->deleteBasic( 'delete' );
// Get our restore command.
$cron = $this->core->cron->get_restore_command();
$cron = strstr( $cron, 'php' );
$cron = strstr( $cron, ' > /dev/null', true );
// Restore.
exec( $cron, $output ); // phpcs:ignore
$this->deleteBasic( 'restore' );
}
/**
* Test restore_archive_file_pcl.
*
* @since xxx
*/
public function test_restore_archive_file_pcl() {
/*
* Test: Basic test.
*
* Create a basic backup. Delete some stuff. Restore the backup. Make sure everything worked.
*/
// Create a backup if don't already have one.
$this->core->restore_helper = $this->fake_restore_helper;
$this->info = $this->core->archive_files( true );
$this->deleteBasic( 'delete' );
// Pad necessary $_POST vars.
$_POST['restore_now'] = 1;
$_POST['archive_key'] = 0;
$_POST['archive_filename'] = basename( $this->info['filepath'] );
$this->core->restore_archive_file();
$this->deleteBasic( 'restore' );
}
}