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 / Regression / onload-permissions.php

onload-permissions.php in Simple Analytics 1.110, at tests/Regression/onload-permissions.php

39 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require __DIR__ . '/../Support/isolated-options.php';
4
5 sa_with_isolated_options(static function () {
6 $key = SimpleAnalytics\SettingName::ONLOAD_CALLBACK;
7 $hashKey = SimpleAnalytics\SettingName::ONLOAD_CALLBACK_HASH;
8 $script = new SimpleAnalytics\Scripts\AnalyticsScript();
9
10 delete_option($hashKey);
11 update_option($key, 'legacyCallback()');
12 sa_assert(! isset($script->attributes()['onload']), 'Unverified legacy text must remain inert.');
13
14 $admin = get_user_by('login', 'admin');
15 sa_assert($admin !== false, 'The wp-env admin user must exist.');
16 wp_set_current_user($admin->ID);
17 $field = new SimpleAnalytics\Settings\Blocks\Fields\Input($key, 'Onload Callback');
18 register_setting('simpleanalytics-advanced', $key, ['sanitize_callback' => $field->getValueSanitizer()]);
19 update_option($key, 'authorizedCallback()');
20 sa_assert(($script->attributes()['onload'] ?? null) === 'authorizedCallback()', 'Authorized saves must enable the callback.');
21
22 // Simulate a multisite administrator (or DISALLOW_UNFILTERED_HTML) through
23 // WordPress's real capability mapping, without changing any user records.
24 $deny = static function ($caps, $cap) { return $cap === 'unfiltered_html' ? ['do_not_allow'] : $caps; };
25 add_filter('map_meta_cap', $deny, 10, 2);
26 $hash = get_option($hashKey);
27 update_option($key, 'unauthorizedCallback()');
28 sa_assert(get_option($key) === 'authorizedCallback()', 'Users without unfiltered_html must not replace the callback.');
29 sa_assert(get_option($hashKey) === $hash, 'Unauthorized saves must not change the recorded hash.');
30 remove_filter('map_meta_cap', $deny, 10);
31
32 remove_filter('sanitize_option_' . $key, $field->getValueSanitizer());
33 update_option($key, 'changedOutsideSettings()');
34 sa_assert(! isset($script->attributes()['onload']), 'Code that differs from the verified value must remain inert.');
35 register_setting('simpleanalytics-advanced', $key, ['sanitize_callback' => $field->getValueSanitizer()]);
36 update_option($key, '');
37 sa_assert(! isset($script->attributes()['onload']), 'Clearing the callback must remove the handler.');
38 });
39