show_errors(); ?> create_api_key(); //This clears out the database of any test tables $worker = new BitsBackupWorker($api); $api->set_api_key($api_key); $backup_state_machine = new BitsBackupStateMachine($api); $restore_state_machine = new BitsRestoreStateMachine($api); function create_backup($api, $opts=array()) { $api_key = $api->create_api_key(); $api->set_api_key($api_key); return $api->create_backup($opts); } function pass() { echo "PASS"; } function fail($obj) { echo "FAIL
"; echo "
";
  var_dump($obj);
  echo "
"; } ?> "true" )); $backup_worker = new BitsBackupWorker($api); $backup_worker->scan_schema($backup['id']); $backup_worker->scan_table($backup['id'],"wp_posts", 0, sha1(BitsUtil::get_schema("wp_posts"))); $committed = $api->commit_backup($backup["id"]); return $api->create_restore($backup['id']); } ?>
Test name Status
API - Backup
API Create backup "true" )); if($result["id"] > 0) { pass(); } else { fail($result); }; ?>
API get backups(empty) "true" )); if(is_array($result)) { pass(); } else { fail($result); }; ?>
API get a backup "true" )); $result = $api->get_backup($backup["id"]); if($result["id"] == $backup["id"]) { pass(); } else { fail($result); }; ?>
API error on null get_backup get_backup(null); if(is_wp_error($result)) { pass(); } else { fail($result); }; ?>
API commit backup (empty) "true" )); $result = $api->commit_backup($backup["id"]); if($result["id"] == $backup["id"]) { pass(); } else { fail($result); }; ?>
API add to backup (schema doesn't exist) "true" )); $result = $api->add_schemas_to_backup($backup["id"], ['no-exist'] ); if( !is_wp_error($result) && count($result["schemas"]) == 1 && $result['schemas'][0]['status'] == 200 ) { pass(); } else { fail($result); }; ?>
API can ignore AUTO_INCREMENT=\d+ in schemas schema_fingerprint_replace_autoincrement($str) != $res) { $was = $api->schema_fingerprint_replace_autoincrement($str); fail("remove autoincrement from:
$str
is not equal to:
$res
was:
$was"); return false; } return true; } $auto = "CREATE TABLE `wp_options` ( `option_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `option_name` varchar(64) NOT NULL DEFAULT '', `option_value` longtext NOT NULL, `autoload` varchar(20) NOT NULL DEFAULT 'yes', PRIMARY KEY (`option_id`), UNIQUE KEY `option_name` (`option_name`) ) ENGINE=InnoDB AUTO_INCREMENT=161 DEFAULT CHARSET=utf8"; $no_auto = "CREATE TABLE `wp_options` ( `option_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `option_name` varchar(64) NOT NULL DEFAULT '', `option_value` longtext NOT NULL, `autoload` varchar(20) NOT NULL DEFAULT 'yes', PRIMARY KEY (`option_id`), UNIQUE KEY `option_name` (`option_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; if(test_replace_autoincrement($api, "AUTO_INCREMENT=", "AUTO_INCREMENT=") && test_replace_autoincrement($api, $auto, $no_auto) && test_replace_autoincrement($api, $no_auto, $no_auto) ) { pass(); } ?>
API add file to backup "true" )); $files = array( $WP_PATH."/wp-config.php" => "http://test.test/path/to/file" ); $result = $api->add_files_to_backup($backup['id'], $files); if( count($result["files"]) == 1 ) { pass(); } else { fail($result); }; ?>
API upload_files return fingerprints upload_files($files, 0, BitsUtil::fs_get_wp_config_path()); if( count($results) == count($files) && reset($results) == $sha1 ) { pass(); } else { fail($results); }; ?>
API content_fingerprint that is passed in is used by add_files_to_backup "true" )); file_put_contents($WP_PATH."/tmpfile", substr(md5(rand()), 0, 7)); $files = array($WP_PATH."/tmpfile"); $sha1 = sha1_file($files[0]); $results = $api->upload_files($files, 0, BitsUtil::fs_get_wp_config_path()); file_put_contents($WP_PATH."/tmpfile", substr(md5(rand()), 0, 7)); $files = $results; $result = $api->add_files_to_backup($backup['id'], $files); if( count($result["files"]) == 1 ) { pass(); } else { fail($result); }; ?>
API handles sym links get_file_metadata($api->root . "/wp-content/plugins/anybackup"); if( $meta["type"] == "directory" && $meta['content_fingerprint'] == null ) { pass(); } else { fail($meta); }; ?>
API fresh backup, next step is scan_schema "true" )); $result = $api->next_step(); if( $result["step"] == "scan_schema" ) { pass(); } else { fail($result); }; ?>
API step scan_schema completes "true" )); $step = $api->next_step(); $result = $api->complete_step($step["step_id"]); if( $result["step_status"] == "COMPLETE" ) { pass(); } else { fail($result); }; ?>
API - Restore
API restore create "true" )); $commit = $api->commit_backup($backup["id"]); $result = $api->create_restore($backup["id"]); if($result["status"] == 200 && $result['id'] > 0) { pass(); } else { fail($result); }; ?>
API restore get schemas "true" )); $commit = $api->commit_backup($backup["id"]); $restore = $api->create_restore($backup["id"]); $result = $api->get_restore_schemas($restore["id"]); if($result["status"] == 200 && count($result['schemas']) == 0) { pass(); } else { fail($result); }; ?>
API restore get rows (404) "true" )); $commit = $api->commit_backup($backup["id"]); $restore = $api->create_restore($backup["id"]); $result = $api->get_restore_rows($restore["id"], "schema-not-exist", 0); if(is_wp_error($result) ) { pass(); } else { fail($result); }; ?>
API restore get missing files "true" )); $commit = $api->commit_backup($backup["id"]); $restore = $api->create_restore($backup["id"]); $files = array(); $result = $api->restore_missing_files($restore['id'], $files); if($result["missing_files"] == [] ) { pass(); } else { fail($result); }; ?>
API restore get file operations needed "true" )); $commit = $api->commit_backup($backup["id"]); $restore = $api->create_restore($backup["id"]); $files = array(); $result = $api->restore_file_operations($restore['id'], $files); if(!is_wp_error($result) && $result == []) { pass(); } else { fail($result); }; ?>
API restore swap schema operations "true" )); $commit = $api->commit_backup($backup["id"]); $restore = $api->create_restore($backup["id"]); $result = $api->swap_schema_operations($restore["id"]); if(!is_wp_error($result) && count($result) == 11) { pass(); } else { fail($result); }; ?>
API restore get restore operations - delete file "true" )); $commit = $api->commit_backup($backup["id"]); $restore = $api->create_restore($backup["id"]); $files = array(BitsUtil::fs_get_wp_config_path()."./index.php"); $result = $api->restore_file_operations($restore["id"], $files); if(!is_wp_error($result) && count($result) == 1) { pass(); } else { fail($result); }; ?>
API - Util
API select_404_results - empty "true" )); $result = $api->select_404_results([], []); if(count($result) == 0) { pass(); } else { fail($result); }; ?>
API select_404_results - one 404 404 ); $success = array( "status" => 200 ); $entry1 = array("a" => "b"); $entry2 = array("c" => "d"); $result = $api->select_404_results([$success, $missing], [$entry1, $entry2]); if($result[0] == $entry2 && sizeof($result) == 1) { pass(); } else { fail($result); }; ?>
API get schemas (schema doesn't exist) get_schemas(["not-exist", "not-exist-2"]); if(count($result) == 2) { pass(); } else { fail($result); }; ?>
API creates schemas create_schemas(["test", "test2"]); if(count($result) == 2) { pass(); } else { fail($result); }; ?>
API get rows get_rows(["xx"]); if(count($result) == 1) { pass(); } else { fail($result); }; ?>
API create rows create_schemas([$schema]); $row1 = array( "content" => array( "field", "".microtime() ), "schema" => sha1($schema)); $row2 = array( "content" => array( "field", "".microtime() . 1 ), "schema" => sha1($schema)); $result = $api->create_rows([ $row1, $row2 ]); if(count($result) == 2 && $result[0]["status"] == 200) { pass(); } else { fail($result); }; ?>
API get row groups get_row_groups(["xx"]); if(count($result) == 1) { pass(); } else { fail($result); }; ?>
API create row group create_schemas([$schema]); $row1 = array( "content" => array( "field", microtime() ), "schema" => sha1($schema)); $row_result = $api->create_rows([ $row1 ]); $row_group1 = array( "rows" => [ "not-found" ], "schema" => sha1($schema) ); $result = $api->create_row_groups(array(array("schema" => sha1($schema), "rows" => [$row1]))); if(count($result) == 1 && $result[0]["status"] == 200) { pass(); } else { fail($result); }; ?>
API Add missing row group "true" )); $result = $api->create_schemas(["test"]); $row1 = array("a" => "b"); $row2 = array("a" => "c"); $result = $api->add_rows_to_backup($backup["id"], [$row1, $row2], "test"); if(count($result) == 2) { pass(); } else { fail($result); }; ?>
API row fingerprints with same data are equal "b"); $row1 = array( "content" => $content, "schema" => "1" ); $row2 = array( "content" => $content, "schema" => "1" ); $fingerprint1 = $api->fingerprint_for_row($row1); $fingerprint2 = $api->fingerprint_for_row($row2); if($fingerprint1 == $fingerprint2) { pass(); } else { fail($result); }; ?>
API row fingerprint includes schema "b"); $row1 = array( "content" => $content, "schema" => "1" ); $row2 = array( "content" => $content, "schema" => "2" ); $fingerprint1 = $api->fingerprint_for_row($row1); $fingerprint2 = $api->fingerprint_for_row($row2); if($fingerprint1 != $fingerprint2) { pass(); } else { fail([$fingerprint1, $fingerprint2]); }; ?>
API row fingerprint content order doesn't matter "b", "c" => "d"); $content2 = array("c" => "d", "a" => "b"); $row1 = array( "content" => $content1, "schema" => "1" ); $row2 = array( "content" => $content2, "schema" => "1" ); $fingerprint1 = $api->fingerprint_for_row($row1); $fingerprint2 = $api->fingerprint_for_row($row2); if($fingerprint1 == $fingerprint2) { pass(); } else { fail([$fingerprint1, $fingerprint2]); }; ?>
API row_group row content order doesn't matter "b", "c" => "d"); $content2 = array("c" => "d", "a" => "b"); $row1 = array( "content" => $content1, "schema" => $schema); $row2 = array( "content" => $content2, "schema" => $schema); $row_group1 = array("rows" => [$row1, $row2], "schema" => $schema); $row_group2 = array("rows" => [$row1, $row2], "schema" => $schema); $fingerprint1 = $api->fingerprint_for_row_group($row_group1); $fingerprint2 = $api->fingerprint_for_row_group($row_group2); if($fingerprint1 == $fingerprint2) { pass(); } else { fail([$fingerprint1, $fingerprint2]); }; ?>
API row_group row order does matter "b", "c" => "d"); $content2 = array("c" => "d", "a" => "b"); $row1 = array( "content" => $content1, "schema" => $schema); $row2 = array( "content" => $content2, "schema" => $schema); $row_group1 = array("rows" => [$row1, $row2], "schema" => $schema); $row_group2 = array("rows" => [$row2, $row1], "schema" => $schema); $fingerprint1 = $api->fingerprint_for_row_group($row_group1); $fingerprint2 = $api->fingerprint_for_row_group($row_group2); if($fingerprint1 != $fingerprint2) { pass(); } else { fail([$fingerprint1, $fingerprint2]); }; ?>
API row_group fingerprint includes schema "b", "c" => "d"); $content2 = array("c" => "d", "a" => "b"); $row1 = array( "content" => $content1, "schema" => $schema); $row2 = array( "content" => $content2, "schema" => $schema); $row_group1 = array("rows" => [$row1, $row2], "schema" => $schema); $row_group2 = array("rows" => [$row1, $row2], "schema" => ($schema."DIFFERENT")); $fingerprint1 = $api->fingerprint_for_row_group($row_group1); $fingerprint2 = $api->fingerprint_for_row_group($row_group2); if($fingerprint1 != $fingerprint2) { pass(); } else { fail([$fingerprint1, $fingerprint2]); }; ?>
API upload files upload_files($files, 0, BitsUtil::fs_get_wp_config_path()); if( count($results) == count($files) && count($results[$WP_PATH."/index.php"]) > 0 ) { pass(); } else { fail($results); }; ?>
Backup State Machine
Invalid State return WP_Error process_step($worker, "NOT-REAL-STEP"); if( is_wp_error($result) ) { pass(); } else { fail($result); }; ?>
Restore State Machine
Backup Worker
Util - all_tables 0) { pass(); } else { fail($result); }; ?>
Backup Worker - query throws error query("NOT VALID SQL"); $worker->query("SELECT 1"); // Clear out database errors if(is_wp_error($result)) { pass(); } else { fail($result); }; ?>
Backup API - creates backup 0) { pass(); } else { fail($result);}; ?>
Backup Worker - upload files upload_files($backup_id, 0, BitsUtil::fs_get_wp_config_path()); $result = $api->get_backup($backup_id); if($result["file_count"] > 5 ) { pass(); } else { fail($result); }; ?>
Backup Worker - scan schema scan_schema($backup_id); if(is_wp_error($scan_response)) return fail($scan_response); $result = $api->get_backup($backup_id); if($result["schema_count"] > 0 && $result["id"] == $backup_id) { pass(); } else { fail($result); }; ?>
Backup Worker - scan table - handles page 0 of table 3 scan_schema($backup_id); $schema = 'wp_posts'; $worker->scan_table($backup_id,$schema, 0, sha1(BitsUtil::get_schema($schema))); $result = $api->get_backup($backup_id); if( $result["row_group_count"] >= 1 ) { pass(); } else { fail($result); }; ?>
[integration] Backup Worker - adds schema to active backup API scan_schema($backup_id); $backup = $api->get_backup($backup_id); if($backup["schema_count"] > 0) { pass(); } else { fail($backup); }; ?>
[integration] Backup Worker - complete empty backup complete($backup_id); $backup = $api->get_backup($backup_id); if($backup["state"] == "COMMITTED") { pass(); } else { fail($backup); }; ?>
Restore Worker
Restore Worker - create_restore can create a restore 0) { pass(); } else { fail($result); }; ?>
Restore Worker - restore_schema adds tables get_results("SELECT COUNT(*) FROM information_schema.tables", ARRAY_N)[0][0]; $worker->restore_schema($restore['id']); $new_count = $wpdb->get_results("SELECT COUNT(*) FROM information_schema.tables", ARRAY_N)[0][0]; if($new_count > $count) { pass(); } else { fail("$count is not < $new_count"); }; ?>
Restore Worker - query throws error query("NOT VALID SQL"); $worker->query("SELECT 1"); if(is_wp_error($result)) { pass(); } else { fail($result); }; ?>
Restore Worker - download_files downloads files "fp1", "url" => $missing_file, "filename" => "/tmp/fp1" )); $result = $worker->download_files($restore['id'], $to_download); $file_exists = file_exists($file_path); if($file_exists && !is_wp_error($result)) { pass(); } else { fail($result); }; ?>
Restore Worker - swap_fs renames and deletes "delete", "path" => BitsUtil::fs_get_wp_config_path()."/wp-content/test_delete" ), array( "type" => "copy", "from" => BitsUtil::fs_get_wp_config_path()."/wp-content/test_copy", "to" => BitsUtil::fs_get_wp_config_path()."/wp-content/test_copy_complete" ) ); touch(BitsUtil::fs_get_wp_config_path()."/wp-content/test_delete"); touch(BitsUtil::fs_get_wp_config_path()."/wp-content/test_copy"); unlink(BitsUtil::fs_get_wp_config_path()."/wp-content/test_copy_complete"); $result = $worker->process_file_operations($operations, BitsUtil::fs_get_wp_config_path()); $copyd = file_exists(BitsUtil::fs_get_wp_config_path()."/wp-content/test_copy_complete"); $deleted = !file_exists(BitsUtil::fs_get_wp_config_path()."/wp-content/test_delete"); if($copyd && $deleted) { pass(); } else { fail("copyd $copyd/ deleted $deleted"); }; ?>

End of tests