Skip to content

Commit ad601a8

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-96377' into 2.3-develop-pr13
2 parents 57d3194 + 5bcdb45 commit ad601a8

File tree

5 files changed

+341
-2
lines changed

5 files changed

+341
-2
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Category.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,20 @@ public function getProductsPosition($category)
486486
$this->getCategoryProductTable(),
487487
['product_id', 'position']
488488
)->where(
489-
'category_id = :category_id'
489+
"{$this->getTable('catalog_category_product')}.category_id = ?",
490+
$category->getId()
490491
);
492+
$websiteId = $category->getStore()->getWebsiteId();
493+
if ($websiteId) {
494+
$select->join(
495+
['product_website' => $this->getTable('catalog_product_website')],
496+
"product_website.product_id = {$this->getTable('catalog_category_product')}.product_id",
497+
[]
498+
)->where(
499+
'product_website.website_id = ?',
500+
$websiteId
501+
);
502+
}
491503
$bind = ['category_id' => (int)$category->getId()];
492504

493505
return $this->getConnection()->fetchPairs($select, $bind);

app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,20 @@ public function getProductsPosition($category)
699699
$this->getTable('catalog_category_product'),
700700
['product_id', 'position']
701701
)->where(
702-
'category_id = :category_id'
702+
"{$this->getTable('catalog_category_product')}.category_id = ?",
703+
$category->getId()
703704
);
705+
$websiteId = $category->getStore()->getWebsiteId();
706+
if ($websiteId) {
707+
$select->join(
708+
['product_website' => $this->getTable('catalog_product_website')],
709+
"product_website.product_id = {$this->getTable('catalog_category_product')}.product_id",
710+
[]
711+
)->where(
712+
'product_website.website_id = ?',
713+
$websiteId
714+
);
715+
}
704716
$bind = ['category_id' => (int)$category->getId()];
705717

706718
return $this->getConnection()->fetchPairs($select, $bind);

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/CategoryTest.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,35 @@
66
namespace Magento\Catalog\Controller\Adminhtml;
77

88
use Magento\Framework\App\Request\Http as HttpRequest;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\Store\Model\Store;
11+
use Magento\Catalog\Model\ResourceModel\Product;
912

1013
/**
1114
* @magentoAppArea adminhtml
1215
*/
1316
class CategoryTest extends \Magento\TestFramework\TestCase\AbstractBackendController
1417
{
18+
/**
19+
* @var \Magento\Catalog\Model\ResourceModel\Product
20+
*/
21+
protected $productResource;
22+
23+
/**
24+
* @inheritDoc
25+
*
26+
* @throws \Magento\Framework\Exception\AuthenticationException
27+
*/
28+
protected function setUp()
29+
{
30+
parent::setUp();
31+
32+
/** @var Product $productResource */
33+
$this->productResource = Bootstrap::getObjectManager()->get(
34+
Product::class
35+
);
36+
}
37+
1538
/**
1639
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
1740
* @magentoDbIsolation enabled
@@ -408,4 +431,128 @@ public function moveActionDataProvider()
408431
[400, 401, 'first_url_key', 0, 'second_url_key', true],
409432
];
410433
}
434+
435+
/**
436+
* @magentoDataFixture Magento/Catalog/_files/products_in_different_stores.php
437+
* @magentoDbIsolation disabled
438+
* @dataProvider saveActionWithDifferentWebsitesDataProvider
439+
*
440+
* @param array $postData
441+
*/
442+
public function testSaveCategoryWithProductPosition(array $postData)
443+
{
444+
/** @var $store \Magento\Store\Model\Store */
445+
$store = Bootstrap::getObjectManager()->create(Store::class);
446+
$store->load('fixturestore', 'code');
447+
$storeId = $store->getId();
448+
$oldCategoryProductsCount = $this->getCategoryProductsCount();
449+
$this->getRequest()->setParam('store', $storeId);
450+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
451+
$this->getRequest()->setParam('id', 96377);
452+
$this->getRequest()->setPostValue($postData);
453+
$this->dispatch('backend/catalog/category/save');
454+
$newCategoryProductsCount = $this->getCategoryProductsCount();
455+
$this->assertEquals(
456+
$oldCategoryProductsCount,
457+
$newCategoryProductsCount,
458+
'After changing product position number of records from catalog_category_product has changed'
459+
);
460+
}
461+
462+
/**
463+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
464+
* @return array
465+
*/
466+
public function saveActionWithDifferentWebsitesDataProvider()
467+
{
468+
return [
469+
'default_values' => [
470+
[
471+
'store_id' => '1',
472+
'entity_id' => '96377',
473+
'attribute_set_id' => '4',
474+
'parent_id' => '2',
475+
'created_at' => '2018-11-29 08:28:37',
476+
'updated_at' => '2018-11-29 08:57:43',
477+
'path' => '1/2/96377',
478+
'level' => '2',
479+
'children_count' => '0',
480+
'row_id' => '96377',
481+
'name' => 'Category 1',
482+
'display_mode' => 'PRODUCTS',
483+
'url_key' => 'category-1',
484+
'url_path' => 'category-1',
485+
'automatic_sorting' => '0',
486+
'is_active' => '1',
487+
'is_anchor' => '1',
488+
'include_in_menu' => '1',
489+
'custom_use_parent_settings' => '0',
490+
'custom_apply_to_products' => '0',
491+
'path_ids' => [
492+
0 => '1',
493+
1 => '2',
494+
2 => '96377'
495+
],
496+
'use_config' => [
497+
'available_sort_by' => 'true',
498+
'default_sort_by' => 'true',
499+
'filter_price_range' => 'true'
500+
],
501+
'id' => '',
502+
'parent' => '0',
503+
'use_default' => [
504+
'name' => '1',
505+
'url_key' => '1',
506+
'meta_title' => '1',
507+
'is_active' => '1',
508+
'include_in_menu' => '1',
509+
'custom_use_parent_settings' => '1',
510+
'custom_apply_to_products' => '1',
511+
'description' => '1',
512+
'landing_page' => '1',
513+
'display_mode' => '1',
514+
'custom_design' => '1',
515+
'page_layout' => '1',
516+
'meta_keywords' => '1',
517+
'meta_description' => '1',
518+
'custom_layout_update' => '1',
519+
'image' => '1'
520+
],
521+
'filter_price_range' => false,
522+
'meta_title' => false,
523+
'url_key_create_redirect' => 'category-1',
524+
'description' => false,
525+
'landing_page' => false,
526+
'default_sort_by' => 'position',
527+
'available_sort_by' => false,
528+
'custom_design' => false,
529+
'page_layout' => false,
530+
'meta_keywords' => false,
531+
'meta_description' => false,
532+
'custom_layout_update' => false,
533+
'position_cache_key' => '5c069248346ac',
534+
'is_smart_category' => '0',
535+
'smart_category_rules' => false,
536+
'sort_order' => '0',
537+
'vm_category_products' => '{"1":1,"3":0}'
538+
]
539+
]
540+
];
541+
}
542+
543+
/**
544+
* Get items count from catalog_category_product
545+
*
546+
* @return int
547+
*/
548+
private function getCategoryProductsCount(): int
549+
{
550+
$oldCategoryProducts = $this->productResource->getConnection()->select()->from(
551+
$this->productResource->getTable('catalog_category_product'),
552+
'product_id'
553+
);
554+
return count(
555+
$this->productResource->getConnection()->fetchAll($oldCategoryProducts)
556+
);
557+
}
411558
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\TestFramework\Helper\Bootstrap;
8+
use Magento\Framework\Indexer\IndexerRegistry;
9+
use Magento\Catalog\Model\Product;
10+
use Magento\Catalog\Model\Product\Type;
11+
use Magento\Catalog\Model\Product\Visibility;
12+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
13+
use Magento\Catalog\Api\ProductRepositoryInterface;
14+
15+
require __DIR__ . '/../../Store/_files/core_fixturestore.php';
16+
17+
$objectManager = Bootstrap::getObjectManager();
18+
19+
/** @var Magento\Store\Model\Website $website */
20+
$website = $objectManager->get(Magento\Store\Model\Website::class);
21+
22+
$website->setData(
23+
[
24+
'code' => 'second_website',
25+
'name' => 'Test Website',
26+
]
27+
);
28+
29+
$website->save();
30+
31+
$objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)->reinitStores();
32+
33+
/** @var IndexerRegistry $indexerRegistry */
34+
$indexerRegistry = $objectManager->create(IndexerRegistry::class);
35+
$indexer = $indexerRegistry->get('catalogsearch_fulltext');
36+
37+
$indexer->reindexAll();
38+
39+
$category = $objectManager->create(\Magento\Catalog\Model\Category::class);
40+
$category->isObjectNew(true);
41+
$category->setId(96377)
42+
->setName('Category 1')
43+
->setParentId(2)
44+
->setPath('1/2/96377')
45+
->setLevel(2)
46+
->setAvailableSortBy('name')
47+
->setDefaultSortBy('name')
48+
->setIsActive(true)
49+
->setPosition(1)
50+
->save();
51+
52+
/** @var $product Product */
53+
$product = $objectManager->create(Product::class);
54+
$product->isObjectNew(true);
55+
$product->setTypeId(Type::TYPE_SIMPLE)
56+
->setAttributeSetId(4)
57+
->setWebsiteIds([1])
58+
->setName('Simple Product')
59+
->setSku('simple_1')
60+
->setPrice(10)
61+
->setWeight(1)
62+
->setShortDescription("Short description")
63+
->setTaxClassId(0)
64+
->setDescription('Description with <b>html tag</b>')
65+
->setMetaTitle('meta title')
66+
->setMetaKeyword('meta keyword')
67+
->setMetaDescription('meta description')
68+
->setVisibility(Visibility::VISIBILITY_BOTH)
69+
->setStatus(Status::STATUS_ENABLED)
70+
->setCategoryIds([96377]);
71+
72+
/** @var ProductRepositoryInterface $productRepository */
73+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
74+
$productRepository->save($product);
75+
76+
/** @var $product Product */
77+
$product = $objectManager->create(Product::class);
78+
$product->isObjectNew(true);
79+
$product->setTypeId(Type::TYPE_SIMPLE)
80+
->setAttributeSetId(4)
81+
->setWebsiteIds([$website->getId()])
82+
->setName('Simple Product 2')
83+
->setSku('simple_2')
84+
->setPrice(10)
85+
->setWeight(1)
86+
->setShortDescription("Short description")
87+
->setTaxClassId(0)
88+
->setDescription('Description with <b>html tag</b>')
89+
->setMetaTitle('meta title')
90+
->setMetaKeyword('meta keyword')
91+
->setMetaDescription('meta description')
92+
->setVisibility(Visibility::VISIBILITY_BOTH)
93+
->setStatus(Status::STATUS_ENABLED)
94+
->setCategoryIds([96377]);
95+
96+
/** @var ProductRepositoryInterface $productRepository */
97+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
98+
$productRepository->save($product);
99+
100+
/** @var $product Product */
101+
$product = $objectManager->create(Product::class);
102+
$product->isObjectNew(true);
103+
$product->setTypeId(Type::TYPE_SIMPLE)
104+
->setAttributeSetId(4)
105+
->setWebsiteIds([1, $website->getId()])
106+
->setName('Simple Product 3')
107+
->setSku('simple_3')
108+
->setPrice(10)
109+
->setWeight(1)
110+
->setShortDescription("Short description")
111+
->setTaxClassId(0)
112+
->setDescription('Description with <b>html tag</b>')
113+
->setMetaTitle('meta title')
114+
->setMetaKeyword('meta keyword')
115+
->setMetaDescription('meta description')
116+
->setVisibility(Visibility::VISIBILITY_BOTH)
117+
->setStatus(Status::STATUS_ENABLED)
118+
->setCategoryIds([96377]);
119+
120+
/** @var ProductRepositoryInterface $productRepository */
121+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
122+
$productRepository->save($product);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
require __DIR__ . '/../../Store/_files/core_fixturestore_rollback.php';
8+
9+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
10+
/** @var \Magento\Framework\Registry $registry */
11+
$registry = $objectManager->get(\Magento\Framework\Registry::class);
12+
13+
$registry->unregister('isSecureArea');
14+
$registry->register('isSecureArea', true);
15+
16+
//Remove category
17+
/** @var $category \Magento\Catalog\Model\Category */
18+
$category = $objectManager->create(\Magento\Catalog\Model\Category::class);
19+
$category->load(96377);
20+
if ($category->getId()) {
21+
$category->delete();
22+
}
23+
24+
$productSkuList = ['simple_1', 'simple_2', 'simple_3'];
25+
foreach ($productSkuList as $sku) {
26+
try {
27+
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
28+
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
29+
$product = $productRepository->get($sku, true);
30+
if ($product->getId()) {
31+
$productRepository->delete($product);
32+
}
33+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
34+
//Product already removed
35+
}
36+
}
37+
38+
/** @var Magento\Store\Model\Website $website */
39+
$website = $objectManager->get(Magento\Store\Model\Website::class);
40+
$website->load('second_website', 'code');
41+
if ($website->getId()) {
42+
$website->delete();
43+
}
44+
45+
$registry->unregister('isSecureArea');
46+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)