/** * @return void */ public function testExecute() { $formId = 'user_login'; $login = '******'; $loginParams = ['username' => $login]; $customerId = 7; $redirectUrl = 'http://magento.com/customer/account/login/'; $captchaValue = 'some-value'; $captcha = $this->getMock('Magento\\Captcha\\Model\\DefaultModel', [], [], '', false); $captcha->expects($this->once())->method('isRequired')->with($login)->willReturn(true); $captcha->expects($this->once())->method('isCorrect')->with($captchaValue)->willReturn(false); $captcha->expects($this->once())->method('logAttempt')->with($login); $this->helperMock->expects($this->once())->method('getCaptcha')->with($formId)->willReturn($captcha); $response = $this->getMock('Magento\\Framework\\App\\Response\\Http', [], [], '', false); $response->expects($this->once())->method('setRedirect')->with($redirectUrl); $request = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false); $request->expects($this->any())->method('getPost')->with('login')->willReturn($loginParams); $controller = $this->getMock('Magento\\Framework\\App\\Action\\Action', [], [], '', false); $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request)); $controller->expects($this->any())->method('getResponse')->will($this->returnValue($response)); $this->captchaStringResolverMock->expects($this->once())->method('resolve')->with($request, $formId)->willReturn($captchaValue); $customerDataMock = $this->getMock('\\Magento\\Customer\\Model\\Data\\Customer', ['getId'], [], '', false); $customerDataMock->expects($this->once())->method('getId')->willReturn($customerId); $this->customerRepositoryMock->expects($this->once())->method('get')->with($login)->willReturn($customerDataMock); $this->authenticationMock->expects($this->once())->method('processAuthenticationFailure')->with($customerId); $this->messageManagerMock->expects($this->once())->method('addError')->with(__('Incorrect CAPTCHA')); $this->actionFlagMock->expects($this->once())->method('set')->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true); $this->customerSessionMock->expects($this->once())->method('setUsername')->with($login); $this->customerSessionMock->expects($this->once())->method('getBeforeAuthUrl')->willReturn(false); $this->customerUrlMock->expects($this->once())->method('getLoginUrl')->willReturn($redirectUrl); $this->observer->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller])); }
public function testSubscribe() { $email = '*****@*****.**'; $this->resource->expects($this->any())->method('loadByEmail')->willReturn(['subscriber_status' => 3, 'subscriber_email' => $email, 'name' => 'subscriber_name']); $this->resource->expects($this->any())->method('getIdFieldName')->willReturn('id_field'); $this->scopeConfig->expects($this->any())->method('getValue')->willReturn(true); $this->customerSession->expects($this->any())->method('isLoggedIn')->willReturn(true); $customerDataModel = $this->getMock('\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $this->customerSession->expects($this->any())->method('getCustomerDataObject')->willReturn($customerDataModel); $this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(1); $customerDataModel->expects($this->any())->method('getEmail')->willReturn($email); $this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel); $customerDataModel->expects($this->any())->method('getStoreId')->willReturn(1); $customerDataModel->expects($this->any())->method('getId')->willReturn(1); $this->transportBuilder->expects($this->any())->method('setTemplateIdentifier')->willReturnSelf(); $this->transportBuilder->expects($this->any())->method('setTemplateOptions')->willReturnSelf(); $this->transportBuilder->expects($this->any())->method('setTemplateVars')->willReturnSelf(); $this->transportBuilder->expects($this->any())->method('setFrom')->willReturnSelf(); $this->transportBuilder->expects($this->any())->method('addTo')->willReturnSelf(); $storeModel = $this->getMock('\\Magento\\Store\\Model\\Store', ['getId'], [], '', false); $this->scopeConfig->expects($this->any())->method('getValue')->willReturn('*****@*****.**'); $this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel); $storeModel->expects($this->any())->method('getId')->willReturn(1); $transport = $this->getMock('\\Magento\\Framework\\Mail\\TransportInterface'); $this->transportBuilder->expects($this->any())->method('getTransport')->willReturn($transport); $transport->expects($this->any())->method('sendMessage')->willReturnSelf(); $inlineTranslation = $this->getMock('Magento\\Framework\\Translate\\Inline\\StateInterface'); $inlineTranslation->expects($this->any())->method('resume')->willReturnSelf(); $this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf(); $this->assertEquals(1, $this->subscriber->subscribe($email)); }
public function testSaveActionWithException() { $this->formKeyValidatorMock->expects($this->once())->method('validate')->will($this->returnValue(true)); $this->customerSessionMock->expects($this->any())->method('getCustomerId')->will($this->returnValue(1)); $this->customerRepositoryMock->expects($this->any())->method('getById')->will($this->throwException(new NoSuchEntityException(__(NoSuchEntityException::MESSAGE_SINGLE_FIELD, ['fieldName' => 'customerId', 'value' => 'value'])))); $this->redirectMock->expects($this->once())->method('redirect')->with($this->responseMock, 'customer/account/', []); $this->messageManagerMock->expects($this->never())->method('addSuccess'); $this->messageManagerMock->expects($this->once())->method('addError')->with('Something went wrong while saving your subscription.'); $this->action->execute(); }
public function testConstruct() { $this->customerRepository = $this->getMockForAbstractClass('Magento\\Customer\\Api\\CustomerRepositoryInterface'); $this->customerViewHelper = $this->getMock('Magento\\Customer\\Helper\\View', [], [], '', false); $dummyCustomer = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\CustomerInterface'); $this->customerRepository->expects($this->once())->method('getById')->with('customer id')->will($this->returnValue($dummyCustomer)); $this->customerViewHelper->expects($this->once())->method('getCustomerName')->with($dummyCustomer)->will($this->returnValue(new \Magento\Framework\Object())); $this->request = $this->getMockForAbstractClass('Magento\\Framework\\App\\RequestInterface'); $this->request->expects($this->at(0))->method('getParam')->with('customerId', false)->will($this->returnValue('customer id')); $this->request->expects($this->at(1))->method('getParam')->with('productId', false)->will($this->returnValue(false)); $objectManagerHelper = new ObjectManagerHelper($this); $this->model = $objectManagerHelper->getObject('Magento\\Review\\Block\\Adminhtml\\Main', ['request' => $this->request, 'customerRepository' => $this->customerRepository, 'customerViewHelper' => $this->customerViewHelper]); }
/** * @return void */ public function testExecute() { $customerId = 1; $observerMock = $this->getMock('Magento\\Framework\\Event\\Observer', [], [], '', false); $eventMock = $this->getMock('Magento\\Framework\\Event', ['getData'], [], '', false); $observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock); $eventMock->expects($this->once())->method('getData')->with('model')->willReturn($this->customerModelMock); $this->customerModelMock->expects($this->once())->method('getId')->willReturn($customerId); $this->customerRepositoryMock->expects($this->once())->method('getById')->willReturn($this->customerDataMock); $this->customerDataMock->expects($this->once())->method('getId')->willReturn($customerId); $this->accountManagementHelperMock->expects($this->once())->method('processUnlockData')->with($customerId); $this->customerRepositoryMock->expects($this->once())->method('save')->with($this->customerDataMock); $this->customerLoginSuccessObserver->execute($observerMock); }
/** * @return void */ public function testExecute() { $username = '******'; $customerId = 1; $observerMock = $this->getMock('Magento\\Framework\\Event\\Observer', [], [], '', false); $eventMock = $this->getMock('Magento\\Framework\\Event', ['getData'], [], '', false); $observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock); $eventMock->expects($this->once())->method('getData')->with('username')->willReturn($username); $this->customerRepositoryMock->expects($this->once())->method('get')->with($username)->willReturn($this->customerData); $this->customerData->expects($this->exactly(2))->method('getId')->willReturn($customerId); $this->accountManagementHelperMock->expects($this->once())->method('processCustomerLockoutData')->with($customerId); $this->customerRepositoryMock->expects($this->once())->method('save')->with($this->customerData); $this->observer->execute($observerMock); }
/** * @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); }
/** * test get customer method, method returns customer from service */ public function testGetCustomerLoadCustomerFromService() { $this->moduleManagerMock->expects($this->once())->method('isEnabled')->with($this->equalTo('Magento_PageCache'))->will($this->returnValue(false)); $this->customerSessionMock->expects($this->once())->method('getId')->will($this->returnValue($this->customerId)); $this->customerRepositoryMock->expects($this->once())->method('getById')->with($this->equalTo($this->customerId))->will($this->returnValue($this->customerDataMock)); $this->assertEquals($this->customerDataMock, $this->currentCustomer->getCustomer()); }
public function testBeforeDispatch() { $customerId = 1; $customerGroupId = 1; $this->appState->expects($this->any())->method('getAreaCode')->willReturn(\Magento\Framework\App\Area::AREA_FRONTEND); $this->request->expects($this->any())->method('isPost')->willReturn(true); $customerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMockForAbstractClass(); $customerMock->expects($this->any())->method('getGroupId')->willReturn($customerGroupId); $this->customerRepository->expects($this->any())->method('getById')->with($customerId)->willReturn($customerMock); $this->session->expects($this->any())->method('getCustomerId')->willReturn($customerId); $this->session->expects($this->any())->method('setCustomerData')->with($customerMock); $this->session->expects($this->any())->method('setCustomerGroupId')->with($customerGroupId); $this->session->expects($this->once())->method('regenerateId'); $this->notificationStorage->expects($this->any())->method('isExists')->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerId)->willReturn(true); $this->plugin->beforeDispatch($this->abstractAction, $this->request); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testEditPostActionWithoutErrors() { $customerId = 24; $address = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressInterface', [], '', false); $loadedCustomer = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\CustomerInterface', [], 'loadedCustomer', false); $loadedCustomer->expects($this->once())->method('getAddresses')->willReturn([$address, $address]); $this->resultRedirectFactory->expects($this->once())->method('create')->willReturn($this->redirectResultMock); $this->formKeyValidator->expects($this->once())->method('validate')->willReturn(true); $this->request->expects($this->once())->method('isPost')->willReturn(true); $this->customerSession->expects($this->once())->method('getCustomerId')->willReturn($customerId); $this->customerExtractor->expects($this->once())->method('extract')->willReturn($this->customer); $this->customer->expects($this->once())->method('setId')->with($customerId); $this->customer->expects($this->once())->method('getAddresses')->willReturn(null); $this->customerRepository->expects($this->exactly(2))->method('getById')->with($customerId)->willReturn($loadedCustomer); $this->customer->expects($this->once())->method('setAddresses')->with([$address, $address]); $this->request->expects($this->once())->method('getParam')->with('change_password')->willReturn(true); $this->request->expects($this->at(2))->method('getPost')->with('current_password', null)->willReturn(123); $this->request->expects($this->at(3))->method('getPost')->with('password', null)->willReturn(321); $this->request->expects($this->at(4))->method('getPost')->with('password_confirmation', null)->willReturn(321); $this->customerAccountManagement->expects($this->once())->method('changePassword'); $this->customerRepository->expects($this->once())->method('save'); $messageCollection = $this->getMock('Magento\\Framework\\Message\\Collection', [], [], '', false); $messageCollection->expects($this->once())->method('getCount')->willReturn(0); $this->messageManager->expects($this->once())->method('getMessages')->willReturn($messageCollection); $this->messageManager->expects($this->once())->method('addSuccess')->with('You saved the account information.'); $this->redirectResultMock->expects($this->once())->method('setPath')->with('customer/account')->willReturn('http://test.com/customer/account/edit'); $this->assertSame($this->redirectResultMock, $this->getController()->execute()); }
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)); }
/** * @return void */ public function testCreatePostActionRegistrationDisabled() { $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false)); $this->registration->expects($this->once())->method('isAllowed')->will($this->returnValue(false)); $this->redirectMock->expects($this->once())->method('redirect')->with($this->responseMock, '*/*/', [])->will($this->returnValue(false)); $this->customerRepository->expects($this->never())->method('save'); $this->model->execute(); }
public function testExecuteWithException() { $customersIds = [10, 11, 12]; $this->customerCollectionMock->expects($this->any())->method('getAllIds')->willReturn($customersIds); $this->customerRepositoryMock->expects($this->any())->method('getById')->willThrowException(new \Exception('Some message.')); $this->messageManagerMock->expects($this->once())->method('addError')->with('Some message.'); $this->massAction->execute(); }
/** * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecuteWithExistentCustomer() { $customerId = 22; $addressId = 11; $subscription = 'true'; $postValue = ['customer' => ['entity_id' => $customerId, 'code' => 'value', 'coolness' => false, 'disable_auto_group_change' => 'false'], 'address' => ['_template_' => '_template_', $addressId => ['entity_id' => $addressId, 'default_billing' => 'true', 'default_shipping' => 'true', 'code' => 'value', 'coolness' => false, 'region' => 'region', 'region_id' => 'region_id']], 'subscription' => $subscription]; $filteredData = ['entity_id' => $customerId, 'code' => 'value', 'coolness' => false, 'disable_auto_group_change' => 'false']; $addressFilteredData = ['entity_id' => $addressId, 'default_billing' => 'true', 'default_shipping' => 'true', 'code' => 'value', 'coolness' => false, 'region' => 'region', 'region_id' => 'region_id']; $savedData = ['entity_id' => $customerId, 'darkness' => true, 'name' => 'Name', \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_BILLING => false, \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_SHIPPING => false]; $mergedData = ['entity_id' => $customerId, 'darkness' => true, 'name' => 'Name', 'code' => 'value', 'disable_auto_group_change' => 0, \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_BILLING => $addressId, \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_SHIPPING => $addressId, 'confirmation' => false, 'sendemail_store_id' => '1', 'id' => $customerId]; $mergedAddressData = ['entity_id' => $addressId, 'default_billing' => true, 'default_shipping' => true, 'code' => 'value', 'region' => ['region' => 'region', 'region_id' => 'region_id'], 'region_id' => 'region_id', 'id' => $addressId]; /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $formMock */ $attributeMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface')->disableOriginalConstructor()->getMock(); $attributeMock->expects($this->exactly(2))->method('getAttributeCode')->willReturn('coolness'); $attributeMock->expects($this->exactly(2))->method('getFrontendInput')->willReturn('int'); $attributes = [$attributeMock]; $this->requestMock->expects($this->exactly(3))->method('getPostValue')->willReturn($postValue); $this->requestMock->expects($this->exactly(3))->method('getPost')->willReturnMap([['customer', null, $postValue['customer']], ['address', null, $postValue['address']], ['subscription', null, $subscription]]); /** @var \Magento\Customer\Model\Metadata\Form|\PHPUnit_Framework_MockObject_MockObject $formMock */ $formMock = $this->getMockBuilder('Magento\\Customer\\Model\\Metadata\\Form')->disableOriginalConstructor()->getMock(); $this->formFactoryMock->expects($this->exactly(2))->method('create')->willReturnMap([[\Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'adminhtml_customer', [], false, \Magento\Customer\Model\Metadata\Form::DONT_IGNORE_INVISIBLE, [], $formMock], [\Magento\Customer\Api\AddressMetadataInterface::ENTITY_TYPE_ADDRESS, 'adminhtml_customer_address', [], false, \Magento\Customer\Model\Metadata\Form::DONT_IGNORE_INVISIBLE, [], $formMock]]); $formMock->expects($this->exactly(2))->method('extractData')->willReturnMap([[$this->requestMock, 'customer', true, $filteredData], [$this->requestMock, 'address/' . $addressId, true, $addressFilteredData]]); /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */ $objectMock = $this->getMockBuilder('Magento\\Framework\\DataObject')->disableOriginalConstructor()->getMock(); $this->objectFactoryMock->expects($this->exactly(2))->method('create')->with(['data' => $postValue])->willReturn($objectMock); $objectMock->expects($this->exactly(2))->method('getData')->willReturnMap([['customer', null, $postValue['customer']], ['address/' . $addressId, null, $postValue['address'][$addressId]]]); $formMock->expects($this->exactly(2))->method('getAttributes')->willReturn($attributes); /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */ $customerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock(); $this->customerDataFactoryMock->expects($this->once())->method('create')->willReturn($customerMock); $this->customerRepositoryMock->expects($this->once())->method('getById')->with($customerId)->willReturn($customerMock); $this->customerMapperMock->expects($this->once())->method('toFlatArray')->with($customerMock)->willReturn($savedData); $addressMock = $this->getMockBuilder('\\Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock(); $this->addressDataFactoryMock->expects($this->once())->method('create')->willReturn($addressMock); $this->dataHelperMock->expects($this->exactly(2))->method('populateWithArray')->willReturnMap([[$customerMock, $mergedData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface', $this->dataHelperMock], [$addressMock, $mergedAddressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface', $this->dataHelperMock]]); $customerMock->expects($this->once())->method('setAddresses')->with([$addressMock])->willReturnSelf(); $this->customerRepositoryMock->expects($this->once())->method('save')->with($customerMock)->willReturnSelf(); $customerEmail = '*****@*****.**'; $customerMock->expects($this->once())->method('getEmail')->willReturn($customerEmail); $this->emailNotificationMock->expects($this->once())->method('credentialsChanged')->with($customerMock, $customerEmail)->willReturnSelf(); $this->authorizationMock->expects($this->once())->method('isAllowed')->with(null)->willReturn(true); /** @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject $subscriberMock */ $subscriberMock = $this->getMockBuilder('Magento\\Newsletter\\Model\\Subscriber')->disableOriginalConstructor()->getMock(); $this->subscriberFactoryMock->expects($this->once())->method('create')->with()->willReturn($subscriberMock); $subscriberMock->expects($this->once())->method('subscribeCustomerById')->with($customerId); $subscriberMock->expects($this->never())->method('unsubscribeCustomerById'); $this->sessionMock->expects($this->once())->method('unsCustomerFormData'); $this->registryMock->expects($this->once())->method('register')->with(\Magento\Customer\Controller\RegistryConstants::CURRENT_CUSTOMER_ID, $customerId); $this->messageManagerMock->expects($this->once())->method('addSuccess')->with(__('You saved the customer.'))->willReturnSelf(); $this->requestMock->expects($this->once())->method('getParam')->with('back', false)->willReturn(true); /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */ $redirectMock = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Redirect')->disableOriginalConstructor()->getMock(); $this->redirectFactoryMock->expects($this->once())->method('create')->with([])->willReturn($redirectMock); $redirectMock->expects($this->once())->method('setPath')->with('customer/*/edit', ['id' => $customerId, '_current' => true])->willReturn(true); $this->assertEquals($redirectMock, $this->model->execute()); }
public function testExecuteException() { $exception = new \Exception('Exception message'); $this->prepareMocksForTesting(); $this->customerData->expects($this->once())->method('getDefaultBilling')->willReturn(false); $this->customerRepository->expects($this->once())->method('save')->with($this->customerData)->willThrowException($exception); $this->messageManager->expects($this->once())->method('addError')->with('[Customer ID: 12] We can\'t save the customer.'); $this->logger->expects($this->once())->method('critical')->with($exception); $this->prepareMocksForErrorMessagesProcessing(); $this->assertSame($this->resultJson, $this->controller->execute()); }
public function testExecuteWithException() { $token = 'token'; $customerId = '11'; $password = '******'; $passwordConfirmation = 'password'; $email = '*****@*****.**'; $this->requestMock->expects($this->exactly(2))->method('getQuery')->willReturnMap([['token', $token], ['id', $customerId]]); $this->requestMock->expects($this->exactly(2))->method('getPost')->willReturnMap([['password', $password], ['password_confirmation', $passwordConfirmation]]); /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */ $customerMock = $this->getMockBuilder('\\Magento\\Customer\\Api\\Data\\CustomerInterface')->getMockForAbstractClass(); $this->customerRepositoryMock->expects($this->once())->method('getById')->with($customerId)->willReturn($customerMock); $customerMock->expects($this->once())->method('getEmail')->willReturn($email); $this->accountManagementMock->expects($this->once())->method('resetPassword')->with($email, $token, $password)->willThrowException(new \Exception('Exception.')); $this->messageManagerMock->expects($this->once())->method('addError')->with(__('Something went wrong while saving the new password.'))->willReturnSelf(); /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */ $redirectMock = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Redirect')->disableOriginalConstructor()->getMock(); $this->redirectFactoryMock->expects($this->once())->method('create')->with([])->willReturn($redirectMock); $redirectMock->expects($this->once())->method('setPath')->with('*/*/createPassword', ['id' => $customerId, 'token' => $token])->willReturnSelf(); $this->assertEquals($redirectMock, $this->model->execute()); }
/** * @param bool $expectedResult * @param bool $isCustomerIdValid * @param bool $isCustomerEmulated * @dataProvider getIsLoggedInDataProvider */ public function testIsLoggedIn($expectedResult, $isCustomerIdValid, $isCustomerEmulated) { $customerId = 1; $this->_storageMock->expects($this->any())->method('getData')->with('customer_id')->will($this->returnValue($customerId)); if ($isCustomerIdValid) { $this->customerRepositoryMock->expects($this->once())->method('getById')->with($customerId); } else { $this->customerRepositoryMock->expects($this->once())->method('getById')->with($customerId)->will($this->throwException(new \Exception('Customer ID is invalid.'))); } $this->_storageMock->expects($this->any())->method('getIsCustomerEmulated')->will($this->returnValue($isCustomerEmulated)); $this->assertEquals($expectedResult, $this->_model->isLoggedIn()); }
public function testSubscribeCustomerById() { $customerId = 1; $customerDataMock = $this->getMockBuilder('\\Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $this->customerRepository->expects($this->atLeastOnce())->method('getById')->with($customerId)->willReturn($customerDataMock); $this->resource->expects($this->atLeastOnce())->method('loadByCustomerData')->with($customerDataMock)->willReturn(['subscriber_id' => 1, 'subscriber_status' => 3]); $customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id'); $this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf(); $customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id'); $customerDataMock->expects($this->once())->method('getEmail')->willReturn('email'); $this->sendEmailCheck(); $this->subscriber->subscribeCustomerById($customerId); }
public function testGetCustomer() { $firstName = 'John'; $lastName = 'Doe'; $customerId = 1003; $customer = new \Magento\Framework\DataObject(['firstname' => $firstName, 'lastname' => $lastName]); $this->customerSessionMock->expects($this->once())->method('getCustomerId')->willReturn($customerId); $this->customerRepositoryMock->expects($this->once())->method('getById')->with($customerId)->willReturn($customer); $this->configMock->expects($this->any())->method('useVault')->willReturn(true); $this->customerSessionMock->expects($this->any())->method('isLoggedIn')->willReturn(true); $this->assertEquals($firstName, $this->block->currentCustomerName()); $this->assertEquals($lastName, $this->block->currentCustomerLastName()); }
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); }
/** * @param $conditions * @param $exportShippingCustomerAddressCalls * @param $exportBillingCustomerAddressCalls * @dataProvider prepareRegisteredCustomerQuoteDataProvider */ public function testPrepareRegisteredCustomerQuote($conditions, $exportBillingCustomerAddressCalls, $exportShippingCustomerAddressCalls) { $customerDataMock = $this->getMockBuilder('Magento\\Customer\\Model\\Customer')->setMethods(['getDefaultShipping', 'getDefaultBilling'])->disableOriginalConstructor()->getMock(); $customerDataMock->expects($this->any())->method('getDefaultBilling')->willReturn($conditions['isDefaultBilling']); $customerDataMock->expects($this->any())->method('getDefaultShipping')->willReturn($conditions['isDefaultShipping']); $this->customerRepositoryMock->expects($this->once())->method('getById')->willReturn($customerDataMock); $customerAddressMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock(); $shippingAddressMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Address')->setMethods(['getTelephone', 'getSameAsBilling', 'getCustomerId', 'getSaveInAddressBook', 'exportCustomerAddress'])->disableOriginalConstructor()->getMock(); $shippingAddressMock->expects($this->any())->method('getTelephone')->willReturn($conditions['isTelephone']); $shippingAddressMock->expects($this->any())->method('getSameAsBilling')->willReturn($conditions['isShippingSameAsBilling']); $shippingAddressMock->expects($this->any())->method('getCustomerId')->willReturn(true); $shippingAddressMock->expects($this->any())->method('getSaveInAddressBook')->willReturn($conditions['isShippingSaveInAddressBook']); $shippingAddressMock->expects($this->exactly($exportShippingCustomerAddressCalls))->method('exportCustomerAddress')->willReturn($customerAddressMock); $billingAddressMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Address')->setMethods(['getTelephone', 'getCustomerId', 'getSaveInAddressBook', 'exportCustomerAddress'])->disableOriginalConstructor()->getMock(); $billingAddressMock->expects($this->any())->method('getTelephone')->willReturn($conditions['isTelephone']); $billingAddressMock->expects($this->any())->method('getCustomerId')->willReturn(true); $billingAddressMock->expects($this->any())->method('getSaveInAddressBook')->willReturn($conditions['isBillingSaveInAddressBook']); $billingAddressMock->expects($this->exactly($exportBillingCustomerAddressCalls))->method('exportCustomerAddress')->willReturn($customerAddressMock); $this->quoteMock->expects($this->any())->method('isVirtual')->willReturn(false); $this->quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddressMock); $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($shippingAddressMock); $this->assertInstanceOf('Magento\\Quote\\Model\\Quote', $this->quote->prepareRegisteredCustomerQuote($this->quoteMock, 1)); }
/** * @return void */ public function testAuthenticate() { $username = '******'; $password = '******'; $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->accountManagementHelper->expects($this->once())->method('checkIfLocked')->with($customerData); $this->accountManagementHelper->expects($this->once())->method('validatePasswordAndLockStatus')->with($customerData, $password); $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)); }
public function testGetCustomerWithSession() { $customerId = 1; $data = $customerId . ',2'; $this->urlDecoderMock->expects($this->any())->method('decode')->willReturnArgument(0); $this->requestMock->expects($this->once())->method('getParam')->with('data', null)->willReturn($data); $this->customerSessionMock->expects($this->once())->method('getCustomerId')->willReturn($customerId); $customer = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock(); $this->customerRepositoryMock->expects($this->once())->method('getById')->with($customerId)->willReturn($customer); $this->customerFactoryMock->expects($this->never())->method('create'); $this->assertEquals($customer, $this->model->getCustomer()); // Check that customer is cached $this->assertSame($customer, $this->model->getCustomer()); }
/** * Test case when module is enabled and user is logged in * * @return void */ public function testReportConcurrentUsersToNewRelic() { $testCustomerId = 1; /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserver */ $eventObserver = $this->getMockBuilder('Magento\\Framework\\Event\\Observer')->disableOriginalConstructor()->getMock(); $this->config->expects($this->once())->method('isNewRelicEnabled')->willReturn(true); $this->customerSession->expects($this->once())->method('isLoggedIn')->willReturn(true); $this->customerSession->expects($this->once())->method('getCustomerId')->willReturn($testCustomerId); $customerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $this->customerRepository->expects($this->once())->method('getById')->willReturn($customerMock); $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock); $websiteMock = $this->getMockBuilder('Magento\\Store\\Model\\Website')->disableOriginalConstructor()->getMock(); $this->storeManager->expects($this->once())->method('getWebsite')->willReturn($websiteMock); $this->newRelicWrapper->expects($this->exactly(4))->method('addCustomParameter')->willReturn(true); $this->model->execute($eventObserver); }
/** * @param $customerId * @param $key * @param $backUrl * @param $successUrl * @param $resultUrl * @param $isSetFlag * @param $successMessage * * @dataProvider getSuccessRedirectDataProvider */ public function testSuccessRedirect($customerId, $key, $backUrl, $successUrl, $resultUrl, $isSetFlag, $successMessage) { $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false)); $this->requestMock->expects($this->any())->method('getParam')->willReturnMap([['id', false, $customerId], ['key', false, $key], ['back_url', false, $backUrl]]); $this->customerRepositoryMock->expects($this->any())->method('getById')->with($customerId)->will($this->returnValue($this->customerDataMock)); $email = '*****@*****.**'; $this->customerDataMock->expects($this->once())->method('getEmail')->will($this->returnValue($email)); $this->customerAccountManagementMock->expects($this->once())->method('activate')->with($this->equalTo($email), $this->equalTo($key))->will($this->returnValue($this->customerDataMock)); $this->customerSessionMock->expects($this->any())->method('setCustomerDataAsLoggedIn')->with($this->equalTo($this->customerDataMock))->willReturnSelf(); $this->messageManagerMock->expects($this->any())->method('addSuccess')->with($this->stringContains($successMessage))->willReturnSelf(); $this->storeMock->expects($this->any())->method('getFrontendName')->will($this->returnValue('frontend')); $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($this->storeMock)); $this->urlMock->expects($this->any())->method('getUrl')->with($this->equalTo('*/*/index'), ['_secure' => true])->will($this->returnValue($successUrl)); $this->redirectMock->expects($this->never())->method('success')->with($this->equalTo($resultUrl))->willReturn($resultUrl); $this->scopeConfigMock->expects($this->never())->method('isSetFlag')->with(Url::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD, ScopeInterface::SCOPE_STORE)->willReturn($isSetFlag); $this->model->execute(); }
/** * Run test getQuote method * * @return void * @dataProvider getQuoteDataProvider */ public function testGetQuoteWithQuoteId($customerId, $quoteCustomerId, $expectedNumberOfInvokes) { $quoteId = 22; $storeId = 10; $this->quote->expects($this->any())->method('getQuoteId')->will($this->returnValue($quoteId)); $this->quote->expects($this->any())->method('setQuoteId')->with($quoteId); $this->quote->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId)); $this->quote->expects($this->any())->method('getCustomerId')->will($this->returnValue($customerId)); $dataCustomerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock(); $this->customerRepositoryMock->expects($this->{$expectedNumberOfInvokes}())->method('getById')->with($customerId)->willReturn($dataCustomerMock); $quoteMock = $this->getMock('Magento\\Quote\\Model\\Quote', ['setStoreId', 'setCustomerGroupId', 'setIsActive', 'getId', 'assignCustomer', 'setIgnoreOldQty', 'setIsSuperMode', 'getCustomerId', '__wakeup'], [], '', false); $quoteMock->expects($this->once())->method('setStoreId')->with($storeId); $quoteMock->expects($this->{$expectedNumberOfInvokes}())->method('assignCustomer')->with($dataCustomerMock); $quoteMock->expects($this->once())->method('setIgnoreOldQty')->with(true); $quoteMock->expects($this->once())->method('setIsSuperMode')->with(true); $quoteMock->expects($this->once())->method('getCustomerId')->will($this->returnValue($quoteCustomerId)); $this->quoteRepositoryMock->expects($this->once())->method('get')->with($quoteId)->willReturn($quoteMock); $this->assertEquals($quoteMock, $this->quote->getQuote()); }
/** * @param string $message * @param string $exception * * @dataProvider exceptionDataProvider */ public function testGeneralException($message, $exception) { $customerId = 1; $address = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->getMockForAbstractClass(); $currentCustomerMock = $this->getCurrentCustomerMock($customerId, $address); $newCustomerMock = $this->getNewCustomerMock($customerId, $address); $exception = new $exception(__($message)); $this->validator->expects($this->once())->method('validate')->with($this->request)->willReturn(true); $this->request->expects($this->once())->method('isPost')->willReturn(true); $this->request->expects($this->exactly(3))->method('getParam')->withConsecutive(['change_email'], ['change_email'], ['change_password'])->willReturn(false); $this->request->expects($this->any())->method('getPostValue')->willReturn(true); $this->customerSession->expects($this->once())->method('getCustomerId')->willReturn($customerId); $this->customerSession->expects($this->once())->method('setCustomerFormData')->with(true)->willReturnSelf(); $this->customerRepository->expects($this->once())->method('getById')->with($customerId)->willReturn($currentCustomerMock); $this->customerRepository->expects($this->once())->method('save')->with($newCustomerMock)->willThrowException($exception); $this->customerExtractor->expects($this->once())->method('extract')->with('customer_account_edit', $this->request)->willReturn($newCustomerMock); $this->resultRedirect->expects($this->once())->method('setPath')->with('*/*/edit')->willReturnSelf(); $this->assertSame($this->resultRedirect, $this->model->execute()); }
/** * Test case when module is enabled and user is logged in * * @return void */ public function testReportConcurrentUsers() { $testCustomerId = 1; $testAction = 'JSON string'; /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserver */ $eventObserver = $this->getMockBuilder('Magento\\Framework\\Event\\Observer')->disableOriginalConstructor()->getMock(); $this->config->expects($this->once())->method('isNewRelicEnabled')->willReturn(true); $this->customerSession->expects($this->once())->method('isLoggedIn')->willReturn(true); $this->customerSession->expects($this->once())->method('getCustomerId')->willReturn($testCustomerId); $customerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $this->customerRepository->expects($this->once())->method('getById')->willReturn($customerMock); $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock); $websiteMock = $this->getMockBuilder('Magento\\Store\\Model\\Website')->disableOriginalConstructor()->getMock(); $this->storeManager->expects($this->once())->method('getWebsite')->willReturn($websiteMock); $this->jsonEncoder->expects($this->once())->method('encode')->willReturn($testAction); $this->usersModel->expects($this->once())->method('setData')->with(['type' => 'user_action', 'action' => $testAction])->willReturnSelf(); $this->usersModel->expects($this->once())->method('save'); $this->model->execute($eventObserver); }
public function testResetPasswordActionSendEmail() { $customerId = 1; $email = '*****@*****.**'; $websiteId = 1; $redirectLink = 'customer/*/edit'; $this->_request->expects($this->once())->method('getParam')->with($this->equalTo('customer_id'), $this->equalTo(0))->will($this->returnValue($customerId)); $customer = $this->getMockForAbstractClass('\\Magento\\Customer\\Api\\Data\\CustomerInterface', ['getId', 'getEmail', 'getWebsiteId']); $customer->expects($this->once())->method('getEmail')->will($this->returnValue($email)); $customer->expects($this->once())->method('getWebsiteId')->will($this->returnValue($websiteId)); $this->_customerRepositoryMock->expects($this->once())->method('getById')->with($customerId)->will($this->returnValue($customer)); // verify initiatePasswordReset() is called $this->_customerAccountManagementMock->expects($this->once())->method('initiatePasswordReset')->with($email, AccountManagement::EMAIL_REMINDER, $websiteId); // verify success message $this->messageManager->expects($this->once())->method('addSuccess')->with($this->equalTo('Customer will receive an email with a link to reset password.')); // verify redirect $this->_helper->expects($this->any())->method('getUrl')->with($this->equalTo('customer/*/edit'), $this->equalTo(['id' => $customerId, '_current' => true]))->will($this->returnValue($redirectLink)); $this->resultRedirectMock->expects($this->once())->method('setPath')->with($redirectLink, ['id' => $customerId, '_current' => true]); $this->assertInstanceOf('Magento\\Backend\\Model\\View\\Result\\Redirect', $this->_testedObject->execute()); }
/** * @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); } }