DataProviderWithDefaultAddressesTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Test\Unit\Model\Customer;
  8. use Magento\Customer\Model\AttributeMetadataResolver;
  9. use Magento\Customer\Model\Customer\DataProviderWithDefaultAddresses;
  10. use Magento\Customer\Model\FileUploaderDataResolver;
  11. use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
  12. use Magento\Customer\Model\ResourceModel\Customer\Collection as CustomerCollection;
  13. use Magento\Eav\Model\Config;
  14. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  15. use Magento\Eav\Model\Entity\Type;
  16. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  17. use Magento\Ui\Component\Form\Field;
  18. /**
  19. * Test for class \Magento\Customer\Model\Customer\DataProviderWithDefaultAddresses
  20. *
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class DataProviderWithDefaultAddressesTest extends \PHPUnit\Framework\TestCase
  24. {
  25. private const ATTRIBUTE_CODE = 'test-code';
  26. /**
  27. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $eavConfigMock;
  30. /**
  31. * @var CustomerCollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $customerCollectionFactoryMock;
  34. /**
  35. * @var \Magento\Framework\Session\SessionManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $sessionMock;
  38. /**
  39. * @var \Magento\Directory\Model\CountryFactory|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $countryFactoryMock;
  42. /**
  43. * @var \Magento\Customer\Model\Customer|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $customerMock;
  46. /**
  47. * @var CustomerCollection|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $customerCollectionMock;
  50. /**
  51. * @var FileUploaderDataResolver|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $fileUploaderDataResolver;
  54. /**
  55. * @var AttributeMetadataResolver|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. private $attributeMetadataResolver;
  58. /**
  59. * @return void
  60. */
  61. protected function setUp(): void
  62. {
  63. $this->eavConfigMock = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
  64. $this->customerCollectionFactoryMock = $this->createPartialMock(CustomerCollectionFactory::class, ['create']);
  65. $this->sessionMock = $this->getMockBuilder(\Magento\Framework\Session\SessionManagerInterface::class)
  66. ->setMethods(['getCustomerFormData', 'unsCustomerFormData'])
  67. ->getMockForAbstractClass();
  68. $this->countryFactoryMock = $this->getMockBuilder(\Magento\Directory\Model\CountryFactory::class)
  69. ->disableOriginalConstructor()
  70. ->setMethods(['create', 'loadByCode', 'getName'])
  71. ->getMock();
  72. $this->customerMock = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->customerCollectionMock = $this->getMockBuilder(CustomerCollection::class)
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $this->customerCollectionMock->expects($this->once())->method('addAttributeToSelect')->with('*');
  79. $this->customerCollectionFactoryMock->expects($this->once())
  80. ->method('create')
  81. ->willReturn($this->customerCollectionMock);
  82. $this->eavConfigMock->expects($this->atLeastOnce())
  83. ->method('getEntityType')
  84. ->with('customer')
  85. ->willReturn($this->getTypeCustomerMock([]));
  86. $this->fileUploaderDataResolver = $this->getMockBuilder(FileUploaderDataResolver::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $this->attributeMetadataResolver = $this->getMockBuilder(AttributeMetadataResolver::class)
  90. ->disableOriginalConstructor()
  91. ->setMethods(['getAttributesMeta'])
  92. ->getMock();
  93. $this->attributeMetadataResolver->expects($this->at(0))
  94. ->method('getAttributesMeta')
  95. ->willReturn(
  96. [
  97. 'arguments' => [
  98. 'data' => [
  99. 'config' => [
  100. 'dataType' => 'frontend_input',
  101. 'formElement' => 'frontend_input',
  102. 'options' => 'test-options',
  103. 'visible' => null,
  104. 'required' => 'is_required',
  105. 'label' => __('frontend_label'),
  106. 'sortOrder' => 'sort_order',
  107. 'notice' => 'note',
  108. 'default' => 'default_value',
  109. 'size' => 'multiline_count',
  110. 'componentType' => Field::NAME,
  111. ],
  112. ],
  113. ],
  114. ]
  115. );
  116. $this->attributeMetadataResolver->expects($this->at(1))
  117. ->method('getAttributesMeta')
  118. ->willReturn(
  119. [
  120. 'arguments' => [
  121. 'data' => [
  122. 'config' => [
  123. 'dataType' => 'frontend_input',
  124. 'formElement' => 'frontend_input',
  125. 'visible' => null,
  126. 'required' => 'is_required',
  127. 'label' => __('frontend_label'),
  128. 'sortOrder' => 'sort_order',
  129. 'notice' => 'note',
  130. 'default' => 'default_value',
  131. 'size' => 'multiline_count',
  132. 'componentType' => Field::NAME,
  133. 'prefer' => 'toggle',
  134. 'valueMap' => [
  135. 'true' => 1,
  136. 'false' => 0,
  137. ],
  138. ],
  139. ],
  140. ],
  141. ]
  142. );
  143. $helper = new ObjectManager($this);
  144. $this->dataProvider = $helper->getObject(
  145. DataProviderWithDefaultAddresses::class,
  146. [
  147. 'name' => 'test-name',
  148. 'primaryFieldName' => 'primary-field-name',
  149. 'requestFieldName' => 'request-field-name',
  150. 'customerCollectionFactory' => $this->customerCollectionFactoryMock,
  151. 'eavConfig' => $this->eavConfigMock,
  152. 'countryFactory' => $this->countryFactoryMock,
  153. 'session' => $this->sessionMock,
  154. 'fileUploaderDataResolver' => $this->fileUploaderDataResolver,
  155. 'attributeMetadataResolver' => $this->attributeMetadataResolver,
  156. true
  157. ]
  158. );
  159. }
  160. /**
  161. * Run test getAttributesMeta method
  162. *
  163. * @param array $expected
  164. * @return void
  165. *
  166. * @dataProvider getAttributesMetaDataProvider
  167. */
  168. public function testGetAttributesMetaWithOptions(array $expected): void
  169. {
  170. $meta = $this->dataProvider->getMeta();
  171. $this->assertNotEmpty($meta);
  172. $this->assertEquals($expected, $meta);
  173. }
  174. /**
  175. * Data provider for testGetAttributesMeta
  176. *
  177. * @return array
  178. */
  179. public function getAttributesMetaDataProvider(): array
  180. {
  181. return [
  182. [
  183. 'expected' => [
  184. 'customer' => [
  185. 'children' => [
  186. self::ATTRIBUTE_CODE => [
  187. 'arguments' => [
  188. 'data' => [
  189. 'config' => [
  190. 'dataType' => 'frontend_input',
  191. 'formElement' => 'frontend_input',
  192. 'options' => 'test-options',
  193. 'visible' => null,
  194. 'required' => 'is_required',
  195. 'label' => __('frontend_label'),
  196. 'sortOrder' => 'sort_order',
  197. 'notice' => 'note',
  198. 'default' => 'default_value',
  199. 'size' => 'multiline_count',
  200. 'componentType' => Field::NAME,
  201. ],
  202. ],
  203. ],
  204. ],
  205. 'test-code-boolean' => [
  206. 'arguments' => [
  207. 'data' => [
  208. 'config' => [
  209. 'dataType' => 'frontend_input',
  210. 'formElement' => 'frontend_input',
  211. 'visible' => null,
  212. 'required' => 'is_required',
  213. 'label' => __('frontend_label'),
  214. 'sortOrder' => 'sort_order',
  215. 'notice' => 'note',
  216. 'default' => 'default_value',
  217. 'size' => 'multiline_count',
  218. 'componentType' => Field::NAME,
  219. 'prefer' => 'toggle',
  220. 'valueMap' => [
  221. 'true' => 1,
  222. 'false' => 0,
  223. ],
  224. ],
  225. ],
  226. ],
  227. ],
  228. ],
  229. ],
  230. ]
  231. ]
  232. ];
  233. }
  234. /**
  235. * @param array $customerAttributes
  236. * @return Type|\PHPUnit_Framework_MockObject_MockObject
  237. */
  238. protected function getTypeCustomerMock($customerAttributes = [])
  239. {
  240. $typeCustomerMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
  241. ->disableOriginalConstructor()
  242. ->getMock();
  243. $attributesCollection = !empty($customerAttributes) ? $customerAttributes : $this->getAttributeMock();
  244. foreach ($attributesCollection as $attribute) {
  245. $attribute->expects($this->any())
  246. ->method('getEntityType')
  247. ->willReturn($typeCustomerMock);
  248. }
  249. $typeCustomerMock->expects($this->atLeastOnce())
  250. ->method('getAttributeCollection')
  251. ->willReturn($attributesCollection);
  252. return $typeCustomerMock;
  253. }
  254. /**
  255. * @param array $options
  256. * @return AbstractAttribute[]|\PHPUnit_Framework_MockObject_MockObject[]
  257. */
  258. protected function getAttributeMock($options = []): array
  259. {
  260. $attributeMock = $this->getMockBuilder(AbstractAttribute::class)
  261. ->setMethods(
  262. [
  263. 'getAttributeCode',
  264. 'getDataUsingMethod',
  265. 'getFrontendInput',
  266. 'getIsVisible',
  267. 'getSource',
  268. 'getIsUserDefined',
  269. 'getUsedInForms',
  270. 'getEntityType',
  271. ]
  272. )
  273. ->disableOriginalConstructor()
  274. ->getMockForAbstractClass();
  275. $attributeCode = self::ATTRIBUTE_CODE;
  276. if (isset($options[self::ATTRIBUTE_CODE]['specific_code_prefix'])) {
  277. $attributeCode .= $options[self::ATTRIBUTE_CODE]['specific_code_prefix'];
  278. }
  279. $attributeMock->expects($this->once())
  280. ->method('getAttributeCode')
  281. ->willReturn($attributeCode);
  282. $attributeBooleanMock = $this->getMockBuilder(AbstractAttribute::class)
  283. ->setMethods(
  284. [
  285. 'getAttributeCode',
  286. 'getDataUsingMethod',
  287. 'getFrontendInput',
  288. 'getIsVisible',
  289. 'getIsUserDefined',
  290. 'getUsedInForms',
  291. 'getSource',
  292. 'getEntityType',
  293. ]
  294. )
  295. ->disableOriginalConstructor()
  296. ->getMockForAbstractClass();
  297. $booleanAttributeCode = 'test-code-boolean';
  298. if (isset($options['test-code-boolean']['specific_code_prefix'])) {
  299. $booleanAttributeCode .= $options['test-code-boolean']['specific_code_prefix'];
  300. }
  301. $attributeBooleanMock->expects($this->once())
  302. ->method('getAttributeCode')
  303. ->willReturn($booleanAttributeCode);
  304. $mocks = [$attributeMock, $attributeBooleanMock];
  305. return $mocks;
  306. }
  307. /**
  308. * @return void
  309. */
  310. public function testGetData(): void
  311. {
  312. $customerData = [
  313. 'email' => 'test@test.ua',
  314. 'default_billing' => 2,
  315. 'default_shipping' => 2,
  316. 'password_hash' => 'password_hash',
  317. 'rp_token' => 'rp_token',
  318. 'confirmation' => 'confirmation',
  319. ];
  320. $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
  321. ->disableOriginalConstructor()
  322. ->getMock();
  323. $this->customerCollectionMock->expects($this->once())->method('getItems')->willReturn([$this->customerMock]);
  324. $this->customerMock->expects($this->once())->method('getData')->willReturn($customerData);
  325. $this->customerMock->expects($this->atLeastOnce())->method('getId')->willReturn(1);
  326. $this->customerMock->expects($this->once())->method('getDefaultBillingAddress')->willReturn($address);
  327. $this->countryFactoryMock->expects($this->once())->method('create')->willReturnSelf();
  328. $this->countryFactoryMock->expects($this->once())->method('loadByCode')->willReturnSelf();
  329. $this->countryFactoryMock->expects($this->once())->method('getName')->willReturn('Ukraine');
  330. $this->sessionMock->expects($this->once())
  331. ->method('getCustomerFormData')
  332. ->willReturn(null);
  333. $this->assertEquals(
  334. [
  335. 1 => [
  336. 'customer' => [
  337. 'email' => 'test@test.ua',
  338. 'default_billing' => 2,
  339. 'default_shipping' => 2,
  340. ],
  341. 'default_billing_address' => [
  342. 'country' => 'Ukraine',
  343. ],
  344. 'default_shipping_address' => [],
  345. 'customer_id' => 1
  346. ]
  347. ],
  348. $this->dataProvider->getData()
  349. );
  350. }
  351. /**
  352. * @return void
  353. */
  354. public function testGetDataWithCustomerFormData(): void
  355. {
  356. $customerId = 11;
  357. $customerFormData = [
  358. 'customer' => [
  359. 'email' => 'test1@test1.ua',
  360. 'default_billing' => 3,
  361. 'default_shipping' => 3,
  362. 'entity_id' => $customerId,
  363. ],
  364. 'address' => [
  365. 3 => [
  366. 'firstname' => 'firstname1',
  367. 'lastname' => 'lastname1',
  368. 'street' => [
  369. 'street1',
  370. 'street2',
  371. ],
  372. 'default_billing' => 3,
  373. 'default_shipping' => 3,
  374. ],
  375. ],
  376. ];
  377. $this->customerCollectionMock->expects($this->once())->method('getItems')->willReturn([$this->customerMock]);
  378. $this->customerMock->expects($this->once())
  379. ->method('getData')
  380. ->willReturn([
  381. 'email' => 'test@test.ua',
  382. 'default_billing' => 2,
  383. 'default_shipping' => 2,
  384. ]);
  385. $this->customerMock->expects($this->atLeastOnce())->method('getId')->willReturn($customerId);
  386. $this->sessionMock->expects($this->once())->method('getCustomerFormData')->willReturn($customerFormData);
  387. $this->sessionMock->expects($this->once())->method('unsCustomerFormData');
  388. $this->assertEquals([$customerId => $customerFormData], $this->dataProvider->getData());
  389. }
  390. }