inhaus-plugin-kleinanzeigen/tests/ChatContactTest.php
Friederich Loheide f05c44733f Kleinanzeigen-Plugin 1.0.0
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 17:06:02 +00:00

60 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Plugins\Kleinanzeigen;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Kleinanzeigen\Models\Listing;
use Tests\TestCase;
/**
* Kontakt über das Chat-Plugin – nur, wenn es in dieser Installation liegt (plugins/chat).
*/
class ChatContactTest extends TestCase
{
use RefreshDatabase;
private ?string $previousState = null;
protected function setUp(): void
{
$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' => true];
file_put_contents($file, json_encode($state));
parent::setUp();
if (! is_dir(base_path('plugins/chat'))) {
$this->markTestSkipped('Chat-Plugin nicht installiert.');
}
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();
}
public function test_contact_opens_a_chat_with_the_seller(): void
{
$seller = User::factory()->create();
$buyer = User::factory()->create();
$listing = Listing::query()->create(['user_id' => $seller->id, 'type' => 'give', 'category' => 'moebel', 'title' => 'Sofa', 'description' => 'Abholen in Mainz', 'status' => 'active', 'published_at' => now(), 'expires_at' => now()->addDays(5)]);
$this->actingAs($buyer)->get("/kleinanzeigen/{$listing->id}")->assertInertia(fn (Assert $p) => $p->where('listing.contact', 'chat'));
$location = $this->actingAs($buyer)->post("/kleinanzeigen/{$listing->id}/kontakt", ['message' => 'Kann ich es Samstag holen?'])->assertRedirect()->headers->get('Location');
$this->assertMatchesRegularExpression('#/chat/\d+$#', $location);
$this->actingAs($seller)->getJson('/chat/api/gespraeche/'.basename($location).'/nachrichten')->assertOk()
->assertJsonPath('messages.0.body', "Zu deiner Anzeige „Sofa“:\n".route('kleinanzeigen.show', $listing)."\n\nKann ich es Samstag holen?");
}
}