Skip to content

Commit 5b1f11f

Browse files
authored
Merge pull request #4589 from magento-tsg/2.2.10-develop-pr106
[TSG] Fixes for 2.2 (pr106) (2.2.10-develop)
2 parents 8f029af + c5f4d4d commit 5b1f11f

File tree

16 files changed

+368
-27
lines changed

16 files changed

+368
-27
lines changed

app/code/Magento/Downloadable/Controller/Adminhtml/Downloadable/File/Upload.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Magento\MediaStorage\Helper\File\Storage\Database;
1818
use Magento\Framework\App\RequestInterface;
1919
use Magento\Framework\App\State;
20+
use Magento\Framework\Exception\FileSystemException;
21+
use Magento\Framework\Exception\LocalizedException;
2022

2123
/**
2224
* Upload controller
@@ -109,23 +111,27 @@ public function dispatch(RequestInterface $request)
109111
*/
110112
public function execute()
111113
{
112-
$type = $this->getRequest()->getParam('type');
113-
$tmpPath = '';
114-
if ($type == 'samples') {
115-
$tmpPath = $this->_sample->getBaseTmpPath();
116-
} elseif ($type == 'links') {
117-
$tmpPath = $this->_link->getBaseTmpPath();
118-
} elseif ($type == 'link_samples') {
119-
$tmpPath = $this->_link->getBaseSampleTmpPath();
120-
}
121-
122114
try {
115+
$type = $this->getRequest()->getParam('type');
116+
$tmpPath = '';
117+
if ($type === 'samples') {
118+
$tmpPath = $this->_sample->getBaseTmpPath();
119+
} elseif ($type === 'links') {
120+
$tmpPath = $this->_link->getBaseTmpPath();
121+
} elseif ($type === 'link_samples') {
122+
$tmpPath = $this->_link->getBaseSampleTmpPath();
123+
} else {
124+
throw new LocalizedException(__('Upload type can not be determined.'));
125+
}
126+
123127
$uploader = $this->uploaderFactory->create(['fileId' => $type]);
124128

125129
$result = $this->_fileHelper->uploadFromTmp($tmpPath, $uploader);
126130

127131
if (!$result) {
128-
throw new \Exception('File can not be moved from temporary folder to the destination folder.');
132+
throw new FileSystemException(
133+
__('File can not be moved from temporary folder to the destination folder.')
134+
);
129135
}
130136

131137
unset($result['tmp_name'], $result['path']);
@@ -137,6 +143,7 @@ public function execute()
137143
} catch (\Exception $e) {
138144
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
139145
}
146+
140147
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
141148
}
142149
}

app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use Magento\Eav\Model\Entity\Attribute as EntityAttribute;
1111
use Magento\Framework\App\ObjectManager;
1212
use Magento\Framework\DB\Select;
13+
use Magento\Framework\Exception\CouldNotDeleteException;
1314
use Magento\Framework\Model\AbstractModel;
15+
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
1416

1517
/**
1618
* EAV attribute resource model
@@ -19,7 +21,7 @@
1921
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2022
* @since 100.0.2
2123
*/
22-
class Attribute extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
24+
class Attribute extends AbstractDb
2325
{
2426
/**
2527
* Eav Entity attributes cache
@@ -188,6 +190,23 @@ protected function _beforeSave(AbstractModel $object)
188190
return parent::_beforeSave($object);
189191
}
190192

193+
/**
194+
* @inheritdoc
195+
*
196+
* @param AbstractModel $attribute
197+
* @return AbstractDb
198+
* @throws CouldNotDeleteException
199+
*/
200+
protected function _beforeDelete(AbstractModel $attribute)
201+
{
202+
/** @var $attribute \Magento\Eav\Api\Data\AttributeInterface */
203+
if ($attribute->getId() && !$attribute->getIsUserDefined()) {
204+
throw new CouldNotDeleteException(__("The system attribute can't be deleted."));
205+
}
206+
207+
return parent::_beforeDelete($attribute);
208+
}
209+
191210
/**
192211
* Save additional attribute data after save attribute
193212
*

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeRepositoryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
1010

11+
/**
12+
* API tests for \Magento\Catalog\Model\Product\Attribute\Repository.
13+
*/
1114
class ProductAttributeRepositoryTest extends \Magento\TestFramework\TestCase\WebapiAbstract
1215
{
1316
const SERVICE_NAME = 'catalogProductAttributeRepositoryV1';
@@ -328,6 +331,20 @@ public function testUpdateWithNewOption()
328331
$this->assertEquals(4, count($output['options']));
329332
}
330333

334+
/**
335+
* Trying to delete system attribute.
336+
*
337+
* @magentoApiDataFixture Magento/Catalog/_files/product_system_attribute.php
338+
* @expectedException \Exception
339+
* @expectedExceptionMessage The system attribute can't be deleted.
340+
* @return void
341+
*/
342+
public function testDeleteSystemAttributeById()
343+
{
344+
$attributeCode = 'test_attribute_code_333';
345+
$this->deleteAttribute($attributeCode);
346+
}
347+
331348
/**
332349
* @magentoApiDataFixture Magento/Catalog/_files/product_attribute.php
333350
*/

dev/tests/integration/testsuite/Magento/Catalog/Model/Indexer/Product/Flat/ProcessorTest.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66
namespace Magento\Catalog\Model\Indexer\Product\Flat;
77

8+
use Magento\Catalog\Model\Product\Attribute\Repository;
9+
810
/**
9-
* Class FullTest
11+
* Integration tests for \Magento\Catalog\Model\Indexer\Product\Flat\Processor.
1012
*/
1113
class ProcessorTest extends \Magento\TestFramework\Indexer\TestCase
1214
{
@@ -64,22 +66,23 @@ public function testSaveAttribute()
6466
}
6567

6668
/**
67-
* @magentoDbIsolation enabled
69+
* @magentoDbIsolation disabled
6870
* @magentoAppIsolation enabled
6971
* @magentoAppArea adminhtml
70-
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
72+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_custom_attribute_in_flat.php
7173
* @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
7274
*/
7375
public function testDeleteAttribute()
7476
{
75-
/** @var $product \Magento\Catalog\Model\Product */
76-
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
77-
\Magento\Catalog\Model\Product::class
78-
);
79-
80-
/** @var \Magento\Catalog\Model\ResourceModel\Product $productResource */
81-
$productResource = $product->getResource();
82-
$productResource->getAttribute('media_gallery')->delete();
77+
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $model */
78+
$model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
79+
->get(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
80+
/** @var Repository $productAttributeRepository */
81+
$productAttributeRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
82+
->get(Repository::class);
83+
$productAttrubute = $productAttributeRepository->get('flat_attribute');
84+
$productAttributeId = $productAttrubute->getAttributeId();
85+
$model->load($productAttributeId)->delete();
8386

8487
$this->assertTrue($this->_processor->getIndexer()->isInvalid());
8588
}

dev/tests/integration/testsuite/Magento/Catalog/Model/Layer/Filter/_files/attribute_with_option.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'is_global' => 1,
2121
'frontend_input' => 'select',
2222
'is_filterable' => 1,
23+
'is_user_defined' => 1,
2324
'option' => ['value' => ['option_0' => [0 => 'Option Label']]],
2425
'backend_type' => 'int',
2526
]

dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Eav/AttributeTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Eav;
77

8+
/**
9+
* Test for \Magento\Catalog\Model\ResourceModel\Eav\Attribute.
10+
*/
811
class AttributeTest extends \PHPUnit\Framework\TestCase
912
{
1013
/**
@@ -19,6 +22,11 @@ protected function setUp()
1922
);
2023
}
2124

25+
/**
26+
* Test Create -> Read -> Update -> Delete attribute operations.
27+
*
28+
* @return void
29+
*/
2230
public function testCRUD()
2331
{
2432
$this->_model->setAttributeCode(
@@ -31,7 +39,7 @@ public function testCRUD()
3139
)->getId()
3240
)->setFrontendLabel(
3341
'test'
34-
);
42+
)->setIsUserDefined(1);
3543
$crud = new \Magento\TestFramework\Entity($this->_model, ['frontend_label' => uniqid()]);
3644
$crud->testCrud();
3745
}

dev/tests/integration/testsuite/Magento/Catalog/_files/category_attribute.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
1010
$attribute->setAttributeCode('test_attribute_code_666')
1111
->setEntityTypeId(3)
12-
->setIsGlobal(1);
12+
->setIsGlobal(1)
13+
->setIsUserDefined(1);
1314
$attribute->save();

dev/tests/integration/testsuite/Magento/Catalog/_files/product_attribute.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
$attribute->setAttributeCode('test_attribute_code_333')
1111
->setEntityTypeId(4)
1212
->setIsGlobal(1)
13-
->setPrice(95);
13+
->setPrice(95)
14+
->setIsUserDefined(1);
1415
$attribute->save();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
require __DIR__ . '/product_attribute.php';
9+
/** @var $attributeRepository \Magento\Catalog\Model\Product\Attribute\Repository */
10+
$attributeRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
11+
->get(\Magento\Catalog\Model\Product\Attribute\Repository::class);
12+
/** @var $attribute \Magento\Eav\Api\Data\AttributeInterface */
13+
$attribute = $attributeRepository->get('test_attribute_code_333');
14+
15+
$attributeRepository->save($attribute->setIsUserDefined(0));
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
use Magento\Framework\Exception\NoSuchEntityException;
9+
10+
/** @var $attributeRepository \Magento\Catalog\Model\Product\Attribute\Repository */
11+
$attributeRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
12+
->get(\Magento\Catalog\Model\Product\Attribute\Repository::class);
13+
14+
try {
15+
/** @var $attribute \Magento\Eav\Api\Data\AttributeInterface */
16+
$attribute = $attributeRepository->get('test_attribute_code_333');
17+
$attributeRepository->save($attribute->setIsUserDefined(1));
18+
} catch (NoSuchEntityException $e) {
19+
}
20+
/** @var \Magento\Framework\Registry $registry */
21+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
22+
23+
$registry->unregister('isSecureArea');
24+
$registry->register('isSecureArea', true);
25+
26+
try {
27+
$attribute = $attributeRepository->get('test_attribute_code_333');
28+
if ($attribute->getId()) {
29+
$attribute->delete();
30+
}
31+
} catch (\Exception $e) {
32+
}
33+
34+
$registry->unregister('isSecureArea');
35+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)