| 1 |
<?php |
| 2 |
|
| 3 |
/** Run only through wp eval-file against the local wp-env installation. */ |
| 4 |
function sa_assert($condition, string $message): void |
| 5 |
{ |
| 6 |
if (! $condition) throw new RuntimeException($message); |
| 7 |
} |
| 8 |
|
| 9 |
function sa_with_isolated_options(callable $test): void |
| 10 |
{ |
| 11 |
global $wpdb, $wp_object_cache; |
| 12 |
|
| 13 |
sa_assert(! wp_using_ext_object_cache(), 'Regression tests require the default object cache.'); |
| 14 |
$originalTable = $wpdb->options; |
| 15 |
$originalCache = $wp_object_cache; |
| 16 |
$table = 'sa_regression_options'; |
| 17 |
$guard = static function ($sql) use ($table) { |
| 18 |
if (! preg_match('/^\s*(SELECT|SHOW|DESCRIBE|EXPLAIN)\b/i', $sql) && strpos($sql, $table) === false) { |
| 19 |
throw new RuntimeException('Refusing a write outside the temporary test table.'); |
| 20 |
} |
| 21 |
return $sql; |
| 22 |
}; |
| 23 |
add_filter('query', $guard, 9999); |
| 24 |
|
| 25 |
try { |
| 26 |
sa_assert($wpdb->query("CREATE TEMPORARY TABLE $table LIKE $originalTable") !== false, $wpdb->last_error); |
| 27 |
sa_assert($wpdb->query("INSERT INTO $table SELECT * FROM $originalTable") !== false, $wpdb->last_error); |
| 28 |
$wpdb->options = $table; |
| 29 |
$wp_object_cache = new WP_Object_Cache(); |
| 30 |
wp_cache_switch_to_blog(get_current_blog_id()); |
| 31 |
$test(); |
| 32 |
echo "Regression checks passed\n"; |
| 33 |
} finally { |
| 34 |
$wpdb->options = $originalTable; |
| 35 |
$wp_object_cache = $originalCache; |
| 36 |
remove_filter('query', $guard, 9999); |
| 37 |
} |
| 38 |
// The database connection automatically drops the temporary table on exit. |
| 39 |
} |
| 40 |
|