You are here

public function ProductVariationTitleGenerationTest::testTitleGeneration in Commerce Core 8.2

Test the title generation.

File

modules/product/tests/src/Functional/ProductVariationTitleGenerationTest.php, line 65

Class

ProductVariationTitleGenerationTest
Tests the product variation title generation.

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testTitleGeneration() {

  /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
  $variation = $this
    ->createEntity('commerce_product_variation', [
    'type' => 'test_default',
    'sku' => strtolower($this
      ->randomMachineName()),
  ]);

  /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
  $this
    ->createEntity('commerce_product', [
    'type' => 'default',
    'title' => 'My product',
    'variations' => [
      $variation,
    ],
  ]);
  $this
    ->assertNotEmpty(empty($variation
    ->getTitle()));
  $this->variationType
    ->setGenerateTitle(TRUE);
  $this->variationType
    ->save();

  /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
  $variation = $this
    ->createEntity('commerce_product_variation', [
    'type' => 'test_default',
    'sku' => strtolower($this
      ->randomMachineName()),
  ]);

  /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
  $product = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
    'title' => 'My second product',
    'variations' => [
      $variation,
    ],
  ]);
  $this
    ->assertEquals($variation
    ->getTitle(), $product
    ->getTitle());

  // Adding attributes should change the titles.
  $variation_type = ProductVariationType::load($variation
    ->bundle());
  $size_attributes = $this
    ->createAttributeSet($variation_type, 'size', [
    'small' => 'Small',
    'medium' => 'Medium',
    'large' => 'Large',
  ]);
  $variation = ProductVariation::load($variation
    ->id());
  $variation->attribute_size = $size_attributes['small']
    ->id();
  $variation
    ->save();
  $this
    ->assertEquals($product
    ->getTitle() . ' - Small', $variation
    ->getTitle());
  $color_attributes = $this
    ->createAttributeSet($variation_type, 'color', [
    'red' => 'Red',
  ]);
  $variation = ProductVariation::load($variation
    ->id());
  $variation->attribute_color = $color_attributes['red']
    ->id();
  $variation
    ->save();
  $this
    ->assertEquals($product
    ->getTitle() . ' - Small, Red', $variation
    ->getTitle());
}