mivita/dev/subdomain-optimization-grok/tests/SessionDomainSwitchingTest.php
2025-10-20 17:42:08 +02:00

252 lines
7.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\DomainType;
use App\Models\UserShop;
use App\Services\DomainService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Session;
use Tests\TestCase;
/**
* Session Domain Switching Test - Testet Session-Verhalten bei Domain-Wechseln
*/
class SessionDomainSwitchingTest extends TestCase
{
use RefreshDatabase;
private DomainService $domainService;
protected function setUp(): void
{
parent::setUp();
$this->domainService = app(DomainService::class);
}
/** @test */
public function it_preserves_user_shop_when_switching_from_shop_to_portal()
{
// UserShop erstellen
$userShop = UserShop::factory()->create([
'slug' => 'testshop',
'active' => true,
]);
$userShop->user()->create([
'payment_shop' => now()->addDays(30),
]);
// 1. UserShop-Domain aufrufen
$response = $this->get('http://testshop.mivita.care');
$response->assertStatus(200);
// UserShop sollte in Session sein
$this->assertEquals($userShop->id, Session::get('user_shop.id'));
// 2. Zu Portal wechseln (in.mivita.care)
$response = $this->get('http://in.mivita.care');
$response->assertStatus(200);
// UserShop sollte erhalten bleiben
$this->assertEquals($userShop->id, Session::get('user_shop.id'));
$this->assertEquals('testshop.mivita.care', Session::get('user_shop_domain'));
}
/** @test */
public function it_preserves_user_shop_when_switching_from_shop_to_checkout()
{
// UserShop erstellen
$userShop = UserShop::factory()->create([
'slug' => 'checkoutshop',
'active' => true,
]);
$userShop->user()->create([
'payment_shop' => now()->addDays(30),
]);
// 1. UserShop aufrufen
$this->get('http://checkoutshop.mivita.care');
// 2. Zu Checkout wechseln
$this->get('http://checkout.mivita.care');
// UserShop sollte erhalten bleiben
$this->assertEquals($userShop->id, Session::get('user_shop.id'));
}
/** @test */
public function it_clears_user_shop_when_switching_to_main_domain()
{
// UserShop erstellen
$userShop = UserShop::factory()->create([
'slug' => 'mainshop',
'active' => true,
]);
$userShop->user()->create([
'payment_shop' => now()->addDays(30),
]);
// 1. UserShop aufrufen
$this->get('http://mainshop.mivita.care');
$this->assertEquals($userShop->id, Session::get('user_shop.id'));
// 2. Zu Haupt-Domain wechseln
$this->get('http://mivita.care');
// UserShop sollte entfernt sein
$this->assertNull(Session::get('user_shop'));
$this->assertNull(Session::get('user_shop_domain'));
}
/** @test */
public function it_handles_invalid_user_shop_gracefully()
{
// Inaktiver UserShop
$userShop = UserShop::factory()->create([
'slug' => 'inactiveshop',
'active' => false, // Inaktiv
]);
$userShop->user()->create([
'payment_shop' => now()->addDays(30),
]);
// UserShop-Domain aufrufen
$response = $this->get('http://inactiveshop.mivita.care');
// Sollte umgeleitet werden (unbekannte Domain)
$response->assertRedirect();
// Session sollte sauber sein
$this->assertNull(Session::get('user_shop'));
}
/** @test */
public function it_sets_correct_session_domain_for_different_domain_types()
{
// Haupt-Domain
$this->get('http://mivita.care');
$this->assertEquals('.mivita.care', config('session.domain'));
// Shop-Domain
$this->get('http://mivita.shop');
$this->assertEquals('.mivita.shop', config('session.domain'));
// CRM-Domain
$this->get('http://my.mivita.care');
$this->assertEquals('.mivita.care', config('session.domain'));
// Portal-Domain
$this->get('http://in.mivita.care');
$this->assertEquals('.mivita.care', config('session.domain'));
}
/** @test */
public function it_handles_session_domain_switching_correctly()
{
// Verschiedene Domains durchgehen
$domains = [
'http://mivita.care' => '.mivita.care',
'http://mivita.shop' => '.mivita.shop',
'http://my.mivita.care' => '.mivita.care',
'http://checkout.mivita.care' => '.mivita.care',
];
foreach ($domains as $url => $expectedDomain) {
$this->get($url);
$this->assertEquals(
$expectedDomain,
config('session.domain'),
"Session domain for {$url} should be {$expectedDomain}"
);
}
}
/** @test */
public function it_maintains_cart_data_across_domain_switches()
{
// UserShop erstellen
$userShop = UserShop::factory()->create([
'slug' => 'cartshop',
'active' => true,
]);
$userShop->user()->create([
'payment_shop' => now()->addDays(30),
]);
// 1. UserShop aufrufen und Warenkorb füllen
$this->get('http://cartshop.mivita.care');
Session::put('cart', ['item1' => ['quantity' => 2]]);
Session::put('cart_total', 50.00);
Session::save();
// 2. Zu Checkout wechseln
$this->get('http://checkout.mivita.care');
// Warenkorb sollte erhalten bleiben
$this->assertEquals(['item1' => ['quantity' => 2]], Session::get('cart'));
$this->assertEquals(50.00, Session::get('cart_total'));
$this->assertNotNull(Session::get('user_shop'));
}
/** @test */
public function it_handles_concurrent_sessions_correctly()
{
// Zwei verschiedene UserShops
$shop1 = UserShop::factory()->create(['slug' => 'shop1', 'active' => true]);
$shop2 = UserShop::factory()->create(['slug' => 'shop2', 'active' => true]);
$shop1->user()->create(['payment_shop' => now()->addDays(30)]);
$shop2->user()->create(['payment_shop' => now()->addDays(30)]);
// Erste Session: Shop1
$this->withSession(['user_shop' => $shop1])
->get('http://shop1.mivita.care');
$this->assertEquals($shop1->id, Session::get('user_shop.id'));
// Zweite Session: Shop2 (neue Session simulieren)
Session::flush(); // Session zurücksetzen
$this->withSession(['user_shop' => $shop2])
->get('http://shop2.mivita.care');
$this->assertEquals($shop2->id, Session::get('user_shop.id'));
}
/** @test */
public function it_logs_domain_changes_correctly()
{
// UserShop erstellen
$userShop = UserShop::factory()->create([
'slug' => 'logshop',
'active' => true,
]);
$userShop->user()->create([
'payment_shop' => now()->addDays(30),
]);
// Domain-Wechsel durchführen
$this->get('http://logshop.mivita.care');
$this->get('http://in.mivita.care');
// Logs sollten Domain-Änderung enthalten
// (würde in der Praxis durch Log-Datei-Überprüfung getestet werden)
$this->assertTrue(true); // Placeholder für tatsächlichen Log-Test
}
/** @test */
public function it_handles_unknown_domains_with_proper_error_handling()
{
// Unbekannte Domain aufrufen
$response = $this->get('http://nonexistent.mivita.care');
// Sollte umgeleitet werden
$response->assertRedirect('http://mivita.care');
// Session sollte sauber bleiben
$this->assertNull(Session::get('user_shop'));
}
}