You are here

public function ProductAdminTest::testCreateProductVariationForm in Commerce Stock 8

Test the create form.

File

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

Class

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

Namespace

Drupal\Tests\commerce_stock\Functional

Code

public function testCreateProductVariationForm() {
  $this
    ->drupalGet('admin/commerce/products');
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Add product');

  // Create a product.
  $store_ids = EntityHelper::extractIds($this->stores);
  $title = $this
    ->randomMachineName();
  $edit = [
    'title[0][value]' => $title,
  ];
  foreach ($store_ids as $store_id) {
    $edit['stores[target_id][value][' . $store_id . ']'] = $store_id;
  }
  $this
    ->submitForm($edit, 'Save and add variations');
  $this
    ->assertNotEmpty($this
    ->getSession()
    ->getPage()
    ->hasLink('Add variation'));

  // Create a variation.
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Add variation');
  $this
    ->assertSession()
    ->pageTextContains(t('Add variation'));
  $this
    ->assertSession()
    ->fieldExists('sku[0][value]');
  $this
    ->assertSession()
    ->fieldExists('price[0][number]');
  $this
    ->assertSession()
    ->fieldExists('status[value]');
  $this
    ->assertSession()
    ->fieldExists('commerce_stock_always_in_stock[value]');
  $this
    ->assertSession()
    ->buttonExists('Save');
  $variation_sku = $this
    ->randomMachineName();
  $this
    ->getSession()
    ->getPage()
    ->fillField('sku[0][value]', $variation_sku);
  $this
    ->getSession()
    ->getPage()
    ->fillField('price[0][number]', '9.99');
  $this
    ->getSession()
    ->getPage()
    ->fillField('title[0][value]', $this
    ->randomString());
  $this
    ->submitForm([], t('Save'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $variation = ProductVariation::load(4);
  $this
    ->assertEquals($variation_sku, $variation
    ->getSku());
  $this
    ->assertEquals('0', $variation
    ->get('commerce_stock_always_in_stock')
    ->getValue()[0]['value']);
}