Skip to content

Commit 4f63342

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-74012' into 2.2-develop-pr61
2 parents 72357cc + babc79f commit 4f63342

File tree

6 files changed

+327
-23
lines changed

6 files changed

+327
-23
lines changed

app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminProductAttributeActionGroup.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@
3636
<click selector="{{AdminProductAttributeGridSection.attributeCode(attributeCode)}}" stepKey="clickRowToEdit"/>
3737
<waitForPageLoad stepKey="waitForColorAttributePageLoad"/>
3838
</actionGroup>
39-
39+
<!--Save product attribute and see success message-->
40+
<actionGroup name="SaveProductAttribute">
41+
<scrollToTopOfPage stepKey="scrollToTop"/>
42+
<click selector="{{AdminMainActionsSection.save}}" stepKey="clickSaveAttribute"/>
43+
<see selector="{{AdminMessagesSection.success}}" userInput="You saved the product attribute." stepKey="seeSuccessMessage"/>
44+
</actionGroup>
4045
<actionGroup name="deleteProductAttribute">
4146
<arguments>
4247
<argument name="ProductAttribute"/>

app/code/Magento/ImportExport/Controller/Adminhtml/Import/Download.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*/
66
namespace Magento\ImportExport\Controller\Adminhtml\Import;
77

8+
use Magento\Framework\App\Filesystem\DirectoryList;
89
use Magento\Framework\Component\ComponentRegistrar;
910
use Magento\ImportExport\Controller\Adminhtml\Import as ImportController;
10-
use Magento\Framework\App\Filesystem\DirectoryList;
1111

1212
/**
1313
* Download sample file controller
@@ -36,6 +36,11 @@ class Download extends ImportController
3636
*/
3737
protected $fileFactory;
3838

39+
/**
40+
* @var \Magento\ImportExport\Model\Import\SampleFileProvider
41+
*/
42+
private $sampleFileProvider;
43+
3944
/**
4045
* Constructor
4146
*
@@ -44,46 +49,48 @@ class Download extends ImportController
4449
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
4550
* @param \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory
4651
* @param ComponentRegistrar $componentRegistrar
52+
* @param \Magento\ImportExport\Model\Import\SampleFileProvider|null $sampleFileProvider
4753
*/
4854
public function __construct(
4955
\Magento\Backend\App\Action\Context $context,
5056
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
5157
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
5258
\Magento\Framework\Filesystem\Directory\ReadFactory $readFactory,
53-
\Magento\Framework\Component\ComponentRegistrar $componentRegistrar
59+
\Magento\Framework\Component\ComponentRegistrar $componentRegistrar,
60+
\Magento\ImportExport\Model\Import\SampleFileProvider $sampleFileProvider = null
5461
) {
55-
parent::__construct(
56-
$context
57-
);
62+
parent::__construct($context);
5863
$this->fileFactory = $fileFactory;
5964
$this->resultRawFactory = $resultRawFactory;
6065
$this->readFactory = $readFactory;
6166
$this->componentRegistrar = $componentRegistrar;
67+
$this->sampleFileProvider = $sampleFileProvider
68+
?: \Magento\Framework\App\ObjectManager::getInstance()
69+
->get(\Magento\ImportExport\Model\Import\SampleFileProvider::class);
6270
}
6371

6472
/**
6573
* Download sample file action
6674
*
67-
* @return \Magento\Framework\Controller\Result\Raw
75+
* @return \Magento\Framework\Controller\Result\Raw|\Magento\Framework\Controller\Result\Redirect
6876
*/
6977
public function execute()
7078
{
71-
$fileName = $this->getRequest()->getParam('filename') . '.csv';
72-
$moduleDir = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, self::SAMPLE_FILES_MODULE);
73-
$fileAbsolutePath = $moduleDir . '/Files/Sample/' . $fileName;
74-
$directoryRead = $this->readFactory->create($moduleDir);
75-
$filePath = $directoryRead->getRelativePath($fileAbsolutePath);
79+
$entityName = $this->getRequest()->getParam('filename');
7680

77-
if (!$directoryRead->isFile($filePath)) {
81+
try {
82+
$fileContents = $this->sampleFileProvider->getFileContents($entityName);
83+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
7884
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
7985
$this->messageManager->addError(__('There is no sample file for this entity.'));
8086
$resultRedirect = $this->resultRedirectFactory->create();
8187
$resultRedirect->setPath('*/import');
88+
8289
return $resultRedirect;
8390
}
8491

85-
$fileSize = isset($directoryRead->stat($filePath)['size'])
86-
? $directoryRead->stat($filePath)['size'] : null;
92+
$fileSize = $this->sampleFileProvider->getSize($entityName);
93+
$fileName = $entityName . '.csv';
8794

8895
$this->fileFactory->create(
8996
$fileName,
@@ -95,7 +102,8 @@ public function execute()
95102

96103
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
97104
$resultRaw = $this->resultRawFactory->create();
98-
$resultRaw->setContents($directoryRead->readFile($filePath));
105+
$resultRaw->setContents($fileContents);
106+
99107
return $resultRaw;
100108
}
101109
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ImportExport\Model\Import;
9+
10+
use Magento\Framework\Component\ComponentRegistrar;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\Filesystem\Directory\ReadInterface;
13+
use Magento\Framework\Filesystem\Directory\ReadFactory;
14+
15+
/**
16+
* Import sample file provider model.
17+
*
18+
* This class support only *.csv.
19+
*/
20+
class SampleFileProvider
21+
{
22+
/**
23+
* Associate an import entity to its module, e.g ['entity_name' => 'module_name']
24+
*
25+
* @var array
26+
*/
27+
private $samples;
28+
29+
/**
30+
* @var ComponentRegistrar
31+
*/
32+
private $componentRegistrar;
33+
34+
/**
35+
* @var ReadFactory
36+
*/
37+
private $readFactory;
38+
39+
/**
40+
* @param ReadFactory $readFactory
41+
* @param ComponentRegistrar $componentRegistrar
42+
* @param array $samples
43+
*/
44+
public function __construct(
45+
ReadFactory $readFactory,
46+
ComponentRegistrar $componentRegistrar,
47+
array $samples = []
48+
) {
49+
$this->readFactory = $readFactory;
50+
$this->componentRegistrar = $componentRegistrar;
51+
$this->samples = $samples;
52+
}
53+
54+
/**
55+
* Returns the size for the given file associated to an import entity.
56+
*
57+
* @param string $entityName
58+
* @return int|null
59+
*/
60+
public function getSize(string $entityName)
61+
{
62+
$directoryRead = $this->getDirectoryRead($entityName);
63+
$filePath = $this->getPath($entityName);
64+
$fileSize = $directoryRead->stat($filePath)['size'] ?? null;
65+
66+
return $fileSize;
67+
}
68+
69+
/**
70+
* Returns content for the given file associated to an import entity.
71+
*
72+
* @param string $entityName
73+
* @return string
74+
*/
75+
public function getFileContents(string $entityName): string
76+
{
77+
$directoryRead = $this->getDirectoryRead($entityName);
78+
$filePath = $this->getPath($entityName);
79+
80+
return $directoryRead->readFile($filePath);
81+
}
82+
83+
/**
84+
* @param string $entityName
85+
* @return string
86+
* @throws NoSuchEntityException
87+
*/
88+
private function getPath(string $entityName): string
89+
{
90+
$directoryRead = $this->getDirectoryRead($entityName);
91+
$fileAbsolutePath = 'Files/Sample/' . $entityName . '.csv';
92+
$filePath = $directoryRead->getRelativePath($fileAbsolutePath);
93+
94+
if (!$directoryRead->isFile($filePath)) {
95+
throw new NoSuchEntityException(__("There is no file: %file", ['file' => $filePath]));
96+
}
97+
98+
return $filePath;
99+
}
100+
101+
/**
102+
* @param string $entityName
103+
* @return ReadInterface
104+
*/
105+
private function getDirectoryRead(string $entityName): ReadInterface
106+
{
107+
$moduleName = $this->getModuleName($entityName);
108+
$moduleDir = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
109+
$directoryRead = $this->readFactory->create($moduleDir);
110+
111+
return $directoryRead;
112+
}
113+
114+
/**
115+
* @param string $entityName
116+
* @return string
117+
* @throws NoSuchEntityException
118+
*/
119+
private function getModuleName(string $entityName): string
120+
{
121+
if (!isset($this->samples[$entityName])) {
122+
throw new NoSuchEntityException();
123+
}
124+
125+
return $this->samples[$entityName];
126+
}
127+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ImportExport\Test\Unit\Model\Import;
7+
8+
use Magento\Framework\Filesystem\Directory\ReadFactory;
9+
use Magento\Framework\Filesystem\Directory\ReadInterface;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\ImportExport\Model\Import\SampleFileProvider;
12+
13+
/**
14+
* Test class for Magento\ImportExport\Model\Import\SampleFileProvider.
15+
*/
16+
class SampleFileProviderTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
private $readerMock;
22+
23+
/**
24+
* @var ReadFactory|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $readerFactoryMock;
27+
28+
/**
29+
* @var ObjectManager
30+
*/
31+
private $objectManager;
32+
33+
/**
34+
* @var SampleFileProvider
35+
*/
36+
private $model;
37+
38+
/**
39+
* @var string
40+
*/
41+
private $entityName = 'test_sample';
42+
43+
/**
44+
* @var string
45+
*/
46+
private $moduleName = 'Test_Sample';
47+
48+
/**
49+
* @var string
50+
*/
51+
private $filePath = 'Files/Sample/test_sample.csv';
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
protected function setUp()
57+
{
58+
$this->objectManager = new ObjectManager($this);
59+
$this->readerMock = $this->getMockBuilder(ReadInterface::class)
60+
->disableOriginalConstructor()
61+
->getMockForAbstractClass();
62+
$this->readerFactoryMock = $this->createMock(ReadFactory::class);
63+
$this->readerFactoryMock->expects($this->any())->method('create')->willReturn($this->readerMock);
64+
$this->readerMock->expects($this->any())->method('getRelativePath')->willReturnArgument(0);
65+
$this->model = $this->objectManager->getObject(
66+
SampleFileProvider::class,
67+
[
68+
'readFactory' => $this->readerFactoryMock,
69+
]
70+
);
71+
}
72+
73+
/**
74+
* @return void
75+
*/
76+
public function testGetSize()
77+
{
78+
$fileSize = 10;
79+
80+
$this->objectManager->setBackwardCompatibleProperty(
81+
$this->model,
82+
'samples',
83+
[$this->entityName => $this->moduleName]
84+
);
85+
$this->readerMock->expects($this->atLeastOnce())->method('isFile')->willReturn(true);
86+
$this->readerMock->expects($this->once())->method('stat')
87+
->with($this->filePath)
88+
->willReturn(['size' => $fileSize]);
89+
90+
$actualSize = $this->model->getSize($this->entityName);
91+
$this->assertEquals($fileSize, $actualSize);
92+
}
93+
94+
/**
95+
* @return void
96+
*/
97+
public function testGetFileContents()
98+
{
99+
$fileContent = 'test';
100+
101+
$this->objectManager->setBackwardCompatibleProperty(
102+
$this->model,
103+
'samples',
104+
[$this->entityName => $this->moduleName]
105+
);
106+
$this->readerMock->expects($this->atLeastOnce())->method('isFile')->willReturn(true);
107+
$this->readerMock->expects($this->once())->method('readFile')
108+
->with($this->filePath)
109+
->willReturn($fileContent);
110+
111+
$actualContent = $this->model->getFileContents($this->entityName);
112+
$this->assertEquals($fileContent, $actualContent);
113+
}
114+
115+
/**
116+
* @dataProvider methodDataProvider
117+
* @expectedException \Magento\Framework\Exception\NoSuchEntityException
118+
* @param string $methodName
119+
* @return void
120+
*/
121+
public function testMethodCallMissingSample(string $methodName)
122+
{
123+
$this->model->{$methodName}('missingType');
124+
}
125+
126+
/**
127+
* @dataProvider methodDataProvider
128+
* @expectedException \Magento\Framework\Exception\NoSuchEntityException
129+
* @expectedExceptionMessage There is no file: Files/Sample/test_sample.csv
130+
* @param string $methodName
131+
* @return void
132+
*/
133+
public function testMethodCallMissingFile(string $methodName)
134+
{
135+
$this->objectManager->setBackwardCompatibleProperty(
136+
$this->model,
137+
'samples',
138+
[$this->entityName => $this->moduleName]
139+
);
140+
$this->readerMock->expects($this->atLeastOnce())->method('isFile')->willReturn(false);
141+
142+
$this->model->{$methodName}($this->entityName);
143+
}
144+
145+
/**
146+
* @return array
147+
*/
148+
public function methodDataProvider(): array
149+
{
150+
return [
151+
['getSize'],
152+
['getFileContents'],
153+
];
154+
}
155+
}

0 commit comments

Comments
 (0)