CustomerTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * Unit test for customer service layer \Magento\Customer\Model\Customer
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. /**
  9. * Test class for \Magento\Customer\Model\Customer testing
  10. */
  11. namespace Magento\Customer\Test\Unit\Model;
  12. use Magento\Customer\Model\Customer;
  13. use Magento\Customer\Model\AccountConfirmation;
  14. use Magento\Customer\Model\ResourceModel\Address\CollectionFactory as AddressCollectionFactory;
  15. use Magento\Customer\Api\Data\CustomerInterfaceFactory;
  16. /**
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. * @SuppressWarnings(PHPMD.TooManyFields)
  19. */
  20. class CustomerTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /** @var Customer */
  23. protected $_model;
  24. /** @var \Magento\Store\Model\Website|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $_website;
  26. /** @var \Magento\Store\Model\StoreManager|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $_storeManager;
  28. /** @var \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $_config;
  30. /** @var \Magento\Eav\Model\Attribute|\PHPUnit_Framework_MockObject_MockObject */
  31. protected $_attribute;
  32. /** @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  33. protected $_scopeConfigMock;
  34. /** @var \Magento\Framework\Mail\Template\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject */
  35. protected $_transportBuilderMock;
  36. /** @var \Magento\Framework\Mail\TransportInterface|\PHPUnit_Framework_MockObject_MockObject */
  37. protected $_transportMock;
  38. /** @var \Magento\Framework\Encryption\EncryptorInterface|\PHPUnit_Framework_MockObject_MockObject */
  39. protected $_encryptor;
  40. /** @var \Magento\Customer\Model\AttributeFactory|\PHPUnit_Framework_MockObject_MockObject */
  41. protected $attributeFactoryMock;
  42. /** @var \Magento\Customer\Model\Attribute|\PHPUnit_Framework_MockObject_MockObject */
  43. protected $attributeCustomerMock;
  44. /** @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject */
  45. protected $registryMock;
  46. /** @var \Magento\Customer\Model\ResourceModel\Customer|\PHPUnit_Framework_MockObject_MockObject */
  47. protected $resourceMock;
  48. /**
  49. * @var \Magento\Framework\Reflection\DataObjectProcessor|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $dataObjectProcessor;
  52. /**
  53. * @var AccountConfirmation|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. private $accountConfirmation;
  56. /**
  57. * @var AddressCollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  58. */
  59. private $addressesFactory;
  60. /**
  61. * @var CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  62. */
  63. private $customerDataFactory;
  64. /**
  65. * @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
  66. */
  67. private $dataObjectHelper;
  68. protected function setUp()
  69. {
  70. $this->_website = $this->createMock(\Magento\Store\Model\Website::class);
  71. $this->_config = $this->createMock(\Magento\Eav\Model\Config::class);
  72. $this->_attribute = $this->createMock(\Magento\Eav\Model\Attribute::class);
  73. $this->_storeManager = $this->createMock(\Magento\Store\Model\StoreManager::class);
  74. $this->_storetMock = $this->createMock(\Magento\Store\Model\Store::class);
  75. $this->_scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  76. $this->_transportBuilderMock = $this->createMock(\Magento\Framework\Mail\Template\TransportBuilder::class);
  77. $this->_transportMock = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
  78. $this->attributeFactoryMock = $this->createPartialMock(
  79. \Magento\Customer\Model\AttributeFactory::class,
  80. ['create']
  81. );
  82. $this->attributeCustomerMock = $this->createMock(\Magento\Customer\Model\Attribute::class);
  83. $this->resourceMock = $this->createPartialMock(
  84. \Magento\Customer\Model\ResourceModel\Customer::class, // \Magento\Framework\DataObject::class,
  85. ['getIdFieldName']
  86. );
  87. $this->dataObjectProcessor = $this->createPartialMock(
  88. \Magento\Framework\Reflection\DataObjectProcessor::class,
  89. ['buildOutputDataArray']
  90. );
  91. $this->resourceMock->expects($this->any())
  92. ->method('getIdFieldName')
  93. ->will($this->returnValue('id'));
  94. $this->registryMock = $this->createPartialMock(\Magento\Framework\Registry::class, ['registry']);
  95. $this->_encryptor = $this->createMock(\Magento\Framework\Encryption\EncryptorInterface::class);
  96. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  97. $this->accountConfirmation = $this->createMock(AccountConfirmation::class);
  98. $this->addressesFactory = $this->getMockBuilder(AddressCollectionFactory::class)
  99. ->disableOriginalConstructor()
  100. ->setMethods(['create'])
  101. ->getMock();
  102. $this->customerDataFactory = $this->getMockBuilder(CustomerInterfaceFactory::class)
  103. ->disableOriginalConstructor()
  104. ->setMethods(['create'])
  105. ->getMock();
  106. $this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
  107. ->disableOriginalConstructor()
  108. ->setMethods(['populateWithArray'])
  109. ->getMock();
  110. $this->_model = $helper->getObject(
  111. \Magento\Customer\Model\Customer::class,
  112. [
  113. 'storeManager' => $this->_storeManager,
  114. 'config' => $this->_config,
  115. 'transportBuilder' => $this->_transportBuilderMock,
  116. 'scopeConfig' => $this->_scopeConfigMock,
  117. 'encryptor' => $this->_encryptor,
  118. 'attributeFactory' => $this->attributeFactoryMock,
  119. 'registry' => $this->registryMock,
  120. 'resource' => $this->resourceMock,
  121. 'dataObjectProcessor' => $this->dataObjectProcessor,
  122. 'accountConfirmation' => $this->accountConfirmation,
  123. '_addressesFactory' => $this->addressesFactory,
  124. 'customerDataFactory' => $this->customerDataFactory,
  125. 'dataObjectHelper' => $this->dataObjectHelper
  126. ]
  127. );
  128. }
  129. public function testHashPassword()
  130. {
  131. $this->_encryptor->expects(
  132. $this->once()
  133. )->method(
  134. 'getHash'
  135. )->with(
  136. 'password',
  137. 'salt'
  138. )->will(
  139. $this->returnValue('hash')
  140. );
  141. $this->assertEquals('hash', $this->_model->hashPassword('password', 'salt'));
  142. }
  143. /**
  144. * @expectedException \Magento\Framework\Exception\LocalizedException
  145. * @expectedExceptionMessage The transactional account email type is incorrect. Verify and try again.
  146. */
  147. public function testSendNewAccountEmailException()
  148. {
  149. $this->_model->sendNewAccountEmail('test');
  150. }
  151. public function testSendNewAccountEmailWithoutStoreId()
  152. {
  153. $store = $this->createMock(\Magento\Store\Model\Store::class);
  154. $website = $this->createMock(\Magento\Store\Model\Website::class);
  155. $website->expects($this->once())
  156. ->method('getStoreIds')
  157. ->will($this->returnValue([1, 2, 3, 4]));
  158. $this->_storeManager->expects($this->once())
  159. ->method('getWebsite')
  160. ->with(1)
  161. ->will($this->returnValue($website));
  162. $this->_storeManager->expects($this->once())
  163. ->method('getStore')
  164. ->with(1)
  165. ->will($this->returnValue($store));
  166. $this->_config->expects($this->exactly(3))
  167. ->method('getAttribute')
  168. ->will($this->returnValue($this->_attribute));
  169. $this->_attribute->expects($this->exactly(3))
  170. ->method('getIsVisible')
  171. ->will($this->returnValue(true));
  172. $methods = [
  173. 'setTemplateIdentifier',
  174. 'setTemplateOptions',
  175. 'setTemplateVars',
  176. 'setFrom',
  177. 'addTo',
  178. ];
  179. foreach ($methods as $method) {
  180. $this->_transportBuilderMock->expects($this->once())
  181. ->method($method)
  182. ->will($this->returnSelf());
  183. }
  184. $transportMock = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
  185. $transportMock->expects($this->once())
  186. ->method('sendMessage')
  187. ->will($this->returnSelf());
  188. $this->_transportBuilderMock->expects($this->once())
  189. ->method('getTransport')
  190. ->will($this->returnValue($transportMock));
  191. $this->_model->setData([
  192. 'website_id' => 1,
  193. 'store_id' => 1,
  194. 'email' => 'email@example.com',
  195. 'firstname' => 'FirstName',
  196. 'lastname' => 'LastName',
  197. 'middlename' => 'MiddleName',
  198. 'prefix' => 'Name Prefix',
  199. ]);
  200. $this->_model->sendNewAccountEmail('registered');
  201. }
  202. /**
  203. * @param $lockExpires
  204. * @param $expectedResult
  205. * @dataProvider isCustomerLockedDataProvider
  206. */
  207. public function testIsCustomerLocked($lockExpires, $expectedResult)
  208. {
  209. $this->_model->setLockExpires($lockExpires);
  210. $this->assertEquals($expectedResult, $this->_model->isCustomerLocked());
  211. }
  212. /**
  213. * @return array
  214. */
  215. public function isCustomerLockedDataProvider()
  216. {
  217. return [
  218. ['lockExpirationDate' => date("F j, Y", strtotime('-1 days')), 'expectedResult' => false],
  219. ['lockExpirationDate' => date("F j, Y", strtotime('+1 days')), 'expectedResult' => true]
  220. ];
  221. }
  222. /**
  223. * @param int $customerId
  224. * @param int $websiteId
  225. * @param bool $isConfirmationRequired
  226. * @param bool $expected
  227. * @dataProvider dataProviderIsConfirmationRequired
  228. */
  229. public function testIsConfirmationRequired(
  230. $customerId,
  231. $websiteId,
  232. $isConfirmationRequired,
  233. $expected
  234. ) {
  235. $customerEmail = 'test1@example.com';
  236. $this->_model->setData('id', $customerId);
  237. $this->_model->setData('website_id', $websiteId);
  238. $this->_model->setData('email', $customerEmail);
  239. $this->accountConfirmation->expects($this->once())
  240. ->method('isConfirmationRequired')
  241. ->with($websiteId, $customerId, $customerEmail)
  242. ->willReturn($isConfirmationRequired);
  243. $this->assertEquals($expected, $this->_model->isConfirmationRequired());
  244. }
  245. /**
  246. * @return array
  247. */
  248. public function dataProviderIsConfirmationRequired()
  249. {
  250. return [
  251. [null, null, false, false],
  252. [1, 1, true, true],
  253. [1, null, true, true],
  254. ];
  255. }
  256. public function testUpdateData()
  257. {
  258. $customerDataAttributes = [
  259. 'attribute' => 'attribute',
  260. 'test1' => 'test1',
  261. 'test33' => 'test33',
  262. ];
  263. $customer = $this->createPartialMock(
  264. \Magento\Customer\Model\Data\Customer::class,
  265. [
  266. 'getCustomAttributes',
  267. 'getId',
  268. ]
  269. );
  270. $attribute = $this->createPartialMock(
  271. \Magento\Framework\Api\AttributeValue::class,
  272. [
  273. 'getAttributeCode',
  274. 'getValue',
  275. ]
  276. );
  277. $this->dataObjectProcessor->expects($this->once())
  278. ->method('buildOutputDataArray')
  279. ->withConsecutive(
  280. [$customer, \Magento\Customer\Api\Data\CustomerInterface::class]
  281. )->willReturn($customerDataAttributes);
  282. $attribute->expects($this->exactly(3))
  283. ->method('getAttributeCode')
  284. ->willReturn('test33');
  285. $attribute->expects($this->exactly(2))
  286. ->method('getValue')
  287. ->willReturn('test33');
  288. $customer->expects($this->once())
  289. ->method('getCustomAttributes')
  290. ->willReturn([$attribute->getAttributeCode() => $attribute]);
  291. $this->_model->updateData($customer);
  292. foreach ($customerDataAttributes as $key => $value) {
  293. $expectedResult[strtolower(trim(preg_replace('/([A-Z]|[0-9]+)/', "_$1", $key), '_'))] = $value;
  294. }
  295. $expectedResult[$attribute->getAttributeCode()] = $attribute->getValue();
  296. $this->assertEquals($this->_model->getData(), $expectedResult);
  297. }
  298. /**
  299. * Test for the \Magento\Customer\Model\Customer::getDataModel() method
  300. */
  301. public function testGetDataModel()
  302. {
  303. $customerId = 1;
  304. $this->_model->setEntityId($customerId);
  305. $this->_model->setId($customerId);
  306. $addressDataModel = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\AddressInterface::class);
  307. $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
  308. ->disableOriginalConstructor()
  309. ->setMethods(['setCustomer', 'getDataModel'])
  310. ->getMock();
  311. $address->expects($this->atLeastOnce())->method('getDataModel')->willReturn($addressDataModel);
  312. $addresses = new \ArrayIterator([$address, $address]);
  313. $addressCollection = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Address\Collection::class)
  314. ->disableOriginalConstructor()
  315. ->setMethods(['setCustomerFilter', 'addAttributeToSelect', 'getIterator', 'getItems'])
  316. ->getMock();
  317. $addressCollection->expects($this->atLeastOnce())->method('setCustomerFilter')->willReturnSelf();
  318. $addressCollection->expects($this->atLeastOnce())->method('addAttributeToSelect')->willReturnSelf();
  319. $addressCollection->expects($this->atLeastOnce())->method('getIterator')
  320. ->willReturn($addresses);
  321. $addressCollection->expects($this->atLeastOnce())->method('getItems')
  322. ->willReturn($addresses);
  323. $this->addressesFactory->expects($this->atLeastOnce())->method('create')->willReturn($addressCollection);
  324. $customerDataObject = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\CustomerInterface::class);
  325. $this->customerDataFactory->expects($this->atLeastOnce())->method('create')->willReturn($customerDataObject);
  326. $this->dataObjectHelper->expects($this->atLeastOnce())->method('populateWithArray')
  327. ->with($customerDataObject, $this->_model->getData(), \Magento\Customer\Api\Data\CustomerInterface::class)
  328. ->willReturnSelf();
  329. $customerDataObject->expects($this->atLeastOnce())->method('setAddresses')
  330. ->with([$addressDataModel, $addressDataModel])
  331. ->willReturnSelf();
  332. $customerDataObject->expects($this->atLeastOnce())->method('setId')->with($customerId)->willReturnSelf();
  333. $this->_model->getDataModel();
  334. $this->assertEquals($customerDataObject, $this->_model->getDataModel());
  335. }
  336. }