ProductDefaultVariationEventTest.php in Commerce Core 8.2
File
modules/product/tests/src/Kernel/ProductDefaultVariationEventTest.php
View source
<?php
namespace Drupal\Tests\commerce_product\Kernel;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductInterface;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
class ProductDefaultVariationEventTest extends CommerceKernelTestBase {
public static $modules = [
'path',
'commerce_product',
'commerce_product_test',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig('user');
$this
->installEntitySchema('commerce_product_variation');
$this
->installEntitySchema('commerce_product');
$this
->installConfig([
'commerce_product',
]);
user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, [
'view commerce_product',
]);
user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, [
'view commerce_product',
]);
}
public function testChangeDefaultVariation() {
$variation = ProductVariation::create([
'type' => 'default',
'sku' => 'TEST_DEFAULT_VARIATION_EVENT',
'title' => $this
->randomString(),
'status' => 1,
]);
$variation
->save();
$variation1 = ProductVariation::create([
'type' => 'default',
'sku' => 'EXPECTED_VARIATION',
'title' => $this
->randomString(),
'status' => 1,
]);
$variation1
->save();
$product = Product::create([
'type' => 'default',
'title' => 'My Product Title',
'variations' => [
$variation,
$variation1,
],
]);
$product
->save();
$default_variation = $product
->getDefaultVariation();
$this
->assertEquals('EXPECTED_VARIATION', $default_variation
->getSku());
$variation
->setSku('MODIFIED_SKU');
$variation
->save();
$product = $this
->reloadEntity($product);
assert($product instanceof ProductInterface);
$default_variation = $product
->getDefaultVariation();
$this
->assertEquals('MODIFIED_SKU', $default_variation
->getSku());
}
}