317 lines
12 KiB
PHP
317 lines
12 KiB
PHP
<?php
|
|
|
|
namespace Tests\Integration;
|
|
|
|
use App\Domain\DomainContext;
|
|
use App\Domain\DomainType;
|
|
use App\Models\UserShop;
|
|
use App\Models\User;
|
|
use App\Services\DomainSessionManager;
|
|
use App\Services\OptimizedDomainService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Domain Session Integration Tests
|
|
*
|
|
* Testet das Session-Management zwischen verschiedenen Domains
|
|
*/
|
|
class DomainSessionIntegrationTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private DomainSessionManager $sessionManager;
|
|
private OptimizedDomainService $domainService;
|
|
private UserShop $testUserShop;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->sessionManager = new DomainSessionManager();
|
|
$this->domainService = new OptimizedDomainService(config('domains'));
|
|
|
|
// Test UserShop erstellen
|
|
$user = User::factory()->create([
|
|
'payment_shop' => now()->addMonths(1),
|
|
]);
|
|
|
|
$this->testUserShop = UserShop::factory()->create([
|
|
'slug' => 'testberater',
|
|
'user_id' => $user->id,
|
|
'active' => true,
|
|
'name' => 'Test Berater Shop',
|
|
]);
|
|
}
|
|
|
|
public function test_user_shop_session_synchronization(): void
|
|
{
|
|
// UserShop-Domain Context erstellen
|
|
$context = DomainContext::create(
|
|
DomainType::USER_SHOP,
|
|
'testberater.mivita.test',
|
|
'testberater',
|
|
$this->testUserShop
|
|
);
|
|
|
|
// Session-Synchronisation ausführen
|
|
$this->sessionManager->syncUserShopToSession($context);
|
|
|
|
// Prüfen, ob UserShop in Session gespeichert wurde
|
|
$this->assertNotNull(Session::get('user_shop'));
|
|
$this->assertEquals($this->testUserShop->id, Session::get('user_shop')->id);
|
|
$this->assertEquals('testberater.mivita.test', Session::get('user_shop_domain'));
|
|
}
|
|
|
|
public function test_session_preservation_across_domains(): void
|
|
{
|
|
// 1. Zuerst auf UserShop-Domain
|
|
$userShopContext = DomainContext::create(
|
|
DomainType::USER_SHOP,
|
|
'testberater.mivita.test',
|
|
'testberater',
|
|
$this->testUserShop
|
|
);
|
|
|
|
$this->sessionManager->syncUserShopToSession($userShopContext);
|
|
$originalUserShopId = Session::get('user_shop')->id;
|
|
|
|
// 2. Dann zu CRM wechseln (sollte UserShop erhalten)
|
|
$crmContext = DomainContext::create(
|
|
DomainType::CRM,
|
|
'my.mivita.test',
|
|
'my'
|
|
);
|
|
|
|
$this->sessionManager->syncUserShopToSession($crmContext);
|
|
|
|
// UserShop-Daten sollten erhalten bleiben
|
|
$this->assertNotNull(Session::get('user_shop'));
|
|
$this->assertEquals($originalUserShopId, Session::get('user_shop')->id);
|
|
$this->assertEquals('my.mivita.test', Session::get('user_shop_domain'));
|
|
}
|
|
|
|
public function test_session_clearing_for_main_domain(): void
|
|
{
|
|
// Zuerst UserShop in Session setzen
|
|
$userShopContext = DomainContext::create(
|
|
DomainType::USER_SHOP,
|
|
'testberater.mivita.test',
|
|
'testberater',
|
|
$this->testUserShop
|
|
);
|
|
|
|
$this->sessionManager->syncUserShopToSession($userShopContext);
|
|
$this->assertNotNull(Session::get('user_shop'));
|
|
|
|
// Dann zur Hauptdomain wechseln
|
|
$mainContext = DomainContext::create(
|
|
DomainType::MAIN,
|
|
'mivita.test'
|
|
);
|
|
|
|
$this->sessionManager->syncUserShopToSession($mainContext);
|
|
|
|
// UserShop-Daten sollten entfernt werden
|
|
$this->assertNull(Session::get('user_shop'));
|
|
$this->assertNull(Session::get('user_shop_domain'));
|
|
}
|
|
|
|
public function test_checkout_domain_preserves_all_session_data(): void
|
|
{
|
|
// UserShop-Session aufbauen
|
|
$userShopContext = DomainContext::create(
|
|
DomainType::USER_SHOP,
|
|
'testberater.mivita.test',
|
|
'testberater',
|
|
$this->testUserShop
|
|
);
|
|
|
|
$this->sessionManager->syncUserShopToSession($userShopContext);
|
|
|
|
// Zusätzliche Session-Daten hinzufügen (Warenkorb simulation)
|
|
Session::put('cart', ['product1', 'product2']);
|
|
Session::put('language', 'de');
|
|
|
|
// Zu Checkout wechseln
|
|
$checkoutContext = DomainContext::create(
|
|
DomainType::CHECKOUT,
|
|
'checkout.mivita.test'
|
|
);
|
|
|
|
$this->sessionManager->syncUserShopToSession($checkoutContext);
|
|
|
|
// Alle Session-Daten sollten erhalten bleiben
|
|
$this->assertNotNull(Session::get('user_shop'));
|
|
$this->assertEquals(['product1', 'product2'], Session::get('cart'));
|
|
$this->assertEquals('de', Session::get('language'));
|
|
$this->assertEquals('checkout.mivita.test', Session::get('user_shop_domain'));
|
|
}
|
|
|
|
public function test_portal_domain_session_handling(): void
|
|
{
|
|
// UserShop-Session aufbauen
|
|
$userShopContext = DomainContext::create(
|
|
DomainType::USER_SHOP,
|
|
'testberater.mivita.test',
|
|
'testberater',
|
|
$this->testUserShop
|
|
);
|
|
|
|
$this->sessionManager->syncUserShopToSession($userShopContext);
|
|
$originalUserShopId = Session::get('user_shop')->id;
|
|
|
|
// Zu Portal wechseln
|
|
$portalContext = DomainContext::create(
|
|
DomainType::PORTAL,
|
|
'in.mivita.test',
|
|
'in'
|
|
);
|
|
|
|
$this->sessionManager->syncUserShopToSession($portalContext);
|
|
|
|
// UserShop sollte erhalten bleiben (für "Zurück zum Shop" Funktion)
|
|
$this->assertNotNull(Session::get('user_shop'));
|
|
$this->assertEquals($originalUserShopId, Session::get('user_shop')->id);
|
|
$this->assertEquals('in.mivita.test', Session::get('user_shop_domain'));
|
|
}
|
|
|
|
public function test_session_preservation_decision_logic(): void
|
|
{
|
|
$userShopContext = DomainContext::create(DomainType::USER_SHOP, 'test.mivita.test', 'test');
|
|
$crmContext = DomainContext::create(DomainType::CRM, 'my.mivita.test', 'my');
|
|
$mainContext = DomainContext::create(DomainType::MAIN, 'mivita.test');
|
|
|
|
// Von UserShop zu CRM: Session erhalten
|
|
$this->assertTrue(
|
|
$this->sessionManager->shouldPreserveSessionData($userShopContext, $crmContext)
|
|
);
|
|
|
|
// Von UserShop zu Main: Session nicht erhalten
|
|
$this->assertFalse(
|
|
$this->sessionManager->shouldPreserveSessionData($userShopContext, $mainContext)
|
|
);
|
|
|
|
// Von CRM zu Portal: Session erhalten
|
|
$portalContext = DomainContext::create(DomainType::PORTAL, 'in.mivita.test', 'in');
|
|
$this->assertTrue(
|
|
$this->sessionManager->shouldPreserveSessionData($crmContext, $portalContext)
|
|
);
|
|
}
|
|
|
|
public function test_domain_context_storage_in_session(): void
|
|
{
|
|
$context = DomainContext::create(
|
|
DomainType::USER_SHOP,
|
|
'testberater.mivita.test',
|
|
'testberater',
|
|
$this->testUserShop
|
|
);
|
|
|
|
$this->sessionManager->storeDomainContextInSession($context);
|
|
|
|
$storedContext = Session::get('domain_context');
|
|
$this->assertNotNull($storedContext);
|
|
$this->assertEquals('user-shop', $storedContext['type']);
|
|
$this->assertEquals('testberater.mivita.test', $storedContext['host']);
|
|
$this->assertEquals('testberater', $storedContext['subdomain']);
|
|
$this->assertEquals($this->testUserShop->id, $storedContext['user_shop_id']);
|
|
}
|
|
|
|
public function test_domain_context_loading_from_session(): void
|
|
{
|
|
// Context in Session speichern
|
|
Session::put('domain_context', [
|
|
'type' => 'crm',
|
|
'host' => 'my.mivita.test',
|
|
'subdomain' => 'my',
|
|
'user_shop_id' => null,
|
|
'timestamp' => now()->toISOString(),
|
|
]);
|
|
|
|
$loadedContext = $this->sessionManager->loadDomainContextFromSession();
|
|
|
|
$this->assertNotNull($loadedContext);
|
|
$this->assertEquals(DomainType::CRM, $loadedContext->type);
|
|
$this->assertEquals('my.mivita.test', $loadedContext->host);
|
|
$this->assertEquals('my', $loadedContext->subdomain);
|
|
}
|
|
|
|
public function test_complete_domain_switching_workflow(): void
|
|
{
|
|
// 1. Start auf UserShop-Domain
|
|
$userShopContext = $this->domainService->resolveDomain('testberater.mivita.test');
|
|
|
|
// UserShop sollte geladen werden
|
|
$this->assertEquals(DomainType::USER_SHOP, $userShopContext->type);
|
|
$this->assertNotNull($userShopContext->userShop);
|
|
|
|
$request = Request::create('https://testberater.mivita.test');
|
|
$this->sessionManager->handleDomainSpecificSession($userShopContext, $request);
|
|
|
|
// Prüfen: UserShop in Session
|
|
$this->assertNotNull(Session::get('user_shop'));
|
|
$this->assertEquals($this->testUserShop->id, Session::get('user_shop')->id);
|
|
|
|
// 2. Wechsel zu CRM
|
|
$crmContext = $this->domainService->resolveDomain('my.mivita.test');
|
|
$crmRequest = Request::create('https://my.mivita.test');
|
|
$this->sessionManager->handleDomainSpecificSession($crmContext, $crmRequest);
|
|
|
|
// Prüfen: UserShop immer noch in Session, aber Domain aktualisiert
|
|
$this->assertNotNull(Session::get('user_shop'));
|
|
$this->assertEquals($this->testUserShop->id, Session::get('user_shop')->id);
|
|
$this->assertEquals('my.mivita.test', Session::get('user_shop_domain'));
|
|
|
|
// 3. Wechsel zu Checkout
|
|
$checkoutContext = $this->domainService->resolveDomain('checkout.mivita.test');
|
|
$checkoutRequest = Request::create('https://checkout.mivita.test');
|
|
$this->sessionManager->handleDomainSpecificSession($checkoutContext, $checkoutRequest);
|
|
|
|
// Prüfen: Alle Session-Daten erhalten
|
|
$this->assertNotNull(Session::get('user_shop'));
|
|
$this->assertEquals($this->testUserShop->id, Session::get('user_shop')->id);
|
|
$this->assertEquals('checkout.mivita.test', Session::get('user_shop_domain'));
|
|
|
|
// 4. Wechsel zur Hauptdomain
|
|
$mainContext = $this->domainService->resolveDomain('mivita.test');
|
|
$mainRequest = Request::create('https://mivita.test');
|
|
$this->sessionManager->handleDomainSpecificSession($mainContext, $mainRequest);
|
|
|
|
// Prüfen: UserShop-Session gelöscht
|
|
$this->assertNull(Session::get('user_shop'));
|
|
$this->assertNull(Session::get('user_shop_domain'));
|
|
}
|
|
|
|
public function test_fallback_shop_domain_handling(): void
|
|
{
|
|
// Aloevera UserShop für Fallback erstellen
|
|
$fallbackUser = User::factory()->create([
|
|
'payment_shop' => now()->addMonths(1),
|
|
]);
|
|
|
|
$fallbackShop = UserShop::factory()->create([
|
|
'slug' => 'aloevera',
|
|
'user_id' => $fallbackUser->id,
|
|
'active' => true,
|
|
'name' => 'Aloevera Fallback Shop',
|
|
]);
|
|
|
|
// Shop-Hauptdomain aufrufen
|
|
$shopContext = $this->domainService->resolveDomain('mivita.shop');
|
|
|
|
$this->assertEquals(DomainType::SHOP, $shopContext->type);
|
|
$this->assertNotNull($shopContext->userShop);
|
|
$this->assertEquals('aloevera', $shopContext->userShop->slug);
|
|
|
|
// Session-Management
|
|
$request = Request::create('https://mivita.shop');
|
|
$this->sessionManager->handleDomainSpecificSession($shopContext, $request);
|
|
|
|
// Fallback-Shop sollte in Session gesetzt werden
|
|
$this->assertNotNull(Session::get('user_shop'));
|
|
$this->assertEquals($fallbackShop->id, Session::get('user_shop')->id);
|
|
}
|
|
}
|