You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.0 KiB

<?php
namespace Tests\Feature;
use App\Models\Application;
use App\Models\ApplicationFile;
use App\Models\Competition;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class ParticipantApplicationFileUploadTest extends TestCase
{
use RefreshDatabase;
public function test_participant_upload_persists_real_storage_path(): void
{
Storage::fake('public');
[$user, $application] = $this->seedEditableApplication();
Sanctum::actingAs($user);
$response = $this->withHeader('Accept', 'application/json')->post(
'/api/applications/current/files?competition_slug=xxf',
[
'kind' => 'plan',
'file' => UploadedFile::fake()->create('商业计划书.pdf', 120, 'application/pdf'),
]
);
$response->assertCreated()
->assertJsonPath('kind', 'plan')
->assertJsonPath('original_name', '商业计划书.pdf');
$file = ApplicationFile::query()->findOrFail($response->json('id'));
$this->assertIsString($file->path);
$this->assertNotSame('0', $file->path);
$this->assertStringStartsWith("applications/{$application->id}/", $file->path);
Storage::disk('public')->assertExists($file->path);
}
public function test_participant_upload_rejects_when_storage_write_returns_false(): void
{
[$user] = $this->seedEditableApplication();
Sanctum::actingAs($user);
$disk = \Mockery::mock(\Illuminate\Contracts\Filesystem\Filesystem::class);
$disk->shouldReceive('putFileAs')->once()->andReturn(false);
$disk->shouldReceive('exists')->never();
Storage::shouldReceive('disk')->with('public')->andReturn($disk);
$response = $this->withHeader('Accept', 'application/json')->post(
'/api/applications/current/files?competition_slug=xxf',
[
'kind' => 'plan',
'file' => UploadedFile::fake()->create('商业计划书.pdf', 120, 'application/pdf'),
]
);
$response->assertStatus(422)
->assertJsonValidationErrors(['file']);
$this->assertSame(0, ApplicationFile::query()->count());
}
public function test_participant_upload_rejects_when_storage_throws(): void
{
[$user] = $this->seedEditableApplication();
Sanctum::actingAs($user);
$root = storage_path('framework/testing/unwritable-'.uniqid('', true));
mkdir($root, 0700, true);
chmod($root, 0500);
$this->beforeApplicationDestroyed(static function () use ($root): void {
@chmod($root, 0700);
@rmdir($root);
});
config(['filesystems.disks.public.root' => $root]);
config(['filesystems.disks.public.throw' => false]);
Storage::forgetDisk('public');
$response = $this->withHeader('Accept', 'application/json')->post(
'/api/applications/current/files?competition_slug=xxf',
[
'kind' => 'plan',
'file' => UploadedFile::fake()->create('商业计划书.pdf', 120, 'application/pdf'),
]
);
$response->assertStatus(422)
->assertJsonValidationErrors(['file']);
$this->assertSame(0, ApplicationFile::query()->count());
}
/**
* @return array{0: User, 1: Application}
*/
private function seedEditableApplication(): array
{
$user = User::query()->create(['mobile' => '13800138077']);
$competition = Competition::query()->create([
'slug' => 'xxf',
'name' => '测试赛',
'published' => true,
'signup_close_at' => now()->addDay(),
]);
$application = Application::query()->create([
'user_id' => $user->id,
'competition_id' => $competition->id,
'status' => 'draft',
]);
return [$user, $application];
}
}