You are here

public function ProductAdminTest::testSingleVariationMode in Commerce Core 8.2

Tests the single variation mode.

File

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

Class

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

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testSingleVariationMode() {
  $this
    ->drupalGet('admin/commerce/config/product-types/default/edit');
  $this
    ->submitForm([
    'multipleVariations' => FALSE,
  ], 'Save');
  $this
    ->drupalGet('admin/commerce/products');
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Add product');
  $this
    ->assertSession()
    ->buttonNotExists('Save and add variations');
  $this
    ->assertSession()
    ->fieldExists('variations[entity][sku][0][value]');
  $title = 'Mug';
  $store_id = $this->stores[0]
    ->id();
  $sku = strtolower($this
    ->randomMachineName());

  // Fill all needed fields except the image.
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->fillField('title[0][value]', $title);
  $page
    ->fillField('stores[target_id][value][' . $store_id . ']', $store_id);
  $page
    ->fillField('variations[entity][sku][0][value]', $sku);
  $page
    ->fillField('variations[entity][price][0][number]', '99.99');

  // Upload the image.
  $this
    ->submitForm([
    'files[variations_entity_field_image_0]' => $this->testImage->realpath,
  ], 'variations_entity_field_image_0_upload_button');
  $this
    ->assertSession()
    ->buttonExists('variations_entity_field_image_0_remove_button');

  // Submit the form.
  $this
    ->submitForm([], 'Save');

  // Confirm that we've avoided the #commerce_element_submit bug where
  // uploading a file saves the variation in the background, causing the
  // later submit to fail due to the SKU already existing in the database.
  $this
    ->assertSession()
    ->pageTextNotContains(sprintf('The SKU "%s" is already in use and must be unique.', $sku));
  $this
    ->assertSession()
    ->pageTextContains('The product Mug has been successfully saved');
  $product = Product::load(1);
  $this
    ->assertNotEmpty($product);
  $this
    ->assertEquals($title, $product
    ->getTitle());
  $this
    ->assertEquals([
    $store_id,
  ], $product
    ->getStoreIds());
  $variation = $product
    ->getDefaultVariation();
  $this
    ->assertNotEmpty($variation);
  $this
    ->assertEquals($sku, $variation
    ->getSku());
  $this
    ->assertEquals(new Price('99.99', 'USD'), $variation
    ->getPrice());
  $this
    ->assertFalse($variation
    ->get('field_image')
    ->isEmpty());
  $this
    ->drupalGet($product
    ->toUrl('edit-form'));
  $edit = [
    'title[0][value]' => 'New title',
    'variations[entity][price][0][number]' => '199.99',
  ];
  $this
    ->submitForm($edit, 'Save');
  \Drupal::entityTypeManager()
    ->getStorage('commerce_product')
    ->resetCache([
    1,
  ]);
  \Drupal::entityTypeManager()
    ->getStorage('commerce_product_variation')
    ->resetCache([
    1,
  ]);
  $product = Product::load(1);
  $this
    ->assertNotEmpty($product);
  $this
    ->assertEquals('New title', $product
    ->getTitle());
  $this
    ->assertEquals([
    $store_id,
  ], $product
    ->getStoreIds());
  $variation = $product
    ->getDefaultVariation();
  $this
    ->assertNotEmpty($variation);
  $this
    ->assertEquals(1, $variation
    ->id());
  $this
    ->assertEquals($sku, $variation
    ->getSku());
  $this
    ->assertEquals(new Price('199.99', 'USD'), $variation
    ->getPrice());

  // The variation collection page should be inaccessible.
  $this
    ->drupalGet($variation
    ->toUrl('collection'));
  $this
    ->assertSession()
    ->statusCodeEquals('403');
}