Rapport-Laravel/tests/Feature/NeblogisticreportController...

166 lines
6.0 KiB
PHP

<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class NeblogisticreportControllerTest extends TestCase
{
use RefreshDatabase, WithFaker;
protected function setUp(): void
{
parent::setUp();
// Create a test user with ABM role
$this->abmUser = User::factory()->create([
'roles' => json_encode(['1655449175-a39a5017-8232-4d58-NEBMITRAABM-90d06bc733d0'])
]);
// Create a test user without ABM role
$this->regularUser = User::factory()->create([
'roles' => json_encode(['regular-role'])
]);
}
public function test_abmapprove_requires_authentication()
{
$response = $this->get('/neblogisticreport/abmapprove?submission_id=TEST123');
$response->assertStatus(200);
$response->assertViewIs('neblogisticreport.abmapprove');
$response->assertViewHas('success', false);
$response->assertViewHas('message', 'Please login to perform this action');
}
public function test_abmapprove_requires_abm_role()
{
$this->actingAs($this->regularUser);
$response = $this->get('/neblogisticreport/abmapprove?submission_id=TEST123');
$response->assertStatus(200);
$response->assertViewIs('neblogisticreport.abmapprove');
$response->assertViewHas('success', false);
$response->assertViewHas('message', 'Access denied. Only ABM users can perform this action.');
}
public function test_abmapprove_requires_submission_id()
{
$this->actingAs($this->abmUser);
$response = $this->get('/neblogisticreport/abmapprove');
$response->assertStatus(200);
$response->assertViewIs('neblogisticreport.abmapprove');
$response->assertViewHas('success', false);
$response->assertViewHas('message', 'Submission ID is required');
}
public function test_abmapprove_validates_submission_id_format()
{
$this->actingAs($this->abmUser);
$response = $this->get('/neblogisticreport/abmapprove?submission_id=<script>alert("xss")</script>');
$response->assertStatus(200);
$response->assertViewIs('neblogisticreport.abmapprove');
$response->assertViewHas('success', false);
$response->assertViewHas('message', 'Invalid submission ID format.');
}
public function test_abmapprove_handles_nonexistent_record()
{
$this->actingAs($this->abmUser);
$response = $this->get('/neblogisticreport/abmapprove?submission_id=NONEXISTENT123');
$response->assertStatus(200);
$response->assertViewIs('neblogisticreport.abmapprove');
$response->assertViewHas('success', false);
$response->assertViewHas('message', 'Record not found with the provided submission ID');
}
public function test_abmapprove_successfully_updates_status()
{
$this->actingAs($this->abmUser);
// Create a test record in the neblogistic table
$submissionId = 'TEST123';
DB::table('neblogistic')->insert([
'submission_id' => $submissionId,
'status' => 'Pending',
'emp_name' => 'Test Employee',
'depot_name' => 'Test Depot',
'doctor_name' => 'Dr. Test',
'created_at' => now(),
'updated_at' => now()
]);
$response = $this->get('/neblogisticreport/abmapprove?submission_id=' . $submissionId);
$response->assertStatus(200);
$response->assertViewIs('neblogisticreport.abmapprove');
$response->assertViewHas('success', true);
$response->assertViewHas('message', 'Status successfully updated to L1 Approved for submission ID: ' . $submissionId);
$response->assertViewHas('previous_status', 'Pending');
$response->assertViewHas('new_status', 'L1 Approved');
// Verify the database was updated
$this->assertDatabaseHas('neblogistic', [
'submission_id' => $submissionId,
'status' => 'L1 Approved'
]);
}
public function test_abmapprove_prevents_double_approval()
{
$this->actingAs($this->abmUser);
// Create a test record that's already approved
$submissionId = 'TEST456';
DB::table('neblogistic')->insert([
'submission_id' => $submissionId,
'status' => 'L1 Approved',
'emp_name' => 'Test Employee',
'depot_name' => 'Test Depot',
'created_at' => now(),
'updated_at' => now()
]);
$response = $this->get('/neblogisticreport/abmapprove?submission_id=' . $submissionId);
$response->assertStatus(200);
$response->assertViewIs('neblogisticreport.abmapprove');
$response->assertViewHas('success', false);
$response->assertViewHas('message', 'Status is already L1 Approved');
}
public function test_abmapprove_prevents_invalid_status_transition()
{
$this->actingAs($this->abmUser);
// Create a test record with an invalid status for approval
$submissionId = 'TEST789';
DB::table('neblogistic')->insert([
'submission_id' => $submissionId,
'status' => 'Rejected',
'emp_name' => 'Test Employee',
'depot_name' => 'Test Depot',
'created_at' => now(),
'updated_at' => now()
]);
$response = $this->get('/neblogisticreport/abmapprove?submission_id=' . $submissionId);
$response->assertStatus(200);
$response->assertViewIs('neblogisticreport.abmapprove');
$response->assertViewHas('success', false);
$response->assertViewHas('message', 'Current status "Rejected" does not allow approval to L1 Approved');
}
}