Files
crewli/api/tests/Feature/Api/V1/EventImageUploadTest.php

192 lines
6.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\V1;
use App\Models\Event;
use App\Models\Organisation;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class EventImageUploadTest extends TestCase
{
use RefreshDatabase;
private Organisation $organisation;
private User $orgAdmin;
private Event $event;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->organisation = Organisation::factory()->create();
$this->orgAdmin = User::factory()->create();
$this->orgAdmin->assignRole('org_admin');
$this->organisation->users()->attach($this->orgAdmin, ['role' => 'org_admin']);
$this->event = Event::factory()->create([
'organisation_id' => $this->organisation->id,
]);
Storage::fake('public');
}
public function test_upload_banner_jpg(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson(
"/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/upload-image",
[
'image' => UploadedFile::fake()->image('banner.jpg', 1200, 400),
'type' => 'banner',
]
);
$response->assertOk()
->assertJsonStructure(['url']);
$this->event->refresh();
$this->assertNotNull($this->event->registration_banner_url);
}
public function test_upload_logo_png(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson(
"/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/upload-image",
[
'image' => UploadedFile::fake()->image('logo.png', 200, 200),
'type' => 'logo',
]
);
$response->assertOk()
->assertJsonStructure(['url']);
$this->event->refresh();
$this->assertNotNull($this->event->registration_logo_url);
}
public function test_upload_rejects_invalid_file_type(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson(
"/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/upload-image",
[
'image' => UploadedFile::fake()->create('document.pdf', 100, 'application/pdf'),
'type' => 'banner',
]
);
$response->assertUnprocessable();
}
public function test_upload_rejects_file_too_large(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson(
"/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/upload-image",
[
'image' => UploadedFile::fake()->image('large.jpg')->size(6000),
'type' => 'banner',
]
);
$response->assertUnprocessable();
}
public function test_upload_requires_authentication(): void
{
$response = $this->postJson(
"/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/upload-image",
[
'image' => UploadedFile::fake()->image('banner.jpg'),
'type' => 'banner',
]
);
$response->assertUnauthorized();
}
public function test_upload_rejects_wrong_organisation(): void
{
$otherOrg = Organisation::factory()->create();
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson(
"/api/v1/organisations/{$otherOrg->id}/events/{$this->event->id}/upload-image",
[
'image' => UploadedFile::fake()->image('banner.jpg'),
'type' => 'banner',
]
);
$response->assertNotFound();
}
public function test_registration_data_includes_branding_fields(): void
{
$event = Event::factory()->create([
'organisation_id' => $this->organisation->id,
'status' => 'registration_open',
'slug' => 'branding-test-event',
'registration_welcome_text' => 'Welcome to our event!',
'registration_banner_url' => 'https://example.com/banner.jpg',
'registration_logo_url' => 'https://example.com/logo.png',
]);
$response = $this->getJson('/api/v1/public/events/branding-test-event/registration-data');
$response->assertOk()
->assertJsonPath('data.event.registration_welcome_text', 'Welcome to our event!')
->assertJsonPath('data.event.registration_banner_url', 'https://example.com/banner.jpg')
->assertJsonPath('data.event.registration_logo_url', 'https://example.com/logo.png');
}
public function test_registration_data_includes_null_branding_fields(): void
{
Event::factory()->create([
'organisation_id' => $this->organisation->id,
'status' => 'registration_open',
'slug' => 'no-branding-event',
]);
$response = $this->getJson('/api/v1/public/events/no-branding-event/registration-data');
$response->assertOk()
->assertJsonPath('data.event.registration_welcome_text', null)
->assertJsonPath('data.event.registration_banner_url', null)
->assertJsonPath('data.event.registration_logo_url', null);
}
public function test_event_update_saves_welcome_text(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson(
"/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}",
[
'registration_welcome_text' => 'We zoeken vrijwilligers!',
]
);
$response->assertOk();
$this->event->refresh();
$this->assertEquals('We zoeken vrijwilligers!', $this->event->registration_welcome_text);
}
}