You are here

public function CartUpdateItemsResourceTest::testInvalidPayload in Commerce Cart API 8

Tests malformed payloads.

File

tests/src/Functional/CartUpdateItemsResourceTest.php, line 51

Class

CartUpdateItemsResourceTest
Tests the cart update items resource.

Namespace

Drupal\Tests\commerce_cart_api\Functional

Code

public function testInvalidPayload() {
  $request_options = $this
    ->getAuthenticationRequestOptions('PATCH');
  $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType;
  $cart = $this->cartProvider
    ->createCart('default', $this->store, $this->account);
  $url = Url::fromUri('base:cart/' . $cart
    ->id() . '/items');
  $url
    ->setOption('query', [
    '_format' => static::$format,
  ]);
  $request_options[RequestOptions::BODY] = '{"1":{"quantity":"1"}}';
  $response = $this
    ->request('PATCH', $url, $request_options);
  $this
    ->assertResourceErrorResponse(422, 'Unable to find order item 1', $response);

  // Create an item in another cart.
  $another_cart = $this->cartProvider
    ->createCart('default', $this->store);
  $this->cartManager
    ->addEntity($another_cart, $this->variation, 2);
  $request_options[RequestOptions::BODY] = '{"1":{"quantity":"1"}}';
  $response = $this
    ->request('PATCH', $url, $request_options);
  $this
    ->assertResourceErrorResponse(422, 'Invalid order item', $response);

  // Give the original cart a valid order item.
  $this->cartManager
    ->addEntity($cart, $this->variation, 2);
  $request_options[RequestOptions::BODY] = '{"2":{"quantity":"1", "another_field":"1"}}';
  $response = $this
    ->request('PATCH', $url, $request_options);
  $this
    ->assertResourceErrorResponse(422, 'You only have access to update the quantity', $response);
  $request_options[RequestOptions::BODY] = '{"2":{"not_quantity":"1"}}';
  $response = $this
    ->request('PATCH', $url, $request_options);
  $this
    ->assertResourceErrorResponse(422, 'You only have access to update the quantity', $response);
  $request_options[RequestOptions::BODY] = '{"2":{"quantity":"-1"}}';
  $response = $this
    ->request('PATCH', $url, $request_options);
  $this
    ->assertResourceErrorResponse(422, 'Quantity must be positive value', $response);
}