@@ -77,6 +77,12 @@ public function testAssignCustomer()
77
77
//Use masked cart Id
78
78
$ cartId = $ quoteIdMask ->getMaskedId ();
79
79
80
+ // get customer ID token
81
+ /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
82
+ $ customerTokenService = $ this ->objectManager ->create (
83
+ 'Magento\Integration\Api\CustomerTokenServiceInterface '
84
+ );
85
+ $ token = $ customerTokenService ->createCustomerAccessToken ('customer@example.com ' , 'password ' );
80
86
/** @var $repository \Magento\Customer\Api\CustomerRepositoryInterface */
81
87
$ repository = $ this ->objectManager ->create ('Magento\Customer\Api\CustomerRepositoryInterface ' );
82
88
/** @var $customer \Magento\Customer\Api\Data\CustomerInterface */
@@ -87,11 +93,13 @@ public function testAssignCustomer()
87
93
'rest ' => [
88
94
'resourcePath ' => '/V1/guest-carts/ ' . $ cartId ,
89
95
'httpMethod ' => \Magento \Framework \Webapi \Rest \Request::HTTP_METHOD_PUT ,
96
+ 'token ' => $ token
90
97
],
91
98
'soap ' => [
92
99
'service ' => self ::SERVICE_NAME ,
93
100
'serviceVersion ' => 'V1 ' ,
94
101
'operation ' => self ::SERVICE_NAME . 'AssignCustomer ' ,
102
+ 'token ' => $ token
95
103
],
96
104
];
97
105
@@ -193,60 +201,24 @@ public function testAssignCustomerThrowsExceptionIfTargetCartIsNotAnonymous()
193
201
//Use masked cart Id
194
202
$ cartId = $ quoteIdMask ->getMaskedId ();
195
203
204
+ // get customer ID token
205
+ /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
206
+ $ customerTokenService = $ this ->objectManager ->create (
207
+ 'Magento\Integration\Api\CustomerTokenServiceInterface '
208
+ );
209
+ $ token = $ customerTokenService ->createCustomerAccessToken ('customer@example.com ' , 'password ' );
210
+
196
211
$ serviceInfo = [
197
212
'rest ' => [
198
213
'httpMethod ' => \Magento \Framework \Webapi \Rest \Request::HTTP_METHOD_PUT ,
199
214
'resourcePath ' => '/V1/guest-carts/ ' . $ cartId ,
215
+ 'token ' => $ token
200
216
],
201
217
'soap ' => [
202
218
'service ' => self ::SERVICE_NAME ,
203
219
'serviceVersion ' => 'V1 ' ,
204
220
'operation ' => self ::SERVICE_NAME . 'AssignCustomer ' ,
205
- ],
206
- ];
207
-
208
- $ requestData = [
209
- 'cartId ' => $ cartId ,
210
- 'customerId ' => $ customerId ,
211
- 'storeId ' => 1 ,
212
- ];
213
- $ this ->_webApiCall ($ serviceInfo , $ requestData );
214
- }
215
-
216
- /**
217
- * @magentoApiDataFixture Magento/Sales/_files/quote.php
218
- * @magentoApiDataFixture Magento/Customer/_files/customer_non_default_website_id.php
219
- * @expectedException \Exception
220
- * @expectedExceptionMessage Cannot assign customer to the given cart. The cart belongs to different store.
221
- */
222
- public function testAssignCustomerThrowsExceptionIfCartIsAssignedToDifferentStore ()
223
- {
224
- $ repository = $ this ->objectManager ->create ('Magento\Customer\Api\CustomerRepositoryInterface ' );
225
- /** @var $customer \Magento\Customer\Api\Data\CustomerInterface */
226
- $ customer = $ repository ->getById (1 );
227
- /** @var $quote \Magento\Quote\Model\Quote */
228
- $ quote = $ this ->objectManager ->create ('Magento\Quote\Model\Quote ' )->load ('test01 ' , 'reserved_order_id ' );
229
-
230
- $ customerId = $ customer ->getId ();
231
- $ cartId = $ quote ->getId ();
232
-
233
- /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
234
- $ quoteIdMask = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
235
- ->create ('Magento\Quote\Model\QuoteIdMaskFactory ' )
236
- ->create ();
237
- $ quoteIdMask ->load ($ cartId , 'quote_id ' );
238
- //Use masked cart Id
239
- $ cartId = $ quoteIdMask ->getMaskedId ();
240
-
241
- $ serviceInfo = [
242
- 'soap ' => [
243
- 'service ' => self ::SERVICE_NAME ,
244
- 'serviceVersion ' => 'V1 ' ,
245
- 'operation ' => self ::SERVICE_NAME . 'AssignCustomer ' ,
246
- ],
247
- 'rest ' => [
248
- 'httpMethod ' => \Magento \Framework \Webapi \Rest \Request::HTTP_METHOD_PUT ,
249
- 'resourcePath ' => '/V1/guest-carts/ ' . $ cartId ,
221
+ 'token ' => $ token
250
222
],
251
223
];
252
224
@@ -288,15 +260,23 @@ public function testAssignCustomerThrowsExceptionIfCustomerAlreadyHasActiveCart(
288
260
289
261
$ customerId = $ customer ->getId ();
290
262
263
+ // get customer ID token
264
+ /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
265
+ $ customerTokenService = $ this ->objectManager ->create (
266
+ 'Magento\Integration\Api\CustomerTokenServiceInterface '
267
+ );
268
+ $ token = $ customerTokenService ->createCustomerAccessToken ('customer@example.com ' , 'password ' );
291
269
$ serviceInfo = [
292
270
'soap ' => [
293
271
'service ' => self ::SERVICE_NAME ,
294
272
'operation ' => self ::SERVICE_NAME . 'AssignCustomer ' ,
295
273
'serviceVersion ' => 'V1 ' ,
274
+ 'token ' => $ token
296
275
],
297
276
'rest ' => [
298
277
'resourcePath ' => '/V1/guest-carts/ ' . $ cartId ,
299
278
'httpMethod ' => \Magento \Framework \Webapi \Rest \Request::HTTP_METHOD_PUT ,
279
+ 'token ' => $ token
300
280
],
301
281
];
302
282
@@ -344,4 +324,50 @@ public function testPlaceOrder()
344
324
$ this ->assertCount (1 , $ items );
345
325
$ this ->assertEquals ('Simple Product ' , $ items [0 ]->getName ());
346
326
}
327
+
328
+ /**
329
+ * @magentoApiDataFixture Magento/Sales/_files/quote.php
330
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
331
+ * @expectedException \Exception
332
+ * @expectedExceptionMessage Cannot assign customer to the given cart. You don't have permission for this operation.
333
+ */
334
+ public function testAssignCustomerByGuestUser ()
335
+ {
336
+ /** @var $quote \Magento\Quote\Model\Quote */
337
+ $ quote = $ this ->objectManager ->create ('Magento\Quote\Model\Quote ' )->load ('test01 ' , 'reserved_order_id ' );
338
+ $ cartId = $ quote ->getId ();
339
+ /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
340
+ $ quoteIdMaskFactory = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
341
+ ->create ('Magento\Quote\Model\QuoteIdMaskFactory ' );
342
+ $ quoteIdMask = $ quoteIdMaskFactory ->create ();
343
+ $ quoteIdMask ->load ($ cartId , 'quote_id ' );
344
+ //Use masked cart Id
345
+ $ cartId = $ quoteIdMask ->getMaskedId ();
346
+ $ repository = $ this ->objectManager ->create ('Magento\Customer\Api\CustomerRepositoryInterface ' );
347
+ /** @var $customer \Magento\Customer\Api\Data\CustomerInterface */
348
+ $ customer = $ repository ->getById (1 );
349
+ $ customerId = $ customer ->getId ();
350
+
351
+ $ serviceInfo = [
352
+ 'rest ' => [
353
+ 'resourcePath ' => '/V1/guest-carts/ ' . $ cartId ,
354
+ 'httpMethod ' => \Magento \Framework \Webapi \Rest \Request::HTTP_METHOD_PUT ,
355
+ ],
356
+ 'soap ' => [
357
+ 'service ' => self ::SERVICE_NAME ,
358
+ 'serviceVersion ' => 'V1 ' ,
359
+ 'operation ' => self ::SERVICE_NAME . 'AssignCustomer ' ,
360
+ ],
361
+ ];
362
+
363
+ $ requestData = [
364
+ 'cartId ' => $ cartId ,
365
+ 'customerId ' => $ customerId ,
366
+ 'storeId ' => 1 ,
367
+ ];
368
+ // Cart must be anonymous (see fixture)
369
+ $ this ->assertEmpty ($ quote ->getCustomerId ());
370
+
371
+ $ this ->_webApiCall ($ serviceInfo , $ requestData );
372
+ }
347
373
}
0 commit comments