You are here

public function StockTest::testStockChangeAfterEditingOrder in Ubercart 8.4

Tests stock increment/decrement when admin edits order.

File

uc_stock/tests/src/Functional/StockTest.php, line 145

Class

StockTest
Tests the stock control functionality.

Namespace

Drupal\Tests\uc_stock\Functional

Code

public function testStockChangeAfterEditingOrder() {

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();

  // Set stock level.
  $prefix = 'stock[' . $this->product->model->value . ']';
  $stock = rand(100, 1000);
  $edit = [
    $prefix . '[active]' => 1,
    $prefix . '[stock]' => $stock,
  ];
  $this
    ->drupalGet('node/' . $this->product
    ->id() . '/edit/stock');
  $this
    ->submitForm($edit, 'Save changes');

  // Enable product quantity field.
  $edit = [
    'uc_product_add_to_cart_qty' => TRUE,
  ];
  $this
    ->drupalGet('admin/store/config/products');
  $this
    ->submitForm($edit, 'Save configuration');

  // Add the product to the cart and place an order.
  $qty = rand(1, 50);
  $edit = [
    'qty' => $qty,
  ];
  $this
    ->addToCart($this->product, $edit);
  $order = $this
    ->checkout();

  // Go to the order page.
  $this
    ->drupalGet('admin/store/orders/' . $order
    ->id() . '/edit');

  // Get the first OrderProduct entity's id.
  $order_products = $order->products;
  reset($order_products);
  $order_product_id = key($order_products);

  // Verify that product's quantity is found.
  $assert
    ->fieldValueEquals('products[' . $order_product_id . '][qty]', (string) $qty);

  // Increase the quantity of the product on order edit form.
  $increased_qty = $qty + rand(1, 50);
  $edit = [
    'products[' . $order_product_id . '][qty]' => $increased_qty,
  ];
  $this
    ->drupalGet('admin/store/orders/' . $order
    ->id() . '/edit');
  $this
    ->submitForm($edit, 'Save changes');

  // Verify the product's quantity is updated.
  $assert
    ->fieldValueEquals('products[' . $order_product_id . '][qty]', (string) $increased_qty);
  $this
    ->assertEquals($stock - $increased_qty, uc_stock_level($this->product->model->value));
}