Skip to content

Commit 8c815df

Browse files
committed
Merge remote-tracking branch 'origin/2.1.16-develop' into 2.1.16-develop-pr60
2 parents 3de87a3 + ed63da0 commit 8c815df

File tree

13 files changed

+229
-74
lines changed

13 files changed

+229
-74
lines changed

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
use Magento\ImportExport\Model\Import;
1010
use Magento\ImportExport\Block\Adminhtml\Import\Frame\Result;
1111
use Magento\Framework\Controller\ResultFactory;
12-
use Magento\Framework\App\Filesystem\DirectoryList;
13-
use Magento\ImportExport\Model\Import\Adapter as ImportAdapter;
14-
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1512

1613
class Validate extends ImportResultController
1714
{
@@ -42,12 +39,7 @@ public function execute()
4239
/** @var $import \Magento\ImportExport\Model\Import */
4340
$import = $this->getImport()->setData($data);
4441
try {
45-
$source = ImportAdapter::findAdapterFor(
46-
$import->uploadSource(),
47-
$this->_objectManager->create('Magento\Framework\Filesystem')
48-
->getDirectoryWrite(DirectoryList::ROOT),
49-
$data[$import::FIELD_FIELD_SEPARATOR]
50-
);
42+
$source = $import->uploadFileAndGetSource();
5143
$this->processValidationResult($import->validateSource($source), $resultBlock);
5244
} catch (\Magento\Framework\Exception\LocalizedException $e) {
5345
$resultBlock->addError($e->getMessage());

app/code/Magento/ImportExport/Model/Import.php

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

1111
use Magento\Framework\App\Filesystem\DirectoryList;
1212
use Magento\Framework\HTTP\Adapter\FileTransferFactory;
13+
use Magento\Framework\Stdlib\DateTime\DateTime;
1314
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
1415
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1516

@@ -78,6 +79,11 @@ class Import extends \Magento\ImportExport\Model\AbstractModel
7879
*/
7980
const FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR = '_import_multiple_value_separator';
8081

82+
/**
83+
* Import empty attribute value constant.
84+
*/
85+
const FIELD_EMPTY_ATTRIBUTE_VALUE_CONSTANT = '_import_empty_attribute_value_constant';
86+
8187
/**
8288
* Allow multiple values wrapping in double quotes for additional attributes.
8389
*/
@@ -90,6 +96,11 @@ class Import extends \Magento\ImportExport\Model\AbstractModel
9096
*/
9197
const DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR = ',';
9298

99+
/**
100+
* default empty attribute value constant
101+
*/
102+
const DEFAULT_EMPTY_ATTRIBUTE_VALUE_CONSTANT = '__EMPTY__VALUE__';
103+
93104
/**#@+
94105
* Import constants
95106
*/
@@ -162,6 +173,16 @@ class Import extends \Magento\ImportExport\Model\AbstractModel
162173
*/
163174
protected $_filesystem;
164175

176+
/**
177+
* @var History
178+
*/
179+
private $importHistoryModel;
180+
181+
/**
182+
* @var DateTime
183+
*/
184+
private $localeDate;
185+
165186
/**
166187
* @param \Psr\Log\LoggerInterface $logger
167188
* @param \Magento\Framework\Filesystem $filesystem
@@ -176,7 +197,7 @@ class Import extends \Magento\ImportExport\Model\AbstractModel
176197
* @param Source\Import\Behavior\Factory $behaviorFactory
177198
* @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
178199
* @param History $importHistoryModel
179-
* @param \Magento\Framework\Stdlib\DateTime\DateTime
200+
* @param DateTime $localeDate
180201
* @param array $data
181202
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
182203
*/
@@ -194,7 +215,7 @@ public function __construct(
194215
\Magento\ImportExport\Model\Source\Import\Behavior\Factory $behaviorFactory,
195216
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
196217
\Magento\ImportExport\Model\History $importHistoryModel,
197-
\Magento\Framework\Stdlib\DateTime\DateTime $localeDate,
218+
DateTime $localeDate,
198219
array $data = []
199220
) {
200221
$this->_importExportData = $importExportData;
@@ -221,7 +242,6 @@ public function __construct(
221242
*/
222243
protected function _getEntityAdapter()
223244
{
224-
225245
if (!$this->_entityAdapter) {
226246
$entities = $this->_importConfig->getEntities();
227247
if (isset($entities[$this->getEntity()])) {
@@ -239,8 +259,8 @@ protected function _getEntityAdapter()
239259
throw new \Magento\Framework\Exception\LocalizedException(
240260
__(
241261
'The entity adapter object must be an instance of %1 or %2.',
242-
'Magento\ImportExport\Model\Import\Entity\AbstractEntity',
243-
'Magento\ImportExport\Model\Import\AbstractEntity'
262+
\Magento\ImportExport\Model\Import\Entity\AbstractEntity::class,
263+
\Magento\ImportExport\Model\Import\AbstractEntity::class
244264
)
245265
);
246266
}
@@ -435,7 +455,6 @@ public function importSource()
435455
$this->importHistoryModel->invalidateReport($this);
436456
}
437457

438-
439458
return $result;
440459
}
441460

@@ -537,14 +556,28 @@ public function uploadSource()
537556
}
538557
$this->_removeBom($sourceFile);
539558
$this->createHistoryReport($sourceFileRelative, $entity, $extension, $result);
540-
// trying to create source adapter for file and catch possible exception to be convinced in its adequacy
559+
560+
return $sourceFile;
561+
}
562+
563+
/**
564+
* Move uploaded file and provide source instance.
565+
*
566+
* @return Import\AbstractSource
567+
* @throws \Magento\Framework\Exception\FileSystemException
568+
* @throws \Magento\Framework\Exception\LocalizedException
569+
*/
570+
public function uploadFileAndGetSource()
571+
{
572+
$sourceFile = $this->uploadSource();
541573
try {
542-
$this->_getSourceAdapter($sourceFile);
574+
$source = $this->_getSourceAdapter($sourceFile);
543575
} catch (\Exception $e) {
544-
$this->_varDirectory->delete($sourceFileRelative);
576+
$this->_varDirectory->delete($this->_varDirectory->getRelativePath($sourceFile));
545577
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
546578
}
547-
return $sourceFile;
579+
580+
return $source;
548581
}
549582

550583
/**
@@ -708,7 +741,9 @@ public function isReportEntityType($entity = null)
708741
try {
709742
$result = $this->_getEntityAdapter()->isNeedToLogInHistory();
710743
} catch (\Exception $e) {
711-
throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a correct entity model'));
744+
throw new \Magento\Framework\Exception\LocalizedException(
745+
__('Please enter a correct entity model')
746+
);
712747
}
713748
} else {
714749
throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a correct entity model'));
@@ -720,11 +755,11 @@ public function isReportEntityType($entity = null)
720755
}
721756

722757
/**
723-
* Create history report
758+
* Create history report.
724759
*
760+
* @param string $sourceFileRelative
725761
* @param string $entity
726762
* @param string $extension
727-
* @param string $sourceFileRelative
728763
* @param array $result
729764
* @return $this
730765
* @throws \Magento\Framework\Exception\LocalizedException
@@ -737,7 +772,7 @@ protected function createHistoryReport($sourceFileRelative, $entity, $extension
737772
$sourceFileRelative = $this->_varDirectory->getRelativePath(self::IMPORT_DIR . $fileName);
738773
} elseif (isset($result['name'])) {
739774
$fileName = $result['name'];
740-
} elseif (!is_null($extension)) {
775+
} elseif ($extension !== null) {
741776
$fileName = $entity . $extension;
742777
} else {
743778
$fileName = basename($sourceFileRelative);
@@ -759,7 +794,6 @@ protected function createHistoryReport($sourceFileRelative, $entity, $extension
759794
return $this;
760795
}
761796

762-
763797
/**
764798
* Get count of created items
765799
*

app/code/Magento/ImportExport/Model/Import/Source/Zip.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\ImportExport\Model\Import\Source;
77

8+
use Magento\Framework\Exception\ValidatorException;
9+
810
/**
911
* Zip import adapter.
1012
*/
@@ -14,17 +16,22 @@ class Zip extends Csv
1416
* @param string $file
1517
* @param \Magento\Framework\Filesystem\Directory\Write $directory
1618
* @param string $options
19+
* @throws \Magento\Framework\Exception\ValidatorException
1720
*/
1821
public function __construct(
1922
$file,
2023
\Magento\Framework\Filesystem\Directory\Write $directory,
2124
$options
2225
) {
2326
$zip = new \Magento\Framework\Archive\Zip();
24-
$file = $zip->unpack(
25-
$directory->getRelativePath($file),
26-
$directory->getRelativePath(preg_replace('/\.zip$/i', '.csv', $file))
27+
$csvFile = $zip->unpack(
28+
$file,
29+
preg_replace('/\.zip$/i', '.csv', $file)
2730
);
28-
parent::__construct($file, $directory, $options);
31+
if (!$csvFile) {
32+
throw new ValidatorException(__('Sorry, but the data is invalid or the file is not uploaded.'));
33+
}
34+
$directory->delete($directory->getRelativePath($file));
35+
parent::__construct($csvFile, $directory, $options);
2936
}
3037
}

app/code/Magento/Store/etc/config.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@
130130
<html>html</html>
131131
<phtml>phtml</phtml>
132132
<shtml>shtml</shtml>
133+
<phpt>phpt</phpt>
134+
<pht>pht</pht>
133135
</protected_extensions>
134136
<public_files_valid_paths>
135137
<protected>

app/code/Magento/Swagger/Block/Index.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Class Index
1212
*
1313
* @api
14+
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
1415
*/
1516
class Index extends Template
1617
{
@@ -19,7 +20,7 @@ class Index extends Template
1920
*/
2021
private function getParamStore()
2122
{
22-
return $this->getRequest()->getParam('store') ?: 'all';
23+
return $this->stripTags($this->getRequest()->getParam('store')) ?: 'all';
2324
}
2425

2526
/**

app/code/Magento/Swagger/view/frontend/templates/swagger-ui/index.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ $schemaUrl = $block->getSchemaUrl();
2424
<div class="swagger-ui-wrap">
2525
<a id="logo" href="http://swagger.io">swagger</a>
2626
<form id='api_selector'>
27-
<input id="input_baseUrl" type="hidden" value="<?php /* @escapeNotVerified */ echo $schemaUrl ?>"/>
27+
<input id="input_baseUrl" type="hidden" value="<?php echo $block->escapeUrl($schemaUrl) ?>"/>
2828
<div class='input'><input placeholder="api_key" id="input_apiKey" name="apiKey" type="text"/></div>
2929
<div class='input'><a id="explore" href="#" data-sw-translate>apply</a></div>
3030
</form>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Code\File\Validator;
8+
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
/**
12+
* Tests protected extensions.
13+
*/
14+
class NotProtectedExtensionTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* Test that phpt, pht is invalid extension type
18+
* @dataProvider isValidDataProvider
19+
* @param string $extension
20+
* @return void
21+
*/
22+
public function testIsValid($extension)
23+
{
24+
$objectManager = Bootstrap::getObjectManager();
25+
/** @var \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $model */
26+
$model = $objectManager->create(\Magento\MediaStorage\Model\File\Validator\NotProtectedExtension::class);
27+
$this->assertFalse($model->isValid($extension));
28+
}
29+
30+
/**
31+
* Data provider for testIsValid
32+
*
33+
* @return array
34+
*/
35+
public function isValidDataProvider()
36+
{
37+
return [
38+
['phpt'],
39+
['pht'],
40+
];
41+
}
42+
}

dev/tests/integration/testsuite/Magento/ImportExport/Controller/Adminhtml/Import/ValidateTest.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ class ValidateTest extends \Magento\TestFramework\TestCase\AbstractBackendContro
1717
/**
1818
* @dataProvider validationDataProvider
1919
* @param string $fileName
20+
* @param string $mimeType
2021
* @param string $message
22+
* @param string $delimiter
2123
* @backupGlobals enabled
2224
* @magentoDbIsolation enabled
25+
* @SuppressWarnings(PHPMD.Superglobals)
2326
*/
24-
public function testValidationReturn($fileName, $message)
27+
public function testValidationReturn($fileName, $mimeType, $message, $delimiter)
2528
{
2629
$validationStrategy = ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_STOP_ON_ERROR;
2730

@@ -30,16 +33,16 @@ public function testValidationReturn($fileName, $message)
3033
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
3134

3235
/** @var $formKey \Magento\Framework\Data\Form\FormKey */
33-
$formKey = $this->_objectManager->get('Magento\Framework\Data\Form\FormKey');
36+
$formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
3437
$this->getRequest()->setPostValue('form_key', $formKey->getFormKey());
3538
$this->getRequest()->setPostValue('entity', 'catalog_product');
3639
$this->getRequest()->setPostValue('behavior', 'append');
3740
$this->getRequest()->setPostValue(Import::FIELD_NAME_VALIDATION_STRATEGY, $validationStrategy);
3841
$this->getRequest()->setPostValue(Import::FIELD_NAME_ALLOWED_ERROR_COUNT, 0);
39-
$this->getRequest()->setPostValue('_import_field_separator', ',');
42+
$this->getRequest()->setPostValue('_import_field_separator', $delimiter);
4043

4144
/** @var \Magento\TestFramework\App\Filesystem $filesystem */
42-
$filesystem = $this->_objectManager->get('Magento\Framework\Filesystem');
45+
$filesystem = $this->_objectManager->get(\Magento\Framework\Filesystem::class);
4346
$tmpDir = $filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
4447
$subDir = str_replace('\\', '_', __CLASS__);
4548
$tmpDir->create($subDir);
@@ -49,7 +52,7 @@ public function testValidationReturn($fileName, $message)
4952
$_FILES = [
5053
'import_file' => [
5154
'name' => $fileName,
52-
'type' => 'text/csv',
55+
'type' => $mimeType,
5356
'tmp_name' => $target,
5457
'error' => 0,
5558
'size' => filesize($target)
@@ -59,8 +62,8 @@ public function testValidationReturn($fileName, $message)
5962
$this->_objectManager->configure(
6063
[
6164
'preferences' => [
62-
'Magento\Framework\HTTP\Adapter\FileTransferFactory' =>
63-
'Magento\ImportExport\Controller\Adminhtml\Import\HttpFactoryMock'
65+
\Magento\Framework\HTTP\Adapter\FileTransferFactory::class =>
66+
\Magento\ImportExport\Controller\Adminhtml\Import\HttpFactoryMock::class
6467
]
6568
]
6669
);
@@ -83,12 +86,22 @@ public function validationDataProvider()
8386
return [
8487
[
8588
'file_name' => 'catalog_product.csv',
86-
'message' => 'File is valid'
89+
'mime-type' => 'text/csv',
90+
'message' => 'File is valid',
91+
'delimiter' => ',',
8792
],
8893
[
8994
'file_name' => 'test.txt',
90-
'message' => '\'txt\' file extension is not supported'
91-
]
95+
'mime-type' => 'text/csv',
96+
'message' => '\'txt\' file extension is not supported',
97+
'delimiter' => ',',
98+
],
99+
[
100+
'file_name' => 'catalog_product.zip',
101+
'mime-type' => 'application/zip',
102+
'message' => 'File is valid',
103+
'delimiter' => ',',
104+
],
92105
];
93106
}
94107
}

0 commit comments

Comments
 (0)