AccountManagementTest.php 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123
  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;
  7. use Magento\Customer\Api\Data\CustomerInterface;
  8. use Magento\Customer\Model\AccountConfirmation;
  9. use Magento\Customer\Model\AccountManagement;
  10. use Magento\Customer\Model\AuthenticationInterface;
  11. use Magento\Customer\Model\Data\Customer;
  12. use Magento\Customer\Model\EmailNotificationInterface;
  13. use Magento\Framework\Api\SearchCriteriaBuilder;
  14. use Magento\Framework\App\Area;
  15. use Magento\Framework\Exception\NoSuchEntityException;
  16. use Magento\Framework\Intl\DateTimeFactory;
  17. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  18. use Magento\Store\Model\ScopeInterface;
  19. /**
  20. * @SuppressWarnings(PHPMD.TooManyFields)
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class AccountManagementTest extends \PHPUnit\Framework\TestCase
  24. {
  25. /** @var AccountManagement */
  26. protected $accountManagement;
  27. /** @var ObjectManagerHelper */
  28. protected $objectManagerHelper;
  29. /** @var \Magento\Customer\Model\CustomerFactory|\PHPUnit_Framework_MockObject_MockObject */
  30. protected $customerFactory;
  31. /** @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  32. protected $manager;
  33. /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  34. protected $storeManager;
  35. /** @var \Magento\Framework\Math\Random|\PHPUnit_Framework_MockObject_MockObject */
  36. protected $random;
  37. /** @var \Magento\Customer\Model\Metadata\Validator|\PHPUnit_Framework_MockObject_MockObject */
  38. protected $validator;
  39. /** @var \Magento\Customer\Api\Data\ValidationResultsInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject */
  40. protected $validationResultsInterfaceFactory;
  41. /** @var \Magento\Customer\Api\AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
  42. protected $addressRepository;
  43. /** @var \Magento\Customer\Api\CustomerMetadataInterface|\PHPUnit_Framework_MockObject_MockObject */
  44. protected $customerMetadata;
  45. /** @var \Magento\Customer\Model\CustomerRegistry|\PHPUnit_Framework_MockObject_MockObject */
  46. protected $customerRegistry;
  47. /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
  48. protected $logger;
  49. /** @var \Magento\Framework\Encryption\EncryptorInterface|\PHPUnit_Framework_MockObject_MockObject */
  50. protected $encryptor;
  51. /** @var \Magento\Customer\Model\Config\Share|\PHPUnit_Framework_MockObject_MockObject */
  52. protected $share;
  53. /** @var \Magento\Framework\Stdlib\StringUtils|\PHPUnit_Framework_MockObject_MockObject */
  54. protected $string;
  55. /** @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
  56. protected $customerRepository;
  57. /** @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  58. protected $scopeConfig;
  59. /** @var \Magento\Framework\Mail\Template\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject */
  60. protected $transportBuilder;
  61. /** @var \Magento\Framework\Reflection\DataObjectProcessor|\PHPUnit_Framework_MockObject_MockObject */
  62. protected $dataObjectProcessor;
  63. /** @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject */
  64. protected $registry;
  65. /** @var \Magento\Customer\Helper\View|\PHPUnit_Framework_MockObject_MockObject */
  66. protected $customerViewHelper;
  67. /** @var \Magento\Framework\Stdlib\DateTime|\PHPUnit_Framework_MockObject_MockObject */
  68. protected $dateTime;
  69. /** @var \Magento\Customer\Model\Customer|\PHPUnit_Framework_MockObject_MockObject */
  70. protected $customer;
  71. /** @var \Magento\Framework\DataObjectFactory|\PHPUnit_Framework_MockObject_MockObject */
  72. protected $objectFactory;
  73. /** @var \Magento\Framework\Api\ExtensibleDataObjectConverter|\PHPUnit_Framework_MockObject_MockObject */
  74. protected $extensibleDataObjectConverter;
  75. /**
  76. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\Store
  77. */
  78. protected $store;
  79. /**
  80. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Model\Data\CustomerSecure
  81. */
  82. protected $customerSecure;
  83. /**
  84. * @var AuthenticationInterface |\PHPUnit_Framework_MockObject_MockObject
  85. */
  86. protected $authenticationMock;
  87. /**
  88. * @var EmailNotificationInterface |\PHPUnit_Framework_MockObject_MockObject
  89. */
  90. protected $emailNotificationMock;
  91. /**
  92. * @var DateTimeFactory|\PHPUnit_Framework_MockObject_MockObject
  93. */
  94. private $dateTimeFactory;
  95. /**
  96. * @var AccountConfirmation|\PHPUnit_Framework_MockObject_MockObject
  97. */
  98. private $accountConfirmation;
  99. /**
  100. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Session\SessionManagerInterface
  101. */
  102. private $sessionManager;
  103. /**
  104. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory
  105. */
  106. private $visitorCollectionFactory;
  107. /**
  108. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Session\SaveHandlerInterface
  109. */
  110. private $saveHandler;
  111. /**
  112. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Model\AddressRegistry
  113. */
  114. private $addressRegistryMock;
  115. /**
  116. * @var \PHPUnit_Framework_MockObject_MockObject|SearchCriteriaBuilder
  117. */
  118. private $searchCriteriaBuilderMock;
  119. /**
  120. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  121. */
  122. protected function setUp()
  123. {
  124. $this->customerFactory = $this->createPartialMock(\Magento\Customer\Model\CustomerFactory::class, ['create']);
  125. $this->manager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  126. $this->store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  127. ->disableOriginalConstructor()
  128. ->getMock();
  129. $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  130. $this->random = $this->createMock(\Magento\Framework\Math\Random::class);
  131. $this->validator = $this->createMock(\Magento\Customer\Model\Metadata\Validator::class);
  132. $this->validationResultsInterfaceFactory = $this->createMock(
  133. \Magento\Customer\Api\Data\ValidationResultsInterfaceFactory::class
  134. );
  135. $this->addressRepository = $this->createMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
  136. $this->customerMetadata = $this->createMock(\Magento\Customer\Api\CustomerMetadataInterface::class);
  137. $this->customerRegistry = $this->createMock(\Magento\Customer\Model\CustomerRegistry::class);
  138. $this->logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  139. $this->encryptor = $this->createMock(\Magento\Framework\Encryption\EncryptorInterface::class);
  140. $this->share = $this->createMock(\Magento\Customer\Model\Config\Share::class);
  141. $this->string = $this->createMock(\Magento\Framework\Stdlib\StringUtils::class);
  142. $this->customerRepository = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  143. $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  144. ->disableOriginalConstructor()
  145. ->getMock();
  146. $this->transportBuilder = $this->createMock(\Magento\Framework\Mail\Template\TransportBuilder::class);
  147. $this->dataObjectProcessor = $this->createMock(\Magento\Framework\Reflection\DataObjectProcessor::class);
  148. $this->registry = $this->createMock(\Magento\Framework\Registry::class);
  149. $this->customerViewHelper = $this->createMock(\Magento\Customer\Helper\View::class);
  150. $this->dateTime = $this->createMock(\Magento\Framework\Stdlib\DateTime::class);
  151. $this->customer = $this->createMock(\Magento\Customer\Model\Customer::class);
  152. $this->objectFactory = $this->createMock(\Magento\Framework\DataObjectFactory::class);
  153. $this->addressRegistryMock = $this->createMock(\Magento\Customer\Model\AddressRegistry::class);
  154. $this->extensibleDataObjectConverter = $this->createMock(
  155. \Magento\Framework\Api\ExtensibleDataObjectConverter::class
  156. );
  157. $this->authenticationMock = $this->getMockBuilder(AuthenticationInterface::class)
  158. ->disableOriginalConstructor()
  159. ->getMock();
  160. $this->emailNotificationMock = $this->getMockBuilder(EmailNotificationInterface::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $this->customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
  164. ->setMethods(['setRpToken', 'addData', 'setRpTokenCreatedAt', 'setData'])
  165. ->disableOriginalConstructor()
  166. ->getMock();
  167. $this->dateTimeFactory = $this->createMock(DateTimeFactory::class);
  168. $this->accountConfirmation = $this->createMock(AccountConfirmation::class);
  169. $this->searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);
  170. $this->visitorCollectionFactory = $this->getMockBuilder(
  171. \Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory::class
  172. )
  173. ->disableOriginalConstructor()
  174. ->setMethods(['create'])
  175. ->getMock();
  176. $this->sessionManager = $this->getMockBuilder(\Magento\Framework\Session\SessionManagerInterface::class)
  177. ->disableOriginalConstructor()
  178. ->getMock();
  179. $this->saveHandler = $this->getMockBuilder(\Magento\Framework\Session\SaveHandlerInterface::class)
  180. ->disableOriginalConstructor()
  181. ->getMock();
  182. $this->objectManagerHelper = new ObjectManagerHelper($this);
  183. $this->accountManagement = $this->objectManagerHelper->getObject(
  184. \Magento\Customer\Model\AccountManagement::class,
  185. [
  186. 'customerFactory' => $this->customerFactory,
  187. 'eventManager' => $this->manager,
  188. 'storeManager' => $this->storeManager,
  189. 'mathRandom' => $this->random,
  190. 'validator' => $this->validator,
  191. 'validationResultsDataFactory' => $this->validationResultsInterfaceFactory,
  192. 'addressRepository' => $this->addressRepository,
  193. 'customerMetadataService' => $this->customerMetadata,
  194. 'customerRegistry' => $this->customerRegistry,
  195. 'logger' => $this->logger,
  196. 'encryptor' => $this->encryptor,
  197. 'configShare' => $this->share,
  198. 'stringHelper' => $this->string,
  199. 'customerRepository' => $this->customerRepository,
  200. 'scopeConfig' => $this->scopeConfig,
  201. 'transportBuilder' => $this->transportBuilder,
  202. 'dataProcessor' => $this->dataObjectProcessor,
  203. 'registry' => $this->registry,
  204. 'customerViewHelper' => $this->customerViewHelper,
  205. 'dateTime' => $this->dateTime,
  206. 'customerModel' => $this->customer,
  207. 'objectFactory' => $this->objectFactory,
  208. 'extensibleDataObjectConverter' => $this->extensibleDataObjectConverter,
  209. 'dateTimeFactory' => $this->dateTimeFactory,
  210. 'accountConfirmation' => $this->accountConfirmation,
  211. 'sessionManager' => $this->sessionManager,
  212. 'saveHandler' => $this->saveHandler,
  213. 'visitorCollectionFactory' => $this->visitorCollectionFactory,
  214. 'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
  215. 'addressRegistry' => $this->addressRegistryMock,
  216. ]
  217. );
  218. $this->objectManagerHelper->setBackwardCompatibleProperty(
  219. $this->accountManagement,
  220. 'authentication',
  221. $this->authenticationMock
  222. );
  223. $this->objectManagerHelper->setBackwardCompatibleProperty(
  224. $this->accountManagement,
  225. 'emailNotification',
  226. $this->emailNotificationMock
  227. );
  228. }
  229. /**
  230. * @expectedException \Magento\Framework\Exception\InputException
  231. */
  232. public function testCreateAccountWithPasswordHashWithExistingCustomer()
  233. {
  234. $websiteId = 1;
  235. $storeId = 1;
  236. $customerId = 1;
  237. $customerEmail = 'email@email.com';
  238. $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
  239. $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
  240. $website->expects($this->once())
  241. ->method('getStoreIds')
  242. ->willReturn([1, 2, 3]);
  243. $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
  244. $customer->expects($this->once())
  245. ->method('getId')
  246. ->willReturn($customerId);
  247. $customer->expects($this->once())
  248. ->method('getEmail')
  249. ->willReturn($customerEmail);
  250. $customer->expects($this->once())
  251. ->method('getWebsiteId')
  252. ->willReturn($websiteId);
  253. $customer->expects($this->atLeastOnce())
  254. ->method('getStoreId')
  255. ->willReturn($storeId);
  256. $this->customerRepository
  257. ->expects($this->once())
  258. ->method('get')
  259. ->with($customerEmail)
  260. ->willReturn($customer);
  261. $this->share
  262. ->expects($this->once())
  263. ->method('isWebsiteScope')
  264. ->willReturn(true);
  265. $this->storeManager
  266. ->expects($this->once())
  267. ->method('getWebsite')
  268. ->with($websiteId)
  269. ->willReturn($website);
  270. $this->accountManagement->createAccountWithPasswordHash($customer, $hash);
  271. }
  272. /**
  273. * @expectedException \Magento\Framework\Exception\State\InputMismatchException
  274. */
  275. public function testCreateAccountWithPasswordHashWithCustomerWithoutStoreId()
  276. {
  277. $websiteId = 1;
  278. $storeId = null;
  279. $defaultStoreId = 1;
  280. $customerId = 1;
  281. $customerEmail = 'email@email.com';
  282. $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
  283. $address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  284. ->disableOriginalConstructor()
  285. ->getMock();
  286. $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
  287. $store->expects($this->once())
  288. ->method('getId')
  289. ->willReturn($defaultStoreId);
  290. $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
  291. $website->expects($this->atLeastOnce())
  292. ->method('getStoreIds')
  293. ->willReturn([1, 2, 3]);
  294. $website->expects($this->once())
  295. ->method('getDefaultStore')
  296. ->willReturn($store);
  297. $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
  298. $customer->expects($this->atLeastOnce())
  299. ->method('getId')
  300. ->willReturn($customerId);
  301. $customer->expects($this->once())
  302. ->method('getEmail')
  303. ->willReturn($customerEmail);
  304. $customer->expects($this->atLeastOnce())
  305. ->method('getWebsiteId')
  306. ->willReturn($websiteId);
  307. $customer->expects($this->atLeastOnce())
  308. ->method('getStoreId')
  309. ->willReturn($storeId);
  310. $customer->expects($this->once())
  311. ->method('setStoreId')
  312. ->with($defaultStoreId);
  313. $customer
  314. ->expects($this->once())
  315. ->method('getAddresses')
  316. ->willReturn([$address]);
  317. $customer
  318. ->expects($this->once())
  319. ->method('setAddresses')
  320. ->with(null);
  321. $this->customerRepository
  322. ->expects($this->once())
  323. ->method('get')
  324. ->with($customerEmail)
  325. ->willReturn($customer);
  326. $this->share
  327. ->expects($this->atLeastOnce())
  328. ->method('isWebsiteScope')
  329. ->willReturn(true);
  330. $this->storeManager
  331. ->expects($this->atLeastOnce())
  332. ->method('getWebsite')
  333. ->with($websiteId)
  334. ->willReturn($website);
  335. $exception = new \Magento\Framework\Exception\AlreadyExistsException(
  336. new \Magento\Framework\Phrase('Exception message')
  337. );
  338. $this->customerRepository
  339. ->expects($this->once())
  340. ->method('save')
  341. ->with($customer, $hash)
  342. ->willThrowException($exception);
  343. $this->accountManagement->createAccountWithPasswordHash($customer, $hash);
  344. }
  345. /**
  346. * @expectedException \Magento\Framework\Exception\LocalizedException
  347. */
  348. public function testCreateAccountWithPasswordHashWithLocalizedException()
  349. {
  350. $websiteId = 1;
  351. $storeId = null;
  352. $defaultStoreId = 1;
  353. $customerId = 1;
  354. $customerEmail = 'email@email.com';
  355. $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
  356. $address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  357. ->disableOriginalConstructor()
  358. ->getMock();
  359. $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
  360. $store->expects($this->once())
  361. ->method('getId')
  362. ->willReturn($defaultStoreId);
  363. $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
  364. $website->expects($this->once())
  365. ->method('getStoreIds')
  366. ->willReturn([1, 2, 3]);
  367. $website->expects($this->once())
  368. ->method('getDefaultStore')
  369. ->willReturn($store);
  370. $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
  371. $customer->expects($this->atLeastOnce())
  372. ->method('getId')
  373. ->willReturn($customerId);
  374. $customer->expects($this->once())
  375. ->method('getEmail')
  376. ->willReturn($customerEmail);
  377. $customer->expects($this->atLeastOnce())
  378. ->method('getWebsiteId')
  379. ->willReturn($websiteId);
  380. $customer->expects($this->atLeastOnce())
  381. ->method('getStoreId')
  382. ->willReturn($storeId);
  383. $customer->expects($this->once())
  384. ->method('setStoreId')
  385. ->with($defaultStoreId);
  386. $customer
  387. ->expects($this->once())
  388. ->method('getAddresses')
  389. ->willReturn([$address]);
  390. $customer
  391. ->expects($this->once())
  392. ->method('setAddresses')
  393. ->with(null);
  394. $this->customerRepository
  395. ->expects($this->once())
  396. ->method('get')
  397. ->with($customerEmail)
  398. ->willReturn($customer);
  399. $this->share
  400. ->expects($this->once())
  401. ->method('isWebsiteScope')
  402. ->willReturn(true);
  403. $this->storeManager
  404. ->expects($this->atLeastOnce())
  405. ->method('getWebsite')
  406. ->with($websiteId)
  407. ->willReturn($website);
  408. $exception = new \Magento\Framework\Exception\LocalizedException(
  409. new \Magento\Framework\Phrase('Exception message')
  410. );
  411. $this->customerRepository
  412. ->expects($this->once())
  413. ->method('save')
  414. ->with($customer, $hash)
  415. ->willThrowException($exception);
  416. $this->accountManagement->createAccountWithPasswordHash($customer, $hash);
  417. }
  418. /**
  419. * @expectedException \Magento\Framework\Exception\LocalizedException
  420. */
  421. public function testCreateAccountWithPasswordHashWithAddressException()
  422. {
  423. $websiteId = 1;
  424. $storeId = null;
  425. $defaultStoreId = 1;
  426. $customerId = 1;
  427. $customerEmail = 'email@email.com';
  428. $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
  429. $address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  430. ->disableOriginalConstructor()
  431. ->getMock();
  432. $address->expects($this->once())
  433. ->method('setCustomerId')
  434. ->with($customerId);
  435. $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
  436. $store->expects($this->once())
  437. ->method('getId')
  438. ->willReturn($defaultStoreId);
  439. $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
  440. $website->expects($this->once())
  441. ->method('getStoreIds')
  442. ->willReturn([1, 2, 3]);
  443. $website->expects($this->once())
  444. ->method('getDefaultStore')
  445. ->willReturn($store);
  446. $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
  447. $customer->expects($this->atLeastOnce())
  448. ->method('getId')
  449. ->willReturn($customerId);
  450. $customer->expects($this->once())
  451. ->method('getEmail')
  452. ->willReturn($customerEmail);
  453. $customer->expects($this->atLeastOnce())
  454. ->method('getWebsiteId')
  455. ->willReturn($websiteId);
  456. $customer->expects($this->atLeastOnce())
  457. ->method('getStoreId')
  458. ->willReturn($storeId);
  459. $customer->expects($this->once())
  460. ->method('setStoreId')
  461. ->with($defaultStoreId);
  462. $customer
  463. ->expects($this->once())
  464. ->method('getAddresses')
  465. ->willReturn([$address]);
  466. $customer
  467. ->expects($this->once())
  468. ->method('setAddresses')
  469. ->with(null);
  470. $this->customerRepository
  471. ->expects($this->once())
  472. ->method('get')
  473. ->with($customerEmail)
  474. ->willReturn($customer);
  475. $this->share
  476. ->expects($this->once())
  477. ->method('isWebsiteScope')
  478. ->willReturn(true);
  479. $this->storeManager
  480. ->expects($this->atLeastOnce())
  481. ->method('getWebsite')
  482. ->with($websiteId)
  483. ->willReturn($website);
  484. $this->customerRepository
  485. ->expects($this->once())
  486. ->method('save')
  487. ->with($customer, $hash)
  488. ->willReturn($customer);
  489. $exception = new \Magento\Framework\Exception\InputException(
  490. new \Magento\Framework\Phrase('Exception message')
  491. );
  492. $this->addressRepository
  493. ->expects($this->atLeastOnce())
  494. ->method('save')
  495. ->with($address)
  496. ->willThrowException($exception);
  497. $this->customerRepository
  498. ->expects($this->once())
  499. ->method('delete')
  500. ->with($customer);
  501. $this->accountManagement->createAccountWithPasswordHash($customer, $hash);
  502. }
  503. /**
  504. * @expectedException \Magento\Framework\Exception\LocalizedException
  505. */
  506. public function testCreateAccountWithPasswordHashWithNewCustomerAndLocalizedException()
  507. {
  508. $storeId = 1;
  509. $storeName = 'store_name';
  510. $websiteId = 1;
  511. $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
  512. $customerMock = $this->getMockBuilder(Customer::class)
  513. ->disableOriginalConstructor()
  514. ->getMock();
  515. $customerMock->expects($this->atLeastOnce())
  516. ->method('getId')
  517. ->willReturn(null);
  518. $customerMock->expects($this->atLeastOnce())
  519. ->method('getStoreId')
  520. ->willReturn($storeId);
  521. $customerMock->expects($this->atLeastOnce())
  522. ->method('getWebsiteId')
  523. ->willReturn($websiteId);
  524. $customerMock->expects($this->once())
  525. ->method('setCreatedIn')
  526. ->with($storeName)
  527. ->willReturnSelf();
  528. $customerMock->expects($this->once())
  529. ->method('getAddresses')
  530. ->willReturn([]);
  531. $customerMock->expects($this->once())
  532. ->method('setAddresses')
  533. ->with(null)
  534. ->willReturnSelf();
  535. $this->share
  536. ->expects($this->once())
  537. ->method('isWebsiteScope')
  538. ->willReturn(true);
  539. $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
  540. $website->expects($this->once())
  541. ->method('getStoreIds')
  542. ->willReturn([1, 2, 3]);
  543. $this->storeManager
  544. ->expects($this->atLeastOnce())
  545. ->method('getWebsite')
  546. ->with($websiteId)
  547. ->willReturn($website);
  548. $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  549. ->disableOriginalConstructor()
  550. ->getMock();
  551. $storeMock->expects($this->once())
  552. ->method('getName')
  553. ->willReturn($storeName);
  554. $this->storeManager->expects($this->exactly(1))
  555. ->method('getStore')
  556. ->with($storeId)
  557. ->willReturn($storeMock);
  558. $exception = new \Magento\Framework\Exception\LocalizedException(
  559. new \Magento\Framework\Phrase('Exception message')
  560. );
  561. $this->customerRepository
  562. ->expects($this->once())
  563. ->method('save')
  564. ->with($customerMock, $hash)
  565. ->willThrowException($exception);
  566. $this->accountManagement->createAccountWithPasswordHash($customerMock, $hash);
  567. }
  568. /**
  569. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  570. */
  571. public function testCreateAccountWithoutPassword()
  572. {
  573. $websiteId = 1;
  574. $storeId = null;
  575. $defaultStoreId = 1;
  576. $customerId = 1;
  577. $customerEmail = 'email@email.com';
  578. $newLinkToken = '2jh43j5h2345jh23lh452h345hfuzasd96ofu';
  579. $datetime = $this->prepareDateTimeFactory();
  580. $address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  581. ->disableOriginalConstructor()
  582. ->getMock();
  583. $address->expects($this->once())
  584. ->method('setCustomerId')
  585. ->with($customerId);
  586. $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
  587. $store->expects($this->once())
  588. ->method('getId')
  589. ->willReturn($defaultStoreId);
  590. $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
  591. $website->expects($this->atLeastOnce())
  592. ->method('getStoreIds')
  593. ->willReturn([1, 2, 3]);
  594. $website->expects($this->once())
  595. ->method('getDefaultStore')
  596. ->willReturn($store);
  597. $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
  598. $customer->expects($this->atLeastOnce())
  599. ->method('getId')
  600. ->willReturn($customerId);
  601. $customer->expects($this->atLeastOnce())
  602. ->method('getEmail')
  603. ->willReturn($customerEmail);
  604. $customer->expects($this->atLeastOnce())
  605. ->method('getWebsiteId')
  606. ->willReturn($websiteId);
  607. $customer->expects($this->atLeastOnce())
  608. ->method('getStoreId')
  609. ->willReturn($storeId);
  610. $customer->expects($this->once())
  611. ->method('setStoreId')
  612. ->with($defaultStoreId);
  613. $customer->expects($this->once())
  614. ->method('getAddresses')
  615. ->willReturn([$address]);
  616. $customer->expects($this->once())
  617. ->method('setAddresses')
  618. ->with(null);
  619. $this->customerRepository
  620. ->expects($this->once())
  621. ->method('get')
  622. ->with($customerEmail)
  623. ->willReturn($customer);
  624. $this->share->expects($this->once())
  625. ->method('isWebsiteScope')
  626. ->willReturn(true);
  627. $this->storeManager->expects($this->atLeastOnce())
  628. ->method('getWebsite')
  629. ->with($websiteId)
  630. ->willReturn($website);
  631. $this->customerRepository->expects($this->atLeastOnce())
  632. ->method('save')
  633. ->willReturn($customer);
  634. $this->addressRepository->expects($this->atLeastOnce())
  635. ->method('save')
  636. ->with($address);
  637. $this->customerRepository->expects($this->once())
  638. ->method('getById')
  639. ->with($customerId)
  640. ->willReturn($customer);
  641. $this->random->expects($this->once())
  642. ->method('getUniqueHash')
  643. ->willReturn($newLinkToken);
  644. $customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
  645. ->setMethods(['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash'])
  646. ->disableOriginalConstructor()
  647. ->getMock();
  648. $customerSecure->expects($this->any())
  649. ->method('setRpToken')
  650. ->with($newLinkToken);
  651. $customerSecure->expects($this->any())
  652. ->method('setRpTokenCreatedAt')
  653. ->with($datetime)
  654. ->willReturnSelf();
  655. $customerSecure->expects($this->any())
  656. ->method('getPasswordHash')
  657. ->willReturn(null);
  658. $this->customerRegistry->expects($this->atLeastOnce())
  659. ->method('retrieveSecureData')
  660. ->willReturn($customerSecure);
  661. $this->emailNotificationMock->expects($this->once())
  662. ->method('newAccount')
  663. ->willReturnSelf();
  664. $this->accountManagement->createAccount($customer);
  665. }
  666. /**
  667. * Data provider for testCreateAccountWithPasswordInputException test
  668. *
  669. * @return array
  670. */
  671. public function dataProviderCheckPasswordStrength()
  672. {
  673. return [
  674. [
  675. 'testNumber' => 1,
  676. 'password' => 'qwer',
  677. 'minPasswordLength' => 5,
  678. 'minCharacterSetsNum' => 1,
  679. ],
  680. [
  681. 'testNumber' => 2,
  682. 'password' => 'wrfewqedf1',
  683. 'minPasswordLength' => 5,
  684. 'minCharacterSetsNum' => 3,
  685. ],
  686. ];
  687. }
  688. /**
  689. * @param int $testNumber
  690. * @param string $password
  691. * @param int $minPasswordLength
  692. * @param int $minCharacterSetsNum
  693. * @dataProvider dataProviderCheckPasswordStrength
  694. */
  695. public function testCreateAccountWithPasswordInputException(
  696. $testNumber,
  697. $password,
  698. $minPasswordLength,
  699. $minCharacterSetsNum
  700. ) {
  701. $this->scopeConfig->expects($this->any())
  702. ->method('getValue')
  703. ->will(
  704. $this->returnValueMap(
  705. [
  706. [
  707. AccountManagement::XML_PATH_MINIMUM_PASSWORD_LENGTH,
  708. 'default',
  709. null,
  710. $minPasswordLength,
  711. ],
  712. [
  713. AccountManagement::XML_PATH_REQUIRED_CHARACTER_CLASSES_NUMBER,
  714. 'default',
  715. null,
  716. $minCharacterSetsNum,
  717. ],
  718. ]
  719. )
  720. );
  721. $this->string->expects($this->any())
  722. ->method('strlen')
  723. ->with($password)
  724. ->willReturn(iconv_strlen($password, 'UTF-8'));
  725. if ($testNumber == 1) {
  726. $this->expectException(\Magento\Framework\Exception\InputException::class);
  727. $this->expectExceptionMessage('The password needs at least ' . $minPasswordLength . ' characters. '
  728. . 'Create a new password and try again.');
  729. }
  730. if ($testNumber == 2) {
  731. $this->expectException(\Magento\Framework\Exception\InputException::class);
  732. $this->expectExceptionMessage('Minimum of different classes of characters in password is ' .
  733. $minCharacterSetsNum . '. Classes of characters: Lower Case, Upper Case, Digits, Special Characters.');
  734. }
  735. $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
  736. $this->accountManagement->createAccount($customer, $password);
  737. }
  738. /**
  739. * @throws \Magento\Framework\Exception\LocalizedException
  740. */
  741. public function testCreateAccountInputExceptionExtraLongPassword()
  742. {
  743. $password = '257*chars*************************************************************************************'
  744. . '****************************************************************************************************'
  745. . '***************************************************************';
  746. $this->string->expects($this->any())
  747. ->method('strlen')
  748. ->with($password)
  749. ->willReturn(iconv_strlen($password, 'UTF-8'));
  750. $this->expectException(\Magento\Framework\Exception\InputException::class);
  751. $this->expectExceptionMessage('Please enter a password with at most 256 characters.');
  752. $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
  753. $this->accountManagement->createAccount($customer, $password);
  754. }
  755. /**
  756. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  757. */
  758. public function testCreateAccountWithPassword()
  759. {
  760. $websiteId = 1;
  761. $storeId = null;
  762. $defaultStoreId = 1;
  763. $customerId = 1;
  764. $customerEmail = 'email@email.com';
  765. $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
  766. $newLinkToken = '2jh43j5h2345jh23lh452h345hfuzasd96ofu';
  767. $templateIdentifier = 'Template Identifier';
  768. $sender = 'Sender';
  769. $password = 'wrfewqedf1';
  770. $minPasswordLength = 5;
  771. $minCharacterSetsNum = 2;
  772. $datetime = $this->prepareDateTimeFactory();
  773. $this->scopeConfig->expects($this->any())
  774. ->method('getValue')
  775. ->willReturnMap(
  776. [
  777. [
  778. AccountManagement::XML_PATH_MINIMUM_PASSWORD_LENGTH,
  779. 'default',
  780. null,
  781. $minPasswordLength,
  782. ],
  783. [
  784. AccountManagement::XML_PATH_REQUIRED_CHARACTER_CLASSES_NUMBER,
  785. 'default',
  786. null,
  787. $minCharacterSetsNum,
  788. ],
  789. [
  790. AccountManagement::XML_PATH_REGISTER_EMAIL_TEMPLATE,
  791. ScopeInterface::SCOPE_STORE,
  792. $defaultStoreId,
  793. $templateIdentifier,
  794. ],
  795. [
  796. AccountManagement::XML_PATH_REGISTER_EMAIL_IDENTITY,
  797. ScopeInterface::SCOPE_STORE,
  798. 1,
  799. $sender,
  800. ],
  801. ]
  802. );
  803. $this->string->expects($this->any())
  804. ->method('strlen')
  805. ->with($password)
  806. ->willReturn(iconv_strlen($password, 'UTF-8'));
  807. $this->encryptor->expects($this->once())
  808. ->method('getHash')
  809. ->with($password, true)
  810. ->willReturn($hash);
  811. $address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  812. ->disableOriginalConstructor()
  813. ->getMock();
  814. $address->expects($this->once())
  815. ->method('setCustomerId')
  816. ->with($customerId);
  817. $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
  818. $store->expects($this->once())
  819. ->method('getId')
  820. ->willReturn($defaultStoreId);
  821. $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
  822. $website->expects($this->atLeastOnce())
  823. ->method('getStoreIds')
  824. ->willReturn([1, 2, 3]);
  825. $website->expects($this->once())
  826. ->method('getDefaultStore')
  827. ->willReturn($store);
  828. $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
  829. $customer->expects($this->atLeastOnce())
  830. ->method('getId')
  831. ->willReturn($customerId);
  832. $customer->expects($this->atLeastOnce())
  833. ->method('getEmail')
  834. ->willReturn($customerEmail);
  835. $customer->expects($this->atLeastOnce())
  836. ->method('getWebsiteId')
  837. ->willReturn($websiteId);
  838. $customer->expects($this->atLeastOnce())
  839. ->method('getStoreId')
  840. ->willReturn($storeId);
  841. $customer->expects($this->once())
  842. ->method('setStoreId')
  843. ->with($defaultStoreId);
  844. $customer->expects($this->once())
  845. ->method('getAddresses')
  846. ->willReturn([$address]);
  847. $customer->expects($this->once())
  848. ->method('setAddresses')
  849. ->with(null);
  850. $this->customerRepository
  851. ->expects($this->once())
  852. ->method('get')
  853. ->with($customerEmail)
  854. ->willReturn($customer);
  855. $this->share->expects($this->once())
  856. ->method('isWebsiteScope')
  857. ->willReturn(true);
  858. $this->storeManager->expects($this->atLeastOnce())
  859. ->method('getWebsite')
  860. ->with($websiteId)
  861. ->willReturn($website);
  862. $this->customerRepository->expects($this->atLeastOnce())
  863. ->method('save')
  864. ->willReturn($customer);
  865. $this->addressRepository->expects($this->atLeastOnce())
  866. ->method('save')
  867. ->with($address);
  868. $this->customerRepository->expects($this->once())
  869. ->method('getById')
  870. ->with($customerId)
  871. ->willReturn($customer);
  872. $this->random->expects($this->once())
  873. ->method('getUniqueHash')
  874. ->willReturn($newLinkToken);
  875. $customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
  876. ->setMethods(['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash'])
  877. ->disableOriginalConstructor()
  878. ->getMock();
  879. $customerSecure->expects($this->any())
  880. ->method('setRpToken')
  881. ->with($newLinkToken);
  882. $customerSecure->expects($this->any())
  883. ->method('setRpTokenCreatedAt')
  884. ->with($datetime)
  885. ->willReturnSelf();
  886. $customerSecure->expects($this->any())
  887. ->method('getPasswordHash')
  888. ->willReturn($hash);
  889. $this->customerRegistry->expects($this->atLeastOnce())
  890. ->method('retrieveSecureData')
  891. ->willReturn($customerSecure);
  892. $this->emailNotificationMock->expects($this->once())
  893. ->method('newAccount')
  894. ->willReturnSelf();
  895. $this->accountManagement->createAccount($customer, $password);
  896. }
  897. /**
  898. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  899. */
  900. public function testSendPasswordReminderEmail()
  901. {
  902. $customerId = 1;
  903. $customerStoreId = 2;
  904. $customerEmail = 'email@email.com';
  905. $customerData = ['key' => 'value'];
  906. $customerName = 'Customer Name';
  907. $templateIdentifier = 'Template Identifier';
  908. $sender = 'Sender';
  909. $customer = $this->getMockBuilder(Customer::class)
  910. ->disableOriginalConstructor()
  911. ->getMock();
  912. $customer->expects($this->any())
  913. ->method('getStoreId')
  914. ->willReturn($customerStoreId);
  915. $customer->expects($this->any())
  916. ->method('getId')
  917. ->willReturn($customerId);
  918. $customer->expects($this->any())
  919. ->method('getEmail')
  920. ->willReturn($customerEmail);
  921. $this->store->expects($this->any())
  922. ->method('getId')
  923. ->willReturn($customerStoreId);
  924. $this->storeManager->expects($this->at(0))
  925. ->method('getStore')
  926. ->willReturn($this->store);
  927. $this->storeManager->expects($this->at(1))
  928. ->method('getStore')
  929. ->with($customerStoreId)
  930. ->willReturn($this->store);
  931. $this->customerRegistry->expects($this->once())
  932. ->method('retrieveSecureData')
  933. ->with($customerId)
  934. ->willReturn($this->customerSecure);
  935. $this->dataObjectProcessor->expects($this->once())
  936. ->method('buildOutputDataArray')
  937. ->with($customer, CustomerInterface::class)
  938. ->willReturn($customerData);
  939. $this->customerViewHelper->expects($this->any())
  940. ->method('getCustomerName')
  941. ->with($customer)
  942. ->willReturn($customerName);
  943. $this->customerSecure->expects($this->once())
  944. ->method('addData')
  945. ->with($customerData)
  946. ->willReturnSelf();
  947. $this->customerSecure->expects($this->once())
  948. ->method('setData')
  949. ->with('name', $customerName)
  950. ->willReturnSelf();
  951. $this->scopeConfig->expects($this->at(0))
  952. ->method('getValue')
  953. ->with(AccountManagement::XML_PATH_REMIND_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $customerStoreId)
  954. ->willReturn($templateIdentifier);
  955. $this->scopeConfig->expects($this->at(1))
  956. ->method('getValue')
  957. ->with(AccountManagement::XML_PATH_FORGOT_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, $customerStoreId)
  958. ->willReturn($sender);
  959. $transport = $this->getMockBuilder(\Magento\Framework\Mail\TransportInterface::class)
  960. ->getMock();
  961. $this->transportBuilder->expects($this->once())
  962. ->method('setTemplateIdentifier')
  963. ->with($templateIdentifier)
  964. ->willReturnSelf();
  965. $this->transportBuilder->expects($this->once())
  966. ->method('setTemplateOptions')
  967. ->with(['area' => Area::AREA_FRONTEND, 'store' => $customerStoreId])
  968. ->willReturnSelf();
  969. $this->transportBuilder->expects($this->once())
  970. ->method('setTemplateVars')
  971. ->with(['customer' => $this->customerSecure, 'store' => $this->store])
  972. ->willReturnSelf();
  973. $this->transportBuilder->expects($this->once())
  974. ->method('setFrom')
  975. ->with($sender)
  976. ->willReturnSelf();
  977. $this->transportBuilder->expects($this->once())
  978. ->method('addTo')
  979. ->with($customerEmail, $customerName)
  980. ->willReturnSelf();
  981. $this->transportBuilder->expects($this->once())
  982. ->method('getTransport')
  983. ->willReturn($transport);
  984. $transport->expects($this->once())
  985. ->method('sendMessage');
  986. $this->assertEquals($this->accountManagement, $this->accountManagement->sendPasswordReminderEmail($customer));
  987. }
  988. /**
  989. * @param string $email
  990. * @param string $templateIdentifier
  991. * @param string $sender
  992. * @param int $storeId
  993. * @param int $customerId
  994. * @param string $hash
  995. */
  996. protected function prepareInitiatePasswordReset($email, $templateIdentifier, $sender, $storeId, $customerId, $hash)
  997. {
  998. $websiteId = 1;
  999. $addressId = 5;
  1000. $datetime = $this->prepareDateTimeFactory();
  1001. $customerData = ['key' => 'value'];
  1002. $customerName = 'Customer Name';
  1003. $this->store->expects($this->once())
  1004. ->method('getWebsiteId')
  1005. ->willReturn($websiteId);
  1006. $this->store->expects($this->any())
  1007. ->method('getId')
  1008. ->willReturn($storeId);
  1009. $this->storeManager->expects($this->any())
  1010. ->method('getStore')
  1011. ->willReturn($this->store);
  1012. /** @var \Magento\Customer\Model\Address|\PHPUnit_Framework_MockObject_MockObject $addressModel */
  1013. $addressModel = $this->getMockBuilder(\Magento\Customer\Model\Address::class)->disableOriginalConstructor()
  1014. ->setMethods(['setShouldIgnoreValidation'])->getMock();
  1015. /** @var \Magento\Customer\Api\Data\AddressInterface|\PHPUnit_Framework_MockObject_MockObject $customer */
  1016. $address = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
  1017. $address->expects($this->once())
  1018. ->method('getId')
  1019. ->willReturn($addressId);
  1020. /** @var Customer|\PHPUnit_Framework_MockObject_MockObject $customer */
  1021. $customer = $this->getMockBuilder(Customer::class)
  1022. ->disableOriginalConstructor()
  1023. ->getMock();
  1024. $customer->expects($this->any())
  1025. ->method('getEmail')
  1026. ->willReturn($email);
  1027. $customer->expects($this->any())
  1028. ->method('getId')
  1029. ->willReturn($customerId);
  1030. $customer->expects($this->any())
  1031. ->method('getStoreId')
  1032. ->willReturn($storeId);
  1033. $customer->expects($this->any())
  1034. ->method('getAddresses')
  1035. ->willReturn([$address]);
  1036. $this->customerRepository->expects($this->once())
  1037. ->method('get')
  1038. ->willReturn($customer);
  1039. $this->addressRegistryMock->expects($this->once())
  1040. ->method('retrieve')
  1041. ->with($addressId)
  1042. ->willReturn($addressModel);
  1043. $addressModel->expects($this->once())
  1044. ->method('setShouldIgnoreValidation')
  1045. ->with(true);
  1046. $this->customerRepository->expects($this->once())
  1047. ->method('get')
  1048. ->with($email, $websiteId)
  1049. ->willReturn($customer);
  1050. $this->customerRepository->expects($this->once())
  1051. ->method('save')
  1052. ->with($customer)
  1053. ->willReturnSelf();
  1054. $this->random->expects($this->once())
  1055. ->method('getUniqueHash')
  1056. ->willReturn($hash);
  1057. $this->customerViewHelper->expects($this->any())
  1058. ->method('getCustomerName')
  1059. ->with($customer)
  1060. ->willReturn($customerName);
  1061. $this->customerSecure->expects($this->any())
  1062. ->method('setRpToken')
  1063. ->with($hash)
  1064. ->willReturnSelf();
  1065. $this->customerSecure->expects($this->any())
  1066. ->method('setRpTokenCreatedAt')
  1067. ->with($datetime)
  1068. ->willReturnSelf();
  1069. $this->customerSecure->expects($this->any())
  1070. ->method('addData')
  1071. ->with($customerData)
  1072. ->willReturnSelf();
  1073. $this->customerSecure->expects($this->any())
  1074. ->method('setData')
  1075. ->with('name', $customerName)
  1076. ->willReturnSelf();
  1077. $this->customerRegistry->expects($this->any())
  1078. ->method('retrieveSecureData')
  1079. ->with($customerId)
  1080. ->willReturn($this->customerSecure);
  1081. $this->dataObjectProcessor->expects($this->any())
  1082. ->method('buildOutputDataArray')
  1083. ->with($customer, Customer::class)
  1084. ->willReturn($customerData);
  1085. $this->prepareEmailSend($email, $templateIdentifier, $sender, $storeId, $customerName);
  1086. }
  1087. /**
  1088. * @param string $email
  1089. * @param int $templateIdentifier
  1090. * @param string $sender
  1091. * @param int $storeId
  1092. * @param string $customerName
  1093. */
  1094. protected function prepareEmailSend($email, $templateIdentifier, $sender, $storeId, $customerName)
  1095. {
  1096. $transport = $this->getMockBuilder(\Magento\Framework\Mail\TransportInterface::class)
  1097. ->getMock();
  1098. $this->transportBuilder->expects($this->any())
  1099. ->method('setTemplateIdentifier')
  1100. ->with($templateIdentifier)
  1101. ->willReturnSelf();
  1102. $this->transportBuilder->expects($this->any())
  1103. ->method('setTemplateOptions')
  1104. ->with(['area' => Area::AREA_FRONTEND, 'store' => $storeId])
  1105. ->willReturnSelf();
  1106. $this->transportBuilder->expects($this->any())
  1107. ->method('setTemplateVars')
  1108. ->with(['customer' => $this->customerSecure, 'store' => $this->store])
  1109. ->willReturnSelf();
  1110. $this->transportBuilder->expects($this->any())
  1111. ->method('setFrom')
  1112. ->with($sender)
  1113. ->willReturnSelf();
  1114. $this->transportBuilder->expects($this->any())
  1115. ->method('addTo')
  1116. ->with($email, $customerName)
  1117. ->willReturnSelf();
  1118. $this->transportBuilder->expects($this->any())
  1119. ->method('getTransport')
  1120. ->willReturn($transport);
  1121. $transport->expects($this->any())
  1122. ->method('sendMessage');
  1123. }
  1124. /**
  1125. * @throws \Magento\Framework\Exception\LocalizedException
  1126. */
  1127. public function testInitiatePasswordResetEmailReminder()
  1128. {
  1129. $customerId = 1;
  1130. $email = 'test@example.com';
  1131. $template = AccountManagement::EMAIL_REMINDER;
  1132. $templateIdentifier = 'Template Identifier';
  1133. $sender = 'Sender';
  1134. $storeId = 1;
  1135. mt_srand(mt_rand() + (100000000 * (float)microtime()) % PHP_INT_MAX);
  1136. $hash = md5(uniqid(microtime() . mt_rand(0, mt_getrandmax()), true));
  1137. $this->emailNotificationMock->expects($this->once())
  1138. ->method('passwordReminder')
  1139. ->willReturnSelf();
  1140. $this->prepareInitiatePasswordReset($email, $templateIdentifier, $sender, $storeId, $customerId, $hash);
  1141. $this->assertTrue($this->accountManagement->initiatePasswordReset($email, $template));
  1142. }
  1143. /**
  1144. * @throws \Magento\Framework\Exception\LocalizedException
  1145. */
  1146. public function testInitiatePasswordResetEmailReset()
  1147. {
  1148. $storeId = 1;
  1149. $customerId = 1;
  1150. $email = 'test@example.com';
  1151. $template = AccountManagement::EMAIL_RESET;
  1152. $templateIdentifier = 'Template Identifier';
  1153. $sender = 'Sender';
  1154. mt_srand(mt_rand() + (100000000 * (float)microtime()) % PHP_INT_MAX);
  1155. $hash = md5(uniqid(microtime() . mt_rand(0, mt_getrandmax()), true));
  1156. $this->emailNotificationMock->expects($this->once())
  1157. ->method('passwordResetConfirmation')
  1158. ->willReturnSelf();
  1159. $this->prepareInitiatePasswordReset($email, $templateIdentifier, $sender, $storeId, $customerId, $hash);
  1160. $this->assertTrue($this->accountManagement->initiatePasswordReset($email, $template));
  1161. }
  1162. /**
  1163. * @throws \Magento\Framework\Exception\LocalizedException
  1164. */
  1165. public function testInitiatePasswordResetNoTemplate()
  1166. {
  1167. $storeId = 1;
  1168. $customerId = 1;
  1169. $email = 'test@example.com';
  1170. $template = null;
  1171. $templateIdentifier = 'Template Identifier';
  1172. $sender = 'Sender';
  1173. mt_srand(mt_rand() + (100000000 * (float)microtime()) % PHP_INT_MAX);
  1174. $hash = md5(uniqid(microtime() . mt_rand(0, mt_getrandmax()), true));
  1175. $this->prepareInitiatePasswordReset($email, $templateIdentifier, $sender, $storeId, $customerId, $hash);
  1176. $this->expectException(\Magento\Framework\Exception\InputException::class);
  1177. $this->expectExceptionMessage(
  1178. 'Invalid value of "" provided for the template field. Possible values: email_reminder or email_reset.'
  1179. );
  1180. $this->accountManagement->initiatePasswordReset($email, $template);
  1181. }
  1182. /**
  1183. * @expectedException \Magento\Framework\Exception\InputException
  1184. * @expectedExceptionMessage Invalid value of "0" provided for the customerId field
  1185. */
  1186. public function testValidateResetPasswordTokenBadCustomerId()
  1187. {
  1188. $this->accountManagement->validateResetPasswordLinkToken(0, '');
  1189. }
  1190. /**
  1191. * @expectedException \Magento\Framework\Exception\InputException
  1192. * @expectedExceptionMessage "resetPasswordLinkToken" is required. Enter and try again.
  1193. */
  1194. public function testValidateResetPasswordTokenBadResetPasswordLinkToken()
  1195. {
  1196. $this->accountManagement->validateResetPasswordLinkToken(22, null);
  1197. }
  1198. /**
  1199. * @expectedException \Magento\Framework\Exception\State\InputMismatchException
  1200. * @expectedExceptionMessage The password token is mismatched. Reset and try again.
  1201. */
  1202. public function testValidateResetPasswordTokenTokenMismatch()
  1203. {
  1204. $this->customerRegistry->expects($this->atLeastOnce())
  1205. ->method('retrieveSecureData')
  1206. ->willReturn($this->customerSecure);
  1207. $this->accountManagement->validateResetPasswordLinkToken(22, 'newStringToken');
  1208. }
  1209. /**
  1210. * @expectedException \Magento\Framework\Exception\State\ExpiredException
  1211. * @expectedExceptionMessage The password token is expired. Reset and try again.
  1212. */
  1213. public function testValidateResetPasswordTokenTokenExpired()
  1214. {
  1215. $this->reInitModel();
  1216. $this->customerRegistry->expects($this->atLeastOnce())
  1217. ->method('retrieveSecureData')
  1218. ->willReturn($this->customerSecure);
  1219. $this->accountManagement->validateResetPasswordLinkToken(22, 'newStringToken');
  1220. }
  1221. /**
  1222. * return bool
  1223. */
  1224. public function testValidateResetPasswordToken()
  1225. {
  1226. $this->reInitModel();
  1227. $this->customer
  1228. ->expects($this->once())
  1229. ->method('getResetPasswordLinkExpirationPeriod')
  1230. ->willReturn(100000);
  1231. $this->customerRegistry->expects($this->atLeastOnce())
  1232. ->method('retrieveSecureData')
  1233. ->willReturn($this->customerSecure);
  1234. $this->assertTrue($this->accountManagement->validateResetPasswordLinkToken(22, 'newStringToken'));
  1235. }
  1236. /**
  1237. * reInit $this->accountManagement object
  1238. */
  1239. private function reInitModel()
  1240. {
  1241. $this->customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
  1242. ->disableOriginalConstructor()
  1243. ->setMethods(
  1244. [
  1245. 'getRpToken',
  1246. 'getRpTokenCreatedAt',
  1247. 'getPasswordHash',
  1248. 'setPasswordHash',
  1249. 'setRpToken',
  1250. 'setRpTokenCreatedAt',
  1251. ]
  1252. )
  1253. ->getMock();
  1254. $this->customerSecure->expects($this->any())
  1255. ->method('getRpToken')
  1256. ->willReturn('newStringToken');
  1257. $pastDateTime = '2016-10-25 00:00:00';
  1258. $this->customerSecure->expects($this->any())
  1259. ->method('getRpTokenCreatedAt')
  1260. ->willReturn($pastDateTime);
  1261. $this->customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
  1262. ->disableOriginalConstructor()
  1263. ->setMethods(['getResetPasswordLinkExpirationPeriod'])
  1264. ->getMock();
  1265. $this->prepareDateTimeFactory();
  1266. $this->sessionManager = $this->getMockBuilder(\Magento\Framework\Session\SessionManagerInterface::class)
  1267. ->disableOriginalConstructor()
  1268. ->getMockForAbstractClass();
  1269. $this->visitorCollectionFactory = $this->getMockBuilder(
  1270. \Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory::class
  1271. )
  1272. ->disableOriginalConstructor()
  1273. ->setMethods(['create'])
  1274. ->getMock();
  1275. $this->saveHandler = $this->getMockBuilder(\Magento\Framework\Session\SaveHandlerInterface::class)
  1276. ->disableOriginalConstructor()
  1277. ->setMethods(['destroy'])
  1278. ->getMockForAbstractClass();
  1279. $dateTime = '2017-10-25 18:57:08';
  1280. $timestamp = '1508983028';
  1281. $dateTimeMock = $this->getMockBuilder(\DateTime::class)
  1282. ->disableOriginalConstructor()
  1283. ->setMethods(['format', 'getTimestamp', 'setTimestamp'])
  1284. ->getMock();
  1285. $dateTimeMock->expects($this->any())
  1286. ->method('format')
  1287. ->with(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT)
  1288. ->willReturn($dateTime);
  1289. $dateTimeMock->expects($this->any())
  1290. ->method('getTimestamp')
  1291. ->willReturn($timestamp);
  1292. $dateTimeMock->expects($this->any())
  1293. ->method('setTimestamp')
  1294. ->willReturnSelf();
  1295. $dateTimeFactory = $this->getMockBuilder(DateTimeFactory::class)
  1296. ->disableOriginalConstructor()
  1297. ->setMethods(['create'])
  1298. ->getMock();
  1299. $dateTimeFactory->expects($this->any())->method('create')->willReturn($dateTimeMock);
  1300. $this->objectManagerHelper = new ObjectManagerHelper($this);
  1301. $this->accountManagement = $this->objectManagerHelper->getObject(
  1302. \Magento\Customer\Model\AccountManagement::class,
  1303. [
  1304. 'customerFactory' => $this->customerFactory,
  1305. 'customerRegistry' => $this->customerRegistry,
  1306. 'customerRepository' => $this->customerRepository,
  1307. 'customerModel' => $this->customer,
  1308. 'dateTimeFactory' => $dateTimeFactory,
  1309. 'stringHelper' => $this->string,
  1310. 'scopeConfig' => $this->scopeConfig,
  1311. 'sessionManager' => $this->sessionManager,
  1312. 'visitorCollectionFactory' => $this->visitorCollectionFactory,
  1313. 'saveHandler' => $this->saveHandler,
  1314. 'encryptor' => $this->encryptor,
  1315. 'dataProcessor' => $this->dataObjectProcessor,
  1316. 'storeManager' => $this->storeManager,
  1317. 'addressRegistry' => $this->addressRegistryMock,
  1318. 'transportBuilder' => $this->transportBuilder,
  1319. ]
  1320. );
  1321. $this->objectManagerHelper->setBackwardCompatibleProperty(
  1322. $this->accountManagement,
  1323. 'authentication',
  1324. $this->authenticationMock
  1325. );
  1326. }
  1327. /**
  1328. * @return void
  1329. */
  1330. public function testChangePassword()
  1331. {
  1332. $customerId = 7;
  1333. $email = 'test@example.com';
  1334. $currentPassword = '1234567';
  1335. $newPassword = 'abcdefg';
  1336. $passwordHash = '1a2b3f4c';
  1337. $this->reInitModel();
  1338. $customer = $this->getMockBuilder(CustomerInterface::class)
  1339. ->disableOriginalConstructor()
  1340. ->getMock();
  1341. $customer->expects($this->any())
  1342. ->method('getId')
  1343. ->willReturn($customerId);
  1344. $customer->expects($this->once())
  1345. ->method('getAddresses')
  1346. ->willReturn([]);
  1347. $this->customerRepository
  1348. ->expects($this->once())
  1349. ->method('get')
  1350. ->with($email)
  1351. ->willReturn($customer);
  1352. $this->authenticationMock->expects($this->once())
  1353. ->method('authenticate');
  1354. $this->customerSecure->expects($this->once())
  1355. ->method('setRpToken')
  1356. ->with(null);
  1357. $this->customerSecure->expects($this->once())
  1358. ->method('setRpTokenCreatedAt')
  1359. ->willReturnSelf();
  1360. $this->customerSecure->expects($this->any())
  1361. ->method('getPasswordHash')
  1362. ->willReturn($passwordHash);
  1363. $this->customerRegistry->expects($this->any())
  1364. ->method('retrieveSecureData')
  1365. ->with($customerId)
  1366. ->willReturn($this->customerSecure);
  1367. $this->scopeConfig->expects($this->any())
  1368. ->method('getValue')
  1369. ->willReturnMap(
  1370. [
  1371. [
  1372. AccountManagement::XML_PATH_MINIMUM_PASSWORD_LENGTH,
  1373. 'default',
  1374. null,
  1375. 7,
  1376. ],
  1377. [
  1378. AccountManagement::XML_PATH_REQUIRED_CHARACTER_CLASSES_NUMBER,
  1379. 'default',
  1380. null,
  1381. 1,
  1382. ],
  1383. ]
  1384. );
  1385. $this->string->expects($this->any())
  1386. ->method('strlen')
  1387. ->with($newPassword)
  1388. ->willReturn(7);
  1389. $this->customerRepository
  1390. ->expects($this->once())
  1391. ->method('save')
  1392. ->with($customer);
  1393. $this->sessionManager->expects($this->atLeastOnce())->method('getSessionId');
  1394. $visitor = $this->getMockBuilder(\Magento\Customer\Model\Visitor::class)
  1395. ->disableOriginalConstructor()
  1396. ->setMethods(['getSessionId'])
  1397. ->getMock();
  1398. $visitor->expects($this->atLeastOnce())->method('getSessionId')
  1399. ->willReturnOnConsecutiveCalls('session_id_1', 'session_id_2');
  1400. $visitorCollection = $this->getMockBuilder(
  1401. \Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory::class
  1402. )
  1403. ->disableOriginalConstructor()->setMethods(['addFieldToFilter', 'getItems'])->getMock();
  1404. $visitorCollection->expects($this->atLeastOnce())->method('addFieldToFilter')->willReturnSelf();
  1405. $visitorCollection->expects($this->atLeastOnce())->method('getItems')->willReturn([$visitor, $visitor]);
  1406. $this->visitorCollectionFactory->expects($this->atLeastOnce())->method('create')
  1407. ->willReturn($visitorCollection);
  1408. $this->saveHandler->expects($this->atLeastOnce())->method('destroy')
  1409. ->withConsecutive(
  1410. ['session_id_1'],
  1411. ['session_id_2']
  1412. );
  1413. $this->assertTrue($this->accountManagement->changePassword($email, $currentPassword, $newPassword));
  1414. }
  1415. /**
  1416. * @throws \Magento\Framework\Exception\LocalizedException
  1417. */
  1418. public function testResetPassword()
  1419. {
  1420. $customerEmail = 'customer@example.com';
  1421. $customerId = '1';
  1422. $addressId = 5;
  1423. $resetToken = 'newStringToken';
  1424. $newPassword = 'new_password';
  1425. $this->reInitModel();
  1426. /** @var \Magento\Customer\Model\Address|\PHPUnit_Framework_MockObject_MockObject $addressModel */
  1427. $addressModel = $this->getMockBuilder(\Magento\Customer\Model\Address::class)->disableOriginalConstructor()
  1428. ->setMethods(['setShouldIgnoreValidation'])->getMock();
  1429. /** @var \Magento\Customer\Api\Data\AddressInterface|\PHPUnit_Framework_MockObject_MockObject $customer */
  1430. $address = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
  1431. $address->expects($this->any())
  1432. ->method('getId')
  1433. ->willReturn($addressId);
  1434. /** @var Customer|\PHPUnit_Framework_MockObject_MockObject $customer */
  1435. $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
  1436. $customer->expects($this->any())->method('getId')->willReturn($customerId);
  1437. $customer->expects($this->any())
  1438. ->method('getAddresses')
  1439. ->willReturn([$address]);
  1440. $this->addressRegistryMock->expects($this->once())
  1441. ->method('retrieve')
  1442. ->with($addressId)
  1443. ->willReturn($addressModel);
  1444. $addressModel->expects($this->once())
  1445. ->method('setShouldIgnoreValidation')
  1446. ->with(true);
  1447. $this->customerRepository->expects($this->atLeastOnce())->method('get')->with($customerEmail)
  1448. ->willReturn($customer);
  1449. $this->customer->expects($this->atLeastOnce())->method('getResetPasswordLinkExpirationPeriod')
  1450. ->willReturn(100000);
  1451. $this->string->expects($this->any())->method('strlen')->willReturnCallback(
  1452. function ($string) {
  1453. return strlen($string);
  1454. }
  1455. );
  1456. $this->customerRegistry->expects($this->atLeastOnce())->method('retrieveSecureData')
  1457. ->willReturn($this->customerSecure);
  1458. $this->customerSecure->expects($this->once())->method('setRpToken')->with(null);
  1459. $this->customerSecure->expects($this->once())->method('setRpTokenCreatedAt')->with(null);
  1460. $this->customerSecure->expects($this->any())->method('setPasswordHash')->willReturn(null);
  1461. $this->sessionManager->expects($this->atLeastOnce())->method('destroy');
  1462. $this->sessionManager->expects($this->atLeastOnce())->method('getSessionId');
  1463. $visitor = $this->getMockBuilder(\Magento\Customer\Model\Visitor::class)
  1464. ->disableOriginalConstructor()
  1465. ->setMethods(['getSessionId'])
  1466. ->getMock();
  1467. $visitor->expects($this->atLeastOnce())->method('getSessionId')
  1468. ->willReturnOnConsecutiveCalls('session_id_1', 'session_id_2');
  1469. $visitorCollection = $this->getMockBuilder(
  1470. \Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory::class
  1471. )
  1472. ->disableOriginalConstructor()->setMethods(['addFieldToFilter', 'getItems'])->getMock();
  1473. $visitorCollection->expects($this->atLeastOnce())->method('addFieldToFilter')->willReturnSelf();
  1474. $visitorCollection->expects($this->atLeastOnce())->method('getItems')->willReturn([$visitor, $visitor]);
  1475. $this->visitorCollectionFactory->expects($this->atLeastOnce())->method('create')
  1476. ->willReturn($visitorCollection);
  1477. $this->saveHandler->expects($this->atLeastOnce())->method('destroy')
  1478. ->withConsecutive(
  1479. ['session_id_1'],
  1480. ['session_id_2']
  1481. );
  1482. $this->assertTrue($this->accountManagement->resetPassword($customerEmail, $resetToken, $newPassword));
  1483. }
  1484. /**
  1485. * @return void
  1486. */
  1487. public function testChangePasswordException()
  1488. {
  1489. $email = 'test@example.com';
  1490. $currentPassword = '1234567';
  1491. $newPassword = 'abcdefg';
  1492. $exception = new NoSuchEntityException(
  1493. new \Magento\Framework\Phrase('Exception message')
  1494. );
  1495. $this->customerRepository
  1496. ->expects($this->once())
  1497. ->method('get')
  1498. ->with($email)
  1499. ->willThrowException($exception);
  1500. $this->expectException(\Magento\Framework\Exception\InvalidEmailOrPasswordException::class);
  1501. $this->expectExceptionMessage('Invalid login or password.');
  1502. $this->accountManagement->changePassword($email, $currentPassword, $newPassword);
  1503. }
  1504. /**
  1505. * @return void
  1506. */
  1507. public function testAuthenticate()
  1508. {
  1509. $username = 'login';
  1510. $password = '1234567';
  1511. $passwordHash = '1a2b3f4c';
  1512. $customerData = $this->getMockBuilder(Customer::class)
  1513. ->disableOriginalConstructor()
  1514. ->getMock();
  1515. $customerModel = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
  1516. ->disableOriginalConstructor()
  1517. ->getMock();
  1518. $customerModel->expects($this->once())
  1519. ->method('updateData')
  1520. ->willReturn($customerModel);
  1521. $this->customerRepository
  1522. ->expects($this->once())
  1523. ->method('get')
  1524. ->with($username)
  1525. ->willReturn($customerData);
  1526. $this->authenticationMock->expects($this->once())
  1527. ->method('authenticate');
  1528. $customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
  1529. ->setMethods(['getPasswordHash'])
  1530. ->disableOriginalConstructor()
  1531. ->getMock();
  1532. $customerSecure->expects($this->any())
  1533. ->method('getPasswordHash')
  1534. ->willReturn($passwordHash);
  1535. $this->customerRegistry->expects($this->any())
  1536. ->method('retrieveSecureData')
  1537. ->willReturn($customerSecure);
  1538. $this->customerFactory->expects($this->once())
  1539. ->method('create')
  1540. ->willReturn($customerModel);
  1541. $this->manager->expects($this->exactly(2))
  1542. ->method('dispatch')
  1543. ->withConsecutive(
  1544. [
  1545. 'customer_customer_authenticated',
  1546. ['model' => $customerModel, 'password' => $password],
  1547. ],
  1548. [
  1549. 'customer_data_object_login', ['customer' => $customerData],
  1550. ]
  1551. );
  1552. $this->assertEquals($customerData, $this->accountManagement->authenticate($username, $password));
  1553. }
  1554. /**
  1555. * @param int $isConfirmationRequired
  1556. * @param string|null $confirmation
  1557. * @param string $expected
  1558. * @dataProvider dataProviderGetConfirmationStatus
  1559. */
  1560. public function testGetConfirmationStatus(
  1561. $isConfirmationRequired,
  1562. $confirmation,
  1563. $expected
  1564. ) {
  1565. $websiteId = 1;
  1566. $customerId = 1;
  1567. $customerEmail = 'test1@example.com';
  1568. $customerMock = $this->getMockBuilder(Customer::class)
  1569. ->disableOriginalConstructor()
  1570. ->getMock();
  1571. $customerMock->expects($this->once())
  1572. ->method('getId')
  1573. ->willReturn($customerId);
  1574. $customerMock->expects($this->any())
  1575. ->method('getConfirmation')
  1576. ->willReturn($confirmation);
  1577. $customerMock->expects($this->once())
  1578. ->method('getEmail')
  1579. ->willReturn($customerEmail);
  1580. $customerMock->expects($this->once())
  1581. ->method('getWebsiteId')
  1582. ->willReturn($websiteId);
  1583. $this->accountConfirmation->expects($this->once())
  1584. ->method('isConfirmationRequired')
  1585. ->with($websiteId, $customerId, $customerEmail)
  1586. ->willReturn($isConfirmationRequired);
  1587. $this->customerRepository->expects($this->once())
  1588. ->method('getById')
  1589. ->with($customerId)
  1590. ->willReturn($customerMock);
  1591. $this->assertEquals($expected, $this->accountManagement->getConfirmationStatus($customerId));
  1592. }
  1593. /**
  1594. * @return array
  1595. */
  1596. public function dataProviderGetConfirmationStatus()
  1597. {
  1598. return [
  1599. [0, null, AccountManagement::ACCOUNT_CONFIRMATION_NOT_REQUIRED],
  1600. [0, null, AccountManagement::ACCOUNT_CONFIRMATION_NOT_REQUIRED],
  1601. [0, null, AccountManagement::ACCOUNT_CONFIRMATION_NOT_REQUIRED],
  1602. [1, null, AccountManagement::ACCOUNT_CONFIRMED],
  1603. [1, 'test', AccountManagement::ACCOUNT_CONFIRMATION_REQUIRED],
  1604. ];
  1605. }
  1606. /**
  1607. * @expectedException \Magento\Framework\Exception\LocalizedException
  1608. */
  1609. public function testCreateAccountWithPasswordHashForGuest()
  1610. {
  1611. $storeId = 1;
  1612. $storeName = 'store_name';
  1613. $websiteId = 1;
  1614. $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
  1615. $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  1616. ->disableOriginalConstructor()
  1617. ->getMock();
  1618. $storeMock->expects($this->once())
  1619. ->method('getId')
  1620. ->willReturn($storeId);
  1621. $storeMock->expects($this->once())
  1622. ->method('getWebsiteId')
  1623. ->willReturn($websiteId);
  1624. $storeMock->expects($this->once())
  1625. ->method('getName')
  1626. ->willReturn($storeName);
  1627. $this->storeManager->expects($this->exactly(3))
  1628. ->method('getStore')
  1629. ->willReturn($storeMock);
  1630. $customerMock = $this->getMockBuilder(Customer::class)
  1631. ->disableOriginalConstructor()
  1632. ->getMock();
  1633. $customerMock->expects($this->exactly(2))
  1634. ->method('getId')
  1635. ->willReturn(null);
  1636. $customerMock->expects($this->exactly(3))
  1637. ->method('getStoreId')
  1638. ->willReturn(null);
  1639. $customerMock->expects($this->exactly(3))
  1640. ->method('getWebsiteId')
  1641. ->willReturn(null);
  1642. $customerMock->expects($this->once())
  1643. ->method('setStoreId')
  1644. ->with($storeId)
  1645. ->willReturnSelf();
  1646. $customerMock->expects($this->once())
  1647. ->method('setWebsiteId')
  1648. ->with($websiteId)
  1649. ->willReturnSelf();
  1650. $customerMock->expects($this->once())
  1651. ->method('setCreatedIn')
  1652. ->with($storeName)
  1653. ->willReturnSelf();
  1654. $customerMock->expects($this->once())
  1655. ->method('getAddresses')
  1656. ->willReturn(null);
  1657. $customerMock->expects($this->once())
  1658. ->method('setAddresses')
  1659. ->with(null)
  1660. ->willReturnSelf();
  1661. $this->customerRepository
  1662. ->expects($this->once())
  1663. ->method('save')
  1664. ->with($customerMock, $hash)
  1665. ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('Exception message')));
  1666. $this->accountManagement->createAccountWithPasswordHash($customerMock, $hash);
  1667. }
  1668. /**
  1669. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  1670. */
  1671. public function testCreateAccountWithPasswordHashWithCustomerAddresses()
  1672. {
  1673. $websiteId = 1;
  1674. $addressId = 2;
  1675. $customerId = null;
  1676. $storeId = 1;
  1677. $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
  1678. $this->prepareDateTimeFactory();
  1679. //Handle store
  1680. $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
  1681. $store->expects($this->any())
  1682. ->method('getWebsiteId')
  1683. ->willReturn($websiteId);
  1684. //Handle address - existing and non-existing. Non-Existing should return null when call getId method
  1685. $existingAddress = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  1686. ->disableOriginalConstructor()
  1687. ->getMock();
  1688. $nonExistingAddress = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  1689. ->disableOriginalConstructor()
  1690. ->getMock();
  1691. //Ensure that existing address is not in use
  1692. $this->addressRepository
  1693. ->expects($this->atLeastOnce())
  1694. ->method("save")
  1695. ->withConsecutive(
  1696. [$this->logicalNot($this->identicalTo($existingAddress))],
  1697. [$this->identicalTo($nonExistingAddress)]
  1698. );
  1699. $existingAddress
  1700. ->expects($this->any())
  1701. ->method("getId")
  1702. ->willReturn($addressId);
  1703. //Expects that id for existing address should be unset
  1704. $existingAddress
  1705. ->expects($this->once())
  1706. ->method("setId")
  1707. ->with(null);
  1708. //Handle Customer calls
  1709. $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
  1710. $customer
  1711. ->expects($this->atLeastOnce())
  1712. ->method('getWebsiteId')
  1713. ->willReturn($websiteId);
  1714. $customer
  1715. ->expects($this->atLeastOnce())
  1716. ->method('getStoreId')
  1717. ->willReturn($storeId);
  1718. $customer
  1719. ->expects($this->any())
  1720. ->method("getId")
  1721. ->willReturn($customerId);
  1722. //Return Customer from customer repository
  1723. $this->customerRepository
  1724. ->expects($this->atLeastOnce())
  1725. ->method('save')
  1726. ->willReturn($customer);
  1727. $this->customerRepository
  1728. ->expects($this->once())
  1729. ->method('getById')
  1730. ->with($customerId)
  1731. ->willReturn($customer);
  1732. $customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
  1733. ->setMethods(['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash'])
  1734. ->disableOriginalConstructor()
  1735. ->getMock();
  1736. $customerSecure->expects($this->once())
  1737. ->method('setRpToken')
  1738. ->with($hash);
  1739. $customerSecure->expects($this->any())
  1740. ->method('getPasswordHash')
  1741. ->willReturn($hash);
  1742. $this->customerRegistry->expects($this->any())
  1743. ->method('retrieveSecureData')
  1744. ->with($customerId)
  1745. ->willReturn($customerSecure);
  1746. $this->random->expects($this->once())
  1747. ->method('getUniqueHash')
  1748. ->willReturn($hash);
  1749. $customer
  1750. ->expects($this->atLeastOnce())
  1751. ->method('getAddresses')
  1752. ->willReturn([$existingAddress, $nonExistingAddress]);
  1753. $this->storeManager
  1754. ->expects($this->atLeastOnce())
  1755. ->method('getStore')
  1756. ->willReturn($store);
  1757. $this->share
  1758. ->expects($this->once())
  1759. ->method('isWebsiteScope')
  1760. ->willReturn(true);
  1761. $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
  1762. $website->expects($this->once())
  1763. ->method('getStoreIds')
  1764. ->willReturn([1, 2, 3]);
  1765. $this->storeManager
  1766. ->expects($this->atLeastOnce())
  1767. ->method('getWebsite')
  1768. ->with($websiteId)
  1769. ->willReturn($website);
  1770. $this->assertSame($customer, $this->accountManagement->createAccountWithPasswordHash($customer, $hash));
  1771. }
  1772. /**
  1773. * @return string
  1774. */
  1775. private function prepareDateTimeFactory()
  1776. {
  1777. $dateTime = '2017-10-25 18:57:08';
  1778. $timestamp = '1508983028';
  1779. $dateTimeMock = $this->createMock(\DateTime::class);
  1780. $dateTimeMock->expects($this->any())
  1781. ->method('format')
  1782. ->with(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT)
  1783. ->willReturn($dateTime);
  1784. $dateTimeMock
  1785. ->expects($this->any())
  1786. ->method('getTimestamp')
  1787. ->willReturn($timestamp);
  1788. $this->dateTimeFactory
  1789. ->expects($this->any())
  1790. ->method('create')
  1791. ->willReturn($dateTimeMock);
  1792. return $dateTime;
  1793. }
  1794. /**
  1795. * @return void
  1796. */
  1797. public function testCreateAccountUnexpectedValueException(): void
  1798. {
  1799. $websiteId = 1;
  1800. $storeId = null;
  1801. $defaultStoreId = 1;
  1802. $customerId = 1;
  1803. $customerEmail = 'email@email.com';
  1804. $newLinkToken = '2jh43j5h2345jh23lh452h345hfuzasd96ofu';
  1805. $exception = new \UnexpectedValueException('Template file was not found');
  1806. $datetime = $this->prepareDateTimeFactory();
  1807. $address = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
  1808. $address->expects($this->once())
  1809. ->method('setCustomerId')
  1810. ->with($customerId);
  1811. $store = $this->createMock(\Magento\Store\Model\Store::class);
  1812. $store->expects($this->once())
  1813. ->method('getId')
  1814. ->willReturn($defaultStoreId);
  1815. $website = $this->createMock(\Magento\Store\Model\Website::class);
  1816. $website->expects($this->atLeastOnce())
  1817. ->method('getStoreIds')
  1818. ->willReturn([1, 2, 3]);
  1819. $website->expects($this->once())
  1820. ->method('getDefaultStore')
  1821. ->willReturn($store);
  1822. $customer = $this->createMock(Customer::class);
  1823. $customer->expects($this->atLeastOnce())
  1824. ->method('getId')
  1825. ->willReturn($customerId);
  1826. $customer->expects($this->atLeastOnce())
  1827. ->method('getEmail')
  1828. ->willReturn($customerEmail);
  1829. $customer->expects($this->atLeastOnce())
  1830. ->method('getWebsiteId')
  1831. ->willReturn($websiteId);
  1832. $customer->expects($this->atLeastOnce())
  1833. ->method('getStoreId')
  1834. ->willReturn($storeId);
  1835. $customer->expects($this->once())
  1836. ->method('setStoreId')
  1837. ->with($defaultStoreId);
  1838. $customer->expects($this->once())
  1839. ->method('getAddresses')
  1840. ->willReturn([$address]);
  1841. $customer->expects($this->once())
  1842. ->method('setAddresses')
  1843. ->with(null);
  1844. $this->customerRepository->expects($this->once())
  1845. ->method('get')
  1846. ->with($customerEmail)
  1847. ->willReturn($customer);
  1848. $this->share->expects($this->once())
  1849. ->method('isWebsiteScope')
  1850. ->willReturn(true);
  1851. $this->storeManager->expects($this->atLeastOnce())
  1852. ->method('getWebsite')
  1853. ->with($websiteId)
  1854. ->willReturn($website);
  1855. $this->customerRepository->expects($this->atLeastOnce())
  1856. ->method('save')
  1857. ->willReturn($customer);
  1858. $this->addressRepository->expects($this->atLeastOnce())
  1859. ->method('save')
  1860. ->with($address);
  1861. $this->customerRepository->expects($this->once())
  1862. ->method('getById')
  1863. ->with($customerId)
  1864. ->willReturn($customer);
  1865. $this->random->expects($this->once())
  1866. ->method('getUniqueHash')
  1867. ->willReturn($newLinkToken);
  1868. $customerSecure = $this->createPartialMock(
  1869. \Magento\Customer\Model\Data\CustomerSecure::class,
  1870. ['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash']
  1871. );
  1872. $customerSecure->expects($this->any())
  1873. ->method('setRpToken')
  1874. ->with($newLinkToken);
  1875. $customerSecure->expects($this->any())
  1876. ->method('setRpTokenCreatedAt')
  1877. ->with($datetime)
  1878. ->willReturnSelf();
  1879. $customerSecure->expects($this->any())
  1880. ->method('getPasswordHash')
  1881. ->willReturn(null);
  1882. $this->customerRegistry->expects($this->atLeastOnce())
  1883. ->method('retrieveSecureData')
  1884. ->willReturn($customerSecure);
  1885. $this->emailNotificationMock->expects($this->once())
  1886. ->method('newAccount')
  1887. ->willThrowException($exception);
  1888. $this->logger->expects($this->once())->method('error')->with($exception);
  1889. $this->accountManagement->createAccount($customer);
  1890. }
  1891. /**
  1892. * @expectedException \Magento\Framework\Exception\LocalizedException
  1893. */
  1894. public function testCreateAccountWithStoreNotInWebsite()
  1895. {
  1896. $storeId = 1;
  1897. $websiteId = 1;
  1898. $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
  1899. $customerMock = $this->getMockBuilder(Customer::class)
  1900. ->disableOriginalConstructor()
  1901. ->getMock();
  1902. $customerMock->expects($this->atLeastOnce())
  1903. ->method('getId')
  1904. ->willReturn(null);
  1905. $customerMock->expects($this->atLeastOnce())
  1906. ->method('getStoreId')
  1907. ->willReturn($storeId);
  1908. $customerMock->expects($this->atLeastOnce())
  1909. ->method('getWebsiteId')
  1910. ->willReturn($websiteId);
  1911. $this->share
  1912. ->expects($this->once())
  1913. ->method('isWebsiteScope')
  1914. ->willReturn(true);
  1915. $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
  1916. $website->expects($this->once())
  1917. ->method('getStoreIds')
  1918. ->willReturn([2, 3]);
  1919. $this->storeManager
  1920. ->expects($this->atLeastOnce())
  1921. ->method('getWebsite')
  1922. ->with($websiteId)
  1923. ->willReturn($website);
  1924. $this->accountManagement->createAccountWithPasswordHash($customerMock, $hash);
  1925. }
  1926. }