AddressMetadataTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Api;
  7. use Magento\Customer\Api\Data\AddressInterface as Address;
  8. use Magento\Customer\Model\Data\AttributeMetadata;
  9. use Magento\TestFramework\TestCase\WebapiAbstract;
  10. /**
  11. * Class AddressMetadataTest
  12. */
  13. class AddressMetadataTest extends WebapiAbstract
  14. {
  15. const SERVICE_NAME = "customerAddressMetadataV1";
  16. const SERVICE_VERSION = "V1";
  17. const RESOURCE_PATH = "/V1/attributeMetadata/customerAddress";
  18. /**
  19. * Test retrieval of attribute metadata for the address entity type.
  20. *
  21. * @param string $attributeCode The attribute code of the requested metadata.
  22. * @param array $expectedMetadata Expected entity metadata for the attribute code.
  23. * @dataProvider getAttributeMetadataDataProvider
  24. */
  25. public function testGetAttributeMetadata($attributeCode, $expectedMetadata)
  26. {
  27. $serviceInfo = [
  28. 'rest' => [
  29. 'resourcePath' => self::RESOURCE_PATH . "/attribute/$attributeCode",
  30. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  31. ],
  32. 'soap' => [
  33. 'service' => self::SERVICE_NAME,
  34. 'serviceVersion' => self::SERVICE_VERSION,
  35. 'operation' => self::SERVICE_NAME . 'GetAttributeMetadata',
  36. ],
  37. ];
  38. $requestData = [
  39. 'attributeCode' => $attributeCode,
  40. ];
  41. $attributeMetadata = $this->_webapiCall($serviceInfo, $requestData);
  42. $validationResult = $this->checkValidationRules($expectedMetadata, $attributeMetadata);
  43. list($expectedMetadata, $attributeMetadata) = $validationResult;
  44. $this->assertEquals($expectedMetadata, $attributeMetadata);
  45. }
  46. /**
  47. * Data provider for testGetAttributeMetadata.
  48. *
  49. * @return array
  50. */
  51. public function getAttributeMetadataDataProvider()
  52. {
  53. return [
  54. Address::POSTCODE => [
  55. Address::POSTCODE,
  56. [
  57. AttributeMetadata::FRONTEND_INPUT => 'text',
  58. AttributeMetadata::INPUT_FILTER => '',
  59. AttributeMetadata::STORE_LABEL => 'Zip/Postal Code',
  60. AttributeMetadata::MULTILINE_COUNT => 0,
  61. AttributeMetadata::VALIDATION_RULES => [],
  62. AttributeMetadata::VISIBLE => true,
  63. AttributeMetadata::REQUIRED => false,
  64. AttributeMetadata::DATA_MODEL => \Magento\Customer\Model\Attribute\Data\Postcode::class,
  65. AttributeMetadata::OPTIONS => [],
  66. AttributeMetadata::FRONTEND_CLASS => '',
  67. AttributeMetadata::USER_DEFINED => false,
  68. AttributeMetadata::SORT_ORDER => 110,
  69. AttributeMetadata::FRONTEND_LABEL => 'Zip/Postal Code',
  70. AttributeMetadata::NOTE => '',
  71. AttributeMetadata::SYSTEM => true,
  72. AttributeMetadata::BACKEND_TYPE => 'static',
  73. AttributeMetadata::IS_USED_IN_GRID => true,
  74. AttributeMetadata::IS_VISIBLE_IN_GRID => true,
  75. AttributeMetadata::IS_FILTERABLE_IN_GRID => true,
  76. AttributeMetadata::IS_SEARCHABLE_IN_GRID => true,
  77. AttributeMetadata::ATTRIBUTE_CODE => 'postcode',
  78. ],
  79. ]
  80. ];
  81. }
  82. /**
  83. * Test retrieval of all address attribute metadata.
  84. */
  85. public function testGetAllAttributesMetadata()
  86. {
  87. $serviceInfo = [
  88. 'rest' => [
  89. 'resourcePath' => self::RESOURCE_PATH,
  90. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  91. ],
  92. 'soap' => [
  93. 'service' => self::SERVICE_NAME,
  94. 'serviceVersion' => self::SERVICE_VERSION,
  95. 'operation' => self::SERVICE_NAME . 'GetAllAttributesMetadata',
  96. ],
  97. ];
  98. $attributeMetadata = $this->_webApiCall($serviceInfo);
  99. $this->assertCount(19, $attributeMetadata);
  100. $postcode = $this->getAttributeMetadataDataProvider()[Address::POSTCODE][1];
  101. $validationResult = $this->checkMultipleAttributesValidationRules($postcode, $attributeMetadata);
  102. list($postcode, $attributeMetadata) = $validationResult;
  103. $this->assertContains($postcode, $attributeMetadata);
  104. }
  105. /**
  106. * Test retrieval of custom address attribute metadata.
  107. *
  108. * @magentoApiDataFixture Magento/Customer/_files/attribute_user_defined_address_custom_attribute.php
  109. */
  110. public function testGetCustomAttributesMetadata()
  111. {
  112. $customAttributeCode = 'custom_attribute1';
  113. $serviceInfo = [
  114. 'rest' => [
  115. 'resourcePath' => self::RESOURCE_PATH . '/custom',
  116. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  117. ],
  118. 'soap' => [
  119. 'service' => self::SERVICE_NAME,
  120. 'serviceVersion' => self::SERVICE_VERSION,
  121. 'operation' => self::SERVICE_NAME . 'GetCustomAttributesMetadata',
  122. ],
  123. ];
  124. $requestData = ['attribute_code' => $customAttributeCode];
  125. $attributeMetadata = $this->_webApiCall($serviceInfo, $requestData);
  126. $this->assertCount(2, $attributeMetadata);
  127. $this->assertEquals($customAttributeCode, $attributeMetadata[0]['attribute_code']);
  128. }
  129. /**
  130. * Test retrieval of attributes
  131. *
  132. * @param string $formCode Form code
  133. * @param array $expectedMetadata The expected attribute metadata
  134. * @dataProvider getAttributesDataProvider
  135. */
  136. public function testGetAttributes($formCode, $expectedMetadata)
  137. {
  138. $serviceInfo = [
  139. 'rest' => [
  140. 'resourcePath' => self::RESOURCE_PATH . "/form/$formCode",
  141. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  142. ],
  143. 'soap' => [
  144. 'service' => self::SERVICE_NAME,
  145. 'serviceVersion' => self::SERVICE_VERSION,
  146. 'operation' => self::SERVICE_NAME . 'GetAttributes',
  147. ],
  148. ];
  149. $requestData = [
  150. 'formCode' => $formCode,
  151. ];
  152. $attributeMetadataList = $this->_webApiCall($serviceInfo, $requestData);
  153. foreach ($attributeMetadataList as $attributeMetadata) {
  154. if (isset($attributeMetadata['attribute_code'])
  155. && $attributeMetadata['attribute_code'] == $expectedMetadata['attribute_code']
  156. ) {
  157. $validationResult = $this->checkValidationRules($expectedMetadata, $attributeMetadata);
  158. list($expectedMetadata, $attributeMetadata) = $validationResult;
  159. $this->assertEquals($expectedMetadata, $attributeMetadata);
  160. break;
  161. }
  162. }
  163. }
  164. /**
  165. * Data provider for testGetAttributes.
  166. *
  167. * @return array
  168. */
  169. public function getAttributesDataProvider()
  170. {
  171. $attributeMetadata = $this->getAttributeMetadataDataProvider();
  172. return [
  173. [
  174. 'customer_address_edit',
  175. $attributeMetadata[Address::POSTCODE][1],
  176. ]
  177. ];
  178. }
  179. /**
  180. * Checks that expected and actual attribute metadata validation rules are equal
  181. * and removes the validation rules entry from expected and actual attribute metadata
  182. *
  183. * @param array $expectedResult
  184. * @param array $actualResult
  185. * @return array
  186. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  187. */
  188. public function checkValidationRules($expectedResult, $actualResult)
  189. {
  190. $expectedRules = [];
  191. $actualRules = [];
  192. if (isset($expectedResult[AttributeMetadata::VALIDATION_RULES])) {
  193. $expectedRules = $expectedResult[AttributeMetadata::VALIDATION_RULES];
  194. unset($expectedResult[AttributeMetadata::VALIDATION_RULES]);
  195. }
  196. if (isset($actualResult[AttributeMetadata::VALIDATION_RULES])) {
  197. $actualRules = $actualResult[AttributeMetadata::VALIDATION_RULES];
  198. unset($actualResult[AttributeMetadata::VALIDATION_RULES]);
  199. }
  200. if (is_array($expectedRules) && is_array($actualRules)) {
  201. foreach ($expectedRules as $expectedRule) {
  202. if (isset($expectedRule['name']) && isset($expectedRule['value'])) {
  203. $found = false;
  204. foreach ($actualRules as $actualRule) {
  205. if (isset($actualRule['name']) && isset($actualRule['value'])) {
  206. if ($expectedRule['name'] == $actualRule['name']
  207. && $expectedRule['value'] == $actualRule['value']
  208. ) {
  209. $found = true;
  210. break;
  211. }
  212. }
  213. }
  214. $this->assertTrue($found);
  215. }
  216. }
  217. }
  218. return [$expectedResult, $actualResult];
  219. }
  220. /**
  221. * Check specific attribute validation rules in set of multiple attributes
  222. *
  223. * @param array $expectedResult Set of expected attribute metadata
  224. * @param array $actualResultSet Set of actual attribute metadata
  225. * @return array
  226. */
  227. public function checkMultipleAttributesValidationRules($expectedResult, $actualResultSet)
  228. {
  229. if (is_array($expectedResult) && is_array($actualResultSet)) {
  230. if (isset($expectedResult[AttributeMetadata::ATTRIBUTE_CODE])) {
  231. foreach ($actualResultSet as $actualAttributeKey => $actualAttribute) {
  232. if (isset($actualAttribute[AttributeMetadata::ATTRIBUTE_CODE])
  233. && $expectedResult[AttributeMetadata::ATTRIBUTE_CODE]
  234. == $actualAttribute[AttributeMetadata::ATTRIBUTE_CODE]
  235. ) {
  236. $this->checkValidationRules($expectedResult, $actualAttribute);
  237. unset($actualResultSet[$actualAttributeKey][AttributeMetadata::VALIDATION_RULES]);
  238. }
  239. }
  240. unset($expectedResult[AttributeMetadata::VALIDATION_RULES]);
  241. }
  242. }
  243. return [$expectedResult, $actualResultSet];
  244. }
  245. /**
  246. * Remove test attribute
  247. */
  248. public static function tearDownAfterClass()
  249. {
  250. parent::tearDownAfterClass();
  251. /** @var \Magento\Customer\Model\Attribute $attribute */
  252. $attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  253. \Magento\Customer\Model\Attribute::class
  254. );
  255. foreach (['custom_attribute1', 'custom_attribute2'] as $attributeCode) {
  256. $attribute->loadByCode('customer_address', $attributeCode);
  257. $attribute->delete();
  258. }
  259. }
  260. }