Skip to content

Commit 7d0940b

Browse files
author
Sergey Shvets
committed
Merge remote-tracking branch 'm2/2.2-develop' into Chaika_2.2.4_PR2
2 parents eb46804 + d79156c commit 7d0940b

File tree

6 files changed

+61
-5
lines changed

6 files changed

+61
-5
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Sales\Api\Data\ShipmentTrackSearchResultInterfaceFactory;
1515
use Magento\Sales\Api\ShipmentTrackRepositoryInterface;
1616
use Magento\Sales\Model\Spi\ShipmentTrackResourceInterface;
17+
use Psr\Log\LoggerInterface;
1718

1819
class TrackRepository implements ShipmentTrackRepositoryInterface
1920
{
@@ -37,23 +38,30 @@ class TrackRepository implements ShipmentTrackRepositoryInterface
3738
*/
3839
private $collectionProcessor;
3940

41+
/**
42+
* @var LoggerInterface
43+
*/
44+
private $logger;
45+
4046
/**
4147
* @param ShipmentTrackResourceInterface $trackResource
4248
* @param ShipmentTrackInterfaceFactory $trackFactory
4349
* @param ShipmentTrackSearchResultInterfaceFactory $searchResultFactory
4450
* @param CollectionProcessorInterface $collectionProcessor
51+
* @param LoggerInterface|null $logger
4552
*/
4653
public function __construct(
4754
ShipmentTrackResourceInterface $trackResource,
4855
ShipmentTrackInterfaceFactory $trackFactory,
4956
ShipmentTrackSearchResultInterfaceFactory $searchResultFactory,
50-
CollectionProcessorInterface $collectionProcessor
57+
CollectionProcessorInterface $collectionProcessor,
58+
LoggerInterface $logger = null
5159
) {
52-
5360
$this->trackResource = $trackResource;
5461
$this->trackFactory = $trackFactory;
5562
$this->searchResultFactory = $searchResultFactory;
5663
$this->collectionProcessor = $collectionProcessor;
64+
$this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance()->get(LoggerInterface::class);
5765
}
5866

5967
/**
@@ -64,6 +72,7 @@ public function getList(SearchCriteriaInterface $searchCriteria)
6472
$searchResult = $this->searchResultFactory->create();
6573
$this->collectionProcessor->process($searchCriteria, $searchResult);
6674
$searchResult->setSearchCriteria($searchCriteria);
75+
6776
return $searchResult;
6877
}
6978

@@ -74,6 +83,7 @@ public function get($id)
7483
{
7584
$entity = $this->trackFactory->create();
7685
$this->trackResource->load($entity, $id);
86+
7787
return $entity;
7888
}
7989

@@ -85,8 +95,10 @@ public function delete(ShipmentTrackInterface $entity)
8595
try {
8696
$this->trackResource->delete($entity);
8797
} catch (\Exception $e) {
98+
$this->logger->error($e->getMessage());
8899
throw new CouldNotDeleteException(__('Could not delete the shipment tracking.'), $e);
89100
}
101+
90102
return true;
91103
}
92104

@@ -98,8 +110,10 @@ public function save(ShipmentTrackInterface $entity)
98110
try {
99111
$this->trackResource->save($entity);
100112
} catch (\Exception $e) {
113+
$this->logger->error($e->getMessage());
101114
throw new CouldNotSaveException(__('Could not save the shipment tracking.'), $e);
102115
}
116+
103117
return $entity;
104118
}
105119

@@ -109,6 +123,7 @@ public function save(ShipmentTrackInterface $entity)
109123
public function deleteById($id)
110124
{
111125
$entity = $this->get($id);
126+
112127
return $this->delete($entity);
113128
}
114129
}

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

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

8+
use Magento\Framework\Exception\LocalizedException;
89
use Magento\Sales\Model\ResourceModel\EntityAbstract as SalesResource;
910
use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
1011
use Magento\Sales\Model\Spi\ShipmentTrackResourceInterface;
@@ -74,7 +75,7 @@ protected function _construct()
7475
*
7576
* @param \Magento\Framework\Model\AbstractModel $object
7677
* @return $this
77-
* @throws \Magento\Framework\Exception\LocalizedException
78+
* @throws LocalizedException
7879
*/
7980
protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
8081
{
@@ -86,11 +87,16 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
8687
parent::_beforeSave($object);
8788
$errors = $this->validator->validate($object);
8889
if (!empty($errors)) {
89-
throw new \Magento\Framework\Exception\LocalizedException(
90+
throw new LocalizedException(
9091
__("Cannot save track:\n%1", implode("\n", $errors))
9192
);
9293
}
9394

95+
if ($object->getShipment()->getOrder()->getId() != $object->getOrderId()) {
96+
$errorMessage = 'Shipment with requested ID %1 doesn\'t correspond with Order with requested ID %2.';
97+
throw new LocalizedException(__($errorMessage, $object->getParentId(), $object->getOrderId()));
98+
}
99+
94100
return $this;
95101
}
96102
}

app/code/Magento/Sales/Test/Unit/Model/ResourceModel/Order/Shipment/TrackTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ protected function setUp()
8989
*/
9090
public function testSave()
9191
{
92+
$shipmentMock = $this->createMock(\Magento\Sales\Model\Order\Shipment::class);
93+
$orderMock = $this->createMock(\Magento\Sales\Model\Order::class);
9294
$this->entitySnapshotMock->expects($this->once())
9395
->method('isModified')
9496
->with($this->trackModelMock)
@@ -98,6 +100,8 @@ public function testSave()
98100
->with($this->equalTo($this->trackModelMock))
99101
->will($this->returnValue([]));
100102
$this->trackModelMock->expects($this->any())->method('getData')->willReturn([]);
103+
$this->trackModelMock->expects($this->atLeastOnce())->method('getShipment')->willReturn($shipmentMock);
104+
$shipmentMock->expects($this->atLeastOnce())->method('getOrder')->willReturn($orderMock);
101105
$this->trackResource->save($this->trackModelMock);
102106
$this->assertTrue(true);
103107
}

app/code/Magento/Sales/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,3 +803,4 @@ Created,Created
803803
"PDF Shipments","PDF Shipments"
804804
"PDF Creditmemos","PDF Creditmemos"
805805
Refunds,Refunds
806+
"Shipment with requested ID %1 doesn't correspond with Order with requested ID %2.","Shipment with requested ID %1 doesn't correspond with Order with requested ID %2."

dev/tests/api-functional/testsuite/Magento/Sales/Service/V1/ShipmentAddTrackTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,36 @@ public function testShipmentAddTrack()
7474
self::assertNotEmpty($result[ShipmentTrackInterface::ENTITY_ID]);
7575
self::assertEquals($shipment->getId(), $result[ShipmentTrackInterface::PARENT_ID]);
7676
}
77+
78+
/**
79+
* Try to create track with wrong order ID.
80+
*
81+
* @magentoApiDataFixture Magento/Sales/_files/shipment.php
82+
*
83+
* @expectedException \Exception
84+
* @expectedExceptionMessage Could not save the shipment tracking.
85+
*/
86+
public function testShipmentAddTrackThrowsError()
87+
{
88+
$shipmentCollection = $this->objectManager->get(Collection::class);
89+
/** @var \Magento\Sales\Model\Order\Shipment $shipment */
90+
$shipment = $shipmentCollection->getFirstItem();
91+
92+
$trackData = [
93+
ShipmentTrackInterface::ENTITY_ID => null,
94+
ShipmentTrackInterface::ORDER_ID => $shipment->getOrderId() + 1,
95+
ShipmentTrackInterface::PARENT_ID => $shipment->getId(),
96+
ShipmentTrackInterface::WEIGHT => 20,
97+
ShipmentTrackInterface::QTY => 5,
98+
ShipmentTrackInterface::TRACK_NUMBER => 2,
99+
ShipmentTrackInterface::DESCRIPTION => 'Shipment description',
100+
ShipmentTrackInterface::TITLE => 'Shipment title',
101+
ShipmentTrackInterface::CARRIER_CODE => Track::CUSTOM_CARRIER_CODE,
102+
];
103+
104+
$this->_webApiCall($this->getServiceInfo(), ['entity' => $trackData]);
105+
}
106+
77107
/**
78108
* Returns details about API endpoints and services.
79109
*

lib/internal/Magento/Framework/Data/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ public function clear()
492492
*
493493
* Returns array with results of callback for each item
494494
*
495-
* @param string $callback
495+
* @param callable $callback
496496
* @param array $args
497497
* @return array
498498
*/

0 commit comments

Comments
 (0)