Skip to content

Commit 1ebe5f1

Browse files
committed
MAGETWO-93057: [2.3] salesShipmentRepositoryV1 throws error when adding tracking
1 parent d5ad9dd commit 1ebe5f1

File tree

3 files changed

+140
-82
lines changed

3 files changed

+140
-82
lines changed

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

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Sales\Api\Data\ShipmentInterface;
1010
use Magento\Sales\Model\AbstractModel;
1111
use Magento\Sales\Model\EntityInterface;
12+
use Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\Collection as CommentsCollection;
1213

1314
/**
1415
* Sales order shipment model
@@ -94,9 +95,14 @@ class Shipment extends AbstractModel implements EntityInterface, ShipmentInterfa
9495
protected $orderRepository;
9596

9697
/**
97-
* @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection|null
98+
* @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection
9899
*/
99-
private $tracksCollection = null;
100+
private $tracksCollection;
101+
102+
/**
103+
* @var CommentsCollection
104+
*/
105+
private $commentsCollection;
100106

101107
/**
102108
* @param \Magento\Framework\Model\Context $context
@@ -414,43 +420,45 @@ public function addTrack(\Magento\Sales\Model\Order\Shipment\Track $track)
414420
public function addComment($comment, $notify = false, $visibleOnFront = false)
415421
{
416422
if (!$comment instanceof \Magento\Sales\Model\Order\Shipment\Comment) {
417-
$comment = $this->_commentFactory->create()->setComment(
418-
$comment
419-
)->setIsCustomerNotified(
420-
$notify
421-
)->setIsVisibleOnFront(
422-
$visibleOnFront
423-
);
423+
$comment = $this->_commentFactory->create()
424+
->setComment($comment)
425+
->setIsCustomerNotified($notify)
426+
->setIsVisibleOnFront($visibleOnFront);
424427
}
425-
$comment->setShipment($this)->setParentId($this->getId())->setStoreId($this->getStoreId());
428+
$comment->setShipment($this)
429+
->setParentId($this->getId())
430+
->setStoreId($this->getStoreId());
426431
if (!$comment->getId()) {
427432
$this->getCommentsCollection()->addItem($comment);
428433
}
434+
$comments = $this->getComments();
435+
$comments[] = $comment;
436+
$this->setComments($comments);
429437
$this->_hasDataChanges = true;
430438
return $this;
431439
}
432440

433441
/**
434-
* Retrieve comments collection.
442+
* Retrieves comments collection.
435443
*
436444
* @param bool $reload
437-
* @return \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\Collection
445+
* @return CommentsCollection
438446
*/
439447
public function getCommentsCollection($reload = false)
440448
{
441-
if (!$this->hasData(ShipmentInterface::COMMENTS) || $reload) {
442-
$comments = $this->_commentCollectionFactory->create()
443-
->setShipmentFilter($this->getId())
444-
->setCreatedAtOrder();
445-
$this->setComments($comments);
446-
449+
if ($this->commentsCollection === null || $reload) {
450+
$this->commentsCollection = $this->_commentCollectionFactory->create();
447451
if ($this->getId()) {
448-
foreach ($this->getComments() as $comment) {
452+
$this->commentsCollection->setShipmentFilter($this->getId())
453+
->setCreatedAtOrder();
454+
455+
foreach ($this->commentsCollection as $comment) {
449456
$comment->setShipment($this);
450457
}
451458
}
452459
}
453-
return $this->getComments();
460+
461+
return $this->commentsCollection;
454462
}
455463

456464
/**

app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentTest.php

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ShipmentTest extends \PHPUnit\Framework\TestCase
2525
private $commentCollection;
2626

2727
/**
28-
* @var \Magento\Sales\Model\Order\shipment
28+
* @var Shipment
2929
*/
3030
private $shipmentModel;
3131

@@ -46,9 +46,6 @@ public function testGetIncrementId()
4646
$this->assertEquals('test_increment_id', $this->shipmentModel->getIncrementId());
4747
}
4848

49-
/**
50-
* @covers \Magento\Sales\Model\Order\Shipment::getCommentsCollection
51-
*/
5249
public function testGetCommentsCollection()
5350
{
5451
$shipmentId = 1;
@@ -58,36 +55,29 @@ public function testGetCommentsCollection()
5855
->disableOriginalConstructor()
5956
->setMethods(['setShipment'])
6057
->getMock();
61-
$shipmentItem->expects(static::once())
62-
->method('setShipment')
58+
$shipmentItem->method('setShipment')
6359
->with($this->shipmentModel);
6460
$collection = [$shipmentItem];
6561

66-
$this->commentCollection->expects(static::once())
62+
$this->commentCollection->expects(self::once())
6763
->method('setShipmentFilter')
6864
->with($shipmentId)
6965
->willReturnSelf();
70-
$this->commentCollection->expects(static::once())
66+
$this->commentCollection->expects(self::once())
7167
->method('setCreatedAtOrder')
7268
->willReturnSelf();
7369

74-
$this->commentCollection->expects(static::once())
75-
->method('load')
76-
->willReturnSelf();
77-
7870
$reflection = new \ReflectionClass(Collection::class);
7971
$reflectionProperty = $reflection->getProperty('_items');
8072
$reflectionProperty->setAccessible(true);
8173
$reflectionProperty->setValue($this->commentCollection, $collection);
8274

83-
$expected = $this->shipmentModel->getCommentsCollection();
75+
$actual = $this->shipmentModel->getCommentsCollection();
8476

85-
static::assertEquals($expected, $this->commentCollection);
77+
self::assertTrue(is_object($actual));
78+
self::assertEquals($this->commentCollection, $actual);
8679
}
8780

88-
/**
89-
* @covers \Magento\Sales\Model\Order\Shipment::getComments
90-
*/
9181
public function testGetComments()
9282
{
9383
$shipmentId = 1;
@@ -97,30 +87,27 @@ public function testGetComments()
9787
->disableOriginalConstructor()
9888
->setMethods(['setShipment'])
9989
->getMock();
100-
$shipmentItem->expects(static::once())
90+
$shipmentItem->expects(self::once())
10191
->method('setShipment')
10292
->with($this->shipmentModel);
10393
$collection = [$shipmentItem];
10494

105-
$this->commentCollection->expects(static::once())
106-
->method('setShipmentFilter')
95+
$this->commentCollection->method('setShipmentFilter')
10796
->with($shipmentId)
10897
->willReturnSelf();
10998

110-
$this->commentCollection->expects(static::once())
111-
->method('load')
112-
->willReturnSelf();
113-
11499
$reflection = new \ReflectionClass(Collection::class);
115100
$reflectionProperty = $reflection->getProperty('_items');
116101
$reflectionProperty->setAccessible(true);
117102
$reflectionProperty->setValue($this->commentCollection, $collection);
118103

119-
$this->commentCollection->expects(static::once())
104+
$this->commentCollection->expects(self::once())
120105
->method('getItems')
121106
->willReturn($collection);
122107

123-
static::assertEquals($this->shipmentModel->getComments(), $collection);
108+
$actual = $this->shipmentModel->getComments();
109+
self::assertTrue(is_array($actual));
110+
self::assertEquals($collection, $actual);
124111
}
125112

126113
/**
@@ -131,16 +118,15 @@ private function initCommentsCollectionFactoryMock()
131118
{
132119
$this->commentCollection = $this->getMockBuilder(Collection::class)
133120
->disableOriginalConstructor()
134-
->setMethods(['setShipmentFilter', 'setCreatedAtOrder', 'getItems', 'load', '__wakeup'])
121+
->setMethods(['setShipmentFilter', 'setCreatedAtOrder', 'getItems', 'load'])
135122
->getMock();
136123

137124
$this->commentCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
138125
->disableOriginalConstructor()
139126
->setMethods(['create'])
140127
->getMock();
141128

142-
$this->commentCollectionFactory->expects(static::any())
143-
->method('create')
129+
$this->commentCollectionFactory->method('create')
144130
->willReturn($this->commentCollection);
145131
}
146132
}

dev/tests/integration/testsuite/Magento/Sales/Model/Order/ShipmentTest.php

Lines changed: 97 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,147 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Sales\Model\Order;
89

10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\Payment\Helper\Data;
12+
use Magento\Sales\Api\Data\CommentInterface;
13+
use Magento\Sales\Api\Data\OrderInterface;
14+
use Magento\Sales\Api\Data\ShipmentInterface;
15+
use Magento\Sales\Api\Data\ShipmentItemInterface;
16+
use Magento\Sales\Api\Data\ShipmentTrackInterface;
17+
use Magento\Sales\Api\OrderRepositoryInterface;
18+
use Magento\Sales\Api\ShipmentRepositoryInterface;
19+
use Magento\TestFramework\Helper\Bootstrap;
20+
use Magento\TestFramework\ObjectManager;
21+
922
/**
10-
* Class ShipmentTest
1123
* @magentoAppIsolation enabled
12-
* @package Magento\Sales\Model\Order
24+
* @magentoDataFixture Magento/Sales/_files/order.php
1325
*/
1426
class ShipmentTest extends \PHPUnit\Framework\TestCase
1527
{
28+
/**
29+
* @var ObjectManager
30+
*/
31+
private $objectManager;
32+
33+
/**
34+
* @var ShipmentRepositoryInterface
35+
*/
36+
private $shipmentRepository;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
protected function setUp()
42+
{
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->shipmentRepository = $this->objectManager->get(ShipmentRepositoryInterface::class);
45+
}
46+
1647
/**
1748
* Check the correctness and stability of set/get packages of shipment
1849
*
19-
* @magentoDataFixture Magento/Sales/_files/order.php
50+
* @magentoAppArea frontend
2051
*/
2152
public function testPackages()
2253
{
23-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
24-
$objectManager->get(\Magento\Framework\App\State::class)->setAreaCode('frontend');
25-
$order = $objectManager->create(\Magento\Sales\Model\Order::class);
26-
$order->loadByIncrementId('100000001');
27-
$order->setCustomerEmail('[email protected]');
54+
$order = $this->getOrder('100000001');
2855

2956
$payment = $order->getPayment();
30-
$paymentInfoBlock = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
31-
\Magento\Payment\Helper\Data::class
32-
)->getInfoBlock(
33-
$payment
34-
);
57+
$paymentInfoBlock = $this->objectManager->get(Data::class)
58+
->getInfoBlock($payment);
3559
$payment->setBlockMock($paymentInfoBlock);
3660

3761
$items = [];
3862
foreach ($order->getItems() as $item) {
3963
$items[$item->getId()] = $item->getQtyOrdered();
4064
}
4165
/** @var \Magento\Sales\Model\Order\Shipment $shipment */
42-
$shipment = $objectManager->get(ShipmentFactory::class)->create($order, $items);
66+
$shipment = $this->objectManager->get(ShipmentFactory::class)->create($order, $items);
4367

4468
$packages = [['1'], ['2']];
4569

4670
$shipment->setPackages($packages);
47-
$this->assertEquals($packages, $shipment->getPackages());
48-
$shipment->save();
49-
$shipment->save();
50-
$shipment->load($shipment->getId());
51-
$this->assertEquals($packages, $shipment->getPackages());
71+
$saved = $this->shipmentRepository->save($shipment);
72+
self::assertEquals($packages, $saved->getPackages());
5273
}
5374

5475
/**
5576
* Check that getTracksCollection() always return collection instance.
56-
*
57-
* @magentoDataFixture Magento/Sales/_files/order.php
5877
*/
5978
public function testAddTrack()
6079
{
61-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
80+
$order = $this->getOrder('100000001');
6281

63-
$order = $objectManager->create(\Magento\Sales\Model\Order::class);
64-
$order->loadByIncrementId('100000001');
82+
/** @var ShipmentInterface $shipment */
83+
$shipment = $this->objectManager->create(ShipmentInterface::class);
84+
85+
/** @var ShipmentTrackInterface $track */
86+
$track = $this->objectManager->create(ShipmentTrackInterface::class);
87+
$track->setNumber('Test Number')
88+
->setTitle('Test Title')
89+
->setCarrierCode('Test CODE');
6590

6691
$items = [];
6792
foreach ($order->getItems() as $item) {
6893
$items[$item->getId()] = $item->getQtyOrdered();
6994
}
7095
/** @var \Magento\Sales\Model\Order\Shipment $shipment */
71-
$shipment = $objectManager->get(ShipmentFactory::class)->create($order, $items);
96+
$shipment = $this->objectManager->get(ShipmentFactory::class)->create($order, $items);
97+
$shipment->addTrack($track);
7298
$shipment->save();
99+
$saved = $this->shipmentRepository->save($shipment);
100+
self::assertNotEmpty($saved->getTracks());
101+
}
102+
103+
/**
104+
* Checks adding comment to the shipment entity.
105+
*/
106+
public function testAddComment()
107+
{
108+
$message1 = 'Test Comment 1';
109+
$message2 = 'Test Comment 2';
110+
$order = $this->getOrder('100000001');
111+
112+
/** @var ShipmentInterface $shipment */
113+
$shipment = $this->objectManager->create(ShipmentInterface::class);
114+
$shipment->setOrder($order)
115+
->addItem($this->objectManager->create(ShipmentItemInterface::class))
116+
->addComment($message1)
117+
->addComment($message2);
73118

74-
/** @var $track \Magento\Sales\Model\Order\Shipment\Track */
75-
$track = $objectManager->get(\Magento\Sales\Model\Order\Shipment\Track::class);
76-
$track->setNumber('Test Number')->setTitle('Test Title')->setCarrierCode('Test CODE');
119+
$saved = $this->shipmentRepository->save($shipment);
120+
121+
$comments = $saved->getComments();
122+
$actual = array_map(function (CommentInterface $comment) {
123+
return $comment->getComment();
124+
}, $comments);
125+
self::assertEquals(2, count($actual));
126+
self::assertEquals([$message1, $message2], $actual);
127+
}
128+
129+
/**
130+
* Gets order entity by increment id.
131+
*
132+
* @param string $incrementId
133+
* @return OrderInterface
134+
*/
135+
private function getOrder(string $incrementId): OrderInterface
136+
{
137+
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
138+
$searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
139+
$searchCriteria = $searchCriteriaBuilder->addFilter('increment_id', $incrementId)
140+
->create();
77141

78-
$this->assertEmpty($shipment->getTracks());
79-
$shipment->addTrack($track)->save();
142+
/** @var OrderRepositoryInterface $repository */
143+
$repository = $this->objectManager->get(OrderRepositoryInterface::class);
144+
$items = $repository->getList($searchCriteria)
145+
->getItems();
80146

81-
//to empty cache
82-
$shipment->setTracks(null);
83-
$this->assertNotEmpty($shipment->getTracks());
147+
return array_pop($items);
84148
}
85149
}

0 commit comments

Comments
 (0)