107 lines
3.8 KiB
PHP
107 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Requests\Admin;
|
|
|
|
use App\Enums\UserEnum;
|
|
use App\Http\Requests\Admin\UserRequest;
|
|
use App\Models\Medium;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Spatie\Permission\Models\Role;
|
|
use Tests\Concerns\ValidatesFormRequests;
|
|
use Tests\TestCase;
|
|
|
|
class UserRequestTest extends TestCase
|
|
{
|
|
use RefreshDatabase, ValidatesFormRequests;
|
|
|
|
protected function valid(array $overrides = []): array
|
|
{
|
|
return array_merge([
|
|
'phone' => '09120000000',
|
|
'password' => 'secret123',
|
|
'password_confirmation' => 'secret123',
|
|
'level' => UserEnum::Doctor->value,
|
|
], $overrides);
|
|
}
|
|
|
|
public function test_passes_with_required_fields_on_create(): void
|
|
{
|
|
$this->assertPasses(UserRequest::class, $this->valid());
|
|
}
|
|
|
|
public function test_passes_with_optional_fields_and_roles(): void
|
|
{
|
|
$role = Role::create(['name' => 'editor']);
|
|
|
|
$this->assertPasses(UserRequest::class, $this->valid([
|
|
'username' => 'ali',
|
|
'fullname' => 'علی',
|
|
'email' => 'ali@example.com',
|
|
'role' => [$role->id],
|
|
]));
|
|
}
|
|
|
|
public function test_fails_without_phone_password_or_level(): void
|
|
{
|
|
$errors = $this->formRequestErrors(UserRequest::class, []);
|
|
|
|
$this->assertArrayHasKey('phone', $errors);
|
|
$this->assertArrayHasKey('password', $errors);
|
|
$this->assertArrayHasKey('level', $errors);
|
|
}
|
|
|
|
public function test_fails_with_short_or_unconfirmed_password(): void
|
|
{
|
|
$this->assertFailsOn('password', UserRequest::class, $this->valid(['password' => 'short', 'password_confirmation' => 'short']));
|
|
$this->assertFailsOn('password', UserRequest::class, $this->valid(['password_confirmation' => 'different']));
|
|
}
|
|
|
|
public function test_fails_with_level_outside_admin_list(): void
|
|
{
|
|
$this->assertFailsOn('level', UserRequest::class, $this->valid(['level' => UserEnum::Vet->value]));
|
|
}
|
|
|
|
public function test_fails_with_duplicate_username_or_email(): void
|
|
{
|
|
User::factory()->create(['username' => 'taken', 'email' => 'taken@example.com']);
|
|
|
|
$this->assertFailsOn('username', UserRequest::class, $this->valid(['username' => 'taken']));
|
|
$this->assertFailsOn('email', UserRequest::class, $this->valid(['email' => 'taken@example.com']));
|
|
}
|
|
|
|
public function test_profile_url_must_match_existing_medium(): void
|
|
{
|
|
$medium = Medium::factory()->create(['url' => 'http://example.test/a.png']);
|
|
|
|
$this->assertPasses(UserRequest::class, $this->valid(['profile_url' => $medium->url]));
|
|
$this->assertFailsOn('profile_url', UserRequest::class, $this->valid(['profile_url' => 'http://example.test/missing.png']));
|
|
}
|
|
|
|
public function test_fails_with_invalid_phone_or_unknown_role(): void
|
|
{
|
|
$this->assertFailsOn('phone', UserRequest::class, $this->valid(['phone' => 'abc']));
|
|
$this->assertFailsOn('role', UserRequest::class, $this->valid(['role' => [999]]));
|
|
}
|
|
|
|
public function test_update_allows_own_username_and_email_and_optional_password(): void
|
|
{
|
|
$user = User::factory()->create(['username' => 'me', 'email' => 'me@example.com']);
|
|
|
|
$this->assertPasses(UserRequest::class, [
|
|
'username' => 'me',
|
|
'email' => 'me@example.com',
|
|
'phone' => '09120000000',
|
|
'level' => UserEnum::Doctor->value,
|
|
], [], null, ['user' => $user]);
|
|
}
|
|
|
|
public function test_update_still_rejects_another_users_username(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
User::factory()->create(['username' => 'other']);
|
|
|
|
$this->assertFailsOn('username', UserRequest::class, $this->valid(['username' => 'other']), [], null, ['user' => $user]);
|
|
}
|
|
}
|