Loheide-eu-mitarbeiterplatt.../tests/Feature/News/LikesAndReachTest.php
Friederich Loheide b3e2b525e1
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 6m10s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Failing after 6m3s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m33s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 7m25s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m28s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m27s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m26s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m27s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 44s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m12s
Herzen, Reichweite und App-Anbindung (0.30.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 12:24:11 +00:00

75 lines
3.3 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\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);
}
}