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')); } public function test_prefetching_a_post_does_not_count_as_reading(): void { $post = NewsPost::factory()->create(['published_at' => now()->subHour()]); $user = User::factory()->create(); $this->actingAs($user)->get("/neuigkeiten/{$post->slug}", ['Purpose' => 'prefetch'])->assertOk(); $this->assertSame(0, DB::table('news_reads')->count()); $this->actingAs($user)->get("/neuigkeiten/{$post->slug}")->assertOk(); $this->assertSame(1, DB::table('news_reads')->count()); } }