Kleinanzeigen-Plugin 1.0.0

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-27 17:06:02 +00:00
commit f05c44733f
50 changed files with 3854 additions and 0 deletions

152
tests/KleinanzeigenTest.php Normal file
View file

@ -0,0 +1,152 @@
<?php
namespace Tests\Plugins\Kleinanzeigen;
use App\Models\Role;
use App\Models\User;
use App\Notifications\PortalNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Kleinanzeigen\Mail\ContactMessage;
use Inhaus\Plugins\Kleinanzeigen\Market;
use Inhaus\Plugins\Kleinanzeigen\Models\Listing;
use Tests\TestCase;
/**
* Läuft innerhalb einer Inhaus-Installation, in der das Plugin unter plugins/kleinanzeigen liegt:
* vendor-dev/bin/phpunit plugins/kleinanzeigen/tests
*/
class KleinanzeigenTest extends TestCase
{
use RefreshDatabase;
private ?string $previousState = null;
private User $moderator;
protected function setUp(): void
{
// Plugin aktivieren, bevor die Anwendung startet – so laufen seine Migrationen mit
$file = getcwd().'/storage/app/plugins.json';
$this->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)));
}
}