205 lines
8.8 KiB
PHP
205 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Enums\UserRole;
|
|
use App\Filament\Resources\Pages\Pages\EditPage;
|
|
use App\Models\Page;
|
|
use App\Models\Property;
|
|
use App\Models\Site;
|
|
use App\Models\User;
|
|
use App\Tenancy\TenantContext;
|
|
use App\Themes\SectionRegistry;
|
|
use App\Themes\Theme;
|
|
use App\Themes\ThemeFactory;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\View;
|
|
use Livewire\Livewire;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use RuntimeException;
|
|
use Tests\TestCase;
|
|
|
|
class SectionPropertyTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_sections_use_the_selected_property_and_return_to_the_primary_property(): void
|
|
{
|
|
$this->withoutVite();
|
|
$site = Site::factory()->create(['domain' => 'wohnungen.test', 'is_default' => true]);
|
|
Property::factory()->for($site)->create(['name' => 'Hauptwohnung', 'size_sqm' => 53]);
|
|
$second = Property::factory()->for($site)->create(['name' => 'Bergwohnung', 'size_sqm' => 87]);
|
|
Page::factory()->for($site)->create([
|
|
'slug' => Page::HOME,
|
|
'sections' => [
|
|
['type' => 'facts', 'data' => ['heading' => 'Erste Wohnung']],
|
|
['type' => 'facts', 'data' => ['heading' => 'Zweite Wohnung', 'property_id' => (string) $second->id]],
|
|
['type' => 'facts', 'data' => ['heading' => 'Wieder Hauptwohnung', 'property_id' => null]],
|
|
['type' => 'booking', 'data' => ['property_id' => $second->id]],
|
|
],
|
|
]);
|
|
|
|
$this->get('http://wohnungen.test/')
|
|
->assertOk()
|
|
->assertSeeInOrder(['Erste Wohnung', '53 m²', 'Zweite Wohnung', '87 m²', 'Wieder Hauptwohnung', '53 m²'])
|
|
->assertSee('"propertyId":'.$second->id, false);
|
|
|
|
$this->assertSame('Hauptwohnung', app(Theme::class)->property()->name);
|
|
}
|
|
|
|
public function test_deferred_views_and_partials_see_the_selected_theme_property(): void
|
|
{
|
|
$site = Site::factory()->create();
|
|
$primary = Property::factory()->for($site)->create(['name' => 'Hauptwohnung']);
|
|
$second = Property::factory()->for($site)->create(['name' => 'Bergwohnung']);
|
|
$theme = app(ThemeFactory::class)->forSite($site)->activate();
|
|
app()->instance(Theme::class, $theme);
|
|
View::composer('theme::sections.facts', function ($view): void {
|
|
$view->with('heading', $view->getData()['theme']->property()->name.' / '.app(Theme::class)->property()->name);
|
|
});
|
|
|
|
$sections = app(SectionRegistry::class)->render($theme, [
|
|
['type' => 'facts', 'data' => ['property_id' => $second->id]],
|
|
['type' => 'facts', 'data' => []],
|
|
]);
|
|
|
|
$this->assertSame($primary->id, $theme->property()->id);
|
|
$this->assertStringContainsString('Bergwohnung / Bergwohnung', $sections[0]->render());
|
|
$this->assertStringContainsString('Hauptwohnung / Hauptwohnung', $sections[1]->render());
|
|
$this->assertSame($primary->id, $theme->property()->id);
|
|
}
|
|
|
|
public function test_nested_property_contexts_restore_the_unresolved_primary_cache_after_an_exception(): void
|
|
{
|
|
$site = Site::factory()->create();
|
|
$primary = Property::factory()->for($site)->create();
|
|
$second = Property::factory()->for($site)->create();
|
|
$theme = app(ThemeFactory::class)->forSite($site);
|
|
|
|
try {
|
|
$theme->runForProperty($second, function () use ($theme, $primary, $second): void {
|
|
$this->assertSame($second->id, $theme->property()->id);
|
|
$theme->runForProperty($primary, function () use ($theme, $primary): void {
|
|
$this->assertSame($primary->id, $theme->property()->id);
|
|
});
|
|
$this->assertSame($second->id, $theme->property()->id);
|
|
|
|
throw new RuntimeException('Abbruch');
|
|
});
|
|
$this->fail('The callback must throw.');
|
|
} catch (RuntimeException $exception) {
|
|
$this->assertSame('Abbruch', $exception->getMessage());
|
|
}
|
|
|
|
$primary->update(['is_active' => false]);
|
|
$this->assertSame($second->id, $theme->property()->id);
|
|
}
|
|
|
|
public function test_a_failing_deferred_view_restores_the_primary_property(): void
|
|
{
|
|
$site = Site::factory()->create();
|
|
$primary = Property::factory()->for($site)->create();
|
|
$second = Property::factory()->for($site)->create();
|
|
$theme = app(ThemeFactory::class)->forSite($site)->activate();
|
|
$sections = app(SectionRegistry::class)->render($theme, [
|
|
['type' => 'facts', 'data' => ['property_id' => $second->id]],
|
|
]);
|
|
View::composer('theme::sections.facts', function () use ($theme, $second): void {
|
|
$this->assertSame($second->id, $theme->property()->id);
|
|
|
|
throw new RuntimeException('Vorlage abgebrochen');
|
|
});
|
|
|
|
try {
|
|
$sections[0]->render();
|
|
$this->fail('The view must throw.');
|
|
} catch (RuntimeException $exception) {
|
|
$this->assertSame('Vorlage abgebrochen', $exception->getMessage());
|
|
}
|
|
|
|
$this->assertSame($primary->id, $theme->property()->id);
|
|
}
|
|
|
|
/** @return array<string, array{string}> */
|
|
public static function invalidPropertyProvider(): array
|
|
{
|
|
return [
|
|
'other website of same tenant' => ['other_site'],
|
|
'other tenant' => ['other_tenant'],
|
|
'inactive property' => ['inactive'],
|
|
'deleted property' => ['deleted'],
|
|
];
|
|
}
|
|
|
|
#[DataProvider('invalidPropertyProvider')]
|
|
public function test_invalid_property_assignments_are_skipped(string $kind): void
|
|
{
|
|
$site = Site::factory()->create();
|
|
Property::factory()->for($site)->create();
|
|
$otherSite = Site::factory()->create(['tenant_id' => $site->tenant_id]);
|
|
$property = match ($kind) {
|
|
'other_site' => Property::factory()->for($otherSite)->create(),
|
|
'other_tenant' => Property::factory()->for(Site::factory())->create(),
|
|
default => Property::factory()->for($site)->create(['is_active' => false]),
|
|
};
|
|
if ($kind === 'deleted') {
|
|
$property->delete();
|
|
}
|
|
app(TenantContext::class)->set($site->tenant);
|
|
$theme = app(ThemeFactory::class)->forSite($site)->activate();
|
|
|
|
$sections = app(SectionRegistry::class)->render($theme, [
|
|
['type' => 'facts', 'data' => ['property_id' => $property->id]],
|
|
['type' => 'facts', 'data' => ['heading' => 'Hauptwohnung bleibt sichtbar']],
|
|
]);
|
|
|
|
$this->assertCount(1, $sections);
|
|
$this->assertStringContainsString('Hauptwohnung bleibt sichtbar', $sections[0]->render());
|
|
}
|
|
|
|
public function test_editor_saves_a_property_of_the_pages_website(): void
|
|
{
|
|
$site = Site::factory()->create();
|
|
$property = Property::factory()->for($site)->create();
|
|
$page = Page::factory()->for($site)->create();
|
|
$user = User::factory()->role(UserRole::Content)->create();
|
|
$user->tenants()->syncWithoutDetaching([$site->tenant_id => ['role' => UserRole::Content->value]]);
|
|
app(TenantContext::class)->set($site->tenant);
|
|
$this->actingAs($user);
|
|
|
|
Livewire::test(EditPage::class, ['record' => $page->getRouteKey()])
|
|
->fillForm(['sections' => [['type' => 'facts', 'data' => ['property_id' => $property->id]]]])
|
|
->call('save')
|
|
->assertHasNoFormErrors();
|
|
|
|
$this->assertSame($property->id, (int) $page->fresh()->sections[0]['data']['property_id']);
|
|
}
|
|
|
|
#[DataProvider('invalidPropertyProvider')]
|
|
public function test_editor_rejects_properties_outside_the_available_selection(string $kind): void
|
|
{
|
|
$site = Site::factory()->create();
|
|
$page = Page::factory()->for($site)->create(['sections' => []]);
|
|
$otherSite = Site::factory()->create(['tenant_id' => $site->tenant_id]);
|
|
$property = match ($kind) {
|
|
'other_site' => Property::factory()->for($otherSite)->create(),
|
|
'other_tenant' => Property::factory()->for(Site::factory())->create(),
|
|
default => Property::factory()->for($site)->create(['is_active' => false]),
|
|
};
|
|
if ($kind === 'deleted') {
|
|
$property->delete();
|
|
}
|
|
$user = User::factory()->role(UserRole::Content)->create();
|
|
$user->tenants()->syncWithoutDetaching([$site->tenant_id => ['role' => UserRole::Content->value]]);
|
|
app(TenantContext::class)->set($site->tenant);
|
|
$this->actingAs($user);
|
|
|
|
Livewire::test(EditPage::class, ['record' => $page->getRouteKey()])
|
|
->fillForm(['sections' => [['type' => 'facts', 'data' => ['property_id' => $property->id]]]])
|
|
->call('save')
|
|
->assertHasFormErrors();
|
|
|
|
$this->assertSame([], $page->fresh()->sections);
|
|
}
|
|
}
|