create(['name' => 'Kommunikation']); $role->grants()->create(['ability' => 'polls.manage']); $this->editor = User::factory()->withRoles([$role])->create(); } private function payload(array $overrides = []): array { return [ 'title' => 'Kantine', 'questions' => [ ['id' => 'essen', 'type' => 'single', 'label' => 'Wie schmeckt es?', 'options' => [['id' => 'gut', 'label' => 'Gut'], ['id' => 'geht', 'label' => 'Geht so'], ['label' => '']]], ['id' => 'tage', 'type' => 'multiple', 'label' => 'An welchen Tagen?', 'max' => 2, 'options' => ['Mo', 'Di', 'Mi']], ['id' => 'note', 'type' => 'scale', 'label' => 'Gesamtnote', 'scale' => 5], ['id' => 'idee', 'type' => 'text', 'label' => 'Wünsche', 'required' => false], ['type' => 'single', 'label' => 'Unvollständig', 'options' => ['nur eine']], ], 'results_visibility' => Poll::AFTER_VOTE, 'published' => true, ...$overrides, ]; } private function create(array $overrides = []): Poll { $this->actingAs($this->editor)->post('/redaktion/umfragen', $this->payload($overrides))->assertSessionHasNoErrors(); return Poll::query()->latest('id')->firstOrFail(); } public function test_editors_build_polls_and_incomplete_questions_are_dropped(): void { $poll = $this->create(); $this->assertCount(4, $poll->questions); $this->assertCount(2, $poll->questions[0]['options']); $this->assertSame(2, $poll->questions[1]['max']); $this->assertTrue($poll->isOpen()); $this->actingAs(User::factory()->create())->get('/redaktion/umfragen/neu')->assertForbidden(); } public function test_voting_once_with_validation_and_results_after_vote(): void { $poll = $this->create(); $user = User::factory()->create(); $this->actingAs($user)->get("/umfragen/{$poll->id}")->assertInertia(fn (Assert $page) => $page ->where('poll.can_vote', true)->where('poll.results', null)->where('poll.results_note', 'Die Ergebnisse sehen Sie nach Ihrer Abstimmung.')); $this->actingAs($user)->post("/umfragen/{$poll->id}/abstimmen", ['answers' => ['essen' => 'gut', 'tage' => ['Mo', 'Di', 'Mi']]]) ->assertSessionHasErrors(['answers.note', 'answers.tage']); $ids = collect($poll->questions[1]['options'])->pluck('id'); $this->actingAs($user)->post("/umfragen/{$poll->id}/abstimmen", ['answers' => ['essen' => 'gut', 'tage' => [$ids[0]], 'note' => 4, 'idee' => 'Mehr Salat']]) ->assertSessionHasNoErrors()->assertSessionHas('status', 'Danke für Ihre Teilnahme!'); $this->actingAs($user)->post("/umfragen/{$poll->id}/abstimmen", ['answers' => ['essen' => 'geht', 'tage' => [$ids[1]], 'note' => 1]]) ->assertSessionHasErrors('answers'); $this->assertSame($user->id, PollResponse::query()->value('user_id')); $this->actingAs($user)->get("/umfragen/{$poll->id}")->assertInertia(fn (Assert $page) => $page ->where('poll.voted', true) ->where('poll.results.total', 1) ->where('poll.results.questions.0.options.0.percent', 100) ->where('poll.results.questions.2.average', 4) ->where('poll.results.questions.3.texts', null)); $this->actingAs($this->editor)->get("/umfragen/{$poll->id}")->assertInertia(fn (Assert $page) => $page ->where('poll.results.questions.3.texts', ['Mehr Salat'])); } public function test_anonymous_polls_store_no_person_and_export_has_no_names(): void { $poll = $this->create(['anonymous' => true]); $user = User::factory()->create(['first_name' => 'Greta', 'last_name' => 'Geheim']); $this->actingAs($user)->post("/umfragen/{$poll->id}/abstimmen", ['answers' => ['essen' => 'gut', 'tage' => [$poll->questions[1]['options'][0]['id']], 'note' => 3]]); $response = PollResponse::query()->firstOrFail(); $this->assertNull($response->user_id); $this->assertNotSame((string) $user->id, $response->voter); $csv = $this->actingAs($this->editor)->get("/redaktion/umfragen/{$poll->id}/ergebnisse.csv")->assertOk()->streamedContent(); $this->assertStringNotContainsString('Geheim', $csv); $this->assertStringContainsString('Gut', $csv); // Nachträglich nicht mehr umschaltbar $this->actingAs($this->editor)->put("/redaktion/umfragen/{$poll->id}", $this->payload(['anonymous' => false, 'questions' => $poll->questions])) ->assertSessionHasErrors('anonymous'); } public function test_results_visibility_after_end_and_editors_only(): void { $user = User::factory()->create(); $afterEnd = $this->create(['results_visibility' => Poll::AFTER_END, 'closes_at' => now()->addDay()->format('Y-m-d\TH:i')]); $editorsOnly = $this->create(['results_visibility' => Poll::EDITORS]); foreach ([$afterEnd, $editorsOnly] as $poll) { $this->actingAs($user)->post("/umfragen/{$poll->id}/abstimmen", ['answers' => ['essen' => 'gut', 'tage' => [$poll->questions[1]['options'][0]['id']], 'note' => 5]]); $this->actingAs($user)->get("/umfragen/{$poll->id}")->assertInertia(fn (Assert $page) => $page->where('poll.results', null)); } $this->travel(2)->days(); $this->actingAs($user)->get("/umfragen/{$afterEnd->id}")->assertInertia(fn (Assert $page) => $page->where('poll.results.total', 1)->where('poll.open', false)); $this->actingAs($user)->post("/umfragen/{$afterEnd->id}/abstimmen", ['answers' => ['essen' => 'gut']])->assertSessionHasErrors('answers'); $this->actingAs($user)->get("/umfragen/{$editorsOnly->id}")->assertInertia(fn (Assert $page) => $page->where('poll.results', null)); } public function test_audience_drafts_and_announcement(): void { Notification::fake(); $pflege = Role::query()->create(['name' => 'Pflege']); $nurse = User::factory()->withRoles([$pflege])->create(); $other = User::factory()->create(); $draft = $this->create(['published' => false]); $this->actingAs($other)->get("/umfragen/{$draft->id}")->assertNotFound(); $poll = $this->create(['announce' => true, 'audience' => ['role' => [(string) $pflege->id]]]); $this->assertSame([(string) $pflege->id], Audiences::get('poll', $poll->id)['role']); $this->actingAs($other)->get("/umfragen/{$poll->id}")->assertNotFound(); $this->actingAs($nurse)->get("/umfragen/{$poll->id}")->assertOk(); Notification::assertSentTo($nurse, PortalNotification::class); Notification::assertNotSentTo($other, PortalNotification::class); $this->assertNotNull($poll->fresh()->announced_at); $this->actingAs($nurse)->get('/umfragen')->assertInertia(fn (Assert $page) => $page->has('open', 1)); $this->actingAs($other)->get('/umfragen')->assertInertia(fn (Assert $page) => $page->has('open', 0)); } public function test_polls_via_api(): void { $poll = $this->create(); $user = User::factory()->create(); $headers = ['Authorization' => 'Bearer '.$user->createToken('App')->plainTextToken]; $this->getJson('/api/v1/polls', $headers)->assertOk()->assertJsonPath('data.open.0.id', $poll->id); $this->getJson("/api/v1/polls/{$poll->id}", $headers)->assertJsonPath('data.can_vote', true); $this->postJson("/api/v1/polls/{$poll->id}/vote", ['answers' => ['essen' => 'geht', 'tage' => [$poll->questions[1]['options'][2]['id']], 'note' => 2]], $headers) ->assertOk()->assertJsonPath('data.voted', true)->assertJsonPath('data.results.total', 1); } }