| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?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)或上级会员(会员 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', 'superior_id' => (string) $agent->id]);
- $this->assertSame(0, $data['code'], (string) $data['msg']);
- $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', 'superior_id' => (string) $agent->id])['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', 'superior_id' => '9999'])['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']);
- Agent::query()->whereKey($agent->id)->update(['status' => Agent::STATUS_DISABLED]);
- $this->assertSame('代理已禁用', $this->call(['member_id' => '1002', 'type' => 'agent', 'superior_id' => (string) $agent->id])['msg']);
- $this->assertNull(DB::table('users')->where('id', $b)->value('agent_id'));
- }
- }
|