12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Model;
- use Magento\Customer\Api\Data\CustomerInterface;
- use Magento\Customer\Model\AccountConfirmation;
- use Magento\Customer\Model\AccountManagement;
- use Magento\Customer\Model\AuthenticationInterface;
- use Magento\Customer\Model\Data\Customer;
- use Magento\Customer\Model\EmailNotificationInterface;
- use Magento\Framework\Api\SearchCriteriaBuilder;
- use Magento\Framework\App\Area;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Framework\Intl\DateTimeFactory;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
- use Magento\Store\Model\ScopeInterface;
- /**
- * @SuppressWarnings(PHPMD.TooManyFields)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class AccountManagementTest extends \PHPUnit\Framework\TestCase
- {
- /** @var AccountManagement */
- protected $accountManagement;
- /** @var ObjectManagerHelper */
- protected $objectManagerHelper;
- /** @var \Magento\Customer\Model\CustomerFactory|\PHPUnit_Framework_MockObject_MockObject */
- protected $customerFactory;
- /** @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $manager;
- /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $storeManager;
- /** @var \Magento\Framework\Math\Random|\PHPUnit_Framework_MockObject_MockObject */
- protected $random;
- /** @var \Magento\Customer\Model\Metadata\Validator|\PHPUnit_Framework_MockObject_MockObject */
- protected $validator;
- /** @var \Magento\Customer\Api\Data\ValidationResultsInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject */
- protected $validationResultsInterfaceFactory;
- /** @var \Magento\Customer\Api\AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $addressRepository;
- /** @var \Magento\Customer\Api\CustomerMetadataInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $customerMetadata;
- /** @var \Magento\Customer\Model\CustomerRegistry|\PHPUnit_Framework_MockObject_MockObject */
- protected $customerRegistry;
- /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $logger;
- /** @var \Magento\Framework\Encryption\EncryptorInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $encryptor;
- /** @var \Magento\Customer\Model\Config\Share|\PHPUnit_Framework_MockObject_MockObject */
- protected $share;
- /** @var \Magento\Framework\Stdlib\StringUtils|\PHPUnit_Framework_MockObject_MockObject */
- protected $string;
- /** @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $customerRepository;
- /** @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $scopeConfig;
- /** @var \Magento\Framework\Mail\Template\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject */
- protected $transportBuilder;
- /** @var \Magento\Framework\Reflection\DataObjectProcessor|\PHPUnit_Framework_MockObject_MockObject */
- protected $dataObjectProcessor;
- /** @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject */
- protected $registry;
- /** @var \Magento\Customer\Helper\View|\PHPUnit_Framework_MockObject_MockObject */
- protected $customerViewHelper;
- /** @var \Magento\Framework\Stdlib\DateTime|\PHPUnit_Framework_MockObject_MockObject */
- protected $dateTime;
- /** @var \Magento\Customer\Model\Customer|\PHPUnit_Framework_MockObject_MockObject */
- protected $customer;
- /** @var \Magento\Framework\DataObjectFactory|\PHPUnit_Framework_MockObject_MockObject */
- protected $objectFactory;
- /** @var \Magento\Framework\Api\ExtensibleDataObjectConverter|\PHPUnit_Framework_MockObject_MockObject */
- protected $extensibleDataObjectConverter;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\Store
- */
- protected $store;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Model\Data\CustomerSecure
- */
- protected $customerSecure;
- /**
- * @var AuthenticationInterface |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $authenticationMock;
- /**
- * @var EmailNotificationInterface |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $emailNotificationMock;
- /**
- * @var DateTimeFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $dateTimeFactory;
- /**
- * @var AccountConfirmation|\PHPUnit_Framework_MockObject_MockObject
- */
- private $accountConfirmation;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Session\SessionManagerInterface
- */
- private $sessionManager;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory
- */
- private $visitorCollectionFactory;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Session\SaveHandlerInterface
- */
- private $saveHandler;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Model\AddressRegistry
- */
- private $addressRegistryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|SearchCriteriaBuilder
- */
- private $searchCriteriaBuilderMock;
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- protected function setUp()
- {
- $this->customerFactory = $this->createPartialMock(\Magento\Customer\Model\CustomerFactory::class, ['create']);
- $this->manager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
- $this->store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
- $this->random = $this->createMock(\Magento\Framework\Math\Random::class);
- $this->validator = $this->createMock(\Magento\Customer\Model\Metadata\Validator::class);
- $this->validationResultsInterfaceFactory = $this->createMock(
- \Magento\Customer\Api\Data\ValidationResultsInterfaceFactory::class
- );
- $this->addressRepository = $this->createMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
- $this->customerMetadata = $this->createMock(\Magento\Customer\Api\CustomerMetadataInterface::class);
- $this->customerRegistry = $this->createMock(\Magento\Customer\Model\CustomerRegistry::class);
- $this->logger = $this->createMock(\Psr\Log\LoggerInterface::class);
- $this->encryptor = $this->createMock(\Magento\Framework\Encryption\EncryptorInterface::class);
- $this->share = $this->createMock(\Magento\Customer\Model\Config\Share::class);
- $this->string = $this->createMock(\Magento\Framework\Stdlib\StringUtils::class);
- $this->customerRepository = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
- $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->transportBuilder = $this->createMock(\Magento\Framework\Mail\Template\TransportBuilder::class);
- $this->dataObjectProcessor = $this->createMock(\Magento\Framework\Reflection\DataObjectProcessor::class);
- $this->registry = $this->createMock(\Magento\Framework\Registry::class);
- $this->customerViewHelper = $this->createMock(\Magento\Customer\Helper\View::class);
- $this->dateTime = $this->createMock(\Magento\Framework\Stdlib\DateTime::class);
- $this->customer = $this->createMock(\Magento\Customer\Model\Customer::class);
- $this->objectFactory = $this->createMock(\Magento\Framework\DataObjectFactory::class);
- $this->addressRegistryMock = $this->createMock(\Magento\Customer\Model\AddressRegistry::class);
- $this->extensibleDataObjectConverter = $this->createMock(
- \Magento\Framework\Api\ExtensibleDataObjectConverter::class
- );
- $this->authenticationMock = $this->getMockBuilder(AuthenticationInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->emailNotificationMock = $this->getMockBuilder(EmailNotificationInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
- ->setMethods(['setRpToken', 'addData', 'setRpTokenCreatedAt', 'setData'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->dateTimeFactory = $this->createMock(DateTimeFactory::class);
- $this->accountConfirmation = $this->createMock(AccountConfirmation::class);
- $this->searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);
- $this->visitorCollectionFactory = $this->getMockBuilder(
- \Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory::class
- )
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->sessionManager = $this->getMockBuilder(\Magento\Framework\Session\SessionManagerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->saveHandler = $this->getMockBuilder(\Magento\Framework\Session\SaveHandlerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->objectManagerHelper = new ObjectManagerHelper($this);
- $this->accountManagement = $this->objectManagerHelper->getObject(
- \Magento\Customer\Model\AccountManagement::class,
- [
- 'customerFactory' => $this->customerFactory,
- 'eventManager' => $this->manager,
- 'storeManager' => $this->storeManager,
- 'mathRandom' => $this->random,
- 'validator' => $this->validator,
- 'validationResultsDataFactory' => $this->validationResultsInterfaceFactory,
- 'addressRepository' => $this->addressRepository,
- 'customerMetadataService' => $this->customerMetadata,
- 'customerRegistry' => $this->customerRegistry,
- 'logger' => $this->logger,
- 'encryptor' => $this->encryptor,
- 'configShare' => $this->share,
- 'stringHelper' => $this->string,
- 'customerRepository' => $this->customerRepository,
- 'scopeConfig' => $this->scopeConfig,
- 'transportBuilder' => $this->transportBuilder,
- 'dataProcessor' => $this->dataObjectProcessor,
- 'registry' => $this->registry,
- 'customerViewHelper' => $this->customerViewHelper,
- 'dateTime' => $this->dateTime,
- 'customerModel' => $this->customer,
- 'objectFactory' => $this->objectFactory,
- 'extensibleDataObjectConverter' => $this->extensibleDataObjectConverter,
- 'dateTimeFactory' => $this->dateTimeFactory,
- 'accountConfirmation' => $this->accountConfirmation,
- 'sessionManager' => $this->sessionManager,
- 'saveHandler' => $this->saveHandler,
- 'visitorCollectionFactory' => $this->visitorCollectionFactory,
- 'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
- 'addressRegistry' => $this->addressRegistryMock,
- ]
- );
- $this->objectManagerHelper->setBackwardCompatibleProperty(
- $this->accountManagement,
- 'authentication',
- $this->authenticationMock
- );
- $this->objectManagerHelper->setBackwardCompatibleProperty(
- $this->accountManagement,
- 'emailNotification',
- $this->emailNotificationMock
- );
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- */
- public function testCreateAccountWithPasswordHashWithExistingCustomer()
- {
- $websiteId = 1;
- $storeId = 1;
- $customerId = 1;
- $customerEmail = 'email@email.com';
- $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
- $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
- $website->expects($this->once())
- ->method('getStoreIds')
- ->willReturn([1, 2, 3]);
- $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
- $customer->expects($this->once())
- ->method('getId')
- ->willReturn($customerId);
- $customer->expects($this->once())
- ->method('getEmail')
- ->willReturn($customerEmail);
- $customer->expects($this->once())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- $customer->expects($this->atLeastOnce())
- ->method('getStoreId')
- ->willReturn($storeId);
- $this->customerRepository
- ->expects($this->once())
- ->method('get')
- ->with($customerEmail)
- ->willReturn($customer);
- $this->share
- ->expects($this->once())
- ->method('isWebsiteScope')
- ->willReturn(true);
- $this->storeManager
- ->expects($this->once())
- ->method('getWebsite')
- ->with($websiteId)
- ->willReturn($website);
- $this->accountManagement->createAccountWithPasswordHash($customer, $hash);
- }
- /**
- * @expectedException \Magento\Framework\Exception\State\InputMismatchException
- */
- public function testCreateAccountWithPasswordHashWithCustomerWithoutStoreId()
- {
- $websiteId = 1;
- $storeId = null;
- $defaultStoreId = 1;
- $customerId = 1;
- $customerEmail = 'email@email.com';
- $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
- $address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
- $store->expects($this->once())
- ->method('getId')
- ->willReturn($defaultStoreId);
- $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
- $website->expects($this->atLeastOnce())
- ->method('getStoreIds')
- ->willReturn([1, 2, 3]);
- $website->expects($this->once())
- ->method('getDefaultStore')
- ->willReturn($store);
- $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
- $customer->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($customerId);
- $customer->expects($this->once())
- ->method('getEmail')
- ->willReturn($customerEmail);
- $customer->expects($this->atLeastOnce())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- $customer->expects($this->atLeastOnce())
- ->method('getStoreId')
- ->willReturn($storeId);
- $customer->expects($this->once())
- ->method('setStoreId')
- ->with($defaultStoreId);
- $customer
- ->expects($this->once())
- ->method('getAddresses')
- ->willReturn([$address]);
- $customer
- ->expects($this->once())
- ->method('setAddresses')
- ->with(null);
- $this->customerRepository
- ->expects($this->once())
- ->method('get')
- ->with($customerEmail)
- ->willReturn($customer);
- $this->share
- ->expects($this->atLeastOnce())
- ->method('isWebsiteScope')
- ->willReturn(true);
- $this->storeManager
- ->expects($this->atLeastOnce())
- ->method('getWebsite')
- ->with($websiteId)
- ->willReturn($website);
- $exception = new \Magento\Framework\Exception\AlreadyExistsException(
- new \Magento\Framework\Phrase('Exception message')
- );
- $this->customerRepository
- ->expects($this->once())
- ->method('save')
- ->with($customer, $hash)
- ->willThrowException($exception);
- $this->accountManagement->createAccountWithPasswordHash($customer, $hash);
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- */
- public function testCreateAccountWithPasswordHashWithLocalizedException()
- {
- $websiteId = 1;
- $storeId = null;
- $defaultStoreId = 1;
- $customerId = 1;
- $customerEmail = 'email@email.com';
- $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
- $address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
- $store->expects($this->once())
- ->method('getId')
- ->willReturn($defaultStoreId);
- $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
- $website->expects($this->once())
- ->method('getStoreIds')
- ->willReturn([1, 2, 3]);
- $website->expects($this->once())
- ->method('getDefaultStore')
- ->willReturn($store);
- $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
- $customer->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($customerId);
- $customer->expects($this->once())
- ->method('getEmail')
- ->willReturn($customerEmail);
- $customer->expects($this->atLeastOnce())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- $customer->expects($this->atLeastOnce())
- ->method('getStoreId')
- ->willReturn($storeId);
- $customer->expects($this->once())
- ->method('setStoreId')
- ->with($defaultStoreId);
- $customer
- ->expects($this->once())
- ->method('getAddresses')
- ->willReturn([$address]);
- $customer
- ->expects($this->once())
- ->method('setAddresses')
- ->with(null);
- $this->customerRepository
- ->expects($this->once())
- ->method('get')
- ->with($customerEmail)
- ->willReturn($customer);
- $this->share
- ->expects($this->once())
- ->method('isWebsiteScope')
- ->willReturn(true);
- $this->storeManager
- ->expects($this->atLeastOnce())
- ->method('getWebsite')
- ->with($websiteId)
- ->willReturn($website);
- $exception = new \Magento\Framework\Exception\LocalizedException(
- new \Magento\Framework\Phrase('Exception message')
- );
- $this->customerRepository
- ->expects($this->once())
- ->method('save')
- ->with($customer, $hash)
- ->willThrowException($exception);
- $this->accountManagement->createAccountWithPasswordHash($customer, $hash);
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- */
- public function testCreateAccountWithPasswordHashWithAddressException()
- {
- $websiteId = 1;
- $storeId = null;
- $defaultStoreId = 1;
- $customerId = 1;
- $customerEmail = 'email@email.com';
- $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
- $address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $address->expects($this->once())
- ->method('setCustomerId')
- ->with($customerId);
- $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
- $store->expects($this->once())
- ->method('getId')
- ->willReturn($defaultStoreId);
- $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
- $website->expects($this->once())
- ->method('getStoreIds')
- ->willReturn([1, 2, 3]);
- $website->expects($this->once())
- ->method('getDefaultStore')
- ->willReturn($store);
- $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
- $customer->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($customerId);
- $customer->expects($this->once())
- ->method('getEmail')
- ->willReturn($customerEmail);
- $customer->expects($this->atLeastOnce())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- $customer->expects($this->atLeastOnce())
- ->method('getStoreId')
- ->willReturn($storeId);
- $customer->expects($this->once())
- ->method('setStoreId')
- ->with($defaultStoreId);
- $customer
- ->expects($this->once())
- ->method('getAddresses')
- ->willReturn([$address]);
- $customer
- ->expects($this->once())
- ->method('setAddresses')
- ->with(null);
- $this->customerRepository
- ->expects($this->once())
- ->method('get')
- ->with($customerEmail)
- ->willReturn($customer);
- $this->share
- ->expects($this->once())
- ->method('isWebsiteScope')
- ->willReturn(true);
- $this->storeManager
- ->expects($this->atLeastOnce())
- ->method('getWebsite')
- ->with($websiteId)
- ->willReturn($website);
- $this->customerRepository
- ->expects($this->once())
- ->method('save')
- ->with($customer, $hash)
- ->willReturn($customer);
- $exception = new \Magento\Framework\Exception\InputException(
- new \Magento\Framework\Phrase('Exception message')
- );
- $this->addressRepository
- ->expects($this->atLeastOnce())
- ->method('save')
- ->with($address)
- ->willThrowException($exception);
- $this->customerRepository
- ->expects($this->once())
- ->method('delete')
- ->with($customer);
- $this->accountManagement->createAccountWithPasswordHash($customer, $hash);
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- */
- public function testCreateAccountWithPasswordHashWithNewCustomerAndLocalizedException()
- {
- $storeId = 1;
- $storeName = 'store_name';
- $websiteId = 1;
- $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
- $customerMock = $this->getMockBuilder(Customer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customerMock->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn(null);
- $customerMock->expects($this->atLeastOnce())
- ->method('getStoreId')
- ->willReturn($storeId);
- $customerMock->expects($this->atLeastOnce())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- $customerMock->expects($this->once())
- ->method('setCreatedIn')
- ->with($storeName)
- ->willReturnSelf();
- $customerMock->expects($this->once())
- ->method('getAddresses')
- ->willReturn([]);
- $customerMock->expects($this->once())
- ->method('setAddresses')
- ->with(null)
- ->willReturnSelf();
- $this->share
- ->expects($this->once())
- ->method('isWebsiteScope')
- ->willReturn(true);
- $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
- $website->expects($this->once())
- ->method('getStoreIds')
- ->willReturn([1, 2, 3]);
- $this->storeManager
- ->expects($this->atLeastOnce())
- ->method('getWebsite')
- ->with($websiteId)
- ->willReturn($website);
- $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->getMock();
- $storeMock->expects($this->once())
- ->method('getName')
- ->willReturn($storeName);
- $this->storeManager->expects($this->exactly(1))
- ->method('getStore')
- ->with($storeId)
- ->willReturn($storeMock);
- $exception = new \Magento\Framework\Exception\LocalizedException(
- new \Magento\Framework\Phrase('Exception message')
- );
- $this->customerRepository
- ->expects($this->once())
- ->method('save')
- ->with($customerMock, $hash)
- ->willThrowException($exception);
- $this->accountManagement->createAccountWithPasswordHash($customerMock, $hash);
- }
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testCreateAccountWithoutPassword()
- {
- $websiteId = 1;
- $storeId = null;
- $defaultStoreId = 1;
- $customerId = 1;
- $customerEmail = 'email@email.com';
- $newLinkToken = '2jh43j5h2345jh23lh452h345hfuzasd96ofu';
- $datetime = $this->prepareDateTimeFactory();
- $address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $address->expects($this->once())
- ->method('setCustomerId')
- ->with($customerId);
- $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
- $store->expects($this->once())
- ->method('getId')
- ->willReturn($defaultStoreId);
- $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
- $website->expects($this->atLeastOnce())
- ->method('getStoreIds')
- ->willReturn([1, 2, 3]);
- $website->expects($this->once())
- ->method('getDefaultStore')
- ->willReturn($store);
- $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
- $customer->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($customerId);
- $customer->expects($this->atLeastOnce())
- ->method('getEmail')
- ->willReturn($customerEmail);
- $customer->expects($this->atLeastOnce())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- $customer->expects($this->atLeastOnce())
- ->method('getStoreId')
- ->willReturn($storeId);
- $customer->expects($this->once())
- ->method('setStoreId')
- ->with($defaultStoreId);
- $customer->expects($this->once())
- ->method('getAddresses')
- ->willReturn([$address]);
- $customer->expects($this->once())
- ->method('setAddresses')
- ->with(null);
- $this->customerRepository
- ->expects($this->once())
- ->method('get')
- ->with($customerEmail)
- ->willReturn($customer);
- $this->share->expects($this->once())
- ->method('isWebsiteScope')
- ->willReturn(true);
- $this->storeManager->expects($this->atLeastOnce())
- ->method('getWebsite')
- ->with($websiteId)
- ->willReturn($website);
- $this->customerRepository->expects($this->atLeastOnce())
- ->method('save')
- ->willReturn($customer);
- $this->addressRepository->expects($this->atLeastOnce())
- ->method('save')
- ->with($address);
- $this->customerRepository->expects($this->once())
- ->method('getById')
- ->with($customerId)
- ->willReturn($customer);
- $this->random->expects($this->once())
- ->method('getUniqueHash')
- ->willReturn($newLinkToken);
- $customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
- ->setMethods(['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash'])
- ->disableOriginalConstructor()
- ->getMock();
- $customerSecure->expects($this->any())
- ->method('setRpToken')
- ->with($newLinkToken);
- $customerSecure->expects($this->any())
- ->method('setRpTokenCreatedAt')
- ->with($datetime)
- ->willReturnSelf();
- $customerSecure->expects($this->any())
- ->method('getPasswordHash')
- ->willReturn(null);
- $this->customerRegistry->expects($this->atLeastOnce())
- ->method('retrieveSecureData')
- ->willReturn($customerSecure);
- $this->emailNotificationMock->expects($this->once())
- ->method('newAccount')
- ->willReturnSelf();
- $this->accountManagement->createAccount($customer);
- }
- /**
- * Data provider for testCreateAccountWithPasswordInputException test
- *
- * @return array
- */
- public function dataProviderCheckPasswordStrength()
- {
- return [
- [
- 'testNumber' => 1,
- 'password' => 'qwer',
- 'minPasswordLength' => 5,
- 'minCharacterSetsNum' => 1,
- ],
- [
- 'testNumber' => 2,
- 'password' => 'wrfewqedf1',
- 'minPasswordLength' => 5,
- 'minCharacterSetsNum' => 3,
- ],
- ];
- }
- /**
- * @param int $testNumber
- * @param string $password
- * @param int $minPasswordLength
- * @param int $minCharacterSetsNum
- * @dataProvider dataProviderCheckPasswordStrength
- */
- public function testCreateAccountWithPasswordInputException(
- $testNumber,
- $password,
- $minPasswordLength,
- $minCharacterSetsNum
- ) {
- $this->scopeConfig->expects($this->any())
- ->method('getValue')
- ->will(
- $this->returnValueMap(
- [
- [
- AccountManagement::XML_PATH_MINIMUM_PASSWORD_LENGTH,
- 'default',
- null,
- $minPasswordLength,
- ],
- [
- AccountManagement::XML_PATH_REQUIRED_CHARACTER_CLASSES_NUMBER,
- 'default',
- null,
- $minCharacterSetsNum,
- ],
- ]
- )
- );
- $this->string->expects($this->any())
- ->method('strlen')
- ->with($password)
- ->willReturn(iconv_strlen($password, 'UTF-8'));
- if ($testNumber == 1) {
- $this->expectException(\Magento\Framework\Exception\InputException::class);
- $this->expectExceptionMessage('The password needs at least ' . $minPasswordLength . ' characters. '
- . 'Create a new password and try again.');
- }
- if ($testNumber == 2) {
- $this->expectException(\Magento\Framework\Exception\InputException::class);
- $this->expectExceptionMessage('Minimum of different classes of characters in password is ' .
- $minCharacterSetsNum . '. Classes of characters: Lower Case, Upper Case, Digits, Special Characters.');
- }
- $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
- $this->accountManagement->createAccount($customer, $password);
- }
- /**
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function testCreateAccountInputExceptionExtraLongPassword()
- {
- $password = '257*chars*************************************************************************************'
- . '****************************************************************************************************'
- . '***************************************************************';
- $this->string->expects($this->any())
- ->method('strlen')
- ->with($password)
- ->willReturn(iconv_strlen($password, 'UTF-8'));
- $this->expectException(\Magento\Framework\Exception\InputException::class);
- $this->expectExceptionMessage('Please enter a password with at most 256 characters.');
- $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
- $this->accountManagement->createAccount($customer, $password);
- }
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testCreateAccountWithPassword()
- {
- $websiteId = 1;
- $storeId = null;
- $defaultStoreId = 1;
- $customerId = 1;
- $customerEmail = 'email@email.com';
- $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
- $newLinkToken = '2jh43j5h2345jh23lh452h345hfuzasd96ofu';
- $templateIdentifier = 'Template Identifier';
- $sender = 'Sender';
- $password = 'wrfewqedf1';
- $minPasswordLength = 5;
- $minCharacterSetsNum = 2;
- $datetime = $this->prepareDateTimeFactory();
- $this->scopeConfig->expects($this->any())
- ->method('getValue')
- ->willReturnMap(
- [
- [
- AccountManagement::XML_PATH_MINIMUM_PASSWORD_LENGTH,
- 'default',
- null,
- $minPasswordLength,
- ],
- [
- AccountManagement::XML_PATH_REQUIRED_CHARACTER_CLASSES_NUMBER,
- 'default',
- null,
- $minCharacterSetsNum,
- ],
- [
- AccountManagement::XML_PATH_REGISTER_EMAIL_TEMPLATE,
- ScopeInterface::SCOPE_STORE,
- $defaultStoreId,
- $templateIdentifier,
- ],
- [
- AccountManagement::XML_PATH_REGISTER_EMAIL_IDENTITY,
- ScopeInterface::SCOPE_STORE,
- 1,
- $sender,
- ],
- ]
- );
- $this->string->expects($this->any())
- ->method('strlen')
- ->with($password)
- ->willReturn(iconv_strlen($password, 'UTF-8'));
- $this->encryptor->expects($this->once())
- ->method('getHash')
- ->with($password, true)
- ->willReturn($hash);
- $address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $address->expects($this->once())
- ->method('setCustomerId')
- ->with($customerId);
- $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
- $store->expects($this->once())
- ->method('getId')
- ->willReturn($defaultStoreId);
- $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
- $website->expects($this->atLeastOnce())
- ->method('getStoreIds')
- ->willReturn([1, 2, 3]);
- $website->expects($this->once())
- ->method('getDefaultStore')
- ->willReturn($store);
- $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
- $customer->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($customerId);
- $customer->expects($this->atLeastOnce())
- ->method('getEmail')
- ->willReturn($customerEmail);
- $customer->expects($this->atLeastOnce())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- $customer->expects($this->atLeastOnce())
- ->method('getStoreId')
- ->willReturn($storeId);
- $customer->expects($this->once())
- ->method('setStoreId')
- ->with($defaultStoreId);
- $customer->expects($this->once())
- ->method('getAddresses')
- ->willReturn([$address]);
- $customer->expects($this->once())
- ->method('setAddresses')
- ->with(null);
- $this->customerRepository
- ->expects($this->once())
- ->method('get')
- ->with($customerEmail)
- ->willReturn($customer);
- $this->share->expects($this->once())
- ->method('isWebsiteScope')
- ->willReturn(true);
- $this->storeManager->expects($this->atLeastOnce())
- ->method('getWebsite')
- ->with($websiteId)
- ->willReturn($website);
- $this->customerRepository->expects($this->atLeastOnce())
- ->method('save')
- ->willReturn($customer);
- $this->addressRepository->expects($this->atLeastOnce())
- ->method('save')
- ->with($address);
- $this->customerRepository->expects($this->once())
- ->method('getById')
- ->with($customerId)
- ->willReturn($customer);
- $this->random->expects($this->once())
- ->method('getUniqueHash')
- ->willReturn($newLinkToken);
- $customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
- ->setMethods(['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash'])
- ->disableOriginalConstructor()
- ->getMock();
- $customerSecure->expects($this->any())
- ->method('setRpToken')
- ->with($newLinkToken);
- $customerSecure->expects($this->any())
- ->method('setRpTokenCreatedAt')
- ->with($datetime)
- ->willReturnSelf();
- $customerSecure->expects($this->any())
- ->method('getPasswordHash')
- ->willReturn($hash);
- $this->customerRegistry->expects($this->atLeastOnce())
- ->method('retrieveSecureData')
- ->willReturn($customerSecure);
- $this->emailNotificationMock->expects($this->once())
- ->method('newAccount')
- ->willReturnSelf();
- $this->accountManagement->createAccount($customer, $password);
- }
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testSendPasswordReminderEmail()
- {
- $customerId = 1;
- $customerStoreId = 2;
- $customerEmail = 'email@email.com';
- $customerData = ['key' => 'value'];
- $customerName = 'Customer Name';
- $templateIdentifier = 'Template Identifier';
- $sender = 'Sender';
- $customer = $this->getMockBuilder(Customer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customer->expects($this->any())
- ->method('getStoreId')
- ->willReturn($customerStoreId);
- $customer->expects($this->any())
- ->method('getId')
- ->willReturn($customerId);
- $customer->expects($this->any())
- ->method('getEmail')
- ->willReturn($customerEmail);
- $this->store->expects($this->any())
- ->method('getId')
- ->willReturn($customerStoreId);
- $this->storeManager->expects($this->at(0))
- ->method('getStore')
- ->willReturn($this->store);
- $this->storeManager->expects($this->at(1))
- ->method('getStore')
- ->with($customerStoreId)
- ->willReturn($this->store);
- $this->customerRegistry->expects($this->once())
- ->method('retrieveSecureData')
- ->with($customerId)
- ->willReturn($this->customerSecure);
- $this->dataObjectProcessor->expects($this->once())
- ->method('buildOutputDataArray')
- ->with($customer, CustomerInterface::class)
- ->willReturn($customerData);
- $this->customerViewHelper->expects($this->any())
- ->method('getCustomerName')
- ->with($customer)
- ->willReturn($customerName);
- $this->customerSecure->expects($this->once())
- ->method('addData')
- ->with($customerData)
- ->willReturnSelf();
- $this->customerSecure->expects($this->once())
- ->method('setData')
- ->with('name', $customerName)
- ->willReturnSelf();
- $this->scopeConfig->expects($this->at(0))
- ->method('getValue')
- ->with(AccountManagement::XML_PATH_REMIND_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $customerStoreId)
- ->willReturn($templateIdentifier);
- $this->scopeConfig->expects($this->at(1))
- ->method('getValue')
- ->with(AccountManagement::XML_PATH_FORGOT_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, $customerStoreId)
- ->willReturn($sender);
- $transport = $this->getMockBuilder(\Magento\Framework\Mail\TransportInterface::class)
- ->getMock();
- $this->transportBuilder->expects($this->once())
- ->method('setTemplateIdentifier')
- ->with($templateIdentifier)
- ->willReturnSelf();
- $this->transportBuilder->expects($this->once())
- ->method('setTemplateOptions')
- ->with(['area' => Area::AREA_FRONTEND, 'store' => $customerStoreId])
- ->willReturnSelf();
- $this->transportBuilder->expects($this->once())
- ->method('setTemplateVars')
- ->with(['customer' => $this->customerSecure, 'store' => $this->store])
- ->willReturnSelf();
- $this->transportBuilder->expects($this->once())
- ->method('setFrom')
- ->with($sender)
- ->willReturnSelf();
- $this->transportBuilder->expects($this->once())
- ->method('addTo')
- ->with($customerEmail, $customerName)
- ->willReturnSelf();
- $this->transportBuilder->expects($this->once())
- ->method('getTransport')
- ->willReturn($transport);
- $transport->expects($this->once())
- ->method('sendMessage');
- $this->assertEquals($this->accountManagement, $this->accountManagement->sendPasswordReminderEmail($customer));
- }
- /**
- * @param string $email
- * @param string $templateIdentifier
- * @param string $sender
- * @param int $storeId
- * @param int $customerId
- * @param string $hash
- */
- protected function prepareInitiatePasswordReset($email, $templateIdentifier, $sender, $storeId, $customerId, $hash)
- {
- $websiteId = 1;
- $addressId = 5;
- $datetime = $this->prepareDateTimeFactory();
- $customerData = ['key' => 'value'];
- $customerName = 'Customer Name';
- $this->store->expects($this->once())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- $this->store->expects($this->any())
- ->method('getId')
- ->willReturn($storeId);
- $this->storeManager->expects($this->any())
- ->method('getStore')
- ->willReturn($this->store);
- /** @var \Magento\Customer\Model\Address|\PHPUnit_Framework_MockObject_MockObject $addressModel */
- $addressModel = $this->getMockBuilder(\Magento\Customer\Model\Address::class)->disableOriginalConstructor()
- ->setMethods(['setShouldIgnoreValidation'])->getMock();
- /** @var \Magento\Customer\Api\Data\AddressInterface|\PHPUnit_Framework_MockObject_MockObject $customer */
- $address = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
- $address->expects($this->once())
- ->method('getId')
- ->willReturn($addressId);
- /** @var Customer|\PHPUnit_Framework_MockObject_MockObject $customer */
- $customer = $this->getMockBuilder(Customer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customer->expects($this->any())
- ->method('getEmail')
- ->willReturn($email);
- $customer->expects($this->any())
- ->method('getId')
- ->willReturn($customerId);
- $customer->expects($this->any())
- ->method('getStoreId')
- ->willReturn($storeId);
- $customer->expects($this->any())
- ->method('getAddresses')
- ->willReturn([$address]);
- $this->customerRepository->expects($this->once())
- ->method('get')
- ->willReturn($customer);
- $this->addressRegistryMock->expects($this->once())
- ->method('retrieve')
- ->with($addressId)
- ->willReturn($addressModel);
- $addressModel->expects($this->once())
- ->method('setShouldIgnoreValidation')
- ->with(true);
- $this->customerRepository->expects($this->once())
- ->method('get')
- ->with($email, $websiteId)
- ->willReturn($customer);
- $this->customerRepository->expects($this->once())
- ->method('save')
- ->with($customer)
- ->willReturnSelf();
- $this->random->expects($this->once())
- ->method('getUniqueHash')
- ->willReturn($hash);
- $this->customerViewHelper->expects($this->any())
- ->method('getCustomerName')
- ->with($customer)
- ->willReturn($customerName);
- $this->customerSecure->expects($this->any())
- ->method('setRpToken')
- ->with($hash)
- ->willReturnSelf();
- $this->customerSecure->expects($this->any())
- ->method('setRpTokenCreatedAt')
- ->with($datetime)
- ->willReturnSelf();
- $this->customerSecure->expects($this->any())
- ->method('addData')
- ->with($customerData)
- ->willReturnSelf();
- $this->customerSecure->expects($this->any())
- ->method('setData')
- ->with('name', $customerName)
- ->willReturnSelf();
- $this->customerRegistry->expects($this->any())
- ->method('retrieveSecureData')
- ->with($customerId)
- ->willReturn($this->customerSecure);
- $this->dataObjectProcessor->expects($this->any())
- ->method('buildOutputDataArray')
- ->with($customer, Customer::class)
- ->willReturn($customerData);
- $this->prepareEmailSend($email, $templateIdentifier, $sender, $storeId, $customerName);
- }
- /**
- * @param string $email
- * @param int $templateIdentifier
- * @param string $sender
- * @param int $storeId
- * @param string $customerName
- */
- protected function prepareEmailSend($email, $templateIdentifier, $sender, $storeId, $customerName)
- {
- $transport = $this->getMockBuilder(\Magento\Framework\Mail\TransportInterface::class)
- ->getMock();
- $this->transportBuilder->expects($this->any())
- ->method('setTemplateIdentifier')
- ->with($templateIdentifier)
- ->willReturnSelf();
- $this->transportBuilder->expects($this->any())
- ->method('setTemplateOptions')
- ->with(['area' => Area::AREA_FRONTEND, 'store' => $storeId])
- ->willReturnSelf();
- $this->transportBuilder->expects($this->any())
- ->method('setTemplateVars')
- ->with(['customer' => $this->customerSecure, 'store' => $this->store])
- ->willReturnSelf();
- $this->transportBuilder->expects($this->any())
- ->method('setFrom')
- ->with($sender)
- ->willReturnSelf();
- $this->transportBuilder->expects($this->any())
- ->method('addTo')
- ->with($email, $customerName)
- ->willReturnSelf();
- $this->transportBuilder->expects($this->any())
- ->method('getTransport')
- ->willReturn($transport);
- $transport->expects($this->any())
- ->method('sendMessage');
- }
- /**
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function testInitiatePasswordResetEmailReminder()
- {
- $customerId = 1;
- $email = 'test@example.com';
- $template = AccountManagement::EMAIL_REMINDER;
- $templateIdentifier = 'Template Identifier';
- $sender = 'Sender';
- $storeId = 1;
- mt_srand(mt_rand() + (100000000 * (float)microtime()) % PHP_INT_MAX);
- $hash = md5(uniqid(microtime() . mt_rand(0, mt_getrandmax()), true));
- $this->emailNotificationMock->expects($this->once())
- ->method('passwordReminder')
- ->willReturnSelf();
- $this->prepareInitiatePasswordReset($email, $templateIdentifier, $sender, $storeId, $customerId, $hash);
- $this->assertTrue($this->accountManagement->initiatePasswordReset($email, $template));
- }
- /**
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function testInitiatePasswordResetEmailReset()
- {
- $storeId = 1;
- $customerId = 1;
- $email = 'test@example.com';
- $template = AccountManagement::EMAIL_RESET;
- $templateIdentifier = 'Template Identifier';
- $sender = 'Sender';
- mt_srand(mt_rand() + (100000000 * (float)microtime()) % PHP_INT_MAX);
- $hash = md5(uniqid(microtime() . mt_rand(0, mt_getrandmax()), true));
- $this->emailNotificationMock->expects($this->once())
- ->method('passwordResetConfirmation')
- ->willReturnSelf();
- $this->prepareInitiatePasswordReset($email, $templateIdentifier, $sender, $storeId, $customerId, $hash);
- $this->assertTrue($this->accountManagement->initiatePasswordReset($email, $template));
- }
- /**
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function testInitiatePasswordResetNoTemplate()
- {
- $storeId = 1;
- $customerId = 1;
- $email = 'test@example.com';
- $template = null;
- $templateIdentifier = 'Template Identifier';
- $sender = 'Sender';
- mt_srand(mt_rand() + (100000000 * (float)microtime()) % PHP_INT_MAX);
- $hash = md5(uniqid(microtime() . mt_rand(0, mt_getrandmax()), true));
- $this->prepareInitiatePasswordReset($email, $templateIdentifier, $sender, $storeId, $customerId, $hash);
- $this->expectException(\Magento\Framework\Exception\InputException::class);
- $this->expectExceptionMessage(
- 'Invalid value of "" provided for the template field. Possible values: email_reminder or email_reset.'
- );
- $this->accountManagement->initiatePasswordReset($email, $template);
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- * @expectedExceptionMessage Invalid value of "0" provided for the customerId field
- */
- public function testValidateResetPasswordTokenBadCustomerId()
- {
- $this->accountManagement->validateResetPasswordLinkToken(0, '');
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- * @expectedExceptionMessage "resetPasswordLinkToken" is required. Enter and try again.
- */
- public function testValidateResetPasswordTokenBadResetPasswordLinkToken()
- {
- $this->accountManagement->validateResetPasswordLinkToken(22, null);
- }
- /**
- * @expectedException \Magento\Framework\Exception\State\InputMismatchException
- * @expectedExceptionMessage The password token is mismatched. Reset and try again.
- */
- public function testValidateResetPasswordTokenTokenMismatch()
- {
- $this->customerRegistry->expects($this->atLeastOnce())
- ->method('retrieveSecureData')
- ->willReturn($this->customerSecure);
- $this->accountManagement->validateResetPasswordLinkToken(22, 'newStringToken');
- }
- /**
- * @expectedException \Magento\Framework\Exception\State\ExpiredException
- * @expectedExceptionMessage The password token is expired. Reset and try again.
- */
- public function testValidateResetPasswordTokenTokenExpired()
- {
- $this->reInitModel();
- $this->customerRegistry->expects($this->atLeastOnce())
- ->method('retrieveSecureData')
- ->willReturn($this->customerSecure);
- $this->accountManagement->validateResetPasswordLinkToken(22, 'newStringToken');
- }
- /**
- * return bool
- */
- public function testValidateResetPasswordToken()
- {
- $this->reInitModel();
- $this->customer
- ->expects($this->once())
- ->method('getResetPasswordLinkExpirationPeriod')
- ->willReturn(100000);
- $this->customerRegistry->expects($this->atLeastOnce())
- ->method('retrieveSecureData')
- ->willReturn($this->customerSecure);
- $this->assertTrue($this->accountManagement->validateResetPasswordLinkToken(22, 'newStringToken'));
- }
- /**
- * reInit $this->accountManagement object
- */
- private function reInitModel()
- {
- $this->customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
- ->disableOriginalConstructor()
- ->setMethods(
- [
- 'getRpToken',
- 'getRpTokenCreatedAt',
- 'getPasswordHash',
- 'setPasswordHash',
- 'setRpToken',
- 'setRpTokenCreatedAt',
- ]
- )
- ->getMock();
- $this->customerSecure->expects($this->any())
- ->method('getRpToken')
- ->willReturn('newStringToken');
- $pastDateTime = '2016-10-25 00:00:00';
- $this->customerSecure->expects($this->any())
- ->method('getRpTokenCreatedAt')
- ->willReturn($pastDateTime);
- $this->customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
- ->disableOriginalConstructor()
- ->setMethods(['getResetPasswordLinkExpirationPeriod'])
- ->getMock();
- $this->prepareDateTimeFactory();
- $this->sessionManager = $this->getMockBuilder(\Magento\Framework\Session\SessionManagerInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->visitorCollectionFactory = $this->getMockBuilder(
- \Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory::class
- )
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->saveHandler = $this->getMockBuilder(\Magento\Framework\Session\SaveHandlerInterface::class)
- ->disableOriginalConstructor()
- ->setMethods(['destroy'])
- ->getMockForAbstractClass();
- $dateTime = '2017-10-25 18:57:08';
- $timestamp = '1508983028';
- $dateTimeMock = $this->getMockBuilder(\DateTime::class)
- ->disableOriginalConstructor()
- ->setMethods(['format', 'getTimestamp', 'setTimestamp'])
- ->getMock();
- $dateTimeMock->expects($this->any())
- ->method('format')
- ->with(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT)
- ->willReturn($dateTime);
- $dateTimeMock->expects($this->any())
- ->method('getTimestamp')
- ->willReturn($timestamp);
- $dateTimeMock->expects($this->any())
- ->method('setTimestamp')
- ->willReturnSelf();
- $dateTimeFactory = $this->getMockBuilder(DateTimeFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $dateTimeFactory->expects($this->any())->method('create')->willReturn($dateTimeMock);
- $this->objectManagerHelper = new ObjectManagerHelper($this);
- $this->accountManagement = $this->objectManagerHelper->getObject(
- \Magento\Customer\Model\AccountManagement::class,
- [
- 'customerFactory' => $this->customerFactory,
- 'customerRegistry' => $this->customerRegistry,
- 'customerRepository' => $this->customerRepository,
- 'customerModel' => $this->customer,
- 'dateTimeFactory' => $dateTimeFactory,
- 'stringHelper' => $this->string,
- 'scopeConfig' => $this->scopeConfig,
- 'sessionManager' => $this->sessionManager,
- 'visitorCollectionFactory' => $this->visitorCollectionFactory,
- 'saveHandler' => $this->saveHandler,
- 'encryptor' => $this->encryptor,
- 'dataProcessor' => $this->dataObjectProcessor,
- 'storeManager' => $this->storeManager,
- 'addressRegistry' => $this->addressRegistryMock,
- 'transportBuilder' => $this->transportBuilder,
- ]
- );
- $this->objectManagerHelper->setBackwardCompatibleProperty(
- $this->accountManagement,
- 'authentication',
- $this->authenticationMock
- );
- }
- /**
- * @return void
- */
- public function testChangePassword()
- {
- $customerId = 7;
- $email = 'test@example.com';
- $currentPassword = '1234567';
- $newPassword = 'abcdefg';
- $passwordHash = '1a2b3f4c';
- $this->reInitModel();
- $customer = $this->getMockBuilder(CustomerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customer->expects($this->any())
- ->method('getId')
- ->willReturn($customerId);
- $customer->expects($this->once())
- ->method('getAddresses')
- ->willReturn([]);
- $this->customerRepository
- ->expects($this->once())
- ->method('get')
- ->with($email)
- ->willReturn($customer);
- $this->authenticationMock->expects($this->once())
- ->method('authenticate');
- $this->customerSecure->expects($this->once())
- ->method('setRpToken')
- ->with(null);
- $this->customerSecure->expects($this->once())
- ->method('setRpTokenCreatedAt')
- ->willReturnSelf();
- $this->customerSecure->expects($this->any())
- ->method('getPasswordHash')
- ->willReturn($passwordHash);
- $this->customerRegistry->expects($this->any())
- ->method('retrieveSecureData')
- ->with($customerId)
- ->willReturn($this->customerSecure);
- $this->scopeConfig->expects($this->any())
- ->method('getValue')
- ->willReturnMap(
- [
- [
- AccountManagement::XML_PATH_MINIMUM_PASSWORD_LENGTH,
- 'default',
- null,
- 7,
- ],
- [
- AccountManagement::XML_PATH_REQUIRED_CHARACTER_CLASSES_NUMBER,
- 'default',
- null,
- 1,
- ],
- ]
- );
- $this->string->expects($this->any())
- ->method('strlen')
- ->with($newPassword)
- ->willReturn(7);
- $this->customerRepository
- ->expects($this->once())
- ->method('save')
- ->with($customer);
- $this->sessionManager->expects($this->atLeastOnce())->method('getSessionId');
- $visitor = $this->getMockBuilder(\Magento\Customer\Model\Visitor::class)
- ->disableOriginalConstructor()
- ->setMethods(['getSessionId'])
- ->getMock();
- $visitor->expects($this->atLeastOnce())->method('getSessionId')
- ->willReturnOnConsecutiveCalls('session_id_1', 'session_id_2');
- $visitorCollection = $this->getMockBuilder(
- \Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory::class
- )
- ->disableOriginalConstructor()->setMethods(['addFieldToFilter', 'getItems'])->getMock();
- $visitorCollection->expects($this->atLeastOnce())->method('addFieldToFilter')->willReturnSelf();
- $visitorCollection->expects($this->atLeastOnce())->method('getItems')->willReturn([$visitor, $visitor]);
- $this->visitorCollectionFactory->expects($this->atLeastOnce())->method('create')
- ->willReturn($visitorCollection);
- $this->saveHandler->expects($this->atLeastOnce())->method('destroy')
- ->withConsecutive(
- ['session_id_1'],
- ['session_id_2']
- );
- $this->assertTrue($this->accountManagement->changePassword($email, $currentPassword, $newPassword));
- }
- /**
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function testResetPassword()
- {
- $customerEmail = 'customer@example.com';
- $customerId = '1';
- $addressId = 5;
- $resetToken = 'newStringToken';
- $newPassword = 'new_password';
- $this->reInitModel();
- /** @var \Magento\Customer\Model\Address|\PHPUnit_Framework_MockObject_MockObject $addressModel */
- $addressModel = $this->getMockBuilder(\Magento\Customer\Model\Address::class)->disableOriginalConstructor()
- ->setMethods(['setShouldIgnoreValidation'])->getMock();
- /** @var \Magento\Customer\Api\Data\AddressInterface|\PHPUnit_Framework_MockObject_MockObject $customer */
- $address = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
- $address->expects($this->any())
- ->method('getId')
- ->willReturn($addressId);
- /** @var Customer|\PHPUnit_Framework_MockObject_MockObject $customer */
- $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
- $customer->expects($this->any())->method('getId')->willReturn($customerId);
- $customer->expects($this->any())
- ->method('getAddresses')
- ->willReturn([$address]);
- $this->addressRegistryMock->expects($this->once())
- ->method('retrieve')
- ->with($addressId)
- ->willReturn($addressModel);
- $addressModel->expects($this->once())
- ->method('setShouldIgnoreValidation')
- ->with(true);
- $this->customerRepository->expects($this->atLeastOnce())->method('get')->with($customerEmail)
- ->willReturn($customer);
- $this->customer->expects($this->atLeastOnce())->method('getResetPasswordLinkExpirationPeriod')
- ->willReturn(100000);
- $this->string->expects($this->any())->method('strlen')->willReturnCallback(
- function ($string) {
- return strlen($string);
- }
- );
- $this->customerRegistry->expects($this->atLeastOnce())->method('retrieveSecureData')
- ->willReturn($this->customerSecure);
- $this->customerSecure->expects($this->once())->method('setRpToken')->with(null);
- $this->customerSecure->expects($this->once())->method('setRpTokenCreatedAt')->with(null);
- $this->customerSecure->expects($this->any())->method('setPasswordHash')->willReturn(null);
- $this->sessionManager->expects($this->atLeastOnce())->method('destroy');
- $this->sessionManager->expects($this->atLeastOnce())->method('getSessionId');
- $visitor = $this->getMockBuilder(\Magento\Customer\Model\Visitor::class)
- ->disableOriginalConstructor()
- ->setMethods(['getSessionId'])
- ->getMock();
- $visitor->expects($this->atLeastOnce())->method('getSessionId')
- ->willReturnOnConsecutiveCalls('session_id_1', 'session_id_2');
- $visitorCollection = $this->getMockBuilder(
- \Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory::class
- )
- ->disableOriginalConstructor()->setMethods(['addFieldToFilter', 'getItems'])->getMock();
- $visitorCollection->expects($this->atLeastOnce())->method('addFieldToFilter')->willReturnSelf();
- $visitorCollection->expects($this->atLeastOnce())->method('getItems')->willReturn([$visitor, $visitor]);
- $this->visitorCollectionFactory->expects($this->atLeastOnce())->method('create')
- ->willReturn($visitorCollection);
- $this->saveHandler->expects($this->atLeastOnce())->method('destroy')
- ->withConsecutive(
- ['session_id_1'],
- ['session_id_2']
- );
- $this->assertTrue($this->accountManagement->resetPassword($customerEmail, $resetToken, $newPassword));
- }
- /**
- * @return void
- */
- public function testChangePasswordException()
- {
- $email = 'test@example.com';
- $currentPassword = '1234567';
- $newPassword = 'abcdefg';
- $exception = new NoSuchEntityException(
- new \Magento\Framework\Phrase('Exception message')
- );
- $this->customerRepository
- ->expects($this->once())
- ->method('get')
- ->with($email)
- ->willThrowException($exception);
- $this->expectException(\Magento\Framework\Exception\InvalidEmailOrPasswordException::class);
- $this->expectExceptionMessage('Invalid login or password.');
- $this->accountManagement->changePassword($email, $currentPassword, $newPassword);
- }
- /**
- * @return void
- */
- public function testAuthenticate()
- {
- $username = 'login';
- $password = '1234567';
- $passwordHash = '1a2b3f4c';
- $customerData = $this->getMockBuilder(Customer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customerModel = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customerModel->expects($this->once())
- ->method('updateData')
- ->willReturn($customerModel);
- $this->customerRepository
- ->expects($this->once())
- ->method('get')
- ->with($username)
- ->willReturn($customerData);
- $this->authenticationMock->expects($this->once())
- ->method('authenticate');
- $customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
- ->setMethods(['getPasswordHash'])
- ->disableOriginalConstructor()
- ->getMock();
- $customerSecure->expects($this->any())
- ->method('getPasswordHash')
- ->willReturn($passwordHash);
- $this->customerRegistry->expects($this->any())
- ->method('retrieveSecureData')
- ->willReturn($customerSecure);
- $this->customerFactory->expects($this->once())
- ->method('create')
- ->willReturn($customerModel);
- $this->manager->expects($this->exactly(2))
- ->method('dispatch')
- ->withConsecutive(
- [
- 'customer_customer_authenticated',
- ['model' => $customerModel, 'password' => $password],
- ],
- [
- 'customer_data_object_login', ['customer' => $customerData],
- ]
- );
- $this->assertEquals($customerData, $this->accountManagement->authenticate($username, $password));
- }
- /**
- * @param int $isConfirmationRequired
- * @param string|null $confirmation
- * @param string $expected
- * @dataProvider dataProviderGetConfirmationStatus
- */
- public function testGetConfirmationStatus(
- $isConfirmationRequired,
- $confirmation,
- $expected
- ) {
- $websiteId = 1;
- $customerId = 1;
- $customerEmail = 'test1@example.com';
- $customerMock = $this->getMockBuilder(Customer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customerMock->expects($this->once())
- ->method('getId')
- ->willReturn($customerId);
- $customerMock->expects($this->any())
- ->method('getConfirmation')
- ->willReturn($confirmation);
- $customerMock->expects($this->once())
- ->method('getEmail')
- ->willReturn($customerEmail);
- $customerMock->expects($this->once())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- $this->accountConfirmation->expects($this->once())
- ->method('isConfirmationRequired')
- ->with($websiteId, $customerId, $customerEmail)
- ->willReturn($isConfirmationRequired);
- $this->customerRepository->expects($this->once())
- ->method('getById')
- ->with($customerId)
- ->willReturn($customerMock);
- $this->assertEquals($expected, $this->accountManagement->getConfirmationStatus($customerId));
- }
- /**
- * @return array
- */
- public function dataProviderGetConfirmationStatus()
- {
- return [
- [0, null, AccountManagement::ACCOUNT_CONFIRMATION_NOT_REQUIRED],
- [0, null, AccountManagement::ACCOUNT_CONFIRMATION_NOT_REQUIRED],
- [0, null, AccountManagement::ACCOUNT_CONFIRMATION_NOT_REQUIRED],
- [1, null, AccountManagement::ACCOUNT_CONFIRMED],
- [1, 'test', AccountManagement::ACCOUNT_CONFIRMATION_REQUIRED],
- ];
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- */
- public function testCreateAccountWithPasswordHashForGuest()
- {
- $storeId = 1;
- $storeName = 'store_name';
- $websiteId = 1;
- $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
- $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->getMock();
- $storeMock->expects($this->once())
- ->method('getId')
- ->willReturn($storeId);
- $storeMock->expects($this->once())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- $storeMock->expects($this->once())
- ->method('getName')
- ->willReturn($storeName);
- $this->storeManager->expects($this->exactly(3))
- ->method('getStore')
- ->willReturn($storeMock);
- $customerMock = $this->getMockBuilder(Customer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customerMock->expects($this->exactly(2))
- ->method('getId')
- ->willReturn(null);
- $customerMock->expects($this->exactly(3))
- ->method('getStoreId')
- ->willReturn(null);
- $customerMock->expects($this->exactly(3))
- ->method('getWebsiteId')
- ->willReturn(null);
- $customerMock->expects($this->once())
- ->method('setStoreId')
- ->with($storeId)
- ->willReturnSelf();
- $customerMock->expects($this->once())
- ->method('setWebsiteId')
- ->with($websiteId)
- ->willReturnSelf();
- $customerMock->expects($this->once())
- ->method('setCreatedIn')
- ->with($storeName)
- ->willReturnSelf();
- $customerMock->expects($this->once())
- ->method('getAddresses')
- ->willReturn(null);
- $customerMock->expects($this->once())
- ->method('setAddresses')
- ->with(null)
- ->willReturnSelf();
- $this->customerRepository
- ->expects($this->once())
- ->method('save')
- ->with($customerMock, $hash)
- ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('Exception message')));
- $this->accountManagement->createAccountWithPasswordHash($customerMock, $hash);
- }
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testCreateAccountWithPasswordHashWithCustomerAddresses()
- {
- $websiteId = 1;
- $addressId = 2;
- $customerId = null;
- $storeId = 1;
- $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
- $this->prepareDateTimeFactory();
- //Handle store
- $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
- $store->expects($this->any())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- //Handle address - existing and non-existing. Non-Existing should return null when call getId method
- $existingAddress = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $nonExistingAddress = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- //Ensure that existing address is not in use
- $this->addressRepository
- ->expects($this->atLeastOnce())
- ->method("save")
- ->withConsecutive(
- [$this->logicalNot($this->identicalTo($existingAddress))],
- [$this->identicalTo($nonExistingAddress)]
- );
- $existingAddress
- ->expects($this->any())
- ->method("getId")
- ->willReturn($addressId);
- //Expects that id for existing address should be unset
- $existingAddress
- ->expects($this->once())
- ->method("setId")
- ->with(null);
- //Handle Customer calls
- $customer = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
- $customer
- ->expects($this->atLeastOnce())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- $customer
- ->expects($this->atLeastOnce())
- ->method('getStoreId')
- ->willReturn($storeId);
- $customer
- ->expects($this->any())
- ->method("getId")
- ->willReturn($customerId);
- //Return Customer from customer repository
- $this->customerRepository
- ->expects($this->atLeastOnce())
- ->method('save')
- ->willReturn($customer);
- $this->customerRepository
- ->expects($this->once())
- ->method('getById')
- ->with($customerId)
- ->willReturn($customer);
- $customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
- ->setMethods(['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash'])
- ->disableOriginalConstructor()
- ->getMock();
- $customerSecure->expects($this->once())
- ->method('setRpToken')
- ->with($hash);
- $customerSecure->expects($this->any())
- ->method('getPasswordHash')
- ->willReturn($hash);
- $this->customerRegistry->expects($this->any())
- ->method('retrieveSecureData')
- ->with($customerId)
- ->willReturn($customerSecure);
- $this->random->expects($this->once())
- ->method('getUniqueHash')
- ->willReturn($hash);
- $customer
- ->expects($this->atLeastOnce())
- ->method('getAddresses')
- ->willReturn([$existingAddress, $nonExistingAddress]);
- $this->storeManager
- ->expects($this->atLeastOnce())
- ->method('getStore')
- ->willReturn($store);
- $this->share
- ->expects($this->once())
- ->method('isWebsiteScope')
- ->willReturn(true);
- $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
- $website->expects($this->once())
- ->method('getStoreIds')
- ->willReturn([1, 2, 3]);
- $this->storeManager
- ->expects($this->atLeastOnce())
- ->method('getWebsite')
- ->with($websiteId)
- ->willReturn($website);
- $this->assertSame($customer, $this->accountManagement->createAccountWithPasswordHash($customer, $hash));
- }
- /**
- * @return string
- */
- private function prepareDateTimeFactory()
- {
- $dateTime = '2017-10-25 18:57:08';
- $timestamp = '1508983028';
- $dateTimeMock = $this->createMock(\DateTime::class);
- $dateTimeMock->expects($this->any())
- ->method('format')
- ->with(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT)
- ->willReturn($dateTime);
- $dateTimeMock
- ->expects($this->any())
- ->method('getTimestamp')
- ->willReturn($timestamp);
- $this->dateTimeFactory
- ->expects($this->any())
- ->method('create')
- ->willReturn($dateTimeMock);
- return $dateTime;
- }
- /**
- * @return void
- */
- public function testCreateAccountUnexpectedValueException(): void
- {
- $websiteId = 1;
- $storeId = null;
- $defaultStoreId = 1;
- $customerId = 1;
- $customerEmail = 'email@email.com';
- $newLinkToken = '2jh43j5h2345jh23lh452h345hfuzasd96ofu';
- $exception = new \UnexpectedValueException('Template file was not found');
- $datetime = $this->prepareDateTimeFactory();
- $address = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
- $address->expects($this->once())
- ->method('setCustomerId')
- ->with($customerId);
- $store = $this->createMock(\Magento\Store\Model\Store::class);
- $store->expects($this->once())
- ->method('getId')
- ->willReturn($defaultStoreId);
- $website = $this->createMock(\Magento\Store\Model\Website::class);
- $website->expects($this->atLeastOnce())
- ->method('getStoreIds')
- ->willReturn([1, 2, 3]);
- $website->expects($this->once())
- ->method('getDefaultStore')
- ->willReturn($store);
- $customer = $this->createMock(Customer::class);
- $customer->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($customerId);
- $customer->expects($this->atLeastOnce())
- ->method('getEmail')
- ->willReturn($customerEmail);
- $customer->expects($this->atLeastOnce())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- $customer->expects($this->atLeastOnce())
- ->method('getStoreId')
- ->willReturn($storeId);
- $customer->expects($this->once())
- ->method('setStoreId')
- ->with($defaultStoreId);
- $customer->expects($this->once())
- ->method('getAddresses')
- ->willReturn([$address]);
- $customer->expects($this->once())
- ->method('setAddresses')
- ->with(null);
- $this->customerRepository->expects($this->once())
- ->method('get')
- ->with($customerEmail)
- ->willReturn($customer);
- $this->share->expects($this->once())
- ->method('isWebsiteScope')
- ->willReturn(true);
- $this->storeManager->expects($this->atLeastOnce())
- ->method('getWebsite')
- ->with($websiteId)
- ->willReturn($website);
- $this->customerRepository->expects($this->atLeastOnce())
- ->method('save')
- ->willReturn($customer);
- $this->addressRepository->expects($this->atLeastOnce())
- ->method('save')
- ->with($address);
- $this->customerRepository->expects($this->once())
- ->method('getById')
- ->with($customerId)
- ->willReturn($customer);
- $this->random->expects($this->once())
- ->method('getUniqueHash')
- ->willReturn($newLinkToken);
- $customerSecure = $this->createPartialMock(
- \Magento\Customer\Model\Data\CustomerSecure::class,
- ['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash']
- );
- $customerSecure->expects($this->any())
- ->method('setRpToken')
- ->with($newLinkToken);
- $customerSecure->expects($this->any())
- ->method('setRpTokenCreatedAt')
- ->with($datetime)
- ->willReturnSelf();
- $customerSecure->expects($this->any())
- ->method('getPasswordHash')
- ->willReturn(null);
- $this->customerRegistry->expects($this->atLeastOnce())
- ->method('retrieveSecureData')
- ->willReturn($customerSecure);
- $this->emailNotificationMock->expects($this->once())
- ->method('newAccount')
- ->willThrowException($exception);
- $this->logger->expects($this->once())->method('error')->with($exception);
- $this->accountManagement->createAccount($customer);
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- */
- public function testCreateAccountWithStoreNotInWebsite()
- {
- $storeId = 1;
- $websiteId = 1;
- $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
- $customerMock = $this->getMockBuilder(Customer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customerMock->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn(null);
- $customerMock->expects($this->atLeastOnce())
- ->method('getStoreId')
- ->willReturn($storeId);
- $customerMock->expects($this->atLeastOnce())
- ->method('getWebsiteId')
- ->willReturn($websiteId);
- $this->share
- ->expects($this->once())
- ->method('isWebsiteScope')
- ->willReturn(true);
- $website = $this->getMockBuilder(\Magento\Store\Model\Website::class)->disableOriginalConstructor()->getMock();
- $website->expects($this->once())
- ->method('getStoreIds')
- ->willReturn([2, 3]);
- $this->storeManager
- ->expects($this->atLeastOnce())
- ->method('getWebsite')
- ->with($websiteId)
- ->willReturn($website);
- $this->accountManagement->createAccountWithPasswordHash($customerMock, $hash);
- }
- }
|