You are here

public function ProductAdminTest::testEditProduct in Commerce Stock 8

Tests editing a product variation.

File

tests/src/Functional/ProductAdminTest.php, line 73

Class

ProductAdminTest
Test that the product creation form contains the stock settings fields.

Namespace

Drupal\Tests\commerce_stock\Functional

Code

public function testEditProduct() {
  $product = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
  ]);
  $original_sku = strtolower($this
    ->randomMachineName());
  $variation = $this
    ->createEntity('commerce_product_variation', [
    'type' => 'default',
    'product_id' => $product
      ->id(),
    'sku' => $original_sku,
    'title' => $this
      ->randomString(),
  ]);
  $this
    ->drupalGet($variation
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->fieldExists('sku[0][value]');
  $this
    ->assertSession()
    ->buttonExists('Save');
  $this
    ->assertSession()
    ->fieldExists('commerce_stock_always_in_stock[value]');
  $this
    ->assertSession()
    ->checkboxNotChecked('commerce_stock_always_in_stock[value]');
  $new_sku = strtolower($this
    ->randomMachineName());
  $new_price_amount = '1.11';
  $variations_edit = [
    'sku[0][value]' => $new_sku,
    'price[0][number]' => $new_price_amount,
    'status[value]' => 1,
  ];
  $checkbox = $this
    ->getSession()
    ->getPage()
    ->findField('commerce_stock_always_in_stock[value]');
  if ($checkbox) {
    $checkbox
      ->check();
  }
  $this
    ->submitForm($variations_edit, 'Save');
  \Drupal::service('entity_type.manager')
    ->getStorage('commerce_product_variation')
    ->resetCache([
    $variation
      ->id(),
  ]);
  $variation = ProductVariation::load($variation
    ->id());
  $this
    ->assertEquals($variation
    ->getSku(), $new_sku, 'SKU successfully changed.');
  $this
    ->assertEquals($variation
    ->getPrice()
    ->getNumber(), $new_price_amount);
  $this
    ->assertEquals('1', $variation
    ->get('commerce_stock_always_in_stock')
    ->getValue()[0]['value']);
}