previousState = is_file($file) ? file_get_contents($file) : null; $state = json_decode($this->previousState ?? '{}', true) ?: []; $state['gruppen'] = ['enabled' => true]; file_put_contents($file, json_encode($state)); parent::setUp(); Storage::fake('local'); } protected function tearDown(): void { $file = storage_path('app/plugins.json'); $this->previousState === null ? @unlink($file) : file_put_contents($file, $this->previousState); parent::tearDown(); } private function group(User $creator, string $privacy, string $name = 'Laufgruppe'): Group { $this->actingAs($creator)->post('/gruppen', ['name' => $name, 'privacy' => $privacy, 'description' => 'Wir laufen mittwochs.'])->assertRedirect(); return Group::query()->latest('id')->firstOrFail(); } public function test_open_groups_everyone_can_read_and_join(): void { [$ada, $bob] = User::factory()->count(2)->create()->all(); $group = $this->group($ada, 'open'); $this->assertSame('laufgruppe', $group->slug); $this->assertSame(1, $group->members_count); $this->actingAs($ada)->postJson("/gruppen/{$group->slug}/beitraege", ['body' => 'Mittwoch 17 Uhr am Haupteingang?'])->assertCreated(); // Nicht-Mitglieder lesen mit, dürfen aber nicht schreiben $this->actingAs($bob)->get("/gruppen/{$group->slug}")->assertInertia(fn (Assert $p) => $p->has('posts', 1)->where('can.post', false)->where('can.read', true)); $this->actingAs($bob)->postJson("/gruppen/{$group->slug}/beitraege", ['body' => 'Hallo'])->assertForbidden(); $this->actingAs($bob)->post("/gruppen/{$group->slug}/beitreten")->assertSessionHas('status', 'Willkommen in der Gruppe!'); $this->assertSame(2, $group->fresh()->members_count); $this->actingAs($bob)->get('/gruppen')->assertInertia(fn (Assert $p) => $p->has('groups', 1)->has('posts', 1)); } public function test_closed_groups_need_approval_and_hide_posts(): void { Notification::fake(); [$ada, $bob] = User::factory()->count(2)->create()->all(); $group = $this->group($ada, 'closed', 'MAV-Austausch'); $this->actingAs($ada)->postJson("/gruppen/{$group->slug}/beitraege", ['body' => 'Intern']); $this->actingAs($bob)->get("/gruppen/{$group->slug}")->assertInertia(fn (Assert $p) => $p->has('posts', 0)->where('can.read', false)->where('members', null)); $this->actingAs($bob)->post("/gruppen/{$group->slug}/beitreten")->assertSessionHas('status', 'Anfrage gesendet – die Gruppe entscheidet.'); Notification::assertSentTo($ada, PortalNotification::class, fn ($n) => str_contains($n->title, 'Beitrittsanfrage')); $this->actingAs($ada)->get("/gruppen/{$group->slug}")->assertInertia(fn (Assert $p) => $p->where('members.pending.0.id', $bob->id)); $this->actingAs($ada)->post("/gruppen/{$group->slug}/anfragen/{$bob->id}", ['accept' => true]); $this->actingAs($bob)->get("/gruppen/{$group->slug}")->assertInertia(fn (Assert $p) => $p->has('posts', 1)->where('can.post', true)); } public function test_secret_groups_are_invisible_and_invite_only(): void { Notification::fake(); [$ada, $bob, $eve] = User::factory()->count(3)->create()->all(); $group = $this->group($ada, 'secret', 'Projekt Umzug'); $this->actingAs($eve)->get("/gruppen/{$group->slug}")->assertNotFound(); $this->actingAs($eve)->post("/gruppen/{$group->slug}/beitreten")->assertNotFound(); $this->actingAs($eve)->get('/gruppen/entdecken')->assertInertia(fn (Assert $p) => $p->has('groups', 0)); $this->actingAs($ada)->post("/gruppen/{$group->slug}/einladen", ['users' => [$bob->id]])->assertSessionHas('status'); Notification::assertSentTo($bob, PortalNotification::class, fn ($n) => str_contains($n->title, 'Einladung')); $this->actingAs($bob)->get('/gruppen')->assertInertia(fn (Assert $p) => $p->has('invitations', 1)); $this->actingAs($bob)->post("/gruppen/{$group->slug}/beitreten")->assertSessionHas('status', 'Willkommen in der Gruppe!'); // Nur Admins/Moderation laden in nicht offene Gruppen ein $this->actingAs($bob)->post("/gruppen/{$group->slug}/einladen", ['users' => [$eve->id]])->assertForbidden(); } public function test_posts_with_photos_polls_reactions_comments_and_moderation(): void { Notification::fake(); [$ada, $bob, $cem] = User::factory()->count(3)->create()->all(); $group = $this->group($ada, 'open'); $this->actingAs($bob)->post("/gruppen/{$group->slug}/beitreten"); $this->actingAs($cem)->post("/gruppen/{$group->slug}/beitreten"); $post = $this->actingAs($ada)->post("/gruppen/{$group->slug}/beitraege", [ 'body' => 'Welcher Tag passt?', 'poll' => ['Mittwoch', 'Donnerstag', ''], 'files' => [UploadedFile::fake()->image('strecke.jpg', 2000, 1500), UploadedFile::fake()->create('plan.pdf', 20, 'application/pdf')], ], ['Accept' => 'application/json'])->assertCreated()->json('post'); $this->assertCount(1, $post['images']); $this->assertSame(1600, $post['images'][0]['width']); $this->assertSame('plan.pdf', $post['files'][0]['name']); $this->assertCount(2, $post['poll']['options']); Notification::assertSentTo([$bob, $cem], PortalNotification::class, fn ($n) => str_contains($n->title, 'Neu in')); $this->actingAs($bob)->postJson("/gruppen/beitraege/{$post['id']}/abstimmen", ['options' => [1]])->assertJsonPath('poll.options.1.votes', 1); $this->actingAs($cem)->postJson("/gruppen/beitraege/{$post['id']}/reaktion", ['emoji' => '❤️'])->assertOk(); $this->actingAs($bob)->postJson("/gruppen/beitraege/{$post['id']}/kommentare", ['body' => 'Donnerstag!'])->assertOk(); Notification::assertSentTo($ada, PortalNotification::class, fn ($n) => str_contains($n->title, 'kommentiert')); // Melden → Gruppen-Admin erfährt es; Admin pinnt und löscht $this->actingAs($cem)->postJson("/gruppen/beitraege/{$post['id']}/melden", ['reason' => 'Test'])->assertOk(); Notification::assertSentTo($ada, PortalNotification::class, fn ($n) => str_contains($n->title, 'gemeldet')); $this->actingAs($bob)->postJson("/gruppen/beitraege/{$post['id']}/anpinnen", ['pinned' => true])->assertForbidden(); $this->actingAs($ada)->postJson("/gruppen/beitraege/{$post['id']}/anpinnen", ['pinned' => true])->assertOk(); $this->actingAs($bob)->get("/gruppen/{$group->slug}")->assertInertia(fn (Assert $p) => $p ->where('posts.0.pinned', true)->where('posts.0.poll.mine', [1])->where('posts.0.reactions.0.emoji', '❤️') ->where('posts.0.comments.0.body', 'Donnerstag!')->where('posts.0.comments_count', 1)); // Fotos nur für Berechtigte (offen: alle), gelöschte weg $this->actingAs($cem)->get($post['images'][0]['thumb_url'])->assertOk(); $this->actingAs($bob)->deleteJson("/gruppen/beitraege/{$post['id']}")->assertForbidden(); $this->actingAs($ada)->deleteJson("/gruppen/beitraege/{$post['id']}")->assertOk(); $this->assertSame(0, Post::query()->count()); $this->assertSame([], Storage::disk('local')->allFiles("gruppen/{$group->id}/{$post['id']}")); } public function test_roles_and_last_admin_handover(): void { [$ada, $bob] = User::factory()->count(2)->create()->all(); $group = $this->group($ada, 'open'); $this->actingAs($bob)->post("/gruppen/{$group->slug}/beitreten"); $this->actingAs($bob)->put("/gruppen/{$group->slug}/mitglieder/{$ada->id}", ['role' => 'member'])->assertForbidden(); $this->actingAs($ada)->put("/gruppen/{$group->slug}/mitglieder/{$ada->id}", ['role' => 'member'])->assertSessionHasErrors('role'); $this->actingAs($ada)->post("/gruppen/{$group->slug}/verlassen"); $this->assertSame('admin', $group->membership($bob)->role); } public function test_app_api_with_token(): void { [$ada, $bob, $eve] = User::factory()->count(3)->create()->all(); $open = $this->group($ada, 'open'); $closed = $this->group($ada, 'closed', 'Vorstand'); $secret = $this->group($ada, 'secret', 'Geheimrat'); Sanctum::actingAs($ada); $image = $this->post("/api/v1/gruppen/{$open->slug}/beitraege", ['body' => 'Mit Foto', 'files' => [UploadedFile::fake()->image('lauf.jpg', 60, 40)]], ['Accept' => 'application/json']) ->assertCreated()->json('post.images.0.thumb_url'); $this->assertStringContainsString('/api/v1/gruppen/dateien/', $image); $this->get($image)->assertOk(); $this->postJson("/api/v1/gruppen/{$closed->slug}/beitraege", ['body' => 'Nur für uns'])->assertCreated(); Sanctum::actingAs($bob); $this->getJson('/api/v1/gruppen/entdecken')->assertOk()->assertJsonCount(2, 'data')->assertJsonMissing(['name' => 'Geheimrat']); $this->getJson("/api/v1/gruppen/{$secret->slug}")->assertNotFound(); $this->getJson("/api/v1/gruppen/{$closed->slug}")->assertOk()->assertJsonCount(0, 'posts')->assertJsonPath('can.read', false)->assertJsonPath('members', []); $this->postJson("/api/v1/gruppen/{$closed->slug}/beitreten")->assertJsonPath('status', 'pending'); $post = $this->getJson("/api/v1/gruppen/{$open->slug}")->assertOk()->assertJsonPath('can.post', false)->json('posts.0.id'); $this->postJson("/api/v1/gruppen/{$open->slug}/beitreten")->assertJsonPath('status', 'active'); $this->postJson("/api/v1/gruppen/beitraege/{$post}/reaktion", ['emoji' => '👍'])->assertOk(); $this->postJson("/api/v1/gruppen/beitraege/{$post}/kommentare", ['body' => 'Bin dabei!'])->assertOk()->assertJsonPath('comments.0.body', 'Bin dabei!'); $this->getJson("/api/v1/gruppen/beitraege/{$post}")->assertJsonPath('post.my_reaction', '👍')->assertJsonPath('comments.0.user.id', $bob->id); $this->getJson('/api/v1/gruppen')->assertJsonCount(1, 'groups')->assertJsonCount(1, 'posts'); // Bilder aus fremden, geschlossenen Gruppen bleiben zu Sanctum::actingAs($eve); $this->get($image)->assertOk(); $closedPost = Post::query()->where('group_id', $closed->id)->firstOrFail(); $this->getJson("/api/v1/gruppen/beitraege/{$closedPost->id}")->assertNotFound(); } public function test_posts_reach_the_feed_and_invitations_and_requests_become_tasks(): void { if (! class_exists(Feed::class)) { $this->markTestSkipped('Inhaus ohne Feed'); } [$ada, $bob, $eve] = User::factory()->count(3)->create()->all(); $group = $this->group($ada, 'closed'); $this->actingAs($ada)->postJson("/gruppen/{$group->slug}/beitraege", ['body' => 'Mittwoch 17 Uhr am Haupteingang?'])->assertCreated(); $feed = collect(app(Feed::class)->page($ada)['items'])->firstWhere('kind', 'group-post'); $this->assertSame('Mittwoch 17 Uhr am Haupteingang?', $feed['text']); $this->assertSame('Laufgruppe', $feed['context']['label']); // Herz direkt im Feed = Reaktion $this->assertSame('reaction', $feed['like']['mode']); $this->actingAs($ada)->postJson($feed['like']['url'], ['emoji' => '❤️'])->assertOk(); $liked = collect(app(Feed::class)->page($ada)['items'])->firstWhere('kind', 'group-post'); $this->assertTrue($liked['like']['liked']); $this->assertSame(1, $liked['stats']['likes']); // Nicht-Mitglieder sehen den Beitrag nicht $this->assertNull(collect(app(Feed::class)->page($eve)['items'])->firstWhere('kind', 'group-post')); // Einladung → Aufgabe für Bob; Beitrittsanfrage von Eve → Aufgabe für Ada $this->actingAs($ada)->post("/gruppen/{$group->slug}/einladen", ['users' => [$bob->id]]); $this->actingAs($eve)->post("/gruppen/{$group->slug}/beitreten"); $this->assertContains('Einladung in „Laufgruppe“ annehmen', array_column(app(Tasks::class)->forUser($bob), 'title')); $this->assertContains('1 Beitrittsanfrage in „Laufgruppe“', array_column(app(Tasks::class)->forUser($ada), 'title')); } }