ServiceVersionV1Test.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Class to test routing based on Service Versioning(for V1 version of a service)
  8. */
  9. namespace Magento\Webapi\Routing;
  10. use Magento\Framework\Api\AttributeValue;
  11. use Magento\TestFramework\Authentication\OauthHelper;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. use Magento\TestModule1\Service\V1\Entity\Item;
  14. use Magento\TestModule1\Service\V1\Entity\ItemFactory;
  15. class ServiceVersionV1Test extends \Magento\Webapi\Routing\BaseService
  16. {
  17. /**
  18. * @var string
  19. */
  20. protected $_version;
  21. /**
  22. * @var string
  23. */
  24. protected $_restResourcePath;
  25. /**
  26. * @var string
  27. */
  28. protected $_soapService = 'testModule1AllSoapAndRest';
  29. /** @var \Magento\Framework\Api\AttributeValueFactory */
  30. protected $valueFactory;
  31. /** @var ItemFactory */
  32. protected $itemFactory;
  33. protected function setUp()
  34. {
  35. $this->_version = 'V1';
  36. $this->_soapService = 'testModule1AllSoapAndRestV1';
  37. $this->_restResourcePath = "/{$this->_version}/testmodule1/";
  38. $this->valueFactory = Bootstrap::getObjectManager()->create(
  39. \Magento\Framework\Api\AttributeValueFactory::class
  40. );
  41. $this->itemFactory = Bootstrap::getObjectManager()->create(
  42. \Magento\TestModule1\Service\V1\Entity\ItemFactory::class
  43. );
  44. }
  45. /**
  46. * Test get item
  47. */
  48. public function testItem()
  49. {
  50. $itemId = 1;
  51. $serviceInfo = [
  52. 'rest' => [
  53. 'resourcePath' => $this->_restResourcePath . $itemId,
  54. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  55. ],
  56. 'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Item'],
  57. ];
  58. $requestData = ['itemId' => $itemId];
  59. $item = $this->_webApiCall($serviceInfo, $requestData);
  60. $this->assertEquals('testProduct1', $item['name'], 'Item was retrieved unsuccessfully');
  61. }
  62. /**
  63. * Test get item with any type
  64. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  65. */
  66. public function testItemAnyType()
  67. {
  68. $this->_markTestAsRestOnly('Test will fail for SOAP because attribute values get converted to strings.');
  69. $customerAttributes = [
  70. Item::CUSTOM_ATTRIBUTE_1 => [
  71. AttributeValue::ATTRIBUTE_CODE => Item::CUSTOM_ATTRIBUTE_1,
  72. AttributeValue::VALUE => '12345',
  73. ],
  74. Item::CUSTOM_ATTRIBUTE_2 => [
  75. AttributeValue::ATTRIBUTE_CODE => Item::CUSTOM_ATTRIBUTE_2,
  76. AttributeValue::VALUE => 12345,
  77. ],
  78. Item::CUSTOM_ATTRIBUTE_3 => [
  79. AttributeValue::ATTRIBUTE_CODE => Item::CUSTOM_ATTRIBUTE_3,
  80. AttributeValue::VALUE => true,
  81. ],
  82. ];
  83. $attributeValue1 = $this->valueFactory->create()
  84. ->setAttributeCode(Item::CUSTOM_ATTRIBUTE_1)
  85. ->setValue('12345');
  86. $attributeValue2 = $this->valueFactory->create()
  87. ->setAttributeCode(Item::CUSTOM_ATTRIBUTE_2)
  88. ->setValue(12345);
  89. $attributeValue3 = $this->valueFactory->create()
  90. ->setAttributeCode(Item::CUSTOM_ATTRIBUTE_3)
  91. ->setValue(true);
  92. $item = $this->itemFactory->create()
  93. ->setItemId(1)
  94. ->setName('testProductAnyType')
  95. ->setCustomAttributes([$attributeValue1, $attributeValue2, $attributeValue3]);
  96. $serviceInfo = [
  97. 'rest' => [
  98. 'resourcePath' => $this->_restResourcePath . 'itemAnyType',
  99. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  100. ],
  101. 'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'ItemAnyType'],
  102. ];
  103. $requestData = $item->__toArray();
  104. $item = $this->_webApiCall($serviceInfo, ['entityItem' => $requestData]);
  105. $this->assertSame(
  106. $attributeValue1->getValue(),
  107. $item['custom_attributes'][0]['value'],
  108. 'Serialized attribute value type does\'t match pre-defined type.'
  109. ); // string '12345' is expected
  110. $this->assertSame(
  111. $attributeValue2->getValue(),
  112. $item['custom_attributes'][1]['value'],
  113. 'Serialized attribute value type does\'t match pre-defined type.'
  114. ); // integer 12345 is expected
  115. $this->assertSame(
  116. $attributeValue3->getValue(),
  117. $item['custom_attributes'][2]['value'],
  118. 'Serialized attribute value type does\'t match pre-defined type.'
  119. ); // boolean true is expected
  120. }
  121. /**
  122. * Test fetching all items
  123. */
  124. public function testItems()
  125. {
  126. $itemArr = [['item_id' => 1, 'name' => 'testProduct1'], ['item_id' => 2, 'name' => 'testProduct2']];
  127. $serviceInfo = [
  128. 'rest' => [
  129. 'resourcePath' => $this->_restResourcePath,
  130. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET
  131. ],
  132. 'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Items'],
  133. ];
  134. $item = $this->_webApiCall($serviceInfo);
  135. $this->assertEquals($itemArr, $item, 'Items were not retrieved');
  136. }
  137. /**
  138. * Test create item
  139. */
  140. public function testCreate()
  141. {
  142. $createdItemName = 'createdItemName';
  143. $serviceInfo = [
  144. 'rest' => [
  145. 'resourcePath' => $this->_restResourcePath,
  146. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
  147. ],
  148. 'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Create'],
  149. ];
  150. $requestData = ['name' => $createdItemName];
  151. $item = $this->_webApiCall($serviceInfo, $requestData);
  152. $this->assertEquals($createdItemName, $item['name'], 'Item creation failed');
  153. }
  154. /**
  155. * Test create item with missing proper resources
  156. */
  157. public function testCreateWithoutResources()
  158. {
  159. $createdItemName = 'createdItemName';
  160. $serviceInfo = [
  161. 'rest' => [
  162. 'resourcePath' => $this->_restResourcePath,
  163. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
  164. ],
  165. 'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Create'],
  166. ];
  167. $requestData = ['name' => $createdItemName];
  168. // getting new credentials that do not match the api resources
  169. OauthHelper::clearApiAccessCredentials();
  170. OauthHelper::getApiAccessCredentials([]);
  171. try {
  172. $this->assertUnauthorizedException($serviceInfo, $requestData);
  173. } catch (\Exception $e) {
  174. OauthHelper::clearApiAccessCredentials();
  175. throw $e;
  176. }
  177. // to allow good credentials to be restored (this is statically stored on OauthHelper)
  178. OauthHelper::clearApiAccessCredentials();
  179. }
  180. /**
  181. * Test update item
  182. */
  183. public function testUpdate()
  184. {
  185. $itemId = 1;
  186. $serviceInfo = [
  187. 'rest' => [
  188. 'resourcePath' => $this->_restResourcePath . $itemId,
  189. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  190. ],
  191. 'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Update'],
  192. ];
  193. $requestData = ['entityItem' => ['itemId' => $itemId, 'name' => 'testName']];
  194. $item = $this->_webApiCall($serviceInfo, $requestData);
  195. $this->assertEquals('Updated' . $requestData['entityItem']['name'], $item['name'], 'Item update failed');
  196. }
  197. /**
  198. * Negative Test: Invoking non-existent delete api which is only available in V2
  199. */
  200. public function testDelete()
  201. {
  202. $itemId = 1;
  203. $serviceInfo = [
  204. 'rest' => [
  205. 'resourcePath' => $this->_restResourcePath . $itemId,
  206. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
  207. ],
  208. 'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Delete'],
  209. ];
  210. $requestData = ['itemId' => $itemId, 'name' => 'testName'];
  211. $this->_assertNoRouteOrOperationException($serviceInfo, $requestData);
  212. }
  213. public function testOverwritten()
  214. {
  215. $this->_markTestAsRestOnly();
  216. $serviceInfo = [
  217. 'rest' => [
  218. 'resourcePath' => $this->_restResourcePath . 'overwritten',
  219. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  220. ],
  221. ];
  222. $item = $this->_webApiCall($serviceInfo, []);
  223. $this->assertEquals(['item_id' => -55, 'name' => 'testProduct1'], $item);
  224. }
  225. public function testDefaulted()
  226. {
  227. $this->_markTestAsRestOnly();
  228. $serviceInfo = [
  229. 'rest' => [
  230. 'resourcePath' => $this->_restResourcePath . 'testOptionalParam',
  231. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  232. ],
  233. ];
  234. $item = $this->_webApiCall($serviceInfo, []);
  235. $this->assertEquals(['item_id' => 3, 'name' => 'Default Name'], $item);
  236. }
  237. public function testDefaultedWithValue()
  238. {
  239. $this->_markTestAsRestOnly();
  240. $serviceInfo = [
  241. 'rest' => [
  242. 'resourcePath' => $this->_restResourcePath . 'testOptionalParam',
  243. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  244. ],
  245. ];
  246. $item = $this->_webApiCall($serviceInfo, ['name' => 'Ms. LaGrange']);
  247. $this->assertEquals(['item_id' => 3, 'name' => 'Ms. LaGrange'], $item);
  248. }
  249. }