CustomerMetadataTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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\CustomerMetadataInterface;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\TestFramework\Helper\CacheCleaner;
  11. class CustomerMetadataTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var CustomerRepositoryInterface */
  14. private $customerRepository;
  15. /** @var CustomerMetadataInterface */
  16. private $service;
  17. /** @var CustomerMetadataInterface */
  18. private $serviceTwo;
  19. /**
  20. * @var \Magento\Framework\Api\ExtensibleDataObjectConverter
  21. */
  22. private $extensibleDataObjectConverter;
  23. protected function setUp()
  24. {
  25. CacheCleaner::cleanAll();
  26. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  27. $objectManager->configure(
  28. [\Magento\Framework\Api\ExtensionAttribute\Config\Reader::class => [
  29. 'arguments' => [
  30. 'fileResolver' => ['instance' => \Magento\Customer\Model\FileResolverStub::class],
  31. ],
  32. ],
  33. ]
  34. );
  35. $this->customerRepository = $objectManager->create(
  36. \Magento\Customer\Api\CustomerRepositoryInterface::class
  37. );
  38. $this->service = $objectManager->create(\Magento\Customer\Api\CustomerMetadataInterface::class);
  39. $this->serviceTwo = $objectManager->create(\Magento\Customer\Api\CustomerMetadataInterface::class);
  40. $this->extensibleDataObjectConverter = $objectManager->get(
  41. \Magento\Framework\Api\ExtensibleDataObjectConverter::class
  42. );
  43. }
  44. public function testGetCustomAttributesMetadata()
  45. {
  46. $customAttributesMetadataQty = count($this->service->getCustomAttributesMetadata()) ;
  47. // Verify the consistency of getCustomerAttributeMetadata() function from the 2nd call of the same service
  48. $customAttributesMetadata1Qty = count($this->service->getCustomAttributesMetadata());
  49. $this->assertEquals(
  50. $customAttributesMetadataQty,
  51. $customAttributesMetadata1Qty,
  52. "Invalid number of attributes returned."
  53. );
  54. // Verify the consistency of getCustomAttributesMetadata() function from the 2nd service
  55. $customAttributesMetadata2Qty = count($this->serviceTwo->getCustomAttributesMetadata());
  56. $this->assertEquals(
  57. $customAttributesMetadataQty,
  58. $customAttributesMetadata2Qty,
  59. "Invalid number of attributes returned."
  60. );
  61. }
  62. public function testGetNestedOptionsCustomerAttributesMetadata()
  63. {
  64. $nestedOptionsAttribute = 'store_id';
  65. $customAttributesMetadata = $this->service->getAttributeMetadata($nestedOptionsAttribute);
  66. // Verify the consistency of getAttributeMetadata() function from the 2nd call of the same service
  67. $customAttributesMetadata1 = $this->service->getAttributeMetadata($nestedOptionsAttribute);
  68. $this->assertEquals(
  69. $customAttributesMetadata,
  70. $customAttributesMetadata1,
  71. 'Different attribute metadata returned from the 2nd call of the same service'
  72. );
  73. // Verify the consistency of getAttributeMetadata() function from the 2nd service
  74. $customAttributesMetadata2 = $this->serviceTwo->getAttributeMetadata($nestedOptionsAttribute);
  75. $this->assertEquals(
  76. $customAttributesMetadata,
  77. $customAttributesMetadata2,
  78. 'Different attribute metadata returned from the 2nd service'
  79. );
  80. $options = $customAttributesMetadata->getOptions();
  81. $nestedOptionExists = false;
  82. foreach ($options as $option) {
  83. if (strpos($option->getLabel(), 'Main Website Store') !== false) {
  84. $this->assertNotEmpty($option->getOptions());
  85. //Check nested option
  86. $this->assertTrue(strpos($option->getOptions()[0]->getLabel(), 'Default Store View') !== false);
  87. $nestedOptionExists = true;
  88. }
  89. }
  90. if (!$nestedOptionExists) {
  91. $this->fail('Nested attribute options were expected.');
  92. }
  93. // Verify the consistency of attribute metadata from two calls of the same service
  94. // after getOptions was called
  95. $customAttributesMetadata1->getOptions();
  96. $this->assertEquals(
  97. $customAttributesMetadata,
  98. $customAttributesMetadata1,
  99. 'Attribute metadata from the same service became different after getOptions was called'
  100. );
  101. // Verify the consistency of attribute metadata from two services
  102. // after getOptions was called
  103. $customAttributesMetadata2->getOptions();
  104. $this->assertEquals(
  105. $customAttributesMetadata,
  106. $customAttributesMetadata2,
  107. 'Attribute metadata from two services are different after getOptions was called'
  108. );
  109. }
  110. /**
  111. * @magentoDataFixture Magento/Customer/_files/attribute_user_defined_custom_attribute.php
  112. */
  113. public function testGetCustomAttributesMetadataWithCustomAttributes()
  114. {
  115. $customAttributesMetadata = $this->service->getCustomAttributesMetadata();
  116. // Verify the consistency of getCustomAttributesMetadata() function from the 2nd call of the same service
  117. $customAttributesMetadata1 = $this->service->getCustomAttributesMetadata();
  118. $this->assertEquals(
  119. $customAttributesMetadata,
  120. $customAttributesMetadata1,
  121. 'Different custom attribute metadata returned from the 2nd call of the same service'
  122. );
  123. // Verify the consistency of getCustomAttributesMetadata() function from the 2nd service
  124. $customAttributesMetadata2 = $this->serviceTwo->getCustomAttributesMetadata();
  125. $this->assertEquals(
  126. $customAttributesMetadata,
  127. $customAttributesMetadata2,
  128. 'Different custom attribute metadata returned from the 2nd service'
  129. );
  130. $expectedCustomAttributeCodeArray = ['custom_attribute1', 'custom_attribute2', 'customer_image'];
  131. $actual = [];
  132. foreach ($customAttributesMetadata as $attribute) {
  133. $actual[] = $attribute->getAttributeCode();
  134. }
  135. $this->assertEquals(
  136. $expectedCustomAttributeCodeArray,
  137. array_intersect($expectedCustomAttributeCodeArray, $actual),
  138. "Expected attributes not returned from the service."
  139. );
  140. // Verify the consistency of custom attribute metadata from two calls of the same service
  141. // after getAttributeCode was called
  142. foreach ($customAttributesMetadata1 as $attribute) {
  143. $attribute->getAttributeCode();
  144. }
  145. $this->assertEquals(
  146. $customAttributesMetadata,
  147. $customAttributesMetadata1,
  148. 'Custom attribute metadata from the same service became different after getAttributeCode was called'
  149. );
  150. // Verify the consistency of custom attribute metadata from two services
  151. // after getAttributeCode was called
  152. foreach ($customAttributesMetadata2 as $attribute) {
  153. $attribute->getAttributeCode();
  154. }
  155. $this->assertEquals(
  156. $customAttributesMetadata,
  157. $customAttributesMetadata2,
  158. 'Custom attribute metadata from two services are different after getAttributeCode was called'
  159. );
  160. }
  161. /**
  162. * @magentoDataFixture Magento/Customer/_files/attribute_user_defined_custom_attribute.php
  163. */
  164. public function testGetAllAttributesMetadataWithCustomAttribute()
  165. {
  166. $allAttributesMetadata = $this->service->getAllAttributesMetadata();
  167. // Verify the consistency of getAllAttributesMetadata() function from the 2nd call of the same service
  168. $allAttributesMetadata2 = $this->service->getAllAttributesMetadata();
  169. $this->assertEquals(
  170. $allAttributesMetadata,
  171. $allAttributesMetadata2,
  172. 'Different attribute metadata returned from the 2nd call of the same service'
  173. );
  174. // Verify the consistency of getAllAttributesMetadata() function from the 2nd service
  175. $allAttributesMetadata3 = $this->serviceTwo->getAllAttributesMetadata();
  176. $this->assertEquals(
  177. $allAttributesMetadata,
  178. $allAttributesMetadata3,
  179. 'Different attribute metadata returned from the 2nd service'
  180. );
  181. }
  182. /**
  183. * @magentoDataFixture Magento/Customer/_files/customer.php
  184. */
  185. public function testGetCustomerAttributeMetadata()
  186. {
  187. // Expect these attributes to exist but do not check the value
  188. $expectAttrsWOutVals = ['created_at', 'updated_at'];
  189. // Expect these attributes to exist and check the value - values come from _files/customer.php
  190. $expectAttrsWithVals = [
  191. 'id' => 1,
  192. 'website_id' => 1,
  193. 'store_id' => 1,
  194. 'group_id' => 1,
  195. 'prefix' => 'Mr.',
  196. 'firstname' => 'John',
  197. 'middlename' => 'A',
  198. 'lastname' => 'Smith',
  199. 'suffix' => 'Esq.',
  200. 'email' => 'customer@example.com',
  201. 'default_billing' => '1',
  202. 'default_shipping' => '1',
  203. 'disable_auto_group_change' => 0,
  204. 'taxvat' => '12',
  205. 'gender' => 0
  206. ];
  207. $customer = $this->customerRepository->getById(1);
  208. $this->assertNotNull($customer);
  209. $attributes = $this->extensibleDataObjectConverter->toFlatArray(
  210. $customer,
  211. [],
  212. \Magento\Customer\Api\Data\CustomerInterface::class
  213. );
  214. $this->assertNotEmpty($attributes);
  215. // remove odd extension attributes
  216. $allAttributes = $expectAttrsWithVals;
  217. $allAttributes['created_at'] = $attributes['created_at'];
  218. $allAttributes['updated_at'] = $attributes['updated_at'];
  219. $attributes = array_intersect_key($attributes, $allAttributes);
  220. foreach ($attributes as $attributeCode => $attributeValue) {
  221. $this->assertNotNull($attributeCode);
  222. $this->assertNotNull($attributeValue);
  223. $attributeMetadata = $this->service->getAttributeMetadata($attributeCode);
  224. // Verify the consistency of getAttributeMetadata() function from the 2nd call of the same service
  225. $attributeMetadata1 = $this->service->getAttributeMetadata($attributeCode);
  226. $this->assertEquals(
  227. $attributeMetadata,
  228. $attributeMetadata1,
  229. 'Different attribute metadata returned from the 2nd call of the same service'
  230. );
  231. // Verify the consistency of getAttributeMetadata() function from the 2nd service
  232. $attributeMetadata2 = $this->serviceTwo->getAttributeMetadata($attributeCode);
  233. $this->assertEquals(
  234. $attributeMetadata,
  235. $attributeMetadata2,
  236. 'Different attribute metadata returned from the 2nd service'
  237. );
  238. $attrMetadataCode = $attributeMetadata->getAttributeCode();
  239. // Verify the consistency of attribute metadata from two calls of the same service
  240. // after getAttributeCode was called
  241. $attributeMetadata1->getAttributeCode();
  242. $this->assertEquals(
  243. $attributeMetadata,
  244. $attributeMetadata1,
  245. 'Attribute metadata from the same service became different after getAttributeCode was called'
  246. );
  247. // Verify the consistency of attribute metadata from two services
  248. // after getAttributeCode was called
  249. $attributeMetadata2->getAttributeCode();
  250. $this->assertEquals(
  251. $attributeMetadata,
  252. $attributeMetadata2,
  253. 'Attribute metadata returned from the 2nd service became different after getAttributeCode was called'
  254. );
  255. $this->assertSame($attributeCode, $attrMetadataCode);
  256. if (($key = array_search($attrMetadataCode, $expectAttrsWOutVals)) !== false) {
  257. unset($expectAttrsWOutVals[$key]);
  258. } else {
  259. $this->assertArrayHasKey($attrMetadataCode, $expectAttrsWithVals);
  260. $this->assertSame(
  261. $expectAttrsWithVals[$attrMetadataCode],
  262. $attributeValue,
  263. "Failed for {$attrMetadataCode}"
  264. );
  265. unset($expectAttrsWithVals[$attrMetadataCode]);
  266. }
  267. }
  268. $this->assertEmpty($expectAttrsWOutVals);
  269. $this->assertEmpty($expectAttrsWithVals);
  270. }
  271. public function testGetCustomerAttributeMetadataNoSuchEntity()
  272. {
  273. try {
  274. $this->service->getAttributeMetadata('wrong_attribute_code');
  275. $this->fail('Expected exception not thrown.');
  276. } catch (NoSuchEntityException $e) {
  277. $this->assertEquals(
  278. 'No such entity with entityType = customer, attributeCode = wrong_attribute_code',
  279. $e->getMessage()
  280. );
  281. }
  282. // Verify the consistency of getAttributeMetadata() function from the 2nd call of the same service
  283. try {
  284. $this->service->getAttributeMetadata('wrong_attribute_code');
  285. $this->fail('Expected exception not thrown when called the 2nd time.');
  286. } catch (NoSuchEntityException $e) {
  287. $this->assertEquals(
  288. 'No such entity with entityType = customer, attributeCode = wrong_attribute_code',
  289. $e->getMessage()
  290. );
  291. }
  292. // Verify the consistency of getAttributeMetadata() function from the 2nd service
  293. try {
  294. $this->serviceTwo->getAttributeMetadata('wrong_attribute_code');
  295. $this->fail('Expected exception not thrown when called with the 2nd service.');
  296. } catch (NoSuchEntityException $e) {
  297. $this->assertEquals(
  298. 'No such entity with entityType = customer, attributeCode = wrong_attribute_code',
  299. $e->getMessage()
  300. );
  301. }
  302. }
  303. public function testGetAttributes()
  304. {
  305. $formAttributesMetadata = $this->service->getAttributes('adminhtml_customer');
  306. $this->assertCount(14, $formAttributesMetadata, "Invalid number of attributes for the specified form.");
  307. // Verify the consistency of getAttributes() function from the 2nd call of the same service
  308. $formAttributesMetadata1 = $this->service->getAttributes('adminhtml_customer');
  309. $this->assertEquals(
  310. $formAttributesMetadata,
  311. $formAttributesMetadata1,
  312. 'Different form attribute metadata returned from the 2nd call of the same service'
  313. );
  314. // Verify the consistency of getAttributes() function from the 2nd service
  315. $formAttributesMetadata2 = $this->serviceTwo->getAttributes('adminhtml_customer');
  316. $this->assertEquals(
  317. $formAttributesMetadata,
  318. $formAttributesMetadata2,
  319. 'Different form attribute metadata returned from the 2nd service'
  320. );
  321. /** Check some fields of one attribute metadata */
  322. $attributeMetadata = $formAttributesMetadata['firstname'];
  323. $this->assertInstanceOf(\Magento\Customer\Model\Data\AttributeMetadata::class, $attributeMetadata);
  324. $this->assertEquals('firstname', $attributeMetadata->getAttributeCode(), 'Attribute code is invalid');
  325. $this->assertNotEmpty($attributeMetadata->getValidationRules(), 'Validation rules are not set');
  326. $this->assertEquals('1', $attributeMetadata->isSystem(), '"Is system" field value is invalid');
  327. $this->assertEquals('40', $attributeMetadata->getSortOrder(), 'Sort order is invalid');
  328. // Verify the consistency of form attribute metadata from two calls of the same service
  329. // after some getters were called
  330. $attributeMetadata1 = $formAttributesMetadata1['firstname'];
  331. $attributeMetadata1->getAttributeCode();
  332. $attributeMetadata1->getValidationRules();
  333. $this->assertEquals(
  334. $formAttributesMetadata,
  335. $formAttributesMetadata1,
  336. 'Form attribute metadata from the same service became different after some getters were called'
  337. );
  338. // Verify the consistency of form attribute metadata from two services
  339. // after some getters were called
  340. $attributeMetadata2 = $formAttributesMetadata2['firstname'];
  341. $attributeMetadata2->getAttributeCode();
  342. $attributeMetadata2->getValidationRules();
  343. $this->assertEquals(
  344. $formAttributesMetadata,
  345. $formAttributesMetadata2,
  346. 'Form attribute metadata from two services are different after some getters were called'
  347. );
  348. }
  349. protected function tearDown()
  350. {
  351. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  352. /* @var \Magento\Framework\Config\CacheInterface $cache */
  353. $cache = $objectManager->create(\Magento\Framework\Config\CacheInterface::class);
  354. $cache->remove('extension_attributes_config');
  355. }
  356. }