SaveTest.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Controller\Adminhtml\Index;
  7. use Magento\Customer\Api\CustomerMetadataInterface;
  8. use Magento\Customer\Api\Data\AttributeMetadataInterface;
  9. use Magento\Customer\Api\Data\CustomerInterface;
  10. use Magento\Customer\Controller\RegistryConstants;
  11. use Magento\Customer\Model\EmailNotificationInterface;
  12. use Magento\Customer\Model\Metadata\Form;
  13. use Magento\Framework\Controller\Result\Redirect;
  14. /**
  15. * Testing Save Customer use case from admin page
  16. *
  17. * @SuppressWarnings(PHPMD.TooManyFields)
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. * @covers \Magento\Customer\Controller\Adminhtml\Index\Save
  20. */
  21. class SaveTest extends \PHPUnit\Framework\TestCase
  22. {
  23. /**
  24. * @var \Magento\Customer\Controller\Adminhtml\Index\Save
  25. */
  26. protected $model;
  27. /**
  28. * @var \Magento\Backend\App\Action\Context
  29. */
  30. protected $context;
  31. /**
  32. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $requestMock;
  35. /**
  36. * @var \Magento\Backend\Model\View\Result\ForwardFactory|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $resultForwardFactoryMock;
  39. /**
  40. * @var \Magento\Backend\Model\View\Result\Forward|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $resultForwardMock;
  43. /**
  44. * @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $resultPageFactoryMock;
  47. /**
  48. * @var \Magento\Framework\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $resultPageMock;
  51. /**
  52. * @var \Magento\Framework\View\Page\Config|\PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $pageConfigMock;
  55. /**
  56. * @var \Magento\Framework\View\Page\Title|\PHPUnit_Framework_MockObject_MockObject
  57. */
  58. protected $pageTitleMock;
  59. /**
  60. * @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  61. */
  62. protected $sessionMock;
  63. /**
  64. * @var \Magento\Customer\Model\Metadata\FormFactory|\PHPUnit_Framework_MockObject_MockObject
  65. */
  66. protected $formFactoryMock;
  67. /**
  68. * @var \Magento\Framework\DataObjectFactory|\PHPUnit_Framework_MockObject_MockObject
  69. */
  70. protected $objectFactoryMock;
  71. /**
  72. * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  73. */
  74. protected $customerDataFactoryMock;
  75. /**
  76. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  77. */
  78. protected $customerRepositoryMock;
  79. /**
  80. * @var \Magento\Customer\Model\Customer\Mapper|\PHPUnit_Framework_MockObject_MockObject
  81. */
  82. protected $customerMapperMock;
  83. /**
  84. * @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
  85. */
  86. protected $dataHelperMock;
  87. /**
  88. * @var \Magento\Framework\AuthorizationInterface|\PHPUnit_Framework_MockObject_MockObject
  89. */
  90. protected $authorizationMock;
  91. /**
  92. * @var \Magento\Newsletter\Model\SubscriberFactory|\PHPUnit_Framework_MockObject_MockObject
  93. */
  94. protected $subscriberFactoryMock;
  95. /**
  96. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  97. */
  98. protected $registryMock;
  99. /**
  100. * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  101. */
  102. protected $messageManagerMock;
  103. /**
  104. * @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
  105. */
  106. protected $redirectFactoryMock;
  107. /**
  108. * @var \Magento\Customer\Model\AccountManagement|\PHPUnit_Framework_MockObject_MockObject
  109. */
  110. protected $managementMock;
  111. /**
  112. * @var \Magento\Customer\Api\Data\AddressInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  113. */
  114. protected $addressDataFactoryMock;
  115. /**
  116. * @var EmailNotificationInterface|\PHPUnit_Framework_MockObject_MockObject
  117. */
  118. protected $emailNotificationMock;
  119. /**
  120. * @var \Magento\Customer\Model\Address\Mapper|\PHPUnit_Framework_MockObject_MockObject
  121. */
  122. protected $customerAddressMapperMock;
  123. /**
  124. * @var \Magento\Customer\Api\AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  125. */
  126. protected $customerAddressRepositoryMock;
  127. /**
  128. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  129. */
  130. protected function setUp()
  131. {
  132. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  133. ->disableOriginalConstructor()
  134. ->getMock();
  135. $this->resultForwardFactoryMock = $this->getMockBuilder(
  136. \Magento\Backend\Model\View\Result\ForwardFactory::class
  137. )->disableOriginalConstructor()
  138. ->setMethods(['create'])
  139. ->getMock();
  140. $this->resultForwardMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class)
  141. ->disableOriginalConstructor()
  142. ->getMock();
  143. $this->resultPageFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
  144. ->disableOriginalConstructor()
  145. ->getMock();
  146. $this->resultPageMock = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
  147. ->disableOriginalConstructor()
  148. ->setMethods(['setActiveMenu', 'getConfig', 'addBreadcrumb'])
  149. ->getMock();
  150. $this->pageConfigMock = $this->getMockBuilder(\Magento\Framework\View\Page\Config::class)
  151. ->disableOriginalConstructor()
  152. ->getMock();
  153. $this->pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
  154. ->disableOriginalConstructor()
  155. ->getMock();
  156. $this->sessionMock = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
  157. ->disableOriginalConstructor()
  158. ->setMethods(['unsCustomerFormData', 'setCustomerFormData'])
  159. ->getMock();
  160. $this->formFactoryMock = $this->getMockBuilder(\Magento\Customer\Model\Metadata\FormFactory::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $this->objectFactoryMock = $this->getMockBuilder(\Magento\Framework\DataObjectFactory::class)
  164. ->disableOriginalConstructor()
  165. ->setMethods(['create'])
  166. ->getMock();
  167. $this->customerDataFactoryMock = $this->getMockBuilder(
  168. \Magento\Customer\Api\Data\CustomerInterfaceFactory::class
  169. )->disableOriginalConstructor()
  170. ->setMethods(['create'])
  171. ->getMock();
  172. $this->customerRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class)
  173. ->disableOriginalConstructor()
  174. ->getMock();
  175. $this->customerAddressRepositoryMock = $this->getMockBuilder(
  176. \Magento\Customer\Api\AddressRepositoryInterface::class
  177. )->disableOriginalConstructor()->getMock();
  178. $this->customerMapperMock = $this->getMockBuilder(
  179. \Magento\Customer\Model\Customer\Mapper::class
  180. )->disableOriginalConstructor()->getMock();
  181. $this->customerAddressMapperMock = $this->getMockBuilder(
  182. \Magento\Customer\Model\Address\Mapper::class
  183. )->disableOriginalConstructor()->getMock();
  184. $this->dataHelperMock = $this->getMockBuilder(
  185. \Magento\Framework\Api\DataObjectHelper::class
  186. )->disableOriginalConstructor()->getMock();
  187. $this->authorizationMock = $this->getMockBuilder(\Magento\Framework\AuthorizationInterface::class)
  188. ->disableOriginalConstructor()
  189. ->getMock();
  190. $this->subscriberFactoryMock = $this->getMockBuilder(\Magento\Newsletter\Model\SubscriberFactory::class)
  191. ->disableOriginalConstructor()
  192. ->setMethods(['create'])
  193. ->getMock();
  194. $this->registryMock = $this->getMockBuilder(\Magento\Framework\Registry::class)
  195. ->disableOriginalConstructor()
  196. ->getMock();
  197. $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
  198. ->disableOriginalConstructor()
  199. ->getMock();
  200. $this->redirectFactoryMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\RedirectFactory::class)
  201. ->disableOriginalConstructor()
  202. ->setMethods(['create'])
  203. ->getMock();
  204. $this->managementMock = $this->getMockBuilder(\Magento\Customer\Model\AccountManagement::class)
  205. ->disableOriginalConstructor()
  206. ->setMethods(['createAccount'])
  207. ->getMock();
  208. $this->addressDataFactoryMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterfaceFactory::class)
  209. ->disableOriginalConstructor()
  210. ->setMethods(['create'])
  211. ->getMock();
  212. $this->emailNotificationMock = $this->getMockBuilder(EmailNotificationInterface::class)
  213. ->disableOriginalConstructor()
  214. ->getMock();
  215. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  216. $this->model = $objectManager->getObject(
  217. \Magento\Customer\Controller\Adminhtml\Index\Save::class,
  218. [
  219. 'resultForwardFactory' => $this->resultForwardFactoryMock,
  220. 'resultPageFactory' => $this->resultPageFactoryMock,
  221. 'formFactory' => $this->formFactoryMock,
  222. 'objectFactory' => $this->objectFactoryMock,
  223. 'customerDataFactory' => $this->customerDataFactoryMock,
  224. 'customerRepository' => $this->customerRepositoryMock,
  225. 'customerMapper' => $this->customerMapperMock,
  226. 'dataObjectHelper' => $this->dataHelperMock,
  227. 'subscriberFactory' => $this->subscriberFactoryMock,
  228. 'coreRegistry' => $this->registryMock,
  229. 'customerAccountManagement' => $this->managementMock,
  230. 'addressDataFactory' => $this->addressDataFactoryMock,
  231. 'request' => $this->requestMock,
  232. 'session' => $this->sessionMock,
  233. 'authorization' => $this->authorizationMock,
  234. 'messageManager' => $this->messageManagerMock,
  235. 'resultRedirectFactory' => $this->redirectFactoryMock,
  236. 'addressRepository' => $this->customerAddressRepositoryMock,
  237. 'addressMapper' => $this->customerAddressMapperMock,
  238. ]
  239. );
  240. $objectManager->setBackwardCompatibleProperty(
  241. $this->model,
  242. 'emailNotification',
  243. $this->emailNotificationMock
  244. );
  245. }
  246. /**
  247. * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
  248. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  249. */
  250. public function testExecuteWithExistentCustomer()
  251. {
  252. $customerId = 22;
  253. $subscription = 'true';
  254. $postValue = [
  255. 'customer' => [
  256. 'entity_id' => $customerId,
  257. 'code' => 'value',
  258. 'coolness' => false,
  259. 'disable_auto_group_change' => 'false',
  260. ],
  261. 'subscription' => $subscription,
  262. ];
  263. $extractedData = [
  264. 'entity_id' => $customerId,
  265. 'code' => 'value',
  266. 'coolness' => false,
  267. 'disable_auto_group_change' => 'false',
  268. ];
  269. $compactedData = [
  270. 'entity_id' => $customerId,
  271. 'code' => 'value',
  272. 'coolness' => false,
  273. 'disable_auto_group_change' => 'false',
  274. CustomerInterface::DEFAULT_BILLING => 2,
  275. CustomerInterface::DEFAULT_SHIPPING => 2
  276. ];
  277. $savedData = [
  278. 'entity_id' => $customerId,
  279. 'darkness' => true,
  280. 'name' => 'Name',
  281. CustomerInterface::DEFAULT_BILLING => false,
  282. CustomerInterface::DEFAULT_SHIPPING => false,
  283. ];
  284. $mergedData = [
  285. 'entity_id' => $customerId,
  286. 'darkness' => true,
  287. 'name' => 'Name',
  288. 'code' => 'value',
  289. 'disable_auto_group_change' => 0,
  290. 'confirmation' => false,
  291. 'sendemail_store_id' => '1',
  292. 'id' => $customerId,
  293. ];
  294. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $customerFormMock */
  295. $attributeMock = $this->getMockBuilder(
  296. \Magento\Customer\Api\Data\AttributeMetadataInterface::class
  297. )->disableOriginalConstructor()->getMock();
  298. $attributeMock->expects($this->atLeastOnce())
  299. ->method('getAttributeCode')
  300. ->willReturn('coolness');
  301. $attributeMock->expects($this->atLeastOnce())
  302. ->method('getFrontendInput')
  303. ->willReturn('int');
  304. $attributes = [$attributeMock];
  305. $this->requestMock->expects($this->atLeastOnce())
  306. ->method('getPostValue')
  307. ->willReturnMap([
  308. [null, null, $postValue],
  309. [CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, null, $postValue['customer']],
  310. ]);
  311. $this->requestMock->expects($this->atLeastOnce())
  312. ->method('getPost')
  313. ->willReturnMap(
  314. [
  315. ['customer', null, $postValue['customer']],
  316. ['subscription', null, $subscription],
  317. ]
  318. );
  319. /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */
  320. $objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  321. ->disableOriginalConstructor()
  322. ->getMock();
  323. $objectMock->expects($this->atLeastOnce())
  324. ->method('getData')
  325. ->willReturnMap(
  326. [
  327. ['customer', null, $postValue['customer']],
  328. ]
  329. );
  330. $this->objectFactoryMock->expects($this->exactly(1))
  331. ->method('create')
  332. ->with(['data' => $postValue])
  333. ->willReturn($objectMock);
  334. $customerFormMock = $this->getMockBuilder(
  335. \Magento\Customer\Model\Metadata\Form::class
  336. )->disableOriginalConstructor()->getMock();
  337. $customerFormMock->expects($this->once())
  338. ->method('extractData')
  339. ->with($this->requestMock, 'customer')
  340. ->willReturn($extractedData);
  341. $customerFormMock->expects($this->once())
  342. ->method('compactData')
  343. ->with($extractedData)
  344. ->willReturn($compactedData);
  345. $customerFormMock->expects($this->once())
  346. ->method('getAttributes')
  347. ->willReturn($attributes);
  348. $this->formFactoryMock->expects($this->exactly(1))
  349. ->method('create')
  350. ->willReturnMap(
  351. [
  352. [
  353. CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
  354. 'adminhtml_customer',
  355. $savedData,
  356. false,
  357. Form::DONT_IGNORE_INVISIBLE,
  358. [],
  359. $customerFormMock
  360. ],
  361. ]
  362. );
  363. /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */
  364. $customerMock = $this->getMockBuilder(
  365. \Magento\Customer\Api\Data\CustomerInterface::class
  366. )->disableOriginalConstructor()->getMock();
  367. $this->customerDataFactoryMock->expects($this->once())
  368. ->method('create')
  369. ->willReturn($customerMock);
  370. $this->customerRepositoryMock->expects($this->exactly(2))
  371. ->method('getById')
  372. ->with($customerId)
  373. ->willReturn($customerMock);
  374. $this->customerMapperMock->expects($this->exactly(2))
  375. ->method('toFlatArray')
  376. ->with($customerMock)
  377. ->willReturn($savedData);
  378. $this->dataHelperMock->expects($this->atLeastOnce())
  379. ->method('populateWithArray')
  380. ->willReturnMap(
  381. [
  382. [
  383. $customerMock,
  384. $mergedData, \Magento\Customer\Api\Data\CustomerInterface::class,
  385. $this->dataHelperMock
  386. ],
  387. ]
  388. );
  389. $this->customerRepositoryMock->expects($this->once())
  390. ->method('save')
  391. ->with($customerMock)
  392. ->willReturnSelf();
  393. $customerEmail = 'customer@email.com';
  394. $customerMock->expects($this->once())->method('getEmail')->willReturn($customerEmail);
  395. $customerMock->expects($this->once())
  396. ->method('getAddresses')
  397. ->willReturn([]);
  398. $this->emailNotificationMock->expects($this->once())
  399. ->method('credentialsChanged')
  400. ->with($customerMock, $customerEmail)
  401. ->willReturnSelf();
  402. $this->authorizationMock->expects($this->once())
  403. ->method('isAllowed')
  404. ->with(null)
  405. ->willReturn(true);
  406. /** @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject $subscriberMock */
  407. $subscriberMock = $this->getMockBuilder(\Magento\Newsletter\Model\Subscriber::class)
  408. ->disableOriginalConstructor()
  409. ->getMock();
  410. $this->subscriberFactoryMock->expects($this->once())
  411. ->method('create')
  412. ->with()
  413. ->willReturn($subscriberMock);
  414. $subscriberMock->expects($this->once())
  415. ->method('subscribeCustomerById')
  416. ->with($customerId);
  417. $subscriberMock->expects($this->never())
  418. ->method('unsubscribeCustomerById');
  419. $this->sessionMock->expects($this->once())
  420. ->method('unsCustomerFormData');
  421. $this->registryMock->expects($this->once())
  422. ->method('register')
  423. ->with(RegistryConstants::CURRENT_CUSTOMER_ID, $customerId);
  424. $this->messageManagerMock->expects($this->once())
  425. ->method('addSuccess')
  426. ->with(__('You saved the customer.'))
  427. ->willReturnSelf();
  428. $this->requestMock->expects($this->once())
  429. ->method('getParam')
  430. ->with('back', false)
  431. ->willReturn(true);
  432. /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
  433. $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
  434. ->disableOriginalConstructor()
  435. ->getMock();
  436. $this->redirectFactoryMock->expects($this->once())
  437. ->method('create')
  438. ->with([])
  439. ->willReturn($redirectMock);
  440. $redirectMock->expects($this->once())
  441. ->method('setPath')
  442. ->with('customer/*/edit', ['id' => $customerId, '_current' => true])
  443. ->willReturn(true);
  444. $this->assertEquals($redirectMock, $this->model->execute());
  445. }
  446. /**
  447. * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
  448. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  449. */
  450. public function testExecuteWithNewCustomer()
  451. {
  452. $customerId = 22;
  453. $subscription = '0';
  454. $postValue = [
  455. 'customer' => [
  456. 'coolness' => false,
  457. 'disable_auto_group_change' => 'false',
  458. ],
  459. 'subscription' => $subscription,
  460. ];
  461. $extractedData = [
  462. 'coolness' => false,
  463. 'disable_auto_group_change' => 'false',
  464. ];
  465. $mergedData = [
  466. 'disable_auto_group_change' => 0,
  467. CustomerInterface::DEFAULT_BILLING => null,
  468. CustomerInterface::DEFAULT_SHIPPING => null,
  469. 'confirmation' => false,
  470. ];
  471. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $customerFormMock */
  472. $attributeMock = $this->getMockBuilder(
  473. \Magento\Customer\Api\Data\AttributeMetadataInterface::class
  474. )->disableOriginalConstructor()->getMock();
  475. $attributeMock->expects($this->atLeastOnce())
  476. ->method('getAttributeCode')
  477. ->willReturn('coolness');
  478. $attributeMock->expects($this->atLeastOnce())
  479. ->method('getFrontendInput')
  480. ->willReturn('int');
  481. $attributes = [$attributeMock];
  482. $this->requestMock->expects($this->any())
  483. ->method('getPostValue')
  484. ->willReturnMap([
  485. [null, null, $postValue],
  486. [CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, null, $postValue['customer']],
  487. ]);
  488. $this->requestMock->expects($this->atLeastOnce())
  489. ->method('getPost')
  490. ->willReturnMap(
  491. [
  492. ['customer', null, $postValue['customer']],
  493. ['subscription', null, $subscription],
  494. ]
  495. );
  496. /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */
  497. $objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  498. ->disableOriginalConstructor()
  499. ->getMock();
  500. $objectMock->expects($this->atLeastOnce())
  501. ->method('getData')
  502. ->willReturnMap(
  503. [
  504. ['customer', null, $postValue['customer']],
  505. ]
  506. );
  507. $this->objectFactoryMock->expects($this->atLeastOnce())
  508. ->method('create')
  509. ->with(['data' => $postValue])
  510. ->willReturn($objectMock);
  511. $customerFormMock = $this->getMockBuilder(
  512. \Magento\Customer\Model\Metadata\Form::class
  513. )->disableOriginalConstructor()->getMock();
  514. $customerFormMock->expects($this->once())
  515. ->method('extractData')
  516. ->with($this->requestMock, 'customer')
  517. ->willReturn($extractedData);
  518. $customerFormMock->expects($this->once())
  519. ->method('compactData')
  520. ->with($extractedData)
  521. ->willReturn($extractedData);
  522. $customerFormMock->expects($this->once())
  523. ->method('getAttributes')
  524. ->willReturn($attributes);
  525. $this->formFactoryMock->expects($this->exactly(1))
  526. ->method('create')
  527. ->willReturnMap(
  528. [
  529. [
  530. CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
  531. 'adminhtml_customer',
  532. [],
  533. false,
  534. Form::DONT_IGNORE_INVISIBLE,
  535. [],
  536. $customerFormMock
  537. ],
  538. ]
  539. );
  540. /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */
  541. $customerMock = $this->getMockBuilder(
  542. \Magento\Customer\Api\Data\CustomerInterface::class
  543. )->disableOriginalConstructor()->getMock();
  544. $this->customerDataFactoryMock->expects($this->once())
  545. ->method('create')
  546. ->willReturn($customerMock);
  547. $this->dataHelperMock->expects($this->atLeastOnce())
  548. ->method('populateWithArray')
  549. ->willReturnMap(
  550. [
  551. [
  552. $customerMock,
  553. $mergedData, \Magento\Customer\Api\Data\CustomerInterface::class,
  554. $this->dataHelperMock
  555. ],
  556. ]
  557. );
  558. $this->managementMock->expects($this->once())
  559. ->method('createAccount')
  560. ->with($customerMock, null, '')
  561. ->willReturn($customerMock);
  562. $customerMock->expects($this->once())
  563. ->method('getId')
  564. ->willReturn($customerId);
  565. $this->authorizationMock->expects($this->once())
  566. ->method('isAllowed')
  567. ->with(null)
  568. ->willReturn(true);
  569. /** @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject $subscriberMock */
  570. $subscriberMock = $this->getMockBuilder(\Magento\Newsletter\Model\Subscriber::class)
  571. ->disableOriginalConstructor()
  572. ->getMock();
  573. $this->subscriberFactoryMock->expects($this->once())
  574. ->method('create')
  575. ->with()
  576. ->willReturn($subscriberMock);
  577. $subscriberMock->expects($this->once())
  578. ->method('unsubscribeCustomerById')
  579. ->with($customerId);
  580. $subscriberMock->expects($this->never())
  581. ->method('subscribeCustomerById');
  582. $this->sessionMock->expects($this->once())
  583. ->method('unsCustomerFormData');
  584. $this->registryMock->expects($this->once())
  585. ->method('register')
  586. ->with(RegistryConstants::CURRENT_CUSTOMER_ID, $customerId);
  587. $this->messageManagerMock->expects($this->once())
  588. ->method('addSuccess')
  589. ->with(__('You saved the customer.'))
  590. ->willReturnSelf();
  591. $this->requestMock->expects($this->once())
  592. ->method('getParam')
  593. ->with('back', false)
  594. ->willReturn(false);
  595. /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
  596. $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
  597. ->disableOriginalConstructor()
  598. ->getMock();
  599. $this->redirectFactoryMock->expects($this->once())
  600. ->method('create')
  601. ->willReturn($redirectMock);
  602. $redirectMock->expects($this->once())
  603. ->method('setPath')
  604. ->with('customer/index', [])
  605. ->willReturnSelf();
  606. $this->assertEquals($redirectMock, $this->model->execute());
  607. }
  608. /**
  609. * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
  610. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  611. */
  612. public function testExecuteWithNewCustomerAndValidationException()
  613. {
  614. $subscription = '0';
  615. $postValue = [
  616. 'customer' => [
  617. 'coolness' => false,
  618. 'disable_auto_group_change' => 'false',
  619. ],
  620. 'subscription' => $subscription,
  621. ];
  622. $extractedData = [
  623. 'coolness' => false,
  624. 'disable_auto_group_change' => 'false',
  625. ];
  626. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $customerFormMock */
  627. $attributeMock = $this->getMockBuilder(
  628. \Magento\Customer\Api\Data\AttributeMetadataInterface::class
  629. )->disableOriginalConstructor()->getMock();
  630. $attributeMock->expects($this->once())
  631. ->method('getAttributeCode')
  632. ->willReturn('coolness');
  633. $attributeMock->expects($this->once())
  634. ->method('getFrontendInput')
  635. ->willReturn('int');
  636. $attributes = [$attributeMock];
  637. $this->requestMock->expects($this->any())
  638. ->method('getPostValue')
  639. ->willReturnMap([
  640. [null, null, $postValue],
  641. [CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, null, $postValue['customer']],
  642. ]);
  643. $this->requestMock->expects($this->atLeastOnce())
  644. ->method('getPost')
  645. ->willReturnMap(
  646. [
  647. ['customer', null, $postValue['customer']],
  648. ]
  649. );
  650. /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */
  651. $objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  652. ->disableOriginalConstructor()
  653. ->getMock();
  654. $objectMock->expects($this->once())
  655. ->method('getData')
  656. ->with('customer')
  657. ->willReturn($postValue['customer']);
  658. $this->objectFactoryMock->expects($this->once())
  659. ->method('create')
  660. ->with(['data' => $postValue])
  661. ->willReturn($objectMock);
  662. $customerFormMock = $this->getMockBuilder(
  663. \Magento\Customer\Model\Metadata\Form::class
  664. )->disableOriginalConstructor()->getMock();
  665. $customerFormMock->expects($this->once())
  666. ->method('extractData')
  667. ->with($this->requestMock, 'customer')
  668. ->willReturn($extractedData);
  669. $customerFormMock->expects($this->once())
  670. ->method('compactData')
  671. ->with($extractedData)
  672. ->willReturn($extractedData);
  673. $customerFormMock->expects($this->once())
  674. ->method('getAttributes')
  675. ->willReturn($attributes);
  676. $this->formFactoryMock->expects($this->once())
  677. ->method('create')
  678. ->with(
  679. CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
  680. 'adminhtml_customer',
  681. [],
  682. false,
  683. Form::DONT_IGNORE_INVISIBLE
  684. )->willReturn($customerFormMock);
  685. /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */
  686. $customerMock = $this->getMockBuilder(
  687. \Magento\Customer\Api\Data\CustomerInterface::class
  688. )->disableOriginalConstructor()->getMock();
  689. $this->customerDataFactoryMock->expects($this->once())
  690. ->method('create')
  691. ->willReturn($customerMock);
  692. $this->managementMock->expects($this->once())
  693. ->method('createAccount')
  694. ->with($customerMock, null, '')
  695. ->willThrowException(new \Magento\Framework\Validator\Exception(__('Validator Exception')));
  696. $customerMock->expects($this->never())
  697. ->method('getId');
  698. $this->authorizationMock->expects($this->never())
  699. ->method('isAllowed');
  700. $this->subscriberFactoryMock->expects($this->never())
  701. ->method('create');
  702. $this->sessionMock->expects($this->never())
  703. ->method('unsCustomerFormData');
  704. $this->registryMock->expects($this->never())
  705. ->method('register');
  706. $this->messageManagerMock->expects($this->never())
  707. ->method('addSuccess');
  708. $this->messageManagerMock->expects($this->once())
  709. ->method('addMessage')
  710. ->with(new \Magento\Framework\Message\Error('Validator Exception'));
  711. $this->sessionMock->expects($this->once())
  712. ->method('setCustomerFormData')
  713. ->with($postValue);
  714. /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
  715. $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
  716. ->disableOriginalConstructor()
  717. ->getMock();
  718. $this->redirectFactoryMock->expects($this->once())
  719. ->method('create')
  720. ->with([])
  721. ->willReturn($redirectMock);
  722. $redirectMock->expects($this->once())
  723. ->method('setPath')
  724. ->with('customer/*/new', ['_current' => true])
  725. ->willReturn(true);
  726. $this->assertEquals($redirectMock, $this->model->execute());
  727. }
  728. /**
  729. * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
  730. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  731. */
  732. public function testExecuteWithNewCustomerAndLocalizedException()
  733. {
  734. $subscription = '0';
  735. $postValue = [
  736. 'customer' => [
  737. 'coolness' => false,
  738. 'disable_auto_group_change' => 'false',
  739. ],
  740. 'subscription' => $subscription,
  741. ];
  742. $extractedData = [
  743. 'coolness' => false,
  744. 'disable_auto_group_change' => 'false',
  745. ];
  746. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $customerFormMock */
  747. $attributeMock = $this->getMockBuilder(
  748. \Magento\Customer\Api\Data\AttributeMetadataInterface::class
  749. )->disableOriginalConstructor()->getMock();
  750. $attributeMock->expects($this->once())
  751. ->method('getAttributeCode')
  752. ->willReturn('coolness');
  753. $attributeMock->expects($this->once())
  754. ->method('getFrontendInput')
  755. ->willReturn('int');
  756. $attributes = [$attributeMock];
  757. $this->requestMock->expects($this->any())
  758. ->method('getPostValue')
  759. ->willReturnMap([
  760. [null, null, $postValue],
  761. [CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, null, $postValue['customer']],
  762. ]);
  763. $this->requestMock->expects($this->atLeastOnce())
  764. ->method('getPost')
  765. ->willReturnMap(
  766. [
  767. ['customer', null, $postValue['customer']],
  768. ]
  769. );
  770. /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */
  771. $objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  772. ->disableOriginalConstructor()
  773. ->getMock();
  774. $objectMock->expects($this->once())
  775. ->method('getData')
  776. ->with('customer')
  777. ->willReturn($postValue['customer']);
  778. $this->objectFactoryMock->expects($this->once())
  779. ->method('create')
  780. ->with(['data' => $postValue])
  781. ->willReturn($objectMock);
  782. /** @var Form|\PHPUnit_Framework_MockObject_MockObject $formMock */
  783. $customerFormMock = $this->getMockBuilder(
  784. \Magento\Customer\Model\Metadata\Form::class
  785. )->disableOriginalConstructor()->getMock();
  786. $customerFormMock->expects($this->once())
  787. ->method('extractData')
  788. ->with($this->requestMock, 'customer')
  789. ->willReturn($extractedData);
  790. $customerFormMock->expects($this->once())
  791. ->method('compactData')
  792. ->with($extractedData)
  793. ->willReturn($extractedData);
  794. $customerFormMock->expects($this->once())
  795. ->method('getAttributes')
  796. ->willReturn($attributes);
  797. $this->formFactoryMock->expects($this->once())
  798. ->method('create')
  799. ->with(
  800. CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
  801. 'adminhtml_customer',
  802. [],
  803. false,
  804. Form::DONT_IGNORE_INVISIBLE
  805. )->willReturn($customerFormMock);
  806. $customerMock = $this->getMockBuilder(
  807. \Magento\Customer\Api\Data\CustomerInterface::class
  808. )->disableOriginalConstructor()->getMock();
  809. $this->customerDataFactoryMock->expects($this->once())
  810. ->method('create')
  811. ->willReturn($customerMock);
  812. $this->managementMock->expects($this->once())
  813. ->method('createAccount')
  814. ->with($customerMock, null, '')
  815. ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('Localized Exception')));
  816. $customerMock->expects($this->never())
  817. ->method('getId');
  818. $this->authorizationMock->expects($this->never())
  819. ->method('isAllowed');
  820. $this->subscriberFactoryMock->expects($this->never())
  821. ->method('create');
  822. $this->sessionMock->expects($this->never())
  823. ->method('unsCustomerFormData');
  824. $this->registryMock->expects($this->never())
  825. ->method('register');
  826. $this->messageManagerMock->expects($this->never())
  827. ->method('addSuccess');
  828. $this->messageManagerMock->expects($this->once())
  829. ->method('addMessage')
  830. ->with(new \Magento\Framework\Message\Error('Localized Exception'));
  831. $this->sessionMock->expects($this->once())
  832. ->method('setCustomerFormData')
  833. ->with($postValue);
  834. /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
  835. $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
  836. ->disableOriginalConstructor()
  837. ->getMock();
  838. $this->redirectFactoryMock->expects($this->once())
  839. ->method('create')
  840. ->with([])
  841. ->willReturn($redirectMock);
  842. $redirectMock->expects($this->once())
  843. ->method('setPath')
  844. ->with('customer/*/new', ['_current' => true])
  845. ->willReturn(true);
  846. $this->assertEquals($redirectMock, $this->model->execute());
  847. }
  848. /**
  849. * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
  850. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  851. */
  852. public function testExecuteWithNewCustomerAndException()
  853. {
  854. $subscription = '0';
  855. $postValue = [
  856. 'customer' => [
  857. 'coolness' => false,
  858. 'disable_auto_group_change' => 'false',
  859. ],
  860. 'subscription' => $subscription,
  861. ];
  862. $extractedData = [
  863. 'coolness' => false,
  864. 'disable_auto_group_change' => 'false',
  865. ];
  866. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $customerFormMock */
  867. $attributeMock = $this->getMockBuilder(
  868. \Magento\Customer\Api\Data\AttributeMetadataInterface::class
  869. )->disableOriginalConstructor()->getMock();
  870. $attributeMock->expects($this->once())
  871. ->method('getAttributeCode')
  872. ->willReturn('coolness');
  873. $attributeMock->expects($this->once())
  874. ->method('getFrontendInput')
  875. ->willReturn('int');
  876. $attributes = [$attributeMock];
  877. $this->requestMock->expects($this->any())
  878. ->method('getPostValue')
  879. ->willReturnMap([
  880. [null, null, $postValue],
  881. [CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, null, $postValue['customer']],
  882. ]);
  883. $this->requestMock->expects($this->atLeastOnce())
  884. ->method('getPost')
  885. ->willReturnMap(
  886. [
  887. ['customer', null, $postValue['customer']],
  888. ]
  889. );
  890. /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */
  891. $objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  892. ->disableOriginalConstructor()
  893. ->getMock();
  894. $objectMock->expects($this->once())
  895. ->method('getData')
  896. ->with('customer')
  897. ->willReturn($postValue['customer']);
  898. $this->objectFactoryMock->expects($this->once())
  899. ->method('create')
  900. ->with(['data' => $postValue])
  901. ->willReturn($objectMock);
  902. $customerFormMock = $this->getMockBuilder(
  903. \Magento\Customer\Model\Metadata\Form::class
  904. )->disableOriginalConstructor()->getMock();
  905. $customerFormMock->expects($this->once())
  906. ->method('extractData')
  907. ->with($this->requestMock, 'customer')
  908. ->willReturn($extractedData);
  909. $customerFormMock->expects($this->once())
  910. ->method('compactData')
  911. ->with($extractedData)
  912. ->willReturn($extractedData);
  913. $customerFormMock->expects($this->once())
  914. ->method('getAttributes')
  915. ->willReturn($attributes);
  916. $this->formFactoryMock->expects($this->once())
  917. ->method('create')
  918. ->with(
  919. CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
  920. 'adminhtml_customer',
  921. [],
  922. false,
  923. Form::DONT_IGNORE_INVISIBLE
  924. )->willReturn($customerFormMock);
  925. /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */
  926. $customerMock = $this->getMockBuilder(
  927. \Magento\Customer\Api\Data\CustomerInterface::class
  928. )->disableOriginalConstructor()->getMock();
  929. $this->customerDataFactoryMock->expects($this->once())
  930. ->method('create')
  931. ->willReturn($customerMock);
  932. $exception = new \Exception(__('Exception'));
  933. $this->managementMock->expects($this->once())
  934. ->method('createAccount')
  935. ->with($customerMock, null, '')
  936. ->willThrowException($exception);
  937. $customerMock->expects($this->never())
  938. ->method('getId');
  939. $this->authorizationMock->expects($this->never())
  940. ->method('isAllowed');
  941. $this->subscriberFactoryMock->expects($this->never())
  942. ->method('create');
  943. $this->sessionMock->expects($this->never())
  944. ->method('unsCustomerFormData');
  945. $this->registryMock->expects($this->never())
  946. ->method('register');
  947. $this->messageManagerMock->expects($this->never())
  948. ->method('addSuccess');
  949. $this->messageManagerMock->expects($this->once())
  950. ->method('addException')
  951. ->with($exception, __('Something went wrong while saving the customer.'));
  952. $this->sessionMock->expects($this->once())
  953. ->method('setCustomerFormData')
  954. ->with($postValue);
  955. /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
  956. $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
  957. ->disableOriginalConstructor()
  958. ->getMock();
  959. $this->redirectFactoryMock->expects($this->once())
  960. ->method('create')
  961. ->with([])
  962. ->willReturn($redirectMock);
  963. $redirectMock->expects($this->once())
  964. ->method('setPath')
  965. ->with('customer/*/new', ['_current' => true])
  966. ->willReturn(true);
  967. $this->assertEquals($redirectMock, $this->model->execute());
  968. }
  969. }