PluginProbe
Simple Analytics / 1.110
Simple Analytics v1.110
1.110 1.109 1.108 1.107 1.106 1.73 1.74 1.75 1.76 1.77 1.78 1.79 1.8 1.80 1.81 1.82 1.83 1.84 1.85 1.86 1.87 1.88 1.89 1.9 1.90 All 99 releases
simpleanalytics / tests / Support / isolated-options.php

isolated-options.php in Simple Analytics 1.110, at tests/Support/isolated-options.php

40 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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