You are here

public function ProductVariationTest::testProductVariation in Commerce Core 8.2

@covers ::getOrderItemTypeId @covers ::getOrderItemTitle @covers ::getProduct @covers ::getProductId @covers ::getSku @covers ::setSku @covers ::getTitle @covers ::setTitle @covers ::getListPrice @covers ::setListPrice @covers ::getPrice @covers ::setPrice @covers ::isActive @covers ::setActive @covers ::getCreatedTime @covers ::setCreatedTime @covers ::getOwner @covers ::setOwner @covers ::getOwnerId @covers ::setOwnerId @covers ::getStores

File

modules/product/tests/src/Kernel/Entity/ProductVariationTest.php, line 80

Class

ProductVariationTest
Tests the Product variation entity.

Namespace

Drupal\Tests\commerce_product\Kernel\Entity

Code

public function testProductVariation() {

  /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
  $product = Product::create([
    'type' => 'default',
    'title' => 'My Product Title',
  ]);
  $product
    ->save();

  /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
  $variation = ProductVariation::create([
    'type' => 'default',
    'product_id' => $product
      ->id(),
  ]);
  $variation
    ->save();
  $product = $this
    ->reloadEntity($product);
  $variation = $this
    ->reloadEntity($variation);

  // Confirm that postSave() added the reference on the parent product.
  $this
    ->assertTrue($product
    ->hasVariation($variation));
  $this
    ->assertEquals('default', $variation
    ->getOrderItemTypeId());
  $this
    ->assertEquals('My Product Title', $variation
    ->getOrderItemTitle());
  $this
    ->assertEquals($product, $variation
    ->getProduct());
  $this
    ->assertEquals($product
    ->id(), $variation
    ->getProductId());
  $variation
    ->setSku('1001');
  $this
    ->assertEquals('1001', $variation
    ->getSku());
  $variation
    ->setTitle('My title');
  $this
    ->assertEquals('My title', $variation
    ->getTitle());
  $list_price = new Price('19.99', 'USD');
  $variation
    ->setListPrice($list_price);
  $this
    ->assertEquals($list_price, $variation
    ->getListPrice());
  $price = new Price('9.99', 'USD');
  $variation
    ->setPrice($price);
  $this
    ->assertEquals($price, $variation
    ->getPrice());
  $variation
    ->setPublished();
  $this
    ->assertEquals(TRUE, $variation
    ->isPublished());
  $variation
    ->setCreatedTime(635879700);
  $this
    ->assertEquals(635879700, $variation
    ->getCreatedTime());
  $variation
    ->setOwner($this->user);
  $this
    ->assertEquals($this->user, $variation
    ->getOwner());
  $this
    ->assertEquals($this->user
    ->id(), $variation
    ->getOwnerId());
  $variation
    ->setOwnerId(0);
  $this
    ->assertInstanceOf(UserInterface::class, $variation
    ->getOwner());
  $this
    ->assertTrue($variation
    ->getOwner()
    ->isAnonymous());

  // Non-existent/deleted user ID.
  $variation
    ->setOwnerId(892);
  $this
    ->assertInstanceOf(UserInterface::class, $variation
    ->getOwner());
  $this
    ->assertTrue($variation
    ->getOwner()
    ->isAnonymous());
  $this
    ->assertEquals(892, $variation
    ->getOwnerId());
  $variation
    ->setOwnerId($this->user
    ->id());
  $this
    ->assertEquals($this->user, $variation
    ->getOwner());
  $this
    ->assertEquals($this->user
    ->id(), $variation
    ->getOwnerId());
  $this
    ->assertEquals($product
    ->getStores(), $variation
    ->getStores());

  // Confirm that deleting the variation deletes the reference.
  $variation
    ->delete();
  $product = $this
    ->reloadEntity($product);
  $this
    ->assertFalse($product
    ->hasVariation($variation));

  // Confirm that the attribute methods return nothing by default.
  $this
    ->assertEmpty($variation
    ->getAttributeFieldNames());
  $this
    ->assertEmpty($variation
    ->getAttributeValueIds());
  $this
    ->assertEmpty($variation
    ->getAttributeValues());
  $this
    ->assertEquals([
    'store',
  ], $variation
    ->getCacheContexts());
}