Skip to content

Commit f3e7f23

Browse files
committed
MAGETWO-56871: [GITHUB] Non-admin do not have permission to search for Categories in Cart Price Rules #6168
1 parent 67c29c6 commit f3e7f23

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2016 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\SalesRule\Controller\Adminhtml\Promo\Widget;
8+
9+
class Chooser extends \Magento\CatalogRule\Controller\Adminhtml\Promo\Widget\Chooser
10+
{
11+
/**
12+
* Authorization level of a basic admin session
13+
*
14+
* @see _isAllowed()
15+
*/
16+
const ADMIN_RESOURCE = 'Magento_SalesRule::quote';
17+
}

app/code/Magento/SalesRule/Model/Rule/Condition/Product.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,26 @@ public function validate(\Magento\Framework\Model\AbstractModel $model)
5151

5252
return parent::validate($product);
5353
}
54+
55+
/**
56+
* Retrieve value element chooser URL
57+
*
58+
* @return string
59+
*/
60+
public function getValueElementChooserUrl()
61+
{
62+
$url = false;
63+
switch ($this->getAttribute()) {
64+
case 'sku':
65+
case 'category_ids':
66+
$url = 'sales_rule/promo_widget/chooser/attribute/' . $this->getAttribute();
67+
if ($this->getJsFormObject()) {
68+
$url .= '/form/' . $this->getJsFormObject();
69+
}
70+
break;
71+
default:
72+
break;
73+
}
74+
return $url !== false ? $this->_backendData->getUrl($url) : '';
75+
}
5476
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\SalesRule\Test\Unit\Model\Rule\Condition;
7+
8+
use \Magento\Rule\Model\Condition\Context;
9+
use \Magento\Backend\Helper\Data;
10+
use \Magento\Eav\Model\Config;
11+
use \Magento\Catalog\Model\ProductFactory;
12+
use \Magento\Catalog\Api\ProductRepositoryInterface;
13+
use \Magento\Eav\Model\Entity\AbstractEntity;
14+
use \Magento\Catalog\Model\ResourceModel\Product;
15+
use \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection;
16+
use \Magento\Framework\Locale\FormatInterface;
17+
use \Magento\SalesRule\Model\Rule\Condition\Product as SalesRuleProduct;
18+
19+
class ProductTest extends \PHPUnit_Framework_TestCase
20+
{
21+
/** @var \Magento\SalesRule\Model\Rule\Condition\Product */
22+
protected $model;
23+
24+
/** @var \Magento\Rule\Model\Condition\Context|\PHPUnit_Framework_MockObject_MockObject */
25+
protected $contextMock;
26+
27+
/** @var \Magento\Backend\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
28+
protected $backendHelperMock;
29+
30+
/** @var \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject */
31+
protected $configMock;
32+
33+
/** @var \Magento\Catalog\Model\ProductFactory|\PHPUnit_Framework_MockObject_MockObject */
34+
protected $productFactoryMock;
35+
36+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
37+
protected $productRepositoryMock;
38+
39+
/** @var \Magento\Catalog\Model\ResourceModel\Product|\PHPUnit_Framework_MockObject_MockObject */
40+
protected $productMock;
41+
42+
/** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection|\PHPUnit_Framework_MockObject_MockObject */
43+
protected $collectionMock;
44+
45+
/** @var \Magento\Framework\Locale\FormatInterface|\PHPUnit_Framework_MockObject_MockObject */
46+
protected $formatMock;
47+
48+
/** @var \Magento\Eav\Model\Entity\AttributeLoaderInterface|\PHPUnit_Framework_MockObject_MockObject */
49+
protected $attributeLoaderInterfaceMock;
50+
51+
/**
52+
* Setup the test
53+
*/
54+
protected function setUp()
55+
{
56+
$this->contextMock = $this->getMockBuilder(Context::class)
57+
->disableOriginalConstructor()
58+
->getMock();
59+
$this->backendHelperMock = $this->getMockBuilder(Data::class)
60+
->disableOriginalConstructor()
61+
->getMock();
62+
$this->configMock = $this->getMockBuilder(Config::class)
63+
->disableOriginalConstructor()
64+
->getMock();
65+
$this->productFactoryMock = $this->getMockBuilder(ProductFactory::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
$this->productRepositoryMock = $this->getMockBuilder(ProductRepositoryInterface::class)
69+
->getMockForAbstractClass();
70+
$this->attributeLoaderInterfaceMock = $this->getMockBuilder(AbstractEntity::class)
71+
->disableOriginalConstructor()
72+
->setMethods(['getAttributesByCode'])
73+
->getMock();
74+
$this->attributeLoaderInterfaceMock
75+
->expects($this->any())
76+
->method('getAttributesByCode')
77+
->will($this->returnValue([]));
78+
$this->productMock = $this->getMockBuilder(Product::class)
79+
->disableOriginalConstructor()
80+
->setMethods(['loadAllAttributes'])
81+
->getMock();
82+
$this->productMock
83+
->expects($this->any())
84+
->method('loadAllAttributes')
85+
->will($this->returnValue($this->attributeLoaderInterfaceMock));
86+
$this->collectionMock = $this->getMockBuilder(Collection::class)
87+
->disableOriginalConstructor()
88+
->getMock();
89+
$this->formatMock = $this->getMockBuilder(FormatInterface::class)
90+
->getMockForAbstractClass();
91+
$this->model = new SalesRuleProduct(
92+
$this->contextMock,
93+
$this->backendHelperMock,
94+
$this->configMock,
95+
$this->productFactoryMock,
96+
$this->productRepositoryMock,
97+
$this->productMock,
98+
$this->collectionMock,
99+
$this->formatMock
100+
);
101+
}
102+
103+
/**
104+
* @return array
105+
*/
106+
public function testGetValueElementChooserUrlDataProvider()
107+
{
108+
return [
109+
'category_ids_without_js_object' => [
110+
'category_ids',
111+
'sales_rule/promo_widget/chooser/attribute/'
112+
],
113+
'category_ids_with_js_object' => [
114+
'category_ids',
115+
'sales_rule/promo_widget/chooser/attribute/',
116+
'jsobject'
117+
],
118+
'sku_without_js_object' => [
119+
'sku',
120+
'sales_rule/promo_widget/chooser/attribute/',
121+
'jsobject'
122+
],
123+
'sku_without_with_js_object' => [
124+
'sku',
125+
'sales_rule/promo_widget/chooser/attribute/'
126+
],
127+
'none' => [
128+
'',
129+
''
130+
]
131+
];
132+
}
133+
134+
/**
135+
* test getValueElementChooserUrl
136+
* @param string $attribute
137+
* @param string $url
138+
* @param string $jsObject
139+
* @dataProvider testGetValueElementChooserUrlDataProvider
140+
*/
141+
public function testGetValueElementChooserUrl($attribute, $url, $jsObject = '')
142+
{
143+
$this->model->setJsFormObject($jsObject);
144+
$this->model->setAttribute($attribute);
145+
$url .= $this->model->getAttribute();
146+
$this->backendHelperMock
147+
->expects($this->any())
148+
->method('getUrl')
149+
->willReturnArgument(0);
150+
151+
if ($this->model->getJsFormObject()) {
152+
$url .= '/form/' . $this->model->getJsFormObject();
153+
}
154+
155+
$this->assertEquals($url, $this->model->getValueElementChooserUrl());
156+
}
157+
}

0 commit comments

Comments
 (0)