Files
alrahma_sunday_school_api/tests/Unit/Services/Auth/RegistrationServiceTest.php
T
2026-06-09 01:03:53 -04:00

153 lines
4.8 KiB
PHP

<?php
namespace Tests\Unit\Services\Auth;
use App\Models\Role;
use App\Models\User;
use App\Services\Auth\RegistrationCaptchaService;
use App\Services\Auth\RegistrationFormatterService;
use App\Services\Auth\RegistrationService;
use App\Services\EmailService;
use App\Services\PhoneFormatterService;
use App\Services\SchoolIdService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Mockery;
use Tests\TestCase;
class RegistrationServiceTest extends TestCase
{
use RefreshDatabase;
public function test_register_creates_parent_user(): void
{
DB::table('configuration')->insert([
['id' => 2001, 'config_key' => 'school_year', 'config_value' => '2025-2026'],
['id' => 2002, 'config_key' => 'semester', 'config_value' => 'Fall'],
]);
Role::query()->create([
'id' => 1,
'name' => 'parent',
'slug' => 'parent',
'description' => 'Parent',
'priority' => 1,
'is_active' => 1,
]);
$captchaService = new RegistrationCaptchaService();
$captcha = $captchaService->generate(4);
$emailService = Mockery::mock(EmailService::class);
$emailService->shouldReceive('send')->once()->andReturn(true);
$service = new RegistrationService(
$emailService,
new SchoolIdService(),
new RegistrationFormatterService(new PhoneFormatterService()),
$captchaService
);
$result = $service->register([
'firstname' => 'Parent',
'lastname' => 'User',
'gender' => 'Male',
'email' => 'parent@example.com',
'confirm_email' => 'parent@example.com',
'cellphone' => '5555555555',
'address_street' => '123 Main',
'apt' => 'A',
'city' => 'City',
'state' => 'CT',
'zip' => '12345',
'accept_school_policy' => 1,
'captcha' => $captcha,
'is_parent' => 1,
'no_second_parent_info' => 1,
]);
$this->assertTrue($result['ok']);
$this->assertDatabaseHas('users', [
'email' => 'parent@example.com',
'status' => 'Inactive',
]);
$this->assertDatabaseHas('user_roles', [
'user_id' => $result['user']->id,
'role_id' => 1,
]);
}
public function test_register_rejects_pending_activation(): void
{
DB::table('configuration')->insert([
['id' => 2001, 'config_key' => 'school_year', 'config_value' => '2025-2026'],
['id' => 2002, 'config_key' => 'semester', 'config_value' => 'Fall'],
]);
Role::query()->create([
'id' => 1,
'name' => 'parent',
'slug' => 'parent',
'description' => 'Parent',
'priority' => 1,
'is_active' => 1,
]);
User::query()->create([
'school_id' => '2500001',
'firstname' => 'Existing',
'lastname' => 'User',
'gender' => 'Male',
'cellphone' => '555-555-5555',
'email' => 'existing@example.com',
'address_street' => '123 Main',
'city' => 'City',
'state' => 'CT',
'zip' => '12345',
'accept_school_policy' => 1,
'is_verified' => 0,
'status' => 'Inactive',
'is_suspended' => 0,
'failed_attempts' => 0,
'password' => pbkdf2_hash('secret'),
'semester' => 'Fall',
'school_year' => '2025-2026',
'token' => 'token',
]);
$captchaService = new RegistrationCaptchaService();
$captcha = $captchaService->generate(4);
$emailService = Mockery::mock(EmailService::class);
$emailService->shouldNotReceive('send');
$service = new RegistrationService(
$emailService,
new SchoolIdService(),
new RegistrationFormatterService(new PhoneFormatterService()),
$captchaService
);
$result = $service->register([
'firstname' => 'Parent',
'lastname' => 'User',
'gender' => 'Male',
'email' => 'existing@example.com',
'confirm_email' => 'existing@example.com',
'cellphone' => '5555555555',
'address_street' => '123 Main',
'apt' => 'A',
'city' => 'City',
'state' => 'CT',
'zip' => '12345',
'accept_school_policy' => 1,
'captcha' => $captcha,
'is_parent' => 1,
'no_second_parent_info' => 1,
]);
$this->assertFalse($result['ok']);
$this->assertSame('pending_activation', $result['code']);
}
}