Skip to content

Commit d0ca827

Browse files
Merge remote-tracking branch 'origin/MC-35302' into 2.3-develop-pr51
2 parents c640482 + c400a66 commit d0ca827

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
/**
1212
* Writes reports in files in csv format
13-
* @inheritdoc
1413
*/
1514
class ReportWriter implements ReportWriterInterface
1615
{
@@ -54,7 +53,7 @@ public function __construct(
5453
}
5554

5655
/**
57-
* {@inheritdoc}
56+
* @inheritdoc
5857
*/
5958
public function write(WriteInterface $directory, $path)
6059
{
@@ -81,7 +80,7 @@ public function write(WriteInterface $directory, $path)
8180
$headers = array_keys($row);
8281
$stream->writeCsv($headers);
8382
}
84-
$stream->writeCsv($row);
83+
$stream->writeCsv($this->prepareRow($row));
8584
}
8685
$stream->unlock();
8786
$stream->close();
@@ -98,4 +97,18 @@ public function write(WriteInterface $directory, $path)
9897

9998
return true;
10099
}
100+
101+
/**
102+
* Replace wrong symbols in row
103+
*
104+
* @param array $row
105+
* @return array
106+
*/
107+
private function prepareRow(array $row): array
108+
{
109+
$row = preg_replace('/(?<!\\\\)"/', '\\"', $row);
110+
$row = preg_replace('/[\\\\]+/', '\\', $row);
111+
112+
return $row;
113+
}
101114
}

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

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,43 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Analytics\Test\Unit\Model;
79

810
use Magento\Analytics\Model\ConfigInterface;
911
use Magento\Analytics\Model\ProviderFactory;
1012
use Magento\Analytics\Model\ReportWriter;
1113
use Magento\Analytics\ReportXml\DB\ReportValidator;
1214
use Magento\Analytics\ReportXml\ReportProvider;
13-
use Magento\Framework\Filesystem\Directory\WriteInterface;
15+
use Magento\Framework\Filesystem\Directory\WriteInterface as DirectoryWriteInterface;
16+
use Magento\Framework\Filesystem\File\WriteInterface as FileWriteInterface;
1417
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use PHPUnit\Framework\TestCase;
1520

1621
/**
1722
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1823
*/
19-
class ReportWriterTest extends \PHPUnit\Framework\TestCase
24+
class ReportWriterTest extends TestCase
2025
{
2126
/**
22-
* @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
27+
* @var ConfigInterface|MockObject
2328
*/
2429
private $configInterfaceMock;
2530

2631
/**
27-
* @var ReportValidator|\PHPUnit_Framework_MockObject_MockObject
32+
* @var ReportValidator|MockObject
2833
*/
2934
private $reportValidatorMock;
3035

3136
/**
32-
* @var ProviderFactory|\PHPUnit_Framework_MockObject_MockObject
37+
* @var ProviderFactory|MockObject
3338
*/
3439
private $providerFactoryMock;
3540

3641
/**
37-
* @var ReportProvider|\PHPUnit_Framework_MockObject_MockObject
42+
* @var ReportProvider|MockObject
3843
*/
3944
private $reportProviderMock;
4045

@@ -44,7 +49,7 @@ class ReportWriterTest extends \PHPUnit\Framework\TestCase
4449
private $objectManagerHelper;
4550

4651
/**
47-
* @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject
52+
* @var DirectoryWriteInterface|MockObject
4853
*/
4954
private $directoryMock;
5055

@@ -80,7 +85,7 @@ protected function setUp()
8085
->disableOriginalConstructor()->getMock();
8186
$this->reportProviderMock = $this->getMockBuilder(ReportProvider::class)
8287
->disableOriginalConstructor()->getMock();
83-
$this->directoryMock = $this->getMockBuilder(WriteInterface::class)->getMockForAbstractClass();
88+
$this->directoryMock = $this->getMockBuilder(DirectoryWriteInterface::class)->getMockForAbstractClass();
8489
$this->objectManagerHelper = new ObjectManagerHelper($this);
8590

8691
$this->reportWriter = $this->objectManagerHelper->getObject(
@@ -95,16 +100,15 @@ protected function setUp()
95100

96101
/**
97102
* @param array $configData
103+
* @param array $fileData
104+
* @param array $expectedFileData
98105
* @return void
99106
*
100107
* @dataProvider configDataProvider
101108
*/
102-
public function testWrite(array $configData)
109+
public function testWrite(array $configData, array $fileData, array $expectedFileData)
103110
{
104111
$errors = [];
105-
$fileData = [
106-
['number' => 1, 'type' => 'Shoes Usual']
107-
];
108112
$this->configInterfaceMock
109113
->expects($this->once())
110114
->method('get')
@@ -123,7 +127,7 @@ public function testWrite(array $configData)
123127
->with($parameterName ?: null)
124128
->willReturn($fileData);
125129
$errorStreamMock = $this->getMockBuilder(
126-
\Magento\Framework\Filesystem\File\WriteInterface::class
130+
FileWriteInterface::class
127131
)->getMockForAbstractClass();
128132
$errorStreamMock
129133
->expects($this->once())
@@ -133,8 +137,8 @@ public function testWrite(array $configData)
133137
->expects($this->exactly(2))
134138
->method('writeCsv')
135139
->withConsecutive(
136-
[array_keys($fileData[0])],
137-
[$fileData[0]]
140+
[array_keys($expectedFileData[0])],
141+
[$expectedFileData[0]]
138142
);
139143
$errorStreamMock->expects($this->once())->method('unlock');
140144
$errorStreamMock->expects($this->once())->method('close');
@@ -166,7 +170,7 @@ public function testWriteErrorFile($configData)
166170
$errors = ['orders', 'SQL Error: test'];
167171
$this->configInterfaceMock->expects($this->once())->method('get')->willReturn([$configData]);
168172
$errorStreamMock = $this->getMockBuilder(
169-
\Magento\Framework\Filesystem\File\WriteInterface::class
173+
FileWriteInterface::class
170174
)->getMockForAbstractClass();
171175
$errorStreamMock->expects($this->once())->method('lock');
172176
$errorStreamMock->expects($this->once())->method('writeCsv')->with($errors);
@@ -196,7 +200,7 @@ public function configDataProvider()
196200
{
197201
return [
198202
'reportProvider' => [
199-
[
203+
'configData' => [
200204
'providers' => [
201205
[
202206
'name' => $this->providerName,
@@ -206,6 +210,12 @@ public function configDataProvider()
206210
],
207211
]
208212
]
213+
],
214+
'fileData' => [
215+
['number' => 1, 'type' => 'Shoes\"" Usual\\\\"']
216+
],
217+
'expectedFileData' => [
218+
['number' => 1, 'type' => 'Shoes\"\" Usual\\"']
209219
]
210220
],
211221
];

0 commit comments

Comments
 (0)