| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace Tests\Integration;
- use App\Http\Controllers\admin\Agent as AdminAgent;
- use App\Http\Controllers\agent\ReferralController;
- use App\Models\Agent\Agent;
- use App\Services\Agent\AgentDomainService;
- 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;
- /** 代理绑定多个域名,不区分 PC / H5;一个主机名只能属于一个代理。 */
- class AgentMultipleDomainsTest extends TestCase
- {
- use AgentTestEnvironment;
- private function bind(int $agentId, array $params): array
- {
- $request = Request::create('/admin/agent/domains', 'POST', ['id' => $agentId] + $params);
- $request->user = (object) ['id' => 1];
- $this->app->instance('request', $request);
- return (new AdminAgent())->domains($request, new AgentDomainService())->getData(true);
- }
- public function test_agent_binds_a_single_list_of_domains(): void
- {
- $agent = $this->agent();
- $other = $this->agent();
- $data = $this->bind($agent->id, ['domains' => ['https://a.com/#/reg', 'b.com', 'https://A.com/other', 'https://m.a.com']]);
- $this->assertSame(0, $data['code'], (string) $data['msg']);
- $this->assertSame(['https://a.com/#/reg', 'b.com', 'https://m.a.com'], $data['data']['domains'], 'same host kept once');
- $this->assertSame(['domains'], array_keys($data['data']), 'no pc / h5 fields');
- $this->assertSame($agent->id, (new AgentDomainService())->resolve(null, 'https://m.a.com/x')->id);
- $taken = $this->bind($other->id, ['domains' => ['c.com', 'b.com']]);
- $this->assertSame('该域名已绑定其他代理:b.com', $taken['msg']);
- $this->assertSame(['b.com'], $this->bind($agent->id, ['domains' => 'b.com'])['data']['domains'], 'string is one domain; list replaced');
- $this->assertSame(['c.com', 'a.com'], $this->bind($other->id, ['domains' => ['c.com', 'a.com']])['data']['domains'], 'released a.com can move');
- $this->assertNotSame(0, $this->bind($other->id, ['pc' => 'x.com'])['code'], 'old pc / h5 params are not accepted');
- $this->assertSame(['c.com', 'a.com'], $this->bind($other->id, ['domains' => ['c.com', 'a.com']])['data']['domains']);
- }
- public function test_equity_domain_does_not_attach_a_member(): void
- {
- $equity = $this->agent(Agent::TYPE_EQUITY);
- $this->assertSame(0, $this->bind($equity->id, ['domains' => ['https://equity.example']])['code']);
- $memberId = DB::table('users')->insertGetId(['member_id' => '1001', 'username' => 'member', 'created_at' => now()]);
- $controller = new ReferralController();
- $domains = new AgentDomainService();
- foreach ([['domain' => 'https://equity.example'], ['agent_code' => $equity->invitation_code]] as $params) {
- $resolve = Request::create('/agent/referral/resolve', 'GET', $params);
- $this->assertNotSame(0, $controller->resolve($resolve, $domains)->getData(true)['code']);
- $request = Request::create('/agent/referral/bind', 'POST', $params);
- $request->user = (object) ['id' => $memberId];
- $bound = $controller->bind($request, $domains)->getData(true);
- $this->assertSame(0, $bound['code'], (string) $bound['msg']);
- $this->assertNull($bound['data']['agent_id']);
- $this->assertNull(DB::table('users')->where('id', $memberId)->value('agent_id'));
- }
- }
- public function test_joint_referral_respects_the_fixed_member_inviter(): void
- {
- Schema::table('users', function (Blueprint $table) {
- $table->integer('from')->default(1);
- $table->string('agent_user_code')->nullable();
- $table->string('user_code')->nullable();
- });
- Schema::create('share_members', function (Blueprint $table) {
- $table->unsignedBigInteger('user_id')->primary();
- $table->unsignedBigInteger('parent_user_id')->nullable();
- });
- DB::table('share_system_state')->insert(['key' => 'relations_lock', 'value' => '1']);
- $joint = $this->agent();
- $equity = $this->agent(Agent::TYPE_EQUITY);
- $parentId = DB::table('users')->insertGetId(['member_id' => '1001', 'username' => 'parent', 'user_code' => 'C1001']);
- $invitedId = DB::table('users')->insertGetId(['member_id' => '1002', 'username' => 'invited', 'created_at' => now()]);
- $unboundId = DB::table('users')->insertGetId(['member_id' => '1003', 'username' => 'unbound',
- 'agent_id' => $equity->id, 'agent_user_code' => 'GONE', 'created_at' => now()]);
- $legacyInvitedId = DB::table('users')->insertGetId(['member_id' => '1004', 'username' => 'legacy',
- 'agent_user_code' => "\tc1001\t", 'created_at' => now()]);
- DB::table('share_members')->insert(['user_id' => $invitedId, 'parent_user_id' => $parentId]);
- $controller = new ReferralController();
- $domains = new AgentDomainService();
- $request = Request::create('/agent/referral/bind', 'POST', ['agent_code' => $joint->invitation_code]);
- foreach ([$invitedId, $legacyInvitedId] as $memberId) {
- $request->user = (object) ['id' => $memberId];
- $body = $controller->bind($request, $domains)->getData(true);
- $this->assertSame(0, $body['code'], (string) $body['msg']);
- $this->assertNull($body['data']['agent_id']);
- $this->assertNull(DB::table('users')->where('id', $memberId)->value('agent_id'));
- }
- $this->assertSame($parentId, DB::table('share_members')->where('user_id', $invitedId)->value('parent_user_id'));
- $this->assertSame("\tc1001\t", DB::table('users')->where('id', $legacyInvitedId)->value('agent_user_code'));
- $request->user = (object) ['id' => $unboundId];
- $body = $controller->bind($request, $domains)->getData(true);
- $this->assertSame(0, $body['code'], (string) $body['msg']);
- $this->assertSame($joint->id, $body['data']['agent_id']);
- $this->assertSame($joint->id, DB::table('users')->where('id', $unboundId)->value('agent_id'));
- $this->assertNull(DB::table('users')->where('id', $unboundId)->value('agent_user_code'));
- $this->assertSame($body['data'], $controller->bind($request, $domains)->getData(true)['data']);
- }
- public function test_migration_merges_old_pc_and_h5_rows(): void
- {
- $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_11_09_140000_merge_agent_domain_types.php';
- $migration->down();
- $agent = $this->agent();
- \Illuminate\Support\Facades\DB::table('agent_domains')->insert([
- ['agent_id' => $agent->id, 'type' => 'pc', 'domain' => 'https://a.com', 'host' => 'a.com', 'status' => 1],
- ['agent_id' => $agent->id, 'type' => 'h5', 'domain' => 'https://a.com/m', 'host' => 'a.com', 'status' => 1],
- ['agent_id' => $agent->id, 'type' => 'h5', 'domain' => 'https://m.b.com', 'host' => 'm.b.com', 'status' => 1],
- ]);
- $migration->up();
- $this->assertSame(['https://a.com', 'https://m.b.com'], (new AgentDomainService())->format($agent)['domains']);
- $this->assertSame([''], \Illuminate\Support\Facades\DB::table('agent_domains')->distinct()->pluck('type')->all());
- }
- }
|