feat(menu): implement TenantMenuController, StoreTenantMenuRequest, and TenantMenuService for managing tenant-specific menus
This commit is contained in:
130
tests/Feature/Menu/TenantMenuControllerTest.php
Normal file
130
tests/Feature/Menu/TenantMenuControllerTest.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Menu;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Menu\Models\Menu;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TenantMenuControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_it_validates_static_content_with_the_menu_schema(): void
|
||||
{
|
||||
$tenant = $this->createTenant();
|
||||
$menu = Menu::query()->create([
|
||||
'code' => 'about',
|
||||
'content_type' => Menu::CONTENT_TYPE_STATIC,
|
||||
'static_content_schema' => [
|
||||
'title' => 'required|string|max:20',
|
||||
'sections' => 'required|array|min:1',
|
||||
'sections.*.body' => 'required|string',
|
||||
],
|
||||
'route' => '/about',
|
||||
]);
|
||||
|
||||
$this->postJson("/api/{$tenant->codigo}/menues/{$menu->code}", [
|
||||
'static_content' => [
|
||||
'title' => 123,
|
||||
'sections' => [],
|
||||
],
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors([
|
||||
'static_content.title',
|
||||
'static_content.sections',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseMissing('tenants_menues', [
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'menu_code' => $menu->code,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_configures_a_static_menu_when_content_matches_the_schema(): void
|
||||
{
|
||||
$tenant = $this->createTenant();
|
||||
$menu = Menu::query()->create([
|
||||
'code' => 'about',
|
||||
'content_type' => Menu::CONTENT_TYPE_STATIC,
|
||||
'static_content_schema' => [
|
||||
'title' => 'required|string|max:20',
|
||||
'sections' => 'required|array|min:1',
|
||||
'sections.*.body' => 'required|string',
|
||||
],
|
||||
'route' => '/about',
|
||||
]);
|
||||
|
||||
$content = [
|
||||
'title' => 'Nosotros',
|
||||
'sections' => [
|
||||
['body' => 'Nuestra historia'],
|
||||
],
|
||||
];
|
||||
|
||||
$this->postJson("/api/{$tenant->codigo}/menues/{$menu->code}", [
|
||||
'static_content' => $content,
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('static_content.title', 'Nosotros')
|
||||
->assertJsonPath('static_content.sections.0.body', 'Nuestra historia');
|
||||
|
||||
$this->assertSame(
|
||||
$content,
|
||||
$tenant->menues()->where('menues.code', $menu->code)->firstOrFail()->pivot->static_content
|
||||
);
|
||||
}
|
||||
|
||||
public function test_dynamic_menus_do_not_require_static_content(): void
|
||||
{
|
||||
$tenant = $this->createTenant();
|
||||
$menu = Menu::query()->create([
|
||||
'code' => 'catalog',
|
||||
'route' => '/catalog',
|
||||
]);
|
||||
|
||||
$this->postJson("/api/{$tenant->codigo}/menues/{$menu->code}")
|
||||
->assertOk()
|
||||
->assertJsonPath('static_content', null);
|
||||
|
||||
$this->assertDatabaseHas('tenants_menues', [
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'menu_code' => $menu->code,
|
||||
'static_content' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createTenant(): Tenant
|
||||
{
|
||||
$headerLogo = $this->createAttachment('header.png');
|
||||
$footerLogo = $this->createAttachment('footer.png');
|
||||
|
||||
return Tenant::query()->create([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
'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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user