You are here

public function ProductAdminTest::testDuplicateVariation in Commerce Core 8.2

Tests duplicating a product variation.

File

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

Class

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

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testDuplicateVariation() {
  $sku = strtolower($this
    ->randomMachineName());
  $product = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
  ]);
  $variation = $this
    ->createEntity('commerce_product_variation', [
    'type' => 'default',
    'product_id' => $product
      ->id(),
    'sku' => $sku,
    'price' => [
      'number' => '12.00',
      'currency_code' => 'USD',
    ],
    'status' => TRUE,
  ]);

  // Check the integrity of the variation form.
  $this
    ->drupalGet($variation
    ->toUrl('duplicate-form'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->fieldExists('sku[0][value]');
  $this
    ->assertSession()
    ->fieldExists('price[0][number]');
  $this
    ->assertSession()
    ->buttonExists('Save');

  // Confirm that we can't save the duplicate form with the existing SKU.
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->addressEquals($variation
    ->toUrl('duplicate-form'));
  $this
    ->assertSession()
    ->pageTextContains(sprintf('The SKU "%s" is already in use and must be unique.', $sku));
  $new_sku = strtolower($this
    ->randomMachineName());
  $variations_edit = [
    'sku[0][value]' => $new_sku,
  ];
  $this
    ->submitForm($variations_edit, 'Save');
  $this
    ->assertSession()
    ->addressEquals($variation
    ->toUrl('collection'));
  $expected_variation_id = $variation
    ->id() + 1;
  $variation = ProductVariation::load($expected_variation_id);
  $this
    ->assertEquals($new_sku, $variation
    ->getSku());
  $this
    ->assertEquals('12.00', $variation
    ->getPrice()
    ->getNumber());
  $this
    ->assertTrue($variation
    ->isPublished());
}