diff --git a/app/code/Magento/Sales/Block/Order/Recent.php b/app/code/Magento/Sales/Block/Order/Recent.php index e57aa1fe420a0..7e5be0ebfbba2 100644 --- a/app/code/Magento/Sales/Block/Order/Recent.php +++ b/app/code/Magento/Sales/Block/Order/Recent.php @@ -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 * @@ -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 */ @@ -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); } /** @@ -55,11 +76,22 @@ 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()] @@ -67,7 +99,7 @@ protected function _construct() 'created_at', 'desc' )->setPageSize( - '5' + self::ORDER_LIMIT )->load(); $this->setOrders($orders); } diff --git a/app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php b/app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php index 99528983a13c9..96162aca42e12 100644 --- a/app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php +++ b/app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php @@ -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 { /** @@ -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)); @@ -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)); @@ -85,17 +107,21 @@ 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( @@ -103,7 +129,8 @@ public function testConstructMethod() $this->orderCollectionFactory, $this->customerSession, $this->orderConfig, - $data + [], + $this->storeManagerMock ); $this->assertEquals($orderCollection, $this->block->getOrders()); } diff --git a/app/code/Magento/Sales/view/frontend/templates/order/recent.phtml b/app/code/Magento/Sales/view/frontend/templates/order/recent.phtml index df7e6bf334d9a..bb354e920c529 100644 --- a/app/code/Magento/Sales/view/frontend/templates/order/recent.phtml +++ b/app/code/Magento/Sales/view/frontend/templates/order/recent.phtml @@ -8,10 +8,13 @@ ?>