|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use ProcessMaker\Models\Process; |
| 6 | +use ProcessMaker\Models\User; |
| 7 | +use ProcessMaker\Nayra\Storage\BpmnDocument; |
| 8 | +use Tests\Feature\Shared\RequestHelper; |
| 9 | +use Tests\TestCase; |
| 10 | + |
| 11 | +class ProcessHtmlTest extends TestCase |
| 12 | +{ |
| 13 | + use RequestHelper; |
| 14 | + |
| 15 | + protected static $DO_NOT_SEND = 'DO_NOT_SEND'; |
| 16 | + |
| 17 | + /** |
| 18 | + * A process with html entities in the documentation field should be able to be loaded. |
| 19 | + * By default, the bpmn processes are loaded with the html entities support. |
| 20 | + */ |
| 21 | + public function test_process_with_html_can_be_loaded() |
| 22 | + { |
| 23 | + $this->user = User::factory()->create([ |
| 24 | + 'is_administrator' => false, |
| 25 | + ]); |
| 26 | + $process = $this->createProcessFromBPMN('tests/Fixtures/process_with_html.bpmn'); |
| 27 | + |
| 28 | + $definitions = $process->getDefinitions(true); |
| 29 | + |
| 30 | + $this->assertNotEmpty($definitions, 'The process could not be loaded correctly'); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * A process with html entities in the documentation field should be able to be stored. |
| 35 | + */ |
| 36 | + public function test_store_process_with_html_entities() |
| 37 | + { |
| 38 | + $route = route('api.processes.store'); |
| 39 | + $base = Process::factory()->make([ |
| 40 | + 'user_id' => static::$DO_NOT_SEND, |
| 41 | + 'process_category_id' => static::$DO_NOT_SEND, |
| 42 | + ]); |
| 43 | + $array = array_diff($base->toArray(), [static::$DO_NOT_SEND]); |
| 44 | + // Add a bpmn content |
| 45 | + $bpmn = file_get_contents(base_path('tests/Fixtures/process_with_html.bpmn')); |
| 46 | + $array['bpmn'] = $bpmn; |
| 47 | + $response = $this->apiCall('POST', $route, $array); |
| 48 | + $response->assertStatus(201); |
| 49 | + $data = $response->json(); |
| 50 | + $process = Process::where('id', $data['id'])->first(); |
| 51 | + |
| 52 | + // Fix bpmn content to remove the html entities |
| 53 | + $fixedBpmn = BpmnDocument::replaceHtmlEntities($process->bpmn); |
| 54 | + $this->assertEquals($fixedBpmn, $process->bpmn); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * A process with html entities in the documentation field should be able to be updated. |
| 59 | + */ |
| 60 | + public function test_update_process_with_html_entities() |
| 61 | + { |
| 62 | + // First create a process |
| 63 | + $route = route('api.processes.store'); |
| 64 | + $base = Process::factory()->make([ |
| 65 | + 'user_id' => static::$DO_NOT_SEND, |
| 66 | + 'process_category_id' => static::$DO_NOT_SEND, |
| 67 | + ]); |
| 68 | + $array = array_diff($base->toArray(), [static::$DO_NOT_SEND]); |
| 69 | + $response = $this->apiCall('POST', $route, $array); |
| 70 | + $response->assertStatus(201); |
| 71 | + $data = $response->json(); |
| 72 | + $process = Process::where('id', $data['id'])->first(); |
| 73 | + |
| 74 | + // Now update the process |
| 75 | + $bpmn = file_get_contents(base_path('tests/Fixtures/process_with_html.bpmn')); |
| 76 | + $updateRoute = route('api.processes.update', ['process' => $process->id]); |
| 77 | + $updateData = [ |
| 78 | + 'name' => $process->name . ' Updated', |
| 79 | + 'description' => $process->description . ' Updated', |
| 80 | + 'bpmn' => $bpmn, |
| 81 | + ]; |
| 82 | + $updateResponse = $this->apiCall('PUT', $updateRoute, $updateData); |
| 83 | + $updateResponse->assertStatus(200); |
| 84 | + |
| 85 | + // Reload the process from database |
| 86 | + $updatedProcess = Process::where('id', $process->id)->first(); |
| 87 | + |
| 88 | + // Check if the process was updated correctly |
| 89 | + $this->assertEquals($process->name . ' Updated', $updatedProcess->name); |
| 90 | + |
| 91 | + // Fix bpmn content to remove the html entities |
| 92 | + $fixedBpmn = BpmnDocument::replaceHtmlEntities($updatedProcess->bpmn); |
| 93 | + $this->assertEquals($fixedBpmn, $updatedProcess->bpmn); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Test updating BPMN content directly via the updateBpmn endpoint. |
| 98 | + * This endpoint allows updating only the BPMN content, not the other process attributes. |
| 99 | + */ |
| 100 | + public function test_update_bpmn_endpoint_with_html_entities() |
| 101 | + { |
| 102 | + // First create a process |
| 103 | + $route = route('api.processes.store'); |
| 104 | + $base = Process::factory()->make([ |
| 105 | + 'user_id' => static::$DO_NOT_SEND, |
| 106 | + 'process_category_id' => static::$DO_NOT_SEND, |
| 107 | + ]); |
| 108 | + $array = array_diff($base->toArray(), [static::$DO_NOT_SEND]); |
| 109 | + $response = $this->apiCall('POST', $route, $array); |
| 110 | + $response->assertStatus(201); |
| 111 | + $data = $response->json(); |
| 112 | + $process = Process::where('id', $data['id'])->first(); |
| 113 | + |
| 114 | + // Now update the process |
| 115 | + $bpmn = file_get_contents(base_path('tests/Fixtures/process_with_html.bpmn')); |
| 116 | + $updateRoute = route('api.processes.update_bpmn', ['process' => $process->id]); |
| 117 | + $updateData = [ |
| 118 | + 'name' => $process->name . ' Updated', |
| 119 | + 'description' => $process->description . ' Updated', |
| 120 | + 'bpmn' => $bpmn, |
| 121 | + ]; |
| 122 | + $updateResponse = $this->apiCall('PUT', $updateRoute, $updateData); |
| 123 | + $updateResponse->assertStatus(200); |
| 124 | + |
| 125 | + // Reload the process from database |
| 126 | + $updatedProcess = Process::where('id', $process->id)->first(); |
| 127 | + |
| 128 | + // Check if the process was updated correctly |
| 129 | + $this->assertEquals($process->name . ' Updated', $updatedProcess->name); |
| 130 | + |
| 131 | + // Fix bpmn content to remove the html entities |
| 132 | + $fixedBpmn = BpmnDocument::replaceHtmlEntities($updatedProcess->bpmn); |
| 133 | + $this->assertEquals($fixedBpmn, $updatedProcess->bpmn); |
| 134 | + } |
| 135 | +} |
0 commit comments