Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 3b42fe9

Browse files
🔃 [EngCom] Public Pull Requests - 2.3-develop
Accepted Public Pull Requests: - magento/magento2#14464: [Forwardport] test command inside if/then clause broke before install script (by @mastiuhin-olexandr) - magento/magento2#14463: [Forwardport] Removed extra backslash from comment block (by @mastiuhin-olexandr) - magento/magento2#14444: [Forwardport] magento/magento2#10057 (by @mastiuhin-olexandr) - magento/magento2#12577: [Backport 2.3] Added Visibility and Status filter to category product grid (by @peterjaap) Fixed GitHub Issues: - magento/magento2#10057: Editing order with backordered items results in new order not correctly marking order items as backordered (reported by @develpr) has been fixed in magento/magento2#14444 by @mastiuhin-olexandr in 2.3-develop branch Related commits: 1. 1f008f4
2 parents b0d58cc + 14e095b commit 3b42fe9

File tree

12 files changed

+216
-11
lines changed

12 files changed

+216
-11
lines changed

app/code/Magento/Catalog/Block/Adminhtml/Category/Tab/Product.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Magento\Backend\Block\Widget\Grid;
1515
use Magento\Backend\Block\Widget\Grid\Column;
1616
use Magento\Backend\Block\Widget\Grid\Extended;
17+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
18+
use Magento\Catalog\Model\Product\Visibility;
19+
use Magento\Framework\App\ObjectManager;
1720

1821
class Product extends \Magento\Backend\Block\Widget\Grid\Extended
1922
{
@@ -29,22 +32,38 @@ class Product extends \Magento\Backend\Block\Widget\Grid\Extended
2932
*/
3033
protected $_productFactory;
3134

35+
/**
36+
* @var Status
37+
*/
38+
private $status;
39+
40+
/**
41+
* @var Visibility
42+
*/
43+
private $visibility;
44+
3245
/**
3346
* @param \Magento\Backend\Block\Template\Context $context
3447
* @param \Magento\Backend\Helper\Data $backendHelper
3548
* @param \Magento\Catalog\Model\ProductFactory $productFactory
3649
* @param \Magento\Framework\Registry $coreRegistry
3750
* @param array $data
51+
* @param Visibility|null $visibility
52+
* @param Status|null $status
3853
*/
3954
public function __construct(
4055
\Magento\Backend\Block\Template\Context $context,
4156
\Magento\Backend\Helper\Data $backendHelper,
4257
\Magento\Catalog\Model\ProductFactory $productFactory,
4358
\Magento\Framework\Registry $coreRegistry,
44-
array $data = []
59+
array $data = [],
60+
Visibility $visibility = null,
61+
Status $status = null
4562
) {
4663
$this->_productFactory = $productFactory;
4764
$this->_coreRegistry = $coreRegistry;
65+
$this->visibility = $visibility ?: ObjectManager::getInstance()->get(Visibility::class);
66+
$this->status = $status ?: ObjectManager::getInstance()->get(Status::class);
4867
parent::__construct($context, $backendHelper, $data);
4968
}
5069

@@ -102,6 +121,10 @@ protected function _prepareCollection()
102121
'name'
103122
)->addAttributeToSelect(
104123
'sku'
124+
)->addAttributeToSelect(
125+
'visibility'
126+
)->addAttributeToSelect(
127+
'status'
105128
)->addAttributeToSelect(
106129
'price'
107130
)->joinField(
@@ -159,6 +182,28 @@ protected function _prepareColumns()
159182
);
160183
$this->addColumn('name', ['header' => __('Name'), 'index' => 'name']);
161184
$this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku']);
185+
$this->addColumn(
186+
'visibility',
187+
[
188+
'header' => __('Visibility'),
189+
'index' => 'visibility',
190+
'type' => 'options',
191+
'options' => $this->visibility->getOptionArray(),
192+
'header_css_class' => 'col-visibility',
193+
'column_css_class' => 'col-visibility'
194+
]
195+
);
196+
197+
$this->addColumn(
198+
'status',
199+
[
200+
'header' => __('Status'),
201+
'index' => 'status',
202+
'type' => 'options',
203+
'options' => $this->status->getOptionArray()
204+
]
205+
);
206+
162207
$this->addColumn(
163208
'price',
164209
[

app/code/Magento/CatalogInventory/Model/Quote/Item/QuantityValidator.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ public function validate(Observer $observer)
104104
$quoteItem = $observer->getEvent()->getItem();
105105
if (!$quoteItem ||
106106
!$quoteItem->getProductId() ||
107-
!$quoteItem->getQuote() ||
108-
$quoteItem->getQuote()->getIsSuperMode()
107+
!$quoteItem->getQuote()
109108
) {
110109
return;
111110
}
@@ -118,6 +117,18 @@ public function validate(Observer $observer)
118117
throw new LocalizedException(__('The Product stock item is invalid. Verify the stock item and try again.'));
119118
}
120119

120+
if (($options = $quoteItem->getQtyOptions()) && $qty > 0) {
121+
foreach ($options as $option) {
122+
$this->optionInitializer->initialize($option, $quoteItem, $qty);
123+
}
124+
} else {
125+
$this->stockItemInitializer->initialize($stockItem, $quoteItem, $qty);
126+
}
127+
128+
if ($quoteItem->getQuote()->getIsSuperMode()) {
129+
return;
130+
}
131+
121132
/* @var \Magento\CatalogInventory\Api\Data\StockStatusInterface $stockStatus */
122133
$stockStatus = $this->stockRegistry->getStockStatus($product->getId(), $product->getStore()->getWebsiteId());
123134

@@ -160,7 +171,7 @@ public function validate(Observer $observer)
160171
/**
161172
* Check item for options
162173
*/
163-
if (($options = $quoteItem->getQtyOptions()) && $qty > 0) {
174+
if ($options) {
164175
$qty = $product->getTypeInstance()->prepareQuoteItemQty($qty, $product);
165176
$quoteItem->setData('qty', $qty);
166177
if ($stockStatus) {
@@ -194,7 +205,7 @@ public function validate(Observer $observer)
194205
$removeError = true;
195206

196207
foreach ($options as $option) {
197-
$result = $this->optionInitializer->initialize($option, $quoteItem, $qty);
208+
$result = $option->getStockStateResult();
198209
if ($result->getHasError()) {
199210
$option->setHasError(true);
200211
//Setting this to false, so no error statuses are cleared
@@ -207,7 +218,7 @@ public function validate(Observer $observer)
207218
}
208219
} else {
209220
if ($quoteItem->getParentItem() === null) {
210-
$result = $this->stockItemInitializer->initialize($stockItem, $quoteItem, $qty);
221+
$result = $quoteItem->getStockStateResult();
211222
if ($result->getHasError()) {
212223
$this->addErrorInfoToQuote($result, $quoteItem);
213224
} else {

app/code/Magento/CatalogInventory/Model/Quote/Item/QuantityValidator/Initializer/Option.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public function initialize(
133133

134134
$stockItem->unsIsChildItem();
135135

136+
$option->setStockStateResult($result);
137+
136138
return $result;
137139
}
138140
}

app/code/Magento/CatalogInventory/Model/Quote/Item/QuantityValidator/Initializer/StockItem.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public function initialize(
135135
$quoteItem->setBackorders($result->getItemBackorders());
136136
}
137137

138+
$quoteItem->setStockStateResult($result);
139+
138140
return $result;
139141
}
140142
}

app/code/Magento/CatalogInventory/Test/Unit/Model/Quote/Item/QuantityValidator/Initializer/QuantityValidatorTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,11 @@ public function testValidateWithOptions()
278278
{
279279
$optionMock = $this->getMockBuilder(OptionItem::class)
280280
->disableOriginalConstructor()
281-
->setMethods(['setHasError'])
281+
->setMethods(['setHasError', 'getStockStateResult'])
282282
->getMock();
283+
$optionMock->expects($this->once())
284+
->method('getStockStateResult')
285+
->willReturn($this->resultMock);
283286
$this->stockRegistryMock->expects($this->at(0))
284287
->method('getStockItem')
285288
->willReturn($this->stockItemMock);
@@ -316,14 +319,17 @@ public function testValidateWithOptionsAndError()
316319
{
317320
$optionMock = $this->getMockBuilder(OptionItem::class)
318321
->disableOriginalConstructor()
319-
->setMethods(['setHasError'])
322+
->setMethods(['setHasError', 'getStockStateResult'])
320323
->getMock();
321324
$this->stockRegistryMock->expects($this->at(0))
322325
->method('getStockItem')
323326
->willReturn($this->stockItemMock);
324327
$this->stockRegistryMock->expects($this->at(1))
325328
->method('getStockStatus')
326329
->willReturn($this->stockStatusMock);
330+
$optionMock->expects($this->once())
331+
->method('getStockStateResult')
332+
->willReturn($this->resultMock);
327333
$options = [$optionMock];
328334
$this->createInitialStub(1);
329335
$this->setUpStubForQuantity(1, true);
@@ -354,12 +360,15 @@ public function testValidateAndRemoveErrorsFromQuote()
354360
{
355361
$optionMock = $this->getMockBuilder(OptionItem::class)
356362
->disableOriginalConstructor()
357-
->setMethods(['setHasError'])
363+
->setMethods(['setHasError', 'getStockStateResult'])
358364
->getMock();
359365
$quoteItem = $this->getMockBuilder(Item::class)
360366
->disableOriginalConstructor()
361367
->setMethods(['getItemId', 'getErrorInfos'])
362368
->getMock();
369+
$optionMock->expects($this->once())
370+
->method('getStockStateResult')
371+
->willReturn($this->resultMock);
363372
$this->stockRegistryMock->expects($this->at(0))
364373
->method('getStockItem')
365374
->willReturn($this->stockItemMock);

app/code/Magento/CatalogInventory/Test/Unit/Model/Quote/Item/QuantityValidator/Initializer/StockItemTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function testInitializeWithSubitem()
8484
'setMessage',
8585
'setBackorders',
8686
'__wakeup',
87+
'setStockStateResult'
8788
]
8889
)
8990
->disableOriginalConstructor()
@@ -178,6 +179,7 @@ public function testInitializeWithSubitem()
178179
$quoteItem->expects($this->once())->method('setMessage')->with('message')->will($this->returnSelf());
179180
$result->expects($this->exactly(2))->method('getItemBackorders')->will($this->returnValue('backorders'));
180181
$quoteItem->expects($this->once())->method('setBackorders')->with('backorders')->will($this->returnSelf());
182+
$quoteItem->expects($this->once())->method('setStockStateResult')->with($result)->will($this->returnSelf());
181183

182184
$this->model->initialize($stockItem, $quoteItem, $qty);
183185
}

app/code/Magento/InstantPurchase/CustomerData/InstantPurchase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class InstantPurchase implements SectionSourceInterface
3030
private $customerSession;
3131

3232
/**
33-
* @var StoreManagerInterface\
33+
* @var StoreManagerInterface
3434
*/
3535
private $storeManager;
3636

dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Category/Edit/Section/ProductGrid.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ class ProductGrid extends Grid
2929
'name' => [
3030
'selector' => '#catalog_category_products_filter_name',
3131
],
32+
'visibility' => [
33+
'selector' => '#catalog_category_products_filter_visibility',
34+
'input' => 'select',
35+
],
36+
'status' => [
37+
'selector' => '#catalog_category_products_filter_status',
38+
'input' => 'select',
39+
],
3240
];
3341

3442
/**
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Catalog\Test\Constraint;
10+
11+
use Magento\Catalog\Test\Fixture\Category;
12+
use Magento\Catalog\Test\Page\Adminhtml\CatalogCategoryEdit;
13+
use Magento\Catalog\Test\Page\Adminhtml\CatalogCategoryIndex;
14+
use Magento\Mtf\Constraint\AbstractConstraint;
15+
16+
/**
17+
* Assert that category products grid filter works correctly.
18+
*/
19+
class AssertCategoryProductsGridFilter extends AbstractConstraint
20+
{
21+
/**
22+
* Grid columns for tests
23+
*
24+
* @var array
25+
*/
26+
private $testFilterColumns = [
27+
'visibility',
28+
];
29+
30+
/**
31+
* Assert that category products grid filter works correctly.
32+
*
33+
* @param CatalogCategoryIndex $catalogCategoryIndex
34+
* @param CatalogCategoryEdit $catalogCategoryEdit
35+
* @param Category $category
36+
* @return void
37+
*/
38+
public function processAssert(
39+
CatalogCategoryIndex $catalogCategoryIndex,
40+
CatalogCategoryEdit $catalogCategoryEdit,
41+
Category $category
42+
) {
43+
$catalogCategoryIndex->getTreeCategories()->selectCategory($category, true);
44+
$categoryProducts = $category->getDataFieldConfig('category_products')['source']->getProducts();
45+
$catalogCategoryEdit->getEditForm()->openSection('category_products');
46+
47+
foreach ($this->testFilterColumns as $field) {
48+
$this->testGridFilter($categoryProducts, $catalogCategoryEdit, $field);
49+
}
50+
}
51+
52+
/**
53+
* @param array $categoryProducts
54+
* @param CatalogCategoryEdit $catalogCategoryEdit
55+
* @param string $filterField
56+
* @return void
57+
*/
58+
private function testGridFilter(array $categoryProducts, CatalogCategoryEdit $catalogCategoryEdit, $filterField)
59+
{
60+
$expectedProducts = [];
61+
foreach ($categoryProducts as $product) {
62+
$expectedProducts[$product->getData('name')] = [
63+
'filter' => $filterField,
64+
'value' => $product->getData($filterField)
65+
];
66+
}
67+
68+
$actualProducts = [];
69+
/** @var \Magento\Catalog\Test\Block\Adminhtml\Category\Edit\CategoryForm $productsFieldset */
70+
$productsFieldset = $catalogCategoryEdit->getEditForm()->getSection('category_products');
71+
$gridRows = $productsFieldset->getProductGrid()->getRowsData(['name', $filterField]);
72+
foreach ($gridRows as $row) {
73+
$actualProducts[$row['name']] = [
74+
'filter' => $filterField,
75+
'value' => $row[$filterField]
76+
];
77+
}
78+
79+
\PHPUnit\Framework\Assert::assertEquals(
80+
$expectedProducts,
81+
$actualProducts,
82+
"Category products grid filter '$filterField' does not work correctly"
83+
);
84+
}
85+
86+
/**
87+
* Returns a string representation of the object.
88+
*
89+
* @return string
90+
*/
91+
public function toString() : string
92+
{
93+
return 'Category products grid filter works correctly';
94+
}
95+
}

dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Category/CreateCategoryEntityTest.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,15 @@
175175
<constraint name="Magento\Catalog\Test\Constraint\AssertCategorySaveMessage" />
176176
<constraint name="Magento\Catalog\Test\Constraint\AssertCategoryOnCustomWebsite" />
177177
</variation>
178+
<variation name="CreateCategoryEntityTestVariation11_ProductsGridFilter" summary="Apply category products grid filter">
179+
<data name="addCategory" xsi:type="string">addSubcategory</data>
180+
<data name="category/data/parent_id/dataset" xsi:type="string">default_category</data>
181+
<data name="category/data/is_active" xsi:type="string">Yes</data>
182+
<data name="category/data/include_in_menu" xsi:type="string">Yes</data>
183+
<data name="category/data/name" xsi:type="string">Subcategory%isolation%</data>
184+
<data name="category/data/category_products/dataset" xsi:type="string">catalogProductSimple::default, catalogProductSimple::not_visible_individually</data>
185+
<constraint name="Magento\Catalog\Test\Constraint\AssertCategorySaveMessage" />
186+
<constraint name="Magento\Catalog\Test\Constraint\AssertCategoryProductsGridFilter" />
187+
</variation>
178188
</testCase>
179189
</config>

dev/tests/integration/testsuite/Magento/CatalogInventory/Model/Quote/Item/QuantityValidatorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,31 @@ public function testQuoteWithOptionsWithErrors()
130130
$this->stockState->expects($this->any())->method('checkQtyIncrements')->willReturn($resultMock);
131131
$this->optionInitializer->expects($this->any())->method('initialize')->willReturn($resultMock);
132132
$resultMock->expects($this->any())->method('getHasError')->willReturn(true);
133+
$this->setMockStockStateResultToQuoteItemOptions($quoteItem, $resultMock);
133134
$this->observer->execute($this->observerMock);
134135
$this->assertCount(2, $quoteItem->getErrorInfos(), 'Expected 2 errors in QuoteItem');
135136
}
136137

138+
/**
139+
* Set mock of Stock State Result to Quote Item Options.
140+
*
141+
*
142+
* @param \Magento\Quote\Model\Quote\Item $quoteItem
143+
* @param \PHPUnit_Framework_MockObject_MockObject $resultMock
144+
*/
145+
private function setMockStockStateResultToQuoteItemOptions($quoteItem, $resultMock)
146+
{
147+
if ($options = $quoteItem->getQtyOptions()) {
148+
foreach ($options as $option) {
149+
$option->setStockStateResult($resultMock);
150+
}
151+
152+
return;
153+
}
154+
155+
$this->fail('Test failed since Quote Item does not have Qty options.');
156+
}
157+
137158
/**
138159
* Gets \Magento\Quote\Model\Quote\Item from \Magento\Quote\Model\Quote by product id
139160
*

0 commit comments

Comments
 (0)