222 lines
7.2 KiB
PHP
222 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\User;
|
|
use App\Models\EmployerProfile;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Tests\TestCase;
|
|
|
|
class EmployerProfileApiTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected $employer;
|
|
protected $token;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->token = 'test-employer-token';
|
|
$this->employer = User::create([
|
|
'name' => 'Original Name',
|
|
'email' => 'employer@example.com',
|
|
'password' => bcrypt('Password@123'),
|
|
'role' => 'employer',
|
|
'api_token' => $this->token,
|
|
]);
|
|
|
|
EmployerProfile::create([
|
|
'user_id' => $this->employer->id,
|
|
'company_name' => 'Original Company',
|
|
'phone' => '+971501112222',
|
|
'verification_status' => 'approved',
|
|
'language' => 'English',
|
|
'notifications' => true,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Test employer can retrieve profile.
|
|
*/
|
|
public function test_employer_can_get_profile()
|
|
{
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer ' . $this->token,
|
|
])->getJson('/api/employers/profile');
|
|
|
|
$response->assertStatus(200)
|
|
->assertJsonStructure([
|
|
'success',
|
|
'data' => [
|
|
'profile' => [
|
|
'name',
|
|
'email',
|
|
'company_name',
|
|
'phone',
|
|
'language',
|
|
'notifications',
|
|
'verification_status',
|
|
]
|
|
]
|
|
]);
|
|
|
|
$this->assertEquals('Original Name', $response->json('data.profile.name'));
|
|
$this->assertEquals('Original Company', $response->json('data.profile.company_name'));
|
|
}
|
|
|
|
/**
|
|
* Test employer can update profile details.
|
|
*/
|
|
public function test_employer_can_update_profile()
|
|
{
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer ' . $this->token,
|
|
])->postJson('/api/employers/profile/update', [
|
|
'name' => 'Updated Name',
|
|
'email' => 'employer_updated@example.com',
|
|
'phone' => '+971509999999',
|
|
'company_name' => 'Updated Company Corp',
|
|
'language' => 'Arabic',
|
|
'notifications' => false,
|
|
]);
|
|
|
|
$response->assertStatus(200)
|
|
->assertJsonStructure([
|
|
'success',
|
|
'message',
|
|
'data' => [
|
|
'profile' => [
|
|
'name',
|
|
'email',
|
|
'company_name',
|
|
'phone',
|
|
'language',
|
|
'notifications',
|
|
]
|
|
]
|
|
]);
|
|
|
|
$this->assertDatabaseHas('users', [
|
|
'id' => $this->employer->id,
|
|
'name' => 'Updated Name',
|
|
'email' => 'employer_updated@example.com',
|
|
]);
|
|
|
|
$this->assertDatabaseHas('employer_profiles', [
|
|
'user_id' => $this->employer->id,
|
|
'company_name' => 'Updated Company Corp',
|
|
'language' => 'Arabic',
|
|
'notifications' => false,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Test employer can update password when providing correct current password.
|
|
*/
|
|
public function test_employer_can_update_password()
|
|
{
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer ' . $this->token,
|
|
])->postJson('/api/employers/profile/update', [
|
|
'name' => 'Original Name',
|
|
'email' => 'employer@example.com',
|
|
'phone' => '+971501112222',
|
|
'company_name' => 'Original Company',
|
|
'language' => 'English',
|
|
'notifications' => true,
|
|
'current_password' => 'Password@123',
|
|
'new_password' => 'NewSecurePassword@123',
|
|
'new_password_confirmation' => 'NewSecurePassword@123',
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
// Verify password hash updated
|
|
$this->assertTrue(Hash::check('NewSecurePassword@123', $this->employer->fresh()->password));
|
|
}
|
|
|
|
/**
|
|
* Test employer password update fails with incorrect current password.
|
|
*/
|
|
public function test_employer_password_update_fails_with_incorrect_current_password()
|
|
{
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer ' . $this->token,
|
|
])->postJson('/api/employers/profile/update', [
|
|
'name' => 'Original Name',
|
|
'email' => 'employer@example.com',
|
|
'phone' => '+971501112222',
|
|
'company_name' => 'Original Company',
|
|
'language' => 'English',
|
|
'notifications' => true,
|
|
'current_password' => 'WrongCurrentPassword',
|
|
'new_password' => 'NewSecurePassword@123',
|
|
'new_password_confirmation' => 'NewSecurePassword@123',
|
|
]);
|
|
|
|
$response->assertStatus(422)
|
|
->assertJsonStructure([
|
|
'success',
|
|
'message',
|
|
'errors' => [
|
|
'current_password'
|
|
]
|
|
]);
|
|
|
|
// Verify password was NOT changed
|
|
$this->assertTrue(Hash::check('Password@123', $this->employer->fresh()->password));
|
|
}
|
|
|
|
/**
|
|
* Test employer dashboard returns only the latest 2 approved charity drives.
|
|
*/
|
|
public function test_employer_dashboard_returns_only_two_charity_drives()
|
|
{
|
|
// Create 5 approved announcements
|
|
for ($i = 1; $i <= 5; $i++) {
|
|
$ann = new \App\Models\Announcement([
|
|
'title' => "Charity Drive $i",
|
|
'body' => "Details of charity drive $i",
|
|
'type' => 'charity',
|
|
'status' => 'approved',
|
|
]);
|
|
$ann->created_at = now()->subMinutes(6 - $i);
|
|
$ann->save();
|
|
}
|
|
|
|
// Create 1 pending announcement
|
|
$pendingAnn = new \App\Models\Announcement([
|
|
'title' => "Pending Charity Drive",
|
|
'body' => "Details of pending charity drive",
|
|
'type' => 'charity',
|
|
'status' => 'pending',
|
|
]);
|
|
$pendingAnn->created_at = now();
|
|
$pendingAnn->save();
|
|
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer ' . $this->token,
|
|
])->getJson('/api/employers/dashboard');
|
|
|
|
$response->assertStatus(200)
|
|
->assertJson([
|
|
'success' => true,
|
|
]);
|
|
|
|
$announcements = $response->json('data.recent_announcements');
|
|
$this->assertCount(2, $announcements);
|
|
|
|
// Make sure it contains the latest 2 approved ones
|
|
$titles = collect($announcements)->pluck('title');
|
|
$this->assertTrue($titles->contains('Charity Drive 5'));
|
|
$this->assertTrue($titles->contains('Charity Drive 4'));
|
|
$this->assertFalse($titles->contains('Charity Drive 3'));
|
|
$this->assertFalse($titles->contains('Charity Drive 2'));
|
|
$this->assertFalse($titles->contains('Charity Drive 1'));
|
|
$this->assertFalse($titles->contains('Pending Charity Drive'));
|
|
}
|
|
}
|