|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\ProcessMaker\Models; |
| 4 | + |
| 5 | +use ProcessMaker\Models\Process; |
| 6 | +use ProcessMaker\Models\ProcessRequest; |
| 7 | +use ProcessMaker\Models\ProcessRequestToken; |
| 8 | +use ProcessMaker\Models\User; |
| 9 | +use ReflectionClass; |
| 10 | +use Tests\TestCase; |
| 11 | + |
| 12 | +class ProcessRequestTokenElementDestinationTest extends TestCase |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Invoke a private method on an object. |
| 16 | + */ |
| 17 | + private function invokePrivateMethod(object $object, string $methodName, array $args = []): mixed |
| 18 | + { |
| 19 | + $reflection = new ReflectionClass($object); |
| 20 | + $method = $reflection->getMethod($methodName); |
| 21 | + $method->setAccessible(true); |
| 22 | + |
| 23 | + return $method->invokeArgs($object, $args); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Test getElementDestinationMustacheContext returns context with APP_URL, _request, _user and process data. |
| 28 | + */ |
| 29 | + public function testGetElementDestinationMustacheContextReturnsExpectedKeys(): void |
| 30 | + { |
| 31 | + $user = User::factory()->create(['status' => 'ACTIVE']); |
| 32 | + $process = Process::factory()->create(); |
| 33 | + $request = ProcessRequest::factory()->create([ |
| 34 | + 'process_id' => $process->id, |
| 35 | + 'user_id' => $user->id, |
| 36 | + 'data' => ['processVar' => 'value123'], |
| 37 | + ]); |
| 38 | + |
| 39 | + $token = ProcessRequestToken::factory()->create([ |
| 40 | + 'process_id' => $process->id, |
| 41 | + 'process_request_id' => $request->id, |
| 42 | + 'user_id' => $user->id, |
| 43 | + 'element_id' => 'end_1', |
| 44 | + 'element_type' => 'end_event', |
| 45 | + ]); |
| 46 | + |
| 47 | + $context = $this->invokePrivateMethod($token, 'getElementDestinationMustacheContext'); |
| 48 | + |
| 49 | + $this->assertIsArray($context); |
| 50 | + $this->assertArrayHasKey('APP_URL', $context); |
| 51 | + $this->assertSame(config('app.url'), $context['APP_URL']); |
| 52 | + $this->assertArrayHasKey('_request', $context); |
| 53 | + $this->assertIsArray($context['_request']); |
| 54 | + $this->assertArrayHasKey('id', $context['_request']); |
| 55 | + $this->assertSame((string) $request->id, (string) $context['_request']['id']); |
| 56 | + $this->assertArrayHasKey('case_number', $context['_request']); |
| 57 | + $this->assertArrayHasKey('_user', $context); |
| 58 | + $this->assertIsArray($context['_user']); |
| 59 | + $this->assertArrayHasKey('id', $context['_user']); |
| 60 | + $this->assertArrayHasKey('processVar', $context); |
| 61 | + $this->assertSame('value123', $context['processVar']); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Test resolveElementDestinationUrl resolves Mustache placeholders APP_URL and _request.id. |
| 66 | + */ |
| 67 | + public function testResolveElementDestinationUrlResolvesMustache(): void |
| 68 | + { |
| 69 | + $user = User::factory()->create(['status' => 'ACTIVE']); |
| 70 | + $process = Process::factory()->create(); |
| 71 | + $request = ProcessRequest::factory()->create([ |
| 72 | + 'process_id' => $process->id, |
| 73 | + 'user_id' => $user->id, |
| 74 | + 'data' => [], |
| 75 | + ]); |
| 76 | + |
| 77 | + $token = ProcessRequestToken::factory()->create([ |
| 78 | + 'process_id' => $process->id, |
| 79 | + 'process_request_id' => $request->id, |
| 80 | + 'user_id' => $user->id, |
| 81 | + 'element_id' => 'end_1', |
| 82 | + 'element_type' => 'end_event', |
| 83 | + ]); |
| 84 | + |
| 85 | + $urlTemplate = '{{APP_URL}}/path/{{_request.id}}'; |
| 86 | + $resolved = $this->invokePrivateMethod($token, 'resolveElementDestinationUrl', [$urlTemplate]); |
| 87 | + |
| 88 | + $expectedUrl = config('app.url') . '/path/' . $request->id; |
| 89 | + $this->assertSame($expectedUrl, $resolved); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test resolveElementDestinationUrl resolves process variable in URL. |
| 94 | + */ |
| 95 | + public function testResolveElementDestinationUrlResolvesProcessVariable(): void |
| 96 | + { |
| 97 | + $user = User::factory()->create(['status' => 'ACTIVE']); |
| 98 | + $process = Process::factory()->create(); |
| 99 | + $request = ProcessRequest::factory()->create([ |
| 100 | + 'process_id' => $process->id, |
| 101 | + 'user_id' => $user->id, |
| 102 | + 'data' => ['segment' => 'admin'], |
| 103 | + ]); |
| 104 | + |
| 105 | + $token = ProcessRequestToken::factory()->create([ |
| 106 | + 'process_id' => $process->id, |
| 107 | + 'process_request_id' => $request->id, |
| 108 | + 'user_id' => $user->id, |
| 109 | + 'element_id' => 'end_1', |
| 110 | + 'element_type' => 'end_event', |
| 111 | + ]); |
| 112 | + |
| 113 | + $urlTemplate = '{{APP_URL}}/{{segment}}/users'; |
| 114 | + $resolved = $this->invokePrivateMethod($token, 'resolveElementDestinationUrl', [$urlTemplate]); |
| 115 | + |
| 116 | + $this->assertSame(config('app.url') . '/admin/users', $resolved); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Test resolveElementDestinationUrl decodes HTML entities in template. |
| 121 | + */ |
| 122 | + public function testResolveElementDestinationUrlDecodesHtmlEntities(): void |
| 123 | + { |
| 124 | + $user = User::factory()->create(['status' => 'ACTIVE']); |
| 125 | + $process = Process::factory()->create(); |
| 126 | + $request = ProcessRequest::factory()->create([ |
| 127 | + 'process_id' => $process->id, |
| 128 | + 'user_id' => $user->id, |
| 129 | + 'data' => [], |
| 130 | + ]); |
| 131 | + |
| 132 | + $token = ProcessRequestToken::factory()->create([ |
| 133 | + 'process_id' => $process->id, |
| 134 | + 'process_request_id' => $request->id, |
| 135 | + 'user_id' => $user->id, |
| 136 | + 'element_id' => 'end_1', |
| 137 | + 'element_type' => 'end_event', |
| 138 | + ]); |
| 139 | + |
| 140 | + $urlWithEntities = 'https://example.com/{{_request.id}}'; |
| 141 | + $resolved = $this->invokePrivateMethod($token, 'resolveElementDestinationUrl', [$urlWithEntities]); |
| 142 | + |
| 143 | + $this->assertStringContainsString((string) $request->id, $resolved); |
| 144 | + $this->assertStringContainsString('https://example.com/', $resolved); |
| 145 | + } |
| 146 | +} |
0 commit comments