You are here

public function ProductVariationGeneratedTitleTest::testTitleGenerated in Commerce Core 8.2

Tests the title is generated.

File

modules/product/tests/src/Kernel/ProductVariationGeneratedTitleTest.php, line 99

Class

ProductVariationGeneratedTitleTest
Tests the product variation title generation.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

public function testTitleGenerated() {

  // Variations without a product have no title, because it can not be
  // determined.
  $variation = ProductVariation::create([
    'type' => $this->variationType
      ->id(),
  ]);
  $variation
    ->save();
  $this
    ->assertNull($variation
    ->label());

  // When variations have a product, but no attributes, the variation label
  // should be the product's.
  $product = Product::create([
    'type' => $this->productType
      ->id(),
    'title' => 'My Super Product',
    'variations' => [
      $variation,
    ],
  ]);
  $product
    ->save();

  /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
  $variation = $this
    ->reloadEntity($variation);
  $this
    ->assertEquals($variation
    ->label(), $product
    ->label());

  // With attribute values, the variation title should be the product plus all
  // of its attributes.
  $color_black = ProductAttributeValue::create([
    'attribute' => $this->attribute
      ->id(),
    'name' => 'Black',
    'weight' => 3,
  ]);
  $color_black
    ->save();

  /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
  $variation = $this
    ->reloadEntity($variation);
  $variation
    ->get('attribute_color')
    ->setValue($color_black);
  $variation
    ->save();
  $this
    ->assertNotEquals($variation
    ->label(), $product
    ->label());
  $this
    ->assertEquals($variation
    ->label(), sprintf('%s - %s', $product
    ->label(), $color_black
    ->label()));
}