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
3.9 KiB

3 months ago
<?php
namespace Tests\Feature;
use App\Models\Application;
use App\Models\Competition;
4 weeks ago
use App\Models\FormSchemaDefinition;
3 months ago
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class ParticipantApplicationSaveTest extends TestCase
{
use RefreshDatabase;
public function test_participant_can_save_draft_via_post_save_endpoint(): void
{
$user = User::query()->create(['mobile' => '13800138088']);
$competition = Competition::query()->create([
'slug' => 'xxf',
'name' => '测试赛',
'published' => true,
]);
Application::query()->create([
'user_id' => $user->id,
'competition_id' => $competition->id,
'status' => 'draft',
]);
Sanctum::actingAs($user);
$this->postJson('/api/applications/current/save?competition_slug=xxf', [
'player_name' => '张三',
'project_name' => '测试项目',
])
->assertOk()
->assertJsonPath('player_name', '张三')
->assertJsonPath('project_name', '测试项目');
}
4 weeks ago
public function test_second_draft_save_rejects_invalid_email_and_keeps_previous_value(): void
{
$user = User::query()->create(['mobile' => '13800138089']);
$competition = Competition::query()->create([
'slug' => 'xxf-email',
'name' => '测试赛',
'published' => true,
]);
Application::query()->create([
'user_id' => $user->id,
'competition_id' => $competition->id,
'status' => 'draft',
]);
Sanctum::actingAs($user);
$this->postJson('/api/applications/current/save?competition_slug=xxf-email', [
'player_name' => '张三',
'contact_email' => 'ok@example.com',
])
->assertOk()
->assertJsonPath('contact_email', 'ok@example.com');
$this->postJson('/api/applications/current/save?competition_slug=xxf-email', [
'player_name' => '李四',
'contact_email' => 'bad-email',
])
->assertUnprocessable()
->assertJsonValidationErrors(['contact_email']);
$this->getJson('/api/applications/current?competition_slug=xxf-email')
->assertOk()
->assertJsonPath('player_name', '张三')
->assertJsonPath('contact_email', 'ok@example.com');
}
public function test_draft_save_response_does_not_prefill_blank_email_from_user_profile(): void
{
$user = User::query()->create([
'mobile' => '13800138090',
'email' => 'profile@example.com',
]);
$competition = Competition::query()->create([
'slug' => 'xxf-prefill',
'name' => '测试赛',
'published' => true,
]);
Application::query()->create([
'user_id' => $user->id,
'competition_id' => $competition->id,
'status' => 'draft',
]);
4 weeks ago
$schema = FormSchemaDefinition::query()->create([
'competition_id' => $competition->id,
'purpose' => FormSchemaDefinition::PURPOSE_SIGNUP,
'name' => '报名表',
'version' => 1,
'is_published' => true,
'schema_json' => [
[
'key' => 'contact_email',
'type' => 'email',
'label' => '注册邮箱',
'prefill_from' => 'user.email',
],
],
]);
$competition->update(['form_schema_id' => $schema->id]);
4 weeks ago
Sanctum::actingAs($user);
$this->postJson('/api/applications/current/save?competition_slug=xxf-prefill', [
'contact_email' => '',
])
->assertOk()
->assertJsonPath('contact_email', null);
}
3 months ago
}