123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Api;
- use Magento\Customer\Api\Data\CustomerInterface as Customer;
- use Magento\Customer\Model\AccountManagement;
- use Magento\Framework\Exception\InputException;
- use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
- use Magento\Newsletter\Model\Subscriber;
- use Magento\Security\Model\Config;
- use Magento\TestFramework\Helper\Bootstrap;
- use Magento\TestFramework\Helper\Customer as CustomerHelper;
- use Magento\TestFramework\TestCase\WebapiAbstract;
- /**
- * Test class for Magento\Customer\Api\AccountManagementInterface
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class AccountManagementTest extends WebapiAbstract
- {
- const SERVICE_VERSION = 'V1';
- const SERVICE_NAME = 'customerAccountManagementV1';
- const RESOURCE_PATH = '/V1/customers';
- /**
- * Sample values for testing
- */
- const ATTRIBUTE_CODE = 'attribute_code';
- const ATTRIBUTE_VALUE = 'attribute_value';
- /**
- * @var AccountManagementInterface
- */
- private $accountManagement;
- /**
- * @var \Magento\Framework\Api\SearchCriteriaBuilder
- */
- private $searchCriteriaBuilder;
- /**
- * @var \Magento\Framework\Api\SortOrderBuilder
- */
- private $sortOrderBuilder;
- /**
- * @var \Magento\Framework\Api\Search\FilterGroupBuilder
- */
- private $filterGroupBuilder;
- /**
- * @var CustomerHelper
- */
- private $customerHelper;
- /**
- * @var array
- */
- private $currentCustomerId;
- /** @var Subscriber */
- private $subscriber;
- /**
- * @var \Magento\Framework\Reflection\DataObjectProcessor
- */
- private $dataObjectProcessor;
- /**
- * @var \Magento\Config\Model\Config
- */
- private $config;
- /**
- * @var int
- */
- private $configValue;
- /**
- * Execute per test initialization.
- */
- public function setUp()
- {
- $this->accountManagement = Bootstrap::getObjectManager()->get(
- \Magento\Customer\Api\AccountManagementInterface::class
- );
- $this->searchCriteriaBuilder = Bootstrap::getObjectManager()->create(
- \Magento\Framework\Api\SearchCriteriaBuilder::class
- );
- $this->sortOrderBuilder = Bootstrap::getObjectManager()->create(
- \Magento\Framework\Api\SortOrderBuilder::class
- );
- $this->filterGroupBuilder = Bootstrap::getObjectManager()->create(
- \Magento\Framework\Api\Search\FilterGroupBuilder::class
- );
- $this->customerHelper = new CustomerHelper();
- $this->dataObjectProcessor = Bootstrap::getObjectManager()->create(
- \Magento\Framework\Reflection\DataObjectProcessor::class
- );
- $this->config = Bootstrap::getObjectManager()->create(
- \Magento\Config\Model\Config::class
- );
- $this->initSubscriber();
- if ($this->config->getConfigDataValue(
- Config::XML_PATH_FRONTEND_AREA .
- Config::XML_PATH_PASSWORD_RESET_PROTECTION_TYPE
- ) != 0) {
- $this->configValue = $this->config
- ->getConfigDataValue(
- Config::XML_PATH_FRONTEND_AREA .
- Config::XML_PATH_PASSWORD_RESET_PROTECTION_TYPE
- );
- $this->config->setDataByPath(
- Config::XML_PATH_FRONTEND_AREA . Config::XML_PATH_PASSWORD_RESET_PROTECTION_TYPE,
- 0
- );
- $this->config->save();
- }
- }
- public function tearDown()
- {
- if (!empty($this->currentCustomerId)) {
- foreach ($this->currentCustomerId as $customerId) {
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/' . $customerId,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
- ],
- 'soap' => [
- 'service' => CustomerRepositoryTest::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => CustomerRepositoryTest::SERVICE_NAME . 'DeleteById',
- ],
- ];
- $response = $this->_webApiCall($serviceInfo, ['customerId' => $customerId]);
- $this->assertTrue($response);
- }
- }
- $this->config->setDataByPath(
- Config::XML_PATH_FRONTEND_AREA . Config::XML_PATH_PASSWORD_RESET_PROTECTION_TYPE,
- $this->configValue
- );
- $this->config->save();
- $this->accountManagement = null;
- $this->subscriber = null;
- }
- private function initSubscriber()
- {
- $this->subscriber = Bootstrap::getObjectManager()->create(
- \Magento\Newsletter\Model\Subscriber::class
- );
- }
- public function testCreateCustomer()
- {
- $customerData = $this->_createCustomer();
- $this->assertNotNull($customerData['id']);
- }
- public function testCreateCustomerWithErrors()
- {
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'CreateAccount',
- ],
- ];
- $customerDataArray = $this->dataObjectProcessor->buildOutputDataArray(
- $this->customerHelper->createSampleCustomerDataObject(),
- \Magento\Customer\Api\Data\CustomerInterface::class
- );
- $invalidEmail = 'invalid';
- $customerDataArray['email'] = $invalidEmail;
- $requestData = ['customer' => $customerDataArray, 'password' => CustomerHelper::PASSWORD];
- try {
- $this->_webApiCall($serviceInfo, $requestData);
- $this->fail('Expected exception did not occur.');
- } catch (\Exception $e) {
- if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
- $expectedException = new InputException();
- $expectedException->addError(__('"Email" is not a valid email address.'));
- $this->assertInstanceOf('SoapFault', $e);
- $this->checkSoapFault(
- $e,
- $expectedException->getRawMessage(),
- 'env:Sender',
- $expectedException->getParameters() // expected error parameters
- );
- } else {
- $this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode());
- $exceptionData = $this->processRestExceptionResult($e);
- $expectedExceptionData = [
- 'message' => '"Email" is not a valid email address.',
- ];
- $this->assertEquals($expectedExceptionData, $exceptionData);
- }
- }
- }
- public function testCreateCustomerWithoutOptionalFields()
- {
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'CreateAccount',
- ],
- ];
- $customerDataArray = $this->dataObjectProcessor->buildOutputDataArray(
- $this->customerHelper->createSampleCustomerDataObject(),
- \Magento\Customer\Api\Data\CustomerInterface::class
- );
- unset($customerDataArray['store_id']);
- unset($customerDataArray['website_id']);
- $requestData = ['customer' => $customerDataArray, 'password' => CustomerHelper::PASSWORD];
- try {
- $customerData = $this->_webApiCall($serviceInfo, $requestData, null, 'all');
- $this->assertNotNull($customerData['id']);
- } catch (\Exception $e) {
- $this->fail('Customer should be created without optional fields.');
- }
- }
- /**
- * Test customer activation when it is required
- *
- * @magentoConfigFixture default_store customer/create_account/confirm 0
- */
- public function testActivateCustomer()
- {
- $customerData = $this->_createCustomer();
- $this->assertNotNull($customerData[Customer::CONFIRMATION], 'Customer activation is not required');
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/' . $customerData[Customer::EMAIL] . '/activate',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Activate',
- ],
- ];
- $requestData = [
- 'email' => $customerData[Customer::EMAIL],
- 'confirmationKey' => $customerData[Customer::CONFIRMATION],
- ];
- $result = $this->_webApiCall($serviceInfo, $requestData);
- $this->assertEquals($customerData[Customer::ID], $result[Customer::ID], 'Wrong customer!');
- $this->assertTrue(
- !isset($result[Customer::CONFIRMATION]) || $result[Customer::CONFIRMATION] === null,
- 'Customer is not activated!'
- );
- }
- public function testGetCustomerActivateCustomer()
- {
- $customerData = $this->_createCustomer();
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/' . $customerData[Customer::EMAIL] . '/activate',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Activate',
- ],
- ];
- $requestData = [
- 'email' => $customerData[Customer::EMAIL],
- 'confirmationKey' => $customerData[Customer::CONFIRMATION],
- ];
- $customerResponseData = $this->_webApiCall($serviceInfo, $requestData);
- $this->assertEquals($customerData[Customer::ID], $customerResponseData[Customer::ID]);
- // Confirmation key is removed after confirmation
- $this->assertFalse(isset($customerResponseData[Customer::CONFIRMATION]));
- }
- public function testValidateResetPasswordLinkToken()
- {
- $customerData = $this->_createCustomer();
- /** @var \Magento\Customer\Model\Customer $customerModel */
- $customerModel = Bootstrap::getObjectManager()->create(\Magento\Customer\Model\CustomerFactory::class)
- ->create();
- $customerModel->load($customerData[Customer::ID]);
- $rpToken = 'lsdj579slkj5987slkj595lkj';
- $customerModel->setRpToken('lsdj579slkj5987slkj595lkj');
- $customerModel->setRpTokenCreatedAt(date('Y-m-d H:i:s'));
- $customerModel->save();
- $path = self::RESOURCE_PATH . '/' . $customerData[Customer::ID] . '/password/resetLinkToken/' . $rpToken;
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => $path,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'ValidateResetPasswordLinkToken',
- ],
- ];
- $this->_webApiCall(
- $serviceInfo,
- ['customerId' => $customerData['id'], 'resetPasswordLinkToken' => $rpToken]
- );
- }
- public function testValidateResetPasswordLinkTokenInvalidToken()
- {
- $customerData = $this->_createCustomer();
- $invalidToken = 'fjjkafjie';
- $path = self::RESOURCE_PATH . '/' . $customerData[Customer::ID] . '/password/resetLinkToken/' . $invalidToken;
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => $path,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'ValidateResetPasswordLinkToken',
- ],
- ];
- $expectedMessage = 'The password token is mismatched. Reset and try again.';
- try {
- if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
- $this->_webApiCall(
- $serviceInfo,
- ['customerId' => $customerData['id'], 'resetPasswordLinkToken' => 'invalid']
- );
- } else {
- $this->_webApiCall($serviceInfo);
- }
- $this->fail("Expected exception to be thrown.");
- } catch (\SoapFault $e) {
- $this->assertContains(
- $expectedMessage,
- $e->getMessage(),
- "Exception message does not match"
- );
- } catch (\Exception $e) {
- $errorObj = $this->processRestExceptionResult($e);
- $this->assertEquals($expectedMessage, $errorObj['message']);
- $this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode());
- }
- }
- public function testInitiatePasswordMissingRequiredFields()
- {
- $this->_markTestAsRestOnly('Soap clients explicitly check for required fields based on WSDL.');
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/password',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ]
- ];
- try {
- $this->_webApiCall($serviceInfo);
- } catch (\Exception $e) {
- $this->assertEquals(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST, $e->getCode());
- $exceptionData = $this->processRestExceptionResult($e);
- $expectedExceptionData = [
- 'message' => 'One or more input exceptions have occurred.',
- 'errors' => [
- [
- 'message' => '"%fieldName" is required. Enter and try again.',
- 'parameters' => [
- 'fieldName' => 'email',
- ],
- ],
- [
- 'message' => '"%fieldName" is required. Enter and try again.',
- 'parameters' => [
- 'fieldName' => 'template',
- ]
- ],
- ],
- ];
- $this->assertEquals($expectedExceptionData, $exceptionData);
- }
- }
- public function testInitiatePasswordReset()
- {
- $customerData = $this->_createCustomer();
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/password',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'InitiatePasswordReset',
- ],
- ];
- $requestData = [
- 'email' => $customerData[Customer::EMAIL],
- 'template' => AccountManagement::EMAIL_RESET,
- 'websiteId' => $customerData[Customer::WEBSITE_ID],
- ];
- // This api doesn't return any response.
- // No exception or response means the request was processed successfully.
- // The webapi framework does not return the header information as yet. A check for HTTP 200 would be ideal here
- $this->_webApiCall($serviceInfo, $requestData);
- }
- public function testSendPasswordResetLinkBadEmailOrWebsite()
- {
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/password',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'InitiatePasswordReset',
- ],
- ];
- $requestData = [
- 'email' => 'dummy@example.com',
- 'template' => AccountManagement::EMAIL_RESET,
- 'websiteId' => 0,
- ];
- try {
- $this->_webApiCall($serviceInfo, $requestData);
- } catch (\Exception $e) {
- $expectedErrorParameters =
- [
- 'fieldName' => 'email',
- 'fieldValue' => 'dummy@example.com',
- 'field2Name' => 'websiteId',
- 'field2Value' => 0,
- ];
- if (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) {
- $errorObj = $this->processRestExceptionResult($e);
- $this->assertEquals(
- 'No such entity with %fieldName = %fieldValue, %field2Name = %field2Value',
- $errorObj['message']
- );
- $this->assertEquals($expectedErrorParameters, $errorObj['parameters']);
- $this->assertEquals(HTTPExceptionCodes::HTTP_NOT_FOUND, $e->getCode());
- } else {
- $this->assertInstanceOf('SoapFault', $e);
- $this->checkSoapFault(
- $e,
- 'No such entity with %fieldName = %fieldValue, %field2Name = %field2Value',
- 'env:Sender',
- $expectedErrorParameters
- );
- }
- }
- }
- public function testGetConfirmationStatus()
- {
- $customerData = $this->_createCustomer();
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/' . $customerData[Customer::ID] . '/confirm',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'GetConfirmationStatus',
- ],
- ];
- $confirmationResponse = $this->_webApiCall($serviceInfo, ['customerId' => $customerData['id']]);
- $this->assertEquals(AccountManagement::ACCOUNT_CONFIRMATION_NOT_REQUIRED, $confirmationResponse);
- }
- public function testResendConfirmation()
- {
- $customerData = $this->_createCustomer();
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/confirm',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'ResendConfirmation',
- ],
- ];
- $requestData = [
- 'email' => $customerData[Customer::EMAIL],
- 'websiteId' => $customerData[Customer::WEBSITE_ID],
- ];
- // This api doesn't return any response.
- // No exception or response means the request was processed successfully.
- // The webapi framework does not return the header information as yet. A check for HTTP 200 would be ideal here
- $this->_webApiCall($serviceInfo, $requestData);
- }
- public function testResendConfirmationBadEmailOrWebsite()
- {
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/confirm',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'ResendConfirmation',
- ],
- ];
- $requestData = [
- 'email' => 'dummy@example.com',
- 'websiteId' => 0,
- ];
- try {
- $this->_webApiCall($serviceInfo, $requestData);
- } catch (\Exception $e) {
- $expectedErrorParameters =
- [
- 'fieldName' => 'email',
- 'fieldValue' => 'dummy@example.com',
- 'field2Name' => 'websiteId',
- 'field2Value' => 0,
- ];
- if (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) {
- $errorObj = $this->processRestExceptionResult($e);
- $this->assertEquals(
- 'No such entity with %fieldName = %fieldValue, %field2Name = %field2Value',
- $errorObj['message']
- );
- $this->assertEquals($expectedErrorParameters, $errorObj['parameters']);
- $this->assertEquals(HTTPExceptionCodes::HTTP_NOT_FOUND, $e->getCode());
- } else {
- $this->assertInstanceOf('SoapFault', $e);
- $this->checkSoapFault(
- $e,
- 'No such entity with %fieldName = %fieldValue, %field2Name = %field2Value',
- 'env:Sender',
- $expectedErrorParameters
- );
- }
- }
- }
- public function testValidateCustomerData()
- {
- $customerData = $this->customerHelper->createSampleCustomerDataObject();
- $customerData->setFirstname(null);
- $customerData->setLastname(null);
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/validate',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Validate',
- ],
- ];
- $customerData = $this->dataObjectProcessor->buildOutputDataArray(
- $customerData,
- \Magento\Customer\Api\Data\CustomerInterface::class
- );
- $requestData = ['customer' => $customerData];
- $validationResponse = $this->_webApiCall($serviceInfo, $requestData);
- $this->assertFalse($validationResponse['valid']);
- $this->assertEquals(
- 'The "First Name" attribute value is empty. Set the attribute and try again.',
- $validationResponse['messages'][0]
- );
- $this->assertEquals(
- 'The "Last Name" attribute value is empty. Set the attribute and try again.',
- $validationResponse['messages'][1]
- );
- }
- public function testIsReadonly()
- {
- $customerData = $this->_createCustomer();
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/' . $customerData[Customer::ID] . '/permissions/readonly',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'IsReadonly',
- ],
- ];
- $response = $this->_webApiCall($serviceInfo, ['customerId' => $customerData['id']]);
- $this->assertFalse($response);
- }
- public function testEmailAvailable()
- {
- $customerData = $this->_createCustomer();
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/isEmailAvailable',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'IsEmailAvailable',
- ],
- ];
- $requestData = [
- 'customerEmail' => $customerData[Customer::EMAIL],
- 'websiteId' => $customerData[Customer::WEBSITE_ID],
- ];
- $this->assertFalse($this->_webApiCall($serviceInfo, $requestData));
- }
- public function testEmailAvailableInvalidEmail()
- {
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/isEmailAvailable',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'IsEmailAvailable',
- ],
- ];
- $requestData = [
- 'customerEmail' => 'invalid',
- 'websiteId' => 0,
- ];
- $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
- }
- /**
- * @magentoApiDataFixture Magento/Customer/_files/attribute_user_defined_address.php
- * @magentoApiDataFixture Magento/Customer/_files/attribute_user_defined_customer.php
- */
- public function testCustomAttributes()
- {
- //Sample customer data comes with the disable_auto_group_change custom attribute
- $customerData = $this->customerHelper->createSampleCustomerDataObject();
- //address attribute code from fixture
- $fixtureAddressAttributeCode = 'address_user_attribute';
- //customer attribute code from fixture
- $fixtureCustomerAttributeCode = 'user_attribute';
- //Custom Attribute Values
- $address1CustomAttributeValue = 'value1';
- $address2CustomAttributeValue = 'value2';
- $customerCustomAttributeValue = 'value3';
- $addresses = $customerData->getAddresses();
- $addresses[0]->setCustomAttribute($fixtureAddressAttributeCode, $address1CustomAttributeValue);
- $addresses[1]->setCustomAttribute($fixtureAddressAttributeCode, $address2CustomAttributeValue);
- $customerData->setAddresses($addresses);
- $customerData->setCustomAttribute($fixtureCustomerAttributeCode, $customerCustomAttributeValue);
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'CreateAccount',
- ],
- ];
- $customerDataArray = $this->dataObjectProcessor->buildOutputDataArray(
- $customerData,
- \Magento\Customer\Api\Data\CustomerInterface::class
- );
- $requestData = ['customer' => $customerDataArray, 'password' => CustomerHelper::PASSWORD];
- $customerData = $this->_webApiCall($serviceInfo, $requestData);
- $customerId = $customerData['id'];
- //TODO: Fix assertions to verify custom attributes
- $this->assertNotNull($customerData);
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/' . $customerId ,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
- ],
- 'soap' => [
- 'service' => CustomerRepositoryTest::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => CustomerRepositoryTest::SERVICE_NAME . 'DeleteById',
- ],
- ];
- $response = $this->_webApiCall($serviceInfo, ['customerId' => $customerId]);
- $this->assertTrue($response);
- }
- /**
- * @magentoApiDataFixture Magento/Customer/_files/customer.php
- * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php
- */
- public function testGetDefaultBillingAddress()
- {
- $fixtureCustomerId = 1;
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . "/$fixtureCustomerId/billingAddress",
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'GetDefaultBillingAddress',
- ],
- ];
- $requestData = ['customerId' => $fixtureCustomerId];
- $addressData = $this->_webApiCall($serviceInfo, $requestData);
- $this->assertEquals(
- $this->getFirstFixtureAddressData(),
- $addressData,
- "Default billing address data is invalid."
- );
- }
- /**
- * @magentoApiDataFixture Magento/Customer/_files/customer.php
- * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php
- */
- public function testGetDefaultShippingAddress()
- {
- $fixtureCustomerId = 1;
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . "/$fixtureCustomerId/shippingAddress",
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'GetDefaultShippingAddress',
- ],
- ];
- $requestData = ['customerId' => $fixtureCustomerId];
- $addressData = $this->_webApiCall($serviceInfo, $requestData);
- $this->assertEquals(
- $this->getFirstFixtureAddressData(),
- $addressData,
- "Default shipping address data is invalid."
- );
- }
- /**
- * @return array|bool|float|int|string
- */
- protected function _createCustomer()
- {
- $customerData = $this->customerHelper->createSampleCustomer();
- $this->currentCustomerId[] = $customerData['id'];
- return $customerData;
- }
- /**
- * Retrieve data of the first fixture address.
- *
- * @return array
- */
- protected function getFirstFixtureAddressData()
- {
- return [
- 'firstname' => 'John',
- 'lastname' => 'Smith',
- 'city' => 'CityM',
- 'country_id' => 'US',
- 'company' => 'CompanyName',
- 'postcode' => '75477',
- 'telephone' => '3468676',
- 'street' => ['Green str, 67'],
- 'id' => 1,
- 'default_billing' => true,
- 'default_shipping' => true,
- 'customer_id' => '1',
- 'region' => ['region' => 'Alabama', 'region_id' => 1, 'region_code' => 'AL'],
- 'region_id' => 1,
- ];
- }
- public function testCreateCustomerWithSubscription()
- {
- $customerData = $this->customerHelper->createSampleCustomer(
- ["extension_attributes" => ["is_subscribed" => true]]
- );
- $this->assertNotNull($customerData['id']);
- $this->subscriber->loadByCustomerId($customerData['id']);
- $this->assertNotNull($this->subscriber->getId());
- $this->assertEquals($customerData['id'], $this->subscriber->getCustomerId());
- }
- public function testUnsubscribeCustomer()
- {
- //Creating customer and subscribe
- $customerData = $this->customerHelper->createSampleCustomer(
- ["extension_attributes" => ["is_subscribed" => true]]
- );
- $this->assertNotNull($customerData['id']);
- $this->subscriber->loadByCustomerId($customerData['id']);
- $subscriptionId = $this->subscriber->getId();
- $this->assertNotNull($subscriptionId);
- $this->assertEquals($customerData['id'], $this->subscriber->getCustomerId());
- //Manage customer in order to unsubscribe
- $this->customerHelper->updateSampleCustomer(
- $customerData["id"],
- array_merge(
- $customerData,
- ["extension_attributes" => ["is_subscribed" => false]]
- )
- );
- $this->initSubscriber();
- $this->subscriber->loadByCustomerId($customerData['id']);
- $this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $this->subscriber->getStatus());
- }
- }
|