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]; } }