|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Engine; |
| 4 | + |
| 5 | +use ProcessMaker\Nayra\Storage\BpmnDocument; |
| 6 | +use DOMDocument; |
| 7 | + |
| 8 | +/** |
| 9 | + * Test for handling HTML entities in BPMN files. |
| 10 | + */ |
| 11 | +class BpmnDocumentHtmlEntitiesTest extends EngineTestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Test the replaceHtmlEntities method directly. |
| 15 | + */ |
| 16 | + public function testReplaceHtmlEntities() |
| 17 | + { |
| 18 | + // String with HTML entities |
| 19 | + $source = 'This is a text with <html> entities & special chars like "quotes" and 'apostrophes' and spaces.'; |
| 20 | + |
| 21 | + // Expected result after replacement |
| 22 | + $expected = 'This is a text with <html> entities & special chars like "quotes" and 'apostrophes' and  spaces.'; |
| 23 | + |
| 24 | + // Test the static method |
| 25 | + $result = BpmnDocument::replaceHtmlEntities($source); |
| 26 | + |
| 27 | + // Assert the result matches the expected output |
| 28 | + $this->assertEquals($expected, $result); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Test loadXML with HTML entities. |
| 33 | + */ |
| 34 | + public function testLoadXmlWithHtmlEntities() |
| 35 | + { |
| 36 | + // Create a BPMN XML string with HTML entities in the documentation tag |
| 37 | + $bpmnXml = file_get_contents(__DIR__ . '/files/BpmnWithHtmlEntities.bpmn'); |
| 38 | + |
| 39 | + // 1. First try to load with regular DOMDocument - should fail or produce incorrect results |
| 40 | + $regularDom = new DOMDocument(); |
| 41 | + $regularLoaded = @$regularDom->loadXML($bpmnXml); // @ to suppress warnings |
| 42 | + |
| 43 | + // If it loads without errors, check that the content is different from what we expect |
| 44 | + if ($regularLoaded) { |
| 45 | + $startEventDoc = $regularDom->getElementsByTagName('documentation')->item(0); |
| 46 | + $originalContent = $startEventDoc ? $startEventDoc->textContent : ''; |
| 47 | + |
| 48 | + // The content should be mangled or different from what we expect with proper entity handling |
| 49 | + $expectedContent = 'This contains <b>HTML</b> entities & special chars like "quotes" and \'apostrophes\' and spaces.'; |
| 50 | + $this->assertNotEquals($expectedContent, $originalContent, 'Standard DOMDocument should not correctly handle HTML entities'); |
| 51 | + } |
| 52 | + |
| 53 | + // 2. Now load with BpmnDocument which should handle HTML entities correctly |
| 54 | + $bpmnDocument = new BpmnDocument(); |
| 55 | + $bpmnDocument->setEngine($this->engine); |
| 56 | + $bpmnDocument->setFactory($this->repository); |
| 57 | + |
| 58 | + // Load the XML with HTML entities |
| 59 | + $result = $bpmnDocument->loadXML($bpmnXml); |
| 60 | + $this->assertTrue($result, 'BpmnDocument should successfully load the XML with HTML entities'); |
| 61 | + |
| 62 | + // Verify that documentation tags contain correctly converted entities |
| 63 | + $startEventDoc = $bpmnDocument->getElementsByTagName('documentation')->item(0); |
| 64 | + $this->assertNotNull($startEventDoc, 'Documentation element should exist'); |
| 65 | + |
| 66 | + // The text content should have the HTML entities properly converted |
| 67 | + $nbsp = "\xC2\xA0"; |
| 68 | + $expectedContent = 'This contains <b>HTML</b> entities & special chars like "quotes" and \'apostrophes\' and ' . $nbsp . 'spaces.'; |
| 69 | + $this->assertEquals($expectedContent, $startEventDoc->textContent, 'HTML entities should be correctly converted'); |
| 70 | + |
| 71 | + // Check the second documentation tag too |
| 72 | + $taskDoc = $bpmnDocument->getElementsByTagName('documentation')->item(1); |
| 73 | + $this->assertNotNull($taskDoc, 'Second documentation element should exist'); |
| 74 | + $expectedTaskContent = 'Another <strong>documentation</strong> with & entities.'; |
| 75 | + $this->assertEquals($expectedTaskContent, $taskDoc->textContent, 'HTML entities in second documentation should be correctly converted'); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Test loading a complex BPMN with HTML entities in various places. |
| 80 | + */ |
| 81 | + public function testLoadComplexBpmnWithHtmlEntities() |
| 82 | + { |
| 83 | + // Create a more complex BPMN with HTML entities in various attributes and text content |
| 84 | + $complexBpmnXml = file_get_contents(__DIR__ . '/files/BpmnWithComplexHtml.bpmn'); |
| 85 | + |
| 86 | + // Load with BpmnDocument |
| 87 | + $bpmnDocument = new BpmnDocument(); |
| 88 | + $bpmnDocument->setEngine($this->engine); |
| 89 | + $bpmnDocument->setFactory($this->repository); |
| 90 | + |
| 91 | + $result = $bpmnDocument->loadXML($complexBpmnXml); |
| 92 | + $this->assertTrue($result, 'BpmnDocument should successfully load complex XML with HTML entities'); |
| 93 | + |
| 94 | + // Check process name attribute |
| 95 | + $process = $bpmnDocument->getElementsByTagName('process')->item(0); |
| 96 | + $this->assertEquals('Process & HTML entities', $process->getAttribute('name'), 'Process name attribute should have entities converted'); |
| 97 | + |
| 98 | + // Check start event name attribute |
| 99 | + $startEvent = $bpmnDocument->getElementsByTagName('startEvent')->item(0); |
| 100 | + $this->assertEquals('Start <event>', $startEvent->getAttribute('name'), 'Start event name attribute should have entities converted'); |
| 101 | + |
| 102 | + // Check documentation content |
| 103 | + $documentation = $startEvent->getElementsByTagName('documentation')->item(0); |
| 104 | + $this->assertEquals( |
| 105 | + 'Documentation with <ul><li>HTML list</li></ul> and & "quotes"', |
| 106 | + $documentation->textContent, |
| 107 | + 'Documentation should have entities converted' |
| 108 | + ); |
| 109 | + |
| 110 | + // Check sequence flow name attribute |
| 111 | + $sequenceFlow = $bpmnDocument->getElementsByTagName('sequenceFlow')->item(0); |
| 112 | + $this->assertEquals( |
| 113 | + 'Flow with & special "chars"', |
| 114 | + $sequenceFlow->getAttribute('name'), |
| 115 | + 'Sequence flow name attribute should have entities converted' |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments