assertTrue(Schema::hasColumns('users', [ 'rol_codigo', 'tenant_codigo', ])); } public function test_a_new_user_has_the_user_role_and_no_tenant_by_default(): void { $user = User::factory()->create(); $this->assertSame('user', $user->rol_codigo); $this->assertNull($user->tenant_codigo); $this->assertSame('user', $user->role->codigo); $this->assertNull($user->tenant); } public function test_a_tenant_admin_belongs_to_its_role_and_tenant(): void { $tenant = $this->createTenant(); $user = User::factory()->create([ 'rol_codigo' => 'tenant_admin', 'tenant_codigo' => $tenant->codigo, ]); $this->assertSame('tenant_admin', $user->role->codigo); $this->assertTrue($user->tenant->is($tenant)); $this->assertTrue( Role::query() ->where('codigo', 'tenant_admin') ->firstOrFail() ->users ->contains($user) ); } public function test_only_a_tenant_admin_can_have_a_tenant_code(): void { $tenant = $this->createTenant(); $this->expectException(LogicException::class); User::factory()->create([ 'rol_codigo' => 'user', 'tenant_codigo' => $tenant->codigo, ]); } public function test_a_tenant_admin_must_have_a_tenant_code(): void { $this->expectException(LogicException::class); User::factory()->create([ 'rol_codigo' => 'tenant_admin', ]); } private function createTenant(): Tenant { $headerLogo = $this->createAttachment('header.png'); $footerLogo = $this->createAttachment('footer.png'); return Tenant::query()->create([ 'codigo' => 'acme', 'nombre' => 'Acme', 'dominio' => 'acme.test', 'primary_color' => '#111111', 'secondary_color' => '#222222', 'danger_color' => '#333333', 'success_color' => '#444444', 'header_bg_color' => '#ffffff', 'footer_bg_color' => '#ffffff', 'header_logo_id' => $headerLogo->id, 'footer_logo_id' => $footerLogo->id, ]); } private function createAttachment(string $filename): Attachment { return Attachment::query()->create([ 'path' => "test/{$filename}", 'filename' => $filename, 'type' => AttachmentType::Image, 'mime_type' => 'image/png', ]); } }