previousState = is_file($file) ? file_get_contents($file) : null; $state = json_decode($this->previousState ?? '{}', true) ?: []; $state['kleinanzeigen'] = ['enabled' => true]; $state['chat'] = ['enabled' => false]; file_put_contents($file, json_encode($state)); parent::setUp(); Storage::fake('local'); $role = Role::query()->create(['name' => 'Marktaufsicht']); $role->grants()->create(['ability' => 'kleinanzeigen.moderate']); $this->moderator = User::factory()->withRoles([$role])->create(); } 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 payload(array $overrides = []): array { return ['type' => 'sell', 'category' => 'fahrzeuge', 'title' => 'Kinderfahrrad 20 Zoll', 'description' => 'Gut erhalten, blau.', 'price' => '45,50', 'negotiable' => true, ...$overrides]; } public function test_new_listings_need_approval_then_go_online_for_30_days(): void { Notification::fake(); $seller = User::factory()->create(); $buyer = User::factory()->create(); $this->actingAs($seller)->post('/kleinanzeigen', [...$this->payload(), 'photos' => [UploadedFile::fake()->image('rad.jpg', 2400, 1800)]])->assertRedirect('/kleinanzeigen/meine'); $listing = Listing::query()->firstOrFail(); $this->assertSame('pending', $listing->status); $this->assertSame(4550, $listing->price_cents); $this->assertSame(1600, $listing->images()->first()->width); Notification::assertSentTo($this->moderator, PortalNotification::class); $this->actingAs($buyer)->get("/kleinanzeigen/{$listing->id}")->assertNotFound(); $this->actingAs($buyer)->get('/kleinanzeigen')->assertInertia(fn (Assert $p) => $p->has('listings', 0)); $this->actingAs($buyer)->post("/kleinanzeigen/{$listing->id}/freigeben")->assertForbidden(); $this->actingAs($this->moderator)->post("/kleinanzeigen/{$listing->id}/freigeben")->assertRedirect(); $listing->refresh(); $this->assertSame('active', $listing->status); $this->assertTrue($listing->expires_at->between(now()->addDays(29), now()->addDays(31))); Notification::assertSentTo($seller, PortalNotification::class, fn ($n) => str_contains($n->title, 'online')); $this->actingAs($buyer)->get('/kleinanzeigen?typ=sell&kategorie=fahrzeuge&q=Fahrrad')->assertInertia(fn (Assert $p) => $p ->has('listings', 1)->where('listings.0.price', '45,50 € VB')); $image = $listing->images()->first(); $this->actingAs($buyer)->get("/kleinanzeigen/bilder/{$image->id}/thumb")->assertOk()->assertHeader('Content-Type', 'image/webp'); } public function test_content_changes_go_back_to_review_and_rejection_has_a_reason(): void { Notification::fake(); $seller = User::factory()->create(); $listing = Listing::query()->create([...collect($this->payload())->except('price')->all(), 'user_id' => $seller->id, 'status' => 'active', 'published_at' => now(), 'expires_at' => now()->addDays(10)]); // Nur „reserviert“ ändert nichts an der Freigabe $this->actingAs($seller)->post("/kleinanzeigen/{$listing->id}/reserviert"); $this->assertTrue($listing->fresh()->reserved); $this->assertSame('active', $listing->fresh()->status); $this->actingAs($seller)->post("/kleinanzeigen/{$listing->id}", $this->payload(['title' => 'Kinderfahrrad – jetzt mit Klingel'])); $this->assertSame('pending', $listing->fresh()->status); $this->actingAs($this->moderator)->post("/kleinanzeigen/{$listing->id}/zurueckgeben", ['note' => 'Bitte ein Foto ergänzen.']); $this->assertSame('rejected', $listing->fresh()->status); Notification::assertSentTo($seller, PortalNotification::class, fn ($n) => $n->body === 'Bitte ein Foto ergänzen.'); $other = User::factory()->create(); $this->actingAs($other)->post("/kleinanzeigen/{$listing->id}", $this->payload())->assertForbidden(); $this->actingAs($other)->get("/kleinanzeigen/{$listing->id}/bearbeiten")->assertForbidden(); } public function test_contact_by_mail_without_chat_hides_the_address(): void { Mail::fake(); $seller = User::factory()->create(['email' => 'verkauf@example.org']); $buyer = User::factory()->create(['email' => 'kauf@example.org']); $listing = Listing::query()->create([...collect($this->payload())->except('price')->all(), 'user_id' => $seller->id, 'status' => 'active', 'published_at' => now(), 'expires_at' => now()->addDays(10)]); $this->actingAs($buyer)->get("/kleinanzeigen/{$listing->id}")->assertInertia(fn (Assert $p) => $p->where('listing.contact', 'mail')->missing('listing.seller.email')); $this->actingAs($buyer)->post("/kleinanzeigen/{$listing->id}/kontakt", ['message' => 'Ist das Rad noch da?'])->assertSessionHas('status'); Mail::assertQueued(ContactMessage::class, fn (ContactMessage $m) => $m->hasTo('verkauf@example.org') && $m->hasReplyTo('kauf@example.org')); $this->actingAs($seller)->post("/kleinanzeigen/{$listing->id}/kontakt", ['message' => 'selbst'])->assertStatus(422); } public function test_favorites_reports_and_housekeeping(): void { Notification::fake(); $seller = User::factory()->create(); $user = User::factory()->create(); $soon = Listing::query()->create([...collect($this->payload())->except('price')->all(), 'user_id' => $seller->id, 'status' => 'active', 'published_at' => now()->subDays(28), 'expires_at' => now()->addDays(2)]); $old = Listing::query()->create([...collect($this->payload(['title' => 'Alt']))->except('price')->all(), 'user_id' => $seller->id, 'status' => 'active', 'published_at' => now()->subDays(40), 'expires_at' => now()->subDay()]); $this->actingAs($user)->post("/kleinanzeigen/{$soon->id}/merken"); $this->actingAs($user)->get('/kleinanzeigen?merkliste=1')->assertInertia(fn (Assert $p) => $p->has('listings', 1)->where('listings.0.favorite', true)); $this->actingAs($user)->post("/kleinanzeigen/{$soon->id}/melden", ['reason' => 'Doppelt eingestellt']); Notification::assertSentTo($this->moderator, PortalNotification::class, fn ($n) => str_contains($n->title, 'gemeldet')); $this->actingAs($this->moderator)->get('/kleinanzeigen/moderation')->assertInertia(fn (Assert $p) => $p->has('reported', 1)->where('reported.0.reasons.0', 'Doppelt eingestellt')); app(Market::class)->housekeeping(); $this->assertSame('expired', $old->fresh()->status); $this->assertNotNull($soon->fresh()->reminded_at); Notification::assertSentTo($seller, PortalNotification::class, fn ($n) => str_contains($n->title, 'läuft bald ab')); $this->actingAs($seller)->post("/kleinanzeigen/{$old->id}/verlaengern"); $this->assertSame('active', $old->fresh()->status); $this->assertTrue($old->fresh()->expires_at->isAfter(now()->addDays(29))); } public function test_app_api_with_token(): void { Notification::fake(); Mail::fake(); $seller = User::factory()->create(); $buyer = User::factory()->create(); Sanctum::actingAs($seller); $id = $this->post('/api/v1/kleinanzeigen', [...$this->payload(), 'photos' => [UploadedFile::fake()->image('rad.jpg', 800, 600)]], ['Accept' => 'application/json']) ->assertCreated()->json('id'); $this->getJson('/api/v1/kleinanzeigen/meine')->assertJsonPath('data.0.status', 'pending'); $this->getJson("/api/v1/kleinanzeigen/{$id}/bearbeiten")->assertJsonPath('data.price', '45,50'); Sanctum::actingAs($buyer); $this->getJson("/api/v1/kleinanzeigen/{$id}")->assertNotFound(); $this->getJson('/api/v1/kleinanzeigen')->assertJsonCount(0, 'data'); app(Market::class)->approve(Listing::query()->findOrFail($id), $this->moderator); $thumb = $this->getJson('/api/v1/kleinanzeigen?q=fahrrad')->assertJsonCount(1, 'data')->json('data.0.thumb_url'); $this->assertStringContainsString('/api/v1/kleinanzeigen/bilder/', $thumb); $this->get($thumb)->assertOk(); $this->getJson("/api/v1/kleinanzeigen/{$id}")->assertJsonPath('data.contact', 'mail')->assertJsonPath('data.seller.id', $seller->id); $this->postJson("/api/v1/kleinanzeigen/{$id}/merken")->assertJsonPath('favorite', true); $this->getJson('/api/v1/kleinanzeigen?merkliste=1')->assertJsonCount(1, 'data'); $this->postJson("/api/v1/kleinanzeigen/{$id}/kontakt", ['message' => 'Noch zu haben?'])->assertOk()->assertJsonPath('via', 'mail'); Mail::assertQueued(ContactMessage::class); $this->postJson("/api/v1/kleinanzeigen/{$id}/reserviert")->assertForbidden(); Sanctum::actingAs($seller); $this->postJson("/api/v1/kleinanzeigen/{$id}/reserviert")->assertJsonPath('data.reserved', true); $this->post("/api/v1/kleinanzeigen/{$id}", $this->payload(['title' => 'Kinderfahrrad 24 Zoll']), ['Accept' => 'application/json']) ->assertJsonPath('message', 'Gespeichert – die Änderungen werden geprüft.'); $this->postJson("/api/v1/kleinanzeigen/{$id}/erledigt")->assertJsonPath('data.status', 'done'); $this->deleteJson("/api/v1/kleinanzeigen/{$id}")->assertOk(); $this->assertNull(Listing::query()->find($id)); } }