FOSSology  4.7.1
Open Source License Compliance by Open Source Software
CustomTextEscapingTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2026 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
14 class CustomTextEscapingTest extends \PHPUnit\Framework\TestCase
15 {
20  public function testEscapeThenUnescapeRoundTrips(string $original, string $expectedAfterCrlfNormalization): void
21  {
22  $escaped = CustomTextEscaping::escapeNewlines($original);
23  $restored = CustomTextEscaping::unescapeNewlines($escaped);
24 
25  $this->assertSame($expectedAfterCrlfNormalization, $restored);
26  }
27 
28  public static function roundTripProvider(): array
29  {
30  return [
31  'literal backslash-n, no real newline' => [
32  'printf("hello\\n");', 'printf("hello\\n");'
33  ],
34  'real newlines' => [
35  "line one\nline two\nline three", "line one\nline two\nline three"
36  ],
37  'backslash immediately before a real newline' => [
38  "a\\backslash then\nnewline", "a\\backslash then\nnewline"
39  ],
40  'trailing lone backslash' => [
41  "trailing backslash\\", "trailing backslash\\"
42  ],
43  'windows CRLF normalizes to LF' => [
44  "windows\r\nline", "windows\nline"
45  ],
46  'empty string' => [
47  '', ''
48  ],
49  ];
50  }
51 
57  public function testEscapeFlattensToSingleLine(): void
58  {
59  $escaped = CustomTextEscaping::escapeNewlines("multi\nline\nvalue");
60 
61  $this->assertStringNotContainsString("\n", $escaped);
62  $this->assertSame('multi\\nline\\nvalue', $escaped);
63  }
64 
70  public function testNullIsTreatedAsEmptyString(): void
71  {
72  $this->assertSame('', CustomTextEscaping::escapeNewlines(null));
73  $this->assertSame('', CustomTextEscaping::unescapeNewlines(null));
74  }
75 }
testEscapeThenUnescapeRoundTrips(string $original, string $expectedAfterCrlfNormalization)
static unescapeNewlines($value)
Reverse escapeNewlines(): literal becomes a real newline, any other escaped character is unescaped ...
static escapeNewlines($value)
Flatten real newlines to a literal so a field stays on one physical CSV line. Backslashes are escap...
Utility functions for specific applications.