Skip to content

Commit

Permalink
magento#14623: Join extension attributes are not added to Order and O…
Browse files Browse the repository at this point in the history
…rder Items

- completed the implementation of tests
  • Loading branch information
Kiraberos committed Apr 11, 2023
1 parent 288f3ad commit 09bad75
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,16 @@
</join>
</attribute>
</extension_attributes>
<extension_attributes for="Magento\Sales\Api\Data\OrderItemInterface">
<attribute code="orderItemApiTestAttribute" type="Magento\User\Api\Data\UserInterface">
<join reference_table="admin_user"
join_on_field="store_id"
reference_field="user_id"
>
<field>firstname</field>
<field>lastname</field>
<field>email</field>
</join>
</attribute>
</extension_attributes>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\Sales\Model\Order;
use Magento\User\Model\User;

/**
* Test join directives.
Expand All @@ -32,17 +34,23 @@ class JoinDirectivesTest extends \Magento\TestFramework\TestCase\WebapiAbstract
private $filterBuilder;

/**
* @var \Magento\User\Model\User
* @var User
*/
private $user;

/**
* @var Order|mixed
*/
private mixed $order;

protected function setUp(): void
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->searchBuilder = $objectManager->create(\Magento\Framework\Api\SearchCriteriaBuilder::class);
$this->sortOrderBuilder = $objectManager->create(\Magento\Framework\Api\SortOrderBuilder::class);
$this->filterBuilder = $objectManager->create(\Magento\Framework\Api\FilterBuilder::class);
$this->user = $objectManager->create(\Magento\User\Model\User::class);
$this->user = $objectManager->create(User::class);
$this->order = $objectManager->create(Order::class);
}

/**
Expand Down Expand Up @@ -173,6 +181,52 @@ public function testGetOrdertList()
$this->assertEquals($expectedExtensionAttributes['email'], $testAttribute['email']);
}

/**
* Test get list of order items with extension attributes.
*
* @magentoApiDataFixture Magento/Sales/_files/order.php
* @magentoAppIsolation enabled
*/
public function testGetOrderItemList()
{
$orderItem = current($this->order->loadByIncrementId('100000001')->getItems());
$orderItemId = $orderItem->getId();
$filter = $this->filterBuilder
->setField('item_id')
->setValue($orderItemId)
->setConditionType('eq')
->create();
$this->searchBuilder->addFilters([$filter]);
$searchData = $this->searchBuilder->create()->__toArray();

$requestData = ['searchCriteria' => $searchData];

$restResourcePath = '/V1/orders/items/';
$soapService = 'salesOrderItemRepositoryV1';
$expectedExtensionAttributes = $this->getExpectedExtensionAttributes();

$serviceInfo = [
'rest' => [
'resourcePath' => $restResourcePath . '?' . http_build_query($requestData),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => $soapService,
'operation' => $soapService . 'GetList',
],
];
$searchResult = $this->_webApiCall($serviceInfo, $requestData);

$this->assertArrayHasKey('items', $searchResult);
$itemData = array_pop($searchResult['items']);
$this->assertArrayHasKey('extension_attributes', $itemData);
$this->assertArrayHasKey('order_item_api_test_attribute', $itemData['extension_attributes']);
$testAttribute = $itemData['extension_attributes']['order_item_api_test_attribute'];
$this->assertEquals($expectedExtensionAttributes['firstname'], $testAttribute['first_name']);
$this->assertEquals($expectedExtensionAttributes['lastname'], $testAttribute['last_name']);
$this->assertEquals($expectedExtensionAttributes['email'], $testAttribute['email']);
}

/**
* Retrieve the admin user's information.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
$product = $productRepository->get('simple');
$billingAddress = $objectManager->create(OrderAddress::class, ['data' => $addressData]);
$storeId = $objectManager->get(StoreManagerInterface::class)->getStore()->getId();
$billingAddress->setAddressType('billing');

$shippingAddress = clone $billingAddress;
Expand All @@ -50,7 +51,8 @@
->setRowTotal($product->getPrice())
->setProductType('simple')
->setName($product->getName())
->setSku($product->getSku());
->setSku($product->getSku())
->setStoreId($storeId);

/** @var Order $order */
$order = $objectManager->create(Order::class);
Expand All @@ -67,7 +69,7 @@
->setCustomerEmail('customer@example.com')
->setBillingAddress($billingAddress)
->setShippingAddress($shippingAddress)
->setStoreId($objectManager->get(StoreManagerInterface::class)->getStore()->getId())
->setStoreId($storeId)
->addItem($orderItem)
->setPayment($payment);

Expand Down

0 comments on commit 09bad75

Please sign in to comment.