204 lines
7.5 KiB
PHP
204 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Tenant;
|
|
|
|
use App\Domains\Core\Auth\Models\User;
|
|
use App\Domains\Core\Authorization\Enums\RoleCode;
|
|
use App\Domains\Core\Tenant\Models\StorefrontWebsiteType;
|
|
use App\Domains\Core\Tenant\Models\Tenant;
|
|
use App\Shared\Attachable\Enums\AttachmentType;
|
|
use App\Shared\Attachable\Models\Attachment;
|
|
use App\Shared\Attachable\Models\AttachmentCrop;
|
|
use Database\Seeders\AuthorizationSeeder;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Laravel\Sanctum\Sanctum;
|
|
use Tests\TestCase;
|
|
|
|
class AdminAppBrandControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->seed(AuthorizationSeeder::class);
|
|
Storage::fake('s3');
|
|
}
|
|
|
|
public function test_only_the_authenticated_adminapp_tenant_brand_is_returned(): void
|
|
{
|
|
$websiteType = $this->websiteType('shopit');
|
|
$carousel = $websiteType->extras()->create([
|
|
'codigo' => 'carousel',
|
|
'nombre' => 'Carrusel principal',
|
|
'is_required' => false,
|
|
'config_schema' => [
|
|
'request_rules' => ['$' => 'required|array'],
|
|
'transforms' => ['$.*' => ['handler' => 'attachment', 'attachment_type' => 'image']],
|
|
],
|
|
]);
|
|
$header = $this->image('header.png');
|
|
$footer = $this->image('footer.png');
|
|
$first = $this->image('first.png');
|
|
$second = $this->image('second.png');
|
|
$cropped = $this->image('first-cropped.png');
|
|
$first->cropVariants()->create([
|
|
'variant' => AttachmentCrop::DESKTOP,
|
|
'crop_horizontal' => ['start_percentage' => 10, 'end_percentage' => 90],
|
|
'crop_vertical' => ['start_percentage' => 20, 'end_percentage' => 80],
|
|
'cropped_attachment_id' => $cropped->id,
|
|
]);
|
|
$tenant = $this->tenant('acme', $websiteType, [
|
|
'nombre' => 'Acme',
|
|
'primary_color' => '#112233',
|
|
'header_bg_color' => '#223344',
|
|
'footer_bg_color' => '#112233',
|
|
'header_logo_id' => $header->id,
|
|
'footer_logo_id' => $footer->id,
|
|
]);
|
|
$tenant->websiteExtras()->create([
|
|
'website_type_extra_id' => $carousel->id,
|
|
'config' => [$first->id, $second->id],
|
|
]);
|
|
$other = $this->tenant('other', $websiteType, ['nombre' => 'Other']);
|
|
$other->websiteExtras()->create([
|
|
'website_type_extra_id' => $carousel->id,
|
|
'config' => [$this->image('other.png')->id],
|
|
]);
|
|
|
|
Sanctum::actingAs($this->admin($tenant));
|
|
|
|
$response = $this->getJson('/api/v1/adminapp/tenant/brand')
|
|
->assertOk()
|
|
->assertJsonPath('data.nombre', 'Acme')
|
|
->assertJsonPath('data.primary_color', '#112233')
|
|
->assertJsonPath('data.header_bg_color', '#223344')
|
|
->assertJsonPath('data.footer_color', 'primary')
|
|
->assertJsonCount(2, 'data.banner_images');
|
|
|
|
foreach (['header_logo' => $header, 'footer_logo' => $footer] as $field => $image) {
|
|
$this->assertStringContainsString($image->path, $response->json("data.{$field}"));
|
|
}
|
|
foreach ([$first, $second] as $index => $image) {
|
|
$this->assertStringContainsString(
|
|
$image->path,
|
|
$response->json("data.banner_images.{$index}.original_url")
|
|
);
|
|
}
|
|
$this->assertStringContainsString($cropped->path, $response->json('data.banner_images.0.url'));
|
|
$response->assertJsonPath('data.banner_images.0.crop.crop_horizontal.start_percentage', 10)
|
|
->assertJsonPath('data.banner_images.0.crop.crop_vertical.end_percentage', 80)
|
|
->assertJsonPath('data.banner_images.1.crop', null);
|
|
$this->assertStringNotContainsString('other.png', $response->getContent());
|
|
}
|
|
|
|
public function test_footer_color_is_mapped_to_a_supported_option(): void
|
|
{
|
|
$websiteType = $this->websiteType('shopit');
|
|
$cases = [
|
|
['primary', '#112233', '#112233'],
|
|
['white', '#112233', '#FFFFFF'],
|
|
['black', '#112233', '#000000'],
|
|
['primary', '#112233', '#445566'],
|
|
];
|
|
|
|
foreach ($cases as $index => [$expected, $primaryColor, $footerColor]) {
|
|
$tenant = $this->tenant("tenant-{$index}", $websiteType, [
|
|
'primary_color' => $primaryColor,
|
|
'footer_bg_color' => $footerColor,
|
|
]);
|
|
Sanctum::actingAs($this->admin($tenant));
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/brand')
|
|
->assertOk()
|
|
->assertJsonPath('data.footer_color', $expected);
|
|
}
|
|
}
|
|
|
|
public function test_missing_and_non_shopit_carousels_return_an_empty_list(): void
|
|
{
|
|
$shopit = $this->websiteType('shopit');
|
|
$tenant = $this->tenant('acme', $shopit);
|
|
Sanctum::actingAs($this->admin($tenant));
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/brand')
|
|
->assertOk()
|
|
->assertJsonPath('data.header_logo', null)
|
|
->assertJsonPath('data.footer_logo', null)
|
|
->assertJsonPath('data.banner_images', []);
|
|
|
|
$otherType = $this->websiteType('other');
|
|
$carousel = $otherType->extras()->create([
|
|
'codigo' => 'carousel',
|
|
'nombre' => 'Other carousel',
|
|
'is_required' => false,
|
|
'config_schema' => [
|
|
'request_rules' => ['$' => 'required|array'],
|
|
'transforms' => ['$.*' => ['handler' => 'attachment', 'attachment_type' => 'image']],
|
|
],
|
|
]);
|
|
$other = $this->tenant('other', $otherType);
|
|
$other->websiteExtras()->create([
|
|
'website_type_extra_id' => $carousel->id,
|
|
'config' => [$this->image('other.png')->id],
|
|
]);
|
|
Sanctum::actingAs($this->admin($other));
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/brand')
|
|
->assertOk()
|
|
->assertJsonPath('data.banner_images', []);
|
|
}
|
|
|
|
public function test_the_brand_endpoint_requires_an_adminapp_session(): void
|
|
{
|
|
$this->getJson('/api/v1/adminapp/tenant/brand')->assertUnauthorized();
|
|
|
|
Sanctum::actingAs(User::factory()->create([
|
|
'rol_codigo' => RoleCode::User->value,
|
|
'tenant_codigo' => null,
|
|
]));
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/brand')->assertForbidden();
|
|
}
|
|
|
|
private function websiteType(string $code): StorefrontWebsiteType
|
|
{
|
|
return StorefrontWebsiteType::query()->create([
|
|
'codigo' => $code,
|
|
'nombre' => ucfirst($code),
|
|
]);
|
|
}
|
|
|
|
private function tenant(string $code, StorefrontWebsiteType $websiteType, array $attributes = []): Tenant
|
|
{
|
|
return Tenant::query()->create([
|
|
'codigo' => $code,
|
|
'nombre' => ucfirst($code),
|
|
'dominio' => "{$code}.test",
|
|
'storefront_website_type_code' => $websiteType->codigo,
|
|
...$attributes,
|
|
]);
|
|
}
|
|
|
|
private function admin(Tenant $tenant): User
|
|
{
|
|
return User::factory()->create([
|
|
'rol_codigo' => RoleCode::AdminApp->value,
|
|
'tenant_codigo' => $tenant->codigo,
|
|
]);
|
|
}
|
|
|
|
private function image(string $filename): Attachment
|
|
{
|
|
return Attachment::query()->create([
|
|
'path' => "tenants/test/{$filename}",
|
|
'filename' => $filename,
|
|
'type' => AttachmentType::Image,
|
|
'mime_type' => 'image/png',
|
|
'extension' => 'png',
|
|
]);
|
|
}
|
|
}
|