/** * @param array $result * @param bool $expectedValue * @dataProvider getButtonDataProvider */ public function testGetButtonData($result, $expectedValue) { $this->registryMock->expects($this->any())->method('registry')->willReturn(1); $this->customerRegistryMock->expects($this->once())->method('retrieve')->willReturn($this->customerModelMock); $this->customerModelMock->expects($this->once())->method('isCustomerLocked')->willReturn($expectedValue); $this->urlBuilderMock->expects($this->any())->method('getUrl')->willReturn('http://website.com/'); $this->assertEquals($result, $this->block->getButtonData()); }
/** * Upgrade customer password hash when customer has logged in * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { $password = $observer->getEvent()->getData('password'); /** @var \Magento\Customer\Model\Customer $model */ $model = $observer->getEvent()->getData('model'); $customer = $this->customerRepository->getById($model->getId()); $customerSecure = $this->customerRegistry->retrieveSecureData($model->getId()); if (!$this->encryptor->validateHashVersion($customerSecure->getPasswordHash(), true)) { $customerSecure->setPasswordHash($this->encryptor->getHash($password, true)); $this->customerRepository->save($customer); } }
/** * Returns Unlock button data * * @return array */ public function getButtonData() { $customerId = $this->getCustomerId(); $data = []; if ($customerId) { $customer = $this->customerRegistry->retrieve($customerId); if ($customer->isCustomerLocked()) { $data = ['label' => __('Unlock'), 'class' => 'unlock unlock-customer', 'on_click' => sprintf("location.href = '%s';", $this->getUnlockUrl()), 'sort_order' => 50]; } } return $data; }
/** * test SaveAuth */ public function testSaveAuth() { $customerId = 1; $customerSecureMock = $this->getMock(\Magento\Customer\Model\Data\CustomerSecure::class, [], [], '', false); $dbAdapter = $this->getMock(\Magento\Framework\DB\Adapter\AdapterInterface::class, [], [], '', false); $this->customerRegistry->expects($this->once())->method('retrieveSecureData')->willReturn($customerSecureMock); $customerSecureMock->expects($this->exactly(3))->method('getData')->willReturn(1); $this->customerResourceModel->expects($this->any())->method('getConnection')->willReturn($dbAdapter); $this->customerResourceModel->expects($this->any())->method('getTable')->willReturn('customer_entity'); $dbAdapter->expects($this->any())->method('update')->with('customer_entity', ['failures_num' => 1, 'first_failure' => 1, 'lock_expires' => 1]); $dbAdapter->expects($this->any())->method('quoteInto')->with('entity_id = ?', $customerId); $this->model->saveAuth($customerId); }
/** * @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); }
/** * {@inheritdoc} */ public function deleteById($customerId) { $customerModel = $this->customerRegistry->retrieve($customerId); $customerModel->delete(); $this->customerRegistry->remove($customerId); return true; }
/** * {@inheritdoc} */ public function saveAddresses($customerId, $addresses) { $customerModel = $this->customerRegistry->retrieve($customerId); $addressModels = []; $inputException = new InputException(); for ($i = 0; $i < count($addresses); $i++) { $address = $addresses[$i]; $addressModel = null; if ($address->getId()) { $addressModel = $customerModel->getAddressItemById($address->getId()); } if (is_null($addressModel)) { $addressModel = $this->addressConverter->createAddressModel($address); $addressModel->setCustomer($customerModel); } else { $this->addressConverter->updateAddressModel($addressModel, $address); } $inputException = $this->_validate($addressModel, $inputException, $i); $addressModels[] = $addressModel; } $this->customerRegistry->remove($customerId); if ($inputException->wasErrorAdded()) { throw $inputException; } $addressIds = array(); /** @var \Magento\Customer\Model\Address $addressModel */ foreach ($addressModels as $addressModel) { $addressModel->save(); $this->addressRegistry->remove($addressModel->getId()); $addressIds[] = $addressModel->getId(); } return $addressIds; }
/** * 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')); }
/** * {@inheritdoc} */ public function authenticate($username, $password) { try { switch ($this->advancedLoginConfigProvider->getLoginMode()) { case LoginMode::LOGIN_TYPE_ONLY_ATTRIBUTE: $customer = $this->loginViaCustomerAttributeOnly($username); break; case LoginMode::LOGIN_TYPE_BOTH: $customer = $this->loginViaCustomerAttributeOrEmail($username); break; default: $customer = $this->loginViaEmailOnly($username); break; } } catch (NoSuchEntityException $e) { throw new InvalidEmailOrPasswordException(__('Invalid login or password.')); } $this->checkPasswordStrength($password); $hash = $this->customerRegistry->retrieveSecureData($customer->getId())->getPasswordHash(); if (!$this->encryptor->validateHash($password, $hash)) { throw new InvalidEmailOrPasswordException(__('Invalid login or password.')); } if ($customer->getConfirmation() && $this->isConfirmationRequired($customer)) { throw new EmailNotConfirmedException(__('This account is not confirmed.')); } $customerModel = $this->customerFactory->create()->updateData($customer); $this->eventManager->dispatch('customer_customer_authenticated', ['model' => $customerModel, 'password' => $password]); $this->eventManager->dispatch('customer_data_object_login', ['customer' => $customer]); return $customer; }
public function testCreateAccountWithPasswordHashWithCustomerAddresses() { $websiteId = 1; $addressId = 2; $customerId = null; $storeId = 1; $hash = '4nj54lkj5jfi03j49f8bgujfgsd'; //Handle store $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->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')->disableOriginalConstructor()->getMock(); $nonExistingAddress = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock(); //Ensure that existing address is not in use $this->addressRepository->expects($this->atLeastOnce())->method("save")->withConsecutive(array($this->logicalNot($this->identicalTo($existingAddress))), array($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('Magento\\Customer\\Api\\Data\\CustomerInterface')->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')->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->assertSame($customer, $this->accountManagement->createAccountWithPasswordHash($customer, $hash)); }
/** * @param string $expectedResult * @param bool $value * @dataProvider getAccountLockDataProvider * @return void */ public function testGetAccountLock($expectedResult, $value) { $this->customerRegistry->expects($this->once())->method('retrieve')->willReturn($this->customerModel); $this->customerModel->expects($this->once())->method('isCustomerLocked')->willReturn($value); $expectedResult = new \Magento\Framework\Phrase($expectedResult); $this->assertEquals($expectedResult, $this->block->getAccountLock()); }
/** * @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); }
/** * Check if customer is locked and throw exception. * * @api * @param \Magento\Customer\Api\Data\CustomerInterface $customer * @throws \Magento\Framework\Exception\State\UserLockedException * @return void */ public function checkIfLocked(\Magento\Customer\Api\Data\CustomerInterface $customer) { $currentCustomer = $this->customerRegistry->retrieve($customer->getId()); if ($currentCustomer->isCustomerLocked()) { throw new UserLockedException(__('The account is locked. Please wait and try again or contact %1.', $this->scopeConfig->getValue('contact/email/recipient_email'))); } }
/** * @magentoDataFixture Magento/Customer/_files/customer.php * @expectedException \Magento\Framework\Exception\NoSuchEntityException * @magentoAppArea adminhtml */ public function testRemoveByEmail() { $customer = $this->_model->retrieve(self::CUSTOMER_ID); $this->assertInstanceOf('\\Magento\\Customer\\Model\\Customer', $customer); $customer->delete(); $this->_model->removeByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID); $this->_model->retrieveByEmail(self::CUSTOMER_EMAIL, $customer->getWebsiteId()); }
/** * Delete customer address by ID. * * @param int $addressId * @return bool true on success * @throws \Magento\Framework\Exception\NoSuchEntityException * @throws \Magento\Framework\Exception\LocalizedException */ public function deleteById($addressId) { $address = $this->addressRegistry->retrieve($addressId); $customerModel = $this->customerRegistry->retrieve($address->getCustomerId()); $customerModel->getAddressesCollection()->clear(); $this->addressResource->delete($address); $this->addressRegistry->remove($addressId); return true; }
/** * Create an object with data merged from Customer and CustomerSecure * * @param CustomerInterface $customer * @return \Magento\Customer\Model\Data\CustomerSecure */ private function getFullCustomerObject($customer) { // No need to flatten the custom attributes or nested objects since the only usage is for email templates and // object passed for events $mergedCustomerData = $this->customerRegistry->retrieveSecureData($customer->getId()); $customerData = $this->dataProcessor->buildOutputDataArray($customer, \Magento\Customer\Api\Data\CustomerInterface::class); $mergedCustomerData->addData($customerData); $mergedCustomerData->setData('name', $this->customerViewHelper->getCustomerName($customer)); return $mergedCustomerData; }
public function testDelete() { $customerId = 14; $customerModel = $this->getMock('Magento\\Customer\\Model\\Customer', ['delete'], [], '', false); $this->customer->expects($this->once())->method('getId')->willReturn($customerId); $this->customerRegistry->expects($this->once())->method('retrieve')->with($customerId)->willReturn($customerModel); $customerModel->expects($this->once())->method('delete'); $this->customerRegistry->expects($this->once())->method('remove')->with($customerId); $this->assertTrue($this->model->delete($this->customer)); }
/** * @param int $testNumber * @param string $oldEmail * @param string $newEmail * @param bool $isPasswordChanged * * @dataProvider sendNotificationEmailsDataProvider * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testSendNotificationEmailsIfRequired($testNumber, $oldEmail, $newEmail, $isPasswordChanged) { $customerId = 1; $customerStoreId = 2; $customerWebsiteId = 1; $customerData = ['key' => 'value']; $customerName = 'Customer Name'; $templateIdentifier = 'Template Identifier'; $sender = 'Sender'; switch ($testNumber) { case 1: $xmlPathTemplate = EmailNotification::XML_PATH_RESET_PASSWORD_TEMPLATE; $expects = $this->once(); break; case 2: $xmlPathTemplate = EmailNotification::XML_PATH_CHANGE_EMAIL_TEMPLATE; $expects = $this->exactly(2); break; case 3: $xmlPathTemplate = EmailNotification::XML_PATH_CHANGE_EMAIL_AND_PASSWORD_TEMPLATE; $expects = $this->exactly(2); break; } $origCustomer = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $origCustomer->expects($this->any())->method('getStoreId')->willReturn(0); $origCustomer->expects($this->any())->method('getId')->willReturn($customerId); $origCustomer->expects($this->any())->method('getWebsiteId')->willReturn($customerWebsiteId); $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $storeMock->expects($this->any())->method('getId')->willReturn($customerStoreId); $this->storeManagerMock->expects(clone $expects)->method('getStore')->willReturn($storeMock); $websiteMock = $this->getMockBuilder('Magento\\Store\\Model\\Website')->disableOriginalConstructor()->setMethods(['getStoreIds'])->getMock(); $websiteMock->expects($this->any())->method('getStoreIds')->willReturn([$customerStoreId]); $this->storeManagerMock->expects(clone $expects)->method('getWebsite')->with($customerWebsiteId)->willReturn($websiteMock); $customerSecureMock = $this->getMockBuilder('Magento\\Customer\\Model\\Data\\CustomerSecure')->disableOriginalConstructor()->getMock(); $this->customerRegistryMock->expects(clone $expects)->method('retrieveSecureData')->with($customerId)->willReturn($customerSecureMock); $this->dataProcessorMock->expects(clone $expects)->method('buildOutputDataArray')->with($origCustomer, '\\Magento\\Customer\\Api\\Data\\CustomerInterface')->willReturn($customerData); $this->customerViewHelperMock->expects($this->any())->method('getCustomerName')->with($origCustomer)->willReturn($customerName); $customerSecureMock->expects(clone $expects)->method('addData')->with($customerData)->willReturnSelf(); $customerSecureMock->expects(clone $expects)->method('setData')->with('name', $customerName)->willReturnSelf(); $savedCustomer = clone $origCustomer; $origCustomer->expects($this->any())->method('getEmail')->willReturn($oldEmail); $savedCustomer->expects($this->any())->method('getEmail')->willReturn($newEmail); $this->scopeConfigMock->expects($this->any())->method('getValue')->withConsecutive([$xmlPathTemplate, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $customerStoreId], [\Magento\Customer\Helper\EmailNotification::XML_PATH_FORGOT_EMAIL_IDENTITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $customerStoreId], [$xmlPathTemplate, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $customerStoreId], [\Magento\Customer\Helper\EmailNotification::XML_PATH_FORGOT_EMAIL_IDENTITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $customerStoreId])->willReturnOnConsecutiveCalls($templateIdentifier, $sender, $templateIdentifier, $sender); $this->transportBuilderMock->expects(clone $expects)->method('setTemplateIdentifier')->with($templateIdentifier)->willReturnSelf(); $this->transportBuilderMock->expects(clone $expects)->method('setTemplateOptions')->with(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $customerStoreId])->willReturnSelf(); $this->transportBuilderMock->expects(clone $expects)->method('setTemplateVars')->with(['customer' => $customerSecureMock, 'store' => $storeMock])->willReturnSelf(); $this->transportBuilderMock->expects(clone $expects)->method('setFrom')->with($sender)->willReturnSelf(); $this->transportBuilderMock->expects(clone $expects)->method('addTo')->withConsecutive([$oldEmail, $customerName], [$newEmail, $customerName])->willReturnSelf(); $transport = $this->getMockBuilder('Magento\\Framework\\Mail\\TransportInterface')->getMock(); $this->transportBuilderMock->expects(clone $expects)->method('getTransport')->willReturn($transport); $transport->expects(clone $expects)->method('sendMessage'); $this->assertEquals($this->helper, $this->helper->sendNotificationEmailsIfRequired($origCustomer, $savedCustomer, $isPasswordChanged)); }
/** * @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 testUpgradeCustomerPassword() { $customerId = '1'; $password = '******'; $passwordHash = 'hash:salt:999'; $model = $this->getMockBuilder('Magento\\Customer\\Model\\Customer')->disableOriginalConstructor()->setMethods(['getId'])->getMock(); $customer = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMockForAbstractClass(); $customerSecure = $this->getMockBuilder('Magento\\Customer\\Model\\Data\\CustomerSecure')->disableOriginalConstructor()->setMethods(['getPasswordHash', 'setPasswordHash'])->getMock(); $model->expects($this->exactly(2))->method('getId')->willReturn($customerId); $this->customerRepository->expects($this->once())->method('getById')->with($customerId)->willReturn($customer); $this->customerRegistry->expects($this->once())->method('retrieveSecureData')->with($customerId)->willReturn($customerSecure); $customerSecure->expects($this->once())->method('getPasswordHash')->willReturn($passwordHash); $this->encryptorMock->expects($this->once())->method('validateHashVersion')->with($passwordHash)->willReturn(false); $this->encryptorMock->expects($this->once())->method('getHash')->with($password, true)->willReturn($passwordHash); $customerSecure->expects($this->once())->method('setPasswordHash')->with($passwordHash); $this->customerRepository->expects($this->once())->method('save')->with($customer); $event = new \Magento\Framework\DataObject(); $event->setData(['password' => 'password', 'model' => $model]); $observerMock = new \Magento\Framework\Event\Observer(); $observerMock->setEvent($event); $this->model->execute($observerMock); }
/** * {@inheritdoc} */ public function authenticate($customerId, $password) { $customerSecure = $this->customerRegistry->retrieveSecureData($customerId); $hash = $customerSecure->getPasswordHash(); if (!$this->encryptor->validateHash($password, $hash)) { $this->processAuthenticationFailure($customerId); if ($this->isLocked($customerId)) { throw new UserLockedException(__('The account is locked.')); } throw new InvalidEmailOrPasswordException(__('Invalid login or password.')); } return true; }
public function testDeleteById() { $addressId = 12; $customerId = 43; $this->address->expects($this->once())->method('getCustomerId')->willReturn($customerId); $addressCollection = $this->getMock('Magento\\Customer\\Model\\ResourceModel\\Address\\Collection', [], [], '', false); $this->addressRegistry->expects($this->once())->method('retrieve')->with($addressId)->willReturn($this->address); $this->customerRegistry->expects($this->once())->method('retrieve')->with($customerId)->willReturn($this->customer); $this->customer->expects($this->once())->method('getAddressesCollection')->willReturn($addressCollection); $addressCollection->expects($this->once())->method('clear'); $this->addressResourceModel->expects($this->once())->method('delete')->with($this->address); $this->addressRegistry->expects($this->once())->method('remove')->with($addressId); $this->assertTrue($this->repository->deleteById($addressId)); }
/** * {@inheritdoc} */ public function assignCustomer($cartId, $customerId) { $storeId = $this->storeManager->getStore()->getId(); $quote = $this->quoteFactory->create()->load($cartId); if ($quote->getId() != $cartId || $quote->getStoreId() != $storeId) { throw new NoSuchEntityException('There is no cart with provided ID.'); } $customer = $this->customerRegistry->retrieve($customerId); if (!in_array($storeId, $customer->getSharedStoreIds())) { throw new StateException('Cannot assign customer to the given cart. The cart belongs to different store.'); } if ($quote->getCustomerId()) { throw new StateException('Cannot assign customer to the given cart. The cart is not anonymous.'); } $currentCustomerQuote = $this->quoteFactory->create()->loadByCustomer($customer); if ($currentCustomerQuote->getId()) { throw new StateException('Cannot assign customer to the given cart. Customer already has active cart.'); } $quote->setCustomer($customer); $quote->setCustomerIsGuest(0); $quote->save(); return true; }
public function testRemoveByEmail() { $this->customer->expects($this->exactly(2))->method('loadByEmail')->with(self::CUSTOMER_EMAIL)->will($this->returnValue($this->customer)); $this->customer->expects($this->any())->method('getId')->will($this->returnValue(self::CUSTOMER_ID)); $this->customer->expects($this->any())->method('getEmail')->will($this->returnValue(self::CUSTOMER_EMAIL)); $this->customer->expects($this->any())->method('getWebsiteId')->will($this->returnValue(self::WEBSITE_ID)); $this->customer->expects($this->any())->method('setEmail')->will($this->returnValue($this->customer)); $this->customer->expects($this->any())->method('setWebsiteId')->will($this->returnValue($this->customer)); $this->customerFactory->expects($this->exactly(2))->method('create')->will($this->returnValue($this->customer)); $actual = $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID); $this->assertEquals($this->customer, $actual); $this->customerRegistry->removeByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID); $actual = $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID); $this->assertEquals($this->customer, $actual); }
/** * @return void */ public function testAuthenticate() { $username = '******'; $password = '******'; $passwordHash = '1a2b3f4c'; $customerData = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $customerModel = $this->getMockBuilder('Magento\\Customer\\Model\\Customer')->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')->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 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); } }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testSendPasswordReminderEmail() { $customerId = 1; $customerStoreId = 2; $customerEmail = '*****@*****.**'; $passwordToken = 'token'; $isFrontendSecure = true; $resetUrl = 'reset url'; $customerData = ['key' => 'value']; $customerName = 'Customer Name'; $templateIdentifier = 'Template Identifier'; $sender = 'Sender'; $customer = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->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); $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $this->storeManager->expects($this->any())->method('getStore')->with($customerStoreId)->willReturn($store); $store->expects($this->any())->method('isFrontUrlSecure')->willReturn($isFrontendSecure); $this->url->expects($this->once())->method('getUrl')->with('customer/account/createPassword', ['_query' => ['id' => $customerId, 'token' => $passwordToken], '_store' => $customerStoreId, '_secure' => $isFrontendSecure])->willReturn($resetUrl); $customerSecure = $this->getMockBuilder('\\Magento\\Customer\\Model\\Data\\CustomerSecure')->disableOriginalConstructor()->setMethods(['addData', 'setData', 'setResetPasswordUrl'])->getMock(); $this->customerRegistry->expects($this->once())->method('retrieveSecureData')->with($customerId)->willReturn($customerSecure); $this->dataObjectProcessor->expects($this->once())->method('buildOutputDataArray')->with($customer, '\\Magento\\Customer\\Api\\Data\\CustomerInterface')->willReturn($customerData); $this->customerViewHelper->expects($this->any())->method('getCustomerName')->with($customer)->willReturn($customerName); $customerSecure->expects($this->once())->method('addData')->with($customerData)->willReturnSelf(); $customerSecure->expects($this->once())->method('setData')->with('name', $customerName)->willReturnSelf(); $customerSecure->expects($this->once())->method('setResetPasswordUrl')->with($resetUrl); $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')->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' => $customerSecure, 'store' => $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, $passwordToken)); }
/** * @return void */ public function testChangePassword() { $customerId = 7; $email = '*****@*****.**'; $currentPassword = '******'; $newPassword = '******'; $passwordHash = '1a2b3f4c'; $customer = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $customer->expects($this->any())->method('getId')->willReturn($customerId); $this->customerRepository->expects($this->once())->method('get')->with($email)->willReturn($customer); $this->accountManagementHelper->expects($this->once())->method('validatePasswordAndLockStatus')->with($customer, $currentPassword); $customerSecure = $this->getMockBuilder('Magento\\Customer\\Model\\Data\\CustomerSecure')->setMethods(['setRpToken', 'setRpTokenCreatedAt', 'setPasswordHash'])->disableOriginalConstructor()->getMock(); $customerSecure->expects($this->once())->method('setRpToken')->with(null); $customerSecure->expects($this->once())->method('setRpTokenCreatedAt')->with(null); $customerSecure->expects($this->once())->method('setPasswordHash')->with($passwordHash); $this->customerRegistry->expects($this->once())->method('retrieveSecureData')->with($customerId)->willReturn($customerSecure); $this->encryptor->expects($this->once())->method('getHash')->with($newPassword, true)->willReturn($passwordHash); $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->assertTrue($this->accountManagement->changePassword($email, $currentPassword, $newPassword)); }
/** * {@inheritdoc} */ public function deleteCustomerByEmail($customerEmail, $websiteId = null) { $customerModel = $this->customerRegistry->retrieveByEmail($customerEmail, $websiteId); $customerId = $customerModel->getId(); $customerModel->delete(); $this->customerRegistry->remove($customerId); return true; }
/** * Retrieve customer data by Id * * @param int $customerId * @return \Magento\Customer\Api\Data\CustomerInterface */ protected function _getCustomerData($customerId) { $customerData = $this->customerRepository->getById($customerId); $this->customerRegistry->remove($customerId); return $customerData; }