public function ProductVariationTest::testAttributes in Commerce Core 8.2
@covers ::getAttributeFieldNames @covers ::getAttributeValueIds @covers ::getAttributeValueId @covers ::getAttributeValues @covers ::getAttributeValue
File
- modules/
product/ tests/ src/ Kernel/ Entity/ ProductVariationTest.php, line 164
Class
- ProductVariationTest
- Tests the Product variation entity.
Namespace
Drupal\Tests\commerce_product\Kernel\EntityCode
public function testAttributes() {
$color_attribute = ProductAttribute::create([
'id' => 'color',
'label' => 'Color',
]);
$color_attribute
->save();
$size_attribute = ProductAttribute::create([
'id' => 'size',
'label' => 'Size',
]);
$size_attribute
->save();
$attribute_field_manager = $this->container
->get('commerce_product.attribute_field_manager');
$attribute_field_manager
->createField($color_attribute, 'default');
$attribute_field_manager
->createField($size_attribute, 'default');
$color_attribute_value = ProductAttributeValue::create([
'attribute' => 'color',
'name' => 'Blue',
'weight' => 0,
]);
$color_attribute_value
->save();
$color_attribute_value = $this
->reloadEntity($color_attribute_value);
$size_attribute_value = ProductAttributeValue::create([
'attribute' => 'size',
'name' => 'Medium',
'weight' => 0,
]);
$size_attribute_value
->save();
$size_attribute_value = $this
->reloadEntity($size_attribute_value);
$product = Product::create([
'type' => 'default',
'title' => 'My Product Title',
]);
$product
->save();
$product = $this
->reloadEntity($product);
/** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
$variation = ProductVariation::create([
'type' => 'default',
'product_id' => $product
->id(),
'attribute_color' => $color_attribute_value
->id(),
'attribute_size' => $size_attribute_value
->id(),
]);
$variation
->save();
$variation = $this
->reloadEntity($variation);
$this
->assertEquals([
'attribute_color',
'attribute_size',
], $variation
->getAttributeFieldNames());
$this
->assertEquals([
'attribute_color' => $color_attribute_value
->id(),
'attribute_size' => $size_attribute_value
->id(),
], $variation
->getAttributeValueIds());
$this
->assertEquals($color_attribute_value
->id(), $variation
->getAttributeValueId('attribute_color'));
$this
->assertEquals($size_attribute_value
->id(), $variation
->getAttributeValueId('attribute_size'));
$this
->assertEquals([
'attribute_color' => $color_attribute_value,
'attribute_size' => $size_attribute_value,
], $variation
->getAttributeValues());
$this
->assertEquals($color_attribute_value, $variation
->getAttributeValue('attribute_color'));
$this
->assertEquals($size_attribute_value, $variation
->getAttributeValue('attribute_size'));
// Confirm that empty fields are excluded properly.
$variation
->set('attribute_size', NULL);
$variation
->save();
$variation = $this
->reloadEntity($variation);
$this
->assertEquals([
'attribute_color' => $color_attribute_value
->id(),
], $variation
->getAttributeValueIds());
$this
->assertNull($variation
->getAttributeValueId('attribute_size'));
$this
->assertEquals([
'attribute_color' => $color_attribute_value,
], $variation
->getAttributeValues());
$this
->assertNull($variation
->getAttributeValue('attribute_size'));
// Confirm that deleted attribute values are handled properly.
$variation
->set('attribute_size', $size_attribute_value
->id());
$variation
->save();
$variation = $this
->reloadEntity($variation);
$size_attribute_value
->delete();
$this
->assertEquals([
'attribute_color' => $color_attribute_value
->id(),
'attribute_size' => $size_attribute_value
->id(),
], $variation
->getAttributeValueIds());
$this
->assertEquals([
'attribute_color' => $color_attribute_value,
], $variation
->getAttributeValues());
$this
->assertNull($variation
->getAttributeValue('attribute_size'));
}