Files
ny-exam-platform/docs/dev/testing.md
T
nxxy335 804b04de60 移除登录失败限流;修复连接信息被 e2e 串味为 127.0.0.1
- bootstrap.php/login.php:整体移除「5 次失败锁 10 分钟」限流,登录失败仅即时提示原因
- 新增 tools/fix-config.php:一键双写修正连接地址(默认 exam.nxxy335.top),清空账号密码让用户重输真实卷王凭据
- NY_DATA 隔离落地:run-all.sh 给写配置的 e2e 指向临时目录;修 Windows Git Bash /c/... 路径规范化 bug;.ny-test-data 加 .gitignore
- 清理 dead throttle 引用(e2e 脚本 + docs)
- 修 login.php 真实 bug:ny_is_auth() 分支在加载 $cfg 前引用导致 Undefined variable 警告
- 全量测试 116 通过(PHP 44 + e2e 72)
2026-09-11 17:25:22 +08:00

97 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 测试
回归测试分两类:**PHP 自测**(纯逻辑断言)与 **Node e2e**(起 mock 服务 + PHP 内置服务器,走真实 HTTP)。
## 一键运行
```bash
bash tests/run-all.sh
```
脚本会依次执行:
| 文件 | 类型 | 覆盖 |
| --- | --- | --- |
| `tests/test-aggregate.php` | PHP | 聚合、去重、考试次数、排序 |
| `tests/test-new-features.php` | PHP | 再次提交置顶排序、XLSX 多表、名单导入列识别 |
| `tools/test-xlsxwriter.php` | PHP | XLSX 写入与 ZIP 打包 |
| `tools/test-config-save.js` | Node e2e | 配置保存、连接变化检测、每卷配置隔离、CSRF |
| `tools/test-draw-alloc.js` | Node e2e | 随机抽取名额分配(加权 / 自定义 / 守恒 fuzz) |
可通过环境变量覆盖运行时路径:
```bash
PHP_BIN=/usr/bin/php NODE_BIN=$(which node) bash tests/run-all.sh
```
## 单独运行 PHP 自测
```bash
php -d extension_dir="C:/php/ext" -d extension=pdo_sqlite tests/test-aggregate.php
php tests/test-new-features.php
```
输出为逐条 `✓ / ✗` 断言,结尾给出通过数 / 总数,失败时非零退出。
## 单独运行 Node e2e
e2e 需要两个进程:
1. **mock 卷王服务**(必须在 `tools/` 目录下启动,内部用 `__dirname` 解析相对路径):
```bash
cd tools
node mock-server.js # 默认监听 19911
```
2. **PHP 内置服务器**(端口 18081):
```bash
php -S 127.0.0.1:18081
```
然后:
```bash
node tools/test-config-save.js
node tools/test-draw-alloc.js
```
::: warning 测试会污染 `data/`
e2e 会备份并修改 `data/ny.db`、`data/config.json`、`data/roster.json`,
并在结束后恢复。若中途异常中断,请手动检查并还原:
```bash
git status data/ # data/ 已被 gitignore,需手工核对
```
:::
## 写新测试时注意
| 陷阱 | 说明 |
| --- | --- |
| CSRF | 写接口必须带 `_csrf`,e2e 里先从 `config_get` 取全局 token |
| DB 优先 | 配置读的是 **DB 优先**,只备份 `config.json` 不够,必须一起备份 `data/ny.db` |
| `/tmp` 路径 | Node 在 Windows 下会把 `/tmp/x.js` 解析成 `C:\tmp\x.js`,脚本请写在项目目录内 |
| `json_decode` | 必须传 `true` 得到关联数组,否则 `is_array(stdClass)` 为 `false` 会静默跳过 |
| mock 账号 | mock-server 接受账号 `mock` |
## 手工冒烟清单
改完代码建议至少过一遍:
- [ ] 登录 → 主页面三张卡片有数字
- [ ] 切换班级 → 卡片与表格联动
- [ ] 点某一行 → 能看到该学生的每次提交
- [ ] 导入含 `.php` 后缀的文件 → 被拒绝
- [ ] 导入正常 `.xlsx` → 成功,且 `data/uploads/` 为空
- [ ] 导出成绩 / 导出缺考 → 能打开,学号不是科学计数法
- [ ] 打印预览 → 默认成绩降序
- [ ] 数据看板全屏进入 / 退出都正常
- [ ] 随机抽取 → 名额守恒
## 相关
- [架构与目录](./architecture.md)
- [接口一览](./api.md)