119 lines
6.9 KiB
PHP
119 lines
6.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
require dirname(__DIR__) . '/vendor/topthink/framework/src/helper.php';
|
|
|
|
use app\adminapi\controller\setting\system\LogController;
|
|
use app\adminapi\service\DesktopSessionService;
|
|
use app\common\enum\AdminTerminalEnum;
|
|
use app\Request;
|
|
use think\facade\Db;
|
|
|
|
// 直接调用报表控制器,使用隔离的内存库,不加载项目环境。
|
|
$app = new think\App(sys_get_temp_dir() . '/desktop-session-report-test');
|
|
$manager = new think\DbManager();
|
|
$manager->setConfig(['default' => 'test', 'connections' => [
|
|
'test' => ['type' => 'sqlite', 'database' => ':memory:', 'prefix' => 'test_'],
|
|
]]);
|
|
$app->instance('think\DbManager', $manager);
|
|
think\Model::setDb($manager);
|
|
$config = new think\Config();
|
|
$config->set(['lists' => ['page_size' => 20, 'page_size_max' => 100],
|
|
'data_scope' => ['enabled' => true]], 'project');
|
|
$app->instance('config', $config);
|
|
think\Validate::maker(static fn ($validate) => $validate->setRequest(request()));
|
|
|
|
foreach ([
|
|
'admin_role' => 'admin_id INTEGER, role_id INTEGER',
|
|
'admin_dept' => 'admin_id INTEGER, dept_id INTEGER',
|
|
'system_role' => 'id INTEGER PRIMARY KEY, name TEXT, data_scope INTEGER, delete_time INTEGER',
|
|
'system_menu' => 'id INTEGER PRIMARY KEY, perms TEXT, is_disable INTEGER DEFAULT 0',
|
|
'system_role_menu' => 'role_id INTEGER, menu_id INTEGER',
|
|
'admin_session' => 'id INTEGER PRIMARY KEY, workstation_slot INTEGER, terminal INTEGER, token TEXT, expire_time INTEGER, update_time INTEGER',
|
|
'desktop_login_session' => 'id INTEGER PRIMARY KEY, admin_id INTEGER, account TEXT, name TEXT, login_time INTEGER,
|
|
logout_time INTEGER NULL, last_seen_time INTEGER, due_time INTEGER, expires_at INTEGER, duration_seconds INTEGER, end_reason TEXT',
|
|
] as $table => $fields) {
|
|
Db::execute("CREATE TABLE test_{$table} ({$fields})");
|
|
}
|
|
$pdo = Db::connect()->getPdo();
|
|
$pdo->sqliteCreateFunction('LEAST', 'min', 2);
|
|
$pdo->sqliteCreateFunction('GREATEST', 'max', 2);
|
|
Db::execute("INSERT INTO test_system_role VALUES (10,'医助',4,NULL),(12,'主管',3,NULL),(13,'系统管理员',1,NULL)");
|
|
Db::execute('INSERT INTO test_admin_role VALUES (20,10),(21,10),(30,12)');
|
|
Db::execute('INSERT INTO test_admin_dept VALUES (20,1),(21,2),(30,1)');
|
|
Db::execute("INSERT INTO test_system_menu VALUES (1,'setting.system.log/desktopSessions',0)");
|
|
Db::execute('INSERT INTO test_system_role_menu VALUES (10,1),(12,1)');
|
|
|
|
$tz = new DateTimeZone('Asia/Shanghai');
|
|
$start = (new DateTimeImmutable('2024-01-02', $tz))->getTimestamp();
|
|
$insert = static function (int $id, int $owner, int $login, ?int $logout, string $reason, ?int $lastSeen = null): void {
|
|
$lastSeen ??= $logout ?? $login;
|
|
Db::name('desktop_login_session')->insert([
|
|
'id' => $id, 'admin_id' => $owner, 'account' => 'staff' . $owner,
|
|
'name' => $owner === 20 ? '张医生' : '王医生', 'login_time' => $login, 'logout_time' => $logout,
|
|
'expires_at' => DesktopSessionService::nextCutoff($login), 'last_seen_time' => $lastSeen,
|
|
'due_time' => min(DesktopSessionService::nextCutoff($login), $lastSeen + DesktopSessionService::HEARTBEAT_GRACE),
|
|
'duration_seconds' => $lastSeen - $login, 'end_reason' => $reason,
|
|
]);
|
|
};
|
|
$insert(1, 20, $start - 1800, $start + 1800, 'logout');
|
|
$insert(2, 20, $start - 200, $start, 'logout');
|
|
$insert(3, 20, $start, $start, 'replaced');
|
|
$insert(4, 21, $start + 1200, $start + 2400, 'revoked');
|
|
$insert(5, 20, $start + 10, null, '', $start + 70);
|
|
$insert(6, 20, $start + 86400 - 600, $start + 86400 + 1200, 'logout');
|
|
$due = $start + 70 + DesktopSessionService::HEARTBEAT_GRACE;
|
|
Db::name('admin_session')->insert(['workstation_slot' => 5, 'terminal' => AdminTerminalEnum::WORKSTATION,
|
|
'token' => 'expired-test-session', 'expire_time' => $due, 'update_time' => $start + 70]);
|
|
|
|
$query = static function (int $viewer, array $params = [], bool $root = false) use ($app): array {
|
|
$request = (new Request())->withServer(['REQUEST_METHOD' => 'GET'])->withGet($params);
|
|
$request->adminId = $viewer;
|
|
// 模拟变更前缓存仍为管理员的情况,数据范围须依据当前角色。
|
|
$request->adminInfo = ['admin_id' => $viewer, 'role_id' => [13], 'root' => (int) $root];
|
|
$app->instance('request', $request);
|
|
try {
|
|
return (new LogController($app))->desktopSessions()->getData();
|
|
} catch (think\exception\HttpResponseException $error) {
|
|
return $error->getResponse()->getData();
|
|
}
|
|
};
|
|
$expect = static function (bool $ok, string $message): void {
|
|
if (!$ok) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$params = ['date' => '2024-01-02'];
|
|
$expect($query(99, $params)['code'] === 0, '无菜单权限不能读取报表');
|
|
$report = $query(1, $params, true)['data'];
|
|
$expect($report['count'] === 5 && array_column($report['lists'], 'id') === [6,4,5,3,1],
|
|
'午夜结束应算在前一天、同秒登录退出算在当天,按登录时间倒序');
|
|
$expect($report['extend']['total_day_seconds'] === 3660, '合计使用自然日内的实际交集');
|
|
$rows = array_column($report['lists'], null, 'id');
|
|
$expect($rows[1]['duration_seconds'] === 3600 && $rows[1]['day_seconds'] === 1800
|
|
&& $rows[1]['login_time'] === '2024-01-01 23:30:00' && $rows[1]['logout_time'] === '2024-01-02 00:30:00',
|
|
'跨午夜分摊时保留会话完整时长与北京时间');
|
|
$expect($rows[5]['status'] === 'timeout' && $rows[5]['status_label'] === '连接超时'
|
|
&& $rows[5]['duration_seconds'] === 60 && $rows[5]['day_seconds'] === 60, '查询可补结算掉线会话');
|
|
$expect($query(1, ['date' => '2024-01-03'], true)['data']['extend']['total_day_seconds'] === 1200,
|
|
'跨次日的会话仅计次日部分');
|
|
$own = $query(20, $params + ['apply_data_scope' => 0, 'page_size' => 1, 'page_no' => 2])['data'];
|
|
$expect($own['count'] === 4 && $own['extend']['total_day_seconds'] === 2460
|
|
&& array_column($own['lists'], 'id') === [5], '本人视图的列表、分页计数与合计使用相同范围');
|
|
$team = $query(30, $params + ['keyword' => '医生'])['data'];
|
|
$expect($team['count'] === 4 && $team['extend']['total_day_seconds'] === 2460, '主管仅可查询同部门历史');
|
|
$other = $query(30, $params + ['keyword' => 'staff21'])['data'];
|
|
$expect($other['count'] === 0 && $other['extend']['total_day_seconds'] === 0, '域外账号搜索不能泄漏计数或汇总');
|
|
$expect($query(1, $params + ['keyword' => '王医生'], true)['data']['count'] === 1, '按姓名快照搜索');
|
|
$expect($query(1, $params + ['keyword' => '%'], true)['data']['count'] === 0, '按字面匹配特殊字符');
|
|
$expect($query(1, ['date' => '2024-02-30'], true)['code'] === 0, '拒绝无效日期');
|
|
|
|
$current = time();
|
|
$insert(7, 20, $current - 10, null, '', $current);
|
|
$live = $query(20)['data'];
|
|
$expect($live['count'] === 1 && $live['lists'][0]['status'] === 'online' && $live['lists'][0]['logout_time'] === null
|
|
&& $live['lists'][0]['duration_seconds'] >= 10, '默认查询北京时间今天,进行中会话实时计时');
|
|
echo "DesktopSessionReportTest OK\n";
|