| 1 |
<?php |
| 2 |
|
| 3 |
namespace Liuch\DmarcSrg\Report; |
| 4 |
|
| 5 |
use Liuch\DmarcSrg\Domains\Domain; |
| 6 |
use Liuch\DmarcSrg\Report\SummaryReport; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class MarkdownEscapingTest extends \PHPUnit\Framework\TestCase |
| 12 |
{ |
| 13 |
private $mockDomain; |
| 14 |
|
| 15 |
public function setUp(): void |
| 16 |
{ |
| 17 |
$this->mockDomain = $this->createMock(Domain::class); |
| 18 |
$this->mockDomain->method('fqdn')->willReturn('test.example.com'); |
| 19 |
$this->mockDomain->method('active')->willReturn(true); |
| 20 |
$this->mockDomain->method('isAssigned')->willReturn(true); |
| 21 |
} |
| 22 |
|
| 23 |
public function testEscapeMarkdownMethodExists() |
| 24 |
{ |
| 25 |
$reflection = new \ReflectionClass(SummaryReport::class); |
| 26 |
$this->assertTrue($reflection->hasMethod('escapeMarkdown'), 'escapeMarkdown method must exist'); |
| 27 |
|
| 28 |
$method = $reflection->getMethod('escapeMarkdown'); |
| 29 |
$this->assertTrue($method->isPrivate(), 'escapeMarkdown method must be private'); |
| 30 |
} |
| 31 |
|
| 32 |
public function testMarkdownSpecialCharactersEscaping() |
| 33 |
{ |
| 34 |
$reflection = new \ReflectionClass(SummaryReport::class); |
| 35 |
$method = $reflection->getMethod('escapeMarkdown'); |
| 36 |
|
| 37 |
$report = new SummaryReport('lastweek'); |
| 38 |
|
| 39 |
|
| 40 |
$testCases = [ |
| 41 |
|
| 42 |
['test|pipe', 'test\\|pipe'], |
| 43 |
['test\\backslash', 'test\\\\backslash'], |
| 44 |
['normal_text', 'normal_text'], |
| 45 |
['test_underscore', 'test_underscore'], |
| 46 |
['test*asterisk', 'test*asterisk'], |
| 47 |
['test#hash', 'test#hash'], |
| 48 |
]; |
| 49 |
|
| 50 |
foreach ($testCases as [$input, $expected]) { |
| 51 |
$result = $method->invokeArgs($report, [$input]); |
| 52 |
$this->assertEquals($expected, $result, "Failed to escape: '$input'"); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public function testComplexStringEscaping() |
| 57 |
{ |
| 58 |
$reflection = new \ReflectionClass(SummaryReport::class); |
| 59 |
$method = $reflection->getMethod('escapeMarkdown'); |
| 60 |
|
| 61 |
$report = new SummaryReport('lastweek'); |
| 62 |
|
| 63 |
|
| 64 |
$complexString = 'Org|Name\\With|Pipes'; |
| 65 |
$expected = 'Org\\|Name\\\\With\\|Pipes'; |
| 66 |
|
| 67 |
$result = $method->invokeArgs($report, [$complexString]); |
| 68 |
$this->assertEquals($expected, $result, 'Complex string escaping failed'); |
| 69 |
} |
| 70 |
|
| 71 |
public function testEmptyAndNullStrings() |
| 72 |
{ |
| 73 |
$reflection = new \ReflectionClass(SummaryReport::class); |
| 74 |
$method = $reflection->getMethod('escapeMarkdown'); |
| 75 |
|
| 76 |
$report = new SummaryReport('lastweek'); |
| 77 |
|
| 78 |
|
| 79 |
$result = $method->invokeArgs($report, ['']); |
| 80 |
$this->assertEquals('', $result, 'Empty string should remain empty'); |
| 81 |
|
| 82 |
|
| 83 |
$result = $method->invokeArgs($report, [' ']); |
| 84 |
$this->assertEquals(' ', $result, 'Spaces should be preserved'); |
| 85 |
} |
| 86 |
|
| 87 |
public function testCommonOrganizationNames() |
| 88 |
{ |
| 89 |
$reflection = new \ReflectionClass(SummaryReport::class); |
| 90 |
$method = $reflection->getMethod('escapeMarkdown'); |
| 91 |
|
| 92 |
$report = new SummaryReport('lastweek'); |
| 93 |
|
| 94 |
|
| 95 |
$orgNames = [ |
| 96 |
'Org|With|Pipes' => 'Org\\|With\\|Pipes', |
| 97 |
'Company\\Path' => 'Company\\\\Path', |
| 98 |
'Normal Company' => 'Normal Company', |
| 99 |
'Company_Name' => 'Company_Name', |
| 100 |
'Google*Analytics' => 'Google*Analytics', |
| 101 |
]; |
| 102 |
|
| 103 |
foreach ($orgNames as $input => $expected) { |
| 104 |
$result = $method->invokeArgs($report, [$input]); |
| 105 |
$this->assertEquals($expected, $result, "Failed to escape org name: '$input'"); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public function testPercentageStringsEscaping() |
| 110 |
{ |
| 111 |
$reflection = new \ReflectionClass(SummaryReport::class); |
| 112 |
$method = $reflection->getMethod('escapeMarkdown'); |
| 113 |
|
| 114 |
$report = new SummaryReport('lastweek'); |
| 115 |
|
| 116 |
|
| 117 |
$percentages = [ |
| 118 |
'50%' => '50%', |
| 119 |
'100%|' => '100%\\|', |
| 120 |
'25%(5+2)' => '25%(5+2)', |
| 121 |
'0' => '0', |
| 122 |
]; |
| 123 |
|
| 124 |
foreach ($percentages as $input => $expected) { |
| 125 |
$result = $method->invokeArgs($report, [$input]); |
| 126 |
$this->assertEquals($expected, $result, "Failed to escape percentage: '$input'"); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public function testIPAddressEscaping() |
| 131 |
{ |
| 132 |
$reflection = new \ReflectionClass(SummaryReport::class); |
| 133 |
$method = $reflection->getMethod('escapeMarkdown'); |
| 134 |
|
| 135 |
$report = new SummaryReport('lastweek'); |
| 136 |
|
| 137 |
|
| 138 |
$ipAddresses = [ |
| 139 |
'192.168.1.1' => '192.168.1.1', |
| 140 |
'2001:db8::1' => '2001:db8::1', |
| 141 |
'special|ip' => 'special\\|ip', |
| 142 |
]; |
| 143 |
|
| 144 |
foreach ($ipAddresses as $input => $expected) { |
| 145 |
$result = $method->invokeArgs($report, [$input]); |
| 146 |
$this->assertEquals($expected, $result, "Failed to escape IP: '$input'"); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public function testBackslashEscapingOrder() |
| 151 |
{ |
| 152 |
$reflection = new \ReflectionClass(SummaryReport::class); |
| 153 |
$method = $reflection->getMethod('escapeMarkdown'); |
| 154 |
|
| 155 |
$report = new SummaryReport('lastweek'); |
| 156 |
|
| 157 |
|
| 158 |
$input = 'test\\|pipe|'; |
| 159 |
$expected = 'test\\\\\\|pipe\\|'; |
| 160 |
|
| 161 |
$result = $method->invokeArgs($report, [$input]); |
| 162 |
$this->assertEquals($expected, $result, 'Backslash escaping order is critical'); |
| 163 |
} |
| 164 |
|
| 165 |
public function testUnicodeCharacters() |
| 166 |
{ |
| 167 |
$reflection = new \ReflectionClass(SummaryReport::class); |
| 168 |
$method = $reflection->getMethod('escapeMarkdown'); |
| 169 |
|
| 170 |
$report = new SummaryReport('lastweek'); |
| 171 |
|
| 172 |
|
| 173 |
$input = 'Café|München|Zürich'; |
| 174 |
$expected = 'Café\\|München\\|Zürich'; |
| 175 |
|
| 176 |
$result = $method->invokeArgs($report, [$input]); |
| 177 |
$this->assertEquals($expected, $result, 'Unicode characters should be preserved while special chars are escaped'); |
| 178 |
} |
| 179 |
} |