Loheide-eu-mitarbeiterplatt.../tests/Feature/News/NewsCommentsTest.php
Friederich Loheide 8a59023569
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m41s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m32s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m11s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m55s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m32s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m30s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m39s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m30s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m3s
Profilseite: Kommentare der Person
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 20:57:09 +00:00

114 lines
5.9 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());
}
public function test_profile_lists_comments_on_visible_posts_only(): void
{
$anna = User::factory()->create();
$viewer = User::factory()->create(['department' => 'Werkstatt']);
$this->post->comments()->create(['user_id' => $anna->id, 'body' => 'Sichtbar für alle']);
$care = NewsCategory::query()->create(['name' => 'Pflege intern', 'slug' => 'pflege-intern']);
Audiences::sync('news_category', $care->id, ['department' => ['Pflege']]);
NewsPost::factory()->create(['category_id' => $care->id])->comments()->create(['user_id' => $anna->id, 'body' => 'Nur Pflege']);
$this->actingAs($viewer)->get("/personen/{$anna->id}")->assertInertia(fn (Assert $page) => $page
->where('panels.0.key', 'news.comments')
->where('panels.0.props.total', 1)
->where('panels.0.props.comments.0.excerpt', 'Sichtbar für alle')
->where('panels.0.props.comments.0.post.title', 'Sommerfest'));
// Ohne Kommentare kein Bereich
$this->actingAs($viewer)->get("/personen/{$viewer->id}")->assertInertia(fn (Assert $page) => $page->where('panels', []));
}
}