| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace Tests\Integration;
- use App\Http\Controllers\admin\User as AdminUser;
- use App\Models\Agent\Agent;
- use App\Services\MemberSuperiorService;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Schema;
- use PHPUnit\Framework\TestCase;
- use Tests\Support\AgentTestEnvironment;
- /** 后台会员列表:无上级的会员可按账号补绑合营代理,或按会员 ID 补绑上级会员。 */
- class AdminBindSuperiorTest extends TestCase
- {
- use AgentTestEnvironment { setUp as private agentSetUp; }
- protected function setUp(): void
- {
- $this->agentSetUp();
- Schema::table('users', function (Blueprint $t) {
- $t->integer('from')->default(1); $t->string('account')->default('');
- $t->string('user_code')->nullable(); $t->string('agent_user_code')->nullable();
- });
- Schema::create('share_members', function (Blueprint $t) {
- $t->unsignedBigInteger('user_id')->primary(); $t->unsignedBigInteger('parent_user_id')->nullable();
- $t->string('invitation_code', 100)->default(''); $t->string('binding_status', 24)->default('root');
- $t->unsignedInteger('level')->default(1); $t->dateTime('bound_at'); $t->timestamps();
- });
- Schema::create('share_tree', function (Blueprint $t) {
- $t->unsignedBigInteger('ancestor_user_id'); $t->unsignedBigInteger('descendant_user_id');
- $t->unsignedInteger('depth'); $t->dateTime('bound_at')->default('1970-01-01 00:00:00');
- $t->primary(['ancestor_user_id', 'descendant_user_id']);
- });
- Schema::create('share_accounts', function (Blueprint $t) {
- $t->unsignedBigInteger('user_id')->primary(); $t->timestamps();
- });
- DB::table('share_system_state')->insert(['key' => 'relations_lock', 'value' => '1']);
- }
- private function member(string $memberId, array $extra = []): int
- {
- $id = DB::table('users')->insertGetId($extra + ['member_id' => $memberId, 'username' => $memberId, 'account' => $memberId,
- 'user_code' => 'C' . $memberId]);
- if (($extra['from'] ?? 1) !== 2) {
- DB::table('share_members')->insert(['user_id' => $id, 'invitation_code' => 'C' . $memberId, 'bound_at' => '2026-09-01 00:00:00']);
- DB::table('share_tree')->insert(['ancestor_user_id' => $id, 'descendant_user_id' => $id, 'depth' => 0]);
- }
- return $id;
- }
- private function call(array $params): array
- {
- $request = Request::create('/admin/user/bindSuperior', 'POST', $params);
- $request->user = (object) ['id' => 1];
- $this->app->instance('request', $request);
- return (new AdminUser())->bindSuperior(new MemberSuperiorService())->getData(true);
- }
- public function test_binds_agent_or_member_only_when_no_superior(): void
- {
- $agent = $this->agent();
- $this->member('1001');
- $b = $this->member('1002');
- $c = $this->member('1003');
- $this->member('1004', ['from' => 2]);
- $data = $this->call(['member_id' => '1001', 'type' => 'agent', 'agent_username' => ' ' . $agent->username . ' ']);
- $this->assertSame(0, $data['code'], (string) $data['msg']);
- $this->assertSame($agent->id, $data['data']['agent_id']);
- $this->assertSame($agent->id, (int) DB::table('users')->where('member_id', '1001')->value('agent_id'));
- $this->assertSame('会员已有上级,不能修改', $this->call(['member_id' => '1001', 'type' => 'member', 'superior_id' => '1002'])['msg']);
- $data = $this->call(['member_id' => '1003', 'type' => 'member', 'superior_id' => '1002']);
- $this->assertSame(0, $data['code'], (string) $data['msg']);
- $this->assertSame('C1002', DB::table('users')->where('id', $c)->value('agent_user_code'));
- $this->assertSame($b, (int) DB::table('share_members')->where('user_id', $c)->value('parent_user_id'));
- $this->assertTrue(DB::table('share_tree')->where(['ancestor_user_id' => $b, 'descendant_user_id' => $c, 'depth' => 1])->exists());
- $this->assertSame('会员已有上级,不能修改', $this->call(['member_id' => '1003', 'type' => 'agent', 'agent_username' => $agent->username])['msg']);
- $this->assertSame('不能绑定自己或自己的下级', $this->call(['member_id' => '1002', 'type' => 'member', 'superior_id' => '1003'])['msg'], 'no cycle');
- $this->assertSame('不能绑定自己', $this->call(['member_id' => '1002', 'type' => 'member', 'superior_id' => '1002'])['msg']);
- $this->assertSame('上级会员不存在', $this->call(['member_id' => '1002', 'type' => 'member', 'superior_id' => '9999'])['msg']);
- $this->assertSame('代理不存在', $this->call(['member_id' => '1002', 'type' => 'agent', 'agent_username' => 'unknown-agent'])['msg']);
- $this->assertSame('试玩会员不能作为上级', $this->call(['member_id' => '1002', 'type' => 'member', 'superior_id' => '1004'])['msg']);
- $this->assertSame('试玩会员不能绑定上级', $this->call(['member_id' => '1004', 'type' => 'member', 'superior_id' => '1002'])['msg']);
- $this->assertNotSame(0, $this->call(['member_id' => '1002', 'type' => 'other', 'superior_id' => '1'])['code']);
- $equity = $this->agent(Agent::TYPE_EQUITY);
- $this->assertSame('只能绑定合营代理', $this->call(['member_id' => '1002', 'type' => 'agent', 'agent_username' => $equity->username])['msg']);
- $this->member('1005', ['agent_id' => $equity->id]);
- $this->member('1006', ['agent_id' => $equity->id, 'agent_user_code' => 'GONE']);
- $data = $this->call(['member_id' => '1005', 'type' => 'agent', 'agent_username' => $agent->username]);
- $this->assertSame(0, $data['code'], (string) $data['msg']);
- $this->assertSame($agent->id, (int) DB::table('users')->where('member_id', '1005')->value('agent_id'));
- $data = $this->call(['member_id' => '1006', 'type' => 'member', 'superior_id' => '1002']);
- $this->assertSame(0, $data['code'], (string) $data['msg']);
- $this->assertSame('C1002', DB::table('users')->where('member_id', '1006')->value('agent_user_code'));
- $this->assertNull(DB::table('users')->where('member_id', '1006')->value('agent_id'));
- Agent::query()->whereKey($agent->id)->update(['status' => Agent::STATUS_DISABLED]);
- $this->assertSame('代理已禁用', $this->call(['member_id' => '1002', 'type' => 'agent', 'agent_username' => $agent->username])['msg']);
- $this->assertNull(DB::table('users')->where('id', $b)->value('agent_id'));
- }
- public function test_rejects_mixed_target_fields_and_never_falls_back_to_other_identifiers(): void
- {
- $agent = $this->agent();
- $member = $this->member('1001');
- $this->member('1002', ['account' => 'inviter-account']);
- foreach ([
- ['type' => 'agent', 'superior_id' => (string) $agent->id],
- ['type' => 'agent', 'agent_username' => $agent->username, 'superior_id' => '1002'],
- ['type' => 'member', 'agent_username' => $agent->username],
- ['type' => 'member', 'superior_id' => '1002', 'agent_username' => $agent->username],
- ] as $params) {
- $this->assertNotSame(0, $this->call($params + ['member_id' => '1001'])['code']);
- }
- $this->assertSame('代理不存在', $this->call([
- 'member_id' => '1001', 'type' => 'agent', 'agent_username' => (string) $agent->id,
- ])['msg']);
- $this->assertSame('上级会员不存在', $this->call([
- 'member_id' => '1001', 'type' => 'member', 'superior_id' => 'inviter-account',
- ])['msg']);
- $this->assertNull(DB::table('users')->where('id', $member)->value('agent_id'));
- $this->assertNull(DB::table('share_members')->where('user_id', $member)->value('parent_user_id'));
- }
- }
|