Files
zyt/server/tests/WecomPromotionOverviewSmokeTest.php
T
2026-08-25 15:05:49 +08:00

72 lines
3.0 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', '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 缺少树形下拉展示部门');
}
}
$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 必须是部门树数组');
}
$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) {
foreach ($scopedOverview['member_options'] as $member) {
if (!in_array((int) $member['id'], $visibleIds, true)) {
throw new RuntimeException('member_options 泄露了当前角色或部门范围外的成员');
}
}
foreach ($scopedOverview['pools'] as $pool) {
if (!in_array((int) $pool['owner_admin_id'], $visibleIds, true)) {
throw new RuntimeException('pools 泄露了当前角色或部门范围外的数据');
}
}
}
}
echo sprintf(
"WECOM_PROMOTION_OVERVIEW_SMOKE_OK configured=%d callback=%d pools=%d links=%d members=%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'])
);