/**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testNewAccount()
 {
     $customerId = 1;
     $customerStoreId = 2;
     $customerEmail = '*****@*****.**';
     $customerData = ['key' => 'value'];
     $customerName = 'Customer Name';
     $templateIdentifier = 'Template Identifier';
     $sender = 'Sender';
     $customer = $this->getMock(\Magento\Customer\Api\Data\CustomerInterface::class, [], [], '', false);
     $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->storeMock->expects($this->any())->method('getId')->willReturn($customerStoreId);
     $this->storeManagerMock->expects($this->once())->method('getStore')->with($customerStoreId)->willReturn($this->storeMock);
     $this->customerRegistryMock->expects($this->once())->method('retrieveSecureData')->with($customerId)->willReturn($this->customerSecureMock);
     $this->dataProcessorMock->expects($this->once())->method('buildOutputDataArray')->with($customer, \Magento\Customer\Api\Data\CustomerInterface::class)->willReturn($customerData);
     $this->customerViewHelperMock->expects($this->any())->method('getCustomerName')->with($customer)->willReturn($customerName);
     $this->customerSecureMock->expects($this->once())->method('addData')->with($customerData)->willReturnSelf();
     $this->customerSecureMock->expects($this->once())->method('setData')->with('name', $customerName)->willReturnSelf();
     $this->scopeConfigMock->expects($this->at(0))->method('getValue')->with(EmailNotification::XML_PATH_REGISTER_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $customerStoreId)->willReturn($templateIdentifier);
     $this->scopeConfigMock->expects($this->at(1))->method('getValue')->with(EmailNotification::XML_PATH_REGISTER_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, $customerStoreId)->willReturn($sender);
     $transport = $this->getMock(\Magento\Framework\Mail\TransportInterface::class, [], [], '', false);
     $this->transportBuilderMock->expects($this->once())->method('setTemplateIdentifier')->with($templateIdentifier)->willReturnSelf();
     $this->transportBuilderMock->expects($this->once())->method('setTemplateOptions')->with(['area' => Area::AREA_FRONTEND, 'store' => $customerStoreId])->willReturnSelf();
     $this->transportBuilderMock->expects($this->once())->method('setTemplateVars')->with(['customer' => $this->customerSecureMock, 'back_url' => '', 'store' => $this->storeMock])->willReturnSelf();
     $this->transportBuilderMock->expects($this->once())->method('setFrom')->with($sender)->willReturnSelf();
     $this->transportBuilderMock->expects($this->once())->method('addTo')->with($customerEmail, $customerName)->willReturnSelf();
     $this->transportBuilderMock->expects($this->once())->method('getTransport')->willReturn($transport);
     $transport->expects($this->once())->method('sendMessage');
     $this->model->newAccount($customer, EmailNotification::NEW_ACCOUNT_EMAIL_REGISTERED, '', $customerStoreId);
 }
Example #2
0
 /**
  * @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;
     $dateTime = date(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
     $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);
     $customer = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->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);
     $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, '\\Magento\\Customer\\Api\\Data\\CustomerInterface')->willReturn($customerData);
     $this->prepareEmailSend($email, $templateIdentifier, $sender, $storeId, $customerName);
 }
 /**
  * reInit $this->accountManagement object
  */
 private function reInitModel()
 {
     $this->customerSecure = $this->getMockBuilder('Magento\\Customer\\Model\\Data\\CustomerSecure')->disableOriginalConstructor()->setMethods(['getRpToken', 'getRpTokenCreatedAt'])->getMock();
     $this->customerSecure->expects($this->any())->method('getRpToken')->willReturn('newStringToken');
     $date = date('Y-m-d', strtotime('-1 year'));
     $this->customerSecure->expects($this->any())->method('getRpTokenCreatedAt')->willReturn($date);
     $this->customer = $this->getMockBuilder('Magento\\Customer\\Model\\Customer')->disableOriginalConstructor()->setMethods(['getResetPasswordLinkExpirationPeriod'])->getMock();
     $this->objectManagerHelper = new ObjectManagerHelper($this);
     $this->accountManagement = $this->objectManagerHelper->getObject('Magento\\Customer\\Model\\AccountManagement', ['customerFactory' => $this->customerFactory, 'customerRegistry' => $this->customerRegistry, 'customerRepository' => $this->customerRepository, 'customerModel' => $this->customer]);
 }
 /**
  * @return void
  */
 public function testCheckIfLocked()
 {
     $customerId = 7;
     $email = '*****@*****.**';
     $customerMock = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false);
     $customerMock->expects($this->once())->method('getId')->willReturn($customerId);
     $this->customerSecure->expects($this->once())->method('isCustomerLocked')->willReturn(true);
     $this->scopeConfigMock->expects($this->once())->method('getValue')->with('contact/email/recipient_email')->willReturn($email);
     $this->customerRegistryMock->expects($this->once())->method('retrieve')->with($customerId)->willReturn($this->customerSecure);
     $this->setExpectedException('\\Magento\\Framework\\Exception\\State\\UserLockedException', __('The account is locked. Please wait and try again or contact %1.', $email));
     $this->helper->checkIfLocked($customerMock);
 }
 public function testInitiatePasswordResetEmailReminder()
 {
     $customerId = 1;
     $email = '*****@*****.**';
     $template = AccountManagement::EMAIL_REMINDER;
     $templateIdentifier = 'Template Identifier';
     $sender = 'Sender';
     $storeId = 1;
     $isFrontendSecure = true;
     $resetUrl = 'reset url';
     mt_srand(mt_rand() + 100000000 * microtime() % PHP_INT_MAX);
     $hash = md5(uniqid(microtime() . mt_rand(0, mt_getrandmax()), true));
     $this->store->expects($this->once())->method('isFrontUrlSecure')->willReturn($isFrontendSecure);
     $this->url->expects($this->once())->method('setScope')->with($storeId)->willReturnSelf();
     $this->url->expects($this->once())->method('getUrl')->with('customer/account/createPassword', ['_query' => ['id' => $customerId, 'token' => $hash], '_store' => $storeId, '_secure' => $isFrontendSecure])->willReturn($resetUrl);
     $this->scopeConfig->expects($this->at(0))->method('getValue')->with(AccountManagement::XML_PATH_REMIND_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $storeId)->willReturn($templateIdentifier);
     $this->scopeConfig->expects($this->at(1))->method('getValue')->with(AccountManagement::XML_PATH_FORGOT_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, $storeId)->willReturn($sender);
     $this->customerSecure->expects($this->any())->method('setResetPasswordUrl')->with($resetUrl)->willReturnSelf();
     $this->prepareInitiatePasswordReset($email, $templateIdentifier, $sender, $storeId, $customerId, $hash);
     $this->assertTrue($this->accountManagement->initiatePasswordReset($email, $template));
 }
 /**
  * @param bool $result
  * @dataProvider validateCustomerPassword
  */
 public function testValidateCustomerPassword($result)
 {
     $customerId = 7;
     $password = '******';
     $hash = '1b2af329dd0';
     $customerMock = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false);
     $this->customerRepositoryMock->expects($this->any())->method('getById')->willReturn($customerMock);
     $this->customerSecure->expects($this->any())->method('getId')->willReturn($customerId);
     $this->customerSecure->expects($this->once())->method('getPasswordHash')->willReturn($hash);
     $this->customerRegistryMock->expects($this->any())->method('retrieveSecureData')->with($customerId)->willReturn($this->customerSecure);
     $this->encryptorMock->expects($this->once())->method('validateHash')->with($password, $hash)->willReturn($result);
     if ($result) {
         $this->assertTrue($this->authentication->authenticate($customerId, $password));
     } else {
         $this->backendConfigMock->expects($this->exactly(2))->method('getValue')->withConsecutive([\Magento\Customer\Model\Authentication::LOCKOUT_THRESHOLD_PATH], [\Magento\Customer\Model\Authentication::MAX_FAILURES_PATH])->willReturnOnConsecutiveCalls(1, 1);
         $this->customerSecure->expects($this->once())->method('isCustomerLocked')->willReturn(false);
         $this->customerRegistryMock->expects($this->once())->method('retrieve')->with($customerId)->willReturn($this->customerSecure);
         $this->customerRepositoryMock->expects($this->once())->method('save')->willReturn($customerMock);
         $this->setExpectedException('\\Magento\\Framework\\Exception\\InvalidEmailOrPasswordException');
         $this->authentication->authenticate($customerId, $password);
     }
 }