Files
crewli/api/tests/Feature/Api/V1/UploadTest.php
bert.hausmans 6a8d21a5b6 feat: registration field polish, multi-category tags, file uploads, Partner icon
- Restructure field editor dialog: move Options section to bottom with
  divider and subheader, fix delete button with flex layout
- Change tag_category (single string) to tag_categories (JSON array)
  supporting multiple category selection in tag picker fields
- Portal tag picker now groups tags by category with subheaders
- Add generic file upload endpoint (FileUploadService + UploadController)
- Replace email branding logo URL text field with ImageUploadField
- Update Partner crowd type default icon to tabler-affiliate
- Apply changes consistently to both field and template dialogs

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 18:03:49 +02:00

126 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\V1;
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 UploadTest extends TestCase
{
use RefreshDatabase;
private User $user;
private Organisation $organisation;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->organisation = Organisation::factory()->create();
$this->user = User::factory()->create();
$this->organisation->users()->attach($this->user, ['role' => 'org_admin']);
Storage::fake('public');
}
public function test_upload_image_succeeds_with_valid_png(): void
{
Sanctum::actingAs($this->user);
$file = UploadedFile::fake()->image('logo.png', 200, 200);
$response = $this->postJson('/api/v1/upload/image', [
'file' => $file,
'purpose' => 'logo',
]);
$response->assertOk()
->assertJsonStructure(['data' => ['url']]);
$url = $response->json('data.url');
$this->assertStringContainsString('uploads/logo', $url);
}
public function test_upload_image_rejects_unsupported_mime(): void
{
Sanctum::actingAs($this->user);
$file = UploadedFile::fake()->create('document.pdf', 100, 'application/pdf');
$response = $this->postJson('/api/v1/upload/image', [
'file' => $file,
'purpose' => 'logo',
]);
$response->assertStatus(422);
}
public function test_upload_image_rejects_oversized_file(): void
{
Sanctum::actingAs($this->user);
$file = UploadedFile::fake()->image('large.png')->size(6000); // 6MB > 5MB limit
$response = $this->postJson('/api/v1/upload/image', [
'file' => $file,
'purpose' => 'logo',
]);
$response->assertStatus(422);
}
public function test_upload_image_requires_authentication(): void
{
$file = UploadedFile::fake()->image('logo.png', 200, 200);
$response = $this->postJson('/api/v1/upload/image', [
'file' => $file,
'purpose' => 'logo',
]);
$response->assertUnauthorized();
}
public function test_upload_image_logs_activity(): void
{
Sanctum::actingAs($this->user);
$file = UploadedFile::fake()->image('logo.png', 200, 200);
$this->postJson('/api/v1/upload/image', [
'file' => $file,
'purpose' => 'logo',
]);
$this->assertDatabaseHas('activity_log', [
'log_name' => 'upload',
'description' => 'image.uploaded',
'causer_id' => $this->user->id,
]);
}
public function test_upload_image_requires_valid_purpose(): void
{
Sanctum::actingAs($this->user);
$file = UploadedFile::fake()->image('logo.png', 200, 200);
$response = $this->postJson('/api/v1/upload/image', [
'file' => $file,
'purpose' => 'invalid',
]);
$response->assertUnprocessable()
->assertJsonValidationErrors('purpose');
}
}