Loheide-eu-mitarbeiterplatt.../tests/Feature/News/NewsCommentsTest.php
Friederich Loheide 9320d602a8
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m42s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m33s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m48s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 8m0s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 8m6s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m10s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m19s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m21s
Kommentare mit Antworten unter Neuigkeiten
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 20:13:41 +00:00

95 lines
4.7 KiB
PHP

<?php
namespace Tests\Feature\News;
use App\Models\Role;
use App\Models\User;
use App\Support\Permissions\Audiences;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\News\Models\NewsCategory;
use Inhaus\Plugins\News\Models\NewsComment;
use Inhaus\Plugins\News\Models\NewsPost;
use Tests\TestCase;
class NewsCommentsTest extends TestCase
{
use RefreshDatabase;
private NewsPost $post;
private User $author;
protected function setUp(): void
{
parent::setUp();
$this->author = User::factory()->create();
$this->post = NewsPost::factory()->create(['author_id' => $this->author->id, 'title' => 'Sommerfest']);
}
public function test_comment_and_reply_form_a_thread_and_notify(): void
{
$anna = User::factory()->create(['first_name' => 'Anna', 'last_name' => 'Arm', 'department' => 'Pflege']);
$ben = User::factory()->create(['first_name' => 'Ben', 'last_name' => 'Bau']);
$this->actingAs($anna)->post("/neuigkeiten/{$this->post->slug}/kommentare", ['body' => 'Tolles Fest!'])->assertSessionHasNoErrors();
$first = NewsComment::query()->sole();
$this->actingAs($ben)->post("/neuigkeiten/{$this->post->slug}/kommentare", ['body' => 'Finde ich auch', 'parent_id' => $first->id]);
$reply = NewsComment::query()->latest('id')->first();
// Antwort auf die Antwort bleibt im selben Strang
$this->actingAs($anna)->post("/neuigkeiten/{$this->post->slug}/kommentare", ['body' => 'Danke, Ben', 'parent_id' => $reply->id]);
$this->assertSame([null, $first->id, $first->id], NewsComment::query()->orderBy('id')->pluck('parent_id')->all());
$this->assertSame($ben->id, NewsComment::query()->latest('id')->first()->reply_to_user_id);
$this->assertSame(3, $this->author->notifications()->count(), 'Autor erfährt von jedem Kommentar');
$this->assertStringContainsString('hat Ihnen geantwortet', $anna->notifications()->first()->data['title']);
$this->actingAs($ben)->get("/neuigkeiten/{$this->post->slug}")->assertInertia(fn (Assert $page) => $page
->where('comments.count', 3)
->has('comments.threads', 1)
->where('comments.threads.0.author.department', 'Pflege')
->has('comments.threads.0.replies', 2)
->where('comments.threads.0.replies.1.reply_to', 'Ben Bau')
->where('comments.threads.0.can.edit', false)
->where('comments.threads.0.replies.0.can.edit', true));
}
public function test_only_own_comments_can_be_edited_and_moderators_delete_any(): void
{
$anna = User::factory()->create();
$comment = $this->post->comments()->create(['user_id' => $anna->id, 'body' => 'Erster']);
$reply = $this->post->comments()->create(['user_id' => $this->author->id, 'parent_id' => $comment->id, 'body' => 'Antwort']);
$this->actingAs($this->author)->put("/kommentare/{$comment->id}", ['body' => 'Fremd'])->assertForbidden();
$this->actingAs($anna)->put("/kommentare/{$comment->id}", ['body' => 'Geändert'])->assertSessionHasNoErrors();
$this->assertNotNull($comment->fresh()->edited_at);
$this->actingAs($this->author)->delete("/kommentare/{$comment->id}")->assertForbidden();
$moderators = Role::query()->create(['name' => 'Redaktion']);
$moderators->grants()->create(['ability' => 'news.comments.moderate']);
$this->actingAs(User::factory()->withRoles([$moderators])->create())->delete("/kommentare/{$comment->id}")->assertRedirect();
// Gelöschter Kommentar mit Antwort bleibt als Platzhalter
$this->actingAs($anna)->get("/neuigkeiten/{$this->post->slug}")->assertInertia(fn (Assert $page) => $page
->where('comments.count', 1)
->where('comments.threads.0.deleted', true)
->where('comments.threads.0.body', null)
->where('comments.threads.0.replies.0.body', 'Antwort'));
}
public function test_closed_comments_and_invisible_posts_reject_comments(): void
{
$user = User::factory()->create(['department' => 'Werkstatt']);
$this->post->update(['comments_enabled' => false]);
$this->actingAs($user)->post("/neuigkeiten/{$this->post->slug}/kommentare", ['body' => 'Hallo'])->assertForbidden();
$care = NewsCategory::query()->create(['name' => 'Pflege intern', 'slug' => 'pflege-intern']);
Audiences::sync('news_category', $care->id, ['department' => ['Pflege']]);
$hidden = NewsPost::factory()->create(['category_id' => $care->id]);
$this->actingAs($user)->post("/neuigkeiten/{$hidden->slug}/kommentare", ['body' => 'Hallo'])->assertNotFound();
$this->assertSame(0, NewsComment::query()->count());
}
}