Skip to content

Commit a93b568

Browse files
author
Bohdan Korablov
committed
Merge remote-tracking branch 'tangoc/MAGETWO-44539' into new_pr_bugs
2 parents 2b4fb06 + ffb15d2 commit a93b568

File tree

4 files changed

+134
-56
lines changed

4 files changed

+134
-56
lines changed

app/code/Magento/Sales/Helper/Guest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ public function loadValidOrder(App\RequestInterface $request)
139139
$lastName = $post['oar_billing_lastname'];
140140
$email = $post['oar_email'];
141141
$zip = $post['oar_zip'];
142+
$storeId = $this->_storeManager->getStore()->getId();
142143

143-
if (empty($incrementId) || empty($lastName) || empty($type) || !in_array(
144+
if (empty($incrementId) || empty($lastName) || empty($type) || empty($storeId) || !in_array(
144145
$type,
145146
['email', 'zip']
146147
) || $type == 'email' && empty($email) || $type == 'zip' && empty($zip)
@@ -149,7 +150,7 @@ public function loadValidOrder(App\RequestInterface $request)
149150
}
150151

151152
if (!$errors) {
152-
$order->loadByIncrementId($incrementId);
153+
$order = $order->loadByIncrementIdAndStoreId($incrementId, $storeId);
153154
}
154155

155156
$errors = true;

app/code/Magento/Sales/Model/Order.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
252252
*/
253253
protected $_trackCollectionFactory;
254254

255+
/**
256+
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
257+
*/
258+
protected $salesOrderCollectionFactory;
259+
255260
/**
256261
* @var PriceCurrencyInterface
257262
*/
@@ -284,6 +289,7 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
284289
* @param \Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory $shipmentCollectionFactory
285290
* @param \Magento\Sales\Model\ResourceModel\Order\Creditmemo\CollectionFactory $memoCollectionFactory
286291
* @param \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\CollectionFactory $trackCollectionFactory
292+
* @param ResourceModel\Order\CollectionFactory $salesOrderCollectionFactory
287293
* @param PriceCurrencyInterface $priceCurrency
288294
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productListFactory
289295
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
@@ -313,6 +319,7 @@ public function __construct(
313319
\Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory $shipmentCollectionFactory,
314320
\Magento\Sales\Model\ResourceModel\Order\Creditmemo\CollectionFactory $memoCollectionFactory,
315321
\Magento\Sales\Model\ResourceModel\Order\Shipment\Track\CollectionFactory $trackCollectionFactory,
322+
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $salesOrderCollectionFactory,
316323
PriceCurrencyInterface $priceCurrency,
317324
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productListFactory,
318325
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
@@ -337,6 +344,7 @@ public function __construct(
337344
$this->_shipmentCollectionFactory = $shipmentCollectionFactory;
338345
$this->_memoCollectionFactory = $memoCollectionFactory;
339346
$this->_trackCollectionFactory = $trackCollectionFactory;
347+
$this->salesOrderCollectionFactory = $salesOrderCollectionFactory;
340348
$this->priceCurrency = $priceCurrency;
341349
parent::__construct(
342350
$context,
@@ -435,6 +443,40 @@ public function loadByIncrementId($incrementId)
435443
return $this->loadByAttribute('increment_id', $incrementId);
436444
}
437445

446+
/**
447+
* Load order by system increment and store identifiers
448+
*
449+
* @param string $incrementId
450+
* @param string $storeId
451+
* @return \Magento\Sales\Model\Order
452+
*/
453+
public function loadByIncrementIdAndStoreId($incrementId, $storeId)
454+
{
455+
$orderCollection = $this->getSalesOrderCollection(
456+
[
457+
'increment_id' => $incrementId,
458+
'store_id' => $storeId
459+
]
460+
);
461+
return $orderCollection->getFirstItem();
462+
}
463+
464+
/**
465+
* Get sales Order collection model populated with data
466+
*
467+
* @param array $filters
468+
* @return \Magento\Sales\Model\ResourceModel\Order\Collection
469+
*/
470+
protected function getSalesOrderCollection(array $filters = [])
471+
{
472+
/** @var \Magento\Sales\Model\ResourceModel\Order\Collection $salesOrderCollection */
473+
$salesOrderCollection = $this->salesOrderCollectionFactory->create();
474+
foreach ($filters as $field => $condition) {
475+
$salesOrderCollection->addFieldToFilter($field, $condition);
476+
}
477+
return $salesOrderCollection->load();
478+
}
479+
438480
/**
439481
* Load order by custom attribute value. Attribute value should be unique
440482
*

app/code/Magento/Sales/Test/Unit/Helper/GuestTest.php

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
* Copyright © 2015 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Sales\Test\Unit\Helper;
87

9-
use \Magento\Sales\Helper\Guest;
10-
8+
use Magento\Sales\Helper\Guest;
119
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1210

1311
/**
14-
* Class GuestTest
1512
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1613
*/
1714
class GuestTest extends \PHPUnit_Framework_TestCase
@@ -49,6 +46,12 @@ class GuestTest extends \PHPUnit_Framework_TestCase
4946
/** @var \Magento\Framework\App\ViewInterface|\PHPUnit_Framework_MockObject_MockObject */
5047
protected $viewInterfaceMock;
5148

49+
/** @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject */
50+
protected $storeModelMock;
51+
52+
/** @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject */
53+
protected $salesOrderMock;
54+
5255
protected function setUp()
5356
{
5457
$this->appContextHelperMock = $this->getMock('Magento\Framework\App\Helper\Context', [], [], '', false);
@@ -66,6 +69,19 @@ protected function setUp()
6669
$this->managerInterfaceMock = $this->getMock('Magento\Framework\Message\ManagerInterface');
6770
$this->orderFactoryMock = $this->getMock('Magento\Sales\Model\OrderFactory', ['create'], [], '', false);
6871
$this->viewInterfaceMock = $this->getMock('Magento\Framework\App\ViewInterface');
72+
$this->storeModelMock = $this->getMockBuilder('Magento\Store\Model\Store')
73+
->disableOriginalConstructor()
74+
->getMock();
75+
$this->salesOrderMock = $this->getMock(
76+
'Magento\Sales\Model\Order',
77+
[
78+
'getProtectCode', 'loadByIncrementIdAndStoreId', 'loadByIncrementId',
79+
'getId', 'getBillingAddress', '__wakeup'
80+
],
81+
[],
82+
'',
83+
false
84+
);
6985

7086
$this->objectManagerHelper = new ObjectManagerHelper($this);
7187
$this->guest = $this->objectManagerHelper->getObject(
@@ -86,8 +102,6 @@ protected function setUp()
86102

87103
public function testLoadValidOrderNotEmptyPost()
88104
{
89-
$this->sessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
90-
91105
$post = [
92106
'oar_order_id' => 1,
93107
'oar_type' => 'email',
@@ -96,20 +110,17 @@ public function testLoadValidOrderNotEmptyPost()
96110
'oar_zip' => 'oar_zip',
97111

98112
];
113+
$storeId = '1';
99114
$incrementId = $post['oar_order_id'];
115+
$protectedCode = 'protectedCode';
116+
$this->sessionMock->expects($this->once())->method('isLoggedIn')->willReturn(false);
100117
$requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
101-
$requestMock->expects($this->once())->method('getPostValue')->will($this->returnValue($post));
102-
103-
$orderMock = $this->getMock(
104-
'Magento\Sales\Model\Order',
105-
['getProtectCode', 'loadByIncrementId', 'getId', 'getBillingAddress', '__wakeup'],
106-
[],
107-
'',
108-
false
109-
);
110-
$this->orderFactoryMock->expects($this->once())->method('create')->will($this->returnValue($orderMock));
111-
$orderMock->expects($this->once())->method('loadByIncrementId')->with($incrementId);
112-
$orderMock->expects($this->exactly(2))->method('getId')->will($this->returnValue($incrementId));
118+
$requestMock->expects($this->once())->method('getPostValue')->willReturn($post);
119+
$this->storeManagerInterfaceMock->expects($this->once())->method('getStore')->willReturn($this->storeModelMock);
120+
$this->storeModelMock->expects($this->once())->method('getId')->willReturn($storeId);
121+
$this->orderFactoryMock->expects($this->once())->method('create')->willReturn($this->salesOrderMock);
122+
$this->salesOrderMock->expects($this->once())->method('loadByIncrementIdAndStoreId')->willReturnSelf();
123+
$this->salesOrderMock->expects($this->any())->method('getId')->willReturn($incrementId);
113124

114125
$billingAddressMock = $this->getMock(
115126
'Magento\Sales\Model\Order\Address',
@@ -118,33 +129,27 @@ public function testLoadValidOrderNotEmptyPost()
118129
'',
119130
false
120131
);
121-
$billingAddressMock->expects($this->once())->method('getLastname')->will(
122-
$this->returnValue($post['oar_billing_lastname'])
123-
);
124-
$billingAddressMock->expects($this->once())->method('getEmail')->will(
125-
$this->returnValue($post['oar_email'])
126-
);
127-
$orderMock->expects($this->once())->method('getBillingAddress')->will($this->returnValue($billingAddressMock));
128-
$protectedCode = 'protectedCode';
129-
$orderMock->expects($this->once())->method('getProtectCode')->will($this->returnValue($protectedCode));
132+
$billingAddressMock->expects($this->once())->method('getLastname')->willReturn(($post['oar_billing_lastname']));
133+
$billingAddressMock->expects($this->once())->method('getEmail')->willReturn(($post['oar_email']));
134+
$this->salesOrderMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddressMock);
135+
$this->salesOrderMock->expects($this->once())->method('getProtectCode')->willReturn($protectedCode);
130136
$metaDataMock = $this->getMock(
131137
'Magento\Framework\Stdlib\Cookie\PublicCookieMetadata',
132138
[],
133139
[],
134140
'',
135141
false
136142
);
137-
$metaDataMock->expects($this->once())
138-
->method('setPath')
143+
$metaDataMock->expects($this->once())->method('setPath')
139144
->with(Guest::COOKIE_PATH)
140-
->will($this->returnSelf());
145+
->willReturnSelf();
141146
$metaDataMock->expects($this->once())
142147
->method('setHttpOnly')
143148
->with(true)
144-
->will($this->returnSelf());
149+
->willReturnSelf();
145150
$this->cookieMetadataFactoryMock->expects($this->once())
146151
->method('createPublicCookieMetadata')
147-
->will($this->returnValue($metaDataMock));
152+
->willReturn($metaDataMock);
148153
$this->cookieManagerMock->expects($this->once())
149154
->method('setPublicCookie')
150155
->with(Guest::COOKIE_NAME, $this->anything(), $metaDataMock);
@@ -153,26 +158,22 @@ public function testLoadValidOrderNotEmptyPost()
153158

154159
public function testLoadValidOrderStoredCookie()
155160
{
156-
$this->sessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
157-
$orderMock = $this->getMock(
158-
'Magento\Sales\Model\Order',
159-
['getProtectCode', 'loadByIncrementId', 'getId', 'getBillingAddress', '__wakeup'],
160-
[],
161-
'',
162-
false
163-
);
164161
$protectedCode = 'protectedCode';
165162
$incrementId = 1;
166163
$cookieData = $protectedCode . ':' . $incrementId;
167164
$cookieDataHash = base64_encode($cookieData);
168-
$this->orderFactoryMock->expects($this->once())->method('create')->will($this->returnValue($orderMock));
169-
170-
$this->cookieManagerMock->expects($this->once())->method('getCookie')->with(Guest::COOKIE_NAME)->will(
171-
$this->returnValue($cookieDataHash)
172-
);
173-
$orderMock->expects($this->once())->method('loadByIncrementId')->with($incrementId);
174-
$orderMock->expects($this->exactly(1))->method('getId')->will($this->returnValue($incrementId));
175-
$orderMock->expects($this->once())->method('getProtectCode')->will($this->returnValue($protectedCode));
165+
$this->sessionMock->expects($this->once())->method('isLoggedIn')->willReturn(false);
166+
$this->orderFactoryMock->expects($this->once())->method('create')->willReturn($this->salesOrderMock);
167+
$this->cookieManagerMock->expects($this->once())
168+
->method('getCookie')
169+
->with(Guest::COOKIE_NAME)
170+
->willReturn($cookieDataHash);
171+
$this->salesOrderMock->expects($this->once())
172+
->method('loadByIncrementId')
173+
->with($incrementId)
174+
->willReturnSelf();
175+
$this->salesOrderMock->expects($this->exactly(1))->method('getId')->willReturn($incrementId);
176+
$this->salesOrderMock->expects($this->once())->method('getProtectCode')->willReturn($protectedCode);
176177
$metaDataMock = $this->getMock(
177178
'Magento\Framework\Stdlib\Cookie\PublicCookieMetadata',
178179
[],
@@ -183,18 +184,17 @@ public function testLoadValidOrderStoredCookie()
183184
$metaDataMock->expects($this->once())
184185
->method('setPath')
185186
->with(Guest::COOKIE_PATH)
186-
->will($this->returnSelf());
187+
->willReturnSelf();
187188
$metaDataMock->expects($this->once())
188189
->method('setHttpOnly')
189190
->with(true)
190-
->will($this->returnSelf());
191+
->willReturnSelf();
191192
$this->cookieMetadataFactoryMock->expects($this->once())
192193
->method('createPublicCookieMetadata')
193-
->will($this->returnValue($metaDataMock));
194+
->willReturn($metaDataMock);
194195
$this->cookieManagerMock->expects($this->once())
195196
->method('setPublicCookie')
196197
->with(Guest::COOKIE_NAME, $this->anything(), $metaDataMock);
197-
198198
$requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
199199
$this->assertTrue($this->guest->loadValidOrder($requestMock));
200200
}

app/code/Magento/Sales/Test/Unit/Model/OrderTest.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
*/
66
namespace Magento\Sales\Test\Unit\Model;
77

8-
use \Magento\Sales\Model\Order;
9-
8+
use Magento\Sales\Model\Order;
109
use Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory as HistoryCollectionFactory;
1110

1211
/**
@@ -54,6 +53,16 @@ class OrderTest extends \PHPUnit_Framework_TestCase
5453
*/
5554
protected $priceCurrency;
5655

56+
/**
57+
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
58+
*/
59+
protected $salesOrderCollectionFactoryMock;
60+
61+
/**
62+
* @var \Magento\Sales\Model\ResourceModel\Order\Collection|\PHPUnit_Framework_MockObject_MockObject
63+
*/
64+
protected $salesOrderCollectionMock;
65+
5766
protected function setUp()
5867
{
5968
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -78,13 +87,24 @@ protected function setUp()
7887
'',
7988
false
8089
);
90+
$this->salesOrderCollectionFactoryMock = $this->getMock(
91+
'Magento\Sales\Model\ResourceModel\Order\CollectionFactory',
92+
['create'],
93+
[],
94+
'',
95+
false
96+
);
8197
$this->item = $this->getMock(
8298
'Magento\Sales\Model\ResourceModel\Order\Item',
8399
['isDeleted', 'getQtyToInvoice', 'getParentItemId', 'getQuoteItemId', 'getLockedDoInvoice'],
84100
[],
85101
'',
86102
false
87103
);
104+
$this->salesOrderCollectionMock = $this->getMockBuilder('Magento\Sales\Model\ResourceModel\Order\Collection')
105+
->disableOriginalConstructor()
106+
->setMethods(['addFieldToFilter', 'load', 'getFirstItem'])
107+
->getMock();
88108
$collection = $this->getMock('Magento\Sales\Model\ResourceModel\Order\Item\Collection', [], [], '', false);
89109
$collection->expects($this->any())
90110
->method('setOrderFilter')
@@ -121,6 +141,7 @@ protected function setUp()
121141
'data' => ['increment_id' => $this->incrementId],
122142
'context' => $context,
123143
'historyCollectionFactory' => $this->historyCollectionFactoryMock,
144+
'salesOrderCollectionFactory' => $this->salesOrderCollectionFactoryMock,
124145
'priceCurrency' => $this->priceCurrency
125146
]
126147
);
@@ -722,6 +743,20 @@ public function testGetStatusHistories()
722743
}
723744
}
724745

746+
public function testLoadByIncrementIdAndStoreId()
747+
{
748+
$incrementId = '000000001';
749+
$storeId = '2';
750+
$this->salesOrderCollectionFactoryMock
751+
->expects($this->once())
752+
->method('create')
753+
->willReturn($this->salesOrderCollectionMock);
754+
$this->salesOrderCollectionMock->expects($this->any())->method('addFieldToFilter')->willReturnSelf();
755+
$this->salesOrderCollectionMock->expects($this->once())->method('load')->willReturnSelf();
756+
$this->salesOrderCollectionMock->expects($this->once())->method('getFirstItem')->willReturn($this->order);
757+
$this->assertSame($this->order, $this->order->loadByIncrementIdAndStoreId($incrementId, $storeId));
758+
}
759+
725760
public function notInvoicingStatesProvider()
726761
{
727762
return [

0 commit comments

Comments
 (0)