Skip to content

Commit 98e4a6e

Browse files
🔃 [EngCom] Public Pull Requests - 2.3-develop
Accepted Public Pull Requests: - #14910: [Forwardport] [FIX]: Recent orders are not filtered per store at the customer account page (by @rostyslav-hymon) - #14610: CSV files: added possibility to select PHP input file mode (by @enrico69) - #14381: [Forwardport] Category\Collection::joinUrlRewrite should use the store set on the collection (by @rostyslav-hymon) Fixed GitHub Issues: - #13704: Category\Collection::joinUrlRewrite should use the store set on the collection (reported by @alepane21) has been fixed in #14381 by @rostyslav-hymon in 2.3-develop branch Related commits: 1. 29431e9 2. 47c8d54 3. 2a01489 4. 2d12dac 5. 8a097fc 6. af3804a 7. 8220e3f
2 parents 4f9b705 + 0a4d090 commit 98e4a6e

File tree

8 files changed

+269
-43
lines changed

8 files changed

+269
-43
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public function joinUrlRewrite()
371371
['request_path'],
372372
sprintf(
373373
'{{table}}.is_autogenerated = 1 AND {{table}}.store_id = %d AND {{table}}.entity_type = \'%s\'',
374-
$this->_storeManager->getStore()->getId(),
374+
$this->getStoreId(),
375375
CategoryUrlRewriteGenerator::ENTITY_TYPE
376376
),
377377
'left'

app/code/Magento/Sales/Block/Order/Recent.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
*/
66
namespace Magento\Sales\Block\Order;
77

8+
use Magento\Framework\View\Element\Template\Context;
9+
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
10+
use Magento\Customer\Model\Session;
11+
use Magento\Sales\Model\Order\Config;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
use Magento\Framework\App\ObjectManager;
14+
815
/**
916
* Sales order history block
1017
*
@@ -13,6 +20,11 @@
1320
*/
1421
class Recent extends \Magento\Framework\View\Element\Template
1522
{
23+
/**
24+
* Limit of orders
25+
*/
26+
const ORDER_LIMIT = 5;
27+
1628
/**
1729
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
1830
*/
@@ -28,25 +40,34 @@ class Recent extends \Magento\Framework\View\Element\Template
2840
*/
2941
protected $_orderConfig;
3042

43+
/**
44+
* @var \Magento\Store\Model\StoreManagerInterface
45+
*/
46+
private $storeManager;
47+
3148
/**
3249
* @param \Magento\Framework\View\Element\Template\Context $context
3350
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
3451
* @param \Magento\Customer\Model\Session $customerSession
3552
* @param \Magento\Sales\Model\Order\Config $orderConfig
3653
* @param array $data
54+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
3755
*/
3856
public function __construct(
39-
\Magento\Framework\View\Element\Template\Context $context,
40-
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
41-
\Magento\Customer\Model\Session $customerSession,
42-
\Magento\Sales\Model\Order\Config $orderConfig,
43-
array $data = []
57+
Context $context,
58+
CollectionFactory $orderCollectionFactory,
59+
Session $customerSession,
60+
Config $orderConfig,
61+
array $data = [],
62+
StoreManagerInterface $storeManager = null
4463
) {
4564
$this->_orderCollectionFactory = $orderCollectionFactory;
4665
$this->_customerSession = $customerSession;
4766
$this->_orderConfig = $orderConfig;
48-
parent::__construct($context, $data);
4967
$this->_isScopePrivate = true;
68+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
69+
->get(StoreManagerInterface::class);
70+
parent::__construct($context, $data);
5071
}
5172

5273
/**
@@ -55,19 +76,30 @@ public function __construct(
5576
protected function _construct()
5677
{
5778
parent::_construct();
79+
$this->getRecentOrders();
80+
}
81+
82+
/**
83+
* Get recently placed orders. By default they will be limited by 5.
84+
*/
85+
private function getRecentOrders()
86+
{
5887
$orders = $this->_orderCollectionFactory->create()->addAttributeToSelect(
5988
'*'
6089
)->addAttributeToFilter(
6190
'customer_id',
6291
$this->_customerSession->getCustomerId()
92+
)->addAttributeToFilter(
93+
'store_id',
94+
$this->storeManager->getStore()->getId()
6395
)->addAttributeToFilter(
6496
'status',
6597
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
6698
)->addAttributeToSort(
6799
'created_at',
68100
'desc'
69101
)->setPageSize(
70-
'5'
102+
self::ORDER_LIMIT
71103
)->load();
72104
$this->setOrders($orders);
73105
}

app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
*/
66
namespace Magento\Sales\Test\Unit\Block\Order;
77

8+
use Magento\Framework\View\Element\Template\Context;
9+
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
10+
use Magento\Customer\Model\Session;
11+
use Magento\Sales\Model\Order\Config;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
use Magento\Framework\View\Layout;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Sales\Model\ResourceModel\Order\Collection;
16+
817
class RecentTest extends \PHPUnit\Framework\TestCase
918
{
1019
/**
@@ -32,26 +41,33 @@ class RecentTest extends \PHPUnit\Framework\TestCase
3241
*/
3342
protected $orderConfig;
3443

44+
/**
45+
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
46+
*/
47+
protected $storeManagerMock;
48+
3549
protected function setUp()
3650
{
37-
$this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
51+
$this->context = $this->createMock(Context::class);
3852
$this->orderCollectionFactory = $this->createPartialMock(
39-
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class,
53+
CollectionFactory::class,
4054
['create']
4155
);
42-
$this->customerSession = $this->createPartialMock(\Magento\Customer\Model\Session::class, ['getCustomerId']);
56+
$this->customerSession = $this->createPartialMock(Session::class, ['getCustomerId']);
4357
$this->orderConfig = $this->createPartialMock(
44-
\Magento\Sales\Model\Order\Config::class,
58+
Config::class,
4559
['getVisibleOnFrontStatuses']
4660
);
61+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
62+
->getMockForAbstractClass();
4763
}
4864

4965
public function testConstructMethod()
5066
{
51-
$data = [];
52-
$attribute = ['customer_id', 'status'];
67+
$attribute = ['customer_id', 'store_id', 'status'];
5368
$customerId = 25;
54-
$layout = $this->createPartialMock(\Magento\Framework\View\Layout::class, ['getBlock']);
69+
$storeId = 4;
70+
$layout = $this->createPartialMock(Layout::class, ['getBlock']);
5571
$this->context->expects($this->once())
5672
->method('getLayout')
5773
->will($this->returnValue($layout));
@@ -64,14 +80,20 @@ public function testConstructMethod()
6480
->method('getVisibleOnFrontStatuses')
6581
->will($this->returnValue($statuses));
6682

67-
$orderCollection = $this->createPartialMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class, [
68-
'addAttributeToSelect',
69-
'addFieldToFilter',
70-
'addAttributeToFilter',
71-
'addAttributeToSort',
72-
'setPageSize',
73-
'load'
74-
]);
83+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
84+
->getMockForAbstractClass();
85+
$storeMock = $this->getMockBuilder(StoreInterface::class)->getMockForAbstractClass();
86+
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($storeMock);
87+
$storeMock->expects($this->any())->method('getId')->willReturn($storeId);
88+
89+
$orderCollection = $this->createPartialMock(Collection::class, [
90+
'addAttributeToSelect',
91+
'addFieldToFilter',
92+
'addAttributeToFilter',
93+
'addAttributeToSort',
94+
'setPageSize',
95+
'load'
96+
]);
7597
$this->orderCollectionFactory->expects($this->once())
7698
->method('create')
7799
->will($this->returnValue($orderCollection));
@@ -85,25 +107,30 @@ public function testConstructMethod()
85107
->willReturnSelf();
86108
$orderCollection->expects($this->at(2))
87109
->method('addAttributeToFilter')
88-
->with($attribute[1], $this->equalTo(['in' => $statuses]))
89-
->will($this->returnSelf());
110+
->with($attribute[1], $this->equalTo($storeId))
111+
->willReturnSelf();
90112
$orderCollection->expects($this->at(3))
113+
->method('addAttributeToFilter')
114+
->with($attribute[2], $this->equalTo(['in' => $statuses]))
115+
->will($this->returnSelf());
116+
$orderCollection->expects($this->at(4))
91117
->method('addAttributeToSort')
92118
->with('created_at', 'desc')
93119
->will($this->returnSelf());
94-
$orderCollection->expects($this->at(4))
120+
$orderCollection->expects($this->at(5))
95121
->method('setPageSize')
96122
->with('5')
97123
->will($this->returnSelf());
98-
$orderCollection->expects($this->at(5))
124+
$orderCollection->expects($this->at(6))
99125
->method('load')
100126
->will($this->returnSelf());
101127
$this->block = new \Magento\Sales\Block\Order\Recent(
102128
$this->context,
103129
$this->orderCollectionFactory,
104130
$this->customerSession,
105131
$this->orderConfig,
106-
$data
132+
[],
133+
$this->storeManagerMock
107134
);
108135
$this->assertEquals($orderCollection, $this->block->getOrders());
109136
}

app/code/Magento/Sales/view/frontend/templates/order/recent.phtml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88

99
?>
1010
<div class="block block-dashboard-orders">
11-
<?php $_orders = $block->getOrders(); ?>
11+
<?php
12+
$_orders = $block->getOrders();
13+
$count = count($_orders);
14+
?>
1215
<div class="block-title order">
1316
<strong><?= /* @escapeNotVerified */ __('Recent Orders') ?></strong>
14-
<?php if (sizeof($_orders->getItems()) > 0): ?>
17+
<?php if ($count > 0): ?>
1518
<a class="action view" href="<?= /* @escapeNotVerified */ $block->getUrl('sales/order/history') ?>">
1619
<span><?= /* @escapeNotVerified */ __('View All') ?></span>
1720
</a>
1821
<?php endif; ?>
1922
</div>
2023
<div class="block-content">
2124
<?= $block->getChildHtml() ?>
22-
<?php if (sizeof($_orders->getItems()) > 0): ?>
25+
<?php if ($count > 0): ?>
2326
<div class="table-wrapper orders-recent">
2427
<table class="data table table-order-items recent" id="my-orders-table">
2528
<caption class="table-caption"><?= /* @escapeNotVerified */ __('Recent Orders') ?></caption>

dev/tests/integration/testsuite/Magento/Bundle/Model/Category/ProductIndexerTest.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,28 @@ public function testCategoryCreate()
215215
}
216216

217217
/**
218+
* Finds 4 categories
219+
*
218220
* @return Category[]
219221
*/
220222
private function getCategories()
221223
{
222-
/** @var Category $category */
223-
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
224-
\Magento\Catalog\Model\Category::class
224+
$collectionFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
225+
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory::class
225226
);
226227

227-
$result = $category->getCollection()->addAttributeToSelect('name')->getItems();
228-
$result = array_slice($result, 2);
228+
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
229+
$collection = $collectionFactory->create();
230+
231+
$collection
232+
->addAttributeToSelect('name')
233+
->addAttributeToFilter('name', ['in' => [
234+
'Category 1',
235+
'Category 2',
236+
'Category 3',
237+
'Category 4',
238+
]]);
229239

230-
return array_slice($result, 0, 4);
240+
return array_values($collection->getItems());
231241
}
232242
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\ResourceModel\Category;
9+
10+
class CollectionTest extends \PHPUnit\Framework\TestCase
11+
{
12+
/**
13+
* @var \Magento\Catalog\Model\ResourceModel\Category\Collection
14+
*/
15+
private $collection;
16+
17+
/**
18+
* Sets up the fixture, for example, opens a network connection.
19+
* This method is called before a test is executed.
20+
*/
21+
protected function setUp()
22+
{
23+
$this->collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
24+
\Magento\Catalog\Model\ResourceModel\Category\Collection::class
25+
);
26+
}
27+
28+
protected function setDown()
29+
{
30+
/* Refresh stores memory cache after store deletion */
31+
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
32+
\Magento\Store\Model\StoreManagerInterface::class
33+
)->reinitStores();
34+
}
35+
36+
/**
37+
* @magentoAppIsolation enabled
38+
* @magentoDbIsolation enabled
39+
* @magentoDataFixture Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php
40+
*/
41+
public function testJoinUrlRewriteOnDefault()
42+
{
43+
$categories = $this->collection->joinUrlRewrite()->addPathFilter('1/2/3');
44+
$this->assertCount(1, $categories);
45+
/** @var $category \Magento\Catalog\Model\Category */
46+
$category = $categories->getFirstItem();
47+
$this->assertStringEndsWith('category.html', $category->getUrl());
48+
}
49+
50+
/**
51+
* @magentoAppIsolation enabled
52+
* @magentoDbIsolation enabled
53+
* @magentoDataFixture Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php
54+
*/
55+
public function testJoinUrlRewriteNotOnDefaultStore()
56+
{
57+
$store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
58+
->create(\Magento\Store\Model\Store::class);
59+
$storeId = $store->load('second_category_store', 'code')->getId();
60+
$categories = $this->collection->setStoreId($storeId)->joinUrlRewrite()->addPathFilter('1/2/3');
61+
$this->assertCount(1, $categories);
62+
/** @var $category \Magento\Catalog\Model\Category */
63+
$category = $categories->getFirstItem();
64+
$this->assertStringEndsWith('category-3-on-2.html', $category->getUrl());
65+
}
66+
}

0 commit comments

Comments
 (0)