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

130 lines
4.0 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Package;
use App\Models\User;
use App\Models\Tenant;
use App\Models\TenantPackage;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use App\Mail\Welcome;
use Illuminate\Auth\Events\Registered;
class RegistrationTest extends TestCase
{
use RefreshDatabase;
public function test_registration_creates_user_and_tenant()
{
$freePackage = Package::factory()->create(['price' => 0]);
$response = $this->post(route('register.store'), [
'name' => 'Test User',
'username' => 'testuser',
'email' => 'test@example.com',
'password' => 'password',
'password_confirmation' => 'password',
'first_name' => 'Test',
'last_name' => 'User',
'address' => 'Test Address',
'phone' => '123456789',
'privacy_consent' => true,
'package_id' => $freePackage->id,
]);
$response->assertRedirect(route('verification.notice'));
$this->assertDatabaseHas('users', [
'username' => 'testuser',
'email' => 'test@example.com',
'first_name' => 'Test',
'last_name' => 'User',
'address' => 'Test Address',
'phone' => '123456789',
]);
$user = User::where('email', 'test@example.com')->first();
$this->assertNotNull($user->tenant);
$this->assertDatabaseHas('tenants', [
'user_id' => $user->id,
'name' => 'Test User',
]);
$this->assertDatabaseHas('tenant_packages', [
'tenant_id' => $user->tenant->id,
'package_id' => $freePackage->id,
]);
}
public function test_registration_without_package()
{
$response = $this->post(route('register.store'), [
'name' => 'Test User',
'username' => 'testuser2',
'email' => 'test2@example.com',
'password' => 'password',
'password_confirmation' => 'password',
'first_name' => 'Test',
'last_name' => 'User',
'address' => 'Test Address',
'phone' => '123456789',
'privacy_consent' => true,
]);
$response->assertRedirect(route('verification.notice'));
$user = User::where('email', 'test2@example.com')->first();
$this->assertNotNull($user->tenant);
$this->assertDatabaseMissing('tenant_packages', [
'tenant_id' => $user->tenant->id,
]);
}
public function test_registration_validation_fails()
{
$response = $this->post(route('register.store'), [
'name' => '',
'username' => '',
'email' => 'invalid',
'password' => 'short',
'password_confirmation' => 'different',
'first_name' => '',
'last_name' => '',
'address' => '',
'phone' => '',
'privacy_consent' => false,
]);
$response->assertSessionHasErrors([
'name', 'username', 'email', 'password', 'first_name', 'last_name', 'address', 'phone', 'privacy_consent',
]);
}
public function test_registered_event_sends_welcome_email()
{
Mail::fake();
$freePackage = Package::factory()->create(['price' => 0]);
$response = $this->post(route('register.store'), [
'name' => 'Test User',
'username' => 'testuser3',
'email' => 'test3@example.com',
'password' => 'password',
'password_confirmation' => 'password',
'first_name' => 'Test',
'last_name' => 'User',
'address' => 'Test Address',
'phone' => '123456789',
'privacy_consent' => true,
'package_id' => $freePackage->id,
]);
Mail::assertQueued(Welcome::class, function ($mail) {
return $mail->to[0]['address'] === 'test3@example.com';
});
}
}