Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into ft-pr-new
Browse files Browse the repository at this point in the history
  • Loading branch information
jilu1 committed Sep 22, 2016
2 parents b5d295a + 1552780 commit 64b86ca
Show file tree
Hide file tree
Showing 91 changed files with 2,781 additions and 415 deletions.
16 changes: 16 additions & 0 deletions app/code/Magento/Backend/Block/Widget/Form/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ class Container extends \Magento\Backend\Block\Widget\Container
* @var string
*/
protected $_blockGroup = 'Magento_Backend';

/**
* @var string
*/
const PARAM_BLOCK_GROUP = 'block_group';

/**
* @var string
*/
const PARAM_MODE = 'mode';

/**
* @var string
Expand All @@ -49,6 +59,12 @@ class Container extends \Magento\Backend\Block\Widget\Container
protected function _construct()
{
parent::_construct();
if ($this->hasData(self::PARAM_BLOCK_GROUP)) {
$this->_blockGroup = $this->_getData(self::PARAM_BLOCK_GROUP);
}
if ($this->hasData(self::PARAM_MODE)) {
$this->_mode = $this->_getData(self::PARAM_MODE);
}

$this->addButton(
'back',
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Backend/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
<resource>Magento_Config::config_general</resource>
<group id="country" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Country Options</label>
<field id="allow" translate="label" type="multiselect" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<field id="allow" translate="label" type="multiselect" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
<label>Allow Countries</label>
<source_model>Magento\Directory\Model\Config\Source\Country</source_model>
<can_be_empty>1</can_be_empty>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln("");
$output->writeln("<info>Unused product attributes successfully cleaned up:</info>");
$output->writeln("<comment> " . implode("\n ", $attributeTables) . "</comment>");
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
} catch (\Exception $exception) {
$this->attributeResource->rollBack();

$output->writeln("");
$output->writeln("<error>{$exception->getMessage()}</error>");
// we must have an exit code higher than zero to indicate something was wrong
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
}
}

Expand Down
16 changes: 9 additions & 7 deletions app/code/Magento/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1909,10 +1909,12 @@ public function addOption(Product\Option $option)
*/
public function getOptionById($optionId)
{
/** @var \Magento\Catalog\Model\Product\Option $option */
foreach ($this->getOptions() as $option) {
if ($option->getId() == $optionId) {
return $option;
if (is_array($this->getOptions())) {
/** @var \Magento\Catalog\Model\Product\Option $option */
foreach ($this->getOptions() as $option) {
if ($option->getId() == $optionId) {
return $option;
}
}
}

Expand Down Expand Up @@ -2272,7 +2274,7 @@ public function getIdentities()
$identities[] = self::CACHE_PRODUCT_CATEGORY_TAG . '_' . $categoryId;
}
}

if (($this->getOrigData('status') != $this->getData('status')) || $this->isStockStatusChanged()) {
foreach ($this->getCategoryIds() as $categoryId) {
$identities[] = self::CACHE_PRODUCT_CATEGORY_TAG . '_' . $categoryId;
Expand All @@ -2287,7 +2289,7 @@ public function getIdentities()

/**
* Check whether stock status changed
*
*
* @return bool
*/
private function isStockStatusChanged()
Expand All @@ -2305,7 +2307,7 @@ private function isStockStatusChanged()
&& ($stockItem->getIsInStock() != $stockData['is_in_stock'])
);
}

/**
* Reload PriceInfo object
*
Expand Down
10 changes: 8 additions & 2 deletions app/code/Magento/Catalog/Model/Product/Attribute/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Eav\Api\Data\AttributeInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand Down Expand Up @@ -117,6 +118,8 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
throw NoSuchEntityException::singleField('attribute_code', $existingModel->getAttributeCode());
}

// Attribute code must not be changed after attribute creation
$attribute->setAttributeCode($existingModel->getAttributeCode());
$attribute->setAttributeId($existingModel->getAttributeId());
$attribute->setIsUserDefined($existingModel->getIsUserDefined());
$attribute->setFrontendInput($existingModel->getFrontendInput());
Expand Down Expand Up @@ -176,8 +179,11 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
$attribute->setIsUserDefined(1);
}
$this->attributeResource->save($attribute);
foreach ($attribute->getOptions() as $option) {
$this->getOptionManagement()->add($attribute->getAttributeCode(), $option);

if (!empty($attribute->getData(AttributeInterface::OPTIONS))) {
foreach ($attribute->getOptions() as $option) {
$this->getOptionManagement()->add($attribute->getAttributeCode(), $option);
}
}
return $this->get($attribute->getAttributeCode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

namespace Magento\Catalog\Test\Unit\Model\Product\Attribute;

use \Magento\Catalog\Model\Product\Attribute\Repository;
use Magento\Catalog\Model\Product\Attribute\Repository;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Catalog\Api\Data\ProductAttributeInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand Down Expand Up @@ -261,4 +263,29 @@ public function testSaveInputExceptionInvalidFieldValue()

$this->model->save($attributeMock);
}

public function testSaveDoesNotSaveAttributeOptionsIfOptionsAreAbsentInPayload()
{
$attributeId = 1;
$attributeCode = 'existing_attribute_code';
$attributeMock = $this->getMock(Attribute::class, [], [], '', false);
$attributeMock->expects($this->any())->method('getAttributeCode')->willReturn($attributeCode);
$attributeMock->expects($this->any())->method('getAttributeId')->willReturn($attributeId);

$existingModelMock = $this->getMock(Attribute::class, [], [], '', false);
$existingModelMock->expects($this->any())->method('getAttributeCode')->willReturn($attributeCode);
$existingModelMock->expects($this->any())->method('getAttributeId')->willReturn($attributeId);

$this->eavAttributeRepositoryMock->expects($this->any())
->method('get')
->with(ProductAttributeInterface::ENTITY_TYPE_CODE, $attributeCode)
->willReturn($existingModelMock);

// Attribute code must not be changed after attribute creation
$attributeMock->expects($this->once())->method('setAttributeCode')->with($attributeCode);
$this->attributeResourceMock->expects($this->once())->method('save')->with($attributeMock);
$this->optionManagementMock->expects($this->never())->method('add');

$this->model->save($attributeMock);
}
}
23 changes: 23 additions & 0 deletions app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1467,4 +1467,27 @@ public function testGetTypeId()
$this->model->setTypeId('typeId');
$this->model->getTypeInstance();
}

public function testGetOptionById()
{
$optionId = 100;
$optionMock = $this->getMock(\Magento\Catalog\Model\Product\Option::class, [], [], '', false);
$this->model->setOptions([$optionMock]);
$optionMock->expects($this->once())->method('getId')->willReturn($optionId);
$this->assertEquals($optionMock, $this->model->getOptionById($optionId));
}

public function testGetOptionByIdWithWrongOptionId()
{
$optionId = 100;
$optionMock = $this->getMock(\Magento\Catalog\Model\Product\Option::class, [], [], '', false);
$this->model->setOptions([$optionMock]);
$optionMock->expects($this->once())->method('getId')->willReturn(200);
$this->assertNull($this->model->getOptionById($optionId));
}

public function testGetOptionByIdForProductWithoutOptions()
{
$this->assertNull($this->model->getOptionById(100));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Categories;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\DB\Helper as DbHelper;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\Store;
Expand Down Expand Up @@ -112,4 +113,38 @@ public function testModifyMeta()

$this->assertArrayHasKey($groupCode, $this->getModel()->modifyMeta($meta));
}

public function testModifyMetaWithCaching()
{
$this->arrayManagerMock->expects($this->exactly(2))
->method('findPath')
->willReturn(true);
$cacheManager = $this->getMockBuilder(CacheInterface::class)
->getMockForAbstractClass();
$cacheManager->expects($this->once())
->method('load')
->with(Categories::CATEGORY_TREE_ID . '_');
$cacheManager->expects($this->once())
->method('save');

$modifier = $this->createModel();
$cacheContextProperty = new \ReflectionProperty(
Categories::class,
'cacheManager'
);
$cacheContextProperty->setAccessible(true);
$cacheContextProperty->setValue($modifier, $cacheManager);

$groupCode = 'test_group_code';
$meta = [
$groupCode => [
'children' => [
'category_ids' => [
'sortOrder' => 10,
],
],
],
];
$modifier->modifyMeta($meta);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@

use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\DB\Helper as DbHelper;
use Magento\Catalog\Model\Category as CategoryModel;
use Magento\Framework\UrlInterface;
use Magento\Framework\Stdlib\ArrayManager;

/**
* Data provider for categories field of product page
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Categories extends AbstractModifier
{
/**#@+
* Category tree cache id
*/
const CATEGORY_TREE_ID = 'CATALOG_PRODUCT_CATEGORY_TREE';
/**#@-*/

/**
* @var CategoryCollectionFactory
*/
Expand All @@ -29,6 +38,7 @@ class Categories extends AbstractModifier

/**
* @var array
* @deprecated
*/
protected $categoriesTrees = [];

Expand All @@ -47,6 +57,11 @@ class Categories extends AbstractModifier
*/
protected $arrayManager;

/**
* @var CacheInterface
*/
private $cacheManager;

/**
* @param LocatorInterface $locator
* @param CategoryCollectionFactory $categoryCollectionFactory
Expand All @@ -68,6 +83,21 @@ public function __construct(
$this->arrayManager = $arrayManager;
}

/**
* Retrieve cache interface
*
* @return CacheInterface
* @deprecated
*/
private function getCacheManager()
{
if (!$this->cacheManager) {
$this->cacheManager = ObjectManager::getInstance()
->get(CacheInterface::class);
}
return $this->cacheManager;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -254,8 +284,9 @@ protected function customizeCategoriesField(array $meta)
*/
protected function getCategoriesTree($filter = null)
{
if (isset($this->categoriesTrees[$filter])) {
return $this->categoriesTrees[$filter];
$categoryTree = $this->getCacheManager()->load(self::CATEGORY_TREE_ID . '_' . $filter);
if ($categoryTree) {
return unserialize($categoryTree);
}

$storeId = $this->locator->getStore()->getId();
Expand Down Expand Up @@ -307,9 +338,16 @@ protected function getCategoriesTree($filter = null)
$categoryById[$category->getId()]['label'] = $category->getName();
$categoryById[$category->getParentId()]['optgroup'][] = &$categoryById[$category->getId()];
}

$this->getCacheManager()->save(
serialize($categoryById[CategoryModel::TREE_ROOT_ID]['optgroup']),
self::CATEGORY_TREE_ID . '_' . $filter,
[
\Magento\Catalog\Model\Category::CACHE_TAG,
\Magento\Framework\App\Cache\Type\Block::CACHE_TAG
]
);

$this->categoriesTrees[$filter] = $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup'];

return $this->categoriesTrees[$filter];
return $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,13 @@
},

save : function() {
var block;

if ($('messages')) {
$('messages').update();
} else {
block = jQuery('<div/>').attr('id', 'messages');
jQuery('.page-main-actions').after(block);
}
TreePanels.rebuildTrees();
if(!jQuery('#set-prop-form').valid()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

// @codingStandardsIgnoreFile
?>
<div class="widget block block-product-link-inline">
<span class="widget block block-product-link-inline">
<a <?php /* @escapeNotVerified */ echo $block->getLinkAttributes() ?>><span><?php echo $block->escapeHtml($block->getLabel()) ?></span></a>
</div>
</span>
Loading

0 comments on commit 64b86ca

Please sign in to comment.