lernschiff/tests/Feature/Livewire/TasksRunPageTest.php

131 lines
4.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
use App\Livewire\Tasks\Run;
use App\Models\License;
use App\Models\Role;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
beforeEach(function () {
$this->seed(\Database\Seeders\RolesSeeder::class);
$tenant = Tenant::firstOrCreate(['name' => 'Test Schule'], ['type' => 'school', 'active' => true]);
$this->user = User::withoutGlobalScopes()->create([
'name' => 'Run Tester', 'email' => 'run@test.example',
'tenant_id' => $tenant->id, 'password' => bcrypt('password'),
]);
$role = Role::where('name', 'child')->first();
DB::table('role_user')->insert(['user_id' => $this->user->id, 'role_id' => $role->id, 'created_at' => now(), 'updated_at' => now()]);
$license = License::withoutGlobalScopes()->create([
'tenant_id' => $tenant->id, 'type' => 'school_flat',
'expires_at' => now()->addYear(), 'active' => true,
]);
DB::table('license_assignments')->insert(['license_id' => $license->id, 'user_id' => $this->user->id, 'created_at' => now(), 'updated_at' => now()]);
$this->actingAs($this->user);
});
it('route tasks.run registered with key param', function () {
expect(\Illuminate\Support\Facades\Route::has('tasks.run'))->toBeTrue();
});
it('GET /tasks/t1/run returns 200 for authenticated user', function () {
$this->get(route('tasks.run', ['key' => 't1']))->assertOk();
});
it('GET /tasks/{key}/run with unknown key returns 404', function () {
$this->get(route('tasks.run', ['key' => 'nonexistent']))->assertNotFound();
});
it('Run mount: math task (t1, primary) loads MATH questions', function () {
$component = Livewire::test(Run::class, ['key' => 't1']);
$component->assertSet('totalSteps', 5); // primary bank has 5 questions
$component->assertSee('Multiplikation bis 100');
$component->assertSee('Was ergibt 12 × 8?');
$component->assertDontSee('Welches Wort ist ein Nomen?');
});
it('Run mount: deutsch task (t2, rose) loads GERMAN questions', function () {
$component = Livewire::test(Run::class, ['key' => 't2']);
$component->assertSee('Welches Wort ist ein Nomen?');
$component->assertDontSee('Was ergibt 12 × 8?');
});
it('Run mount: english task (t3, violet) loads ENGLISH questions', function () {
$component = Livewire::test(Run::class, ['key' => 't3']);
$component->assertSee('past tense of "go"');
$component->assertDontSee('Welches Wort ist ein Nomen?');
});
it('Run mount: sachkunde task (t4, green) loads NATURE questions', function () {
$component = Livewire::test(Run::class, ['key' => 't4']);
$component->assertSee('Verdampfen von Wasser');
});
it('submitAnswer marks correct/incorrect + correctAnswers counter', function () {
Livewire::test(Run::class, ['key' => 't1'])
->call('submitAnswer', 2) // primary q1: correct = 2 (96)
->assertSet('lastAnswerIndex', 2)
->assertSet('lastAnswerCorrect', true)
->assertSet('correctAnswers', 1);
});
it('submitAnswer with wrong index sets lastAnswerCorrect=false, no point', function () {
Livewire::test(Run::class, ['key' => 't1'])
->call('submitAnswer', 0)
->assertSet('lastAnswerCorrect', false)
->assertSet('correctAnswers', 0);
});
it('nextStep advances and clears lastAnswer state', function () {
Livewire::test(Run::class, ['key' => 't1'])
->call('submitAnswer', 2)
->call('nextStep')
->assertSet('currentStep', 2)
->assertSet('lastAnswerIndex', null)
->assertSet('lastAnswerCorrect', null);
});
it('finishing last step sets finished=true', function () {
$run = Livewire::test(Run::class, ['key' => 't1']);
// primary bank has 5 questions
foreach (range(1, 5) as $step) {
$run->call('submitAnswer', 0)->call('nextStep');
}
$run->assertSet('finished', true);
});
it('restart resets all progress', function () {
Livewire::test(Run::class, ['key' => 't1'])
->call('submitAnswer', 2)
->call('nextStep')
->call('restart')
->assertSet('currentStep', 1)
->assertSet('correctAnswers', 0)
->assertSet('finished', false)
->assertSet('lastAnswerIndex', null);
});
it('startTask in Index redirects to tasks.run route (not modal)', function () {
Livewire::test(\App\Livewire\Tasks\Index::class)
->call('startTask', 't1')
->assertRedirect(route('tasks.run', ['key' => 't1']));
});
it('unauthenticated user redirected to login', function () {
auth()->logout();
$this->get(route('tasks.run', ['key' => 't1']))->assertRedirect(route('login'));
});
it('Run placeholder view exists with task-run-skeleton testid', function () {
$view = Run::placeholder([]);
expect($view->getName())->toBe('livewire.tasks.run-placeholder');
expect($view->render())->toContain('task-run-skeleton');
});