Features: - Support login via phone number or email (LoginRequest) - Add members:import-roster command for Excel roster import - Merge survey emails with roster data Code Quality (Phase 1-4): - Add database locking for balance calculation - Add self-approval checks for finance workflow - Create service layer (FinanceDocumentApprovalService, PaymentVerificationService) - Add HasAccountingEntries and HasApprovalWorkflow traits - Create FormRequest classes for validation - Add status-badge component - Define authorization gates in AuthServiceProvider - Add accounting config file Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
347 lines
13 KiB
PHP
347 lines
13 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\FinanceDocument;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Financial Document Workflow Feature Tests
|
|
*
|
|
* Tests the complete financial document workflow including:
|
|
* - Amount-based routing (secretary → chair → board)
|
|
* - Multi-stage approval
|
|
* - Permission-based access control
|
|
*/
|
|
class FinanceDocumentWorkflowTest extends TestCase
|
|
{
|
|
use RefreshDatabase, WithFaker;
|
|
|
|
protected User $requester;
|
|
|
|
protected User $secretary;
|
|
|
|
protected User $chair;
|
|
|
|
protected User $boardMember;
|
|
|
|
protected User $cashier;
|
|
|
|
protected User $accountant;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->withoutMiddleware([\App\Http\Middleware\EnsureUserIsAdmin::class]);
|
|
|
|
Permission::findOrCreate('create_finance_document', 'web');
|
|
Permission::findOrCreate('view_finance_documents', 'web');
|
|
Permission::findOrCreate('approve_as_secretary', 'web');
|
|
Permission::findOrCreate('approve_as_chair', 'web');
|
|
Permission::findOrCreate('approve_board_meeting', 'web');
|
|
|
|
Role::firstOrCreate(['name' => 'admin']);
|
|
// Create roles for new workflow
|
|
Role::create(['name' => 'finance_requester']);
|
|
Role::create(['name' => 'secretary_general']);
|
|
Role::create(['name' => 'finance_chair']);
|
|
Role::create(['name' => 'finance_board_member']);
|
|
Role::create(['name' => 'finance_cashier']);
|
|
Role::create(['name' => 'finance_accountant']);
|
|
|
|
// Create test users
|
|
$this->requester = User::factory()->create(['email' => 'requester@test.com']);
|
|
$this->secretary = User::factory()->create(['email' => 'secretary@test.com']);
|
|
$this->chair = User::factory()->create(['email' => 'chair@test.com']);
|
|
$this->boardMember = User::factory()->create(['email' => 'board@test.com']);
|
|
$this->cashier = User::factory()->create(['email' => 'cashier@test.com']);
|
|
$this->accountant = User::factory()->create(['email' => 'accountant@test.com']);
|
|
|
|
// Assign roles
|
|
$this->requester->assignRole('admin');
|
|
$this->secretary->assignRole('admin');
|
|
$this->chair->assignRole('admin');
|
|
$this->boardMember->assignRole('admin');
|
|
$this->cashier->assignRole('admin');
|
|
$this->accountant->assignRole('admin');
|
|
|
|
$this->requester->assignRole('finance_requester');
|
|
$this->secretary->assignRole('secretary_general');
|
|
$this->chair->assignRole('finance_chair');
|
|
$this->boardMember->assignRole('finance_board_member');
|
|
$this->cashier->assignRole('finance_cashier');
|
|
$this->accountant->assignRole('finance_accountant');
|
|
|
|
// Give permissions
|
|
$this->requester->givePermissionTo('create_finance_document');
|
|
$this->secretary->givePermissionTo(['view_finance_documents', 'approve_as_secretary']);
|
|
$this->chair->givePermissionTo(['view_finance_documents', 'approve_as_chair']);
|
|
$this->boardMember->givePermissionTo('approve_board_meeting');
|
|
}
|
|
|
|
/** @test */
|
|
public function small_amount_workflow_completes_without_chair()
|
|
{
|
|
// Create a small amount document (< 5000)
|
|
$document = FinanceDocument::create([
|
|
'title' => 'Small Expense Reimbursement',
|
|
'description' => 'Test small expense',
|
|
'amount' => 3000,
|
|
'status' => FinanceDocument::STATUS_PENDING,
|
|
'submitted_by_user_id' => $this->requester->id,
|
|
'submitted_at' => now(),
|
|
]);
|
|
|
|
$document->amount_tier = $document->determineAmountTier();
|
|
$document->save();
|
|
|
|
$this->assertEquals('small', $document->amount_tier);
|
|
|
|
// Secretary approves (should complete workflow for small amounts)
|
|
$this->actingAs($this->secretary);
|
|
$document->status = FinanceDocument::STATUS_APPROVED_SECRETARY;
|
|
$document->approved_by_secretary_id = $this->secretary->id;
|
|
$document->secretary_approved_at = now();
|
|
$document->save();
|
|
|
|
$this->assertTrue($document->isApprovalComplete());
|
|
}
|
|
|
|
/** @test */
|
|
public function medium_amount_workflow_requires_chair_approval()
|
|
{
|
|
// Create a medium amount document (5000-50000)
|
|
$document = FinanceDocument::create([
|
|
'title' => 'Medium Purchase Request',
|
|
'description' => 'Test medium purchase',
|
|
'amount' => 25000,
|
|
'status' => FinanceDocument::STATUS_PENDING,
|
|
'submitted_by_user_id' => $this->requester->id,
|
|
'submitted_at' => now(),
|
|
]);
|
|
|
|
$document->amount_tier = $document->determineAmountTier();
|
|
$document->save();
|
|
|
|
$this->assertEquals('medium', $document->amount_tier);
|
|
$this->assertFalse($document->needsBoardMeetingApproval());
|
|
|
|
// Secretary approves
|
|
$document->status = FinanceDocument::STATUS_APPROVED_SECRETARY;
|
|
$document->approved_by_secretary_id = $this->secretary->id;
|
|
$document->secretary_approved_at = now();
|
|
$document->save();
|
|
|
|
$this->assertFalse($document->isApprovalComplete()); // Still needs chair
|
|
|
|
// Chair approves (should complete workflow for medium amounts)
|
|
$document->status = FinanceDocument::STATUS_APPROVED_CHAIR;
|
|
$document->approved_by_chair_id = $this->chair->id;
|
|
$document->chair_approved_at = now();
|
|
$document->save();
|
|
|
|
$this->assertTrue($document->isApprovalComplete());
|
|
}
|
|
|
|
/** @test */
|
|
public function large_amount_workflow_requires_board_meeting_approval()
|
|
{
|
|
// Create a large amount document (> 50000)
|
|
$document = FinanceDocument::create([
|
|
'title' => 'Large Capital Expenditure',
|
|
'description' => 'Test large expenditure',
|
|
'amount' => 75000,
|
|
'status' => FinanceDocument::STATUS_PENDING,
|
|
'submitted_by_user_id' => $this->requester->id,
|
|
'submitted_at' => now(),
|
|
]);
|
|
|
|
$document->amount_tier = $document->determineAmountTier();
|
|
$document->requires_board_meeting = $document->needsBoardMeetingApproval();
|
|
$document->save();
|
|
|
|
$this->assertEquals('large', $document->amount_tier);
|
|
$this->assertTrue($document->requires_board_meeting);
|
|
|
|
// Secretary approves
|
|
$document->status = FinanceDocument::STATUS_APPROVED_SECRETARY;
|
|
$document->approved_by_secretary_id = $this->secretary->id;
|
|
$document->secretary_approved_at = now();
|
|
$document->save();
|
|
|
|
$this->assertFalse($document->isApprovalComplete());
|
|
|
|
// Chair approves
|
|
$document->status = FinanceDocument::STATUS_APPROVED_CHAIR;
|
|
$document->approved_by_chair_id = $this->chair->id;
|
|
$document->chair_approved_at = now();
|
|
$document->save();
|
|
|
|
$this->assertFalse($document->isApprovalComplete()); // Still needs board
|
|
|
|
// Board approval
|
|
$document->status = FinanceDocument::STATUS_APPROVED_BOARD;
|
|
$document->board_meeting_approved_at = now();
|
|
$document->board_meeting_approved_by_id = $this->boardMember->id;
|
|
$document->save();
|
|
|
|
$this->assertTrue($document->isApprovalComplete());
|
|
}
|
|
|
|
/** @test */
|
|
public function requester_can_create_finance_document()
|
|
{
|
|
Storage::fake('local');
|
|
|
|
$this->actingAs($this->requester);
|
|
|
|
$file = UploadedFile::fake()->create('receipt.pdf', 100);
|
|
|
|
$response = $this->post(route('admin.finance.store'), [
|
|
'title' => 'Test Expense',
|
|
'description' => 'Test description',
|
|
'amount' => 5000,
|
|
'attachment' => $file,
|
|
]);
|
|
|
|
$response->assertRedirect();
|
|
$this->assertDatabaseHas('finance_documents', [
|
|
'title' => 'Test Expense',
|
|
'amount' => 5000,
|
|
'submitted_by_user_id' => $this->requester->id,
|
|
]);
|
|
}
|
|
|
|
/** @test */
|
|
public function cashier_cannot_approve_own_submission()
|
|
{
|
|
// Test using canBeApprovedBySecretary (secretary is first approval in new workflow)
|
|
$document = FinanceDocument::create([
|
|
'title' => 'Self Submitted',
|
|
'description' => 'Test',
|
|
'amount' => 1000,
|
|
'status' => FinanceDocument::STATUS_PENDING,
|
|
'submitted_by_user_id' => $this->secretary->id,
|
|
'submitted_at' => now(),
|
|
]);
|
|
|
|
// Secretary cannot approve their own submission
|
|
$this->assertFalse($document->canBeApprovedBySecretary($this->secretary));
|
|
}
|
|
|
|
/** @test */
|
|
public function accountant_cannot_approve_before_cashier()
|
|
{
|
|
// In new workflow: chair cannot approve before secretary
|
|
$document = FinanceDocument::create([
|
|
'title' => 'Pending Document',
|
|
'description' => 'Test',
|
|
'amount' => 25000, // Medium amount needs chair
|
|
'amount_tier' => FinanceDocument::AMOUNT_TIER_MEDIUM,
|
|
'status' => FinanceDocument::STATUS_PENDING,
|
|
'submitted_by_user_id' => $this->requester->id,
|
|
'submitted_at' => now(),
|
|
]);
|
|
|
|
// Chair cannot approve before secretary
|
|
$this->assertFalse($document->canBeApprovedByChair());
|
|
}
|
|
|
|
/** @test */
|
|
public function rejected_document_cannot_proceed()
|
|
{
|
|
$document = FinanceDocument::create([
|
|
'title' => 'Rejected Document',
|
|
'description' => 'Test',
|
|
'amount' => 1000,
|
|
'status' => FinanceDocument::STATUS_REJECTED,
|
|
'submitted_by_user_id' => $this->requester->id,
|
|
'submitted_at' => now(),
|
|
]);
|
|
|
|
$this->assertFalse($document->canBeApprovedBySecretary($this->secretary));
|
|
$this->assertFalse($document->canBeApprovedByChair());
|
|
$this->assertFalse($document->canBeApprovedByBoard());
|
|
}
|
|
|
|
/** @test */
|
|
public function workflow_stages_are_correctly_identified()
|
|
{
|
|
$document = FinanceDocument::create([
|
|
'title' => 'Test Document',
|
|
'description' => 'Test',
|
|
'amount' => 1000,
|
|
'status' => FinanceDocument::STATUS_PENDING,
|
|
'submitted_by_user_id' => $this->requester->id,
|
|
'submitted_at' => now(),
|
|
]);
|
|
|
|
$document->amount_tier = 'small';
|
|
$document->save();
|
|
|
|
// Stage 1: Approval
|
|
$this->assertFalse($document->isApprovalComplete());
|
|
$this->assertFalse($document->isDisbursementComplete());
|
|
$this->assertFalse($document->isRecordingComplete());
|
|
|
|
// Complete approval (secretary only for small)
|
|
$document->status = FinanceDocument::STATUS_APPROVED_SECRETARY;
|
|
$document->approved_by_secretary_id = $this->secretary->id;
|
|
$document->secretary_approved_at = now();
|
|
$document->save();
|
|
|
|
$this->assertTrue($document->isApprovalComplete());
|
|
|
|
// Stage 2: Disbursement (dual confirmation)
|
|
$document->requester_confirmed_at = now();
|
|
$document->requester_confirmed_by_id = $this->requester->id;
|
|
$document->cashier_confirmed_at = now();
|
|
$document->cashier_confirmed_by_id = $this->cashier->id;
|
|
$document->save();
|
|
|
|
$this->assertTrue($document->isDisbursementComplete());
|
|
|
|
// Stage 3: Recording
|
|
$document->accountant_recorded_at = now();
|
|
$document->accountant_recorded_by_id = $this->accountant->id;
|
|
$document->save();
|
|
|
|
$this->assertTrue($document->isRecordingComplete());
|
|
$this->assertTrue($document->isFullyProcessed());
|
|
}
|
|
|
|
/** @test */
|
|
public function amount_tier_is_automatically_determined()
|
|
{
|
|
$smallDoc = FinanceDocument::factory()->make(['amount' => 3000]);
|
|
$this->assertEquals('small', $smallDoc->determineAmountTier());
|
|
|
|
$mediumDoc = FinanceDocument::factory()->make(['amount' => 25000]);
|
|
$this->assertEquals('medium', $mediumDoc->determineAmountTier());
|
|
|
|
$largeDoc = FinanceDocument::factory()->make(['amount' => 75000]);
|
|
$this->assertEquals('large', $largeDoc->determineAmountTier());
|
|
}
|
|
|
|
/** @test */
|
|
public function board_meeting_requirement_is_correctly_identified()
|
|
{
|
|
$smallDoc = FinanceDocument::factory()->make(['amount' => 3000]);
|
|
$this->assertFalse($smallDoc->needsBoardMeetingApproval());
|
|
|
|
$mediumDoc = FinanceDocument::factory()->make(['amount' => 25000]);
|
|
$this->assertFalse($mediumDoc->needsBoardMeetingApproval());
|
|
|
|
$largeDoc = FinanceDocument::factory()->make(['amount' => 75000]);
|
|
$this->assertTrue($largeDoc->needsBoardMeetingApproval());
|
|
}
|
|
}
|