Debian Codegraph @

tests/classes/Report/MarkdownEscapingTest.php

raw symbols SCIP
1 <?php
2
3 namespace Liuch\DmarcSrg\Report;
4
5 use Liuch\DmarcSrg\Domains\Domain;
6 use Liuch\DmarcSrg\Report\SummaryReport;
7
8 /**
9 * Test markdown character escaping functionality
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 // Test characters that actually need escaping in table context
40 $testCases = [
41 // [input, expected_output]
42 ['test|pipe', 'test\\|pipe'], // Pipe breaks tables
43 ['test\\backslash', 'test\\\\backslash'], // Backslash for escaping
44 ['normal_text', 'normal_text'], // Normal text unchanged
45 ['test_underscore', 'test_underscore'], // Underscores OK in table cells
46 ['test*asterisk', 'test*asterisk'], // Asterisks OK in table cells
47 ['test#hash', 'test#hash'], // Hash OK in table cells
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 // Test complex strings focusing on critical table-breaking characters
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 // Test empty string
79 $result = $method->invokeArgs($report, ['']);
80 $this->assertEquals('', $result, 'Empty string should remain empty');
81
82 // Test spaces
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 // Test realistic organization names focusing on table-breaking characters
95 $orgNames = [
96 'Org|With|Pipes' => 'Org\\|With\\|Pipes',
97 'Company\\Path' => 'Company\\\\Path',
98 'Normal Company' => 'Normal Company',
99 'Company_Name' => 'Company_Name', // Underscores OK in tables
100 'Google*Analytics' => 'Google*Analytics', // Asterisks OK in tables
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 // Test percentage strings focusing on table-breaking characters
117 $percentages = [
118 '50%' => '50%',
119 '100%|' => '100%\\|', // Pipe would break table
120 '25%(5+2)' => '25%(5+2)', // Parentheses are OK
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 // Test IP addresses (should normally not contain table-breaking chars)
138 $ipAddresses = [
139 '192.168.1.1' => '192.168.1.1',
140 '2001:db8::1' => '2001:db8::1',
141 'special|ip' => 'special\\|ip', // Edge case with pipe
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 // Test that backslashes are escaped first (important for order)
158 $input = 'test\\|pipe|';
159 $expected = 'test\\\\\\|pipe\\|'; // \\ then \|
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 // Test unicode characters mixed with table-breaking chars
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 }