87 lines
4.3 KiB
PHP
87 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\adminapi\logic\auth {
|
|
// 用内存权限列表替代真实AuthLogic,确保控制器测试不连接业务数据库。
|
|
class AuthLogic
|
|
{
|
|
public static array $permissions = [];
|
|
public static function getAuthByAdminId(int $adminId): array { return self::$permissions; }
|
|
}
|
|
}
|
|
|
|
namespace app\common\service\qywx {
|
|
// API行为由MockHandler测试;此处只验证控制器权限、HTTP方法及参数边界。
|
|
class QywxPromotionOperatorAccess
|
|
{
|
|
public static function hasBasePagePermission(int $adminId, array $adminInfo): bool
|
|
{
|
|
return !empty($adminInfo['root'])
|
|
|| in_array('firstvisit.wecomPromotion/overview', \app\adminapi\logic\auth\AuthLogic::getAuthByAdminId($adminId), true);
|
|
}
|
|
public static function hasPagePermission(int $adminId, array $adminInfo): bool
|
|
{
|
|
return self::hasBasePagePermission($adminId, $adminInfo);
|
|
}
|
|
}
|
|
class QywxPromotionContactApiService
|
|
{
|
|
public static array $calls = [];
|
|
public static bool $fail = false;
|
|
public function createTag(string $name): array
|
|
{
|
|
self::$calls[] = $name;
|
|
if (self::$fail) { throw new \RuntimeException('模拟企微标签失败'); }
|
|
return ['tag' => ['id' => 'remote_tag', 'name' => $name],
|
|
'group_id' => 'remote_group', 'group_name' => '推广渠道', 'reused' => false];
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
use app\adminapi\controller\firstvisit\WecomPromotionController;
|
|
use app\adminapi\logic\auth\AuthLogic;
|
|
use app\common\service\qywx\QywxPromotionContactApiService;
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
new think\App();
|
|
final class CreateTagControllerFixture extends WecomPromotionController
|
|
{
|
|
public function __construct(think\Request $request, bool $root = false)
|
|
{
|
|
$this->request = $request;
|
|
$this->adminId = 12;
|
|
$this->adminInfo = ['root' => $root ? 1 : 0];
|
|
}
|
|
protected function data($data) { return ['code' => 1, 'data' => $data]; }
|
|
protected function fail(string $msg = 'fail', array $data = [], int $code = 0, int $show = 1)
|
|
{
|
|
return ['code' => $code, 'msg' => $msg, 'data' => $data];
|
|
}
|
|
}
|
|
function tagControllerCheck(bool $ok, string $message): void { if (!$ok) { throw new RuntimeException($message); } }
|
|
$request = static function (string $method = 'POST', mixed $name = '直播'): think\Request {
|
|
return (new think\Request())->setMethod($method)->withPost(['name' => $name, 'group_name' => '不允许客户端改组']);
|
|
};
|
|
$result = (new CreateTagControllerFixture($request()))->createTag();
|
|
tagControllerCheck($result['code'] === 0 && QywxPromotionContactApiService::$calls === [], 'no page permission cannot create');
|
|
AuthLogic::$permissions = ['firstvisit.wecomPromotion/overview'];
|
|
$result = (new CreateTagControllerFixture($request('GET')))->createTag();
|
|
tagControllerCheck($result['code'] === 0 && QywxPromotionContactApiService::$calls === [], 'GET cannot create');
|
|
foreach ([['array_name'], 123, true] as $invalid) {
|
|
$result = (new CreateTagControllerFixture($request('POST', $invalid)))->createTag();
|
|
tagControllerCheck($result['code'] === 0 && QywxPromotionContactApiService::$calls === [], 'non-string name rejected before API');
|
|
}
|
|
$result = (new CreateTagControllerFixture($request()))->createTag();
|
|
tagControllerCheck($result['code'] === 1 && $result['data']['tag']['id'] === 'remote_tag'
|
|
&& $result['data']['group_name'] === '推广渠道' && QywxPromotionContactApiService::$calls === ['直播'], 'page permission and response envelope');
|
|
AuthLogic::$permissions = [];
|
|
$result = (new CreateTagControllerFixture($request('POST', '自定义'), true))->createTag();
|
|
tagControllerCheck($result['code'] === 1 && $result['data']['tag']['name'] === '自定义', 'root permitted through same controller guard');
|
|
QywxPromotionContactApiService::$fail = true;
|
|
$result = (new CreateTagControllerFixture($request(), true))->createTag();
|
|
tagControllerCheck($result['code'] === 0 && $result['msg'] === '模拟企微标签失败', 'upstream failure not reported as success');
|
|
echo "WECOM_PROMOTION_CREATE_TAG_CONTROLLER_OK\n";
|
|
}
|