Skip to content

Commit 1fc41c0

Browse files
author
Dmytro Poperechnyy
committed
Merge remote-tracking branch 'tango/MAGETWO-37560' into S53_bugs
2 parents 1506cf4 + 2acd1df commit 1fc41c0

File tree

7 files changed

+522
-13
lines changed

7 files changed

+522
-13
lines changed

app/code/Magento/Catalog/Block/Adminhtml/Category/Widget/Chooser.php

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public function getCategoryCollection()
172172
*
173173
* @param bool|null $expanded
174174
* @return string
175+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
175176
*/
176177
public function getLoadTreeUrl($expanded = null)
177178
{

app/code/Magento/Catalog/view/adminhtml/templates/catalog/category/widget/tree.phtml

100644100755
File mode changed.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* Category chooser for widget's layout updates
9+
*
10+
* @author Magento Core Team <core@magentocommerce.com>
11+
*/
12+
namespace Magento\Widget\Block\Adminhtml\Widget\Catalog\Category;
13+
14+
class Chooser extends \Magento\Catalog\Block\Adminhtml\Category\Widget\Chooser
15+
{
16+
/**
17+
* Get JSON of a tree node or an associative array
18+
*
19+
* @param \Magento\Framework\Data\Tree\Node|array $node
20+
* @param int $level
21+
* @return string
22+
*/
23+
protected function _getNodeJson($node, $level = 0)
24+
{
25+
$item = parent::_getNodeJson($node, $level);
26+
$item['level'] = $node->getLevel();
27+
return $item;
28+
}
29+
}

app/code/Magento/Widget/Controller/Adminhtml/Widget/Instance/Categories.php

100644100755
Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,52 @@
88

99
class Categories extends \Magento\Widget\Controller\Adminhtml\Widget\Instance
1010
{
11+
/**
12+
* @var \Magento\Framework\View\Layout
13+
*/
14+
protected $layout;
15+
16+
/**
17+
* @param \Magento\Backend\App\Action\Context $context
18+
* @param \Magento\Framework\Registry $coreRegistry
19+
* @param \Magento\Widget\Model\Widget\InstanceFactory $widgetFactory
20+
* @param \Psr\Log\LoggerInterface $logger
21+
* @param \Magento\Framework\Math\Random $mathRandom
22+
* @param \Magento\Framework\Translate\InlineInterface $translateInline
23+
* @param \Magento\Framework\View\Layout $layout
24+
*/
25+
public function __construct(
26+
\Magento\Backend\App\Action\Context $context,
27+
\Magento\Framework\Registry $coreRegistry,
28+
\Magento\Widget\Model\Widget\InstanceFactory $widgetFactory,
29+
\Psr\Log\LoggerInterface $logger,
30+
\Magento\Framework\Math\Random $mathRandom,
31+
\Magento\Framework\Translate\InlineInterface $translateInline,
32+
\Magento\Framework\View\Layout $layout
33+
) {
34+
$this->layout = $layout;
35+
parent::__construct($context, $coreRegistry, $widgetFactory, $logger, $mathRandom, $translateInline);
36+
}
37+
1138
/**
1239
* Categories chooser Action (Ajax request)
1340
*
14-
* @return void
41+
* @return \Magento\Framework\Controller\Result\Raw
1542
*/
1643
public function execute()
1744
{
1845
$selected = $this->getRequest()->getParam('selected', '');
1946
$isAnchorOnly = $this->getRequest()->getParam('is_anchor_only', 0);
20-
$chooser = $this->_view->getLayout()->createBlock(
21-
'Magento\Catalog\Block\Adminhtml\Category\Widget\Chooser'
22-
)->setUseMassaction(
23-
true
24-
)->setId(
25-
$this->mathRandom->getUniqueHash('categories')
26-
)->setIsAnchorOnly(
27-
$isAnchorOnly
28-
)->setSelectedCategories(
29-
explode(',', $selected)
30-
);
31-
$this->setBody($chooser->toHtml());
47+
48+
/** @var \Magento\Widget\Block\Adminhtml\Widget\Catalog\Category\Chooser $chooser */
49+
$chooser = $this->layout->createBlock('Magento\Widget\Block\Adminhtml\Widget\Catalog\Category\Chooser')
50+
->setUseMassaction(true)
51+
->setId($this->mathRandom->getUniqueHash('categories'))
52+
->setIsAnchorOnly($isAnchorOnly)
53+
->setSelectedCategories(explode(',', $selected));
54+
55+
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
56+
$resultRaw = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_RAW);
57+
return $resultRaw->setContents($chooser->toHtml());
3258
}
3359
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Widget\Test\Unit\Block\Adminhtml\Widget\Catalog\Category;
8+
9+
class ChooserTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @var \Magento\Catalog\Model\Resource\Category\Collection|\PHPUnit_Framework_MockObject_MockObject
13+
*/
14+
protected $collection;
15+
16+
/**
17+
* @var \Magento\Framework\Data\Tree\Node|\PHPUnit_Framework_MockObject_MockObject
18+
*/
19+
protected $childNode;
20+
21+
/**
22+
* @var \Magento\Framework\Data\Tree\Node|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
protected $rootNode;
25+
26+
/**
27+
* @var \Magento\Catalog\Model\Resource\Category\Tree|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
protected $categoryTree;
30+
31+
/**
32+
* @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
protected $store;
35+
36+
/**
37+
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
protected $storeManager;
40+
41+
/**
42+
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
protected $request;
45+
46+
/**
47+
* @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
protected $escaper;
50+
51+
/**
52+
* @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
53+
*/
54+
protected $eventManager;
55+
56+
/**
57+
* @var \Magento\Backend\Block\Template\Context|\PHPUnit_Framework_MockObject_MockObject
58+
*/
59+
protected $context;
60+
61+
public function setUp()
62+
{
63+
$this->collection = $this->getMock('Magento\Catalog\Model\Resource\Category\Collection', [], [], '', false);
64+
65+
$this->childNode = $this->getMock(
66+
'Magento\Framework\Data\Tree\Node',
67+
['getLevel', 'hasChildren'],
68+
[],
69+
'',
70+
false
71+
);
72+
$this->rootNode = $this->getMock(
73+
'Magento\Framework\Data\Tree\Node',
74+
['getLevel', 'hasChildren', 'getChildren'],
75+
[],
76+
'',
77+
false
78+
);
79+
$this->categoryTree = $this->getMock('Magento\Catalog\Model\Resource\Category\Tree', [], [], '', false);
80+
$this->store = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
81+
$this->storeManager = $this->getMock('Magento\Store\Model\StoreManagerInterface', [], [], '', false);
82+
$this->request = $this->getMock('Magento\Framework\App\RequestInterface', [], [], '', false);
83+
$this->escaper = $this->getMock('Magento\Framework\Escaper', [], [], '', false);
84+
$this->eventManager = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false);
85+
$this->context = $this->getMock('Magento\Backend\Block\Template\Context', [], [], '', false);
86+
}
87+
88+
public function testGetTreeHasLevelField()
89+
{
90+
$rootId = \Magento\Catalog\Model\Category::TREE_ROOT_ID;
91+
$storeGroups = [];
92+
$storeId = 1;
93+
$rootLevel = 2;
94+
$level = 3;
95+
96+
$this->collection->expects($this->any())->method('addAttributeToSelect')->willReturnMap(
97+
[
98+
['url_key', false, $this->collection],
99+
['is_anchor', false, $this->collection]
100+
]
101+
);
102+
103+
$this->childNode->expects($this->atLeastOnce())->method('getLevel')->willReturn($level);
104+
105+
$this->rootNode->expects($this->atLeastOnce())->method('getLevel')->willReturn($rootLevel);
106+
$this->rootNode->expects($this->once())->method('hasChildren')->willReturn(true);
107+
$this->rootNode->expects($this->once())->method('getChildren')->willReturn([$this->childNode]);
108+
109+
$this->categoryTree->expects($this->once())->method('load')->with(null, 3)->willReturnSelf();
110+
$this->categoryTree->expects($this->atLeastOnce())
111+
->method('addCollectionData')
112+
->with($this->collection)
113+
->willReturnSelf();
114+
$this->categoryTree->expects($this->once())->method('getNodeById')->with($rootId)->willReturn($this->rootNode);
115+
116+
$this->store->expects($this->atLeastOnce())->method('getId')->willReturn($storeId);
117+
118+
$this->storeManager->expects($this->once())->method('getGroups')->willReturn($storeGroups);
119+
$this->storeManager->expects($this->atLeastOnce())->method('getStore')->willReturn($this->store);
120+
121+
$this->context->expects($this->once())->method('getStoreManager')->willReturn($this->storeManager);
122+
$this->context->expects($this->once())->method('getRequest')->willReturn($this->request);
123+
$this->context->expects($this->once())->method('getEscaper')->willReturn($this->escaper);
124+
$this->context->expects($this->once())->method('getEventManager')->willReturn($this->eventManager);
125+
126+
/** @var \Magento\Widget\Block\Adminhtml\Widget\Catalog\Category\Chooser $chooser */
127+
$chooser = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))
128+
->getObject(
129+
'Magento\Widget\Block\Adminhtml\Widget\Catalog\Category\Chooser',
130+
[
131+
'categoryTree' => $this->categoryTree,
132+
'context' => $this->context
133+
]
134+
);
135+
$chooser->setData('category_collection', $this->collection);
136+
$result = $chooser->getTree();
137+
$this->assertEquals($level, $result[0]['level']);
138+
}
139+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Widget\Test\Unit\Controller\Adminhtml\Widget\Instance;
8+
9+
class CategoriesTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
13+
*/
14+
protected $request;
15+
16+
/**
17+
* @var \Magento\Framework\Math\Random|\PHPUnit_Framework_MockObject_MockObject
18+
*/
19+
protected $mathRandom;
20+
21+
/**
22+
* @var \Magento\Framework\View\Layout|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
protected $chooser;
25+
26+
/**
27+
* @var string
28+
*/
29+
protected $blockClass = 'Magento\Widget\Block\Adminhtml\Widget\Catalog\Category\Chooser';
30+
31+
/**
32+
* @var \Magento\Framework\View\Layout|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
protected $layout;
35+
36+
/**
37+
* @var \Magento\Framework\Controller\Result\Raw|\PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
protected $resultRaw;
40+
41+
/**
42+
* @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
protected $resultFactory;
45+
46+
/**
47+
* @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
protected $context;
50+
51+
/**
52+
* @var \Magento\Widget\Controller\Adminhtml\Widget\Instance\Categories
53+
*/
54+
protected $controller;
55+
56+
public function setUp()
57+
{
58+
$this->request = $this->getMock('Magento\Framework\App\RequestInterface', [], [], '', false);
59+
$this->mathRandom = $this->getMock('Magento\Framework\Math\Random', [], [], '', false);
60+
$this->chooser = $this->getMock(
61+
$this->blockClass,
62+
['setUseMassaction', 'setId', 'setIsAnchorOnly', 'setSelectedCategories', 'toHtml'],
63+
[],
64+
'',
65+
false
66+
);
67+
$this->layout = $this->getMock('Magento\Framework\View\Layout', [], [], '', false);
68+
$this->resultRaw = $this->getMock('Magento\Framework\Controller\Result\Raw', [], [], '', false);
69+
$this->resultFactory = $this->getMock('Magento\Framework\Controller\ResultFactory', [], [], '', false);
70+
$this->context = $this->getMock('Magento\Backend\App\Action\Context', [], [], '', false);
71+
}
72+
73+
public function testExecute()
74+
{
75+
$selectedCategories = '1';
76+
$isAnchorOnly = true;
77+
$hash = '7e6baeca2d76ca0efc3a299986d31bdc9cd796fb';
78+
$content = 'block_content';
79+
80+
$this->request->expects($this->any())->method('getParam')->willReturnMap(
81+
[
82+
['selected', '', $selectedCategories],
83+
['is_anchor_only', 0, $isAnchorOnly]
84+
]
85+
);
86+
87+
$this->mathRandom->expects($this->once())->method('getUniqueHash')->with('categories')->willReturn($hash);
88+
89+
$this->chooser->expects($this->once())->method('setUseMassaction')->with()->willReturnSelf();
90+
$this->chooser->expects($this->once())->method('setId')->with($hash)->willReturnSelf();
91+
$this->chooser->expects($this->once())->method('setIsAnchorOnly')->with($isAnchorOnly)->willReturnSelf();
92+
$this->chooser->expects($this->once())
93+
->method('setSelectedCategories')
94+
->with(explode(',', $selectedCategories))
95+
->willReturnSelf();
96+
$this->chooser->expects($this->once())->method('toHtml')->willReturn($content);
97+
98+
$this->layout->expects($this->once())
99+
->method('createBlock')
100+
->with($this->blockClass)
101+
->willReturn($this->chooser);
102+
103+
$this->resultRaw->expects($this->once())->method('setContents')->with($content)->willReturnSelf();
104+
105+
$this->resultFactory->expects($this->once())
106+
->method('create')
107+
->with(\Magento\Framework\Controller\ResultFactory::TYPE_RAW)
108+
->willReturn($this->resultRaw);
109+
110+
$this->context->expects($this->once())->method('getRequest')->willReturn($this->request);
111+
$this->context->expects($this->once())->method('getResultFactory')->willReturn($this->resultFactory);
112+
113+
/** @var \Magento\Widget\Controller\Adminhtml\Widget\Instance\Categories $controller */
114+
$this->controller = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))
115+
->getObject(
116+
'Magento\Widget\Controller\Adminhtml\Widget\Instance\Categories',
117+
[
118+
'context' => $this->context,
119+
'mathRandom' => $this->mathRandom,
120+
'layout' => $this->layout
121+
]
122+
);
123+
$this->assertSame($this->resultRaw, $this->controller->execute());
124+
}
125+
}

0 commit comments

Comments
 (0)