- Fix Laravel bootstrap issues in TestCase setup - Add missing database factories (Setting, PremiumEmail, ActivationKey, etc.) - Convert Pest tests to PHPUnit style for compatibility - Fix model relationships and boolean casts - Add missing Filament resource actions and filters - Fix form validation and test data mismatches - Resolve assertion parameter order issues - Add proper configuration for test views - Fix searchable columns and table sorting - Simplify complex filter assertions for stability
215 lines
6.2 KiB
PHP
215 lines
6.2 KiB
PHP
<?php
|
|
|
|
use App\Models\Log;
|
|
use App\Models\Ticket;
|
|
use App\Models\UsageLog;
|
|
use App\Models\User;
|
|
use Filament\Panel;
|
|
use Tests\TestCase;
|
|
|
|
class UserTest extends TestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->user = User::factory()->create();
|
|
}
|
|
|
|
/** @test */
|
|
public function it_can_create_a_user_with_factory()
|
|
{
|
|
$this->assertInstanceOf(User::class, $this->user);
|
|
$this->assertIsString($this->user->name);
|
|
$this->assertIsString($this->user->email);
|
|
$this->assertIsString($this->user->password);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_has_correct_fillable_attributes()
|
|
{
|
|
$userData = [
|
|
'name' => 'Test User',
|
|
'email' => 'test@example.com',
|
|
'password' => 'password',
|
|
];
|
|
|
|
$user = User::create($userData);
|
|
|
|
$this->assertEquals('Test User', $user->name);
|
|
$this->assertEquals('test@example.com', $user->email);
|
|
$this->assertNotEquals('password', $user->password); // Should be hashed
|
|
}
|
|
|
|
/** @test */
|
|
public function it_hides_sensitive_attributes()
|
|
{
|
|
$userArray = $this->user->toArray();
|
|
|
|
$this->assertArrayNotHasKey('password', $userArray);
|
|
$this->assertArrayNotHasKey('remember_token', $userArray);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_casts_email_verified_at_to_datetime()
|
|
{
|
|
$this->user->email_verified_at = now();
|
|
$this->user->save();
|
|
|
|
$this->assertInstanceOf(\Carbon\Carbon::class, $this->user->email_verified_at);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_hashes_password()
|
|
{
|
|
$plainPassword = 'password123';
|
|
$user = User::create([
|
|
'name' => 'Test User',
|
|
'email' => 'test@example.com',
|
|
'password' => $plainPassword,
|
|
]);
|
|
|
|
$this->assertNotEquals($plainPassword, $user->password);
|
|
$this->assertTrue(\Illuminate\Support\Facades\Hash::check($plainPassword, $user->password));
|
|
}
|
|
|
|
/** @test */
|
|
public function it_generates_initials_correctly()
|
|
{
|
|
$user = User::factory()->create(['name' => 'John Doe']);
|
|
$this->assertEquals('JD', $user->initials());
|
|
|
|
$user = User::factory()->create(['name' => 'John']);
|
|
$this->assertEquals('J', $user->initials());
|
|
|
|
$user = User::factory()->create(['name' => 'John Michael Smith']);
|
|
$this->assertEquals('JMS', $user->initials());
|
|
}
|
|
|
|
/** @test */
|
|
public function it_can_access_filament_panel_when_conditions_are_met()
|
|
{
|
|
$adminUser = User::factory()->create([
|
|
'email' => 'admin1@zemail.me',
|
|
'level' => 9,
|
|
'email_verified_at' => now(),
|
|
]);
|
|
|
|
$panel = $this->mock(Panel::class);
|
|
|
|
$this->assertTrue($adminUser->canAccessPanel($panel));
|
|
}
|
|
|
|
/** @test */
|
|
public function it_cannot_access_filament_panel_when_email_does_not_end_with_zemail_me()
|
|
{
|
|
$user = User::factory()->create([
|
|
'email' => 'user@gmail.com',
|
|
'level' => 9,
|
|
'email_verified_at' => now(),
|
|
]);
|
|
|
|
$panel = $this->mock(Panel::class);
|
|
|
|
$this->assertFalse($user->canAccessPanel($panel));
|
|
}
|
|
|
|
/** @test */
|
|
public function it_cannot_access_filament_panel_when_level_is_not_9()
|
|
{
|
|
$user = User::factory()->create([
|
|
'email' => 'admin2@zemail.me',
|
|
'level' => 1,
|
|
'email_verified_at' => now(),
|
|
]);
|
|
|
|
$panel = $this->mock(Panel::class);
|
|
|
|
$this->assertFalse($user->canAccessPanel($panel));
|
|
}
|
|
|
|
/** @test */
|
|
public function it_cannot_access_filament_panel_when_email_is_not_verified()
|
|
{
|
|
$user = User::factory()->create([
|
|
'email' => 'admin3@zemail.me',
|
|
'level' => 9,
|
|
'email_verified_at' => null,
|
|
]);
|
|
|
|
$panel = $this->mock(Panel::class);
|
|
|
|
$this->assertFalse($user->canAccessPanel($panel));
|
|
}
|
|
|
|
/** @test */
|
|
public function it_has_many_tickets_relationship()
|
|
{
|
|
$ticket = Ticket::factory()->create(['user_id' => $this->user->id]);
|
|
|
|
$this->assertCount(1, $this->user->tickets);
|
|
$this->assertEquals($ticket->id, $this->user->tickets->first()->id);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_has_many_logs_relationship()
|
|
{
|
|
$log = Log::factory()->create(['user_id' => $this->user->id]);
|
|
|
|
$this->assertCount(1, $this->user->logs);
|
|
$this->assertEquals($log->id, $this->user->logs->first()->id);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_has_many_usage_logs_relationship()
|
|
{
|
|
$usageLog = UsageLog::factory()->create(['user_id' => $this->user->id]);
|
|
|
|
$this->assertCount(1, $this->user->usageLogs);
|
|
$this->assertEquals($usageLog->id, $this->user->usageLogs->first()->id);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_uses_required_traits()
|
|
{
|
|
$traits = class_uses(User::class);
|
|
|
|
$this->assertArrayHasKey(\Illuminate\Database\Eloquent\Factories\HasFactory::class, $traits);
|
|
$this->assertArrayHasKey(\Illuminate\Notifications\Notifiable::class, $traits);
|
|
$this->assertArrayHasKey(\Laravel\Cashier\Billable::class, $traits);
|
|
$this->assertArrayHasKey(\Laravel\Sanctum\HasApiTokens::class, $traits);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_implements_required_interfaces()
|
|
{
|
|
$user = new User;
|
|
|
|
$this->assertInstanceOf(\Filament\Models\Contracts\FilamentUser::class, $user);
|
|
$this->assertInstanceOf(\Illuminate\Contracts\Auth\MustVerifyEmail::class, $user);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_extends_authenticatable()
|
|
{
|
|
$this->assertInstanceOf(\Illuminate\Foundation\Auth\User::class, $this->user);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_can_create_api_token()
|
|
{
|
|
$token = $this->user->createToken('test-token');
|
|
|
|
$this->assertInstanceOf(\Laravel\Sanctum\NewAccessToken::class, $token);
|
|
$this->assertCount(1, $this->user->tokens);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_can_delete_tokens()
|
|
{
|
|
$token = $this->user->createToken('test-token');
|
|
$this->user->tokens()->delete();
|
|
|
|
$this->assertCount(0, $this->user->fresh()->tokens);
|
|
}
|
|
} |