Loheide-eu-mitarbeiterplatt.../tests/Feature/Ideen/IdeasTest.php
Friederich Loheide 858e248d26
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 9m16s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 9m12s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 9m52s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 9m47s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m4s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m6s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m6s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 56s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m56s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m53s
Ideenbox (0.33.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 13:09:05 +00:00

106 lines
5.3 KiB
PHP

<?php
namespace Tests\Feature\Ideen;
use App\Models\Role;
use App\Models\User;
use App\Notifications\PortalNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Ideen\Models\Idea;
use Tests\TestCase;
class IdeasTest extends TestCase
{
use RefreshDatabase;
private User $board;
protected function setUp(): void
{
parent::setUp();
$role = Role::query()->create(['name' => 'Ideengremium']);
$role->grants()->create(['ability' => 'ideas.manage']);
$this->board = User::factory()->withRoles([$role])->create();
}
public function test_submit_support_and_comment(): void
{
Notification::fake();
$author = User::factory()->create();
$fan = User::factory()->create();
$this->actingAs($author)->post('/ideen', ['title' => 'Überdachte Fahrradständer', 'body' => 'Damit die Räder nicht nass werden.'])->assertRedirect();
$idea = Idea::query()->firstOrFail();
$this->assertSame($author->id, $idea->author_id);
$this->actingAs($author)->post("/ideen/{$idea->id}/unterstuetzen")->assertStatus(422);
$this->actingAs($fan)->post("/ideen/{$idea->id}/unterstuetzen")->assertRedirect();
$this->actingAs($fan)->post("/ideen/{$idea->id}/unterstuetzen");
$this->assertSame(1, $idea->fresh()->supports_count);
$this->actingAs($fan)->post("/ideen/{$idea->id}/kommentare", ['body' => 'Gute Idee!', 'official' => true])->assertRedirect();
$this->assertFalse($idea->comments()->first()->official);
Notification::assertSentTo($author, PortalNotification::class);
$this->actingAs($fan)->get("/ideen/{$idea->id}")->assertInertia(fn (Assert $page) => $page
->where('idea.supported', true)->where('idea.supports', 1)->where('idea.can_edit', false)->where('idea.can_manage', false)
->has('idea.comment_list', 1));
$this->actingAs($fan)->delete("/ideen/{$idea->id}/unterstuetzen");
$this->assertSame(0, $idea->fresh()->supports_count);
}
public function test_anonymous_ideas_keep_no_author(): void
{
$user = User::factory()->create(['first_name' => 'Anna', 'last_name' => 'Nym']);
$this->actingAs($user)->post('/ideen', ['title' => 'Mehr Pausenräume', 'body' => 'Bitte.', 'anonymous' => true]);
$idea = Idea::query()->firstOrFail();
$this->assertNull($idea->author_id);
$this->actingAs($this->board)->get("/ideen/{$idea->id}")->assertInertia(fn (Assert $page) => $page->where('idea.author', null)->where('idea.anonymous', true));
$this->actingAs($this->board)->get('/ideen')->assertDontSee('Nym');
$this->actingAs($user)->put("/ideen/{$idea->id}", ['title' => 'Neu', 'body' => 'x'])->assertForbidden();
}
public function test_board_sets_status_and_informs_author_and_supporters(): void
{
Notification::fake();
$author = User::factory()->create();
$fans = User::factory()->count(2)->create();
$idea = Idea::query()->create(['title' => 'Obstkorb', 'body' => 'Frisches Obst', 'author_id' => $author->id]);
$idea->supporters()->attach($fans->pluck('id'));
$this->actingAs($author)->put("/ideen/{$idea->id}/status", ['status' => 'done'])->assertForbidden();
$this->actingAs($this->board)->put("/ideen/{$idea->id}/status", ['status' => 'done', 'status_note' => 'Ab Montag in jeder Etage.'])->assertRedirect();
$this->assertSame('done', $idea->fresh()->status);
Notification::assertSentTo([$author, ...$fans], PortalNotification::class, fn ($n) => str_contains($n->title, 'Umgesetzt'));
// Nach der Bewertung kann die einreichende Person nicht mehr bearbeiten oder löschen
$this->actingAs($author)->put("/ideen/{$idea->id}", ['title' => 'Neu', 'body' => 'x'])->assertForbidden();
$this->actingAs($author)->delete("/ideen/{$idea->id}")->assertForbidden();
$this->actingAs($this->board)->post("/ideen/{$idea->id}/kommentare", ['body' => 'Danke!', 'official' => true]);
$this->assertTrue($idea->comments()->first()->official);
}
public function test_listing_filters_and_api(): void
{
$user = User::factory()->create();
$coffee = Idea::query()->create(['title' => 'Kaffeemaschine', 'body' => 'Neue bitte', 'status' => 'review']);
Idea::query()->create(['title' => 'Parkplätze', 'body' => 'Mehr davon']);
$this->actingAs($user)->get('/ideen?status=review')->assertInertia(fn (Assert $page) => $page->has('ideas', 1)->where('ideas.0.title', 'Kaffeemaschine')->where('counts.new', 1));
$this->actingAs($user)->get('/ideen?q=Park')->assertInertia(fn (Assert $page) => $page->has('ideas', 1));
$headers = ['Authorization' => 'Bearer '.$user->createToken('App')->plainTextToken];
$this->getJson('/api/v1/ideas', $headers)->assertOk()->assertJsonCount(2, 'data');
$id = $this->postJson('/api/v1/ideas', ['title' => 'Aus der App', 'body' => 'Test'], $headers)->assertCreated()->json('data.id');
$this->postJson("/api/v1/ideas/{$coffee->id}/support", [], $headers)->assertOk()->assertJsonPath('data.supports', 1);
$this->postJson("/api/v1/ideas/{$id}/comments", ['body' => 'Hallo'], $headers)->assertCreated()->assertJsonPath('data.comments', 1);
}
}