AccountManagementMeTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Api;
  7. use Magento\Customer\Api\Data\CustomerInterface;
  8. use Magento\Customer\Model\CustomerRegistry;
  9. use Magento\Integration\Model\Oauth\Token as TokenModel;
  10. use Magento\TestFramework\Helper\Bootstrap;
  11. use Magento\TestFramework\Helper\Customer as CustomerHelper;
  12. /**
  13. * Class AccountManagementMeTest
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  17. * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php
  18. */
  19. class AccountManagementMeTest extends \Magento\TestFramework\TestCase\WebapiAbstract
  20. {
  21. const RESOURCE_PATH = '/V1/customers/me';
  22. const RESOURCE_PATH_CUSTOMER_TOKEN = "/V1/integration/customer/token";
  23. /**
  24. * @var CustomerRepositoryInterface
  25. */
  26. private $customerRepository;
  27. /**
  28. * @var AccountManagementInterface
  29. */
  30. private $customerAccountManagement;
  31. /**
  32. * @var CustomerRegistry
  33. */
  34. private $customerRegistry;
  35. /**
  36. * @var CustomerHelper
  37. */
  38. private $customerHelper;
  39. /**
  40. * @var TokenModel
  41. */
  42. private $token;
  43. /**
  44. * @var CustomerInterface
  45. */
  46. private $customerData;
  47. /**
  48. * @var \Magento\Framework\Reflection\DataObjectProcessor
  49. */
  50. private $dataObjectProcessor;
  51. /**
  52. * Execute per test initialization.
  53. */
  54. public function setUp()
  55. {
  56. $this->_markTestAsRestOnly();
  57. $this->customerRegistry = Bootstrap::getObjectManager()->get(
  58. \Magento\Customer\Model\CustomerRegistry::class
  59. );
  60. $this->customerRepository = Bootstrap::getObjectManager()->get(
  61. \Magento\Customer\Api\CustomerRepositoryInterface::class,
  62. ['customerRegistry' => $this->customerRegistry]
  63. );
  64. $this->customerAccountManagement = Bootstrap::getObjectManager()
  65. ->get(\Magento\Customer\Api\AccountManagementInterface::class);
  66. $this->customerHelper = new CustomerHelper();
  67. $this->customerData = $this->customerHelper->createSampleCustomer();
  68. // get token
  69. $this->resetTokenForCustomerSampleData();
  70. $this->dataObjectProcessor = Bootstrap::getObjectManager()->create(
  71. \Magento\Framework\Reflection\DataObjectProcessor::class
  72. );
  73. }
  74. /**
  75. * Ensure that fixture customer and his addresses are deleted.
  76. */
  77. public function tearDown()
  78. {
  79. $this->customerRepository = null;
  80. /** @var \Magento\Framework\Registry $registry */
  81. $registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
  82. $registry->unregister('isSecureArea');
  83. $registry->register('isSecureArea', true);
  84. $registry->unregister('isSecureArea');
  85. $registry->register('isSecureArea', false);
  86. parent::tearDown();
  87. }
  88. public function testChangePassword()
  89. {
  90. $serviceInfo = [
  91. 'rest' => [
  92. 'resourcePath' => self::RESOURCE_PATH . '/password',
  93. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  94. 'token' => $this->token,
  95. ],
  96. ];
  97. $requestData = ['currentPassword' => 'test@123', 'newPassword' => '123@test'];
  98. $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
  99. $customerResponseData = $this->customerAccountManagement
  100. ->authenticate($this->customerData[CustomerInterface::EMAIL], '123@test');
  101. $this->assertEquals($this->customerData[CustomerInterface::ID], $customerResponseData->getId());
  102. }
  103. public function testUpdateCustomer()
  104. {
  105. $customerData = $this->_getCustomerData($this->customerData[CustomerInterface::ID]);
  106. $lastName = $customerData->getLastname();
  107. $updatedCustomerData = $this->dataObjectProcessor->buildOutputDataArray(
  108. $customerData,
  109. \Magento\Customer\Api\Data\CustomerInterface::class
  110. );
  111. $updatedCustomerData[CustomerInterface::LASTNAME] = $lastName . 'Updated';
  112. $updatedCustomerData[CustomerInterface::ID] = 25;
  113. $serviceInfo = [
  114. 'rest' => [
  115. 'resourcePath' => self::RESOURCE_PATH,
  116. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  117. 'token' => $this->token,
  118. ],
  119. ];
  120. $requestData = ['customer' => $updatedCustomerData];
  121. $response = $this->_webApiCall($serviceInfo, $requestData);
  122. $this->assertEquals($lastName . "Updated", $response[CustomerInterface::LASTNAME]);
  123. $customerData = $this->_getCustomerData($this->customerData[CustomerInterface::ID]);
  124. $this->assertEquals($lastName . "Updated", $customerData->getLastname());
  125. }
  126. public function testGetCustomerData()
  127. {
  128. //Get expected details from the Service directly
  129. $customerData = $this->_getCustomerData($this->customerData[CustomerInterface::ID]);
  130. $expectedCustomerDetails = $this->dataObjectProcessor->buildOutputDataArray(
  131. $customerData,
  132. \Magento\Customer\Api\Data\CustomerInterface::class
  133. );
  134. $expectedCustomerDetails['addresses'][0]['id'] =
  135. (int)$expectedCustomerDetails['addresses'][0]['id'];
  136. $expectedCustomerDetails['addresses'][1]['id'] =
  137. (int)$expectedCustomerDetails['addresses'][1]['id'];
  138. $serviceInfo = [
  139. 'rest' => [
  140. 'resourcePath' => self::RESOURCE_PATH,
  141. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  142. 'token' => $this->token,
  143. ],
  144. ];
  145. $customerDetailsResponse = $this->_webApiCall($serviceInfo);
  146. unset($expectedCustomerDetails['custom_attributes']);
  147. unset($customerDetailsResponse['custom_attributes']); //for REST
  148. $this->assertEquals($expectedCustomerDetails, $customerDetailsResponse);
  149. }
  150. public function testGetCustomerActivateCustomer()
  151. {
  152. $serviceInfo = [
  153. 'rest' => [
  154. 'resourcePath' => self::RESOURCE_PATH . '/activate',
  155. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  156. 'token' => $this->token,
  157. ],
  158. ];
  159. $requestData = ['confirmationKey' => $this->customerData[CustomerInterface::CONFIRMATION]];
  160. $customerResponseData = $this->_webApiCall($serviceInfo, $requestData);
  161. $this->assertEquals($this->customerData[CustomerInterface::ID], $customerResponseData[CustomerInterface::ID]);
  162. // Confirmation key is removed after confirmation
  163. $this->assertFalse(isset($customerResponseData[CustomerInterface::CONFIRMATION]));
  164. }
  165. /**
  166. * Return the customer details.
  167. *
  168. * @param int $customerId
  169. * @return \Magento\Customer\Api\Data\CustomerInterface
  170. */
  171. protected function _getCustomerData($customerId)
  172. {
  173. $data = $this->customerRepository->getById($customerId);
  174. $this->customerRegistry->remove($customerId);
  175. return $data;
  176. }
  177. public function testGetDefaultBillingAddress()
  178. {
  179. $this->resetTokenForCustomerFixture();
  180. $fixtureCustomerId = 1;
  181. $serviceInfo = [
  182. 'rest' => [
  183. 'resourcePath' => "/V1/customers/me/billingAddress",
  184. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  185. 'token' => $this->token,
  186. ],
  187. ];
  188. $requestData = ['customerId' => $fixtureCustomerId];
  189. $addressData = $this->_webApiCall($serviceInfo, $requestData);
  190. $this->assertEquals(
  191. $this->getFirstFixtureAddressData(),
  192. $addressData,
  193. "Default billing address data is invalid."
  194. );
  195. }
  196. public function testGetDefaultShippingAddress()
  197. {
  198. $this->resetTokenForCustomerFixture();
  199. $fixtureCustomerId = 1;
  200. $serviceInfo = [
  201. 'rest' => [
  202. 'resourcePath' => "/V1/customers/me/shippingAddress",
  203. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  204. 'token' => $this->token,
  205. ],
  206. ];
  207. $requestData = ['customerId' => $fixtureCustomerId];
  208. $addressData = $this->_webApiCall($serviceInfo, $requestData);
  209. $this->assertEquals(
  210. $this->getFirstFixtureAddressData(),
  211. $addressData,
  212. "Default shipping address data is invalid."
  213. );
  214. }
  215. /**
  216. * Retrieve data of the first fixture address.
  217. *
  218. * @return array
  219. */
  220. protected function getFirstFixtureAddressData()
  221. {
  222. return [
  223. 'firstname' => 'John',
  224. 'lastname' => 'Smith',
  225. 'city' => 'CityM',
  226. 'country_id' => 'US',
  227. 'company' => 'CompanyName',
  228. 'postcode' => '75477',
  229. 'telephone' => '3468676',
  230. 'street' => ['Green str, 67'],
  231. 'id' => 1,
  232. 'default_billing' => true,
  233. 'default_shipping' => true,
  234. 'customer_id' => '1',
  235. 'region' => ['region' => 'Alabama', 'region_id' => 1, 'region_code' => 'AL'],
  236. 'region_id' => 1,
  237. ];
  238. }
  239. /**
  240. * Retrieve data of the second fixture address.
  241. *
  242. * @return array
  243. */
  244. protected function getSecondFixtureAddressData()
  245. {
  246. return [
  247. 'firstname' => 'John',
  248. 'lastname' => 'Smith',
  249. 'city' => 'CityX',
  250. 'country_id' => 'US',
  251. 'postcode' => '47676',
  252. 'telephone' => '3234676',
  253. 'street' => ['Black str, 48'],
  254. 'id' => 2,
  255. 'default_billing' => false,
  256. 'default_shipping' => false,
  257. 'customer_id' => '1',
  258. 'region' => ['region' => 'Alabama', 'region_id' => 1, 'region_code' => 'AL'],
  259. 'region_id' => 1,
  260. ];
  261. }
  262. /**
  263. * Sets the test's access token for the customer fixture
  264. */
  265. protected function resetTokenForCustomerFixture()
  266. {
  267. $this->resetTokenForCustomer('customer@example.com', 'password');
  268. }
  269. /**
  270. * Sets the test's access token for the created customer sample data
  271. */
  272. protected function resetTokenForCustomerSampleData()
  273. {
  274. $this->resetTokenForCustomer($this->customerData[CustomerInterface::EMAIL], 'test@123');
  275. }
  276. /**
  277. * Sets the test's access token for a particular username and password.
  278. *
  279. * @param string $username
  280. * @param string $password
  281. */
  282. protected function resetTokenForCustomer($username, $password)
  283. {
  284. // get customer ID token
  285. $serviceInfo = [
  286. 'rest' => [
  287. 'resourcePath' => self::RESOURCE_PATH_CUSTOMER_TOKEN,
  288. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  289. ],
  290. ];
  291. $requestData = ['username' => $username, 'password' => $password];
  292. $this->token = $this->_webApiCall($serviceInfo, $requestData);
  293. }
  294. }