migrant-web/tests/Feature/EmployerJobTest.php

366 lines
12 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use App\Models\JobPost;
use App\Models\JobApplication;
use App\Models\Worker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class EmployerJobTest extends TestCase
{
use RefreshDatabase;
protected $employer;
protected function setUp(): void
{
parent::setUp();
$this->employer = User::create([
'name' => 'Jane Sponsor',
'email' => 'sponsor@example.com',
'password' => bcrypt('password'),
'role' => 'employer',
]);
// Create an active premium subscription so Jane can post/view jobs
\Illuminate\Support\Facades\DB::table('subscriptions')->insert([
'user_id' => $this->employer->id,
'plan_id' => 'premium',
'amount_aed' => 199.00,
'starts_at' => now(),
'expires_at' => now()->addDays(30),
'paytabs_transaction_id' => 'TEST_TXN_ID',
'status' => 'active',
'created_at' => now(),
'updated_at' => now(),
]);
}
/**
* Test displaying the employer jobs list with basic plan.
*/
public function test_employer_with_basic_plan_cannot_view_jobs_list()
{
// Update subscription to basic
\Illuminate\Support\Facades\DB::table('subscriptions')
->where('user_id', $this->employer->id)
->update(['plan_id' => 'basic']);
$response = $this->actingAs($this->employer)
->withSession(['user' => $this->employer])
->get('/employer/jobs');
$response->assertRedirect('/employer/subscription');
$response->assertSessionHas('error');
}
/**
* Test displaying the employer jobs list with no subscription.
*/
public function test_employer_with_no_subscription_cannot_view_jobs_list()
{
// Delete subscription
\Illuminate\Support\Facades\DB::table('subscriptions')
->where('user_id', $this->employer->id)
->delete();
$response = $this->actingAs($this->employer)
->withSession(['user' => $this->employer])
->get('/employer/jobs');
$response->assertRedirect('/employer/subscription');
$response->assertSessionHas('error');
}
/**
* Test displaying the employer jobs list.
*/
public function test_employer_can_view_jobs_list()
{
$job = JobPost::create([
'employer_id' => $this->employer->id,
'title' => 'Experienced Mason',
'workers_needed' => 5,
'job_type' => 'Full Time',
'location' => 'Dubai Marina',
'salary' => 2800,
'start_date' => '2026-07-01',
'description' => 'Looking for experienced masons for a residential project.',
'requirements' => '5+ years experience, basic English.',
'status' => 'active',
]);
$response = $this->actingAs($this->employer)
->withSession(['user' => $this->employer])
->get('/employer/jobs');
$response->assertStatus(200);
$response->assertSee('Experienced Mason');
}
/**
* Test displaying the create job form.
*/
public function test_employer_can_view_create_job_form()
{
// Seed a custom skill in the database
\App\Models\Skill::create(['name' => 'painting']);
$response = $this->actingAs($this->employer)
->withSession(['user' => $this->employer])
->get('/employer/jobs/create');
$response->assertStatus(200);
$inertiaData = $response->original->getData();
$professions = $inertiaData['page']['props']['professions'] ?? [];
$this->assertContains('Painting', $professions);
}
/**
* Test storing a new job posting.
*/
public function test_employer_can_post_job()
{
$response = $this->actingAs($this->employer)
->withSession(['user' => $this->employer])
->post('/employer/jobs/create', [
'title' => 'Villa Cleaner',
'main_profession' => 'Cleaner',
'skills' => 'Housekeeping',
'workers_needed' => 2,
'job_type' => 'Part Time',
'location' => 'Al Barsha',
'salary' => 1800,
'start_date' => '2026-07-10',
'description' => 'Need part-time villa cleaners for daily housekeeping tasks.',
'requirements' => 'Housekeeping experience is a plus.',
]);
$response->assertRedirect('/employer/jobs');
$response->assertSessionHas('success');
$this->assertDatabaseHas('job_posts', [
'employer_id' => $this->employer->id,
'title' => 'Villa Cleaner',
'main_profession' => 'Cleaner',
'workers_needed' => 2,
'job_type' => 'Part Time',
'location' => 'Al Barsha',
'salary' => 1800,
]);
}
/**
* Test validation during job posting.
*/
public function test_job_posting_requires_fields()
{
$response = $this->actingAs($this->employer)
->withSession(['user' => $this->employer])
->post('/employer/jobs/create', [
'title' => '',
'workers_needed' => 0,
'salary' => -100,
]);
$response->assertSessionHasErrors(['title', 'main_profession', 'skills', 'workers_needed', 'location', 'salary', 'job_type', 'start_date', 'description']);
}
/**
* Test viewing job applicants.
*/
public function test_employer_can_view_applicants()
{
$job = JobPost::create([
'employer_id' => $this->employer->id,
'title' => 'Mason for Project',
'workers_needed' => 3,
'job_type' => 'Contract',
'location' => 'Sharjah',
'salary' => 3000,
'start_date' => '2026-07-15',
'description' => 'Contract mason project.',
'status' => 'active',
]);
$worker = Worker::create([
'name' => 'John Worker',
'email' => 'worker@example.com',
'phone' => '+971500000001',
'nationality' => 'Indian',
'age' => 30,
'salary' => 2500,
'availability' => 'Available',
'experience' => '4 Years',
'religion' => 'Christian',
'bio' => 'Vetted domestic worker with great experience.',
]);
$application = JobApplication::create([
'job_id' => $job->id,
'worker_id' => $worker->id,
'status' => 'applied',
]);
$response = $this->actingAs($this->employer)
->withSession(['user' => $this->employer])
->get("/employer/jobs/{$job->id}/applicants");
$response->assertStatus(200);
$response->assertSee('John Worker');
}
/**
* Test viewing all job applicants.
*/
public function test_employer_can_view_all_applicants()
{
$job = JobPost::create([
'employer_id' => $this->employer->id,
'title' => 'Mason for Project',
'workers_needed' => 3,
'job_type' => 'Contract',
'location' => 'Sharjah',
'salary' => 3000,
'start_date' => '2026-07-15',
'description' => 'Contract mason project.',
'status' => 'active',
]);
$worker = Worker::create([
'name' => 'John Worker',
'email' => 'worker@example.com',
'phone' => '+971500000001',
'nationality' => 'Indian',
'age' => 30,
'salary' => 2500,
'availability' => 'Available',
'experience' => '4 Years',
'religion' => 'Christian',
'bio' => 'Vetted domestic worker with great experience.',
]);
$application = JobApplication::create([
'job_id' => $job->id,
'worker_id' => $worker->id,
'status' => 'applied',
]);
$response = $this->actingAs($this->employer)
->withSession(['user' => $this->employer])
->get("/employer/jobs/all-applicants");
$response->assertStatus(200);
$response->assertSee('John Worker');
}
/**
* Test viewing shortlisted applicants.
*/
public function test_employer_can_view_shortlisted_applicants()
{
$job = JobPost::create([
'employer_id' => $this->employer->id,
'title' => 'Mason for Project',
'workers_needed' => 3,
'job_type' => 'Contract',
'location' => 'Sharjah',
'salary' => 3000,
'start_date' => '2026-07-15',
'description' => 'Contract mason project.',
'status' => 'active',
]);
$worker = Worker::create([
'name' => 'Shortlisted John',
'email' => 'worker_shortlisted@example.com',
'phone' => '+971500000002',
'nationality' => 'Indian',
'age' => 30,
'salary' => 2500,
'availability' => 'Available',
'experience' => '4 Years',
'religion' => 'Christian',
'bio' => 'Vetted domestic worker with great experience.',
]);
$application = JobApplication::create([
'job_id' => $job->id,
'worker_id' => $worker->id,
'status' => 'shortlisted',
]);
$response = $this->actingAs($this->employer)
->withSession(['user' => $this->employer])
->get("/employer/jobs/shortlisted");
$response->assertStatus(200);
$response->assertSee('Shortlisted John');
}
/**
* Test employer cannot edit a job posting that has active applications.
*/
public function test_employer_cannot_edit_job_with_applications()
{
$job = JobPost::create([
'employer_id' => $this->employer->id,
'title' => 'Job to Edit',
'main_profession' => 'Cleaner',
'workers_needed' => 1,
'job_type' => 'Full Time',
'location' => 'Dubai',
'salary' => 2000,
'start_date' => '2026-07-20',
'description' => 'Will have applications.',
'status' => 'active',
]);
$worker = Worker::create([
'name' => 'Applicant Worker',
'email' => 'worker2@example.com',
'phone' => '+971500000003',
'nationality' => 'Indian',
'age' => 28,
'salary' => 2200,
'availability' => 'Available',
'experience' => '3 Years',
'religion' => 'Christian',
'bio' => 'Ready to clean.',
]);
JobApplication::create([
'job_id' => $job->id,
'worker_id' => $worker->id,
'status' => 'applied',
]);
$response = $this->actingAs($this->employer)
->withSession(['user' => $this->employer])
->post("/employer/jobs/{$job->id}/edit", [
'title' => 'Trying to Edit Title',
'main_profession' => 'Cleaner',
'skills' => 'Housekeeping',
'workers_needed' => 1,
'location' => 'Dubai',
'salary' => 2500,
'job_type' => 'Full Time',
'start_date' => '2026-07-20',
'description' => 'Will have applications.',
'status' => 'active',
]);
$response->assertSessionHasErrors(['general']);
$this->assertDatabaseHas('job_posts', [
'id' => $job->id,
'title' => 'Job to Edit', // should NOT change
]);
}
}