feat: 添加 Mermaid 配置管理系统
- 在 functions.php 中添加 9 个核心配置管理函数 - argon_get_mermaid_option: 获取配置选项(支持简短名称和完整名称) - argon_update_mermaid_option: 保存配置选项 - argon_validate_mermaid_cdn_url: 验证 CDN URL 格式(必须是有效 URL 且以 .js 结尾) - argon_get_mermaid_theme: 获取当前主题对应的 Mermaid 主题 - argon_get_mermaid_default_config: 获取默认配置 - argon_validate_mermaid_settings: 验证配置选项(CDN 来源、主题名称、布尔值) - argon_init_mermaid_config: 初始化默认配置(不覆盖已有配置) - argon_get_all_mermaid_options: 获取所有配置选项 - argon_update_mermaid_settings: 批量更新配置(包含验证) - 创建单元测试文件验证配置管理功能 - 满足 Requirements 5.1, 5.2, 5.3, 5.4, 5.5
This commit is contained in:
236
functions.php
236
functions.php
@@ -8937,3 +8937,239 @@ function argon_normalize_social_url($platform, $input) {
|
|||||||
// 构建完整 URL
|
// 构建完整 URL
|
||||||
return esc_url($base_urls[$platform] . $username);
|
return esc_url($base_urls[$platform] . $username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==========================================================================
|
||||||
|
// Mermaid 图表支持 - 配置管理
|
||||||
|
// ==========================================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 Mermaid 配置选项
|
||||||
|
*
|
||||||
|
* @param string $option_name 配置选项名称
|
||||||
|
* @param mixed $default 默认值
|
||||||
|
* @return mixed 配置选项值
|
||||||
|
*/
|
||||||
|
function argon_get_mermaid_option($option_name, $default = null) {
|
||||||
|
// 配置选项映射表
|
||||||
|
$option_map = [
|
||||||
|
'enabled' => 'argon_enable_mermaid',
|
||||||
|
'cdn_source' => 'argon_mermaid_cdn_source',
|
||||||
|
'custom_cdn_url' => 'argon_mermaid_cdn_custom_url',
|
||||||
|
'theme' => 'argon_mermaid_theme',
|
||||||
|
'use_local' => 'argon_mermaid_use_local',
|
||||||
|
'debug_mode' => 'argon_mermaid_debug_mode'
|
||||||
|
];
|
||||||
|
|
||||||
|
// 如果使用简短名称,转换为完整选项名
|
||||||
|
$full_option_name = isset($option_map[$option_name]) ? $option_map[$option_name] : $option_name;
|
||||||
|
|
||||||
|
// 获取选项值
|
||||||
|
$value = get_option($full_option_name, $default);
|
||||||
|
|
||||||
|
// 如果值为 null 且有默认值,返回默认值
|
||||||
|
if ($value === null && $default !== null) {
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存 Mermaid 配置选项
|
||||||
|
*
|
||||||
|
* @param string $option_name 配置选项名称
|
||||||
|
* @param mixed $value 配置选项值
|
||||||
|
* @return bool 是否保存成功
|
||||||
|
*/
|
||||||
|
function argon_update_mermaid_option($option_name, $value) {
|
||||||
|
// 配置选项映射表
|
||||||
|
$option_map = [
|
||||||
|
'enabled' => 'argon_enable_mermaid',
|
||||||
|
'cdn_source' => 'argon_mermaid_cdn_source',
|
||||||
|
'custom_cdn_url' => 'argon_mermaid_cdn_custom_url',
|
||||||
|
'theme' => 'argon_mermaid_theme',
|
||||||
|
'use_local' => 'argon_mermaid_use_local',
|
||||||
|
'debug_mode' => 'argon_mermaid_debug_mode'
|
||||||
|
];
|
||||||
|
|
||||||
|
// 如果使用简短名称,转换为完整选项名
|
||||||
|
$full_option_name = isset($option_map[$option_name]) ? $option_map[$option_name] : $option_name;
|
||||||
|
|
||||||
|
// 保存选项值
|
||||||
|
return update_option($full_option_name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证 Mermaid CDN URL 格式
|
||||||
|
*
|
||||||
|
* @param string $url CDN URL
|
||||||
|
* @return bool 是否为有效的 CDN URL
|
||||||
|
*/
|
||||||
|
function argon_validate_mermaid_cdn_url($url) {
|
||||||
|
// 空 URL 视为无效
|
||||||
|
if (empty(trim($url))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证 URL 格式
|
||||||
|
if (!filter_var($url, FILTER_VALIDATE_URL)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证是否以 .js 结尾
|
||||||
|
if (!preg_match('/\.js$/i', $url)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证协议(必须是 http 或 https)
|
||||||
|
$parsed_url = parse_url($url);
|
||||||
|
if (!isset($parsed_url['scheme']) || !in_array($parsed_url['scheme'], ['http', 'https'])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前主题模式对应的 Mermaid 主题
|
||||||
|
*
|
||||||
|
* @return string Mermaid 主题名称
|
||||||
|
*/
|
||||||
|
function argon_get_mermaid_theme() {
|
||||||
|
// 获取配置的主题
|
||||||
|
$configured_theme = argon_get_mermaid_option('theme', 'auto');
|
||||||
|
|
||||||
|
// 如果不是自动模式,直接返回配置的主题
|
||||||
|
if ($configured_theme !== 'auto') {
|
||||||
|
return $configured_theme;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 自动模式:根据页面主题返回对应的 Mermaid 主题
|
||||||
|
// 注意:这个函数在 PHP 端调用,无法直接检测前端的夜间模式状态
|
||||||
|
// 实际的主题切换逻辑应该在 JavaScript 中实现
|
||||||
|
// 这里返回默认主题,JavaScript 会根据实际情况覆盖
|
||||||
|
return 'default';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 Mermaid 配置的默认值
|
||||||
|
*
|
||||||
|
* @return array 默认配置数组
|
||||||
|
*/
|
||||||
|
function argon_get_mermaid_default_config() {
|
||||||
|
return [
|
||||||
|
'enabled' => false,
|
||||||
|
'cdn_source' => 'jsdelivr',
|
||||||
|
'custom_cdn_url' => '',
|
||||||
|
'theme' => 'auto',
|
||||||
|
'use_local' => false,
|
||||||
|
'debug_mode' => false
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证 Mermaid 配置选项
|
||||||
|
*
|
||||||
|
* @param array $settings 配置选项数组
|
||||||
|
* @return array 错误信息数组,如果没有错误则返回空数组
|
||||||
|
*/
|
||||||
|
function argon_validate_mermaid_settings($settings) {
|
||||||
|
$errors = [];
|
||||||
|
|
||||||
|
// 验证 CDN 来源
|
||||||
|
if (isset($settings['cdn_source'])) {
|
||||||
|
$valid_sources = ['jsdelivr', 'unpkg', 'custom', 'local'];
|
||||||
|
if (!in_array($settings['cdn_source'], $valid_sources)) {
|
||||||
|
$errors[] = __('无效的 CDN 来源', 'argon');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果选择自定义 CDN,验证 URL
|
||||||
|
if ($settings['cdn_source'] === 'custom') {
|
||||||
|
if (!isset($settings['custom_cdn_url']) || empty($settings['custom_cdn_url'])) {
|
||||||
|
$errors[] = __('自定义 CDN 地址不能为空', 'argon');
|
||||||
|
} elseif (!argon_validate_mermaid_cdn_url($settings['custom_cdn_url'])) {
|
||||||
|
$errors[] = __('CDN 地址格式无效,必须是有效的 URL 且以 .js 结尾', 'argon');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证主题名称
|
||||||
|
if (isset($settings['theme'])) {
|
||||||
|
$valid_themes = ['default', 'dark', 'forest', 'neutral', 'auto'];
|
||||||
|
if (!in_array($settings['theme'], $valid_themes)) {
|
||||||
|
$errors[] = __('无效的图表主题', 'argon');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证布尔值选项
|
||||||
|
$boolean_options = ['enabled', 'use_local', 'debug_mode'];
|
||||||
|
foreach ($boolean_options as $option) {
|
||||||
|
if (isset($settings[$option]) && !is_bool($settings[$option]) && !in_array($settings[$option], ['true', 'false', '1', '0', 1, 0], true)) {
|
||||||
|
$errors[] = sprintf(__('选项 %s 必须是布尔值', 'argon'), $option);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $errors;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化 Mermaid 默认配置
|
||||||
|
* 在主题激活时调用
|
||||||
|
*/
|
||||||
|
function argon_init_mermaid_config() {
|
||||||
|
$defaults = argon_get_mermaid_default_config();
|
||||||
|
|
||||||
|
foreach ($defaults as $key => $value) {
|
||||||
|
// 只在选项不存在时设置默认值
|
||||||
|
if (argon_get_mermaid_option($key) === false) {
|
||||||
|
argon_update_mermaid_option($key, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取所有 Mermaid 配置选项
|
||||||
|
*
|
||||||
|
* @return array 配置选项数组
|
||||||
|
*/
|
||||||
|
function argon_get_all_mermaid_options() {
|
||||||
|
$defaults = argon_get_mermaid_default_config();
|
||||||
|
$options = [];
|
||||||
|
|
||||||
|
foreach ($defaults as $key => $default_value) {
|
||||||
|
$options[$key] = argon_get_mermaid_option($key, $default_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $options;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量更新 Mermaid 配置选项
|
||||||
|
*
|
||||||
|
* @param array $settings 配置选项数组
|
||||||
|
* @return array 包含 success 和 errors 的结果数组
|
||||||
|
*/
|
||||||
|
function argon_update_mermaid_settings($settings) {
|
||||||
|
// 验证配置
|
||||||
|
$errors = argon_validate_mermaid_settings($settings);
|
||||||
|
|
||||||
|
if (!empty($errors)) {
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'errors' => $errors
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存配置
|
||||||
|
$defaults = argon_get_mermaid_default_config();
|
||||||
|
foreach ($defaults as $key => $default_value) {
|
||||||
|
if (isset($settings[$key])) {
|
||||||
|
argon_update_mermaid_option($key, $settings[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'errors' => []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|||||||
234
tests/run-mermaid-config-tests.php
Normal file
234
tests/run-mermaid-config-tests.php
Normal file
@@ -0,0 +1,234 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* 简单的 Mermaid 配置管理测试脚本
|
||||||
|
* 不依赖 PHPUnit,直接运行测试
|
||||||
|
*/
|
||||||
|
|
||||||
|
// 模拟 WordPress 环境
|
||||||
|
if (!function_exists('get_option')) {
|
||||||
|
$_test_options = [];
|
||||||
|
|
||||||
|
function get_option($option, $default = false) {
|
||||||
|
global $_test_options;
|
||||||
|
return isset($_test_options[$option]) ? $_test_options[$option] : $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
function update_option($option, $value) {
|
||||||
|
global $_test_options;
|
||||||
|
$_test_options[$option] = $value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function delete_option($option) {
|
||||||
|
global $_test_options;
|
||||||
|
unset($_test_options[$option]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function __($text, $domain = 'default') {
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
|
||||||
|
function esc_url($url) {
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加载 functions.php 中的 Mermaid 配置函数
|
||||||
|
require_once __DIR__ . '/../functions.php';
|
||||||
|
|
||||||
|
// 测试计数器
|
||||||
|
$tests_run = 0;
|
||||||
|
$tests_passed = 0;
|
||||||
|
$tests_failed = 0;
|
||||||
|
|
||||||
|
// 测试辅助函数
|
||||||
|
function test_assert($condition, $test_name) {
|
||||||
|
global $tests_run, $tests_passed, $tests_failed;
|
||||||
|
$tests_run++;
|
||||||
|
|
||||||
|
if ($condition) {
|
||||||
|
$tests_passed++;
|
||||||
|
echo "✓ {$test_name}\n";
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
$tests_failed++;
|
||||||
|
echo "✗ {$test_name}\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_assert_equals($expected, $actual, $test_name) {
|
||||||
|
return test_assert($expected === $actual, $test_name . " (expected: " . var_export($expected, true) . ", got: " . var_export($actual, true) . ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
function clear_mermaid_options() {
|
||||||
|
$options = [
|
||||||
|
'argon_enable_mermaid',
|
||||||
|
'argon_mermaid_cdn_source',
|
||||||
|
'argon_mermaid_cdn_custom_url',
|
||||||
|
'argon_mermaid_theme',
|
||||||
|
'argon_mermaid_use_local',
|
||||||
|
'argon_mermaid_debug_mode'
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($options as $option) {
|
||||||
|
delete_option($option);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "=== Mermaid 配置管理测试 ===\n\n";
|
||||||
|
|
||||||
|
// 测试 1: 获取配置选项(使用简短名称)
|
||||||
|
clear_mermaid_options();
|
||||||
|
update_option('argon_enable_mermaid', true);
|
||||||
|
test_assert_equals(true, argon_get_mermaid_option('enabled'), "测试 1: 获取配置选项(简短名称)");
|
||||||
|
|
||||||
|
// 测试 2: 获取配置选项(使用完整名称)
|
||||||
|
clear_mermaid_options();
|
||||||
|
update_option('argon_enable_mermaid', true);
|
||||||
|
test_assert_equals(true, argon_get_mermaid_option('argon_enable_mermaid'), "测试 2: 获取配置选项(完整名称)");
|
||||||
|
|
||||||
|
// 测试 3: 获取不存在的配置选项(返回默认值)
|
||||||
|
clear_mermaid_options();
|
||||||
|
test_assert_equals('default_value', argon_get_mermaid_option('non_existent', 'default_value'), "测试 3: 获取不存在的配置(返回默认值)");
|
||||||
|
|
||||||
|
// 测试 4: 保存配置选项(使用简短名称)
|
||||||
|
clear_mermaid_options();
|
||||||
|
argon_update_mermaid_option('enabled', true);
|
||||||
|
test_assert_equals(true, get_option('argon_enable_mermaid'), "测试 4: 保存配置选项(简短名称)");
|
||||||
|
|
||||||
|
// 测试 5: 保存配置选项(使用完整名称)
|
||||||
|
clear_mermaid_options();
|
||||||
|
argon_update_mermaid_option('argon_enable_mermaid', true);
|
||||||
|
test_assert_equals(true, get_option('argon_enable_mermaid'), "测试 5: 保存配置选项(完整名称)");
|
||||||
|
|
||||||
|
// 测试 6: 验证有效的 CDN URL
|
||||||
|
$valid_url = 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js';
|
||||||
|
test_assert(argon_validate_mermaid_cdn_url($valid_url), "测试 6: 验证有效的 CDN URL");
|
||||||
|
|
||||||
|
// 测试 7: 验证无效的 CDN URL(不是 URL)
|
||||||
|
test_assert(!argon_validate_mermaid_cdn_url('not-a-valid-url'), "测试 7: 验证无效的 CDN URL(不是 URL)");
|
||||||
|
|
||||||
|
// 测试 8: 验证无效的 CDN URL(不以 .js 结尾)
|
||||||
|
test_assert(!argon_validate_mermaid_cdn_url('https://example.com/mermaid.css'), "测试 8: 验证无效的 CDN URL(不以 .js 结尾)");
|
||||||
|
|
||||||
|
// 测试 9: 验证空 CDN URL
|
||||||
|
test_assert(!argon_validate_mermaid_cdn_url(''), "测试 9: 验证空 CDN URL");
|
||||||
|
|
||||||
|
// 测试 10: 验证无效协议的 CDN URL
|
||||||
|
test_assert(!argon_validate_mermaid_cdn_url('ftp://example.com/mermaid.min.js'), "测试 10: 验证无效协议的 CDN URL");
|
||||||
|
|
||||||
|
// 测试 11: 获取 Mermaid 主题(配置为 default)
|
||||||
|
clear_mermaid_options();
|
||||||
|
update_option('argon_mermaid_theme', 'default');
|
||||||
|
test_assert_equals('default', argon_get_mermaid_theme(), "测试 11: 获取 Mermaid 主题(default)");
|
||||||
|
|
||||||
|
// 测试 12: 获取 Mermaid 主题(配置为 dark)
|
||||||
|
clear_mermaid_options();
|
||||||
|
update_option('argon_mermaid_theme', 'dark');
|
||||||
|
test_assert_equals('dark', argon_get_mermaid_theme(), "测试 12: 获取 Mermaid 主题(dark)");
|
||||||
|
|
||||||
|
// 测试 13: 获取 Mermaid 主题(配置为 auto)
|
||||||
|
clear_mermaid_options();
|
||||||
|
update_option('argon_mermaid_theme', 'auto');
|
||||||
|
test_assert_equals('default', argon_get_mermaid_theme(), "测试 13: 获取 Mermaid 主题(auto 模式返回 default)");
|
||||||
|
|
||||||
|
// 测试 14: 获取默认配置
|
||||||
|
$defaults = argon_get_mermaid_default_config();
|
||||||
|
test_assert(is_array($defaults), "测试 14: 获取默认配置(返回数组)");
|
||||||
|
test_assert(isset($defaults['enabled']), "测试 14a: 默认配置包含 enabled");
|
||||||
|
test_assert_equals('jsdelivr', $defaults['cdn_source'], "测试 14b: 默认 CDN 来源为 jsdelivr");
|
||||||
|
test_assert_equals('auto', $defaults['theme'], "测试 14c: 默认主题为 auto");
|
||||||
|
|
||||||
|
// 测试 15: 验证有效的配置
|
||||||
|
$valid_settings = [
|
||||||
|
'enabled' => true,
|
||||||
|
'cdn_source' => 'jsdelivr',
|
||||||
|
'theme' => 'default',
|
||||||
|
'use_local' => false,
|
||||||
|
'debug_mode' => false
|
||||||
|
];
|
||||||
|
$errors = argon_validate_mermaid_settings($valid_settings);
|
||||||
|
test_assert(empty($errors), "测试 15: 验证有效的配置(无错误)");
|
||||||
|
|
||||||
|
// 测试 16: 验证无效的 CDN 来源
|
||||||
|
$invalid_settings = ['cdn_source' => 'invalid_source'];
|
||||||
|
$errors = argon_validate_mermaid_settings($invalid_settings);
|
||||||
|
test_assert(!empty($errors), "测试 16: 验证无效的 CDN 来源(有错误)");
|
||||||
|
|
||||||
|
// 测试 17: 验证自定义 CDN 但 URL 为空
|
||||||
|
$invalid_settings = [
|
||||||
|
'cdn_source' => 'custom',
|
||||||
|
'custom_cdn_url' => ''
|
||||||
|
];
|
||||||
|
$errors = argon_validate_mermaid_settings($invalid_settings);
|
||||||
|
test_assert(!empty($errors), "测试 17: 验证自定义 CDN 但 URL 为空(有错误)");
|
||||||
|
|
||||||
|
// 测试 18: 验证自定义 CDN 但 URL 无效
|
||||||
|
$invalid_settings = [
|
||||||
|
'cdn_source' => 'custom',
|
||||||
|
'custom_cdn_url' => 'not-a-url'
|
||||||
|
];
|
||||||
|
$errors = argon_validate_mermaid_settings($invalid_settings);
|
||||||
|
test_assert(!empty($errors), "测试 18: 验证自定义 CDN 但 URL 无效(有错误)");
|
||||||
|
|
||||||
|
// 测试 19: 验证无效的主题名称
|
||||||
|
$invalid_settings = ['theme' => 'invalid_theme'];
|
||||||
|
$errors = argon_validate_mermaid_settings($invalid_settings);
|
||||||
|
test_assert(!empty($errors), "测试 19: 验证无效的主题名称(有错误)");
|
||||||
|
|
||||||
|
// 测试 20: 初始化默认配置
|
||||||
|
clear_mermaid_options();
|
||||||
|
argon_init_mermaid_config();
|
||||||
|
test_assert_equals(false, get_option('argon_enable_mermaid'), "测试 20: 初始化默认配置(enabled 为 false)");
|
||||||
|
test_assert_equals('jsdelivr', get_option('argon_mermaid_cdn_source'), "测试 20a: 初始化默认配置(cdn_source 为 jsdelivr)");
|
||||||
|
|
||||||
|
// 测试 21: 初始化不覆盖已有配置
|
||||||
|
clear_mermaid_options();
|
||||||
|
update_option('argon_enable_mermaid', true);
|
||||||
|
argon_init_mermaid_config();
|
||||||
|
test_assert_equals(true, get_option('argon_enable_mermaid'), "测试 21: 初始化不覆盖已有配置");
|
||||||
|
|
||||||
|
// 测试 22: 获取所有配置选项
|
||||||
|
clear_mermaid_options();
|
||||||
|
update_option('argon_enable_mermaid', true);
|
||||||
|
update_option('argon_mermaid_cdn_source', 'unpkg');
|
||||||
|
$options = argon_get_all_mermaid_options();
|
||||||
|
test_assert(is_array($options), "测试 22: 获取所有配置选项(返回数组)");
|
||||||
|
test_assert_equals(true, $options['enabled'], "测试 22a: 获取所有配置(enabled 正确)");
|
||||||
|
test_assert_equals('unpkg', $options['cdn_source'], "测试 22b: 获取所有配置(cdn_source 正确)");
|
||||||
|
|
||||||
|
// 测试 23: 批量更新配置(成功)
|
||||||
|
clear_mermaid_options();
|
||||||
|
$settings = [
|
||||||
|
'enabled' => true,
|
||||||
|
'cdn_source' => 'jsdelivr',
|
||||||
|
'theme' => 'dark'
|
||||||
|
];
|
||||||
|
$result = argon_update_mermaid_settings($settings);
|
||||||
|
test_assert($result['success'], "测试 23: 批量更新配置(成功)");
|
||||||
|
test_assert(empty($result['errors']), "测试 23a: 批量更新配置(无错误)");
|
||||||
|
test_assert_equals(true, get_option('argon_enable_mermaid'), "测试 23b: 批量更新配置(enabled 已保存)");
|
||||||
|
test_assert_equals('dark', get_option('argon_mermaid_theme'), "测试 23c: 批量更新配置(theme 已保存)");
|
||||||
|
|
||||||
|
// 测试 24: 批量更新配置(失败)
|
||||||
|
clear_mermaid_options();
|
||||||
|
$settings = ['cdn_source' => 'invalid_source'];
|
||||||
|
$result = argon_update_mermaid_settings($settings);
|
||||||
|
test_assert(!$result['success'], "测试 24: 批量更新配置(失败)");
|
||||||
|
test_assert(!empty($result['errors']), "测试 24a: 批量更新配置(有错误信息)");
|
||||||
|
|
||||||
|
// 输出测试结果
|
||||||
|
echo "\n=== 测试结果 ===\n";
|
||||||
|
echo "总计: {$tests_run} 个测试\n";
|
||||||
|
echo "通过: {$tests_passed} 个\n";
|
||||||
|
echo "失败: {$tests_failed} 个\n";
|
||||||
|
|
||||||
|
if ($tests_failed === 0) {
|
||||||
|
echo "\n✓ 所有测试通过!\n";
|
||||||
|
exit(0);
|
||||||
|
} else {
|
||||||
|
echo "\n✗ 有测试失败!\n";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
293
tests/test-mermaid-config.php
Normal file
293
tests/test-mermaid-config.php
Normal file
@@ -0,0 +1,293 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Mermaid 配置管理单元测试
|
||||||
|
*
|
||||||
|
* 测试 functions.php 中的 Mermaid 配置管理函数
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Test_Mermaid_Config extends WP_UnitTestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试前清理配置
|
||||||
|
*/
|
||||||
|
public function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
// 清理所有 Mermaid 配置
|
||||||
|
$options = [
|
||||||
|
'argon_enable_mermaid',
|
||||||
|
'argon_mermaid_cdn_source',
|
||||||
|
'argon_mermaid_cdn_custom_url',
|
||||||
|
'argon_mermaid_theme',
|
||||||
|
'argon_mermaid_use_local',
|
||||||
|
'argon_mermaid_debug_mode'
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($options as $option) {
|
||||||
|
delete_option($option);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试后清理配置
|
||||||
|
*/
|
||||||
|
public function tearDown() {
|
||||||
|
$this->setUp(); // 重用清理逻辑
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试获取配置选项(使用简短名称)
|
||||||
|
*/
|
||||||
|
public function test_get_mermaid_option_with_short_name() {
|
||||||
|
update_option('argon_enable_mermaid', true);
|
||||||
|
$this->assertTrue(argon_get_mermaid_option('enabled'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试获取配置选项(使用完整名称)
|
||||||
|
*/
|
||||||
|
public function test_get_mermaid_option_with_full_name() {
|
||||||
|
update_option('argon_enable_mermaid', true);
|
||||||
|
$this->assertTrue(argon_get_mermaid_option('argon_enable_mermaid'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试获取不存在的配置选项(返回默认值)
|
||||||
|
*/
|
||||||
|
public function test_get_mermaid_option_with_default() {
|
||||||
|
$this->assertEquals('default_value', argon_get_mermaid_option('non_existent', 'default_value'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试保存配置选项(使用简短名称)
|
||||||
|
*/
|
||||||
|
public function test_update_mermaid_option_with_short_name() {
|
||||||
|
$result = argon_update_mermaid_option('enabled', true);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
$this->assertTrue(get_option('argon_enable_mermaid'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试保存配置选项(使用完整名称)
|
||||||
|
*/
|
||||||
|
public function test_update_mermaid_option_with_full_name() {
|
||||||
|
$result = argon_update_mermaid_option('argon_enable_mermaid', true);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
$this->assertTrue(get_option('argon_enable_mermaid'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试验证有效的 CDN URL
|
||||||
|
*/
|
||||||
|
public function test_validate_cdn_url_with_valid_url() {
|
||||||
|
$url = 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js';
|
||||||
|
$this->assertTrue(argon_validate_mermaid_cdn_url($url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试验证无效的 CDN URL(不是 URL)
|
||||||
|
*/
|
||||||
|
public function test_validate_cdn_url_with_invalid_url() {
|
||||||
|
$url = 'not-a-valid-url';
|
||||||
|
$this->assertFalse(argon_validate_mermaid_cdn_url($url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试验证无效的 CDN URL(不以 .js 结尾)
|
||||||
|
*/
|
||||||
|
public function test_validate_cdn_url_without_js_extension() {
|
||||||
|
$url = 'https://example.com/mermaid.css';
|
||||||
|
$this->assertFalse(argon_validate_mermaid_cdn_url($url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试验证空 CDN URL
|
||||||
|
*/
|
||||||
|
public function test_validate_cdn_url_with_empty_string() {
|
||||||
|
$this->assertFalse(argon_validate_mermaid_cdn_url(''));
|
||||||
|
$this->assertFalse(argon_validate_mermaid_cdn_url(' '));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试验证无效协议的 CDN URL
|
||||||
|
*/
|
||||||
|
public function test_validate_cdn_url_with_invalid_protocol() {
|
||||||
|
$url = 'ftp://example.com/mermaid.min.js';
|
||||||
|
$this->assertFalse(argon_validate_mermaid_cdn_url($url));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试获取 Mermaid 主题(配置为 default)
|
||||||
|
*/
|
||||||
|
public function test_get_mermaid_theme_with_default() {
|
||||||
|
update_option('argon_mermaid_theme', 'default');
|
||||||
|
$this->assertEquals('default', argon_get_mermaid_theme());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试获取 Mermaid 主题(配置为 dark)
|
||||||
|
*/
|
||||||
|
public function test_get_mermaid_theme_with_dark() {
|
||||||
|
update_option('argon_mermaid_theme', 'dark');
|
||||||
|
$this->assertEquals('dark', argon_get_mermaid_theme());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试获取 Mermaid 主题(配置为 auto)
|
||||||
|
*/
|
||||||
|
public function test_get_mermaid_theme_with_auto() {
|
||||||
|
update_option('argon_mermaid_theme', 'auto');
|
||||||
|
// auto 模式在 PHP 端返回 default,实际切换在 JS 端
|
||||||
|
$this->assertEquals('default', argon_get_mermaid_theme());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试获取默认配置
|
||||||
|
*/
|
||||||
|
public function test_get_mermaid_default_config() {
|
||||||
|
$defaults = argon_get_mermaid_default_config();
|
||||||
|
|
||||||
|
$this->assertIsArray($defaults);
|
||||||
|
$this->assertArrayHasKey('enabled', $defaults);
|
||||||
|
$this->assertArrayHasKey('cdn_source', $defaults);
|
||||||
|
$this->assertArrayHasKey('theme', $defaults);
|
||||||
|
$this->assertFalse($defaults['enabled']);
|
||||||
|
$this->assertEquals('jsdelivr', $defaults['cdn_source']);
|
||||||
|
$this->assertEquals('auto', $defaults['theme']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试验证有效的配置
|
||||||
|
*/
|
||||||
|
public function test_validate_mermaid_settings_with_valid_settings() {
|
||||||
|
$settings = [
|
||||||
|
'enabled' => true,
|
||||||
|
'cdn_source' => 'jsdelivr',
|
||||||
|
'theme' => 'default',
|
||||||
|
'use_local' => false,
|
||||||
|
'debug_mode' => false
|
||||||
|
];
|
||||||
|
|
||||||
|
$errors = argon_validate_mermaid_settings($settings);
|
||||||
|
$this->assertEmpty($errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试验证无效的 CDN 来源
|
||||||
|
*/
|
||||||
|
public function test_validate_mermaid_settings_with_invalid_cdn_source() {
|
||||||
|
$settings = [
|
||||||
|
'cdn_source' => 'invalid_source'
|
||||||
|
];
|
||||||
|
|
||||||
|
$errors = argon_validate_mermaid_settings($settings);
|
||||||
|
$this->assertNotEmpty($errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试验证自定义 CDN 但 URL 为空
|
||||||
|
*/
|
||||||
|
public function test_validate_mermaid_settings_with_custom_cdn_empty_url() {
|
||||||
|
$settings = [
|
||||||
|
'cdn_source' => 'custom',
|
||||||
|
'custom_cdn_url' => ''
|
||||||
|
];
|
||||||
|
|
||||||
|
$errors = argon_validate_mermaid_settings($settings);
|
||||||
|
$this->assertNotEmpty($errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试验证自定义 CDN 但 URL 无效
|
||||||
|
*/
|
||||||
|
public function test_validate_mermaid_settings_with_custom_cdn_invalid_url() {
|
||||||
|
$settings = [
|
||||||
|
'cdn_source' => 'custom',
|
||||||
|
'custom_cdn_url' => 'not-a-url'
|
||||||
|
];
|
||||||
|
|
||||||
|
$errors = argon_validate_mermaid_settings($settings);
|
||||||
|
$this->assertNotEmpty($errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试验证无效的主题名称
|
||||||
|
*/
|
||||||
|
public function test_validate_mermaid_settings_with_invalid_theme() {
|
||||||
|
$settings = [
|
||||||
|
'theme' => 'invalid_theme'
|
||||||
|
];
|
||||||
|
|
||||||
|
$errors = argon_validate_mermaid_settings($settings);
|
||||||
|
$this->assertNotEmpty($errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试初始化默认配置
|
||||||
|
*/
|
||||||
|
public function test_init_mermaid_config() {
|
||||||
|
argon_init_mermaid_config();
|
||||||
|
|
||||||
|
$this->assertFalse(get_option('argon_enable_mermaid'));
|
||||||
|
$this->assertEquals('jsdelivr', get_option('argon_mermaid_cdn_source'));
|
||||||
|
$this->assertEquals('auto', get_option('argon_mermaid_theme'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试初始化不覆盖已有配置
|
||||||
|
*/
|
||||||
|
public function test_init_mermaid_config_does_not_override() {
|
||||||
|
update_option('argon_enable_mermaid', true);
|
||||||
|
argon_init_mermaid_config();
|
||||||
|
|
||||||
|
// 已有配置不应该被覆盖
|
||||||
|
$this->assertTrue(get_option('argon_enable_mermaid'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试获取所有配置选项
|
||||||
|
*/
|
||||||
|
public function test_get_all_mermaid_options() {
|
||||||
|
update_option('argon_enable_mermaid', true);
|
||||||
|
update_option('argon_mermaid_cdn_source', 'unpkg');
|
||||||
|
|
||||||
|
$options = argon_get_all_mermaid_options();
|
||||||
|
|
||||||
|
$this->assertIsArray($options);
|
||||||
|
$this->assertTrue($options['enabled']);
|
||||||
|
$this->assertEquals('unpkg', $options['cdn_source']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试批量更新配置(成功)
|
||||||
|
*/
|
||||||
|
public function test_update_mermaid_settings_success() {
|
||||||
|
$settings = [
|
||||||
|
'enabled' => true,
|
||||||
|
'cdn_source' => 'jsdelivr',
|
||||||
|
'theme' => 'dark'
|
||||||
|
];
|
||||||
|
|
||||||
|
$result = argon_update_mermaid_settings($settings);
|
||||||
|
|
||||||
|
$this->assertTrue($result['success']);
|
||||||
|
$this->assertEmpty($result['errors']);
|
||||||
|
$this->assertTrue(get_option('argon_enable_mermaid'));
|
||||||
|
$this->assertEquals('dark', get_option('argon_mermaid_theme'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试批量更新配置(失败)
|
||||||
|
*/
|
||||||
|
public function test_update_mermaid_settings_failure() {
|
||||||
|
$settings = [
|
||||||
|
'cdn_source' => 'invalid_source'
|
||||||
|
];
|
||||||
|
|
||||||
|
$result = argon_update_mermaid_settings($settings);
|
||||||
|
|
||||||
|
$this->assertFalse($result['success']);
|
||||||
|
$this->assertNotEmpty($result['errors']);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user