|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 6 | +use Illuminate\Support\Facades\DB; |
| 7 | +use ProcessMaker\Models\Group; |
| 8 | +use ProcessMaker\Models\ProcessRequestToken; |
| 9 | +use ProcessMaker\Models\User; |
| 10 | +use Tests\TestCase; |
| 11 | + |
| 12 | +class SelfServiceOptimizationTest extends TestCase |
| 13 | +{ |
| 14 | + use RefreshDatabase; |
| 15 | + |
| 16 | + /** |
| 17 | + * BULLETPROOF TEST: Verifies that the Subquery approach is 100% equivalent |
| 18 | + * to the Array approach across all possible edge cases and legacy formats. |
| 19 | + */ |
| 20 | + public function test_subquery_optimization_is_bulletproof() |
| 21 | + { |
| 22 | + // 1. Setup Environment |
| 23 | + $user = User::factory()->create(); |
| 24 | + $groupA = Group::factory()->create(['name' => 'Group A']); |
| 25 | + $groupB = Group::factory()->create(['name' => 'Group B']); |
| 26 | + $user->groups()->attach([$groupA->id, $groupB->id]); |
| 27 | + |
| 28 | + $otherUser = User::factory()->create(); |
| 29 | + $otherGroup = Group::factory()->create(['name' => 'Other Group']); |
| 30 | + |
| 31 | + // 2. CREATE SCENARIOS |
| 32 | + |
| 33 | + // Scenario 1: New Format - Int ID in groups array |
| 34 | + $t1 = $this->createSelfServiceTask(['groups' => [$groupA->id]]); |
| 35 | + |
| 36 | + // Scenario 2: New Format - String ID in groups array (Legacy/JSON inconsistency) |
| 37 | + $t2 = $this->createSelfServiceTask(['groups' => [(string) $groupB->id]]); |
| 38 | + |
| 39 | + // Scenario 3: Old Format - Direct ID in array (Very old processes) |
| 40 | + $t3 = $this->createSelfServiceTask([$groupA->id]); |
| 41 | + |
| 42 | + // Scenario 4: Direct User Assignment (Int) |
| 43 | + $t4 = $this->createSelfServiceTask(['users' => [$user->id]]); |
| 44 | + |
| 45 | + // Scenario 5: Direct User Assignment (String) |
| 46 | + $t5 = $this->createSelfServiceTask(['users' => [(string) $user->id]]); |
| 47 | + |
| 48 | + // --- NEGATIVE SCENARIOS (Should NEVER be returned) --- |
| 49 | + |
| 50 | + // Scenario 6: Task for another group |
| 51 | + $t6 = $this->createSelfServiceTask(['groups' => [$otherGroup->id]]); |
| 52 | + |
| 53 | + // Scenario 7: Task for another user |
| 54 | + $t7 = $this->createSelfServiceTask(['users' => [$otherUser->id]]); |
| 55 | + |
| 56 | + // Scenario 8: Task is not ACTIVE |
| 57 | + $t8 = $this->createSelfServiceTask(['users' => [$user->id]], 'COMPLETED'); |
| 58 | + |
| 59 | + // Scenario 9: Task is already assigned to someone |
| 60 | + $t9 = $this->createSelfServiceTask(['users' => [$user->id]], 'ACTIVE', $otherUser->id); |
| 61 | + |
| 62 | + // 3. THE COMPARISON ENGINE |
| 63 | + |
| 64 | + // Method A: Array Pluck (Memory intensive, prone to crash) |
| 65 | + $oldWayIds = $user->availableSelfServiceTaskIds()->sort()->values()->toArray(); |
| 66 | + |
| 67 | + // Method B: Subquery (Optimized, safe) |
| 68 | + $newWayQuery = $user->availableSelfServiceTasksQuery(); |
| 69 | + $resultsNewWay = ProcessRequestToken::whereIn('id', $newWayQuery) |
| 70 | + ->orderBy('id') |
| 71 | + ->pluck('id') |
| 72 | + ->toArray(); |
| 73 | + |
| 74 | + // 4. ASSERTIONS |
| 75 | + |
| 76 | + // A. Integrity check: Both lists must be identical |
| 77 | + $this->assertEquals($oldWayIds, $resultsNewWay, 'FATAL: Subquery results differ from Array results!'); |
| 78 | + |
| 79 | + // B. Coverage check: Ensure all positive scenarios are present |
| 80 | + $expectedIds = [$t1->id, $t2->id, $t3->id, $t4->id, $t5->id]; |
| 81 | + sort($expectedIds); |
| 82 | + $this->assertEquals($expectedIds, $resultsNewWay, 'Subquery missed one of the valid scenarios.'); |
| 83 | + |
| 84 | + // C. Exclusion check: Ensure none of the negative scenarios leaked in |
| 85 | + $forbiddenIds = [$t6->id, $t7->id, $t8->id, $t9->id]; |
| 86 | + foreach ($forbiddenIds as $id) { |
| 87 | + $this->assertNotContains($id, $resultsNewWay, "Security breach: Task $id should not be visible."); |
| 88 | + } |
| 89 | + |
| 90 | + // D. Performance Logic check: Subquery must be an instance of Eloquent Builder |
| 91 | + $this->assertInstanceOf(\Illuminate\Database\Eloquent\Builder::class, $newWayQuery); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * STRESS TEST: Demonstrates the performance and stability gap. |
| 96 | + * This test creates 10,000 tasks to show how the old way struggles vs the new way. |
| 97 | + */ |
| 98 | + public function test_large_data_performance_and_stability() |
| 99 | + { |
| 100 | + $user = User::factory()->create(); |
| 101 | + $group = Group::factory()->create(); |
| 102 | + $user->groups()->attach($group); |
| 103 | + |
| 104 | + // Crear dependencias reales para evitar errores de Foreign Key |
| 105 | + $process = \ProcessMaker\Models\Process::factory()->create(); |
| 106 | + $request = \ProcessMaker\Models\ProcessRequest::factory()->create([ |
| 107 | + 'process_id' => $process->id, |
| 108 | + ]); |
| 109 | + |
| 110 | + echo "\n--- STRESS TEST (10,000 Self-Service Tasks) ---\n"; |
| 111 | + |
| 112 | + // 1. Seed 10,000 tasks efficiently using bulk insert |
| 113 | + $count = 10000; |
| 114 | + $now = now()->toDateTimeString(); |
| 115 | + $chunkSize = 1000; |
| 116 | + |
| 117 | + for ($i = 0; $i < $count / $chunkSize; $i++) { |
| 118 | + $tasks = []; |
| 119 | + for ($j = 0; $j < $chunkSize; $j++) { |
| 120 | + $tasks[] = [ |
| 121 | + 'process_id' => $process->id, |
| 122 | + 'process_request_id' => $request->id, |
| 123 | + 'element_id' => 'node_1', |
| 124 | + 'element_type' => 'task', |
| 125 | + 'status' => 'ACTIVE', |
| 126 | + 'is_self_service' => 1, |
| 127 | + 'self_service_groups' => json_encode(['groups' => [$group->id]]), |
| 128 | + 'created_at' => $now, |
| 129 | + 'updated_at' => $now, |
| 130 | + ]; |
| 131 | + } |
| 132 | + DB::table('process_request_tokens')->insert($tasks); |
| 133 | + } |
| 134 | + |
| 135 | + // 2. Measure OLD WAY (Array of IDs) |
| 136 | + $startMemOld = memory_get_usage(); |
| 137 | + $startTimeOld = microtime(true); |
| 138 | + |
| 139 | + $ids = $user->availableSelfServiceTaskIds(); |
| 140 | + $resultOld = ProcessRequestToken::whereIn('id', $ids)->count(); |
| 141 | + |
| 142 | + $timeOld = microtime(true) - $startTimeOld; |
| 143 | + $memOld = (memory_get_usage() - $startMemOld) / 1024 / 1024; |
| 144 | + |
| 145 | + // 3. Measure NEW WAY (Subquery) |
| 146 | + $startMemNew = memory_get_usage(); |
| 147 | + $startTimeNew = microtime(true); |
| 148 | + |
| 149 | + $query = $user->availableSelfServiceTasksQuery(); |
| 150 | + $resultNew = ProcessRequestToken::whereIn('id', $query)->count(); |
| 151 | + |
| 152 | + $timeNew = microtime(true) - $startTimeNew; |
| 153 | + $memNew = (memory_get_usage() - $startMemNew) / 1024 / 1024; |
| 154 | + |
| 155 | + // OUTPUT RESULTS |
| 156 | + echo 'OLD WAY (Array): Time: ' . number_format($timeOld, 4) . 's | Mem: ' . number_format($memOld, 2) . "MB | Found: $resultOld\n"; |
| 157 | + echo 'NEW WAY (Subquery): Time: ' . number_format($timeNew, 4) . 's | Mem: ' . number_format($memNew, 2) . "MB | Found: $resultNew\n"; |
| 158 | + |
| 159 | + // ASSERTIONS |
| 160 | + $this->assertEquals($resultOld, $resultNew, 'Results must be identical!'); |
| 161 | + |
| 162 | + // En base de datos reales (no en memoria), la subconsulta suele ser más rápida |
| 163 | + // Pero lo más importante es que no tiene límites de placeholders |
| 164 | + $this->assertTrue($resultNew > 0); |
| 165 | + |
| 166 | + echo "----------------------------------------------\n"; |
| 167 | + if ($timeNew > 0) { |
| 168 | + echo 'Optimization: ' . number_format(($timeOld / $timeNew), 1) . "x faster\n"; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private function createSelfServiceTask($groups, $status = 'ACTIVE', $userId = null) |
| 173 | + { |
| 174 | + return ProcessRequestToken::factory()->create([ |
| 175 | + 'is_self_service' => true, |
| 176 | + 'status' => $status, |
| 177 | + 'user_id' => $userId, |
| 178 | + 'self_service_groups' => $groups, |
| 179 | + ]); |
| 180 | + } |
| 181 | +} |
0 commit comments