test assignment completion

This commit is contained in:
Yisroel Baum 2026-08-15 22:39:34 +03:00
parent 8218de16f5
commit 916f070455
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
4 changed files with 378 additions and 3 deletions

View file

@ -3,6 +3,7 @@
namespace Tests\Feature\Schedule;
use App\Auth\CreateSessionDto;
use App\Auth\Clock;
use App\Auth\SessionRepository;
use App\Element\CreateElementDto;
use App\Element\ElementModel;
@ -25,6 +26,7 @@ use DateTimeZone;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Testing\TestResponse;
use Tests\TestCase;
use Tests\Fakes\FakeClock;
class ScheduleEndpointTest extends TestCase
{
@ -94,6 +96,7 @@ class ScheduleEndpointTest extends TestCase
'assignments' => [
[
'id' => 1,
'completedAt' => null,
'element' => [
'name' => 'Chapter 1',
'kind' => 'chapter',
@ -115,6 +118,7 @@ class ScheduleEndpointTest extends TestCase
'assignments' => [
[
'id' => 2,
'completedAt' => null,
'element' => [
'name' => 'Chapter 1',
'kind' => 'chapter',
@ -351,6 +355,130 @@ class ScheduleEndpointTest extends TestCase
->assertExactJson(['error' => 'date is required']);
}
public function test_it_completes_and_reopens_an_assignment(): void
{
$user = $this->createUser('reader@example.com');
$set = $this->createSet($user, 'Course');
$lessonLevel = $this->createLevel($set, 'lesson');
$repository = app(ElementRepository::class);
$repository->create(new CreateElementDto(
name: 'First lesson',
level: $lessonLevel,
parentElement: null,
));
$repository->create(new CreateElementDto(
name: 'Second lesson',
level: $lessonLevel,
parentElement: null,
));
$this->createSession($user, 'valid-token');
$completedAt = new DateTimeImmutable(
'2026-08-15T12:30:45',
new DateTimeZone('UTC'),
);
$this->app->instance(Clock::class, new FakeClock($completedAt));
$this->credentialedPost('/api/schedules', [
'setId' => $set->getId(),
'levelId' => $lessonLevel->getId(),
'startDate' => '2026-08-20',
'targetDate' => '2026-08-20',
])->assertCreated();
$this->credentialedPatch('/api/assignments/1', [
'completed' => true,
])->assertOk()->assertExactJson([
'assignment' => [
'id' => 1,
'completedAt' => '2026-08-15T12:30:45+00:00',
],
]);
$this->assertDatabaseHas('schedule_assignments', [
'id' => 1,
'completed_at' => '2026-08-15 12:30:45',
]);
$this->credentialedGet('/api/schedules/1')
->assertOk()
->assertJsonPath(
'schedule.days.0.assignments.0.completedAt',
'2026-08-15T12:30:45+00:00',
)
->assertJsonPath(
'schedule.days.0.assignments.1.completedAt',
null,
);
$this->credentialedGet('/api/assignments?date=2026-08-20')
->assertOk()
->assertJsonCount(1, 'assignments')
->assertJsonPath('assignments.0.id', 2);
$this->credentialedPatch('/api/assignments/1', [
'completed' => false,
])->assertOk()->assertExactJson([
'assignment' => [
'id' => 1,
'completedAt' => null,
],
]);
$this->assertDatabaseHas('schedule_assignments', [
'id' => 1,
'completed_at' => null,
]);
$this->credentialedGet('/api/assignments?date=2026-08-20')
->assertOk()
->assertJsonCount(2, 'assignments');
}
public function test_it_rejects_invalid_assignment_completion_input(): void
{
$user = $this->createUser('reader@example.com');
$this->createSession($user, 'valid-token');
$this->credentialedPatch('/api/assignments/1', [
'completed' => 'yes',
])->assertBadRequest()->assertExactJson([
'error' => 'completed must be a boolean',
]);
}
public function test_it_hides_another_users_assignment(): void
{
$owner = $this->createUser('owner@example.com');
$viewer = $this->createUser('viewer@example.com');
$set = $this->createSet($owner, 'Course');
$lessonLevel = $this->createLevel($set, 'lesson');
app(ElementRepository::class)->create(new CreateElementDto(
name: 'Welcome',
level: $lessonLevel,
parentElement: null,
));
$this->createSession($owner, 'owner-token');
$this->createSession($viewer, 'viewer-token');
$this->withCredentials()
->withUnencryptedCookie(
AuthMiddleware::COOKIE_NAME,
'owner-token',
)->postJson('/api/schedules', [
'setId' => $set->getId(),
'levelId' => $lessonLevel->getId(),
'startDate' => '2026-08-15',
'targetDate' => '2026-08-15',
])->assertCreated();
$this->withCredentials()
->withUnencryptedCookie(
AuthMiddleware::COOKIE_NAME,
'viewer-token',
)->patchJson('/api/assignments/1', [
'completed' => true,
])->assertNotFound()->assertExactJson([
'error' => 'assignment not found',
]);
$this->assertDatabaseHas('schedule_assignments', [
'id' => 1,
'completed_at' => null,
]);
}
public function test_it_is_stable_after_sources_change_or_are_deleted(): void
{
$user = $this->createUser('reader@example.com');
@ -455,6 +583,9 @@ class ScheduleEndpointTest extends TestCase
{
$this->getJson('/api/assignments?date=2026-08-15')
->assertStatus(401);
$this->patchJson('/api/assignments/1', [
'completed' => true,
])->assertStatus(401);
$this->getJson('/api/schedules')->assertStatus(401);
$this->getJson('/api/schedules/1')->assertStatus(401);
$this->postJson('/api/schedules', [])->assertStatus(401);
@ -520,4 +651,18 @@ class ScheduleEndpointTest extends TestCase
'valid-token',
)->getJson($uri);
}
/**
* @param array<string, mixed> $payload
*/
private function credentialedPatch(
string $uri,
array $payload,
): TestResponse {
return $this->withCredentials()
->withUnencryptedCookie(
AuthMiddleware::COOKIE_NAME,
'valid-token',
)->patchJson($uri, $payload);
}
}