Files
wordpress-theme-aksrt/tests/admin/wechat-settings-rest-contract.test.cjs
T

45 lines
2.1 KiB
JavaScript

'use strict'
const assert = require('node:assert/strict')
const { execFileSync } = require('node:child_process')
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')
const controller = read('src/Admin/UserController.php')
const sender = read('src/Payment/WeChatNotify.php')
const settings = read('src/Settings/Integration.php')
const page = read('assets/admin/src/pages/users/UsersWechatTemplate.vue')
test('设置、状态和测试接口均受后台权限约束', () => {
for (const route of ['/wechat/status', '/wechat/settings', '/wechat/test']) {
assert.match(controller, new RegExp(`register_rest_route\\(self::API_NAMESPACE, '${route}'[\\s\\S]*?'permission_callback'\\s*=>\\s*\\[\\$this, 'check_permission'\\]`))
}
})
test('配置与发送共用公众号登录及已绑定账号', () => {
assert.match(sender, /Settings::get_weixingzh_settings\(\)/)
assert.match(sender, /WeixinGzh::get_instance\(\)->access_token\(\)/)
assert.match(sender, /jio_weixingzh_id/)
assert.doesNotMatch(sender, /jio_wechat_openid|jio_wechat_subscriptions|set_transient\('jio_wechat_token'/)
assert.match(settings, /WechatTemplates::normalize\(\$stored\)/)
assert.doesNotMatch(settings, /jio_wechat_settings'[\s\S]{0,350}'app_secret'/)
})
test('页面提供分类型设置和发送测试', () => {
assert.match(page, /form\.templates\[type\.key\]\.enabled/)
assert.match(page, /form\.templates\[type\.key\]\.id/)
assert.match(page, /form\.templates\[type\.key\]\.keys\[field\.key\]/)
assert.match(page, /\/wechat\/test/)
assert.match(page, /data: JSON\.parse\(snapshot\.value\)/)
assert.doesNotMatch(page, /structuredClone\(form\)/)
assert.doesNotMatch(page, /form\.app_secret|form\.template_id|alert\(/)
})
test('PHP 行为验证', () => {
const output = execFileSync('php', [path.join(root, 'tests/admin/wechat-settings-harness.php')], { encoding: 'utf8' })
assert.match(output, /WeChat settings harness passed/)
})