update 20.10.2025

This commit is contained in:
Kevin Adametz 2025-10-20 17:42:08 +02:00
parent 8c11130b5d
commit a939cd51ef
616 changed files with 84821 additions and 4121 deletions

View file

@ -0,0 +1,188 @@
<?php
namespace Tests\Unit;
use App\Domain\DomainContext;
use App\Domain\DomainType;
use App\Services\DomainService;
use App\Models\UserShop;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
/**
* Domain Routing Test - Beispiel-Tests für das neue System
*/
class DomainRoutingTest extends TestCase
{
use RefreshDatabase;
private DomainService $domainService;
protected function setUp(): void
{
parent::setUp();
$this->domainService = app(DomainService::class);
}
/** @test */
public function it_resolves_main_domain_correctly()
{
$context = $this->domainService->resolveDomain('mivita.care');
$this->assertEquals(DomainType::MAIN, $context->type);
$this->assertEquals('mivita.care', $context->host);
$this->assertNull($context->subdomain);
$this->assertNull($context->userShop);
}
/** @test */
public function it_resolves_crm_domain_correctly()
{
$context = $this->domainService->resolveDomain('my.mivita.care');
$this->assertEquals(DomainType::CRM, $context->type);
$this->assertEquals('my.mivita.care', $context->host);
$this->assertEquals('my', $context->subdomain);
}
/** @test */
public function it_resolves_portal_domain_correctly()
{
$context = $this->domainService->resolveDomain('in.mivita.care');
$this->assertEquals(DomainType::PORTAL, $context->type);
$this->assertEquals('in.mivita.care', $context->host);
$this->assertEquals('in', $context->subdomain);
}
/** @test */
public function it_resolves_user_shop_domain_correctly()
{
// UserShop in DB erstellen
$userShop = UserShop::factory()->create([
'slug' => 'testshop',
'name' => 'Test Shop',
'active' => true,
]);
$userShop->user()->create([
'name' => 'Test User',
'email' => 'test@example.com',
'payment_shop' => now()->addDays(30),
]);
$context = $this->domainService->resolveDomain('testshop.mivita.care');
$this->assertEquals(DomainType::USER_SHOP, $context->type);
$this->assertEquals('testshop.mivita.care', $context->host);
$this->assertEquals('testshop', $context->subdomain);
$this->assertNotNull($context->userShop);
$this->assertEquals('testshop', $context->userShop->slug);
}
/** @test */
public function it_handles_unknown_domains_correctly()
{
$context = $this->domainService->resolveDomain('unknown.mivita.care');
$this->assertEquals(DomainType::UNKNOWN, $context->type);
$this->assertEquals('unknown.mivita.care', $context->host);
$this->assertEquals('unknown', $context->subdomain);
}
/** @test */
public function it_handles_invalid_user_shop_correctly()
{
// UserShop ohne aktive Zahlung
$userShop = UserShop::factory()->create([
'slug' => 'invalidshop',
'active' => true,
]);
$userShop->user()->create([
'name' => 'Test User',
'email' => 'test@example.com',
'payment_shop' => now()->subDays(1), // Abgelaufen
]);
$context = $this->domainService->resolveDomain('invalidshop.mivita.care');
$this->assertEquals(DomainType::UNKNOWN, $context->type);
}
/** @test */
public function it_validates_user_shop_slug_correctly()
{
$this->assertTrue($this->domainService->isValidUserShopSlug('validshop123'));
$this->assertFalse($this->domainService->isValidUserShopSlug('ab')); // Zu kurz
$this->assertFalse($this->domainService->isValidUserShopSlug('invalid-shop!')); // Ungültige Zeichen
}
/** @test */
public function it_builds_urls_correctly()
{
$url = $this->domainService->buildUrl('crm');
$this->assertEquals('https://my.mivita.care', $url);
$url = $this->domainService->buildUrl('user-shop', '/products', 'testshop');
$this->assertEquals('https://testshop.mivita.care/products', $url);
}
/** @test */
public function it_provides_debug_information()
{
$debug = $this->domainService->getDebugInfo('my.mivita.care');
$this->assertArrayHasKey('host', $debug);
$this->assertArrayHasKey('domain_info', $debug);
$this->assertArrayHasKey('determined_type', $debug);
$this->assertArrayHasKey('is_known', $debug);
$this->assertEquals('my.mivita.care', $debug['host']);
$this->assertEquals('crm', $debug['determined_type']);
}
/** @test */
public function domain_context_has_correct_string_representation()
{
$context = $this->domainService->resolveDomain('my.mivita.care');
$string = (string) $context;
$this->assertStringContains('DomainType: crm', $string);
$this->assertStringContains('Host: my.mivita.care', $string);
$this->assertStringContains('Subdomain: my', $string);
}
/** @test */
public function domain_context_to_array_works_correctly()
{
$context = $this->domainService->resolveDomain('my.mivita.care');
$array = $context->toArray();
$this->assertEquals('crm', $array['type']);
$this->assertEquals('my.mivita.care', $array['host']);
$this->assertEquals('my', $array['subdomain']);
$this->assertTrue($array['is_known']);
$this->assertFalse($array['is_user_shop']);
}
/** @test */
public function it_caches_domain_resolution()
{
// Erste Auflösung (uncached)
$start = microtime(true);
$context1 = $this->domainService->resolveDomain('cachetest.mivita.care');
$firstDuration = microtime(true) - $start;
// Zweite Auflösung (cached)
$start = microtime(true);
$context2 = $this->domainService->resolveDomain('cachetest.mivita.care');
$secondDuration = microtime(true) - $start;
// Gleicher Context
$this->assertEquals($context1->type, $context2->type);
$this->assertEquals($context1->host, $context2->host);
// Zweite sollte schneller sein (Cache-Hit)
$this->assertLessThan($firstDuration, $secondDuration);
}
}

View file

@ -0,0 +1,252 @@
<?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'));
}
}