null]); $first = Tenant::factory()->create([ 'bank_iban' => 'DE02120300000000202051', 'operating_mode' => Settings::MODE_LIVE, 'owner_name' => 'Brigitte Beispiel', ]); $second = Tenant::factory()->create([ 'bank_iban' => 'DE02500105170137075030', 'operating_mode' => Settings::MODE_TEST, 'test_recipient' => 'test@zweiter.example', 'owner_name' => 'Zweiter Vermieter', ]); $context = app(TenantContext::class); $context->set($first); Content::save(['legal.imprint' => 'Impressum des ersten Vermieters']); $this->assertSame('Impressum des ersten Vermieters', Content::get('legal.imprint')); $this->assertSame('DE02120300000000202051', config('fewo.bank.iban')); $this->assertSame('Brigitte Beispiel', config('fewo.owner.name')); $this->assertTrue(Settings::isLive()); $this->assertNull(Settings::mailRedirect()); $context->set($second); $this->assertNull(Content::get('legal.imprint')); Content::save(['legal.imprint' => 'Impressum des zweiten Vermieters']); $this->assertSame('DE02500105170137075030', config('fewo.bank.iban')); $this->assertSame('Zweiter Vermieter', config('fewo.owner.name')); $this->assertFalse(Settings::isLive()); $this->assertSame('test@zweiter.example', Settings::mailRedirect()); $context->set($first); $this->assertSame('Impressum des ersten Vermieters', Content::get('legal.imprint')); } public function test_without_a_landlord_the_configuration_of_the_installation_applies(): void { config(['fewo.owner.name' => 'Aus der Konfiguration']); $first = Tenant::factory()->create(['owner_name' => 'Brigitte Beispiel']); Tenant::factory()->create(); $context = app(TenantContext::class); $context->set($first); $this->assertSame('Brigitte Beispiel', config('fewo.owner.name')); $context->forget(); $this->assertSame('Aus der Konfiguration', config('fewo.owner.name')); $this->assertNull(Settings::tenant()); } public function test_the_operator_switches_services_per_landlord_and_an_empty_key_is_allowed(): void { $tenant = Tenant::factory()->create(); $tenant->services()->create(['service' => TenantServiceKey::PortalSync->value, 'is_enabled' => true, 'api_key' => null]); $tenant->services()->create(['service' => TenantServiceKey::AiOffers->value, 'is_enabled' => true, 'api_key' => 'sk-beispiel']); $sync = $tenant->services()->where('service', TenantServiceKey::PortalSync->value)->sole(); $ai = $tenant->services()->where('service', TenantServiceKey::AiOffers->value)->sole(); $this->assertTrue($sync->is_enabled); $this->assertNull($sync->api_key); $this->assertSame('sk-beispiel', $ai->api_key); $this->assertSame(0, Tenant::factory()->create()->services()->count()); } public function test_an_own_service_key_is_stored_encrypted(): void { $tenant = Tenant::factory()->create(); $service = $tenant->services()->create(['service' => TenantServiceKey::AiMarketing->value, 'is_enabled' => true, 'api_key' => 'sk-geheim']); $this->assertNotSame('sk-geheim', DB::table('tenant_services')->where('id', $service->id)->value('api_key')); $this->assertSame('sk-geheim', $service->fresh()->api_key); } public function test_the_operator_switches_a_service_on_the_tenant_form(): void { $tenant = Tenant::factory()->create(); app(TenantContext::class)->set($tenant); $operator = User::factory()->create(); $operator->forceFill(['is_operator' => true])->saveQuietly(); $this->actingAs($operator); Filament::setCurrentPanel('operator'); Livewire::test(EditTenant::class, ['record' => $tenant->getRouteKey()]) ->fillForm([ 'services.'.TenantServiceKey::PortalSync->value.'.is_enabled' => true, 'services.'.TenantServiceKey::AiOffers->value.'.is_enabled' => true, 'services.'.TenantServiceKey::AiOffers->value.'.api_key' => 'sk-beispiel', ]) ->call('save') ->assertHasNoErrors(); $this->assertTrue($tenant->services()->where('service', TenantServiceKey::PortalSync->value)->sole()->is_enabled); $this->assertNull($tenant->services()->where('service', TenantServiceKey::PortalSync->value)->sole()->api_key); $this->assertSame('sk-beispiel', $tenant->services()->where('service', TenantServiceKey::AiOffers->value)->sole()->api_key); $this->assertFalse($tenant->services()->where('service', TenantServiceKey::AiGuestMail->value)->sole()->is_enabled); } /** * The data a new landlord starts with comes from the installation, never from the landlord that happens * to be current – otherwise the migration would copy one landlord's address into the next one. */ public function test_the_defaults_of_a_new_landlord_come_from_the_configuration_not_from_the_current_one(): void { config(['fewo.owner.name' => 'Aus der Konfiguration', 'fewo.owner.email' => 'konfiguration@example.com']); $current = Tenant::factory()->create(['owner_name' => 'Erster Vermieter', 'contact_email' => 'erster@example.com']); app(TenantContext::class)->set($current); $this->assertSame('Erster Vermieter', config('fewo.owner.name')); $next = Tenant::factory()->create(); $this->assertSame('Aus der Konfiguration', $next->owner_name); $this->assertSame('konfiguration@example.com', $next->contact_email); } public function test_the_landlord_of_the_context_decides_the_sender(): void { $tenant = Tenant::factory()->create(['sender_email' => 'versand@vermieter.example', 'sender_name' => 'Vermieter']); app(TenantContext::class)->set($tenant); app(TenantConfig::class)->refresh(); $this->assertSame(['versand@vermieter.example', 'Vermieter'], [config('mail.from.address'), config('mail.from.name')]); } }