Files
wordpress-theme-aksrt/tests/widget-system/admin-stats-column-contract.test.cjs
T
kate522 ef718042d2 feat(admin): 新增后台文章列表销售数据列与快捷编辑配置项
「所有文章」列表此前只有互动数据列,缺少销售数据表头;快捷/批量编辑
展开后也只有原生三项。本次对照参考主题补齐这两处,不含专题链路。

- 新增 PostListEnhance:统管 sale/stats 两列、付费类型筛选、
  orderby=sales_volume 排序,以及四组快捷编辑字段的渲染与保存
- 新增 PostListEditFields:单独承载字段定义,主类职责保持单一
- 新增 admin-post-list.js:按 pay_type/pay_modo 勾选切换字段显隐
- 从 inc/core-hooks.php 迁出原有列实现(删 74 行),不再保留旧闭包
- 清理 admin-main.css 中确认无引用的 .aksrt-tab-pane 与 .aksrt-modal* 死块
- 新增 post-list-quick-edit-contract 测试,更新 stats/icon-picker 契约

快捷编辑锚点列用互动数据列而非参考主题的专题分类法列(本项目无专题)。
真实销量改为页面级批量查询,避免逐行查库。金额运算统一走 Ledger 的
「分」单位,不用裸浮点。保存走 edit_post 钩子,一次覆盖快捷与批量两条路径。
2026-10-06 15:54:47 +08:00

135 lines
6.4 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')
const list = read('src/Admin/Native/PostListEnhance.php')
const domain = read('src/Core/DomainModel.php')
const tags = read('inc/helpers/aksrt-frontend-tags.php')
const pay = read('inc/helpers/aksrt-pay.php')
test('admin post list has the interaction stats column', () => {
assert.match(list, /manage_post_posts_columns/)
assert.match(list, /互动数据/)
assert.match(list, /manage_post_posts_custom_column/)
assert.match(list, /COLUMN_STATS/)
})
test('admin post list has the sales data column with a sortable header', () => {
// 表头「销售数据」必须是可点击排序链接,排序键为 sales_volume。
assert.match(list, /销售数据/)
assert.match(list, /'aksrt_sales'/)
assert.match(list, /orderby.*sales_volume/)
// 排序列必须在表头注册,并把 sales_volume 映射回真实存储键。
assert.match(list, /manage_edit-post_sortable_columns/)
assert.match(list, /sales_volume/)
assert.match(list, /RESOURCE_SALES_VOLUME_KEY/)
})
test('sales column renders pay type, price and both sales counts', () => {
// 未开启付费 → 提示;已开启 → 类型链接 + 价格 + 销量。
assert.match(list, /未开启付费/)
assert.match(list, /pay_type_labels\(\)/)
assert.match(list, /真实销量/)
assert.match(list, /get_content_sales_map/)
// 第一行取自 _aksrt_pay,第二行的销量取自 _aksrt_sales_volume。
assert.match(list, /aksrt_get_pay_meta/)
assert.match(list, /DomainModel::RESOURCE_SALES_VOLUME_KEY/)
// 真实销量链接必须跳到订单页(aksrt_orders)并按商品 ID 过滤。
assert.match(list, /'aksrt_orders'/)
assert.match(list, /'product_id'/)
})
test('interaction counts are read from the keys the theme actually writes', () => {
// 计数一律落在这几个 meta key 上:aksrt_post_views / _aksrt_like_count /
// _aksrt_favorite_count / _aksrt_res_download_count。列回调只能用同一套键,
// 否则全列恒为 0。旧实现读 'views' / 'like' / 'favorite',仓库里没有任何写入方。
assert.match(tags, /'_aksrt_like_count'/)
assert.match(tags, /'_aksrt_favorite_count'/)
assert.match(tags, /'aksrt_post_views'/)
assert.match(list, /aksrt_get_post_views\(\$post_id\)/)
assert.match(list, /aksrt_get_post_likes\(\$post_id\)/)
assert.match(list, /aksrt_get_post_favorites\(\$post_id\)/)
// 资源下载量只有 _aksrt_res_download_count 一个写入键。
assert.match(list, /DomainModel::RESOURCE_DOWNLOAD_COUNT_KEY/)
assert.match(domain, /RESOURCE_DOWNLOAD_COUNT_KEY\s*=\s*'_aksrt_res_download_count'/)
// 不得再直接读这些不存在的键('download_count' 只作为历史遗留键回退读取)。
const column = list.slice(list.indexOf('private function render_stats_column'), list.indexOf('private function render_sales_column'))
assert.doesNotMatch(column, /get_post_meta\(\$post_id, 'views'/)
assert.doesNotMatch(column, /get_post_meta\(\$post_id, 'like'/)
assert.doesNotMatch(column, /get_post_meta\(\$post_id, 'favorite'/)
})
test('admin stats column shares the aksrt_format_number abbreviation', () => {
// 后台列与前台必须同一套缩写口径(≥10000 → 1.2W+,≥1000 → 1.5K)。
assert.match(list, /aksrt_format_number\(\$n\)/)
const column = list.slice(list.indexOf('private function render_stats_column'), list.indexOf('private function render_sales_column'))
assert.doesNotMatch(column, /'万'/)
})
test('sortable meta maps the public orderby keys to the stored keys', () => {
assert.match(list, /'views'\s*=>\s*\\AKSRT\\Core\\DomainModel::POST_VIEW_COUNT_KEY/)
assert.match(list, /'like'\s*=>\s*'_aksrt_like_count'/)
assert.match(list, /'favorite'\s*=>\s*'_aksrt_favorite_count'/)
assert.match(list, /'download_count'\s*=>\s*\\AKSRT\\Core\\DomainModel::RESOURCE_DOWNLOAD_COUNT_KEY/)
// meta_key 必须是真实存储键,而不是用户可见的排序键。
assert.match(list, /\$query->set\('meta_key', \$this->stats_meta_key\(\$orderby\)\)/)
})
test('every counter meta key the admin column renders has a writer', () => {
// 逐键核对:列里展示的每项计数都必须在某个 helper 里真正被写入过。
// _aksrt_sales_volume 的写入方是付费订单流水(PayOrders),不在这份清单里,
// 由下一个用例单独覆盖。
const sources = [
'src/Admin/Native/PostListEnhance.php',
'inc/helpers/aksrt-frontend-tags.php',
'src/Core/PostMetaBox.php',
'src/Frontend/PageAdminSet.php',
].map(read).join('\n')
for (const key of ['aksrt_post_views', '_aksrt_like_count', '_aksrt_favorite_count']) {
assert.ok(sources.includes(`'${key}'`), `${key} is written somewhere`)
}
// 资源帖的浏览有独立键,aksrt_get_post_views() 取两者较大值。
assert.match(tags, /_aksrt_res_view_count/)
assert.match(read('inc/helpers/aksrt-resource.php'), /RESOURCE_VIEW_COUNT_KEY|_aksrt_res_view_count/)
// 资源下载走普通外链,主题内没有任何写入方,所以下载项只在 >0 时出现。
assert.match(list, /\$downloads <= 0/)
assert.match(list, /\$legacy_downloads/)
})
test('sales volume is written by the order pipeline', () => {
// _aksrt_sales_volume 的写入方在付费助手与订单处理器里,列本身只读。
assert.match(pay, /aksrt_bump_content_sales_volume/)
assert.match(pay, /RESOURCE_SALES_VOLUME_KEY/)
assert.match(read('src/Content/PayOrders.php'), /aksrt_bump_content_sales_volume/)
})
test('admin column icons stay behind the Font Awesome enqueue on edit.php', () => {
assert.match(list, /admin_enqueue_scripts[\s\S]*?edit\.php[\s\S]*?aksrt_enqueue_fontawesome\(\)/)
for (const klass of ['fa-eye', 'fa-thumbs-up', 'fa-star', 'fa-download']) {
assert.match(list, new RegExp(klass))
}
})
test('the old closure-based column implementation is gone from core-hooks', () => {
// 列与排序逻辑统一收在 src/Admin/Native/PostListEnhance.php,inc/core-hooks.php
// 不再重复注册,否则同一列会被渲染两次。
const hooks = read('inc/core-hooks.php')
assert.doesNotMatch(hooks, /manage_post_posts_columns/)
assert.doesNotMatch(hooks, /manage_post_posts_custom_column/)
assert.doesNotMatch(hooks, /aksrt_stats/)
})
test('PostListEnhance is wired up from functions.php', () => {
assert.match(read('functions.php'), /PostListEnhance::get_instance\(\)->init\(\)/)
})