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.

152 lines
5.4 KiB

4 weeks ago
<?php
namespace Tests\Feature;
use App\Models\Application;
use App\Models\Competition;
use App\Models\CompetitionTrack;
use App\Models\Reviewer;
use App\Models\ReviewerScope;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ReviewApplicationScopeAndScoreTest extends TestCase
{
use RefreshDatabase;
public function test_reviewer_cannot_list_or_open_other_region_by_query(): void
{
$ctx = $this->seedReviewerWithRegions();
$this->actingAsReviewer($ctx['reviewer']);
$this->getJson('/api/v1/review/applications?competition_slug=hsxt&event_location=suzhou')
->assertOk()
->assertJsonPath('meta.total', 1)
->assertJsonPath('data.0.id', $ctx['shanghai']->id);
$ids = collect($this->getJson('/api/v1/review/applications?competition_slug=hsxt')->json('data'))->pluck('id');
$this->assertTrue($ids->contains($ctx['shanghai']->id));
$this->assertFalse($ids->contains($ctx['suzhou']->id));
$this->getJson('/api/v1/review/applications/'.$ctx['suzhou']->id.'?competition_slug=hsxt')
->assertForbidden();
$this->postJson('/api/v1/review/applications/'.$ctx['suzhou']->id.'/score', [
'competition_slug' => 'hsxt',
'payload' => ['scores' => ['a' => 10, 'b' => 20]],
])->assertForbidden();
}
public function test_score_submit_requires_item_scores_and_stores_sum(): void
{
$ctx = $this->seedReviewerWithRegions();
$this->actingAsReviewer($ctx['reviewer']);
$this->postJson('/api/v1/review/applications/'.$ctx['shanghai']->id.'/score', [
'competition_slug' => 'hsxt',
'payload' => ['line_total' => 88],
])->assertUnprocessable();
$this->postJson('/api/v1/review/applications/'.$ctx['shanghai']->id.'/score', [
'competition_slug' => 'hsxt',
'payload' => ['scores' => ['a' => 40.01, 'b' => 0]],
])->assertUnprocessable();
$this->postJson('/api/v1/review/applications/'.$ctx['shanghai']->id.'/score', [
'competition_slug' => 'hsxt',
'payload' => ['scores' => ['a' => 12.5, 'b' => 30]],
])->assertOk()
->assertJsonPath('data.my_review_score.line_total', '42.5000')
->assertJsonPath('data.my_review_score.payload_json.scores.a', 12.5)
->assertJsonPath('data.my_review_score.payload_json.line_total', 42.5);
}
private function actingAsReviewer(Reviewer $reviewer): void
{
$token = $reviewer->createToken('review')->plainTextToken;
$this->withToken($token);
}
/**
* @return array{reviewer: Reviewer, shanghai: Application, suzhou: Application}
*/
private function seedReviewerWithRegions(): array
{
$competition = Competition::query()->create([
'slug' => 'hsxt',
'name' => '沪苏协同新兴产业科创大赛',
'published' => true,
'status' => 'signup_open',
]);
CompetitionTrack::query()->create([
'competition_id' => $competition->id,
'track_code' => 'track_a',
'title' => '赛道A',
'sort' => 1,
'is_enabled' => true,
'scoring_sheet_json' => [
'version' => 1,
'title' => '测试评分表',
'items' => [
[
'key' => 'a',
'sort' => 1,
'category' => '通用',
'title' => 'A',
'criteria' => '',
'max_score' => 40,
],
[
'key' => 'b',
'sort' => 2,
'category' => '通用',
'title' => 'B',
'criteria' => '',
'max_score' => 60,
],
],
],
]);
$reviewer = Reviewer::query()->create([
'mobile' => '13900139001',
'username' => 'rev_sh',
'name' => '上海评委',
'status' => 'active',
'password_hash' => 'unused',
]);
ReviewerScope::query()->create([
'reviewer_id' => $reviewer->id,
'competition_id' => $competition->id,
'track_code' => 'track_a',
'event_location' => 'shanghai',
]);
$userA = User::query()->create(['mobile' => '13800138001']);
$userB = User::query()->create(['mobile' => '13800138002']);
$shanghai = Application::query()->create([
'user_id' => $userA->id,
'competition_id' => $competition->id,
'status' => 'submitted',
'review_eligible' => true,
'track' => 'track_a',
'event_location' => 'shanghai',
'project_name' => '上海项目',
]);
$suzhou = Application::query()->create([
'user_id' => $userB->id,
'competition_id' => $competition->id,
'status' => 'submitted',
'review_eligible' => true,
'track' => 'track_a',
'event_location' => 'suzhou',
'project_name' => '苏州项目',
]);
return [
'reviewer' => $reviewer,
'shanghai' => $shanghai,
'suzhou' => $suzhou,
];
}
}