Files
wordpress-theme-aksrt/tests/widget-system/icon-picker-contract.test.cjs
T
kate522 0afead968a fix(icons): emit FA5 only and upgrade legacy FA4 icon values
The theme is FA5-only, but aksrt_icon_markup() prefixed bare glyphs with
the FA4 style token, so stored icon-picker values rendered FA4 markup,
and three runtime strings still carried the FA4 double-fa form (report
modal header and submit button, comment report menu item). That left the
navigation icon contract failing at 11/12.

- aksrt_icon_markup() normalizes bare glyphs, legacy FA4 values and
  style-only values to "fa fas fa-<glyph>", keeping the FA5 brand family
  (fa fab) and trailing classes intact.
- aksrt_normalize_icon_key() strips the legacy FA4 prefix so stored
  values resolve against the brand-icon catalog again.
- report.js and the comment report item emit FA5 markup.
- An icon-picker contract test locks the upgrade behaviour.

Verification: 338 widget-system, 12 navigation, 28 admin and 13
front-submit contract tests pass; normalization also exercised against
legacy, FA5 and brand inputs.
2026-09-20 18:48:22 +08:00

164 lines
9.9 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const root = path.resolve(__dirname, '..', '..');
const read = (file) => fs.readFileSync(path.join(root, file), 'utf8');
test('universal icon picker ships catalog data for the bundled FA5 fonts', () => {
const data = read('src/Icons/fa5-icons.php');
assert.match(data, /Font Awesome 5 Free \(5\.15\.4\)/);
assert.match(data, /'fas' =>/, 'solid group');
assert.match(data, /'far' =>/, 'regular group');
assert.match(data, /'fab' =>/, 'brands group');
const names = data.match(/'([a-z0-9-]+)'/g) || [];
assert.ok(names.length > 1500, 'generated catalog is the full bundled set');
for (const must of ["'clock'", "'folder-open'", "'file-alt'", "'weixin'"]) {
assert.match(data, new RegExp(must), `FA5 name ${must} present (FA4 leftovers replaced)`);
}
assert.doesNotMatch(data, /fa-clock-o|fa-folder-open-o|fa-file-text-o/, 'no FA4-only names');
});
test('icon catalog REST route is registered with permission + sprite single source', () => {
const admin = read('src/Admin/Admin.php');
assert.match(admin, /\AKSRT\\Icons\\IconController::class/);
const controller = read('src/Icons/IconController.php');
assert.match(controller, /\/icons\/catalog/);
assert.match(controller, /permission_callback.*check_permission/s);
const catalog = read('src/Icons/IconCatalog.php');
assert.match(catalog, /svg-icon\.js/);
assert.match(catalog, /aksrt_svgs/);
assert.match(catalog, /fa5-icons\.php/);
});
test('widget and nav-menu icon fields render the shared picker', () => {
const formRenderer = read('src/WidgetSystem/FormRenderer.php');
assert.match(formRenderer, /case 'icon':\s*\n\s*echo aksrt_icon_picker_field\(\$field_name, \$field_id, \(string\) \$value\);/);
assert.doesNotMatch(formRenderer, /aksrt-widget-icon/);
const adminForm = read('src/WidgetSystem/AdminFormController.php');
assert.match(adminForm, /aksrt_icon_picker_assets\(/);
const navMenu = read('src/Admin/NavMenuOptions.php');
assert.match(navMenu, /aksrt_icon_picker_field\(/);
assert.match(navMenu, /aksrt_sanitize_icon_value\(\(string\) \(\$raw\['icon'\] \?\? ''\)\)/);
const helper = read('inc/helpers/aksrt-icon-picker.php');
for (const token of ['function aksrt_icon_picker_assets', 'function aksrt_icon_picker_field', 'id="aksrt-modal-icon"', 'aksrt-icon-picker__value']) {
assert.match(helper, new RegExp(token));
}
assert.match(helper, /assets\/js\/runtime\/svg-icon\.js/, 'admin previews reuse the frontend sprite');
});
test('picker JS composes the three value formats and loads the REST catalog', () => {
const js = read('assets/js/admin-icon-picker.js');
for (const token of ['X-WP-Nonce', 'customsvg-', 'zibsvg-', '`fa ${group.id} fa-${name}`', 'data-aksrt-icon-picker', 'aksrt-icon-picker__value']) {
assert.match(js, new RegExp(token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')));
}
assert.match(js, /DOMParser/, 'custom SVG is sanitized client side');
const cssPath = 'assets/css/admin-icon-picker.css';
assert.ok(fs.existsSync(path.join(root, cssPath)), 'picker css shipped');
// Tab/search switching toggles the [hidden] attribute; without this guard
// the explicit display rules on grids/items override it and every group
// stays visible, so the tab bar appears dead.
assert.match(read(cssPath), /#aksrt-modal-icon \[hidden]\s*\{\s*display:\s*none\s*!important/);
});
test('icon values sanitize through the SVG-aware helper on every save path', () => {
const iconsHelper = read('inc/helpers/aksrt-icons.php');
assert.match(iconsHelper, /function aksrt_sanitize_icon_value/);
assert.match(iconsHelper, /function aksrt_icon_markup/);
assert.match(iconsHelper, /function aksrt_enqueue_fontawesome/);
assert.match(iconsHelper, /str_starts_with\(\$value, 'customsvg-'\)/);
const schemaProvider = read('src/Admin/SchemaProvider.php');
assert.match(schemaProvider, /'icon'\s+=>\s+'icon'/);
assert.match(schemaProvider, /case 'icon':\s*\n\s*return function_exists\('aksrt_sanitize_icon_value'\)/);
const fieldSchema = read('src/WidgetSystem/FieldSchema.php');
assert.match(fieldSchema, /case 'icon':\s*\n\s*return function_exists\('aksrt_sanitize_icon_value'\)/);
});
test('FA5 loads under a single canonical handle everywhere', () => {
const picker = read('inc/helpers/aksrt-icon-picker.php');
const admin = read('src/Admin/Admin.php');
const coreHooks = read('inc/core-hooks.php');
const part1b = read('src/Core/ScriptsPart1b.php');
assert.match(picker, /aksrt_enqueue_fontawesome\(\)/);
assert.match(admin, /aksrt_enqueue_fontawesome\(\)/);
assert.match(coreHooks, /aksrt_enqueue_fontawesome\(\)/);
assert.match(part1b, /aksrt_enqueue_fontawesome\(\['bootstrap'\]\)/);
assert.match(part1b, /\['aksrt-font-awesome'\]/, 'public main css depends on the canonical handle');
for (const file of [picker, admin, coreHooks, part1b]) {
assert.doesNotMatch(file, /wp_enqueue_style\(\s*'aksrt-font-awesome'/, 'no duplicate FA registrations');
}
for (const file of ['inc/helpers/aksrt-widgets-markup.php', 'src/WidgetSystem/Renderers/ContentRenderer.php']) {
const source = read(file);
assert.doesNotMatch(source, /fa-clock-o|fa-folder-open-o|fa-file-text-o/, `${file} uses FA5 names`);
}
});
test('custom settings pages and standalone save paths speak the three icon formats', () => {
// 手机底部Tab / 右侧悬浮按钮 custom pages render the shared picker.
for (const page of ['assets/admin/src/pages/content/FooterTabbarSettings.vue', 'assets/admin/src/pages/content/FloatingButtonsSettings.vue']) {
const source = read(page);
assert.match(source, /import FaIconPicker from '@\/components\/form\/FaIconPicker\.vue'/);
assert.match(source, /import IconValue from '@\/components\/form\/IconValue\.vue'/);
assert.match(source, /<FaIconPicker/, `${page} uses the picker for icon fields`);
// FaIconPicker declares modelValue / emits update:modelValue; the Naive UI
// v-model:value convention binds a prop the picker never reads, so
// selection clicks would be dropped.
assert.doesNotMatch(source, /<FaIconPicker[^>]*v-model:value/, `${page} must not bind the picker with v-model:value`);
assert.match(source, /<FaIconPicker v-model=/, `${page} binds the picker with v-model`);
}
// Their REST save paths keep customsvg markup intact.
const controller = read('src/Admin/GlobalControllerPart1.php');
assert.match(controller, /aksrt_sanitize_icon_value\(/);
assert.doesNotMatch(controller, /preg_match\('\/\^fa\\s\+/, 'no FA-only regex gate on icon fields');
// Frontend consumers render every format.
const footer = read('footer.php');
assert.match(footer, /aksrt_icon_markup\(\(string\) \$tab_icon\)/);
assert.match(footer, /aksrt_icon_markup\(\(string\) \$item_icon\)/);
assert.match(read('inc/helpers/aksrt-runtime-win.php'), /aksrt_icon_markup\(\$icon\)/);
// The popover tabs must wrap instead of overflowing the panel.
assert.match(read('assets/admin/src/components/form/FaIconPicker.vue'), /flex-wrap: wrap/);
});
test('nav menu icons render every format without legacy fa-prefix mangling', () => {
const helper = read('inc/helpers/menu-decoration.php');
assert.match(helper, /str_starts_with\(\$icon_class, 'zibsvg-'\)/);
assert.match(helper, /aksrt_icon_markup\(\$icon_class, 'icon mr3 menu-item-icon'\)/);
assert.doesNotMatch(helper, /'<i class="' \. esc_attr\(\$icon_class\)/, 'no raw i-tag bypassing the unified helper');
});
test('icon markup upgrades legacy FA4 values to FA5 and never emits the FA4 form', () => {
const helper = read('inc/helpers/aksrt-icons.php');
// Bare glyph, legacy double-fa value and style-only value all normalize to FA5 solid.
assert.match(helper, /preg_match\('\/\^fa\\s\+fa-\/i', \$value\)/, 'legacy double-fa value is detected');
assert.match(helper, /preg_replace\('\/\^fa\\s\+\/i', 'fa fas ', \$value, 1\)/, 'legacy double-fa value upgrades to FA5');
assert.match(helper, /\$value = 'fa fas ' \. \$value;/, 'bare glyph gets the FA5 solid prefix');
assert.match(helper, /preg_match\('\/\^\(\?:fas\|far\|fab\)\\s\+fa-\/i', \$value\)/, 'style-only value gets the fa token');
// Key normalization strips the same legacy form, otherwise stored values miss the
// brand-icon catalog and the picker shows an unresolvable key.
assert.match(helper, /preg_replace\('\/\^fa\\s\+fa-\/i', '', \(string\) \$name\)/, 'icon keys normalize the legacy FA4 form');
});
test('brand icons render despite the zibll-legacy fa-fab double prefix', () => {
const rule = /\.fa\.fab\{font-family:"Font Awesome 5 Brands";font-weight:400\}/;
for (const file of ['assets/css/main.css', 'assets/css/admin-icon-picker.css', 'assets/admin/src/style.css']) {
assert.match(read(file), rule, `${file} re-asserts the brand family`);
}
assert.match(read('assets/css/main.min.css'), rule, 'minified main css carries the fix');
// Empty sprite entries surface as blank picker items — none may ship.
assert.doesNotMatch(read('assets/js/runtime/svg-icon.js'), /aksrt_svgs\['[a-zA-Z0-9_]+'\]\s*=\s*'';/);
});
test('icon assets are consolidated and dead icon assets are gone', () => {
for (const file of ['assets/icons/upload-add.svg', 'assets/icons/default-favicon.svg', 'assets/icons/default-logo.svg', 'assets/icons/playing.svg', 'assets/icons/user-certification.svg']) {
assert.ok(fs.existsSync(path.join(root, file)), `${file} exists`);
}
for (const file of ['assets/img/upload-add.svg', 'assets/img/vip-1.svg', 'assets/img/vip-2.svg', 'assets/img/404.svg', 'assets/img/msg-system.svg', 'assets/fonts/img-code.ttf', 'assets/js/runtime/gutenberg-extend.js', 'assets/fontawesome5/webfonts/fa-solid-900.ttf']) {
assert.ok(!fs.existsSync(path.join(root, file)), `${file} removed`);
}
const mainCss = read('assets/css/main.css');
assert.match(mainCss, /url\(\.\.\/icons\/playing\.svg\)/);
const phpRefs = read('inc/helpers/comments/form-parts.php');
assert.match(phpRefs, /\/assets\/icons\/upload-add\.svg/);
});