123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- <?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\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";
- /**
- * @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;
- /**
- * Execute per test initialization.
- */
- public function setUp()
- {
- $this->_markTestAsRestOnly();
- $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();
- // get token
- $this->resetTokenForCustomerSampleData();
- $this->dataObjectProcessor = Bootstrap::getObjectManager()->create(
- \Magento\Framework\Reflection\DataObjectProcessor::class
- );
- }
- /**
- * Ensure that fixture customer and his addresses are deleted.
- */
- public function tearDown()
- {
- $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,
- ],
- ];
- $requestData = ['currentPassword' => 'test@123', 'newPassword' => '123@test'];
- $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,
- ],
- ];
- $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,
- ],
- ];
- $customerDetailsResponse = $this->_webApiCall($serviceInfo);
- 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,
- ],
- ];
- $requestData = ['confirmationKey' => $this->customerData[CustomerInterface::CONFIRMATION]];
- $customerResponseData = $this->_webApiCall($serviceInfo, $requestData);
- $this->assertEquals($this->customerData[CustomerInterface::ID], $customerResponseData[CustomerInterface::ID]);
- // Confirmation key is removed after confirmation
- $this->assertFalse(isset($customerResponseData[CustomerInterface::CONFIRMATION]));
- }
- /**
- * 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,
- ],
- ];
- $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,
- ],
- ];
- $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)
- {
- // get customer ID token
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH_CUSTOMER_TOKEN,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
- ],
- ];
- $requestData = ['username' => $username, 'password' => $password];
- $this->token = $this->_webApiCall($serviceInfo, $requestData);
- }
- }
|