Skip to content

Commit

Permalink
🔃 [EngCom] Public Pull Requests - 2.3-develop
Browse files Browse the repository at this point in the history
Accepted Public Pull Requests:
 - magento#14910: [Forwardport] [FIX]: Recent orders are not filtered per store at the customer account page (by @rostyslav-hymon)
 - magento#14610: CSV files: added possibility to select PHP input file mode (by @enrico69)
 - magento#14381: [Forwardport] Category\Collection::joinUrlRewrite should use the store set on the collection (by @rostyslav-hymon)


Fixed GitHub Issues:
 - magento#13704: Category\Collection::joinUrlRewrite should use the store set on the collection (reported by @alepane21) has been fixed in magento#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
  • Loading branch information
magento-engcom-team authored May 6, 2018
2 parents 4f9b705 + 0a4d090 commit 98e4a6e
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public function joinUrlRewrite()
['request_path'],
sprintf(
'{{table}}.is_autogenerated = 1 AND {{table}}.store_id = %d AND {{table}}.entity_type = \'%s\'',
$this->_storeManager->getStore()->getId(),
$this->getStoreId(),
CategoryUrlRewriteGenerator::ENTITY_TYPE
),
'left'
Expand Down
46 changes: 39 additions & 7 deletions app/code/Magento/Sales/Block/Order/Recent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
*/
namespace Magento\Sales\Block\Order;

use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Customer\Model\Session;
use Magento\Sales\Model\Order\Config;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\ObjectManager;

/**
* Sales order history block
*
Expand All @@ -13,6 +20,11 @@
*/
class Recent extends \Magento\Framework\View\Element\Template
{
/**
* Limit of orders
*/
const ORDER_LIMIT = 5;

/**
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
*/
Expand All @@ -28,25 +40,34 @@ class Recent extends \Magento\Framework\View\Element\Template
*/
protected $_orderConfig;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Sales\Model\Order\Config $orderConfig
* @param array $data
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
\Magento\Customer\Model\Session $customerSession,
\Magento\Sales\Model\Order\Config $orderConfig,
array $data = []
Context $context,
CollectionFactory $orderCollectionFactory,
Session $customerSession,
Config $orderConfig,
array $data = [],
StoreManagerInterface $storeManager = null
) {
$this->_orderCollectionFactory = $orderCollectionFactory;
$this->_customerSession = $customerSession;
$this->_orderConfig = $orderConfig;
parent::__construct($context, $data);
$this->_isScopePrivate = true;
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
->get(StoreManagerInterface::class);
parent::__construct($context, $data);
}

/**
Expand All @@ -55,19 +76,30 @@ public function __construct(
protected function _construct()
{
parent::_construct();
$this->getRecentOrders();
}

/**
* Get recently placed orders. By default they will be limited by 5.
*/
private function getRecentOrders()
{
$orders = $this->_orderCollectionFactory->create()->addAttributeToSelect(
'*'
)->addAttributeToFilter(
'customer_id',
$this->_customerSession->getCustomerId()
)->addAttributeToFilter(
'store_id',
$this->storeManager->getStore()->getId()
)->addAttributeToFilter(
'status',
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
)->addAttributeToSort(
'created_at',
'desc'
)->setPageSize(
'5'
self::ORDER_LIMIT
)->load();
$this->setOrders($orders);
}
Expand Down
67 changes: 47 additions & 20 deletions app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
*/
namespace Magento\Sales\Test\Unit\Block\Order;

use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Customer\Model\Session;
use Magento\Sales\Model\Order\Config;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\View\Layout;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Sales\Model\ResourceModel\Order\Collection;

class RecentTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -32,26 +41,33 @@ class RecentTest extends \PHPUnit\Framework\TestCase
*/
protected $orderConfig;

/**
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $storeManagerMock;

protected function setUp()
{
$this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
$this->context = $this->createMock(Context::class);
$this->orderCollectionFactory = $this->createPartialMock(
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class,
CollectionFactory::class,
['create']
);
$this->customerSession = $this->createPartialMock(\Magento\Customer\Model\Session::class, ['getCustomerId']);
$this->customerSession = $this->createPartialMock(Session::class, ['getCustomerId']);
$this->orderConfig = $this->createPartialMock(
\Magento\Sales\Model\Order\Config::class,
Config::class,
['getVisibleOnFrontStatuses']
);
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
->getMockForAbstractClass();
}

public function testConstructMethod()
{
$data = [];
$attribute = ['customer_id', 'status'];
$attribute = ['customer_id', 'store_id', 'status'];
$customerId = 25;
$layout = $this->createPartialMock(\Magento\Framework\View\Layout::class, ['getBlock']);
$storeId = 4;
$layout = $this->createPartialMock(Layout::class, ['getBlock']);
$this->context->expects($this->once())
->method('getLayout')
->will($this->returnValue($layout));
Expand All @@ -64,14 +80,20 @@ public function testConstructMethod()
->method('getVisibleOnFrontStatuses')
->will($this->returnValue($statuses));

$orderCollection = $this->createPartialMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class, [
'addAttributeToSelect',
'addFieldToFilter',
'addAttributeToFilter',
'addAttributeToSort',
'setPageSize',
'load'
]);
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
->getMockForAbstractClass();
$storeMock = $this->getMockBuilder(StoreInterface::class)->getMockForAbstractClass();
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($storeMock);
$storeMock->expects($this->any())->method('getId')->willReturn($storeId);

$orderCollection = $this->createPartialMock(Collection::class, [
'addAttributeToSelect',
'addFieldToFilter',
'addAttributeToFilter',
'addAttributeToSort',
'setPageSize',
'load'
]);
$this->orderCollectionFactory->expects($this->once())
->method('create')
->will($this->returnValue($orderCollection));
Expand All @@ -85,25 +107,30 @@ public function testConstructMethod()
->willReturnSelf();
$orderCollection->expects($this->at(2))
->method('addAttributeToFilter')
->with($attribute[1], $this->equalTo(['in' => $statuses]))
->will($this->returnSelf());
->with($attribute[1], $this->equalTo($storeId))
->willReturnSelf();
$orderCollection->expects($this->at(3))
->method('addAttributeToFilter')
->with($attribute[2], $this->equalTo(['in' => $statuses]))
->will($this->returnSelf());
$orderCollection->expects($this->at(4))
->method('addAttributeToSort')
->with('created_at', 'desc')
->will($this->returnSelf());
$orderCollection->expects($this->at(4))
$orderCollection->expects($this->at(5))
->method('setPageSize')
->with('5')
->will($this->returnSelf());
$orderCollection->expects($this->at(5))
$orderCollection->expects($this->at(6))
->method('load')
->will($this->returnSelf());
$this->block = new \Magento\Sales\Block\Order\Recent(
$this->context,
$this->orderCollectionFactory,
$this->customerSession,
$this->orderConfig,
$data
[],
$this->storeManagerMock
);
$this->assertEquals($orderCollection, $this->block->getOrders());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@

?>
<div class="block block-dashboard-orders">
<?php $_orders = $block->getOrders(); ?>
<?php
$_orders = $block->getOrders();
$count = count($_orders);
?>
<div class="block-title order">
<strong><?= /* @escapeNotVerified */ __('Recent Orders') ?></strong>
<?php if (sizeof($_orders->getItems()) > 0): ?>
<?php if ($count > 0): ?>
<a class="action view" href="<?= /* @escapeNotVerified */ $block->getUrl('sales/order/history') ?>">
<span><?= /* @escapeNotVerified */ __('View All') ?></span>
</a>
<?php endif; ?>
</div>
<div class="block-content">
<?= $block->getChildHtml() ?>
<?php if (sizeof($_orders->getItems()) > 0): ?>
<?php if ($count > 0): ?>
<div class="table-wrapper orders-recent">
<table class="data table table-order-items recent" id="my-orders-table">
<caption class="table-caption"><?= /* @escapeNotVerified */ __('Recent Orders') ?></caption>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,28 @@ public function testCategoryCreate()
}

/**
* Finds 4 categories
*
* @return Category[]
*/
private function getCategories()
{
/** @var Category $category */
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\Category::class
$collectionFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory::class
);

$result = $category->getCollection()->addAttributeToSelect('name')->getItems();
$result = array_slice($result, 2);
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
$collection = $collectionFactory->create();

$collection
->addAttributeToSelect('name')
->addAttributeToFilter('name', ['in' => [
'Category 1',
'Category 2',
'Category 3',
'Category 4',
]]);

return array_slice($result, 0, 4);
return array_values($collection->getItems());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\ResourceModel\Category;

class CollectionTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Catalog\Model\ResourceModel\Category\Collection
*/
private $collection;

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\ResourceModel\Category\Collection::class
);
}

protected function setDown()
{
/* Refresh stores memory cache after store deletion */
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Store\Model\StoreManagerInterface::class
)->reinitStores();
}

/**
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
* @magentoDataFixture Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php
*/
public function testJoinUrlRewriteOnDefault()
{
$categories = $this->collection->joinUrlRewrite()->addPathFilter('1/2/3');
$this->assertCount(1, $categories);
/** @var $category \Magento\Catalog\Model\Category */
$category = $categories->getFirstItem();
$this->assertStringEndsWith('category.html', $category->getUrl());
}

/**
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
* @magentoDataFixture Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php
*/
public function testJoinUrlRewriteNotOnDefaultStore()
{
$store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->create(\Magento\Store\Model\Store::class);
$storeId = $store->load('second_category_store', 'code')->getId();
$categories = $this->collection->setStoreId($storeId)->joinUrlRewrite()->addPathFilter('1/2/3');
$this->assertCount(1, $categories);
/** @var $category \Magento\Catalog\Model\Category */
$category = $categories->getFirstItem();
$this->assertStringEndsWith('category-3-on-2.html', $category->getUrl());
}
}
Loading

0 comments on commit 98e4a6e

Please sign in to comment.