InlineEditTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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\Model\AddressRegistry;
  8. use Magento\Customer\Model\EmailNotificationInterface;
  9. use Magento\Framework\DataObject;
  10. use Magento\Framework\Message\MessageInterface;
  11. /**
  12. * Unit tests for Inline customer edit
  13. *
  14. * @SuppressWarnings(PHPMD.TooManyFields)
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class InlineEditTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /** @var \Magento\Customer\Controller\Adminhtml\Index\InlineEdit */
  20. private $controller;
  21. /** @var \Magento\Backend\App\Action\Context */
  22. private $context;
  23. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject*/
  24. private $request;
  25. /** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject*/
  26. private $messageManager;
  27. /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject*/
  28. protected $customerData;
  29. /** @var \Magento\Customer\Api\Data\AddressInterface|\PHPUnit_Framework_MockObject_MockObject*/
  30. private $address;
  31. /** @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject*/
  32. private $resultJsonFactory;
  33. /** @var \Magento\Framework\Controller\Result\Json|\PHPUnit_Framework_MockObject_MockObject*/
  34. private $resultJson;
  35. /** @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject*/
  36. private $customerRepository;
  37. /** @var \Magento\Customer\Model\Address\Mapper|\PHPUnit_Framework_MockObject_MockObject*/
  38. private $addressMapper;
  39. /** @var \Magento\Customer\Model\Customer\Mapper|\PHPUnit_Framework_MockObject_MockObject*/
  40. private $customerMapper;
  41. /** @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject*/
  42. private $dataObjectHelper;
  43. /** @var \Magento\Customer\Api\Data\AddressInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject*/
  44. private $addressDataFactory;
  45. /** @var \Magento\Customer\Api\AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject*/
  46. private $addressRepository;
  47. /** @var \Magento\Framework\Message\Collection|\PHPUnit_Framework_MockObject_MockObject*/
  48. private $messageCollection;
  49. /** @var \Magento\Framework\Message\MessageInterface|\PHPUnit_Framework_MockObject_MockObject*/
  50. private $message;
  51. /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject*/
  52. private $logger;
  53. /** @var EmailNotificationInterface|\PHPUnit_Framework_MockObject_MockObject */
  54. private $emailNotification;
  55. /** @var AddressRegistry|\PHPUnit_Framework_MockObject_MockObject */
  56. private $addressRegistry;
  57. /** @var array */
  58. private $items;
  59. /**
  60. * Sets up mocks
  61. *
  62. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  63. */
  64. protected function setUp()
  65. {
  66. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  67. $this->request = $this->getMockForAbstractClass(
  68. \Magento\Framework\App\RequestInterface::class,
  69. [],
  70. '',
  71. false
  72. );
  73. $this->messageManager = $this->getMockForAbstractClass(
  74. \Magento\Framework\Message\ManagerInterface::class,
  75. [],
  76. '',
  77. false
  78. );
  79. $this->customerData = $this->getMockForAbstractClass(
  80. \Magento\Customer\Api\Data\CustomerInterface::class,
  81. [],
  82. '',
  83. false
  84. );
  85. $this->address = $this->getMockForAbstractClass(
  86. \Magento\Customer\Api\Data\AddressInterface::class,
  87. [],
  88. 'address',
  89. false
  90. );
  91. $this->addressMapper = $this->createMock(\Magento\Customer\Model\Address\Mapper::class);
  92. $this->customerMapper = $this->createMock(\Magento\Customer\Model\Customer\Mapper::class);
  93. $this->resultJsonFactory = $this->createPartialMock(
  94. \Magento\Framework\Controller\Result\JsonFactory::class,
  95. ['create']
  96. );
  97. $this->resultJson = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
  98. $this->customerRepository = $this->getMockForAbstractClass(
  99. \Magento\Customer\Api\CustomerRepositoryInterface::class,
  100. [],
  101. '',
  102. false
  103. );
  104. $this->dataObjectHelper = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
  105. $this->addressDataFactory = $this->createPartialMock(
  106. \Magento\Customer\Api\Data\AddressInterfaceFactory::class,
  107. ['create']
  108. );
  109. $this->addressRepository = $this->getMockForAbstractClass(
  110. \Magento\Customer\Api\AddressRepositoryInterface::class,
  111. [],
  112. '',
  113. false
  114. );
  115. $this->messageCollection = $this->createMock(\Magento\Framework\Message\Collection::class);
  116. $this->message = $this->getMockForAbstractClass(
  117. \Magento\Framework\Message\MessageInterface::class,
  118. [],
  119. '',
  120. false
  121. );
  122. $this->logger = $this->getMockForAbstractClass(
  123. \Psr\Log\LoggerInterface::class,
  124. [],
  125. '',
  126. false
  127. );
  128. $this->emailNotification = $this->getMockBuilder(EmailNotificationInterface::class)
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $this->context = $objectManager->getObject(
  132. \Magento\Backend\App\Action\Context::class,
  133. [
  134. 'request' => $this->request,
  135. 'messageManager' => $this->messageManager,
  136. ]
  137. );
  138. $this->addressRegistry = $this->createMock(\Magento\Customer\Model\AddressRegistry::class);
  139. $this->controller = $objectManager->getObject(
  140. \Magento\Customer\Controller\Adminhtml\Index\InlineEdit::class,
  141. [
  142. 'context' => $this->context,
  143. 'resultJsonFactory' => $this->resultJsonFactory,
  144. 'customerRepository' => $this->customerRepository,
  145. 'addressMapper' => $this->addressMapper,
  146. 'customerMapper' => $this->customerMapper,
  147. 'dataObjectHelper' => $this->dataObjectHelper,
  148. 'addressDataFactory' => $this->addressDataFactory,
  149. 'addressRepository' => $this->addressRepository,
  150. 'logger' => $this->logger,
  151. 'addressRegistry' => $this->addressRegistry
  152. ]
  153. );
  154. $reflection = new \ReflectionClass(get_class($this->controller));
  155. $reflectionProperty = $reflection->getProperty('emailNotification');
  156. $reflectionProperty->setAccessible(true);
  157. $reflectionProperty->setValue($this->controller, $this->emailNotification);
  158. $this->items = [
  159. 14 => [
  160. 'email' => 'test@test.ua',
  161. 'billing_postcode' => '07294',
  162. ]
  163. ];
  164. }
  165. /**
  166. * Prepare mocks for tests
  167. *
  168. * @param int $populateSequence
  169. */
  170. protected function prepareMocksForTesting($populateSequence = 0)
  171. {
  172. $this->resultJsonFactory->expects($this->once())
  173. ->method('create')
  174. ->willReturn($this->resultJson);
  175. $this->request->expects($this->at(0))
  176. ->method('getParam')
  177. ->with('items', [])
  178. ->willReturn($this->items);
  179. $this->request->expects($this->at(1))
  180. ->method('getParam')
  181. ->with('isAjax')
  182. ->willReturn(true);
  183. $this->customerRepository->expects($this->once())
  184. ->method('getById')
  185. ->with(14)
  186. ->willReturn($this->customerData);
  187. $this->customerMapper->expects($this->once())
  188. ->method('toFlatArray')
  189. ->with($this->customerData)
  190. ->willReturn(['name' => 'Firstname Lastname']);
  191. $this->dataObjectHelper->expects($this->at($populateSequence))
  192. ->method('populateWithArray')
  193. ->with(
  194. $this->customerData,
  195. [
  196. 'name' => 'Firstname Lastname',
  197. 'email' => 'test@test.ua',
  198. ],
  199. \Magento\Customer\Api\Data\CustomerInterface::class
  200. );
  201. $this->customerData->expects($this->any())
  202. ->method('getId')
  203. ->willReturn(12);
  204. }
  205. /**
  206. * Prepare mocks for update customers default billing address use case
  207. */
  208. protected function prepareMocksForUpdateDefaultBilling()
  209. {
  210. $this->prepareMocksForProcessAddressData();
  211. $addressData = [
  212. 'postcode' => '07294',
  213. 'firstname' => 'Firstname',
  214. 'lastname' => 'Lastname',
  215. ];
  216. $this->customerData->expects($this->exactly(2))
  217. ->method('getAddresses')
  218. ->willReturn([$this->address]);
  219. $this->address->expects($this->once())
  220. ->method('isDefaultBilling')
  221. ->willReturn(true);
  222. $this->addressRegistry->expects($this->once())
  223. ->method('retrieve')
  224. ->willReturn(new DataObject());
  225. $this->dataObjectHelper->expects($this->at(0))
  226. ->method('populateWithArray')
  227. ->with(
  228. $this->address,
  229. $addressData,
  230. \Magento\Customer\Api\Data\AddressInterface::class
  231. );
  232. }
  233. /**
  234. * Prepare mocks for processing customers address data use case
  235. */
  236. protected function prepareMocksForProcessAddressData()
  237. {
  238. $this->customerData->expects($this->once())
  239. ->method('getFirstname')
  240. ->willReturn('Firstname');
  241. $this->customerData->expects($this->once())
  242. ->method('getLastname')
  243. ->willReturn('Lastname');
  244. }
  245. /**
  246. * Prepare mocks for error messages processing test
  247. */
  248. protected function prepareMocksForErrorMessagesProcessing()
  249. {
  250. $this->messageManager->expects($this->atLeastOnce())
  251. ->method('getMessages')
  252. ->willReturn($this->messageCollection);
  253. $this->messageCollection->expects($this->once())
  254. ->method('getErrors')
  255. ->willReturn([$this->message]);
  256. $this->messageCollection->expects($this->once())
  257. ->method('getCountByType')
  258. ->with(MessageInterface::TYPE_ERROR)
  259. ->willReturn(1);
  260. $this->message->expects($this->once())
  261. ->method('getText')
  262. ->willReturn('Error text');
  263. $this->resultJson->expects($this->once())
  264. ->method('setData')
  265. ->with([
  266. 'messages' => ['Error text'],
  267. 'error' => true,
  268. ])
  269. ->willReturnSelf();
  270. }
  271. /**
  272. * Unit test for updating customers billing address use case
  273. */
  274. public function testExecuteWithUpdateBilling()
  275. {
  276. $this->prepareMocksForTesting(1);
  277. $this->customerData->expects($this->once())
  278. ->method('getDefaultBilling')
  279. ->willReturn(23);
  280. $this->prepareMocksForUpdateDefaultBilling();
  281. $this->customerRepository->expects($this->once())
  282. ->method('save')
  283. ->with($this->customerData);
  284. $this->emailNotification->expects($this->once())
  285. ->method('credentialsChanged')
  286. ->willReturnSelf();
  287. $this->prepareMocksForErrorMessagesProcessing();
  288. $this->assertSame($this->resultJson, $this->controller->execute());
  289. }
  290. /**
  291. * Unit test for creating customer with empty data use case
  292. */
  293. public function testExecuteWithoutItems()
  294. {
  295. $this->resultJsonFactory->expects($this->once())
  296. ->method('create')
  297. ->willReturn($this->resultJson);
  298. $this->request->expects($this->at(0))
  299. ->method('getParam')
  300. ->with('items', [])
  301. ->willReturn([]);
  302. $this->request->expects($this->at(1))
  303. ->method('getParam')
  304. ->with('isAjax')
  305. ->willReturn(false);
  306. $this->resultJson
  307. ->expects($this->once())
  308. ->method('setData')
  309. ->with([
  310. 'messages' => [__('Please correct the data sent.')],
  311. 'error' => true,
  312. ])
  313. ->willReturnSelf();
  314. $this->assertSame($this->resultJson, $this->controller->execute());
  315. }
  316. /**
  317. * Unit test for verifying Localized Exception during inline edit
  318. */
  319. public function testExecuteLocalizedException()
  320. {
  321. $exception = new \Magento\Framework\Exception\LocalizedException(__('Exception message'));
  322. $this->prepareMocksForTesting();
  323. $this->customerData->expects($this->once())
  324. ->method('getDefaultBilling')
  325. ->willReturn(false);
  326. $this->customerData->expects($this->once())
  327. ->method('getAddresses')
  328. ->willReturn([]);
  329. $this->customerRepository->expects($this->once())
  330. ->method('save')
  331. ->with($this->customerData)
  332. ->willThrowException($exception);
  333. $this->messageManager->expects($this->once())
  334. ->method('addError')
  335. ->with('[Customer ID: 12] Exception message');
  336. $this->logger->expects($this->once())
  337. ->method('critical')
  338. ->with($exception);
  339. $this->prepareMocksForErrorMessagesProcessing();
  340. $this->assertSame($this->resultJson, $this->controller->execute());
  341. }
  342. /**
  343. * Unit test for verifying Execute Exception during inline edit
  344. */
  345. public function testExecuteException()
  346. {
  347. $exception = new \Exception('Exception message');
  348. $this->prepareMocksForTesting();
  349. $this->customerData->expects($this->once())
  350. ->method('getDefaultBilling')
  351. ->willReturn(false);
  352. $this->customerData->expects($this->once())
  353. ->method('getAddresses')
  354. ->willReturn([]);
  355. $this->customerRepository->expects($this->once())
  356. ->method('save')
  357. ->with($this->customerData)
  358. ->willThrowException($exception);
  359. $this->messageManager->expects($this->once())
  360. ->method('addError')
  361. ->with('[Customer ID: 12] We can\'t save the customer.');
  362. $this->logger->expects($this->once())
  363. ->method('critical')
  364. ->with($exception);
  365. $this->prepareMocksForErrorMessagesProcessing();
  366. $this->assertSame($this->resultJson, $this->controller->execute());
  367. }
  368. }