View source
<?php
namespace Drupal\Tests\commerce_product\Functional;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\Tests\commerce_cart\Traits\CartBrowserTestTrait;
use Drupal\Tests\commerce_product\Traits\ProductAttributeTestTrait;
class ProductVariationTitleGenerationTest extends ProductBrowserTestBase {
use CartBrowserTestTrait;
use ProductAttributeTestTrait;
protected $variationType;
protected function setUp() : void {
parent::setUp();
$this->variationType = $this
->createEntity('commerce_product_variation_type', [
'id' => 'test_default',
'label' => 'Test Default',
'orderItemType' => 'default',
]);
$this->attributeFieldManager = $this->container
->get('commerce_product.attribute_field_manager');
}
public function testTitleGenerationSetting() {
$this
->assertEmpty($this->variationType
->shouldGenerateTitle());
$field_definitions = $this->container
->get('entity_field.manager')
->getFieldDefinitions('commerce_product_variation', $this->variationType
->id());
$this
->assertNotEmpty($field_definitions['title']
->isRequired());
$this->variationType
->setGenerateTitle(TRUE);
$this->variationType
->save();
$variation_type = ProductVariationType::load($this->variationType
->id());
$this
->assertNotEmpty($variation_type
->shouldGenerateTitle());
$field_definitions = $this->container
->get('entity_field.manager')
->getFieldDefinitions('commerce_product_variation', $this->variationType
->id());
$this
->assertEmpty($field_definitions['title']
->isRequired());
}
public function testTitleGeneration() {
$variation = $this
->createEntity('commerce_product_variation', [
'type' => 'test_default',
'sku' => strtolower($this
->randomMachineName()),
]);
$this
->createEntity('commerce_product', [
'type' => 'default',
'title' => 'My product',
'variations' => [
$variation,
],
]);
$this
->assertNotEmpty(empty($variation
->getTitle()));
$this->variationType
->setGenerateTitle(TRUE);
$this->variationType
->save();
$variation = $this
->createEntity('commerce_product_variation', [
'type' => 'test_default',
'sku' => strtolower($this
->randomMachineName()),
]);
$product = $this
->createEntity('commerce_product', [
'type' => 'default',
'title' => 'My second product',
'variations' => [
$variation,
],
]);
$this
->assertEquals($variation
->getTitle(), $product
->getTitle());
$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());
}
}