You are here

public function ProductDefaultVariationEventTest::testChangeDefaultVariation in Commerce Core 8.2

Tests that the event allows changing the default variation.

File

modules/product/tests/src/Kernel/ProductDefaultVariationEventTest.php, line 46

Class

ProductDefaultVariationEventTest
Tests the default product variation event.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

public function testChangeDefaultVariation() {
  $variation = ProductVariation::create([
    'type' => 'default',
    'sku' => 'TEST_DEFAULT_VARIATION_EVENT',
    'title' => $this
      ->randomString(),
    'status' => 1,
  ]);
  $variation
    ->save();
  $variation1 = ProductVariation::create([
    'type' => 'default',
    'sku' => 'EXPECTED_VARIATION',
    'title' => $this
      ->randomString(),
    'status' => 1,
  ]);
  $variation1
    ->save();

  /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
  $product = Product::create([
    'type' => 'default',
    'title' => 'My Product Title',
    'variations' => [
      $variation,
      $variation1,
    ],
  ]);
  $product
    ->save();
  $default_variation = $product
    ->getDefaultVariation();
  $this
    ->assertEquals('EXPECTED_VARIATION', $default_variation
    ->getSku());
  $variation
    ->setSku('MODIFIED_SKU');
  $variation
    ->save();

  // Reload the product to clear the defaultVariation property.
  $product = $this
    ->reloadEntity($product);
  assert($product instanceof ProductInterface);
  $default_variation = $product
    ->getDefaultVariation();
  $this
    ->assertEquals('MODIFIED_SKU', $default_variation
    ->getSku());
}