You are here

public function ProductVariationStorageTest::testLoadEnabled in Commerce Core 8.2

Tests loadEnabled() function.

File

modules/product/tests/src/Kernel/ProductVariationStorageTest.php, line 77

Class

ProductVariationStorageTest
Tests the product variation storage.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

public function testLoadEnabled() {
  $variations = [];
  for ($i = 1; $i <= 3; $i++) {
    $variation = ProductVariation::create([
      'type' => 'default',
      'sku' => strtolower($this
        ->randomMachineName()),
      'title' => $this
        ->randomString(),
      'status' => $i % 2,
    ]);
    $variation
      ->save();
    $variations[] = $variation;
  }
  $variations = array_reverse($variations);
  $product = Product::create([
    'type' => 'default',
    'variations' => $variations,
  ]);
  $product
    ->save();
  $variationsFiltered = $this->variationStorage
    ->loadEnabled($product);
  $this
    ->assertEquals(2, count($variationsFiltered), '2 out of 3 variations are enabled');
  $this
    ->assertEquals(reset($variations)
    ->getSku(), reset($variationsFiltered)
    ->getSku(), 'The sort order of the variations remains the same');
}