AddressMetadataTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. use Magento\Customer\Api\AddressMetadataInterface;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\TestFramework\Helper\CacheCleaner;
  10. class AddressMetadataTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /** @var AddressMetadataInterface */
  13. private $service;
  14. /** @var AddressMetadataInterface */
  15. private $serviceTwo;
  16. protected function setUp()
  17. {
  18. CacheCleaner::cleanAll();
  19. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  20. $objectManager->configure(
  21. [
  22. \Magento\Framework\Api\ExtensionAttribute\Config\Reader::class => [
  23. 'arguments' => [
  24. 'fileResolver' => ['instance' => \Magento\Customer\Model\FileResolverStub::class],
  25. ],
  26. ],
  27. ]
  28. );
  29. $this->service = $objectManager->create(\Magento\Customer\Api\AddressMetadataInterface::class);
  30. $this->serviceTwo = $objectManager->create(\Magento\Customer\Api\AddressMetadataInterface::class);
  31. }
  32. public function testGetCustomAttributesMetadata()
  33. {
  34. $customAttributesMetadata = $this->service->getCustomAttributesMetadata();
  35. $this->assertCount(0, $customAttributesMetadata, "Invalid number of attributes returned.");
  36. // Verify the consistency of getCustomAttributeMetadata() function from the 2nd call of the same service
  37. $customAttributesMetadata2 = $this->service->getCustomAttributesMetadata();
  38. $this->assertCount(0, $customAttributesMetadata2, "Invalid number of attributes returned.");
  39. // Verify the consistency of getCustomAttributesMetadata() function from the 2nd service
  40. $customAttributesMetadata3 = $this->serviceTwo->getCustomAttributesMetadata();
  41. $this->assertCount(0, $customAttributesMetadata3, "Invalid number of attributes returned.");
  42. }
  43. /**
  44. * @magentoDataFixture Magento/Customer/_files/attribute_user_defined_address_custom_attribute.php
  45. */
  46. public function testGetCustomAttributesMetadataWithCustomAttribute()
  47. {
  48. $customAttributesMetadata = $this->service->getCustomAttributesMetadata();
  49. // Verify the consistency of getCustomAttributeMetadata() function from the 2nd call of the same service
  50. $customAttributesMetadata1 = $this->service->getCustomAttributesMetadata();
  51. $this->assertEquals(
  52. $customAttributesMetadata,
  53. $customAttributesMetadata1,
  54. 'Different custom attribute metadata returned from the 2nd call of the same service'
  55. );
  56. // Verify the consistency of getCustomAttributesMetadata() function from the 2nd service
  57. $customAttributesMetadata2 = $this->serviceTwo->getCustomAttributesMetadata();
  58. $this->assertEquals(
  59. $customAttributesMetadata,
  60. $customAttributesMetadata2,
  61. 'Different custom attribute metadata returned from the 2nd service'
  62. );
  63. $customAttributeCodeOne = 'custom_attribute1';
  64. $customAttributeFound = false;
  65. $customAttributeCodeTwo = 'custom_attribute2';
  66. $customAttributesFound = false;
  67. foreach ($customAttributesMetadata as $attribute) {
  68. if ($attribute->getAttributeCode() == $customAttributeCodeOne) {
  69. $customAttributeFound = true;
  70. }
  71. if ($attribute->getAttributeCode() == $customAttributeCodeTwo) {
  72. $customAttributesFound = true;
  73. }
  74. }
  75. if (!$customAttributeFound) {
  76. $this->fail("Custom attribute declared in the config not found.");
  77. }
  78. if (!$customAttributesFound) {
  79. $this->fail("Custom attributes declared in the config not found.");
  80. }
  81. $this->assertCount(2, $customAttributesMetadata, "Invalid number of attributes returned.");
  82. // Verify the consistency of the custom attribute metadata from two calls of the same service
  83. // after getAttributeCode was called
  84. foreach ($customAttributesMetadata1 as $attribute1) {
  85. $attribute1->getAttributeCode();
  86. }
  87. $this->assertEquals(
  88. $customAttributesMetadata,
  89. $customAttributesMetadata1,
  90. 'Custom attribute metadata from the same service became different after getAttributeCode was called'
  91. );
  92. // Verify the consistency of the custom attribute metadata from two services
  93. // after getAttributeCode was called
  94. foreach ($customAttributesMetadata2 as $attribute2) {
  95. $attribute2->getAttributeCode();
  96. }
  97. $this->assertEquals(
  98. $customAttributesMetadata,
  99. $customAttributesMetadata2,
  100. 'Custom attribute metadata from two services are different after getAttributeCode was called'
  101. );
  102. }
  103. /**
  104. * @magentoDataFixture Magento/Customer/_files/attribute_user_defined_address_custom_attribute.php
  105. */
  106. public function testGetAllAttributesMetadataWithCustomAttribute()
  107. {
  108. $allAttributesMetadata = $this->service->getAllAttributesMetadata();
  109. // Verify the consistency of getAllAttributesMetadata() function from the 2nd call of the same service
  110. $allAttributesMetadata2 = $this->service->getAllAttributesMetadata();
  111. $this->assertEquals(
  112. $allAttributesMetadata,
  113. $allAttributesMetadata2,
  114. 'Different attribute metadata returned from the 2nd call of the same service'
  115. );
  116. // Verify the consistency of getAllAttributesMetadata() function from the 2nd service
  117. $allAttributesMetadata3 = $this->serviceTwo->getAllAttributesMetadata();
  118. $this->assertEquals(
  119. $allAttributesMetadata,
  120. $allAttributesMetadata3,
  121. 'Different attribute metadata returned from the 2nd service'
  122. );
  123. }
  124. public function testGetAddressAttributeMetadata()
  125. {
  126. $vatValidMetadata = $this->service->getAttributeMetadata('vat_is_valid');
  127. $this->assertNotNull($vatValidMetadata);
  128. $this->assertEquals('vat_is_valid', $vatValidMetadata->getAttributeCode());
  129. $this->assertEquals('text', $vatValidMetadata->getFrontendInput());
  130. $this->assertEquals('VAT number validity', $vatValidMetadata->getStoreLabel());
  131. // Verify the consistency of attribute metadata from two calls of the same service
  132. $vatValidMetadata2 = $this->service->getAttributeMetadata('vat_is_valid');
  133. $this->assertEquals(
  134. $vatValidMetadata,
  135. $vatValidMetadata2,
  136. 'Different attribute metadata returned from the 2nd call of the same service'
  137. );
  138. // Verify the consistency of attribute metadata from two services
  139. $vatValidMetadata3 = $this->serviceTwo->getAttributeMetadata('vat_is_valid');
  140. $this->assertEquals('vat_is_valid', $vatValidMetadata3->getAttributeCode());
  141. $this->assertEquals(
  142. $vatValidMetadata,
  143. $vatValidMetadata3,
  144. 'Different attribute metadata returned from the 2nd service'
  145. );
  146. }
  147. public function testGetAddressAttributeMetadataNoSuchEntity()
  148. {
  149. try {
  150. $this->service->getAttributeMetadata('1');
  151. $this->fail('Expected exception not thrown.');
  152. } catch (NoSuchEntityException $e) {
  153. $this->assertEquals(
  154. 'No such entity with entityType = customer_address, attributeCode = 1',
  155. $e->getMessage()
  156. );
  157. }
  158. // Verify the consistency of getAttributeMetadata() function from the 2nd call of the same service
  159. try {
  160. $this->service->getAttributeMetadata('1');
  161. $this->fail('Expected exception not thrown when called the 2nd time.');
  162. } catch (NoSuchEntityException $e) {
  163. $this->assertEquals(
  164. 'No such entity with entityType = customer_address, attributeCode = 1',
  165. $e->getMessage()
  166. );
  167. }
  168. // Verify the consistency of getAttributeMetadata() function from the 2nd service
  169. try {
  170. $this->serviceTwo->getAttributeMetadata('1');
  171. $this->fail('Expected exception not thrown when called with the 2nd service.');
  172. } catch (NoSuchEntityException $e) {
  173. $this->assertEquals(
  174. 'No such entity with entityType = customer_address, attributeCode = 1',
  175. $e->getMessage()
  176. );
  177. }
  178. }
  179. public function testGetAttributes()
  180. {
  181. /** @var \Magento\Customer\Api\Data\ValidationRuleInterfaceFactory $validationRulesFactory */
  182. $validationRulesFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  183. \Magento\Customer\Api\Data\ValidationRuleInterfaceFactory::class
  184. );
  185. $expectedValidationRules = [
  186. $validationRulesFactory->create(['data' => ['name' => 'max_text_length', 'value' => 255]]),
  187. $validationRulesFactory->create(['data' => ['name' => 'min_text_length', 'value' => 1]]),
  188. ];
  189. // Verify the consistency of getAttributes() function from the 2nd call of the same service
  190. $formAttributesMetadata = $this->service->getAttributes('customer_address_edit');
  191. $this->assertCount(15, $formAttributesMetadata, "Invalid number of attributes for the specified form.");
  192. $formAttributesMetadata1 = $this->service->getAttributes('customer_address_edit');
  193. $this->assertEquals(
  194. $formAttributesMetadata,
  195. $formAttributesMetadata1,
  196. 'Different form attribute metadata returned from the 2nd call of the same service'
  197. );
  198. // Verify the consistency of getAttributes() function from the 2nd service
  199. $formAttributesMetadata2 = $this->serviceTwo->getAttributes('customer_address_edit');
  200. $this->assertEquals(
  201. $formAttributesMetadata,
  202. $formAttributesMetadata2,
  203. 'Different form attribute metadata returned from the 2nd service'
  204. );
  205. /** Check some fields of one attribute metadata */
  206. $attributeMetadata = $formAttributesMetadata['company'];
  207. $this->assertInstanceOf(\Magento\Customer\Model\Data\AttributeMetadata::class, $attributeMetadata);
  208. $this->assertEquals('company', $attributeMetadata->getAttributeCode(), 'Attribute code is invalid');
  209. $validationRules = $attributeMetadata->getValidationRules();
  210. $this->assertEquals($expectedValidationRules, $validationRules);
  211. $this->assertEquals('static', $attributeMetadata->getBackendType(), 'Backend type is invalid');
  212. $this->assertEquals('Company', $attributeMetadata->getFrontendLabel(), 'Frontend label is invalid');
  213. $vatIdAttributeMetadata = $formAttributesMetadata['vat_id'];
  214. $this->assertEquals([], $vatIdAttributeMetadata->getOptions());
  215. $this->assertEquals([], $vatIdAttributeMetadata->getValidationRules());
  216. // Verify the consistency of form attribute metadata from two calls of the same service
  217. // after some getters were called
  218. $attributeMetadata1 = $formAttributesMetadata1['company'];
  219. $this->assertEquals('company', $attributeMetadata1->getAttributeCode(), 'Attribute code is invalid');
  220. $this->assertEquals($expectedValidationRules, $attributeMetadata1->getValidationRules());
  221. $vatIdAttributeMetadata1 = $formAttributesMetadata1['vat_id'];
  222. $this->assertEquals([], $vatIdAttributeMetadata1->getOptions());
  223. $this->assertEquals([], $vatIdAttributeMetadata1->getValidationRules());
  224. $this->assertEquals(
  225. $formAttributesMetadata,
  226. $formAttributesMetadata1,
  227. 'Form attribute metadata from the same service became different after some getters were called'
  228. );
  229. // Verify the consistency of form attribute metadata from two services
  230. // after some getters were called
  231. $attributeMetadata2 = $formAttributesMetadata2['company'];
  232. $this->assertEquals('company', $attributeMetadata2->getAttributeCode(), 'Attribute code is invalid');
  233. $this->assertEquals($expectedValidationRules, $attributeMetadata2->getValidationRules());
  234. $vatIdAttributeMetadata2 = $formAttributesMetadata2['vat_id'];
  235. $this->assertEquals([], $vatIdAttributeMetadata2->getOptions());
  236. $this->assertEquals([], $vatIdAttributeMetadata2->getValidationRules());
  237. $this->assertEquals(
  238. $formAttributesMetadata,
  239. $formAttributesMetadata2,
  240. 'Form attribute metadata from two services are different after some getters were called'
  241. );
  242. }
  243. protected function tearDown()
  244. {
  245. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  246. /* @var \Magento\Framework\Config\CacheInterface $cache */
  247. $cache = $objectManager->create(\Magento\Framework\Config\CacheInterface::class);
  248. $cache->remove('extension_attributes_config');
  249. }
  250. }