Files
fotospiel-app/tests/Feature/Auth/RegistrationTest.php

236 lines
8.5 KiB
PHP

<?php
namespace Tests\Feature\Auth;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Models\User;
use App\Models\Package;
use App\Models\Tenant;
class RegistrationTest extends TestCase
{
use RefreshDatabase;
public function test_registration_screen_can_be_rendered()
{
$response = $this->get(route('register'));
$response->assertStatus(200);
}
public function test_new_users_can_register()
{
$response = $this->post('/de/register', [
'name' => 'Test User',
'username' => 'testuser',
'email' => 'test@example.com',
'password' => 'Password123!',
'password_confirmation' => 'Password123!',
'first_name' => 'Max',
'last_name' => 'Mustermann',
'address' => 'Musterstr. 1',
'phone' => '+49123456789',
'privacy_consent' => true,
]);
$this->assertAuthenticated();
$response->assertRedirect(route('dashboard', absolute: false));
$this->assertDatabaseHas('users', [
'email' => 'test@example.com',
]);
$this->assertDatabaseHas('tenants', [
'user_id' => User::latest()->first()->id,
]);
}
public function test_registration_with_free_package_assigns_tenant_package()
{
$freePackage = Package::factory()->endcustomer()->create(['price' => 0]);
$response = $this->post('/de/register', [
'name' => 'Test User',
'username' => 'testuserfree',
'email' => 'free@example.com',
'password' => 'Password123!',
'password_confirmation' => 'Password123!',
'first_name' => 'Max',
'last_name' => 'Mustermann',
'address' => 'Musterstr. 1',
'phone' => '+49123456789',
'privacy_consent' => true,
'package_id' => $freePackage->id,
]);
$this->assertAuthenticated();
$response->assertRedirect(route('dashboard', absolute: false));
$user = User::latest()->first();
$tenant = Tenant::where('user_id', $user->id)->first();
$this->assertDatabaseHas('tenant_packages', [
'tenant_id' => $tenant->id,
'package_id' => $freePackage->id,
'active' => true,
'price' => 0,
]);
$this->assertDatabaseHas('package_purchases', [
'tenant_id' => $tenant->id,
'package_id' => $freePackage->id,
'type' => 'endcustomer_event',
'price' => 0,
]);
$this->assertEquals('active', $tenant->subscription_status);
}
public function test_registration_with_paid_package_redirects_to_buy()
{
$paidPackage = Package::factory()->endcustomer()->create(['price' => 10]);
$response = $this->post('/de/register', [
'name' => 'Test User',
'username' => 'testuserpaid',
'email' => 'paid@example.com',
'password' => 'Password123!',
'password_confirmation' => 'Password123!',
'first_name' => 'Max',
'last_name' => 'Mustermann',
'address' => 'Musterstr. 1',
'phone' => '+49123456789',
'privacy_consent' => true,
'package_id' => $paidPackage->id,
]);
$this->assertAuthenticated();
$response->assertRedirect(route('buy.packages', $paidPackage->id));
$this->assertDatabaseHas('users', ['email' => 'paid@example.com']);
// Package not assigned yet
$this->assertDatabaseMissing('tenant_packages', ['package_id' => $paidPackage->id]);
}
public function test_registration_fails_with_invalid_email()
{
$response = $this->post('/de/register', [
'name' => 'Test User',
'username' => 'invaliduser',
'email' => 'invalid-email',
'password' => 'password',
'password_confirmation' => 'password',
'first_name' => 'Max',
'last_name' => 'Mustermann',
'address' => 'Musterstr. 1',
'phone' => '+49123456789',
'privacy_consent' => true,
]);
$response->assertStatus(302);
$response->assertSessionHasErrors(['email' => 'Das E-Mail muss eine gültige E-Mail-Adresse sein.']);
$this->assertDatabaseMissing('users', ['email' => 'invalid-email']);
}
public function test_registration_fails_with_short_password()
{
$response = $this->post('/de/register', [
'name' => 'Test User',
'username' => 'shortpass',
'email' => 'short@example.com',
'password' => 'short',
'password_confirmation' => 'short',
'first_name' => 'Max',
'last_name' => 'Mustermann',
'address' => 'Musterstr. 1',
'phone' => '+49123456789',
'privacy_consent' => true,
]);
$response->assertStatus(302);
$response->assertSessionHasErrors(['password' => 'Das Passwort muss mindestens 8 Zeichen lang sein.']);
$this->assertDatabaseMissing('users', ['email' => 'short@example.com']);
}
public function test_registration_fails_without_privacy_consent()
{
$response = $this->post('/de/register', [
'name' => 'Test User',
'username' => 'noconsent',
'email' => 'noconsent@example.com',
'password' => 'Password123!',
'password_confirmation' => 'Password123!',
'first_name' => 'Max',
'last_name' => 'Mustermann',
'address' => 'Musterstr. 1',
'phone' => '+49123456789',
'privacy_consent' => false,
]);
$response->assertStatus(302);
$response->assertSessionHasErrors(['privacy_consent' => 'Die Datenschutzbestätigung muss akzeptiert werden.']);
$this->assertDatabaseMissing('users', ['email' => 'noconsent@example.com']);
}
public function test_registration_fails_with_duplicate_email()
{
// Create existing user
User::factory()->create(['email' => 'duplicate@example.com']);
$response = $this->post('/de/register', [
'name' => 'Duplicate User',
'username' => 'duplicate',
'email' => 'duplicate@example.com',
'password' => 'Password123!',
'password_confirmation' => 'Password123!',
'first_name' => 'Max',
'last_name' => 'Mustermann',
'address' => 'Musterstr. 1',
'phone' => '+49123456789',
'privacy_consent' => true,
]);
$response->assertStatus(302);
$response->assertSessionHasErrors(['email' => 'Das E-Mail wurde bereits verwendet.']);
}
public function test_registration_fails_with_mismatched_passwords()
{
$response = $this->post('/de/register', [
'name' => 'Test User',
'username' => 'mismatch',
'email' => 'mismatch@example.com',
'password' => 'Password123!',
'password_confirmation' => 'different123!',
'first_name' => 'Max',
'last_name' => 'Mustermann',
'address' => 'Musterstr. 1',
'phone' => '+49123456789',
'privacy_consent' => true,
]);
$response->assertStatus(302);
$response->assertSessionHasErrors(['password' => 'Das Passwort-Feld-Bestätigung stimmt nicht überein.']);
$this->assertDatabaseMissing('users', ['email' => 'mismatch@example.com']);
}
public function test_registration_with_invalid_package_id_uses_fallback()
{
$response = $this->post('/de/register', [
'name' => 'Test User',
'username' => 'invalidpkg',
'email' => 'invalidpkg@example.com',
'password' => 'Password123!',
'password_confirmation' => 'Password123!',
'first_name' => 'Max',
'last_name' => 'Mustermann',
'address' => 'Musterstr. 1',
'phone' => '+49123456789',
'privacy_consent' => true,
'package_id' => 999, // Invalid ID
]);
$this->assertAuthenticated();
$response->assertRedirect(route('dashboard', absolute: false));
$this->assertDatabaseHas('users', ['email' => 'invalidpkg@example.com']);
// No package assigned
$user = User::latest()->first();
$tenant = Tenant::where('user_id', $user->id)->first();
$this->assertDatabaseMissing('tenant_packages', ['tenant_id' => $tenant->id]);
}
}