Files

84 lines
3.7 KiB
JavaScript

const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const test = require('node:test');
const root = path.resolve(__dirname, '..', '..');
const read = (file) => fs.readFileSync(path.join(root, file), 'utf8');
test('badges use server-rendered admin-ajax modal actions only', () => {
const badges = read('src/User/Badges.php');
const identity = read('inc/helpers/aksrt-identity.php');
const scripts = read('src/Core/ScriptsPart1b.php');
for (const action of [
'aksrt_user_medal_info_modal',
'aksrt_single_medal_info_modal',
'aksrt_user_medal_manually_add_modal',
'aksrt_user_medal_wear',
'aksrt_user_medal_manually_add',
'aksrt_user_medal_manually_remove',
]) {
assert.match(badges, new RegExp(`wp_ajax_${action}`));
}
assert.match(badges, /wp_ajax_nopriv_aksrt_user_medal_info_modal/);
assert.match(badges, /wp_ajax_nopriv_aksrt_single_medal_info_modal/);
assert.match(identity, /data-aksrt-modal-cache="false"/);
assert.match(identity, /admin-ajax\.php/);
assert.match(identity, /aksrt-badge-modal--overview/);
assert.match(identity, /aksrt-badge-modal--grant/);
assert.match(badges, /aksrt-badge-modal--single/);
assert.doesNotMatch(identity, /data-aksrt-modal-class="full-sm"/);
assert.doesNotMatch(badges, /data-aksrt-modal-class="modal-mini"/);
assert.match(scripts, /user-badge-actions\.js/);
assert.doesNotMatch(scripts, /user-badges\.js/);
assert.doesNotMatch(badges, /register_rest_route|rest_wear|rest_list|rest_grant|rest_revoke/);
assert.doesNotMatch(identity, /data-aksrt-badges-open|data-aksrt-badges-can-wear/);
});
test('badge mutations enforce the public nonce, login, ownership, and manual grant rules', () => {
const badges = read('src/User/Badges.php');
assert.match(badges, /ajax_nonce\(\): string \{ return 'aksrt_public_nonce'; \}/);
assert.match(badges, /wp_verify_nonce\(\$nonce, self::ajax_nonce\(\)\)/);
assert.match(badges, /if \(!is_user_logged_in\(\)\) self::ajax_error/);
assert.match(badges, /if \(\$operation !== 'wear' && !self::can_manually_set\(\)\)/);
assert.match(badges, /\$operation === 'grant' && \(\$item\['get_type'\] \?\? ''\) !== 'manually_add'/);
assert.match(badges, /\$operation === 'revoke' && empty\(\$item\['owned'\]\)/);
assert.match(badges, /if \(!in_array\(\$slug, \$owned, true\)\)/);
assert.match(badges, /'show_unearned'\s*=>\s*\$show_unearned/);
});
test('badge actions close the HTML dialog and reload the server-rendered page', () => {
const actions = read('assets/js/user-badge-actions.js');
assert.match(actions, /aksrt_user_medal_wear/);
assert.match(actions, /aksrt_user_medal_manually_add/);
assert.match(actions, /aksrt_user_medal_manually_remove/);
assert.match(actions, /AksrtModalRuntime\.close\('badge-action'\)/);
assert.match(actions, /window\.location\.reload\(\)/);
assert.doesNotMatch(actions, /AksrtRequest|fetchJson|restUrl/);
});
test('the retired badge rollout and JSON modal CSS are absent', () => {
const retired = [
'src/Settings/FrontendModalRuntime.php',
'src/Admin/Schema/SchemaToolsFrontendModalRollout.php',
'assets/admin/src/schema/sections/tools/frontend-modal-rollout.js',
];
for (const file of retired) {
assert.equal(fs.existsSync(path.join(root, file)), false, `${file} should be deleted`);
}
const sources = [
'src/Core/ScriptsPart1c.php',
'assets/css/frontend/medal.css',
].map(read).join('\n');
assert.doesNotMatch(sources, /frontend_modal_rollout_badge/);
assert.doesNotMatch(sources, /\.uc-badges-/);
assert.match(sources, /grid-template-columns:\s*repeat\(5, 1fr\)/);
assert.match(sources, /grid-template-columns:\s*repeat\(4, 1fr\)/);
assert.match(sources, /grid-template-columns:\s*repeat\(3, 1fr\)/);
});