Skip to content

Commit e3ee625

Browse files
Implement CaseRetentionPolicyLog Tests
1 parent 8458911 commit e3ee625

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

database/factories/ProcessMaker/Models/CaseRetentionPolicyLogFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Database\Factories\ProcessMaker\Models;
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use ProcessMaker\Models\CaseNumber;
7+
use ProcessMaker\Models\Process;
68

79
/**
810
* Model factory for a settings.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Tests\Unit\ProcessMaker\Models;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Illuminate\Support\Facades\Config;
8+
use Illuminate\Support\Facades\DB;
9+
use ProcessMaker\Jobs\EvaluateProcessRetentionJob;
10+
use ProcessMaker\Models\CaseNumber;
11+
use ProcessMaker\Models\CaseParticipated;
12+
use ProcessMaker\Models\CaseRetentionPolicyLog;
13+
use ProcessMaker\Models\CaseStarted;
14+
use ProcessMaker\Models\Process;
15+
use ProcessMaker\Models\ProcessRequest;
16+
use Tests\TestCase;
17+
18+
class CaseRetentionPolicyLogTest extends TestCase
19+
{
20+
use RefreshDatabase;
21+
22+
public function testJobAddsLogRecordWhenCasesAreDeleted(): void
23+
{
24+
Config::set('app.case_retention_policy_enabled', true);
25+
26+
$process = Process::factory()->create([
27+
'properties' => ['retention_period' => 'one_year'],
28+
]);
29+
$process->save();
30+
$process->refresh();
31+
32+
$processRequest = ProcessRequest::factory()->create();
33+
$processRequest->process_id = $process->id;
34+
$processRequest->save();
35+
$processRequest->refresh();
36+
37+
CaseNumber::where('process_request_id', $processRequest->id)->delete();
38+
39+
$oldCaseCreatedAt = Carbon::now()->subMonths(13)->toIso8601String();
40+
$caseOld = CaseNumber::factory()->create([
41+
'created_at' => $oldCaseCreatedAt,
42+
'process_request_id' => $processRequest->id,
43+
]);
44+
CaseStarted::factory()->create(['case_number' => $caseOld->id]);
45+
CaseParticipated::factory()->create(['case_number' => $caseOld->id]);
46+
47+
$this->assertDatabaseCount('case_retention_policy_logs', 0);
48+
49+
EvaluateProcessRetentionJob::dispatchSync($process->id);
50+
51+
$this->assertDatabaseCount('case_retention_policy_logs', 1);
52+
53+
$log = CaseRetentionPolicyLog::first();
54+
$this->assertSame((int) $process->id, (int) $log->process_id);
55+
$this->assertSame(1, $log->deleted_count);
56+
$this->assertNotEmpty($log->total_time_taken);
57+
$this->assertIsNumeric($log->total_time_taken);
58+
$this->assertNotNull($log->deleted_at);
59+
60+
$loggedCaseIds = json_decode($log->case_ids, true);
61+
$this->assertIsArray($loggedCaseIds);
62+
$this->assertContains((int) $caseOld->id, array_map('intval', $loggedCaseIds));
63+
64+
Config::set('app.case_retention_policy_enabled', false);
65+
}
66+
}

0 commit comments

Comments
 (0)