DataProviderTest.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\Customer;
  7. use Magento\Customer\Api\CustomerMetadataInterface;
  8. use Magento\Customer\Model\Config\Share;
  9. use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites;
  10. use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
  11. use Magento\Eav\Model\Config;
  12. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  13. use Magento\Eav\Model\Entity\Type;
  14. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  15. use Magento\Ui\Component\Form\Field;
  16. use Magento\Ui\DataProvider\EavValidationRules;
  17. /**
  18. * Class DataProviderTest
  19. *
  20. * Test for class \Magento\Customer\Model\Customer\DataProvider
  21. *
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class DataProviderTest extends \PHPUnit\Framework\TestCase
  25. {
  26. const ATTRIBUTE_CODE = 'test-code';
  27. const OPTIONS_RESULT = 'test-options';
  28. /**
  29. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $eavConfigMock;
  32. /**
  33. * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $customerCollectionFactoryMock;
  36. /**
  37. * @var EavValidationRules|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $eavValidationRulesMock;
  40. /**
  41. * @var \Magento\Framework\Session\SessionManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $sessionMock;
  44. /**
  45. * @var \Magento\Customer\Model\FileProcessor|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $fileProcessor;
  48. /**
  49. * @var \Magento\Customer\Model\FileUploaderDataResolver|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $fileUploaderDataResolver;
  52. /**
  53. * Set up
  54. *
  55. * @return void
  56. */
  57. protected function setUp()
  58. {
  59. $this->eavConfigMock = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->customerCollectionFactoryMock = $this->createPartialMock(
  63. \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory::class,
  64. ['create']
  65. );
  66. $this->eavValidationRulesMock = $this
  67. ->getMockBuilder(\Magento\Ui\DataProvider\EavValidationRules::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->sessionMock = $this
  71. ->getMockBuilder(\Magento\Framework\Session\SessionManagerInterface::class)
  72. ->setMethods(['getCustomerFormData', 'unsCustomerFormData'])
  73. ->getMockForAbstractClass();
  74. $this->fileProcessor = $this->getMockBuilder(\Magento\Customer\Model\FileProcessor::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->fileUploaderDataResolver = $this->getMockBuilder(\Magento\Customer\Model\FileUploaderDataResolver::class)
  78. ->disableOriginalConstructor()
  79. ->setMethods(['overrideFileUploaderMetadata', 'overrideFileUploaderData'])
  80. ->getMock();
  81. }
  82. /**
  83. * Run test getAttributesMeta method
  84. *
  85. * @param array $expected
  86. * @return void
  87. *
  88. * @dataProvider getAttributesMetaDataProvider
  89. */
  90. public function testGetAttributesMetaWithOptions(array $expected)
  91. {
  92. $helper = new ObjectManager($this);
  93. /** @var \Magento\Customer\Model\Customer\DataProvider $dataProvider */
  94. $dataProvider = $helper->getObject(
  95. \Magento\Customer\Model\Customer\DataProvider::class,
  96. [
  97. 'name' => 'test-name',
  98. 'primaryFieldName' => 'primary-field-name',
  99. 'requestFieldName' => 'request-field-name',
  100. 'eavValidationRules' => $this->eavValidationRulesMock,
  101. 'customerCollectionFactory' => $this->getCustomerCollectionFactoryMock(),
  102. 'eavConfig' => $this->getEavConfigMock(),
  103. 'fileUploaderDataResolver' => $this->fileUploaderDataResolver
  104. ]
  105. );
  106. $meta = $dataProvider->getMeta();
  107. $this->assertNotEmpty($meta);
  108. $this->assertEquals($expected, $meta);
  109. }
  110. /**
  111. * Data provider for testGetAttributesMeta
  112. *
  113. * @return array
  114. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  115. */
  116. public function getAttributesMetaDataProvider()
  117. {
  118. return [
  119. [
  120. 'expected' => [
  121. 'customer' => [
  122. 'children' => [
  123. self::ATTRIBUTE_CODE => [
  124. 'arguments' => [
  125. 'data' => [
  126. 'config' => [
  127. 'dataType' => 'frontend_input',
  128. 'formElement' => 'frontend_input',
  129. 'options' => 'test-options',
  130. 'visible' => null,
  131. 'required' => 'is_required',
  132. 'label' => __('frontend_label'),
  133. 'sortOrder' => 'sort_order',
  134. 'notice' => 'note',
  135. 'default' => 'default_value',
  136. 'size' => 'multiline_count',
  137. 'componentType' => Field::NAME,
  138. ],
  139. ],
  140. ],
  141. ],
  142. 'test-code-boolean' => [
  143. 'arguments' => [
  144. 'data' => [
  145. 'config' => [
  146. 'dataType' => 'frontend_input',
  147. 'formElement' => 'frontend_input',
  148. 'visible' => null,
  149. 'required' => 'is_required',
  150. 'label' => __('frontend_label'),
  151. 'sortOrder' => 'sort_order',
  152. 'notice' => 'note',
  153. 'default' => 'default_value',
  154. 'size' => 'multiline_count',
  155. 'componentType' => Field::NAME,
  156. 'prefer' => 'toggle',
  157. 'valueMap' => [
  158. 'true' => 1,
  159. 'false' => 0,
  160. ],
  161. ],
  162. ],
  163. ],
  164. ],
  165. ],
  166. ],
  167. 'address' => [
  168. 'children' => [
  169. self::ATTRIBUTE_CODE => [
  170. 'arguments' => [
  171. 'data' => [
  172. 'config' => [
  173. 'dataType' => 'frontend_input',
  174. 'formElement' => 'frontend_input',
  175. 'options' => 'test-options',
  176. 'visible' => null,
  177. 'required' => 'is_required',
  178. 'label' => __('frontend_label'),
  179. 'sortOrder' => 'sort_order',
  180. 'notice' => 'note',
  181. 'default' => 'default_value',
  182. 'size' => 'multiline_count',
  183. 'componentType' => Field::NAME,
  184. ],
  185. ],
  186. ],
  187. ],
  188. 'test-code-boolean' => [
  189. 'arguments' => [
  190. 'data' => [
  191. 'config' => [
  192. 'dataType' => 'frontend_input',
  193. 'formElement' => 'frontend_input',
  194. 'visible' => null,
  195. 'required' => 'is_required',
  196. 'label' => 'frontend_label',
  197. 'sortOrder' => 'sort_order',
  198. 'notice' => 'note',
  199. 'default' => 'default_value',
  200. 'size' => 'multiline_count',
  201. 'componentType' => Field::NAME,
  202. 'prefer' => 'toggle',
  203. 'valueMap' => [
  204. 'true' => 1,
  205. 'false' => 0,
  206. ],
  207. ],
  208. ],
  209. ],
  210. ],
  211. 'country_id' => [
  212. 'arguments' => [
  213. 'data' => [
  214. 'config' => [
  215. 'dataType' => 'frontend_input',
  216. 'formElement' => 'frontend_input',
  217. 'options' => 'test-options',
  218. 'visible' => null,
  219. 'required' => 'is_required',
  220. 'label' => __('frontend_label'),
  221. 'sortOrder' => 'sort_order',
  222. 'notice' => 'note',
  223. 'default' => 'default_value',
  224. 'size' => 'multiline_count',
  225. 'componentType' => Field::NAME,
  226. 'filterBy' => [
  227. 'target' => '${ $.provider }:data.customer.website_id',
  228. 'field' => 'website_ids'
  229. ]
  230. ],
  231. ],
  232. ],
  233. ]
  234. ],
  235. ],
  236. ]
  237. ]
  238. ];
  239. }
  240. /**
  241. * @return CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  242. */
  243. protected function getCustomerCollectionFactoryMock()
  244. {
  245. $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
  246. ->disableOriginalConstructor()
  247. ->getMock();
  248. $collectionMock->expects($this->once())
  249. ->method('addAttributeToSelect')
  250. ->with('*');
  251. $this->customerCollectionFactoryMock->expects($this->once())
  252. ->method('create')
  253. ->willReturn($collectionMock);
  254. return $this->customerCollectionFactoryMock;
  255. }
  256. /**
  257. * @return Config|\PHPUnit_Framework_MockObject_MockObject
  258. */
  259. protected function getEavConfigMock($customerAttributes = [])
  260. {
  261. $this->eavConfigMock->expects($this->at(0))
  262. ->method('getEntityType')
  263. ->with('customer')
  264. ->willReturn($this->getTypeCustomerMock($customerAttributes));
  265. $this->eavConfigMock->expects($this->at(1))
  266. ->method('getEntityType')
  267. ->with('customer_address')
  268. ->willReturn($this->getTypeAddressMock());
  269. return $this->eavConfigMock;
  270. }
  271. /**
  272. * @return Type|\PHPUnit_Framework_MockObject_MockObject
  273. */
  274. protected function getTypeCustomerMock($customerAttributes = [])
  275. {
  276. $typeCustomerMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
  277. ->disableOriginalConstructor()
  278. ->getMock();
  279. $attributesCollection = !empty($customerAttributes) ? $customerAttributes : $this->getAttributeMock();
  280. $typeCustomerMock->expects($this->any())
  281. ->method('getEntityTypeCode')
  282. ->willReturn('customer');
  283. foreach ($attributesCollection as $attribute) {
  284. $attribute->expects($this->any())
  285. ->method('getEntityType')
  286. ->willReturn($typeCustomerMock);
  287. }
  288. $typeCustomerMock->expects($this->once())
  289. ->method('getAttributeCollection')
  290. ->willReturn($attributesCollection);
  291. return $typeCustomerMock;
  292. }
  293. /**
  294. * @return Type|\PHPUnit_Framework_MockObject_MockObject
  295. */
  296. protected function getTypeAddressMock()
  297. {
  298. $typeAddressMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
  299. ->disableOriginalConstructor()
  300. ->getMock();
  301. $typeAddressMock->expects($this->once())
  302. ->method('getAttributeCollection')
  303. ->willReturn($this->getAttributeMock('address'));
  304. return $typeAddressMock;
  305. }
  306. /**
  307. * @param \PHPUnit_Framework_MockObject_MockObject $attributeMock
  308. * @param \PHPUnit_Framework_MockObject_MockObject $attributeBooleanMock
  309. * @param array $options
  310. */
  311. private function injectVisibilityProps(
  312. \PHPUnit_Framework_MockObject_MockObject $attributeMock,
  313. \PHPUnit_Framework_MockObject_MockObject $attributeBooleanMock,
  314. array $options = []
  315. ) {
  316. if (isset($options[self::ATTRIBUTE_CODE]['visible'])) {
  317. $attributeMock->expects($this->any())
  318. ->method('getIsVisible')
  319. ->willReturn($options[self::ATTRIBUTE_CODE]['visible']);
  320. }
  321. if (isset($options[self::ATTRIBUTE_CODE]['user_defined'])) {
  322. $attributeMock->expects($this->any())
  323. ->method('getIsUserDefined')
  324. ->willReturn($options[self::ATTRIBUTE_CODE]['user_defined']);
  325. }
  326. if (isset($options[self::ATTRIBUTE_CODE]['is_used_in_forms'])) {
  327. $attributeMock->expects($this->any())
  328. ->method('getUsedInForms')
  329. ->willReturn($options[self::ATTRIBUTE_CODE]['is_used_in_forms']);
  330. }
  331. if (isset($options['test-code-boolean']['visible'])) {
  332. $attributeBooleanMock->expects($this->any())
  333. ->method('getIsVisible')
  334. ->willReturn($options['test-code-boolean']['visible']);
  335. }
  336. if (isset($options['test-code-boolean']['user_defined'])) {
  337. $attributeBooleanMock->expects($this->any())
  338. ->method('getIsUserDefined')
  339. ->willReturn($options['test-code-boolean']['user_defined']);
  340. }
  341. if (isset($options['test-code-boolean']['is_used_in_forms'])) {
  342. $attributeBooleanMock->expects($this->any())
  343. ->method('getUsedInForms')
  344. ->willReturn($options['test-code-boolean']['is_used_in_forms']);
  345. }
  346. }
  347. /**
  348. * @return AbstractAttribute[]|\PHPUnit_Framework_MockObject_MockObject[]
  349. */
  350. protected function getAttributeMock($type = 'customer', $options = [])
  351. {
  352. $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  353. ->setMethods(
  354. [
  355. 'getAttributeCode',
  356. 'getDataUsingMethod',
  357. 'usesSource',
  358. 'getFrontendInput',
  359. 'getIsVisible',
  360. 'getSource',
  361. 'getIsUserDefined',
  362. 'getUsedInForms',
  363. 'getEntityType',
  364. ]
  365. )
  366. ->disableOriginalConstructor()
  367. ->getMockForAbstractClass();
  368. $sourceMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::class)
  369. ->disableOriginalConstructor()
  370. ->getMockForAbstractClass();
  371. $attributeCode = self::ATTRIBUTE_CODE;
  372. if (isset($options[self::ATTRIBUTE_CODE]['specific_code_prefix'])) {
  373. $attributeCode = $attributeCode . $options[self::ATTRIBUTE_CODE]['specific_code_prefix'];
  374. }
  375. $attributeMock->expects($this->exactly(2))
  376. ->method('getAttributeCode')
  377. ->willReturn($attributeCode);
  378. $sourceMock->expects($this->any())
  379. ->method('getAllOptions')
  380. ->willReturn(self::OPTIONS_RESULT);
  381. $attributeMock->expects($this->any())
  382. ->method('getDataUsingMethod')
  383. ->willReturnCallback($this->attributeGetUsingMethodCallback());
  384. $attributeMock->expects($this->any())
  385. ->method('usesSource')
  386. ->willReturn(true);
  387. $attributeMock->expects($this->any())
  388. ->method('getSource')
  389. ->willReturn($sourceMock);
  390. $attributeBooleanMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  391. ->setMethods(
  392. [
  393. 'getAttributeCode',
  394. 'getDataUsingMethod',
  395. 'usesSource',
  396. 'getFrontendInput',
  397. 'getIsVisible',
  398. 'getIsUserDefined',
  399. 'getUsedInForms',
  400. 'getSource',
  401. 'getEntityType',
  402. ]
  403. )
  404. ->disableOriginalConstructor()
  405. ->getMockForAbstractClass();
  406. $attributeBooleanMock->expects($this->any())
  407. ->method('getFrontendInput')
  408. ->willReturn('boolean');
  409. $attributeBooleanMock->expects($this->any())
  410. ->method('getDataUsingMethod')
  411. ->willReturnCallback($this->attributeGetUsingMethodCallback());
  412. $attributeBooleanMock->expects($this->once())
  413. ->method('usesSource')
  414. ->willReturn(false);
  415. $booleanAttributeCode = 'test-code-boolean';
  416. if (isset($options['test-code-boolean']['specific_code_prefix'])) {
  417. $booleanAttributeCode = $booleanAttributeCode . $options['test-code-boolean']['specific_code_prefix'];
  418. }
  419. $attributeBooleanMock->expects($this->exactly(2))
  420. ->method('getAttributeCode')
  421. ->willReturn($booleanAttributeCode);
  422. $this->eavValidationRulesMock->expects($this->any())
  423. ->method('build')
  424. ->willReturnMap([
  425. [$attributeMock, $this->logicalNot($this->isEmpty()), []],
  426. [$attributeBooleanMock, $this->logicalNot($this->isEmpty()), []],
  427. ]);
  428. $mocks = [$attributeMock, $attributeBooleanMock];
  429. $this->injectVisibilityProps($attributeMock, $attributeBooleanMock, $options);
  430. if ($type == "address") {
  431. $mocks[] = $this->getCountryAttrMock();
  432. }
  433. return $mocks;
  434. }
  435. /**
  436. * Callback for ::getDataUsingMethod
  437. *
  438. * @return \Closure
  439. */
  440. private function attributeGetUsingMethodCallback()
  441. {
  442. return function ($origName) {
  443. return $origName;
  444. };
  445. }
  446. /**
  447. * @return \PHPUnit_Framework_MockObject_MockObject
  448. */
  449. private function getCountryAttrMock()
  450. {
  451. $countryByWebsiteMock = $this->getMockBuilder(CountryWithWebsites::class)
  452. ->disableOriginalConstructor()
  453. ->getMock();
  454. $countryByWebsiteMock->expects($this->any())
  455. ->method('getAllOptions')
  456. ->willReturn('test-options');
  457. $shareMock = $this->getMockBuilder(Share::class)
  458. ->disableOriginalConstructor()
  459. ->getMock();
  460. $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  461. $objectManagerMock->expects($this->any())
  462. ->method('get')
  463. ->willReturnMap([
  464. [CountryWithWebsites::class, $countryByWebsiteMock],
  465. [Share::class, $shareMock],
  466. ]);
  467. \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
  468. $countryAttrMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  469. ->setMethods(['getAttributeCode', 'getDataUsingMethod', 'usesSource', 'getSource', 'getLabel'])
  470. ->disableOriginalConstructor()
  471. ->getMockForAbstractClass();
  472. $countryAttrMock->expects($this->exactly(2))
  473. ->method('getAttributeCode')
  474. ->willReturn('country_id');
  475. $countryAttrMock->expects($this->any())
  476. ->method('getDataUsingMethod')
  477. ->willReturnCallback(
  478. function ($origName) {
  479. return $origName;
  480. }
  481. );
  482. $countryAttrMock->expects($this->any())
  483. ->method('getLabel')
  484. ->willReturn(__('frontend_label'));
  485. $countryAttrMock->expects($this->any())
  486. ->method('usesSource')
  487. ->willReturn(true);
  488. $countryAttrMock->expects($this->any())
  489. ->method('getSource')
  490. ->willReturn(null);
  491. return $countryAttrMock;
  492. }
  493. /**
  494. * @return void
  495. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  496. */
  497. public function testGetData()
  498. {
  499. $customerData = [
  500. 'email' => 'test@test.ua',
  501. 'default_billing' => 2,
  502. 'default_shipping' => 2,
  503. 'password_hash' => 'password_hash',
  504. 'rp_token' => 'rp_token',
  505. 'confirmation' => 'confirmation',
  506. ];
  507. $addressData = [
  508. 'firstname' => 'firstname',
  509. 'lastname' => 'lastname',
  510. 'street' => "street\nstreet",
  511. ];
  512. $customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
  513. ->disableOriginalConstructor()
  514. ->getMock();
  515. $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
  516. ->disableOriginalConstructor()
  517. ->getMock();
  518. $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
  519. ->disableOriginalConstructor()
  520. ->getMock();
  521. $collectionMock->expects($this->once())
  522. ->method('addAttributeToSelect')
  523. ->with('*');
  524. $this->customerCollectionFactoryMock->expects($this->once())
  525. ->method('create')
  526. ->willReturn($collectionMock);
  527. $collectionMock->expects($this->once())
  528. ->method('getItems')
  529. ->willReturn([$customer]);
  530. $customer->expects($this->once())
  531. ->method('getData')
  532. ->willReturn($customerData);
  533. $customer->expects($this->once())
  534. ->method('getAddresses')
  535. ->willReturn([$address]);
  536. $address->expects($this->atLeastOnce())
  537. ->method('getId')
  538. ->willReturn(2);
  539. $address->expects($this->once())
  540. ->method('load')
  541. ->with(2)
  542. ->willReturnSelf();
  543. $address->expects($this->once())
  544. ->method('getData')
  545. ->willReturn($addressData);
  546. $helper = new ObjectManager($this);
  547. $dataProvider = $helper->getObject(
  548. \Magento\Customer\Model\Customer\DataProvider::class,
  549. [
  550. 'name' => 'test-name',
  551. 'primaryFieldName' => 'primary-field-name',
  552. 'requestFieldName' => 'request-field-name',
  553. 'eavValidationRules' => $this->eavValidationRulesMock,
  554. 'customerCollectionFactory' => $this->customerCollectionFactoryMock,
  555. 'eavConfig' => $this->getEavConfigMock(),
  556. 'fileUploaderDataResolver' => $this->fileUploaderDataResolver
  557. ]
  558. );
  559. $reflection = new \ReflectionClass(get_class($dataProvider));
  560. $reflectionProperty = $reflection->getProperty('session');
  561. $reflectionProperty->setAccessible(true);
  562. $reflectionProperty->setValue($dataProvider, $this->sessionMock);
  563. $this->sessionMock->expects($this->once())
  564. ->method('getCustomerFormData')
  565. ->willReturn(null);
  566. $this->assertEquals(
  567. [
  568. '' => [
  569. 'customer' => [
  570. 'email' => 'test@test.ua',
  571. 'default_billing' => 2,
  572. 'default_shipping' => 2,
  573. ],
  574. 'address' => [
  575. 2 => [
  576. 'firstname' => 'firstname',
  577. 'lastname' => 'lastname',
  578. // Won't be an array because it isn't defined as a multiline field in this test
  579. 'street' => "street\nstreet",
  580. 'default_billing' => 2,
  581. 'default_shipping' => 2,
  582. ]
  583. ]
  584. ]
  585. ],
  586. $dataProvider->getData()
  587. );
  588. }
  589. /**
  590. * @return void
  591. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  592. */
  593. public function testGetDataWithCustomerFormData()
  594. {
  595. $customerId = 11;
  596. $customerFormData = [
  597. 'customer' => [
  598. 'email' => 'test1@test1.ua',
  599. 'default_billing' => 3,
  600. 'default_shipping' => 3,
  601. 'entity_id' => $customerId,
  602. ],
  603. 'address' => [
  604. 3 => [
  605. 'firstname' => 'firstname1',
  606. 'lastname' => 'lastname1',
  607. 'street' => [
  608. 'street1',
  609. 'street2',
  610. ],
  611. 'default_billing' => 3,
  612. 'default_shipping' => 3,
  613. ],
  614. ],
  615. ];
  616. $customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
  617. ->disableOriginalConstructor()
  618. ->getMock();
  619. $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
  620. ->disableOriginalConstructor()
  621. ->getMock();
  622. $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
  623. ->disableOriginalConstructor()
  624. ->getMock();
  625. $collectionMock->expects($this->once())
  626. ->method('addAttributeToSelect')
  627. ->with('*');
  628. $this->customerCollectionFactoryMock->expects($this->once())
  629. ->method('create')
  630. ->willReturn($collectionMock);
  631. $collectionMock->expects($this->once())
  632. ->method('getItems')
  633. ->willReturn([$customer]);
  634. $customer->expects($this->once())
  635. ->method('getData')
  636. ->willReturn([
  637. 'email' => 'test@test.ua',
  638. 'default_billing' => 2,
  639. 'default_shipping' => 2,
  640. ]);
  641. $customer->expects($this->once())
  642. ->method('getId')
  643. ->willReturn($customerId);
  644. $customer->expects($this->once())
  645. ->method('getAddresses')
  646. ->willReturn([$address]);
  647. $address->expects($this->atLeastOnce())
  648. ->method('getId')
  649. ->willReturn(2);
  650. $address->expects($this->once())
  651. ->method('load')
  652. ->with(2)
  653. ->willReturnSelf();
  654. $address->expects($this->once())
  655. ->method('getData')
  656. ->willReturn([
  657. 'firstname' => 'firstname',
  658. 'lastname' => 'lastname',
  659. 'street' => "street\nstreet",
  660. ]);
  661. $helper = new ObjectManager($this);
  662. $dataProvider = $helper->getObject(
  663. \Magento\Customer\Model\Customer\DataProvider::class,
  664. [
  665. 'name' => 'test-name',
  666. 'primaryFieldName' => 'primary-field-name',
  667. 'requestFieldName' => 'request-field-name',
  668. 'eavValidationRules' => $this->eavValidationRulesMock,
  669. 'customerCollectionFactory' => $this->customerCollectionFactoryMock,
  670. 'eavConfig' => $this->getEavConfigMock(),
  671. 'fileUploaderDataResolver' => $this->fileUploaderDataResolver
  672. ]
  673. );
  674. $reflection = new \ReflectionClass(get_class($dataProvider));
  675. $reflectionProperty = $reflection->getProperty('session');
  676. $reflectionProperty->setAccessible(true);
  677. $reflectionProperty->setValue($dataProvider, $this->sessionMock);
  678. $this->sessionMock->expects($this->once())
  679. ->method('getCustomerFormData')
  680. ->willReturn($customerFormData);
  681. $this->sessionMock->expects($this->once())
  682. ->method('unsCustomerFormData');
  683. $this->assertEquals([$customerId => $customerFormData], $dataProvider->getData());
  684. }
  685. /**
  686. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  687. * @return void
  688. */
  689. public function testGetDataWithCustomAttributeImage()
  690. {
  691. $customerId = 1;
  692. $customerEmail = 'user1@example.com';
  693. $filename = '/filename.ext1';
  694. $customerMock = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
  695. ->disableOriginalConstructor()
  696. ->getMock();
  697. $customerMock->expects($this->once())
  698. ->method('getData')
  699. ->willReturn([
  700. 'email' => $customerEmail,
  701. 'img1' => $filename,
  702. ]);
  703. $customerMock->expects($this->once())
  704. ->method('getAddresses')
  705. ->willReturn([]);
  706. $customerMock->expects($this->once())
  707. ->method('getId')
  708. ->willReturn($customerId);
  709. $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
  710. ->disableOriginalConstructor()
  711. ->getMock();
  712. $collectionMock->expects($this->once())
  713. ->method('getItems')
  714. ->willReturn([$customerMock]);
  715. $this->customerCollectionFactoryMock->expects($this->once())
  716. ->method('create')
  717. ->willReturn($collectionMock);
  718. $this->sessionMock->expects($this->once())
  719. ->method('getCustomerFormData')
  720. ->willReturn([]);
  721. $objectManager = new ObjectManager($this);
  722. $dataProvider = $objectManager->getObject(
  723. \Magento\Customer\Model\Customer\DataProvider::class,
  724. [
  725. 'name' => 'test-name',
  726. 'primaryFieldName' => 'primary-field-name',
  727. 'requestFieldName' => 'request-field-name',
  728. 'eavValidationRules' => $this->eavValidationRulesMock,
  729. 'customerCollectionFactory' => $this->customerCollectionFactoryMock,
  730. 'eavConfig' => $this->getEavConfigMock(),
  731. 'fileUploaderDataResolver' => $this->fileUploaderDataResolver
  732. ]
  733. );
  734. $objectManager->setBackwardCompatibleProperty(
  735. $dataProvider,
  736. 'session',
  737. $this->sessionMock
  738. );
  739. $this->fileUploaderDataResolver->expects($this->atLeastOnce())->method('overrideFileUploaderData')
  740. ->with(
  741. $customerMock,
  742. [
  743. 'email' => $customerEmail,
  744. 'img1' => $filename,
  745. ]
  746. );
  747. $dataProvider->getData();
  748. }
  749. /**
  750. * @return void
  751. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  752. */
  753. public function testGetAttributesMetaWithCustomAttributeImage()
  754. {
  755. $maxFileSize = 1000;
  756. $allowedExtension = 'ext1 ext2';
  757. $attributeCode = 'img1';
  758. $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
  759. ->disableOriginalConstructor()
  760. ->getMock();
  761. $collectionMock->expects($this->once())
  762. ->method('addAttributeToSelect')
  763. ->with('*');
  764. $this->customerCollectionFactoryMock->expects($this->once())
  765. ->method('create')
  766. ->willReturn($collectionMock);
  767. $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  768. ->setMethods([
  769. 'getAttributeCode',
  770. 'getFrontendInput',
  771. 'getDataUsingMethod',
  772. ])
  773. ->disableOriginalConstructor()
  774. ->getMockForAbstractClass();
  775. $attributeMock->expects($this->any())
  776. ->method('getAttributeCode')
  777. ->willReturn($attributeCode);
  778. $attributeMock->expects($this->any())
  779. ->method('getFrontendInput')
  780. ->willReturn('image');
  781. $attributeMock->expects($this->any())
  782. ->method('getDataUsingMethod')
  783. ->willReturnCallback(
  784. function ($origName) {
  785. return $origName;
  786. }
  787. );
  788. $typeCustomerMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
  789. ->disableOriginalConstructor()
  790. ->getMock();
  791. $typeCustomerMock->expects($this->once())
  792. ->method('getAttributeCollection')
  793. ->willReturn([$attributeMock]);
  794. $typeCustomerMock->expects($this->once())
  795. ->method('getEntityTypeCode')
  796. ->willReturn(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
  797. $typeAddressMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
  798. ->disableOriginalConstructor()
  799. ->getMock();
  800. $typeAddressMock->expects($this->once())
  801. ->method('getAttributeCollection')
  802. ->willReturn([]);
  803. $this->eavConfigMock->expects($this->at(0))
  804. ->method('getEntityType')
  805. ->with('customer')
  806. ->willReturn($typeCustomerMock);
  807. $this->eavConfigMock->expects($this->at(1))
  808. ->method('getEntityType')
  809. ->with('customer_address')
  810. ->willReturn($typeAddressMock);
  811. $this->eavValidationRulesMock->expects($this->once())
  812. ->method('build')
  813. ->with($attributeMock, [
  814. 'dataType' => 'frontend_input',
  815. 'formElement' => 'frontend_input',
  816. 'visible' => 'is_visible',
  817. 'required' => 'is_required',
  818. 'sortOrder' => 'sort_order',
  819. 'notice' => 'note',
  820. 'default' => 'default_value',
  821. 'size' => 'multiline_count',
  822. 'label' => __('frontend_label'),
  823. ])
  824. ->willReturn([
  825. 'max_file_size' => $maxFileSize,
  826. 'file_extensions' => 'ext1, eXt2 ', // Added spaces and upper-cases
  827. ]);
  828. $objectManager = new ObjectManager($this);
  829. $dataProvider = $objectManager->getObject(
  830. \Magento\Customer\Model\Customer\DataProvider::class,
  831. [
  832. 'name' => 'test-name',
  833. 'primaryFieldName' => 'primary-field-name',
  834. 'requestFieldName' => 'request-field-name',
  835. 'eavValidationRules' => $this->eavValidationRulesMock,
  836. 'customerCollectionFactory' => $this->customerCollectionFactoryMock,
  837. 'eavConfig' => $this->eavConfigMock
  838. ]
  839. );
  840. $result = $dataProvider->getMeta();
  841. $this->assertNotEmpty($result);
  842. $expected = [
  843. 'customer' => [
  844. 'children' => [
  845. $attributeCode => [
  846. 'arguments' => [
  847. 'data' => [
  848. 'config' => [
  849. 'formElement' => 'fileUploader',
  850. 'componentType' => 'fileUploader',
  851. 'maxFileSize' => $maxFileSize,
  852. 'allowedExtensions' => $allowedExtension,
  853. 'uploaderConfig' => [
  854. 'url' => 'customer/file/customer_upload',
  855. ],
  856. 'sortOrder' => 'sort_order',
  857. 'required' => 'is_required',
  858. 'visible' => null,
  859. 'validation' => [
  860. 'max_file_size' => $maxFileSize,
  861. 'file_extensions' => 'ext1, eXt2 ',
  862. ],
  863. 'label' => __('frontend_label'),
  864. ],
  865. ],
  866. ],
  867. ],
  868. ],
  869. ],
  870. 'address' => [
  871. 'children' => [],
  872. ],
  873. ];
  874. $this->assertEquals($expected, $result);
  875. }
  876. /**
  877. * @return void
  878. */
  879. public function testGetDataWithVisibleAttributes()
  880. {
  881. $firstAttributesBundle = $this->getAttributeMock(
  882. 'customer',
  883. [
  884. self::ATTRIBUTE_CODE => [
  885. 'visible' => true,
  886. 'is_used_in_forms' => ['customer_account_edit'],
  887. 'user_defined' => true,
  888. 'specific_code_prefix' => "_1"
  889. ],
  890. 'test-code-boolean' => [
  891. 'visible' => true,
  892. 'is_used_in_forms' => ['customer_account_create'],
  893. 'user_defined' => true,
  894. 'specific_code_prefix' => "_1"
  895. ]
  896. ]
  897. );
  898. $secondAttributesBundle = $this->getAttributeMock(
  899. 'customer',
  900. [
  901. self::ATTRIBUTE_CODE => [
  902. 'visible' => true,
  903. 'is_used_in_forms' => ['customer_account_create'],
  904. 'user_defined' => false,
  905. 'specific_code_prefix' => "_2"
  906. ],
  907. 'test-code-boolean' => [
  908. 'visible' => true,
  909. 'is_used_in_forms' => ['customer_account_create'],
  910. 'user_defined' => true,
  911. 'specific_code_prefix' => "_2"
  912. ]
  913. ]
  914. );
  915. $helper = new ObjectManager($this);
  916. /** @var \Magento\Customer\Model\Customer\DataProvider $dataProvider */
  917. $dataProvider = $helper->getObject(
  918. \Magento\Customer\Model\Customer\DataProvider::class,
  919. [
  920. 'name' => 'test-name',
  921. 'primaryFieldName' => 'primary-field-name',
  922. 'requestFieldName' => 'request-field-name',
  923. 'eavValidationRules' => $this->eavValidationRulesMock,
  924. 'customerCollectionFactory' => $this->getCustomerCollectionFactoryMock(),
  925. 'eavConfig' => $this->getEavConfigMock(array_merge($firstAttributesBundle, $secondAttributesBundle)),
  926. 'fileUploaderDataResolver' => $this->fileUploaderDataResolver
  927. ]
  928. );
  929. $meta = $dataProvider->getMeta();
  930. $this->assertNotEmpty($meta);
  931. $this->assertEquals($this->getExpectationForVisibleAttributes(), $meta);
  932. }
  933. /**
  934. * @return void
  935. */
  936. public function testGetDataWithVisibleAttributesWithAccountEdit()
  937. {
  938. $firstAttributesBundle = $this->getAttributeMock(
  939. 'customer',
  940. [
  941. self::ATTRIBUTE_CODE => [
  942. 'visible' => true,
  943. 'is_used_in_forms' => ['customer_account_edit'],
  944. 'user_defined' => true,
  945. 'specific_code_prefix' => "_1"
  946. ],
  947. 'test-code-boolean' => [
  948. 'visible' => true,
  949. 'is_used_in_forms' => ['customer_account_create'],
  950. 'user_defined' => true,
  951. 'specific_code_prefix' => "_1"
  952. ]
  953. ]
  954. );
  955. $secondAttributesBundle = $this->getAttributeMock(
  956. 'customer',
  957. [
  958. self::ATTRIBUTE_CODE => [
  959. 'visible' => true,
  960. 'is_used_in_forms' => ['customer_account_create'],
  961. 'user_defined' => false,
  962. 'specific_code_prefix' => "_2"
  963. ],
  964. 'test-code-boolean' => [
  965. 'visible' => true,
  966. 'is_used_in_forms' => ['customer_account_create'],
  967. 'user_defined' => true,
  968. 'specific_code_prefix' => "_2"
  969. ]
  970. ]
  971. );
  972. $helper = new ObjectManager($this);
  973. $context = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\ContextInterface::class)
  974. ->setMethods(['getRequestParam'])
  975. ->getMockForAbstractClass();
  976. $context->expects($this->any())
  977. ->method('getRequestParam')
  978. ->with('request-field-name')
  979. ->willReturn(1);
  980. /** @var \Magento\Customer\Model\Customer\DataProvider $dataProvider */
  981. $dataProvider = $helper->getObject(
  982. \Magento\Customer\Model\Customer\DataProvider::class,
  983. [
  984. 'name' => 'test-name',
  985. 'primaryFieldName' => 'primary-field-name',
  986. 'requestFieldName' => 'request-field-name',
  987. 'eavValidationRules' => $this->eavValidationRulesMock,
  988. 'customerCollectionFactory' => $this->getCustomerCollectionFactoryMock(),
  989. 'context' => $context,
  990. 'eavConfig' => $this->getEavConfigMock(array_merge($firstAttributesBundle, $secondAttributesBundle)),
  991. 'fileUploaderDataResolver' => $this->fileUploaderDataResolver
  992. ]
  993. );
  994. $meta = $dataProvider->getMeta();
  995. $this->assertNotEmpty($meta);
  996. $this->assertEquals($this->getExpectationForVisibleAttributes(false), $meta);
  997. }
  998. /**
  999. * Retrieve all customer variations of attributes with all variations of visibility
  1000. *
  1001. * @param bool $isRegistration
  1002. * @return array
  1003. */
  1004. private function getCustomerAttributeExpectations($isRegistration)
  1005. {
  1006. return [
  1007. self::ATTRIBUTE_CODE . "_1" => [
  1008. 'arguments' => [
  1009. 'data' => [
  1010. 'config' => [
  1011. 'dataType' => 'frontend_input',
  1012. 'formElement' => 'frontend_input',
  1013. 'options' => 'test-options',
  1014. 'visible' => !$isRegistration,
  1015. 'required' => 'is_required',
  1016. 'label' => __('frontend_label'),
  1017. 'sortOrder' => 'sort_order',
  1018. 'notice' => 'note',
  1019. 'default' => 'default_value',
  1020. 'size' => 'multiline_count',
  1021. 'componentType' => Field::NAME,
  1022. ],
  1023. ],
  1024. ],
  1025. ],
  1026. self::ATTRIBUTE_CODE . "_2" => [
  1027. 'arguments' => [
  1028. 'data' => [
  1029. 'config' => [
  1030. 'dataType' => 'frontend_input',
  1031. 'formElement' => 'frontend_input',
  1032. 'options' => 'test-options',
  1033. 'visible' => true,
  1034. 'required' => 'is_required',
  1035. 'label' => __('frontend_label'),
  1036. 'sortOrder' => 'sort_order',
  1037. 'notice' => 'note',
  1038. 'default' => 'default_value',
  1039. 'size' => 'multiline_count',
  1040. 'componentType' => Field::NAME,
  1041. ],
  1042. ],
  1043. ],
  1044. ],
  1045. 'test-code-boolean_1' => [
  1046. 'arguments' => [
  1047. 'data' => [
  1048. 'config' => [
  1049. 'dataType' => 'frontend_input',
  1050. 'formElement' => 'frontend_input',
  1051. 'visible' => $isRegistration,
  1052. 'required' => 'is_required',
  1053. 'label' => __('frontend_label'),
  1054. 'sortOrder' => 'sort_order',
  1055. 'notice' => 'note',
  1056. 'default' => 'default_value',
  1057. 'size' => 'multiline_count',
  1058. 'componentType' => Field::NAME,
  1059. 'prefer' => 'toggle',
  1060. 'valueMap' => [
  1061. 'true' => 1,
  1062. 'false' => 0,
  1063. ],
  1064. ],
  1065. ],
  1066. ],
  1067. ],
  1068. 'test-code-boolean_2' => [
  1069. 'arguments' => [
  1070. 'data' => [
  1071. 'config' => [
  1072. 'dataType' => 'frontend_input',
  1073. 'formElement' => 'frontend_input',
  1074. 'visible' => $isRegistration,
  1075. 'required' => 'is_required',
  1076. 'label' => __('frontend_label'),
  1077. 'sortOrder' => 'sort_order',
  1078. 'notice' => 'note',
  1079. 'default' => 'default_value',
  1080. 'size' => 'multiline_count',
  1081. 'componentType' => Field::NAME,
  1082. 'prefer' => 'toggle',
  1083. 'valueMap' => [
  1084. 'true' => 1,
  1085. 'false' => 0,
  1086. ],
  1087. ],
  1088. ],
  1089. ],
  1090. ],
  1091. ];
  1092. }
  1093. /**
  1094. * Retrieve all variations of attributes with all variations of visibility
  1095. *
  1096. * @param bool $isRegistration
  1097. * @return array
  1098. */
  1099. private function getExpectationForVisibleAttributes($isRegistration = true)
  1100. {
  1101. return [
  1102. 'customer' => [
  1103. 'children' => $this->getCustomerAttributeExpectations($isRegistration),
  1104. ],
  1105. 'address' => [
  1106. 'children' => [
  1107. self::ATTRIBUTE_CODE => [
  1108. 'arguments' => [
  1109. 'data' => [
  1110. 'config' => [
  1111. 'dataType' => 'frontend_input',
  1112. 'formElement' => 'frontend_input',
  1113. 'options' => 'test-options',
  1114. 'visible' => null,
  1115. 'required' => 'is_required',
  1116. 'label' => __('frontend_label'),
  1117. 'sortOrder' => 'sort_order',
  1118. 'notice' => 'note',
  1119. 'default' => 'default_value',
  1120. 'size' => 'multiline_count',
  1121. 'componentType' => Field::NAME,
  1122. ],
  1123. ],
  1124. ],
  1125. ],
  1126. 'test-code-boolean' => [
  1127. 'arguments' => [
  1128. 'data' => [
  1129. 'config' => [
  1130. 'dataType' => 'frontend_input',
  1131. 'formElement' => 'frontend_input',
  1132. 'visible' => null,
  1133. 'required' => 'is_required',
  1134. 'label' => 'frontend_label',
  1135. 'sortOrder' => 'sort_order',
  1136. 'notice' => 'note',
  1137. 'default' => 'default_value',
  1138. 'size' => 'multiline_count',
  1139. 'componentType' => Field::NAME,
  1140. 'prefer' => 'toggle',
  1141. 'valueMap' => [
  1142. 'true' => 1,
  1143. 'false' => 0,
  1144. ],
  1145. ],
  1146. ],
  1147. ],
  1148. ],
  1149. 'country_id' => [
  1150. 'arguments' => [
  1151. 'data' => [
  1152. 'config' => [
  1153. 'dataType' => 'frontend_input',
  1154. 'formElement' => 'frontend_input',
  1155. 'options' => 'test-options',
  1156. 'visible' => null,
  1157. 'required' => 'is_required',
  1158. 'label' => __('frontend_label'),
  1159. 'sortOrder' => 'sort_order',
  1160. 'notice' => 'note',
  1161. 'default' => 'default_value',
  1162. 'size' => 'multiline_count',
  1163. 'componentType' => Field::NAME,
  1164. 'filterBy' => [
  1165. 'target' => '${ $.provider }:data.customer.website_id',
  1166. 'field' => 'website_ids'
  1167. ]
  1168. ],
  1169. ],
  1170. ],
  1171. ]
  1172. ],
  1173. ],
  1174. ];
  1175. }
  1176. }