resolveApiUser(); $leadId = $this->createLeadForUser($user); $this->authenticateApiUser($user); $fieldsResponse = $this->get('/api/leads/fields'); $fieldsResponse->assertStatus(200); $this->assertRulesPayload($fieldsResponse->getContent()); $detailResponse = $this->get("/api/leads/get/{$leadId}"); $detailResponse->assertStatus(200); $this->assertRulesPayload($detailResponse->getContent()); } public function test_contact_endpoints_include_rules_section() { $user = $this->resolveApiUser(); $contactId = $this->createContactForUser($user); $this->authenticateApiUser($user); $fieldsResponse = $this->get('/api/contacts/fields'); $fieldsResponse->assertStatus(200); $this->assertRulesPayload($fieldsResponse->getContent()); $detailResponse = $this->get("/api/contacts/get/{$contactId}"); $detailResponse->assertStatus(200); $this->assertRulesPayload($detailResponse->getContent()); } private function assertRulesPayload(string $content): void { $payload = json_decode($content, true); $data = $payload['data'] ?? null; $this->assertTrue(is_array($data)); $this->assertArrayHasKey('rules', $data); $this->assertTrue(is_array($data['rules'])); if (!empty($data['rules'])) { $rule = $data['rules'][0]; $this->assertArrayHasKey('field', $rule); $this->assertArrayHasKey('field_label', $rule); $this->assertArrayHasKey('when_field', $rule); $this->assertArrayHasKey('when_field_label', $rule); $this->assertArrayHasKey('conditions', $rule); $this->assertTrue(is_array($rule['conditions'])); } } private function createLeadForUser(User $user): int { $payload = [ 'first_name' => 'API', 'last_name' => 'Rules', 'full_name' => 'API Rules', 'created_at' => now(), 'updated_at' => now(), ]; if (Schema::hasColumn('leads', 'company_id')) { $payload['company_id'] = $user->company_id ?: $this->resolveCompanyId(); } if (Schema::hasColumn('leads', 'created_by')) { $payload['created_by'] = $user->id; } if (Schema::hasColumn('leads', 'owned_by_type')) { $payload['owned_by_type'] = \Modules\Platform\User\Entities\User::class; } if (Schema::hasColumn('leads', 'owned_by_id')) { $payload['owned_by_id'] = $user->id; } return (int) DB::table('leads')->insertGetId($payload); } private function createContactForUser(User $user): int { $payload = [ 'first_name' => 'API', 'last_name' => 'Rules', 'full_name' => 'API Rules', 'created_at' => now(), 'updated_at' => now(), ]; if (Schema::hasColumn('contacts', 'company_id')) { $payload['company_id'] = $user->company_id ?: $this->resolveCompanyId(); } if (Schema::hasColumn('contacts', 'created_by')) { $payload['created_by'] = $user->id; } if (Schema::hasColumn('contacts', 'owned_by_type')) { $payload['owned_by_type'] = \Modules\Platform\User\Entities\User::class; } if (Schema::hasColumn('contacts', 'owned_by_id')) { $payload['owned_by_id'] = $user->id; } return (int) DB::table('contacts')->insertGetId($payload); } private function resolveCompanyId() { if (!Schema::hasTable('bap_companies')) { return null; } $companyId = DB::table('bap_companies')->orderBy('id')->value('id'); if ($companyId !== null) { return $companyId; } return DB::table('bap_companies')->insertGetId([ 'name' => 'API Test Company', 'is_enabled' => 1, 'created_at' => now(), 'updated_at' => now(), ]); } private function resolveApiUser(): User { $user = User::query()->orderBy('id')->first(); if ($user === null) { $payload = [ 'name' => 'API Rules User', 'email' => 'api-rules-' . uniqid() . '@example.com', 'password' => bcrypt('secret'), 'created_at' => now(), 'updated_at' => now(), ]; if (Schema::hasColumn('users', 'company_id')) { $payload['company_id'] = $this->resolveCompanyId(); } if (Schema::hasColumn('users', 'is_active')) { $payload['is_active'] = 1; } $userId = DB::table('users')->insertGetId($payload); $user = User::query()->findOrFail($userId); } foreach (['leads.browse', 'contacts.browse'] as $permissionName) { Permission::findOrCreate($permissionName); } $user->givePermissionTo('leads.browse'); $user->givePermissionTo('contacts.browse'); return $user; } private function authenticateApiUser(User $user): void { $token = \JWTAuth::fromUser($user); if (Schema::hasColumn('users', 'jwt_token')) { DB::table('users')->where('id', $user->id)->update(['jwt_token' => $token]); $user->jwt_token = $token; } $this->withHeader('Authorization', 'Bearer ' . $token); } }