Loheide-eu-mitarbeiterplatt.../tests/Feature/News/LikesAndReachTest.php
Friederich Loheide 21307fc953
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m6s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m4s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m25s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 8m3s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m26s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 9m49s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 9m1s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m41s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 45s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m6s
Herzen und Kommentare schon in der Vorschau (0.44.0)
- NewsPost::withStats: Herzen (Anzahl, eigenes) und Kommentare (Anzahl) für
  alle Vorschau-Abfragen: Übersicht, Widgets, Slider, Block, Profil, „Weitere Beiträge“, API
- NewsPostResource: „comments“ (null bei ausgeschalteten Kommentaren)
- NewsStats.vue in Karte, Listeneintrag, Block-Layouts, Slider und Seitenleiste

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 20:44:37 +00:00

97 lines
5 KiB
PHP

<?php
namespace Tests\Feature\News;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\News\Models\NewsComment;
use Inhaus\Plugins\News\Models\NewsPost;
use Tests\TestCase;
class LikesAndReachTest extends TestCase
{
use RefreshDatabase;
public function test_people_can_give_and_take_back_a_heart(): void
{
$user = User::factory()->create();
$post = NewsPost::factory()->create(['published_at' => now()->subHour()]);
$this->actingAs($user)->post("/neuigkeiten/{$post->slug}/herz")->assertRedirect();
$this->actingAs($user)->post("/neuigkeiten/{$post->slug}/herz")->assertRedirect();
$this->assertSame(1, DB::table('news_likes')->where('post_id', $post->id)->count());
$this->actingAs($user)->get("/neuigkeiten/{$post->slug}")->assertInertia(fn (Assert $page) => $page
->where('post.likes', 1)->where('post.liked', true)->where('reach', null));
$this->actingAs($user)->delete("/neuigkeiten/{$post->slug}/herz")->assertRedirect();
$this->assertSame(0, DB::table('news_likes')->count());
}
public function test_hidden_or_unpublished_posts_cannot_get_hearts(): void
{
$user = User::factory()->create();
$draft = NewsPost::factory()->create(['status' => NewsPost::DRAFT, 'published_at' => null]);
$this->actingAs($user)->post("/neuigkeiten/{$draft->slug}/herz")->assertNotFound();
}
public function test_reach_is_anonymous_and_only_for_editors(): void
{
$admin = User::factory()->admin()->create();
$readers = User::factory()->count(3)->create();
$post = NewsPost::factory()->create(['published_at' => now()->subHour()]);
foreach ($readers as $reader) {
$this->actingAs($reader)->get("/neuigkeiten/{$post->slug}")->assertOk();
$this->actingAs($reader)->get("/neuigkeiten/{$post->slug}")->assertOk();
}
$this->actingAs($readers[0])->post("/neuigkeiten/{$post->slug}/herz");
// Gespeichert wird kein Personenbezug
$this->assertSame(3, DB::table('news_reads')->where('post_id', $post->id)->count());
$this->assertFalse(DB::table('news_reads')->whereIn('reader', $readers->pluck('id')->map(fn ($id) => (string) $id))->exists());
$this->actingAs($admin)->get("/neuigkeiten/{$post->slug}")->assertInertia(fn (Assert $page) => $page
->where('reach.readers', 4)
->where('reach.likes', 1)
->where('reach.likers', [$readers[0]->name])
->where('reach.share', 100));
}
public function test_hearts_via_the_api(): void
{
$user = User::factory()->create();
$post = NewsPost::factory()->create(['published_at' => now()->subHour()]);
$headers = ['Authorization' => 'Bearer '.$user->createToken('App')->plainTextToken];
$this->postJson("/api/v1/news/{$post->slug}/like", [], $headers)->assertOk()->assertJsonPath('data.likes', 1)->assertJsonPath('data.liked', true);
$this->getJson('/api/v1/news', $headers)->assertJsonPath('data.0.likes', 1)->assertJsonPath('data.0.liked', true);
$this->getJson("/api/v1/news/{$post->slug}", $headers)->assertJsonPath('data.liked', true);
$this->deleteJson("/api/v1/news/{$post->slug}/like", [], $headers)->assertOk()->assertJsonPath('data.likes', 0);
}
public function test_previews_show_hearts_and_comments_before_opening(): void
{
[$ada, $bob] = User::factory()->count(2)->create()->all();
$post = NewsPost::factory()->create(['published_at' => now()->subHour(), 'title' => 'Sommerfest']);
$quiet = NewsPost::factory()->create(['published_at' => now()->subHours(2), 'comments_enabled' => false]);
DB::table('news_likes')->insert([['post_id' => $post->id, 'user_id' => $ada->id, 'created_at' => now()], ['post_id' => $post->id, 'user_id' => $bob->id, 'created_at' => now()]]);
NewsComment::query()->create(['post_id' => $post->id, 'user_id' => $ada->id, 'body' => 'Super!']);
NewsComment::query()->create(['post_id' => $post->id, 'user_id' => $bob->id, 'body' => 'Bin dabei']);
NewsComment::query()->create(['post_id' => $post->id, 'user_id' => $bob->id, 'body' => 'Gelöscht'])->delete();
$this->actingAs($ada)->get('/neuigkeiten')->assertInertia(fn (Assert $page) => $page
->where('posts.data.0.likes', 2)->where('posts.data.0.liked', true)->where('posts.data.0.comments', 2)
->where('posts.data.1.comments', null));
// Startseite (Widget „Neuigkeiten“) und die App bekommen dieselben Zahlen
$this->actingAs($bob)->get('/')->assertOk()->assertSee('"comments":2', false);
$headers = ['Authorization' => 'Bearer '.$bob->createToken('App')->plainTextToken];
$this->getJson('/api/v1/news', $headers)->assertJsonPath('data.0.likes', 2)->assertJsonPath('data.0.comments', 2)->assertJsonPath('data.1.comments', null);
$this->assertSame($quiet->id, $this->getJson('/api/v1/news', $headers)->json('data.1.id'));
}
}