Skip to content

Commit

Permalink
Merge pull request #277 from magento-goinc/MAGETWO-35516
Browse files Browse the repository at this point in the history
[GoInc] Public GitHub Pull Requests
  • Loading branch information
orlangur committed May 14, 2015
2 parents be89bd9 + 12cf049 commit 271aec2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 38 deletions.
47 changes: 29 additions & 18 deletions app/code/Magento/Catalog/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,10 @@ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block
if (!$category->getIsActive()) {
continue;
}

$nodeId = 'category-node-' . $category->getId();

$block->addIdentity(\Magento\Catalog\Model\Category::CACHE_TAG . '_' . $category->getId());

$tree = $parentCategoryNode->getTree();

$isActiveCategory = false;
/** @var \Magento\Catalog\Model\Category $currentCategory */
$currentCategory = $this->_registry->registry('current_category');
if ($currentCategory && $currentCategory->getId() == $category->getId()) {
$isActiveCategory = true;
}

$categoryData = [
'name' => $category->getName(),
'id' => $nodeId,
'url' => $this->_catalogCategory->getCategoryUrl($category),
'has_active' => $this->hasActive($category),
'is_active' => $isActiveCategory
];
$categoryData = $this->getMenuCategoryData($category);
$categoryNode = new \Magento\Framework\Data\Tree\Node($categoryData, 'id', $tree, $parentCategoryNode);
$parentCategoryNode->addChild($categoryNode);

Expand All @@ -183,6 +166,34 @@ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block
}
}

/**
* Get category data to be added to the Menu
*
* @param \Magento\Framework\Data\Tree\Node $category
* @return array
*/
public function getMenuCategoryData($category)
{
$nodeId = 'category-node-' . $category->getId();

$isActiveCategory = false;
/** @var \Magento\Catalog\Model\Category $currentCategory */
$currentCategory = $this->_registry->registry('current_category');
if ($currentCategory && $currentCategory->getId() == $category->getId()) {
$isActiveCategory = true;
}

$categoryData = [
'name' => $category->getName(),
'id' => $nodeId,
'url' => $this->_catalogCategory->getCategoryUrl($category),
'has_active' => $this->hasActive($category),
'is_active' => $isActiveCategory,
];

return $categoryData;
}

/**
* Checks whether category belongs to active category's path
*
Expand Down
33 changes: 27 additions & 6 deletions app/code/Magento/Catalog/Test/Unit/Model/ObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ class ObserverTest extends \PHPUnit_Framework_TestCase
protected $_observer;

/**
* @var \Magento\Catalog\Helper\Category
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Helper\Category
*/
protected $_catalogCategory;

/**
* @var \Magento\Catalog\Model\Category
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Category
*/
protected $_category;

/**
* @var \Magento\Catalog\Model\Category
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Category
*/
protected $_childrenCategory;

/**
* @var \Magento\Catalog\Model\Indexer\Category\Flat\State
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Indexer\Category\Flat\State
*/
protected $_categoryFlatState;

/**
* @var \Magento\Store\Model\StoreManagerInterface
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;

Expand All @@ -64,11 +64,13 @@ public function setUp()
->disableOriginalConstructor()
->getMock();

$layerResolver = $this->_getCleanMock('Magento\Catalog\Model\Layer\Resolver');
$layerResolver->expects($this->once())->method('get')->willReturn(null);
$this->_observer = (new ObjectManager($this))->getObject('Magento\Catalog\Model\Observer', [
'categoryResource' => $this->_getCleanMock('\Magento\Catalog\Model\Resource\Category'),
'catalogProduct' => $this->_getCleanMock('\Magento\Catalog\Model\Resource\Product'),
'storeManager' => $this->_storeManager,
'catalogLayer' => $this->_getCleanMock('\Magento\Catalog\Model\Layer\Category'),
'layerResolver' => $layerResolver,
'indexIndexer' => $this->_getCleanMock('\Magento\Index\Model\Indexer'),
'catalogCategory' => $this->_catalogCategory,
'catalogData' => $this->_getCleanMock('\Magento\Catalog\Helper\Data'),
Expand Down Expand Up @@ -176,4 +178,23 @@ public function testAddCatalogToTopMenuItemsWithFlat()

$this->_observer->addCatalogToTopmenuItems($observer);
}

public function testGetMenuCategoryData()
{
$category = $this->getMock('Magento\Catalog\Model\Category', ['getId', 'getName'], [], '', false);
$category->expects($this->once())->method('getId')->willReturn('id');
$category->expects($this->once())->method('getName')->willReturn('name');
$this->_catalogCategory->expects($this->once())->method('getCategoryUrl')->willReturn('url');

$this->assertEquals(
[
'name' => 'name',
'id' => 'category-node-id',
'url' => 'url',
'is_active' => false,
'has_active' => false,
],
$this->_observer->getMenuCategoryData($category)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<td id="delete_button_container_<%- data.id %>" class="col-delete">
<input type="hidden" class="delete-flag" name="option[delete][<%- data.id %>]" value="" />
<?php if (!$block->getReadOnly() && !$block->canManageOptionDefaultOnly()):?>
<button title="<?php echo __('Delete') ?>" type="button"
<button id="delete_button_<%- data.id %>" title="<?php echo __('Delete') ?>" type="button"
class="action- scalable delete delete-option"
>
<span><?php echo __('Delete') ?></span>
Expand Down Expand Up @@ -100,7 +100,6 @@ require([
if (isNewOption && !this.isReadOnly) {
this.enableNewOptionDeleteButton(data.id);
}
this.bindRemoveButtons();
this.itemCount++;
this.totalItems++;
this.updateItemsCountField();
Expand Down Expand Up @@ -139,22 +138,16 @@ require([
button.removeClassName('disabled');
});
},
bindRemoveButtons: function() {
var buttons = $$('.delete-option');
for (var i = 0; i < buttons.length; i++) {
if (!$(buttons[i]).binded) {
$(buttons[i]).binded = true;
Event.observe(buttons[i], 'click', this.remove.bind(this));
}
}
}
};

attributeOption.bindRemoveButtons();

if ($('add_new_option_button')) {
Event.observe('add_new_option_button', 'click', attributeOption.add.bind(attributeOption));
}

$('manage-options-panel').on('click', '.delete-option', function(event, element) {
attributeOption.remove(event);
});

<?php foreach ($block->getOptionValues() as $_value): ?>
attributeOption.add(<?php echo $_value->toJson() ?>);
<?php endforeach; ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ define([
if (response.error) {
alert(response.message);
} else {
if (this.element.find(this.options.categoryIdSelector).prop('value') === response.id) {
if (this.element.find(this.options.categoryIdSelector).prop('value') == response.id) {
this.element.find(this.options.categoryPathSelector)
.prop('value', response.path);
}
Expand Down

0 comments on commit 271aec2

Please sign in to comment.