Skip to content

Commit 4d48f22

Browse files
committed
MAGETWO-59529: Shipment with shipping label returns a blank result via REST API
- Added plugin to convert blob shipping label to string for REST API output
1 parent 0e2bca6 commit 4d48f22

File tree

7 files changed

+136
-1
lines changed

7 files changed

+136
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Sales\Plugin;
8+
9+
/**
10+
* Plugin to convert shipping label from blob to base64encoded string
11+
*/
12+
class ShippingLabelConverter
13+
{
14+
/**
15+
* Convert shipping label from blob to base64encoded string
16+
*
17+
* @param \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository
18+
* @param \Magento\Sales\Api\Data\ShipmentSearchResultInterface $searchResult
19+
* @return \Magento\Sales\Api\Data\ShipmentSearchResultInterface
20+
*
21+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
22+
*/
23+
public function afterGetList(
24+
\Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository,
25+
\Magento\Sales\Api\Data\ShipmentSearchResultInterface $searchResult
26+
) {
27+
/** @var \Magento\Sales\Model\Order\Shipment $item */
28+
foreach ($searchResult->getItems() as $item) {
29+
if ($item->getShippingLabel() !== null) {
30+
$item->setShippingLabel(base64_encode($item->getShippingLabel()));
31+
}
32+
}
33+
return $searchResult;
34+
}
35+
36+
/**
37+
* Convert shipping label from blob to base64encoded string
38+
*
39+
* @param \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository
40+
* @param \Magento\Sales\Api\Data\ShipmentInterface $shipment
41+
* @return \Magento\Sales\Api\Data\ShipmentInterface
42+
*
43+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
44+
*/
45+
public function afterGet(
46+
\Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository,
47+
\Magento\Sales\Api\Data\ShipmentInterface $shipment
48+
) {
49+
if ($shipment->getShippingLabel() !== null) {
50+
$shipment->setShippingLabel(base64_encode($shipment->getShippingLabel()));
51+
}
52+
return $shipment;
53+
}
54+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Sales\Test\Unit\Model\Order\Shipment\Plugin;
8+
9+
/**
10+
* Unit test for plugin to convert shipping label from blob to base64encoded string
11+
*/
12+
class ShippingLabelConverterTest extends \PHPUnit\Framework\TestCase
13+
{
14+
/**
15+
* @var \Magento\Sales\Plugin\ShippingLabelConverter
16+
*/
17+
private $model;
18+
19+
/**
20+
* @var \Magento\Sales\Api\Data\ShipmentInterface|\PHPUnit_Framework_MockObject_MockObject
21+
*/
22+
private $shipmentMock;
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
protected function setUp()
28+
{
29+
$this->model = new \Magento\Sales\Plugin\ShippingLabelConverter();
30+
31+
$shippingLabel = 'shipping_label_test';
32+
$shippingLabelEncoded = base64_encode('shipping_label_test');
33+
$this->shipmentMock = $this->getMockBuilder(\Magento\Sales\Api\Data\ShipmentInterface::class)
34+
->disableOriginalConstructor()->getMock();
35+
$this->shipmentMock->expects($this->exactly(2))->method('getShippingLabel')->willReturn($shippingLabel);
36+
$this->shipmentMock->expects($this->once())
37+
->method('setShippingLabel')
38+
->with($shippingLabelEncoded)
39+
->willReturnSelf();
40+
}
41+
42+
/**
43+
* @covers \Magento\Sales\Plugin\ShippingLabelConverter::afterGet()
44+
*/
45+
public function testAfterGet()
46+
{
47+
$this->model->afterGet(
48+
$this->getMockBuilder(\Magento\Sales\Api\ShipmentRepositoryInterface::class)
49+
->disableOriginalConstructor()->getMock(),
50+
$this->shipmentMock
51+
);
52+
}
53+
54+
/**
55+
* @covers \Magento\Sales\Plugin\ShippingLabelConverter::afterGetList()
56+
*/
57+
public function testAfterGetList()
58+
{
59+
$searchResultMock = $this->getMockBuilder(\Magento\Sales\Api\Data\ShipmentSearchResultInterface::class)
60+
->disableOriginalConstructor()->getMock();
61+
$searchResultMock->expects($this->once())->method('getItems')->willReturn([$this->shipmentMock]);
62+
63+
$this->model->afterGetList(
64+
$this->getMockBuilder(\Magento\Sales\Api\ShipmentRepositoryInterface::class)
65+
->disableOriginalConstructor()->getMock(),
66+
$searchResultMock
67+
);
68+
}
69+
}

app/code/Magento/Sales/etc/webapi_rest/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
<type name="Magento\Sales\Model\ResourceModel\Order">
1313
<plugin name="authorization" type="Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization" />
1414
</type>
15+
<type name="Magento\Sales\Api\ShipmentRepositoryInterface">
16+
<plugin name="convert_blob_to_string" type="Magento\Sales\Plugin\ShippingLabelConverter" />
17+
</type>
1518
</config>

app/code/Magento/Sales/etc/webapi_soap/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
<type name="Magento\Sales\Model\ResourceModel\Order">
1313
<plugin name="authorization" type="Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization" />
1414
</type>
15+
<type name="Magento\Sales\Api\ShipmentRepositoryInterface">
16+
<plugin name="convert_blob_to_string" type="Magento\Sales\Plugin\ShippingLabelConverter" />
17+
</type>
1518
</config>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ public function testShipmentGet()
5050
],
5151
];
5252
$result = $this->_webApiCall($serviceInfo, ['id' => $shipment->getId()]);
53-
$this->assertEquals($result, 'test_shipping_label');
53+
$this->assertEquals($result, base64_encode('test_shipping_label'));
5454
}
5555
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,7 @@ public function testShipmentList()
9191
$this->assertEquals($searchData, $result['search_criteria']);
9292
$this->assertEquals('100000002', $result['items'][0]['increment_id']);
9393
$this->assertEquals('100000003', $result['items'][1]['increment_id']);
94+
$this->assertEquals(base64_encode('shipping_label_100000002'), $result['items'][0]['shipping_label']);
95+
$this->assertEquals(base64_encode('shipping_label_100000003'), $result['items'][1]['shipping_label']);
9496
}
9597
}

dev/tests/integration/testsuite/Magento/Sales/_files/shipment_list.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,28 @@
2020
'shipping_address_id' => 1,
2121
'shipment_status' => \Magento\Sales\Model\Order\Shipment::STATUS_NEW,
2222
'store_id' => 1,
23+
'shipping_label' => 'shipping_label_100000001',
2324
],
2425
[
2526
'increment_id' => '100000002',
2627
'shipping_address_id' => 3,
2728
'shipment_status' => \Magento\Sales\Model\Order\Shipment::STATUS_NEW,
2829
'store_id' => 1,
30+
'shipping_label' => 'shipping_label_100000002',
2931
],
3032
[
3133
'increment_id' => '100000003',
3234
'shipping_address_id' => 3,
3335
'shipment_status' => \Magento\Sales\Model\Order\Shipment::STATUS_NEW,
3436
'store_id' => 1,
37+
'shipping_label' => 'shipping_label_100000003',
3538
],
3639
[
3740
'increment_id' => '100000004',
3841
'shipping_address_id' => 4,
3942
'shipment_status' => 'closed',
4043
'store_id' => 1,
44+
'shipping_label' => 'shipping_label_100000004',
4145
],
4246
];
4347

0 commit comments

Comments
 (0)