Files
zyt/server/tests/WecomPromotionOverviewSmokeTest.php
T
2026-09-01 10:42:16 +08:00

104 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
use app\adminapi\logic\firstvisit\WecomPromotionLogic;
use app\common\service\DataScope\DataScopeService;
use think\facade\Db;
require dirname(__DIR__) . '/vendor/autoload.php';
$app = new think\App();
$app->initialize();
$admin = Db::name('admin')->where('root', 1)->whereNull('delete_time')->find();
if (!$admin) {
throw new RuntimeException('未找到 root 管理员,无法执行数据范围冒烟测试');
}
$overview = WecomPromotionLogic::overview((int) $admin['id'], $admin, 'https://example.test');
foreach (['meta', 'config', 'summary', 'pools', 'links', 'member_options', 'operator_options', 'department_options'] as $key) {
if (!array_key_exists($key, $overview)) {
throw new RuntimeException("overview 缺少 {$key}");
}
}
if (!str_ends_with((string) ($overview['config']['callback_url'] ?? ''), '/api/qywx/external-contact/notify')) {
throw new RuntimeException('overview 未返回正确的获客消息回调地址');
}
foreach ($overview['member_options'] as $member) {
if (empty($member['id']) || empty($member['userid'])) {
throw new RuntimeException('member_options 返回了未绑定企业微信 userid 的成员');
}
if (!array_key_exists('display_dept_id', $member)) {
throw new RuntimeException('member_options 缺少树形下拉展示部门');
}
}
$enabledOperatorCount = 0;
foreach ($overview['operator_options'] as $operator) {
$isEnabled = (int) ($operator['disable'] ?? 0) === 0;
if ($isEnabled) {
$enabledOperatorCount++;
}
if ((bool) ($operator['can_grant'] ?? false) !== $isEnabled) {
throw new RuntimeException('操作人可授权状态不应依赖目标账号预先拥有页面权限');
}
}
$memberIds = array_values(array_unique(array_map('intval', array_column($overview['member_options'], 'id'))));
if ($memberIds !== []) {
$disabledMemberCount = (int) Db::name('admin')->whereIn('id', $memberIds)->where('disable', '<>', 0)->count();
if ($disabledMemberCount > 0) {
throw new RuntimeException('member_options 返回了已禁用的成员');
}
}
if (!is_array($overview['department_options'])) {
throw new RuntimeException('department_options 必须是部门树数组');
}
foreach ($overview['pools'] as $pool) {
foreach (['operators', 'operator_admin_ids', 'can_operate', 'can_manage_access', 'can_delete'] as $key) {
if (!array_key_exists($key, $pool)) {
throw new RuntimeException("pools 缺少共享操作权限字段 {$key}");
}
}
}
$scopedAdmin = Db::name('admin')->where('root', 0)->whereNull('delete_time')->order('id', 'asc')->find();
if ($scopedAdmin) {
$visibleIds = DataScopeService::getVisibleAdminIds((int) $scopedAdmin['id'], $scopedAdmin);
$scopedOverview = WecomPromotionLogic::overview((int) $scopedAdmin['id'], $scopedAdmin, 'https://example.test');
if ($visibleIds !== null) {
$scopedPoolIds = array_values(array_filter(array_map('intval', array_column($scopedOverview['pools'], 'id'))));
$sharedPoolMemberIds = $scopedPoolIds === []
? []
: array_map('intval', Db::name('qywx_promotion_pool_member')
->whereIn('pool_id', $scopedPoolIds)
->whereNull('delete_time')
->column('admin_id'));
foreach ($scopedOverview['member_options'] as $member) {
if (!in_array((int) $member['id'], $visibleIds, true)
&& !in_array((int) $member['id'], $sharedPoolMemberIds, true)) {
throw new RuntimeException('member_options 泄露了当前角色或部门范围外的成员');
}
}
foreach ($scopedOverview['operator_options'] as $operator) {
if (!in_array((int) $operator['id'], $visibleIds, true)) {
throw new RuntimeException('operator_options 泄露了当前角色或部门范围外的账号');
}
}
foreach ($scopedOverview['pools'] as $pool) {
if (!in_array((int) $pool['owner_admin_id'], $visibleIds, true)
&& !in_array((int) $scopedAdmin['id'], array_map('intval', $pool['operator_admin_ids'] ?? []), true)) {
throw new RuntimeException('pools 泄露了当前角色或部门范围外的数据');
}
}
}
}
echo sprintf(
"WECOM_PROMOTION_OVERVIEW_SMOKE_OK configured=%d callback=%d pools=%d links=%d members=%d operators=%d\n",
!empty($overview['config']['ready']) ? 1 : 0,
!empty($overview['config']['callback_ready']) ? 1 : 0,
count($overview['pools']),
count($overview['links']),
count($overview['member_options']),
$enabledOperatorCount
);