*/ class UserFactory extends Factory { /** * The current password being used by the factory. */ protected static ?string $password; /** * Define the model's default state. * * @return array */ public function definition(): array { return [ 'company_id' => fn (): int => $this->licensedCompany()->id, 'name' => fake()->name(), 'email' => fake()->unique()->safeEmail(), 'email_verified_at' => now(), 'password' => static::$password ??= Hash::make('password'), 'remember_token' => Str::random(10), 'two_factor_secret' => null, 'two_factor_recovery_codes' => null, 'two_factor_confirmed_at' => null, 'is_platform_admin' => false, 'is_company_admin' => true, 'status' => 'active', ]; } /** * Indicate that the model's email address should be unverified. */ public function unverified(): static { return $this->state(fn (array $attributes) => [ 'email_verified_at' => null, ]); } /** * Indicate that the model has two-factor authentication configured. */ public function withTwoFactor(): static { return $this->state(fn (array $attributes) => [ 'two_factor_secret' => encrypt('secret'), 'two_factor_recovery_codes' => encrypt(json_encode(['recovery-code-1'])), 'two_factor_confirmed_at' => now(), ]); } /** * Use the MPBA system tenant and platform administrator flags. */ public function mpbaPlatformAdmin(): static { return $this->state(fn (array $attributes) => [ 'company_id' => $this->systemCompany()->id, 'is_platform_admin' => true, 'is_company_admin' => true, 'status' => 'active', ]); } /** * Create or reuse a licensed tenant company for ordinary user tests. */ private function licensedCompany(): Company { $company = Company::query()->where('slug', 'test-company')->first(); if (! $company) { $company = Company::query()->create([ 'name' => 'Test Company', 'slug' => 'test-company', 'status' => 'active', 'timezone' => 'Europe/London', 'is_system_company' => false, 'billing_email' => 'billing@test-company.example', ]); } if (! $company->hasActiveLicense()) { LicenseKey::issueFor($company, 'Automated Test Licence', 'enterprise', 250); } return $company; } /** * Create or reuse the MPBA system tenant. */ private function systemCompany(): Company { return Company::query()->firstOrCreate( ['slug' => 'mpba-limited'], [ 'name' => 'MPBA Limited', 'status' => 'active', 'timezone' => 'Europe/London', 'is_system_company' => true, 'billing_email' => 'mark.gregory@mpba.co.uk', ], ); } }