ltsecurityinc安防产品在线商城代码
This commit is contained in:
+290
@@ -0,0 +1,290 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Customer\Api;
|
||||
|
||||
use Magento\Customer\Api\Data\CustomerInterface;
|
||||
use Magento\Framework\Api\AttributeValue;
|
||||
use Magento\Framework\Api\CustomAttributesDataInterface;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use Magento\Framework\Filesystem\Directory\DenyListPathValidator;
|
||||
use Magento\Framework\Filesystem\Directory\WriteFactory;
|
||||
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Helper\Customer as CustomerHelper;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
/**
|
||||
* Test class for Customer's custom attributes
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class AccountManagementCustomAttributesTest 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 CustomerHelper
|
||||
*/
|
||||
private $customerHelper;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $currentCustomerId;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Reflection\DataObjectProcessor
|
||||
*/
|
||||
private $dataObjectProcessor;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Api\Data\ImageFactory
|
||||
*/
|
||||
private $imageFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Filesystem
|
||||
*/
|
||||
private $fileSystem;
|
||||
|
||||
/**
|
||||
* Execute per test initialization.
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->accountManagement = Bootstrap::getObjectManager()->get(
|
||||
\Magento\Customer\Api\AccountManagementInterface::class
|
||||
);
|
||||
|
||||
$this->customerHelper = new CustomerHelper();
|
||||
|
||||
$this->dataObjectProcessor = Bootstrap::getObjectManager()->create(
|
||||
\Magento\Framework\Reflection\DataObjectProcessor::class
|
||||
);
|
||||
|
||||
$this->imageFactory = Bootstrap::getObjectManager()->get(\Magento\Framework\Api\ImageContentFactory::class);
|
||||
|
||||
$this->fileSystem = Bootstrap::getObjectManager()->get(\Magento\Framework\Filesystem::class);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
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->accountManagement = null;
|
||||
$writeFactory = Bootstrap::getObjectManager()
|
||||
->get(WriteFactory::class);
|
||||
$mediaDirectory = $writeFactory->create(DirectoryList::MEDIA);
|
||||
$denyListPathValidator = Bootstrap::getObjectManager()
|
||||
->create(DenyListPathValidator::class, ['driver' => $mediaDirectory->getDriver()]);
|
||||
$denyListPathValidator->addException($mediaDirectory->getAbsolutePath() . ".htaccess");
|
||||
$writeFactoryBypassDenyList = Bootstrap::getObjectManager()
|
||||
->create(WriteFactory::class, ['denyListPathValidator' => $denyListPathValidator]);
|
||||
$mediaDirectoryBypassDenyList = $writeFactoryBypassDenyList->create(DirectoryList::MEDIA);
|
||||
$mediaDirectoryBypassDenyList->delete(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create customer with a sample image file
|
||||
*/
|
||||
protected function createCustomerWithDefaultImageAttribute()
|
||||
{
|
||||
$testImagePath = __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test_image.jpg';
|
||||
$imageData = base64_encode(file_get_contents($testImagePath));
|
||||
$image = $this->imageFactory->create()
|
||||
->setType('image/jpeg')
|
||||
->setName('sample.jpeg')
|
||||
->setBase64EncodedData($imageData);
|
||||
|
||||
$imageData = $this->dataObjectProcessor->buildOutputDataArray(
|
||||
$image,
|
||||
\Magento\Framework\Api\Data\ImageContentInterface::class
|
||||
);
|
||||
return $this->createCustomerWithImageAttribute($imageData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create customer with image attribute
|
||||
*
|
||||
* @param array $imageData
|
||||
* @return array Customer data as array
|
||||
*/
|
||||
protected function createCustomerWithImageAttribute($imageData)
|
||||
{
|
||||
$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',
|
||||
],
|
||||
];
|
||||
|
||||
$customerData = $this->customerHelper->createSampleCustomerDataObject();
|
||||
|
||||
$customerDataArray = $this->dataObjectProcessor->buildOutputDataArray(
|
||||
$customerData,
|
||||
\Magento\Customer\Api\Data\CustomerInterface::class
|
||||
);
|
||||
$customerDataArray['custom_attributes'][] = [
|
||||
'attribute_code' => 'customer_image',
|
||||
'value' => $imageData,
|
||||
];
|
||||
$requestData = [
|
||||
'customer' => $customerDataArray,
|
||||
'password' => \Magento\TestFramework\Helper\Customer::PASSWORD
|
||||
];
|
||||
$customerData = $this->_webApiCall($serviceInfo, $requestData);
|
||||
|
||||
return $customerData;
|
||||
}
|
||||
|
||||
protected function verifyImageAttribute($customAttributeArray, $expectedFileName)
|
||||
{
|
||||
$imageAttributeFound = false;
|
||||
foreach ($customAttributeArray as $customAttribute) {
|
||||
if ($customAttribute[AttributeValue::ATTRIBUTE_CODE] == 'customer_image') {
|
||||
$this->assertStringContainsString($expectedFileName, $customAttribute[AttributeValue::VALUE]);
|
||||
$mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
|
||||
$customerMediaPath = $mediaDirectory->getAbsolutePath(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
|
||||
$imageAttributeFound = $mediaDirectory->getDriver()->isExists(
|
||||
$customerMediaPath . $customAttribute[AttributeValue::VALUE]
|
||||
);
|
||||
$this->assertTrue($imageAttributeFound, 'Expected file was not created');
|
||||
}
|
||||
}
|
||||
if (!$imageAttributeFound) {
|
||||
$this->fail('Expected image attribute missing.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Customer/_files/attribute_user_defined_custom_attribute.php
|
||||
*/
|
||||
public function testCreateCustomerWithImageAttribute()
|
||||
{
|
||||
$customerData = $this->createCustomerWithDefaultImageAttribute();
|
||||
$this->currentCustomerId[] = $customerData['id'];
|
||||
$this->verifyImageAttribute($customerData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES], 'sample.jpeg');
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Customer/_files/attribute_user_defined_custom_attribute.php
|
||||
*/
|
||||
public function testCreateCustomerWithInvalidImageAttribute()
|
||||
{
|
||||
$image = $this->imageFactory->create()
|
||||
->setType('image/jpeg')
|
||||
->setName('sample.jpeg')
|
||||
->setBase64EncodedData('INVALID_IMAGE_DATA');
|
||||
|
||||
$imageData = $this->dataObjectProcessor->buildOutputDataArray(
|
||||
$image,
|
||||
\Magento\Framework\Api\Data\ImageContentInterface::class
|
||||
);
|
||||
$expectedMessage = 'The image content must be valid base64 encoded data.';
|
||||
try {
|
||||
$this->createCustomerWithImageAttribute($imageData);
|
||||
} catch (\SoapFault $e) {
|
||||
$this->assertStringContainsString(
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Customer/_files/attribute_user_defined_custom_attribute.php
|
||||
*/
|
||||
public function testUpdateCustomerWithImageAttribute()
|
||||
{
|
||||
$customerDataArray = $this->createCustomerWithDefaultImageAttribute();
|
||||
$previousCustomerData = $customerDataArray;
|
||||
|
||||
$testImagePath = __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'buttons.png';
|
||||
$imageData = base64_encode(file_get_contents($testImagePath));
|
||||
$image = $this->imageFactory->create()
|
||||
->setType('image/png')
|
||||
->setName('buttons.png')
|
||||
->setBase64EncodedData($imageData);
|
||||
$imageData = $this->dataObjectProcessor->buildOutputDataArray(
|
||||
$image,
|
||||
\Magento\Framework\Api\Data\ImageContentInterface::class
|
||||
);
|
||||
|
||||
//Replace image attribute
|
||||
$customerDataArray['custom_attributes'][1] = [
|
||||
'attribute_code' => 'customer_image',
|
||||
'value' => $imageData,
|
||||
];
|
||||
$requestData = [
|
||||
'customer' => $customerDataArray,
|
||||
'password' => \Magento\TestFramework\Helper\Customer::PASSWORD
|
||||
];
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . "/{$customerDataArray[CustomerInterface::ID]}",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => 'customerCustomerRepositoryV1',
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => 'customerCustomerRepositoryV1Save',
|
||||
],
|
||||
];
|
||||
$customerData = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->verifyImageAttribute($customerData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES], 'buttons.png');
|
||||
|
||||
//Verify that the previous image is deleted
|
||||
$mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
|
||||
$customerMediaPath = $mediaDirectory->getAbsolutePath(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
|
||||
$previousImagePath =
|
||||
$previousCustomerData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES][0][AttributeValue::VALUE];
|
||||
$this->assertFileDoesNotExist($customerMediaPath . $previousImagePath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,415 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Customer\Api;
|
||||
|
||||
use Magento\Customer\Api\Data\CustomerInterface;
|
||||
use Magento\Customer\Model\CustomerRegistry;
|
||||
use Magento\Integration\Api\CustomerTokenServiceInterface;
|
||||
use Magento\Integration\Model\Oauth\Token as TokenModel;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Helper\Customer as CustomerHelper;
|
||||
|
||||
/**
|
||||
* Class AccountManagementMeTest
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php
|
||||
*/
|
||||
class AccountManagementMeTest extends \Magento\TestFramework\TestCase\WebapiAbstract
|
||||
{
|
||||
const RESOURCE_PATH = '/V1/customers/me';
|
||||
const RESOURCE_PATH_CUSTOMER_TOKEN = "/V1/integration/customer/token";
|
||||
const REPO_SERVICE = 'customerCustomerRepositoryV1';
|
||||
const ACCOUNT_SERVICE = 'customerAccountManagementV1';
|
||||
const SERVICE_VERSION = 'V1';
|
||||
|
||||
/**
|
||||
* @var CustomerRepositoryInterface
|
||||
*/
|
||||
private $customerRepository;
|
||||
|
||||
/**
|
||||
* @var AccountManagementInterface
|
||||
*/
|
||||
private $customerAccountManagement;
|
||||
|
||||
/**
|
||||
* @var CustomerRegistry
|
||||
*/
|
||||
private $customerRegistry;
|
||||
|
||||
/**
|
||||
* @var CustomerHelper
|
||||
*/
|
||||
private $customerHelper;
|
||||
|
||||
/**
|
||||
* @var TokenModel
|
||||
*/
|
||||
private $token;
|
||||
|
||||
/**
|
||||
* @var CustomerInterface
|
||||
*/
|
||||
private $customerData;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Reflection\DataObjectProcessor
|
||||
*/
|
||||
private $dataObjectProcessor;
|
||||
|
||||
/**
|
||||
* @var CustomerTokenServiceInterface
|
||||
*/
|
||||
private $tokenService;
|
||||
|
||||
/**
|
||||
* Execute per test initialization.
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->customerRegistry = Bootstrap::getObjectManager()->get(
|
||||
\Magento\Customer\Model\CustomerRegistry::class
|
||||
);
|
||||
|
||||
$this->customerRepository = Bootstrap::getObjectManager()->get(
|
||||
\Magento\Customer\Api\CustomerRepositoryInterface::class,
|
||||
['customerRegistry' => $this->customerRegistry]
|
||||
);
|
||||
|
||||
$this->customerAccountManagement = Bootstrap::getObjectManager()
|
||||
->get(\Magento\Customer\Api\AccountManagementInterface::class);
|
||||
|
||||
$this->customerHelper = new CustomerHelper();
|
||||
$this->customerData = $this->customerHelper->createSampleCustomer();
|
||||
$this->tokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
|
||||
|
||||
// get token
|
||||
$this->resetTokenForCustomerSampleData();
|
||||
|
||||
$this->dataObjectProcessor = Bootstrap::getObjectManager()->create(
|
||||
\Magento\Framework\Reflection\DataObjectProcessor::class
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that fixture customer and his addresses are deleted.
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->customerRepository = null;
|
||||
|
||||
/** @var \Magento\Framework\Registry $registry */
|
||||
$registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
|
||||
$registry->unregister('isSecureArea');
|
||||
$registry->register('isSecureArea', true);
|
||||
|
||||
$registry->unregister('isSecureArea');
|
||||
$registry->register('isSecureArea', false);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testChangePassword()
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . '/password',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
'token' => $this->token,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::ACCOUNT_SERVICE,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::ACCOUNT_SERVICE .'ChangePasswordById',
|
||||
'token' => $this->token
|
||||
]
|
||||
];
|
||||
$requestData = ['currentPassword' => 'test@123', 'newPassword' => '123@test'];
|
||||
if (TESTS_WEB_API_ADAPTER === 'soap') {
|
||||
$requestData['customerId'] = 0;
|
||||
}
|
||||
$this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
|
||||
|
||||
$customerResponseData = $this->customerAccountManagement
|
||||
->authenticate($this->customerData[CustomerInterface::EMAIL], '123@test');
|
||||
$this->assertEquals($this->customerData[CustomerInterface::ID], $customerResponseData->getId());
|
||||
}
|
||||
|
||||
public function testUpdateCustomer()
|
||||
{
|
||||
$customerData = $this->_getCustomerData($this->customerData[CustomerInterface::ID]);
|
||||
$lastName = $customerData->getLastname();
|
||||
|
||||
$updatedCustomerData = $this->dataObjectProcessor->buildOutputDataArray(
|
||||
$customerData,
|
||||
\Magento\Customer\Api\Data\CustomerInterface::class
|
||||
);
|
||||
$updatedCustomerData[CustomerInterface::LASTNAME] = $lastName . 'Updated';
|
||||
$updatedCustomerData[CustomerInterface::ID] = 25;
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
'token' => $this->token,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::REPO_SERVICE,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::REPO_SERVICE .'SaveSelf',
|
||||
'token' => $this->token
|
||||
]
|
||||
];
|
||||
$requestData = ['customer' => $updatedCustomerData];
|
||||
|
||||
$response = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertEquals($lastName . "Updated", $response[CustomerInterface::LASTNAME]);
|
||||
|
||||
$customerData = $this->_getCustomerData($this->customerData[CustomerInterface::ID]);
|
||||
$this->assertEquals($lastName . "Updated", $customerData->getLastname());
|
||||
}
|
||||
|
||||
public function testGetCustomerData()
|
||||
{
|
||||
//Get expected details from the Service directly
|
||||
$customerData = $this->_getCustomerData($this->customerData[CustomerInterface::ID]);
|
||||
$expectedCustomerDetails = $this->dataObjectProcessor->buildOutputDataArray(
|
||||
$customerData,
|
||||
\Magento\Customer\Api\Data\CustomerInterface::class
|
||||
);
|
||||
$expectedCustomerDetails['addresses'][0]['id'] =
|
||||
(int)$expectedCustomerDetails['addresses'][0]['id'];
|
||||
|
||||
$expectedCustomerDetails['addresses'][1]['id'] =
|
||||
(int)$expectedCustomerDetails['addresses'][1]['id'];
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
'token' => $this->token,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::REPO_SERVICE,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::REPO_SERVICE .'GetSelf',
|
||||
'token' => $this->token
|
||||
]
|
||||
];
|
||||
$arguments = [];
|
||||
if (TESTS_WEB_API_ADAPTER === 'soap') {
|
||||
$arguments['customerId'] = 0;
|
||||
}
|
||||
$customerDetailsResponse = $this->_webApiCall($serviceInfo, $arguments);
|
||||
|
||||
unset($expectedCustomerDetails['custom_attributes']);
|
||||
unset($customerDetailsResponse['custom_attributes']); //for REST
|
||||
|
||||
$this->assertEquals($expectedCustomerDetails, $customerDetailsResponse);
|
||||
}
|
||||
|
||||
public function testGetCustomerActivateCustomer()
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . '/activate',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
'token' => $this->token,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::ACCOUNT_SERVICE,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::ACCOUNT_SERVICE .'ActivateById',
|
||||
'token' => $this->token
|
||||
]
|
||||
];
|
||||
|
||||
$requestData = ['confirmationKey' => CustomerHelper::CONFIRMATION];
|
||||
if (TESTS_WEB_API_ADAPTER === 'soap') {
|
||||
$requestData['customerId'] = 0;
|
||||
}
|
||||
|
||||
try {
|
||||
$customerResponseData = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertEquals(
|
||||
$this->customerData[CustomerInterface::ID],
|
||||
$customerResponseData[CustomerInterface::ID]
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$this->fail('Customer is not activated.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the customer details.
|
||||
*
|
||||
* @param int $customerId
|
||||
* @return \Magento\Customer\Api\Data\CustomerInterface
|
||||
*/
|
||||
protected function _getCustomerData($customerId)
|
||||
{
|
||||
$data = $this->customerRepository->getById($customerId);
|
||||
$this->customerRegistry->remove($customerId);
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function testGetDefaultBillingAddress()
|
||||
{
|
||||
$this->resetTokenForCustomerFixture();
|
||||
|
||||
$fixtureCustomerId = 1;
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => "/V1/customers/me/billingAddress",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
'token' => $this->token,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::ACCOUNT_SERVICE,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::ACCOUNT_SERVICE .'GetMyDefaultBillingAddress',
|
||||
'token' => $this->token
|
||||
]
|
||||
];
|
||||
$requestData = ['customerId' => $fixtureCustomerId];
|
||||
$addressData = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertEquals(
|
||||
$this->getFirstFixtureAddressData(),
|
||||
$addressData,
|
||||
"Default billing address data is invalid."
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetDefaultShippingAddress()
|
||||
{
|
||||
$this->resetTokenForCustomerFixture();
|
||||
|
||||
$fixtureCustomerId = 1;
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => "/V1/customers/me/shippingAddress",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
'token' => $this->token,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::ACCOUNT_SERVICE,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::ACCOUNT_SERVICE .'GetMyDefaultShippingAddress',
|
||||
'token' => $this->token
|
||||
]
|
||||
];
|
||||
$requestData = ['customerId' => $fixtureCustomerId];
|
||||
$addressData = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertEquals(
|
||||
$this->getFirstFixtureAddressData(),
|
||||
$addressData,
|
||||
"Default shipping address data is invalid."
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve data of the second fixture address.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getSecondFixtureAddressData()
|
||||
{
|
||||
return [
|
||||
'firstname' => 'John',
|
||||
'lastname' => 'Smith',
|
||||
'city' => 'CityX',
|
||||
'country_id' => 'US',
|
||||
'postcode' => '47676',
|
||||
'telephone' => '3234676',
|
||||
'street' => ['Black str, 48'],
|
||||
'id' => 2,
|
||||
'default_billing' => false,
|
||||
'default_shipping' => false,
|
||||
'customer_id' => '1',
|
||||
'region' => ['region' => 'Alabama', 'region_id' => 1, 'region_code' => 'AL'],
|
||||
'region_id' => 1,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the test's access token for the customer fixture
|
||||
*/
|
||||
protected function resetTokenForCustomerFixture()
|
||||
{
|
||||
$this->resetTokenForCustomer('customer@example.com', 'password');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the test's access token for the created customer sample data
|
||||
*/
|
||||
protected function resetTokenForCustomerSampleData()
|
||||
{
|
||||
$this->resetTokenForCustomer($this->customerData[CustomerInterface::EMAIL], 'test@123');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the test's access token for a particular username and password.
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
*/
|
||||
protected function resetTokenForCustomer($username, $password)
|
||||
{
|
||||
$this->token = $this->tokenService->createCustomerAccessToken($username, $password);
|
||||
$this->customerRegistry->remove($this->customerRepository->get($username)->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer_one_address.php
|
||||
*/
|
||||
public function testGetOtherCustomerInfo()
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => "/V1/customers/me?customerId=1",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
'token' => $this->token,
|
||||
]
|
||||
];
|
||||
$requestData = ['customer' => ["id" => "-1", "Id" => "1"]];
|
||||
try {
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
} catch (\Throwable $exception) {
|
||||
if ($restResponse = json_decode($exception->getMessage(), true)) {
|
||||
$exceptionMessage = $restResponse['message'];
|
||||
}
|
||||
}
|
||||
$this->assertEquals('The customer email is missing. Enter and try again.', $exceptionMessage);
|
||||
}
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Customer\Api;
|
||||
|
||||
use Exception;
|
||||
use Magento\Framework\Exception\AuthenticationException;
|
||||
use Magento\Framework\Webapi\Rest\Request;
|
||||
use Magento\Integration\Api\CustomerTokenServiceInterface;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
/**
|
||||
* Test class for Magento\Integration\Api\CustomerTokenServiceInterface
|
||||
*/
|
||||
class AccountManagementRevokeCustomerTokenTest extends WebapiAbstract
|
||||
{
|
||||
public const RESOURCE_PATH = '/V1/integration/customer/revoke-customer-token';
|
||||
public const INTEGRATION_SERVICE = 'integrationCustomerTokenServiceV1';
|
||||
public const SERVICE_VERSION = 'V1';
|
||||
|
||||
/**
|
||||
* Test token revoking for authenticated customer
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer.php
|
||||
*/
|
||||
public function testRevokeCustomerToken(): void
|
||||
{
|
||||
$token = $this->getCustomerToken();
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH,
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
'token' => $token,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::INTEGRATION_SERVICE,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::INTEGRATION_SERVICE . 'RevokeCustomerAccessToken',
|
||||
'token' => $token,
|
||||
]
|
||||
];
|
||||
|
||||
$requestData = [];
|
||||
if (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
|
||||
$requestData['customerId'] = 0;
|
||||
}
|
||||
|
||||
$this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*
|
||||
* @throws AuthenticationException
|
||||
*/
|
||||
private function getCustomerToken(): string
|
||||
{
|
||||
$userName = 'customer@example.com';
|
||||
$password = 'password';
|
||||
|
||||
/** @var CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = ObjectManager::getInstance()->get(CustomerTokenServiceInterface::class);
|
||||
|
||||
return $customerTokenService->createCustomerAccessToken($userName, $password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test token revoking for guest customer
|
||||
*/
|
||||
public function testRevokeCustomerTokenForGuestCustomer(): void
|
||||
{
|
||||
$this->expectException(Exception::class);
|
||||
$requestData = [];
|
||||
|
||||
if (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
|
||||
$requestData['customerId'] = 0;
|
||||
}
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH,
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::INTEGRATION_SERVICE,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::INTEGRATION_SERVICE . 'RevokeCustomerAccessToken',
|
||||
]
|
||||
];
|
||||
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,900 @@
|
||||
<?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\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\Exception\InputException;
|
||||
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
|
||||
use Magento\Newsletter\Model\Subscriber;
|
||||
use Magento\Security\Model\Config;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
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
|
||||
{
|
||||
public const SERVICE_VERSION = 'V1';
|
||||
public const SERVICE_NAME = 'customerAccountManagementV1';
|
||||
public const RESOURCE_PATH = '/V1/customers';
|
||||
|
||||
/**
|
||||
* Sample values for testing
|
||||
*/
|
||||
public const ATTRIBUTE_CODE = 'attribute_code';
|
||||
public const ATTRIBUTE_VALUE = 'attribute_value';
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
|
||||
$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();
|
||||
}
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
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();
|
||||
|
||||
// Update the customer's confirmation key to a known value
|
||||
$customerData = $this->customerHelper->updateSampleCustomer(
|
||||
$customerData[Customer::ID],
|
||||
[
|
||||
'id' => $customerData[Customer::ID],
|
||||
'confirmation' => CustomerHelper::CONFIRMATION
|
||||
]
|
||||
);
|
||||
|
||||
$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' => CustomerHelper::CONFIRMATION
|
||||
];
|
||||
|
||||
try {
|
||||
$result = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertEquals($customerData[Customer::ID], $result[Customer::ID], 'Wrong customer!');
|
||||
} catch (\Exception $e) {
|
||||
$this->fail('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' => CustomerHelper::CONFIRMATION
|
||||
];
|
||||
|
||||
try {
|
||||
$customerResponseData = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertEquals($customerData[Customer::ID], $customerResponseData[Customer::ID]);
|
||||
} catch (\Exception $e) {
|
||||
$this->fail('Customer is not activated.');
|
||||
}
|
||||
}
|
||||
|
||||
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->assertStringContainsString(
|
||||
$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()
|
||||
{
|
||||
$config = $this->objectManager->get(ScopeConfigInterface::class);
|
||||
|
||||
$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],
|
||||
];
|
||||
|
||||
$emailSetting = $config->getValue(
|
||||
AccountManagement::GUEST_CHECKOUT_LOGIN_OPTION_SYS_CONFIG,
|
||||
ScopeInterface::SCOPE_WEBSITE,
|
||||
$customerData[Customer::WEBSITE_ID]
|
||||
);
|
||||
|
||||
if (!$emailSetting) {
|
||||
$this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
|
||||
} else {
|
||||
$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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,405 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Customer\Api;
|
||||
|
||||
use Magento\Config\Model\ResourceModel\Config;
|
||||
use Magento\Customer\Api\Data\AddressInterface as Address;
|
||||
use Magento\Customer\Model\Data\AttributeMetadata;
|
||||
use Magento\Framework\App\Config\ReinitableConfigInterface;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
/**
|
||||
* Customer Address Metadata API test
|
||||
*/
|
||||
class AddressMetadataTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_NAME = "customerAddressMetadataV1";
|
||||
const SERVICE_VERSION = "V1";
|
||||
const RESOURCE_PATH = "/V1/attributeMetadata/customerAddress";
|
||||
|
||||
/**
|
||||
* @var Config $config
|
||||
*/
|
||||
private $resourceConfig;
|
||||
|
||||
/**
|
||||
* @var ReinitableConfigInterface
|
||||
*/
|
||||
private $reinitConfig;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$objectManager = ObjectManager::getInstance();
|
||||
$this->resourceConfig = $objectManager->get(Config::class);
|
||||
$this->reinitConfig = $objectManager->get(ReinitableConfigInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieval of attribute metadata for the address entity type.
|
||||
*
|
||||
* @param string $attributeCode The attribute code of the requested metadata.
|
||||
* @param array $expectedMetadata Expected entity metadata for the attribute code.
|
||||
* @dataProvider getAttributeMetadataDataProvider
|
||||
* @magentoDbIsolation disabled
|
||||
*/
|
||||
public function testGetAttributeMetadata($attributeCode, $configOptions, $expectedMetadata)
|
||||
{
|
||||
$this->initConfig($configOptions);
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . "/attribute/$attributeCode",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'GetAttributeMetadata',
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
'attributeCode' => $attributeCode,
|
||||
];
|
||||
|
||||
$attributeMetadata = $this->_webapiCall($serviceInfo, $requestData);
|
||||
$validationResult = $this->checkValidationRules($expectedMetadata, $attributeMetadata);
|
||||
list($expectedMetadata, $attributeMetadata) = $validationResult;
|
||||
$this->assertEquals($expectedMetadata, $attributeMetadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testGetAttributeMetadata.
|
||||
*
|
||||
* @return array
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function getAttributeMetadataDataProvider()
|
||||
{
|
||||
return [
|
||||
Address::POSTCODE => [
|
||||
Address::POSTCODE,
|
||||
[],
|
||||
[
|
||||
AttributeMetadata::FRONTEND_INPUT => 'text',
|
||||
AttributeMetadata::INPUT_FILTER => '',
|
||||
AttributeMetadata::STORE_LABEL => 'Zip/Postal Code',
|
||||
AttributeMetadata::MULTILINE_COUNT => 0,
|
||||
AttributeMetadata::VALIDATION_RULES => [],
|
||||
AttributeMetadata::VISIBLE => true,
|
||||
AttributeMetadata::REQUIRED => false,
|
||||
AttributeMetadata::DATA_MODEL => \Magento\Customer\Model\Attribute\Data\Postcode::class,
|
||||
AttributeMetadata::OPTIONS => [],
|
||||
AttributeMetadata::FRONTEND_CLASS => '',
|
||||
AttributeMetadata::USER_DEFINED => false,
|
||||
AttributeMetadata::SORT_ORDER => 110,
|
||||
AttributeMetadata::FRONTEND_LABEL => 'Zip/Postal Code',
|
||||
AttributeMetadata::NOTE => '',
|
||||
AttributeMetadata::SYSTEM => true,
|
||||
AttributeMetadata::BACKEND_TYPE => 'static',
|
||||
AttributeMetadata::IS_USED_IN_GRID => true,
|
||||
AttributeMetadata::IS_VISIBLE_IN_GRID => true,
|
||||
AttributeMetadata::IS_FILTERABLE_IN_GRID => true,
|
||||
AttributeMetadata::IS_SEARCHABLE_IN_GRID => true,
|
||||
AttributeMetadata::ATTRIBUTE_CODE => 'postcode',
|
||||
],
|
||||
],
|
||||
'prefix' => [
|
||||
'prefix',
|
||||
[
|
||||
['path' => 'customer/address/prefix_show', 'value' => 'opt'],
|
||||
['path' => 'customer/address/prefix_options', 'value' => 'prefA;prefB']
|
||||
],
|
||||
[
|
||||
AttributeMetadata::FRONTEND_INPUT => 'text',
|
||||
AttributeMetadata::INPUT_FILTER => '',
|
||||
AttributeMetadata::STORE_LABEL => 'Name Prefix',
|
||||
AttributeMetadata::MULTILINE_COUNT => 0,
|
||||
AttributeMetadata::VALIDATION_RULES => [],
|
||||
AttributeMetadata::VISIBLE => false,
|
||||
AttributeMetadata::REQUIRED => false,
|
||||
AttributeMetadata::DATA_MODEL => '',
|
||||
AttributeMetadata::OPTIONS => [
|
||||
[
|
||||
'label' => 'prefA',
|
||||
'value' => 'prefA',
|
||||
],
|
||||
[
|
||||
'label' => 'prefB',
|
||||
'value' => 'prefB',
|
||||
],
|
||||
],
|
||||
AttributeMetadata::FRONTEND_CLASS => '',
|
||||
AttributeMetadata::USER_DEFINED => false,
|
||||
AttributeMetadata::SORT_ORDER => 10,
|
||||
AttributeMetadata::FRONTEND_LABEL => 'Name Prefix',
|
||||
AttributeMetadata::NOTE => '',
|
||||
AttributeMetadata::SYSTEM => false,
|
||||
AttributeMetadata::BACKEND_TYPE => 'static',
|
||||
AttributeMetadata::IS_USED_IN_GRID => false,
|
||||
AttributeMetadata::IS_VISIBLE_IN_GRID => false,
|
||||
AttributeMetadata::IS_FILTERABLE_IN_GRID => false,
|
||||
AttributeMetadata::IS_SEARCHABLE_IN_GRID => false,
|
||||
AttributeMetadata::ATTRIBUTE_CODE => 'prefix',
|
||||
],
|
||||
],
|
||||
'suffix' => [
|
||||
'suffix',
|
||||
[
|
||||
['path' => 'customer/address/suffix_show', 'value' => 'opt'],
|
||||
['path' => 'customer/address/suffix_options', 'value' => 'suffA;suffB']
|
||||
],
|
||||
[
|
||||
AttributeMetadata::FRONTEND_INPUT => 'text',
|
||||
AttributeMetadata::INPUT_FILTER => '',
|
||||
AttributeMetadata::STORE_LABEL => 'Name Suffix',
|
||||
AttributeMetadata::MULTILINE_COUNT => 0,
|
||||
AttributeMetadata::VALIDATION_RULES => [],
|
||||
AttributeMetadata::VISIBLE => false,
|
||||
AttributeMetadata::REQUIRED => false,
|
||||
AttributeMetadata::DATA_MODEL => '',
|
||||
AttributeMetadata::OPTIONS => [
|
||||
[
|
||||
'label' => 'suffA',
|
||||
'value' => 'suffA',
|
||||
],
|
||||
[
|
||||
'label' => 'suffB',
|
||||
'value' => 'suffB',
|
||||
],
|
||||
],
|
||||
AttributeMetadata::FRONTEND_CLASS => '',
|
||||
AttributeMetadata::USER_DEFINED => false,
|
||||
AttributeMetadata::SORT_ORDER => 50,
|
||||
AttributeMetadata::FRONTEND_LABEL => 'Name Suffix',
|
||||
AttributeMetadata::NOTE => '',
|
||||
AttributeMetadata::SYSTEM => false,
|
||||
AttributeMetadata::BACKEND_TYPE => 'static',
|
||||
AttributeMetadata::IS_USED_IN_GRID => false,
|
||||
AttributeMetadata::IS_VISIBLE_IN_GRID => false,
|
||||
AttributeMetadata::IS_FILTERABLE_IN_GRID => false,
|
||||
AttributeMetadata::IS_SEARCHABLE_IN_GRID => false,
|
||||
AttributeMetadata::ATTRIBUTE_CODE => 'suffix',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieval of all address attribute metadata.
|
||||
*/
|
||||
public function testGetAllAttributesMetadata()
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'GetAllAttributesMetadata',
|
||||
],
|
||||
];
|
||||
|
||||
$attributeMetadata = $this->_webApiCall($serviceInfo);
|
||||
$this->assertCount(19, $attributeMetadata);
|
||||
$postcode = $this->getAttributeMetadataDataProvider()[Address::POSTCODE][2];
|
||||
$validationResult = $this->checkMultipleAttributesValidationRules($postcode, $attributeMetadata);
|
||||
list($postcode, $attributeMetadata) = $validationResult;
|
||||
$this->assertContainsEquals($postcode, $attributeMetadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieval of custom address attribute metadata.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Customer/_files/attribute_user_defined_address_custom_attribute.php
|
||||
*/
|
||||
public function testGetCustomAttributesMetadata()
|
||||
{
|
||||
$customAttributeCode = 'custom_attribute1';
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . '/custom',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'GetCustomAttributesMetadata',
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = ['attribute_code' => $customAttributeCode];
|
||||
$attributeMetadata = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertCount(2, $attributeMetadata);
|
||||
$this->assertEquals($customAttributeCode, $attributeMetadata[0]['attribute_code']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieval of attributes
|
||||
*
|
||||
* @param string $formCode Form code
|
||||
* @param array $expectedMetadata The expected attribute metadata
|
||||
* @dataProvider getAttributesDataProvider
|
||||
*/
|
||||
public function testGetAttributes($formCode, $expectedMetadata)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . "/form/$formCode",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'GetAttributes',
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
'formCode' => $formCode,
|
||||
];
|
||||
|
||||
$attributeMetadataList = $this->_webApiCall($serviceInfo, $requestData);
|
||||
foreach ($attributeMetadataList as $attributeMetadata) {
|
||||
if (isset($attributeMetadata['attribute_code'])
|
||||
&& $attributeMetadata['attribute_code'] == $expectedMetadata['attribute_code']
|
||||
) {
|
||||
$validationResult = $this->checkValidationRules($expectedMetadata, $attributeMetadata);
|
||||
list($expectedMetadata, $attributeMetadata) = $validationResult;
|
||||
$this->assertEquals($expectedMetadata, $attributeMetadata);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testGetAttributes.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAttributesDataProvider()
|
||||
{
|
||||
$attributeMetadata = $this->getAttributeMetadataDataProvider();
|
||||
return [
|
||||
[
|
||||
'customer_address_edit',
|
||||
$attributeMetadata[Address::POSTCODE][2],
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that expected and actual attribute metadata validation rules are equal
|
||||
* and removes the validation rules entry from expected and actual attribute metadata
|
||||
*
|
||||
* @param array $expectedResult
|
||||
* @param array $actualResult
|
||||
* @return array
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
* phpcs:disable Generic.Metrics.NestingLevel
|
||||
*/
|
||||
public function checkValidationRules($expectedResult, $actualResult)
|
||||
{
|
||||
$expectedRules = [];
|
||||
$actualRules = [];
|
||||
|
||||
if (isset($expectedResult[AttributeMetadata::VALIDATION_RULES])) {
|
||||
$expectedRules = $expectedResult[AttributeMetadata::VALIDATION_RULES];
|
||||
unset($expectedResult[AttributeMetadata::VALIDATION_RULES]);
|
||||
}
|
||||
if (isset($actualResult[AttributeMetadata::VALIDATION_RULES])) {
|
||||
$actualRules = $actualResult[AttributeMetadata::VALIDATION_RULES];
|
||||
unset($actualResult[AttributeMetadata::VALIDATION_RULES]);
|
||||
}
|
||||
|
||||
if (is_array($expectedRules) && is_array($actualRules)) {
|
||||
foreach ($expectedRules as $expectedRule) {
|
||||
if (isset($expectedRule['name']) && isset($expectedRule['value'])) {
|
||||
$found = false;
|
||||
foreach ($actualRules as $actualRule) {
|
||||
if (isset($actualRule['name']) && isset($actualRule['value'])) {
|
||||
if ($expectedRule['name'] == $actualRule['name']
|
||||
&& $expectedRule['value'] == $actualRule['value']
|
||||
) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->assertTrue($found);
|
||||
}
|
||||
}
|
||||
}
|
||||
return [$expectedResult, $actualResult];
|
||||
}
|
||||
//phpcs:enable
|
||||
|
||||
/**
|
||||
* Check specific attribute validation rules in set of multiple attributes
|
||||
*
|
||||
* @param array $expectedResult Set of expected attribute metadata
|
||||
* @param array $actualResultSet Set of actual attribute metadata
|
||||
* @return array
|
||||
*/
|
||||
public function checkMultipleAttributesValidationRules($expectedResult, $actualResultSet)
|
||||
{
|
||||
if (is_array($expectedResult) && is_array($actualResultSet)) {
|
||||
if (isset($expectedResult[AttributeMetadata::ATTRIBUTE_CODE])) {
|
||||
foreach ($actualResultSet as $actualAttributeKey => $actualAttribute) {
|
||||
if (isset($actualAttribute[AttributeMetadata::ATTRIBUTE_CODE])
|
||||
&& $expectedResult[AttributeMetadata::ATTRIBUTE_CODE]
|
||||
== $actualAttribute[AttributeMetadata::ATTRIBUTE_CODE]
|
||||
) {
|
||||
$this->checkValidationRules($expectedResult, $actualAttribute);
|
||||
unset($actualResultSet[$actualAttributeKey][AttributeMetadata::VALIDATION_RULES]);
|
||||
}
|
||||
}
|
||||
unset($expectedResult[AttributeMetadata::VALIDATION_RULES]);
|
||||
}
|
||||
}
|
||||
return [$expectedResult, $actualResultSet];
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove test attribute
|
||||
*/
|
||||
public static function tearDownAfterClass(): void
|
||||
{
|
||||
parent::tearDownAfterClass();
|
||||
/** @var \Magento\Customer\Model\Attribute $attribute */
|
||||
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Customer\Model\Attribute::class
|
||||
);
|
||||
foreach (['custom_attribute1', 'custom_attribute2'] as $attributeCode) {
|
||||
$attribute->loadByCode('customer_address', $attributeCode);
|
||||
$attribute->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set core config data.
|
||||
*
|
||||
* @param $configOptions
|
||||
*/
|
||||
private function initConfig(array $configOptions): void
|
||||
{
|
||||
if ($configOptions) {
|
||||
foreach ($configOptions as $option) {
|
||||
$this->resourceConfig->saveConfig($option['path'], $option['value']);
|
||||
}
|
||||
}
|
||||
$this->reinitConfig->reinit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Customer\Api;
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
class AddressRepositoryTest extends \Magento\TestFramework\TestCase\WebapiAbstract
|
||||
{
|
||||
const SOAP_SERVICE_NAME = 'customerAddressRepositoryV1';
|
||||
const SOAP_SERVICE_VERSION = 'V1';
|
||||
|
||||
/** @var \Magento\Customer\Api\AddressRepositoryInterface */
|
||||
protected $addressRepository;
|
||||
|
||||
/** @var \Magento\Customer\Api\CustomerRepositoryInterface */
|
||||
protected $customerRepository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$this->customerRepository = $objectManager->get(
|
||||
\Magento\Customer\Api\CustomerRepositoryInterface::class
|
||||
);
|
||||
$this->addressRepository = $objectManager->get(
|
||||
\Magento\Customer\Api\AddressRepositoryInterface::class
|
||||
);
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that fixture customer and his addresses are deleted.
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
/** @var \Magento\Framework\Registry $registry */
|
||||
$registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
|
||||
$registry->unregister('isSecureArea');
|
||||
$registry->register('isSecureArea', true);
|
||||
|
||||
try {
|
||||
$fixtureFirstAddressId = 1;
|
||||
$this->addressRepository->deleteById($fixtureFirstAddressId);
|
||||
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
|
||||
/** First address fixture was not used */
|
||||
}
|
||||
try {
|
||||
$fixtureSecondAddressId = 2;
|
||||
$this->addressRepository->deleteById($fixtureSecondAddressId);
|
||||
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
|
||||
/** Second address fixture was not used */
|
||||
}
|
||||
try {
|
||||
$fixtureCustomerId = 1;
|
||||
$this->customerRepository->deleteById($fixtureCustomerId);
|
||||
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
|
||||
/** Customer fixture was not used */
|
||||
}
|
||||
|
||||
$registry->unregister('isSecureArea');
|
||||
$registry->register('isSecureArea', false);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer_address.php
|
||||
*/
|
||||
public function testGetAddress()
|
||||
{
|
||||
$fixtureAddressId = 1;
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => "/V1/customers/addresses/{$fixtureAddressId}",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SOAP_SERVICE_NAME,
|
||||
'serviceVersion' => self::SOAP_SERVICE_VERSION,
|
||||
'operation' => self::SOAP_SERVICE_NAME . 'GetById',
|
||||
],
|
||||
];
|
||||
$requestData = ['addressId' => $fixtureAddressId];
|
||||
$addressData = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertEquals(
|
||||
$this->getFirstFixtureAddressData(),
|
||||
$addressData,
|
||||
"Address data is invalid."
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer_address.php
|
||||
*/
|
||||
public function testDeleteAddress()
|
||||
{
|
||||
$fixtureAddressId = 1;
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => "/V1/addresses/{$fixtureAddressId}",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SOAP_SERVICE_NAME,
|
||||
'serviceVersion' => self::SOAP_SERVICE_VERSION,
|
||||
'operation' => self::SOAP_SERVICE_NAME . 'DeleteById',
|
||||
],
|
||||
];
|
||||
$requestData = ['addressId' => $fixtureAddressId];
|
||||
$response = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertTrue($response, 'Expected response should be true.');
|
||||
|
||||
$this->expectException(\Magento\Framework\Exception\NoSuchEntityException::class);
|
||||
$this->expectExceptionMessage('No such entity with addressId = 1');
|
||||
$this->addressRepository->getById($fixtureAddressId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve data of the second fixture address.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getSecondFixtureAddressData()
|
||||
{
|
||||
return [
|
||||
'firstname' => 'John',
|
||||
'lastname' => 'Smith',
|
||||
'city' => 'CityX',
|
||||
'country_id' => 'US',
|
||||
'postcode' => '47676',
|
||||
'telephone' => '3234676',
|
||||
'street' => ['Black str, 48'],
|
||||
'id' => 2,
|
||||
'default_billing' => false,
|
||||
'default_shipping' => false,
|
||||
'customer_id' => '1',
|
||||
'region' => ['region' => 'Alabama', 'region_id' => 1, 'region_code' => 'AL'],
|
||||
'region_id' => 1,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Customer\Api;
|
||||
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
/**
|
||||
* Web API tests for CustomerGroupConfig model.
|
||||
*/
|
||||
class CustomerGroupConfigTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_NAME = "customerCustomerGroupConfigV1";
|
||||
const SERVICE_VERSION = "V1";
|
||||
const RESOURCE_PATH = "/V1/customerGroups";
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSetDefaultGroup()
|
||||
{
|
||||
$customerGroupId = 1;
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . "/default/$customerGroupId",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => 'customerCustomerGroupConfigV1SetDefaultCustomerGroup',
|
||||
],
|
||||
];
|
||||
$requestData = ['id' => $customerGroupId];
|
||||
$groupData = $this->_webApiCall($serviceInfo, $requestData);
|
||||
|
||||
$this->assertEquals($customerGroupId, $groupData, "The default group does not match.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Web API test for setDefaultGroup() method when there is no customer group exists with provided ID.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetDefaultGroupNonExistingGroup()
|
||||
{
|
||||
$customerGroupId = 10000;
|
||||
$expectedMessage = 'No such entity with %fieldName = %fieldValue';
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . "/default/$customerGroupId",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => 'customerCustomerGroupConfigV1SetDefaultCustomerGroup',
|
||||
],
|
||||
];
|
||||
$requestData = ['id' => $customerGroupId];
|
||||
try {
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->fail("Expected exception");
|
||||
} catch (\SoapFault $e) {
|
||||
$this->assertStringContainsString(
|
||||
$expectedMessage,
|
||||
$e->getMessage(),
|
||||
"SoapFault does not contain expected message."
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$this->assertStringContainsString(
|
||||
$expectedMessage,
|
||||
$e->getMessage(),
|
||||
"Exception does not contain expected message."
|
||||
);
|
||||
$this->assertStringContainsString((string)$customerGroupId, $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,348 @@
|
||||
<?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\Data\AttributeMetadata;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
/**
|
||||
* Customer Metadata API test
|
||||
*/
|
||||
class CustomerMetadataTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_NAME = "customerCustomerMetadataV1";
|
||||
const SERVICE_VERSION = "V1";
|
||||
const RESOURCE_PATH = "/V1/attributeMetadata/customer";
|
||||
|
||||
/**
|
||||
* @var CustomerMetadataInterface
|
||||
*/
|
||||
private $customerMetadata;
|
||||
|
||||
/**
|
||||
* Execute per test initialization.
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->customerMetadata = Bootstrap::getObjectManager()->create(CustomerMetadataInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieval of attribute metadata for the customer entity type.
|
||||
*
|
||||
* @param string $attributeCode The attribute code of the requested metadata.
|
||||
* @param array $expectedMetadata Expected entity metadata for the attribute code.
|
||||
* @dataProvider getAttributeMetadataDataProvider
|
||||
*/
|
||||
public function testGetAttributeMetadata($attributeCode, $expectedMetadata)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . "/attribute/$attributeCode",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'GetAttributeMetadata',
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
'attributeCode' => $attributeCode,
|
||||
];
|
||||
|
||||
$attributeMetadata = $this->_webapiCall($serviceInfo, $requestData);
|
||||
|
||||
$validationResult = $this->checkValidationRules($expectedMetadata, $attributeMetadata);
|
||||
list($expectedMetadata, $attributeMetadata) = $validationResult;
|
||||
$this->assertEquals($expectedMetadata, $attributeMetadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testGetAttributeMetadata.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAttributeMetadataDataProvider()
|
||||
{
|
||||
return [
|
||||
Customer::FIRSTNAME => [
|
||||
Customer::FIRSTNAME,
|
||||
[
|
||||
AttributeMetadata::FRONTEND_INPUT => 'text',
|
||||
AttributeMetadata::INPUT_FILTER => 'trim',
|
||||
AttributeMetadata::STORE_LABEL => 'First Name',
|
||||
AttributeMetadata::MULTILINE_COUNT => 0,
|
||||
AttributeMetadata::VALIDATION_RULES => [
|
||||
['name' => 'min_text_length', 'value' => 1],
|
||||
['name' => 'max_text_length', 'value' => 255],
|
||||
],
|
||||
AttributeMetadata::VISIBLE => true,
|
||||
AttributeMetadata::REQUIRED => true,
|
||||
AttributeMetadata::DATA_MODEL => '',
|
||||
AttributeMetadata::OPTIONS => [],
|
||||
AttributeMetadata::FRONTEND_CLASS => 'required-entry',
|
||||
AttributeMetadata::USER_DEFINED => false,
|
||||
AttributeMetadata::SORT_ORDER => 40,
|
||||
AttributeMetadata::FRONTEND_LABEL => 'First Name',
|
||||
AttributeMetadata::NOTE => '',
|
||||
AttributeMetadata::SYSTEM => true,
|
||||
AttributeMetadata::BACKEND_TYPE => 'static',
|
||||
AttributeMetadata::IS_USED_IN_GRID => '',
|
||||
AttributeMetadata::IS_VISIBLE_IN_GRID => '',
|
||||
AttributeMetadata::IS_FILTERABLE_IN_GRID => '',
|
||||
AttributeMetadata::IS_SEARCHABLE_IN_GRID => '',
|
||||
AttributeMetadata::ATTRIBUTE_CODE => 'firstname',
|
||||
|
||||
],
|
||||
],
|
||||
Customer::GENDER => [
|
||||
Customer::GENDER,
|
||||
[
|
||||
AttributeMetadata::FRONTEND_INPUT => 'select',
|
||||
AttributeMetadata::INPUT_FILTER => '',
|
||||
AttributeMetadata::STORE_LABEL => 'Gender',
|
||||
AttributeMetadata::MULTILINE_COUNT => 0,
|
||||
AttributeMetadata::VALIDATION_RULES => [],
|
||||
AttributeMetadata::VISIBLE => false,
|
||||
AttributeMetadata::REQUIRED => false,
|
||||
AttributeMetadata::DATA_MODEL => '',
|
||||
AttributeMetadata::OPTIONS => [
|
||||
['label' => ' ', 'value' => ''],
|
||||
['label' => 'Male', 'value' => '1'],
|
||||
['label' => 'Female', 'value' => '2'],
|
||||
['label' => 'Not Specified', 'value' => '3']
|
||||
],
|
||||
AttributeMetadata::FRONTEND_CLASS => '',
|
||||
AttributeMetadata::USER_DEFINED => false,
|
||||
AttributeMetadata::SORT_ORDER => 110,
|
||||
AttributeMetadata::FRONTEND_LABEL => 'Gender',
|
||||
AttributeMetadata::NOTE => '',
|
||||
AttributeMetadata::SYSTEM => false,
|
||||
AttributeMetadata::BACKEND_TYPE => 'static',
|
||||
AttributeMetadata::IS_USED_IN_GRID => true,
|
||||
AttributeMetadata::IS_VISIBLE_IN_GRID => true,
|
||||
AttributeMetadata::IS_FILTERABLE_IN_GRID => true,
|
||||
AttributeMetadata::IS_SEARCHABLE_IN_GRID => '',
|
||||
AttributeMetadata::ATTRIBUTE_CODE => 'gender',
|
||||
],
|
||||
],
|
||||
Customer::WEBSITE_ID => [
|
||||
Customer::WEBSITE_ID,
|
||||
[
|
||||
AttributeMetadata::FRONTEND_INPUT => 'select',
|
||||
AttributeMetadata::INPUT_FILTER => '',
|
||||
AttributeMetadata::STORE_LABEL => 'Associate to Website',
|
||||
AttributeMetadata::MULTILINE_COUNT => 0,
|
||||
AttributeMetadata::VALIDATION_RULES => [],
|
||||
AttributeMetadata::VISIBLE => true,
|
||||
AttributeMetadata::REQUIRED => true,
|
||||
AttributeMetadata::DATA_MODEL => '',
|
||||
AttributeMetadata::OPTIONS => [
|
||||
['label' => 'Main Website', 'value' => '1'],
|
||||
],
|
||||
AttributeMetadata::FRONTEND_CLASS => 'required-entry',
|
||||
AttributeMetadata::USER_DEFINED => false,
|
||||
AttributeMetadata::SORT_ORDER => 10,
|
||||
AttributeMetadata::FRONTEND_LABEL => 'Associate to Website',
|
||||
AttributeMetadata::NOTE => '',
|
||||
AttributeMetadata::SYSTEM => true,
|
||||
AttributeMetadata::BACKEND_TYPE => 'static',
|
||||
AttributeMetadata::IS_USED_IN_GRID => true,
|
||||
AttributeMetadata::IS_VISIBLE_IN_GRID => true,
|
||||
AttributeMetadata::IS_FILTERABLE_IN_GRID => true,
|
||||
AttributeMetadata::IS_SEARCHABLE_IN_GRID => false,
|
||||
AttributeMetadata::ATTRIBUTE_CODE => 'website_id',
|
||||
],
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieval of all customer attribute metadata.
|
||||
*/
|
||||
public function testGetAllAttributesMetadata()
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'GetAllAttributesMetadata',
|
||||
],
|
||||
];
|
||||
|
||||
$attributeMetadata = $this->_webApiCall($serviceInfo);
|
||||
|
||||
$firstName = $this->getAttributeMetadataDataProvider()[Customer::FIRSTNAME][1];
|
||||
$validationResult = $this->checkMultipleAttributesValidationRules($firstName, $attributeMetadata);
|
||||
list($firstName, $attributeMetadata) = $validationResult;
|
||||
$this->assertContainsEquals($firstName, $attributeMetadata);
|
||||
|
||||
$websiteId = $this->getAttributeMetadataDataProvider()[Customer::WEBSITE_ID][1];
|
||||
$validationResult = $this->checkMultipleAttributesValidationRules($websiteId, $attributeMetadata);
|
||||
list($websiteId, $attributeMetadata) = $validationResult;
|
||||
$this->assertContainsEquals($websiteId, $attributeMetadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieval of custom customer attribute metadata.
|
||||
*/
|
||||
public function testGetCustomAttributesMetadata()
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . '/custom',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'GetCustomAttributesMetadata',
|
||||
],
|
||||
];
|
||||
|
||||
$attributeMetadata = $this->_webApiCall($serviceInfo);
|
||||
|
||||
$this->assertCount(count($this->customerMetadata->getCustomAttributesMetadata()), $attributeMetadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieval of attributes
|
||||
*
|
||||
* @param string $formCode Form code
|
||||
* @param array $expectedMetadata The expected attribute metadata
|
||||
* @dataProvider getAttributesDataProvider
|
||||
*/
|
||||
public function testGetAttributes($formCode, $expectedMetadata)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . "/form/$formCode",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'GetAttributes',
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
'formCode' => $formCode,
|
||||
];
|
||||
|
||||
$attributeMetadataList = $this->_webApiCall($serviceInfo, $requestData);
|
||||
foreach ($attributeMetadataList as $attributeMetadata) {
|
||||
if (isset($attributeMetadata['attribute_code'])
|
||||
&& $attributeMetadata['attribute_code'] == $expectedMetadata['attribute_code']) {
|
||||
$validationResult = $this->checkValidationRules($expectedMetadata, $attributeMetadata);
|
||||
list($expectedMetadata, $attributeMetadata) = $validationResult;
|
||||
$this->assertEquals($expectedMetadata, $attributeMetadata);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testGetAttributes.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAttributesDataProvider()
|
||||
{
|
||||
$attributeMetadata = $this->getAttributeMetadataDataProvider();
|
||||
return [
|
||||
[
|
||||
'adminhtml_customer',
|
||||
$attributeMetadata[Customer::FIRSTNAME][1],
|
||||
],
|
||||
[
|
||||
'adminhtml_customer',
|
||||
$attributeMetadata[Customer::GENDER][1]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
// phpcs:disable Generic.Metrics.NestingLevel
|
||||
/**
|
||||
* Checks that expected and actual attribute metadata validation rules are equal
|
||||
* and removes the validation rules entry from expected and actual attribute metadata
|
||||
*
|
||||
* @param array $expectedResult
|
||||
* @param array $actualResult
|
||||
* @return array
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function checkValidationRules($expectedResult, $actualResult)
|
||||
{
|
||||
$expectedRules = [];
|
||||
$actualRules = [];
|
||||
|
||||
if (isset($expectedResult[AttributeMetadata::VALIDATION_RULES])) {
|
||||
$expectedRules = $expectedResult[AttributeMetadata::VALIDATION_RULES];
|
||||
unset($expectedResult[AttributeMetadata::VALIDATION_RULES]);
|
||||
}
|
||||
if (isset($actualResult[AttributeMetadata::VALIDATION_RULES])) {
|
||||
$actualRules = $actualResult[AttributeMetadata::VALIDATION_RULES];
|
||||
unset($actualResult[AttributeMetadata::VALIDATION_RULES]);
|
||||
}
|
||||
|
||||
if (is_array($expectedRules) && is_array($actualRules)) {
|
||||
foreach ($expectedRules as $expectedRule) {
|
||||
if (isset($expectedRule['name']) && isset($expectedRule['value'])) {
|
||||
$found = false;
|
||||
foreach ($actualRules as $actualRule) {
|
||||
if (isset($actualRule['name']) && isset($actualRule['value'])) {
|
||||
if ($expectedRule['name'] == $actualRule['name']
|
||||
&& $expectedRule['value'] == $actualRule['value']
|
||||
) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->assertTrue($found);
|
||||
}
|
||||
}
|
||||
}
|
||||
return [$expectedResult, $actualResult];
|
||||
}
|
||||
// phpcs:enable
|
||||
|
||||
/**
|
||||
* Check specific attribute validation rules in set of multiple attributes
|
||||
*
|
||||
* @param array $expectedResult Set of expected attribute metadata
|
||||
* @param array $actualResultSet Set of actual attribute metadata
|
||||
* @return array
|
||||
*/
|
||||
public function checkMultipleAttributesValidationRules($expectedResult, $actualResultSet)
|
||||
{
|
||||
if (is_array($expectedResult) && is_array($actualResultSet)) {
|
||||
if (isset($expectedResult[AttributeMetadata::ATTRIBUTE_CODE])) {
|
||||
foreach ($actualResultSet as $actualAttributeKey => $actualAttribute) {
|
||||
if (isset($actualAttribute[AttributeMetadata::ATTRIBUTE_CODE])
|
||||
&& $expectedResult[AttributeMetadata::ATTRIBUTE_CODE]
|
||||
== $actualAttribute[AttributeMetadata::ATTRIBUTE_CODE]
|
||||
) {
|
||||
$this->checkValidationRules($expectedResult, $actualAttribute);
|
||||
unset($actualResultSet[$actualAttributeKey][AttributeMetadata::VALIDATION_RULES]);
|
||||
}
|
||||
}
|
||||
unset($expectedResult[AttributeMetadata::VALIDATION_RULES]);
|
||||
}
|
||||
}
|
||||
return [$expectedResult, $actualResultSet];
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
+213
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Customer\Api;
|
||||
|
||||
use Magento\Customer\Api\Data\CustomerInterface;
|
||||
use Magento\Customer\Model\CustomerRegistry;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Framework\Webapi\Rest\Request;
|
||||
use Magento\Integration\Api\CustomerTokenServiceInterface;
|
||||
use Magento\Integration\Model\Oauth\Token as TokenModel;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Helper\Customer as CustomerHelper;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoApiDataFixture Magento/Store/_files/second_website_with_two_stores.php
|
||||
*/
|
||||
class CustomerSharingOptionsTest extends WebapiAbstract
|
||||
{
|
||||
const RESOURCE_PATH = '/V1/customers/me';
|
||||
const REPO_SERVICE = 'customerCustomerRepositoryV1';
|
||||
const SERVICE_VERSION = 'V1';
|
||||
|
||||
/**
|
||||
* @var CustomerRepositoryInterface
|
||||
*/
|
||||
private $customerRepository;
|
||||
|
||||
/**
|
||||
* @var CustomerRegistry
|
||||
*/
|
||||
private $customerRegistry;
|
||||
|
||||
/**
|
||||
* @var CustomerHelper
|
||||
*/
|
||||
private $customerHelper;
|
||||
|
||||
/**
|
||||
* @var TokenModel
|
||||
*/
|
||||
private $token;
|
||||
|
||||
/**
|
||||
* @var CustomerInterface
|
||||
*/
|
||||
private $customerData;
|
||||
|
||||
/**
|
||||
* @var CustomerTokenServiceInterface
|
||||
*/
|
||||
private $tokenService;
|
||||
|
||||
/**
|
||||
* Execute per test initialization.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->customerRegistry = Bootstrap::getObjectManager()->get(
|
||||
\Magento\Customer\Model\CustomerRegistry::class
|
||||
);
|
||||
|
||||
$this->customerRepository = Bootstrap::getObjectManager()->get(
|
||||
CustomerRepositoryInterface::class,
|
||||
['customerRegistry' => $this->customerRegistry]
|
||||
);
|
||||
|
||||
$this->customerHelper = new CustomerHelper();
|
||||
$this->customerData = $this->customerHelper->createSampleCustomer();
|
||||
$this->tokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
|
||||
|
||||
// get token
|
||||
$this->resetTokenForCustomerSampleData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that fixture customer and his addresses are deleted.
|
||||
*/
|
||||
public function tearDown(): void
|
||||
{
|
||||
$this->customerRepository = null;
|
||||
|
||||
/** @var Registry $registry */
|
||||
$registry = Bootstrap::getObjectManager()->get(Registry::class);
|
||||
$registry->unregister('isSecureArea');
|
||||
$registry->register('isSecureArea', true);
|
||||
|
||||
$registry->unregister('isSecureArea');
|
||||
$registry->register('isSecureArea', false);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $storeCode
|
||||
* @param bool $expectingException
|
||||
* @dataProvider getCustomerDataWebsiteScopeDataProvider
|
||||
*
|
||||
* @magentoConfigFixture default_store customer/account_share/scope 1
|
||||
*/
|
||||
public function testGetCustomerDataWebsiteScope(string $storeCode, bool $expectingException)
|
||||
{
|
||||
$this->_markTestAsRestOnly('SOAP is difficult to generate exception messages, inconsistencies in WSDL');
|
||||
$this->processGetCustomerData($storeCode, $expectingException);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $storeCode
|
||||
* @param bool $expectingException
|
||||
* @dataProvider getCustomerDataGlobalScopeDataProvider
|
||||
*
|
||||
* @magentoConfigFixture customer/account_share/scope 0
|
||||
*/
|
||||
public function testGetCustomerDataGlobalScope(string $storeCode, bool $expectingException)
|
||||
{
|
||||
$this->processGetCustomerData($storeCode, $expectingException);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $storeCode
|
||||
* @param bool $expectingException
|
||||
*/
|
||||
private function processGetCustomerData(string $storeCode, bool $expectingException)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH,
|
||||
'httpMethod' => Request::HTTP_METHOD_GET,
|
||||
'token' => $this->token,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::REPO_SERVICE,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::REPO_SERVICE . 'GetSelf',
|
||||
'token' => $this->token
|
||||
]
|
||||
];
|
||||
$arguments = [];
|
||||
if (TESTS_WEB_API_ADAPTER === 'soap') {
|
||||
$arguments['customerId'] = 0;
|
||||
}
|
||||
|
||||
if ($expectingException) {
|
||||
self::expectException(\Exception::class);
|
||||
self::expectExceptionMessage("The consumer isn't authorized to access %resources.");
|
||||
}
|
||||
|
||||
$this->_webApiCall($serviceInfo, $arguments, null, $storeCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testGetCustomerDataWebsiteScope.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCustomerDataWebsiteScopeDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'Default Store View' => [
|
||||
'store_code' => 'default',
|
||||
'exception' => false
|
||||
],
|
||||
'Custom Store View' => [
|
||||
'store_code' => 'fixture_second_store',
|
||||
'exception' => true
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testGetCustomerDataGlobalScope.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCustomerDataGlobalScopeDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'Default Store View' => [
|
||||
'store_code' => 'default',
|
||||
'exception' => false
|
||||
],
|
||||
'Custom Store View' => [
|
||||
'store_code' => 'fixture_second_store',
|
||||
'exception' => false
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the test's access token for the created customer sample data
|
||||
*/
|
||||
private function resetTokenForCustomerSampleData()
|
||||
{
|
||||
$this->resetTokenForCustomer($this->customerData[CustomerInterface::EMAIL], 'test@123');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the test's access token for a particular username and password.
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
*/
|
||||
private function resetTokenForCustomer($username, $password)
|
||||
{
|
||||
$this->token = $this->tokenService->createCustomerAccessToken($username, $password);
|
||||
$this->customerRegistry->remove($this->customerRepository->get($username)->getId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Customer\Api;
|
||||
|
||||
use Magento\Customer\Model\Data\Group as CustomerGroup;
|
||||
use Magento\Customer\Model\GroupRegistry;
|
||||
use Magento\Customer\Model\ResourceModel\GroupRepository;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
/**
|
||||
* Customer Group Management API test
|
||||
*/
|
||||
class GroupManagementTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_NAME = "customerGroupManagementV1";
|
||||
const SERVICE_VERSION = "V1";
|
||||
const RESOURCE_PATH = "/V1/customerGroups";
|
||||
|
||||
/**
|
||||
* @var GroupRegistry
|
||||
*/
|
||||
private $groupRegistry;
|
||||
|
||||
/**
|
||||
* @var GroupRepository
|
||||
*/
|
||||
private $groupRepository;
|
||||
|
||||
/**
|
||||
* Execute per test initialization.
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$this->groupRegistry = $objectManager->get(\Magento\Customer\Model\GroupRegistry::class);
|
||||
$this->groupRepository = $objectManager->get(\Magento\Customer\Model\ResourceModel\GroupRepository::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the retrieval of the default group for storeId equal to 1.
|
||||
*
|
||||
* @param int $storeId The store Id
|
||||
* @param array $defaultGroupData The default group data for the store with the specified Id.
|
||||
*
|
||||
* @dataProvider getDefaultGroupDataProvider
|
||||
*/
|
||||
public function testGetDefaultGroup($storeId, $defaultGroupData)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . "/default/$storeId",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => 'customerGroupManagementV1GetDefaultGroup',
|
||||
],
|
||||
];
|
||||
$requestData = ['storeId' => $storeId];
|
||||
$groupData = $this->_webApiCall($serviceInfo, $requestData);
|
||||
|
||||
$this->assertEquals($defaultGroupData, $groupData, "The default group does not match.");
|
||||
}
|
||||
|
||||
/**
|
||||
* The testGetDefaultGroup data provider.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDefaultGroupDataProvider()
|
||||
{
|
||||
return [
|
||||
'admin' => [
|
||||
0,
|
||||
[
|
||||
CustomerGroup::ID => 1,
|
||||
CustomerGroup::CODE => 'General',
|
||||
CustomerGroup::TAX_CLASS_ID => 3,
|
||||
CustomerGroup::TAX_CLASS_NAME => 'Retail Customer'
|
||||
],
|
||||
],
|
||||
'base' => [
|
||||
1,
|
||||
[
|
||||
CustomerGroup::ID => 1,
|
||||
CustomerGroup::CODE => 'General',
|
||||
CustomerGroup::TAX_CLASS_ID => 3,
|
||||
CustomerGroup::TAX_CLASS_NAME => 'Retail Customer'
|
||||
],
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the retrieval of a non-existent storeId will return an expected fault.
|
||||
*/
|
||||
public function testGetDefaultGroupNonExistentStore()
|
||||
{
|
||||
/* Store id should not exist */
|
||||
$nonExistentStoreId = 9876;
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . "/default/$nonExistentStoreId",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => 'customerGroupManagementV1GetDefaultGroup',
|
||||
],
|
||||
];
|
||||
$requestData = ['storeId' => $nonExistentStoreId];
|
||||
$expectedMessage = 'No such entity with %fieldName = %fieldValue';
|
||||
|
||||
try {
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->fail("Expected exception");
|
||||
} catch (\SoapFault $e) {
|
||||
$this->assertStringContainsString(
|
||||
$expectedMessage,
|
||||
$e->getMessage(),
|
||||
"SoapFault does not contain expected message."
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$this->assertStringContainsString(
|
||||
$expectedMessage,
|
||||
$e->getMessage(),
|
||||
"Exception does not contain expected message."
|
||||
);
|
||||
$this->assertStringContainsString((string)$nonExistentStoreId, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the group with the specified Id can or cannot be deleted.
|
||||
*
|
||||
* @param int $groupId The group Id
|
||||
* @param bool $isDeleteable Whether the group can or cannot be deleted.
|
||||
*
|
||||
* @dataProvider isReadonlyDataProvider
|
||||
*/
|
||||
public function testIsReadonly($groupId, $isDeleteable)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . "/$groupId/permissions",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => 'customerGroupManagementV1IsReadonly',
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = [CustomerGroup::ID => $groupId];
|
||||
|
||||
$isReadonly = $this->_webApiCall($serviceInfo, $requestData);
|
||||
|
||||
$failureMessage = $isDeleteable
|
||||
? 'The group should be deleteable.' : 'The group should not be deleteable.';
|
||||
$this->assertEquals($isDeleteable, !$isReadonly, $failureMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* The testIsReadonly data provider.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function isReadonlyDataProvider()
|
||||
{
|
||||
return [
|
||||
'NOT LOGGED IN' => [0, false],
|
||||
'General' => [1, false],
|
||||
'Wholesale' => [2, true],
|
||||
'Retailer' => [3, true]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the group with the specified Id can or cannot be deleted.
|
||||
*/
|
||||
public function testIsReadonlyNoSuchGroup()
|
||||
{
|
||||
/* This group ID should not exist in the store. */
|
||||
$groupId = 9999;
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . "/$groupId/permissions",
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => 'customerGroupManagementV1IsReadonly',
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = [CustomerGroup::ID => $groupId];
|
||||
|
||||
$expectedMessage = 'No such entity with %fieldName = %fieldValue';
|
||||
|
||||
try {
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->fail("Expected exception.");
|
||||
} catch (\SoapFault $e) {
|
||||
$this->assertStringContainsString(
|
||||
$expectedMessage,
|
||||
$e->getMessage(),
|
||||
"SoapFault does not contain expected message."
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$this->assertStringContainsString(
|
||||
$expectedMessage,
|
||||
$e->getMessage(),
|
||||
"Exception does not contain expected message."
|
||||
);
|
||||
$this->assertStringContainsString((string)$groupId, $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
After Width: | Height: | Size: 3.0 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
Reference in New Issue
Block a user