Skip to content

Commit 1358482

Browse files
author
Viktor Tymchynskyi
committed
Merge remote-tracking branch 'magento2/develop' into MPI-BUGFIXES
2 parents 3e30f3b + 7200766 commit 1358482

File tree

50 files changed

+989
-533
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+989
-533
lines changed

app/code/Magento/Backend/App/BackendAppList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function getCurrentApp()
4444
if ($appName && isset($this->backendApps[$appName])) {
4545
return $this->backendApps[$appName];
4646
}
47+
return null;
4748
}
4849

4950
/**

app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,27 @@ protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryT
5959
"bo.parent_id = product.$linkField",
6060
[]
6161
);
62-
$this->_addWebsiteJoinToSelect($select, false);
6362
$status = new \Zend_Db_Expr(
6463
'MAX(' . $connection->getCheckSql('e.required_options = 0', 'i.stock_status', '0') . ')'
6564
);
66-
$select->columns(
67-
'website_id',
68-
'cw'
69-
)->join(
65+
$select->join(
7066
['cis' => $this->getTable('cataloginventory_stock')],
7167
'',
72-
['stock_id']
68+
['website_id', 'stock_id']
7369
)->joinLeft(
7470
['bs' => $this->getTable('catalog_product_bundle_selection')],
7571
'bs.option_id = bo.option_id',
7672
[]
7773
)->joinLeft(
7874
['i' => $idxTable],
79-
'i.product_id = bs.product_id AND i.website_id = cw.website_id AND i.stock_id = cis.stock_id',
75+
'i.product_id = bs.product_id AND i.website_id = cis.website_id AND i.stock_id = cis.stock_id',
8076
[]
8177
)->joinLeft(
8278
['e' => $this->getTable('catalog_product_entity')],
8379
'e.entity_id = bs.product_id',
8480
[]
85-
)->where(
86-
'cw.website_id != 0'
8781
)->group(
88-
['product.entity_id', 'cw.website_id', 'cis.stock_id', 'bo.option_id']
82+
['product.entity_id', 'cis.website_id', 'cis.stock_id', 'bo.option_id']
8983
)->columns(
9084
['option_id' => 'bo.option_id', 'status' => $status]
9185
);
@@ -118,45 +112,19 @@ protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryT
118112
protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false)
119113
{
120114
$this->_prepareBundleOptionStockData($entityIds, $usePrimaryTable);
121-
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
122115
$connection = $this->getConnection();
123-
$select = $connection->select()->from(
124-
['e' => $this->getTable('catalog_product_entity')],
125-
['entity_id']
126-
);
127-
$this->_addWebsiteJoinToSelect($select, true);
128-
$this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id');
129-
$select->columns(
130-
'cw.website_id'
131-
)->join(
132-
['cis' => $this->getTable('cataloginventory_stock')],
133-
'',
134-
['stock_id']
135-
)->joinLeft(
136-
['cisi' => $this->getTable('cataloginventory_stock_item')],
137-
'cisi.stock_id = cis.stock_id AND cisi.product_id = e.entity_id',
138-
[]
116+
$select = parent::_getStockStatusSelect($entityIds, $usePrimaryTable);
117+
$select->reset(
118+
\Magento\Framework\DB\Select::COLUMNS
119+
)->columns(
120+
['e.entity_id', 'cis.website_id', 'cis.stock_id']
139121
)->joinLeft(
140122
['o' => $this->_getBundleOptionTable()],
141-
'o.entity_id = e.entity_id AND o.website_id = cw.website_id AND o.stock_id = cis.stock_id',
123+
'o.entity_id = e.entity_id AND o.website_id = cis.website_id AND o.stock_id = cis.stock_id',
142124
[]
143125
)->columns(
144126
['qty' => new \Zend_Db_Expr('0')]
145-
)->where(
146-
'cw.website_id != 0'
147-
)->where(
148-
'e.type_id = ?',
149-
$this->getTypeId()
150-
)->group(
151-
['e.entity_id', 'cw.website_id', 'cis.stock_id']
152-
);
153-
154-
// add limitation of status
155-
$condition = $connection->quoteInto(
156-
'=?',
157-
\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
158127
);
159-
$this->_addAttributeToSelect($select, 'status', "e.$linkField", 'cs.store_id', $condition);
160128

161129
$statusExpr = $this->getStatusExpression($connection);
162130
$select->columns(

app/code/Magento/Catalog/Observer/AddCatalogToTopmenuItemsObserver.php renamed to app/code/Magento/Catalog/Plugin/Block/Topmenu.php

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
* Copyright © 2016 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
namespace Magento\Catalog\Observer;
6+
namespace Magento\Catalog\Plugin\Block;
77

88
use Magento\Catalog\Model\Category;
99
use Magento\Framework\Data\Collection;
1010
use Magento\Framework\Data\Tree\Node;
11-
use Magento\Framework\Event\ObserverInterface;
1211

1312
/**
14-
* Observer that add Categories Tree to Topmenu
13+
* Plugin for top menu block
1514
*/
16-
class AddCatalogToTopmenuItemsObserver implements ObserverInterface
15+
class Topmenu
1716
{
1817
/**
1918
* Catalog category
@@ -38,11 +37,11 @@ class AddCatalogToTopmenuItemsObserver implements ObserverInterface
3837
private $layerResolver;
3938

4039
/**
40+
* Initialize dependencies.
41+
*
4142
* @param \Magento\Catalog\Helper\Category $catalogCategory
42-
* @param \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState
4343
* @param \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
4444
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
45-
* @param \Magento\Catalog\Helper\Category $catalogCategory
4645
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
4746
*/
4847
public function __construct(
@@ -58,26 +57,27 @@ public function __construct(
5857
}
5958

6059
/**
61-
* Checking whether the using static urls in WYSIWYG allowed event
60+
* Build category tree for menu block.
6261
*
63-
* @param \Magento\Framework\Event\Observer $observer
62+
* @param \Magento\Theme\Block\Html\Topmenu $subject
63+
* @param string $outermostClass
64+
* @param string $childrenWrapClass
65+
* @param int $limit
6466
* @return void
67+
* @SuppressWarnings("PMD.UnusedFormalParameter")
6568
*/
66-
public function execute(\Magento\Framework\Event\Observer $observer)
67-
{
68-
$block = $observer->getEvent()->getBlock();
69-
$menuRootNode = $observer->getEvent()->getMenu();
70-
$block->addIdentity(Category::CACHE_TAG);
71-
69+
public function beforeGetHtml(
70+
\Magento\Theme\Block\Html\Topmenu $subject,
71+
$outermostClass = '',
72+
$childrenWrapClass = '',
73+
$limit = 0
74+
) {
7275
$rootId = $this->storeManager->getStore()->getRootCategoryId();
7376
$storeId = $this->storeManager->getStore()->getId();
74-
7577
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
7678
$collection = $this->getCategoryTree($storeId, $rootId);
77-
7879
$currentCategory = $this->getCurrentCategory();
79-
80-
$mapping = [$rootId => $menuRootNode]; // use nodes stack to avoid recursion
80+
$mapping = [$rootId => $subject->getMenu()]; // use nodes stack to avoid recursion
8181
foreach ($collection as $category) {
8282
if (!isset($mapping[$category->getParentId()])) {
8383
continue;
@@ -94,8 +94,28 @@ public function execute(\Magento\Framework\Event\Observer $observer)
9494
$parentCategoryNode->addChild($categoryNode);
9595

9696
$mapping[$category->getId()] = $categoryNode; //add node in stack
97+
}
98+
}
9799

98-
$block->addIdentity(Category::CACHE_TAG . '_' . $category->getId());
100+
/**
101+
* Add list of associated identities to the top menu block for caching purposes.
102+
*
103+
* @param \Magento\Theme\Block\Html\Topmenu $subject
104+
* @return void
105+
*/
106+
public function beforeGetIdentities(\Magento\Theme\Block\Html\Topmenu $subject)
107+
{
108+
$subject->addIdentity(Category::CACHE_TAG);
109+
$rootId = $this->storeManager->getStore()->getRootCategoryId();
110+
$storeId = $this->storeManager->getStore()->getId();
111+
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
112+
$collection = $this->getCategoryTree($storeId, $rootId);
113+
$mapping = [$rootId => $subject->getMenu()]; // use nodes stack to avoid recursion
114+
foreach ($collection as $category) {
115+
if (!isset($mapping[$category->getParentId()])) {
116+
continue;
117+
}
118+
$subject->addIdentity(Category::CACHE_TAG . '_' . $category->getId());
99119
}
100120
}
101121

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66

77
// @codingStandardsIgnoreFile
88

9-
namespace Magento\Catalog\Test\Unit\Observer;
9+
namespace Magento\Catalog\Test\Unit\Plugin\Block;
1010

1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1212

13-
class AddCatalogToTopmenuItemsObserverTest extends \PHPUnit_Framework_TestCase
13+
class TopmenuTest extends \PHPUnit_Framework_TestCase
1414
{
1515
/**
16-
* @var \Magento\Catalog\Observer\AddCatalogToTopmenuItemsObserver
16+
* @var \Magento\Catalog\Plugin\Block\Topmenu
1717
*/
18-
protected $_observer;
18+
protected $block;
1919

2020
/**
2121
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Helper\Category
@@ -100,8 +100,8 @@ protected function setUp()
100100
$collection->expects($this->once())->method('getIterator')
101101
->willReturn(new \ArrayIterator([]));
102102

103-
$this->_observer = (new ObjectManager($this))->getObject(
104-
'Magento\Catalog\Observer\AddCatalogToTopmenuItemsObserver',
103+
$this->block = (new ObjectManager($this))->getObject(
104+
\Magento\Catalog\Plugin\Block\Topmenu::class,
105105
[
106106
'catalogCategory' => $this->_catalogCategory,
107107
'menuCategoryData' => $this->menuCategoryData,
@@ -142,23 +142,11 @@ protected function _preparationData()
142142
);
143143

144144
$blockMock = $this->_getCleanMock('\Magento\Theme\Block\Html\Topmenu');
145-
146-
$eventMock = $this->getMock('\Magento\Framework\Event', ['getBlock'], [], '', false);
147-
$eventMock->expects($this->once())
148-
->method('getBlock')
149-
->will($this->returnValue($blockMock));
150-
151-
$observerMock = $this->getMock('\Magento\Framework\Event\Observer', ['getEvent', 'getMenu'], [], '', false);
152-
$observerMock->expects($this->any())
153-
->method('getEvent')
154-
->will($this->returnValue($eventMock));
155-
156-
return $observerMock;
145+
return $blockMock;
157146
}
158147

159148
public function testAddCatalogToTopMenuItems()
160149
{
161-
$observer = $this->_preparationData();
162-
$this->_observer->execute($observer);
150+
$this->block->beforeGetHtml($this->_preparationData());
163151
}
164152
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
<type name="Magento\Customer\Model\ResourceModel\Visitor">
4747
<plugin name="catalogLog" type="Magento\Catalog\Model\Plugin\Log" />
4848
</type>
49+
<type name="Magento\Theme\Block\Html\Topmenu">
50+
<plugin name="catalogTopmenu" type="Magento\Catalog\Plugin\Block\Topmenu" />
51+
</type>
4952
<type name="Magento\Framework\Mview\View\StateInterface">
5053
<plugin name="setStatusForMview" type="Magento\Catalog\Model\Indexer\Category\Product\Plugin\MviewState" />
5154
</type>

app/code/Magento/Catalog/etc/frontend/events.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,4 @@
1212
<event name="customer_logout">
1313
<observer name="catalog" instance="Magento\Catalog\Observer\Compare\BindCustomerLogoutObserver" shared="false" />
1414
</event>
15-
<event name="page_block_html_topmenu_gethtml_before">
16-
<observer name="catalog_add_topmenu_items" instance="Magento\Catalog\Observer\AddCatalogToTopmenuItemsObserver" />
17-
</event>
1815
</config>

app/code/Magento/CatalogInventory/Helper/Stock.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
use Magento\CatalogInventory\Model\ResourceModel\Stock\Status;
1313
use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;
1414
use Magento\Catalog\Model\Product;
15+
use Magento\CatalogInventory\Api\StockConfigurationInterface;
1516

1617
/**
1718
* Class Stock
19+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1820
*/
1921
class Stock
2022
{
@@ -47,6 +49,11 @@ class Stock
4749
*/
4850
private $stockRegistryProvider;
4951

52+
/**
53+
* @var StockConfigurationInterface
54+
*/
55+
private $stockConfiguration;
56+
5057
/**
5158
* @param StoreManagerInterface $storeManager
5259
* @param ScopeConfigInterface $scopeConfig
@@ -75,7 +82,7 @@ public function __construct(
7582
public function assignStatusToProduct(Product $product, $status = null)
7683
{
7784
if ($status === null) {
78-
$websiteId = $product->getStore()->getWebsiteId();
85+
$websiteId = $this->getStockConfiguration()->getDefaultScopeId();
7986
$stockStatus = $this->stockRegistryProvider->getStockStatus($product->getId(), $websiteId);
8087
$status = $stockStatus->getStockStatus();
8188
}
@@ -90,7 +97,7 @@ public function assignStatusToProduct(Product $product, $status = null)
9097
*/
9198
public function addStockStatusToProducts(AbstractCollection $productCollection)
9299
{
93-
$websiteId = $this->storeManager->getStore($productCollection->getStoreId())->getWebsiteId();
100+
$websiteId = $this->getStockConfiguration()->getDefaultScopeId();
94101
foreach ($productCollection as $product) {
95102
$productId = $product->getId();
96103
$stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $websiteId);
@@ -161,4 +168,18 @@ protected function getStockStatusResource()
161168
}
162169
return $this->stockStatusResource;
163170
}
171+
172+
/**
173+
* @return StockConfigurationInterface
174+
*
175+
* @deprecated
176+
*/
177+
private function getStockConfiguration()
178+
{
179+
if ($this->stockConfiguration === null) {
180+
$this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance()
181+
->get('Magento\CatalogInventory\Api\StockConfigurationInterface');
182+
}
183+
return $this->stockConfiguration;
184+
}
164185
}

app/code/Magento/CatalogInventory/Model/Config/Backend/Backorders.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public function afterSave()
2525
|| $this->getValue() == \Magento\CatalogInventory\Model\Stock::BACKORDERS_NO
2626
)
2727
) {
28-
$this->stockIndex->rebuild();
2928
$this->_stockIndexerProcessor->markIndexerAsInvalid();
3029
}
3130
return parent::afterSave();

app/code/Magento/CatalogInventory/Model/Config/Backend/Managestock.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class Managestock extends AbstractValue
2020
public function afterSave()
2121
{
2222
if ($this->isValueChanged()) {
23-
$this->stockIndex->rebuild();
2423
$this->_stockIndexerProcessor->markIndexerAsInvalid();
2524
}
2625
return parent::afterSave();

app/code/Magento/CatalogInventory/Model/Configuration.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ public function __construct(
148148
*/
149149
public function getDefaultScopeId()
150150
{
151-
return (int) $this->storeManager->getWebsite()->getId();
151+
// TODO: should be fixed in MAGETWO-46043
152+
// "0" is id of admin website, which is used in backend during save entity
153+
return 0;
152154
}
153155

154156
/**

app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class AroundProductRepositorySave
2424

2525
/**
2626
* @var StoreManagerInterface
27+
* @deprecated
2728
*/
2829
protected $storeManager;
2930

@@ -80,7 +81,7 @@ public function aroundSave(
8081

8182
// set fields that the customer should not care about
8283
$stockItem->setProductId($product->getId());
83-
$stockItem->setWebsiteId($this->storeManager->getStore($product->getStoreId())->getWebsiteId());
84+
$stockItem->setWebsiteId($this->stockConfiguration->getDefaultScopeId());
8485

8586
$this->stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
8687

0 commit comments

Comments
 (0)