83 lines
2.4 KiB
PHP
83 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Contests;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class ContestControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_index_returns_contests(): void
|
|
{
|
|
$contest = $this->createContest();
|
|
|
|
$response = $this->getJson('/api/contests');
|
|
|
|
$response->assertStatus(200)
|
|
->assertJsonFragment(['id' => $contest->id]);
|
|
}
|
|
|
|
public function test_index_can_filter_only_active(): void
|
|
{
|
|
$active = $this->createContest(['is_active' => true]);
|
|
$inactive = $this->createContest(['is_active' => false]);
|
|
|
|
$response = $this->getJson('/api/contests?only_active=1');
|
|
|
|
$response->assertStatus(200)
|
|
->assertJsonFragment(['id' => $active->id]);
|
|
|
|
$ids = collect($response->json('data'))->pluck('id')->all();
|
|
$this->assertFalse(in_array($inactive->id, $ids, true));
|
|
}
|
|
|
|
public function test_show_returns_contest(): void
|
|
{
|
|
$contest = $this->createContest();
|
|
|
|
$response = $this->getJson("/api/contests/{$contest->id}");
|
|
|
|
$response->assertStatus(200)
|
|
->assertJsonFragment(['id' => $contest->id]);
|
|
}
|
|
|
|
public function test_admin_can_create_update_and_delete_contest(): void
|
|
{
|
|
$this->actingAsAdmin();
|
|
$ruleSet = $this->createRuleSet();
|
|
|
|
$createResponse = $this->postJson('/api/contests', [
|
|
'name' => ['cs' => 'Test soutěž', 'en' => 'Test contest'],
|
|
'description' => ['cs' => 'Popis', 'en' => 'Description'],
|
|
'rule_set_id' => $ruleSet->id,
|
|
'is_active' => true,
|
|
]);
|
|
|
|
$createResponse->assertStatus(201);
|
|
$contestId = $createResponse->json('id');
|
|
|
|
$updateResponse = $this->putJson("/api/contests/{$contestId}", [
|
|
'name' => ['cs' => 'Upraveno', 'en' => 'Updated'],
|
|
'description' => ['cs' => 'Popis', 'en' => 'Description'],
|
|
'is_active' => false,
|
|
]);
|
|
|
|
$updateResponse->assertStatus(200)
|
|
->assertJsonFragment(['id' => $contestId]);
|
|
|
|
$this->deleteJson("/api/contests/{$contestId}")
|
|
->assertStatus(204);
|
|
}
|
|
|
|
public function test_non_admin_cannot_create_contest(): void
|
|
{
|
|
$this->actingAsUser();
|
|
|
|
$this->postJson('/api/contests', [
|
|
'name' => ['cs' => 'Test soutěž', 'en' => 'Test contest'],
|
|
])->assertStatus(403);
|
|
}
|
|
}
|