Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AccountManagementMeTest.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Customer\Api;
8 
13 use Magento\TestFramework\Helper\Customer as CustomerHelper;
14 
23 {
24  const RESOURCE_PATH = '/V1/customers/me';
25  const RESOURCE_PATH_CUSTOMER_TOKEN = "/V1/integration/customer/token";
26 
30  private $customerRepository;
31 
35  private $customerAccountManagement;
36 
40  private $customerRegistry;
41 
45  private $customerHelper;
46 
50  private $token;
51 
55  private $customerData;
56 
60  private $dataObjectProcessor;
61 
65  public function setUp()
66  {
67  $this->_markTestAsRestOnly();
68 
69  $this->customerRegistry = Bootstrap::getObjectManager()->get(
70  \Magento\Customer\Model\CustomerRegistry::class
71  );
72 
73  $this->customerRepository = Bootstrap::getObjectManager()->get(
74  \Magento\Customer\Api\CustomerRepositoryInterface::class,
75  ['customerRegistry' => $this->customerRegistry]
76  );
77 
78  $this->customerAccountManagement = Bootstrap::getObjectManager()
79  ->get(\Magento\Customer\Api\AccountManagementInterface::class);
80 
81  $this->customerHelper = new CustomerHelper();
82  $this->customerData = $this->customerHelper->createSampleCustomer();
83 
84  // get token
86 
87  $this->dataObjectProcessor = Bootstrap::getObjectManager()->create(
88  \Magento\Framework\Reflection\DataObjectProcessor::class
89  );
90  }
91 
95  public function tearDown()
96  {
97  $this->customerRepository = null;
98 
100  $registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
101  $registry->unregister('isSecureArea');
102  $registry->register('isSecureArea', true);
103 
104  $registry->unregister('isSecureArea');
105  $registry->register('isSecureArea', false);
106  parent::tearDown();
107  }
108 
109  public function testChangePassword()
110  {
111  $serviceInfo = [
112  'rest' => [
113  'resourcePath' => self::RESOURCE_PATH . '/password',
115  'token' => $this->token,
116  ],
117  ];
118  $requestData = ['currentPassword' => 'test@123', 'newPassword' => '123@test'];
119  $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
120 
121  $customerResponseData = $this->customerAccountManagement
122  ->authenticate($this->customerData[CustomerInterface::EMAIL], '123@test');
123  $this->assertEquals($this->customerData[CustomerInterface::ID], $customerResponseData->getId());
124  }
125 
126  public function testUpdateCustomer()
127  {
128  $customerData = $this->_getCustomerData($this->customerData[CustomerInterface::ID]);
129  $lastName = $customerData->getLastname();
130 
131  $updatedCustomerData = $this->dataObjectProcessor->buildOutputDataArray(
132  $customerData,
133  \Magento\Customer\Api\Data\CustomerInterface::class
134  );
135  $updatedCustomerData[CustomerInterface::LASTNAME] = $lastName . 'Updated';
136  $updatedCustomerData[CustomerInterface::ID] = 25;
137 
138  $serviceInfo = [
139  'rest' => [
140  'resourcePath' => self::RESOURCE_PATH,
142  'token' => $this->token,
143  ],
144  ];
145  $requestData = ['customer' => $updatedCustomerData];
146 
147  $response = $this->_webApiCall($serviceInfo, $requestData);
148  $this->assertEquals($lastName . "Updated", $response[CustomerInterface::LASTNAME]);
149 
150  $customerData = $this->_getCustomerData($this->customerData[CustomerInterface::ID]);
151  $this->assertEquals($lastName . "Updated", $customerData->getLastname());
152  }
153 
154  public function testGetCustomerData()
155  {
156  //Get expected details from the Service directly
157  $customerData = $this->_getCustomerData($this->customerData[CustomerInterface::ID]);
158  $expectedCustomerDetails = $this->dataObjectProcessor->buildOutputDataArray(
159  $customerData,
160  \Magento\Customer\Api\Data\CustomerInterface::class
161  );
162  $expectedCustomerDetails['addresses'][0]['id'] =
163  (int)$expectedCustomerDetails['addresses'][0]['id'];
164 
165  $expectedCustomerDetails['addresses'][1]['id'] =
166  (int)$expectedCustomerDetails['addresses'][1]['id'];
167 
168  $serviceInfo = [
169  'rest' => [
170  'resourcePath' => self::RESOURCE_PATH,
172  'token' => $this->token,
173  ],
174  ];
175  $customerDetailsResponse = $this->_webApiCall($serviceInfo);
176 
177  unset($expectedCustomerDetails['custom_attributes']);
178  unset($customerDetailsResponse['custom_attributes']); //for REST
179 
180  $this->assertEquals($expectedCustomerDetails, $customerDetailsResponse);
181  }
182 
184  {
185  $serviceInfo = [
186  'rest' => [
187  'resourcePath' => self::RESOURCE_PATH . '/activate',
189  'token' => $this->token,
190  ],
191  ];
192  $requestData = ['confirmationKey' => $this->customerData[CustomerInterface::CONFIRMATION]];
193  $customerResponseData = $this->_webApiCall($serviceInfo, $requestData);
194  $this->assertEquals($this->customerData[CustomerInterface::ID], $customerResponseData[CustomerInterface::ID]);
195  // Confirmation key is removed after confirmation
196  $this->assertFalse(isset($customerResponseData[CustomerInterface::CONFIRMATION]));
197  }
198 
205  protected function _getCustomerData($customerId)
206  {
207  $data = $this->customerRepository->getById($customerId);
208  $this->customerRegistry->remove($customerId);
209  return $data;
210  }
211 
213  {
215 
216  $fixtureCustomerId = 1;
217  $serviceInfo = [
218  'rest' => [
219  'resourcePath' => "/V1/customers/me/billingAddress",
221  'token' => $this->token,
222  ],
223  ];
224  $requestData = ['customerId' => $fixtureCustomerId];
225  $addressData = $this->_webApiCall($serviceInfo, $requestData);
226  $this->assertEquals(
228  $addressData,
229  "Default billing address data is invalid."
230  );
231  }
232 
234  {
236 
237  $fixtureCustomerId = 1;
238  $serviceInfo = [
239  'rest' => [
240  'resourcePath' => "/V1/customers/me/shippingAddress",
242  'token' => $this->token,
243  ],
244  ];
245  $requestData = ['customerId' => $fixtureCustomerId];
246  $addressData = $this->_webApiCall($serviceInfo, $requestData);
247  $this->assertEquals(
249  $addressData,
250  "Default shipping address data is invalid."
251  );
252  }
253 
259  protected function getFirstFixtureAddressData()
260  {
261  return [
262  'firstname' => 'John',
263  'lastname' => 'Smith',
264  'city' => 'CityM',
265  'country_id' => 'US',
266  'company' => 'CompanyName',
267  'postcode' => '75477',
268  'telephone' => '3468676',
269  'street' => ['Green str, 67'],
270  'id' => 1,
271  'default_billing' => true,
272  'default_shipping' => true,
273  'customer_id' => '1',
274  'region' => ['region' => 'Alabama', 'region_id' => 1, 'region_code' => 'AL'],
275  'region_id' => 1,
276  ];
277  }
278 
284  protected function getSecondFixtureAddressData()
285  {
286  return [
287  'firstname' => 'John',
288  'lastname' => 'Smith',
289  'city' => 'CityX',
290  'country_id' => 'US',
291  'postcode' => '47676',
292  'telephone' => '3234676',
293  'street' => ['Black str, 48'],
294  'id' => 2,
295  'default_billing' => false,
296  'default_shipping' => false,
297  'customer_id' => '1',
298  'region' => ['region' => 'Alabama', 'region_id' => 1, 'region_code' => 'AL'],
299  'region_id' => 1,
300  ];
301  }
302 
306  protected function resetTokenForCustomerFixture()
307  {
308  $this->resetTokenForCustomer('[email protected]', 'password');
309  }
310 
314  protected function resetTokenForCustomerSampleData()
315  {
316  $this->resetTokenForCustomer($this->customerData[CustomerInterface::EMAIL], 'test@123');
317  }
318 
325  protected function resetTokenForCustomer($username, $password)
326  {
327  // get customer ID token
328  $serviceInfo = [
329  'rest' => [
330  'resourcePath' => self::RESOURCE_PATH_CUSTOMER_TOKEN,
332  ],
333  ];
334  $requestData = ['username' => $username, 'password' => $password];
335  $this->token = $this->_webApiCall($serviceInfo, $requestData);
336  }
337 }
$response
Definition: 404.php:11
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$addressData
Definition: order.php:19