You are here

public function ProductAdminTest::testVariationsTab in Commerce Core 8.2

Tests creating a product and its variations.

File

modules/product/tests/src/Functional/ProductAdminTest.php, line 296

Class

ProductAdminTest
Create, view, edit, delete, and change products.

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testVariationsTab() {
  $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
    ->assertSession()
    ->pageTextContains(t('The product @title has been successfully saved', [
    '@title' => $title,
  ]));
  $this
    ->assertSession()
    ->pageTextContains(t('There are no product variations yet.'));
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Add variation');

  // Create a variation.
  $variation_sku = $this
    ->randomMachineName();

  // Fill all needed fields except the image.
  $this
    ->getSession()
    ->getPage()
    ->fillField('sku[0][value]', $variation_sku);
  $this
    ->getSession()
    ->getPage()
    ->fillField('price[0][number]', '9.99');

  // Upload the image.
  $this
    ->submitForm([
    'files[field_image_0]' => $this->testImage->realpath,
  ], 'field_image_0_upload_button');
  $this
    ->assertSession()
    ->buttonExists('field_image_0_remove_button');

  // Submit the form.
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains("Saved the {$title} variation.");
  $variation_in_table = $this
    ->getSession()
    ->getPage()
    ->find('xpath', '//table/tbody/tr/td[text()="' . $variation_sku . '"]');
  $this
    ->assertNotEmpty($variation_in_table);
  $product = Product::load(1);
  $variation = ProductVariation::load(1);
  $this
    ->assertEquals($product
    ->id(), $variation
    ->getProductId());
  $this
    ->assertEquals($variation_sku, $variation
    ->getSku());
  $this
    ->assertFalse($variation
    ->get('field_image')
    ->isEmpty());
  $this->container
    ->get('entity_type.manager')
    ->getStorage('commerce_product')
    ->resetCache([
    $product
      ->id(),
  ]);
  $product = Product::load($product
    ->id());
  $this
    ->assertTrue($product
    ->hasVariation($variation));
}