Skip to content

Commit 5a93a3a

Browse files
committed
MC-37137: Advance Reporting generated csv files are not properly escaped which causes reports to fail on MBI side (continuation)
- Fix escaped quotes with backslash are not properly escaped in the generated csv
1 parent 1dc62a7 commit 5a93a3a

File tree

2 files changed

+73
-14
lines changed

2 files changed

+73
-14
lines changed

app/code/Magento/Analytics/Model/ReportWriter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ public function write(WriteInterface $directory, $path)
103103
/**
104104
* Replace wrong symbols in row
105105
*
106+
* Strip backslashes before double quotes so they will be properly escaped in the generated csv
107+
*
108+
* @see fputcsv()
106109
* @param array $row
107110
* @return array
108111
*/
109112
private function prepareRow(array $row): array
110113
{
111-
$row = preg_replace('/(?<!\\\\)"/', '\\"', $row);
112-
$row = preg_replace('/[\\\\]+/', '\\', $row);
113-
114-
return $row;
114+
return preg_replace('/\\\+(?=\")/', '', $row);
115115
}
116116
}

app/code/Magento/Analytics/Test/Unit/Model/ReportWriterTest.php

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protected function setUp(): void
103103
* @param array $expectedFileData
104104
* @return void
105105
*
106-
* @dataProvider configDataProvider
106+
* @dataProvider writeDataProvider
107107
*/
108108
public function testWrite(array $configData, array $fileData, array $expectedFileData): void
109109
{
@@ -162,7 +162,7 @@ public function testWrite(array $configData, array $fileData, array $expectedFil
162162
* @param array $configData
163163
* @return void
164164
*
165-
* @dataProvider configDataProvider
165+
* @dataProvider writeErrorFileDataProvider
166166
*/
167167
public function testWriteErrorFile(array $configData): void
168168
{
@@ -195,10 +195,75 @@ public function testWriteEmptyReports(): void
195195
/**
196196
* @return array
197197
*/
198-
public function configDataProvider(): array
198+
public function writeDataProvider(): array
199+
{
200+
$configData = [
201+
'providers' => [
202+
[
203+
'name' => $this->providerName,
204+
'class' => $this->providerClass,
205+
'parameters' => [
206+
'name' => $this->reportName
207+
],
208+
]
209+
]
210+
];
211+
return [
212+
[
213+
'configData' => $configData,
214+
'fileData' => [
215+
['number' => 1, 'type' => 'Shoes\"" Usual\\\\"']
216+
],
217+
'expectedFileData' => [
218+
['number' => 1, 'type' => 'Shoes"" Usual"']
219+
]
220+
],
221+
[
222+
'configData' => $configData,
223+
'fileData' => [
224+
['number' => 1, 'type' => 'hello "World"']
225+
],
226+
'expectedFileData' => [
227+
['number' => 1, 'type' => 'hello "World"']
228+
]
229+
],
230+
[
231+
'configData' => $configData,
232+
'fileData' => [
233+
['number' => 1, 'type' => 'hello \"World\"']
234+
],
235+
'expectedFileData' => [
236+
['number' => 1, 'type' => 'hello "World"']
237+
]
238+
],
239+
[
240+
'configData' => $configData,
241+
'fileData' => [
242+
['number' => 1, 'type' => 'hello \\"World\\"']
243+
],
244+
'expectedFileData' => [
245+
['number' => 1, 'type' => 'hello "World"']
246+
]
247+
],
248+
[
249+
'configData' => $configData,
250+
'fileData' => [
251+
['number' => 1, 'type' => 'hello \\\"World\\\"']
252+
],
253+
'expectedFileData' => [
254+
['number' => 1, 'type' => 'hello "World"']
255+
]
256+
],
257+
];
258+
}
259+
260+
/**
261+
* @return array
262+
*/
263+
public function writeErrorFileDataProvider(): array
199264
{
200265
return [
201-
'reportProvider' => [
266+
[
202267
'configData' => [
203268
'providers' => [
204269
[
@@ -210,12 +275,6 @@ public function configDataProvider(): array
210275
]
211276
]
212277
],
213-
'fileData' => [
214-
['number' => 1, 'type' => 'Shoes\"" Usual\\\\"']
215-
],
216-
'expectedFileData' => [
217-
['number' => 1, 'type' => 'Shoes\"\" Usual\\"']
218-
]
219278
],
220279
];
221280
}

0 commit comments

Comments
 (0)