Files
jouwdriedee/tests/Feature/AuthAndEmailTest.php
T
2026-11-01 15:27:57 +01:00

220 lines
6.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Mail\OrderConfirmationMail;
use App\Mail\PasswordResetMail;
use App\Mail\WelcomeMail;
use App\Models\Category;
use App\Models\Material;
use App\Models\Product;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class AuthAndEmailTest extends TestCase
{
use RefreshDatabase;
public function test_customer_can_register_and_receives_welcome_email(): void
{
Mail::fake();
$response = $this->post(route('register.submit'), [
'name' => 'Sophie de Vries',
'email' => 'sophie@example.com',
'password' => 'VeiligWachtwoord123!',
'password_confirmation' => 'VeiligWachtwoord123!',
]);
$response->assertRedirect(route('account.index'));
$this->assertAuthenticated();
$this->assertDatabaseHas('users', [
'email' => 'sophie@example.com',
'name' => 'Sophie de Vries',
'is_admin' => false,
]);
Mail::assertSent(WelcomeMail::class, function ($mail) {
return $mail->hasTo('sophie@example.com')
&& str_contains($mail->envelope()->subject, 'Welkom bij Jouwdriedee.nl');
});
}
public function test_customer_can_login_with_valid_credentials(): void
{
$user = User::create([
'name' => 'Lars Bakker',
'email' => 'lars@example.com',
'password' => Hash::make('MijnGeheimeWachtwoord'),
'is_admin' => false,
]);
$response = $this->post(route('login.submit'), [
'email' => 'lars@example.com',
'password' => 'MijnGeheimeWachtwoord',
]);
$response->assertRedirect(route('account.index'));
$this->assertAuthenticatedAs($user);
}
public function test_admin_login_redirects_to_admin_content_dashboard(): void
{
$admin = User::create([
'name' => 'Jouwdriedee Beheer',
'email' => 'info@jouwdriedee.nl',
'password' => Hash::make('Jw3D!x9#QmK7$vR2'),
'is_admin' => true,
]);
$response = $this->post(route('login.submit'), [
'email' => 'info@jouwdriedee.nl',
'password' => 'Jw3D!x9#QmK7$vR2',
]);
$response->assertRedirect(route('admin.content.index'));
$this->assertAuthenticatedAs($admin);
}
public function test_login_fails_with_invalid_credentials(): void
{
User::create([
'name' => 'Lars Bakker',
'email' => 'lars@example.com',
'password' => Hash::make('MijnGeheimeWachtwoord'),
]);
$response = $this->post(route('login.submit'), [
'email' => 'lars@example.com',
'password' => 'FoutWachtwoord',
]);
$response->assertSessionHasErrors('email');
$this->assertGuest();
}
public function test_customer_can_request_password_reset_and_receives_email(): void
{
Mail::fake();
$user = User::create([
'name' => 'Emma Visser',
'email' => 'emma@example.com',
'password' => Hash::make('Wachtwoord123'),
]);
$response = $this->post(route('password.email'), [
'email' => 'emma@example.com',
]);
$response->assertSessionHas('success');
Mail::assertSent(PasswordResetMail::class, function ($mail) {
return $mail->hasTo('emma@example.com')
&& str_contains($mail->envelope()->subject, 'Wachtwoord opnieuw instellen');
});
$this->assertDatabaseHas('password_reset_tokens', [
'email' => 'emma@example.com',
]);
}
public function test_customer_can_reset_password_with_valid_token(): void
{
$user = User::create([
'name' => 'Emma Visser',
'email' => 'emma@example.com',
'password' => Hash::make('OudWachtwoord123'),
]);
$rawToken = 'test-secret-token-1234567890';
DB::table('password_reset_tokens')->insert([
'email' => 'emma@example.com',
'token' => Hash::make($rawToken),
'created_at' => now(),
]);
$response = $this->post(route('password.update'), [
'token' => $rawToken,
'email' => 'emma@example.com',
'password' => 'GloednieuwWachtwoord123!',
'password_confirmation' => 'GloednieuwWachtwoord123!',
]);
$response->assertRedirect(route('account.index'));
$this->assertAuthenticatedAs($user);
$user->refresh();
$this->assertTrue(Hash::check('GloednieuwWachtwoord123!', $user->password));
$this->assertDatabaseMissing('password_reset_tokens', [
'email' => 'emma@example.com',
]);
}
public function test_checkout_sends_order_confirmation_email(): void
{
Mail::fake();
Material::updateOrCreate(['code' => 'pla'], [
'name' => 'Bio-PLA+',
'code' => 'pla',
'price_multiplier' => 1.0,
'is_active' => true,
]);
$category = Category::create([
'name' => 'Fidgets',
'slug' => 'fidgets',
]);
$product = Product::create([
'name' => 'Kristaldraak Mini',
'slug' => 'kristaldraak-mini',
'category_id' => $category->id,
'base_price' => 15.00,
'description' => 'Test draakje',
'is_active' => true,
]);
// Put item in cart
session()->put('cart', [
'kristaldraak-mini-pla-rood' => [
'id' => $product->id,
'name' => 'Kristaldraak Mini',
'slug' => 'kristaldraak-mini',
'price' => 15.00,
'quantity' => 2,
'material' => 'pla',
'color' => 'Vurig Rood',
'scale' => '100%',
'infill' => '20%',
],
]);
$response = $this->post(route('checkout.store'), [
'customer_name' => 'Klant Tester',
'customer_email' => 'klant@test.nl',
'customer_phone' => '0612345678',
'shipping_street' => 'Hoofdstraat',
'shipping_number' => '42',
'shipping_postal_code' => '1000AA',
'shipping_city' => 'Utrecht',
'payment_method' => 'ideal',
]);
$response->assertRedirect();
Mail::assertSent(OrderConfirmationMail::class, function ($mail) {
return $mail->hasTo('klant@test.nl')
&& str_contains($mail->envelope()->subject, 'Bestelbevestiging');
});
}
}