You are here

public function BundleItemOrderItemTest::testBundleItemOrderItemOnOrderItem in Commerce Product Bundle 8

Tests the integration with commerce order item.

File

tests/src/Kernel/Entity/BundleItemOrderItemTest.php, line 135

Class

BundleItemOrderItemTest
Test the Product Bundle Item entity.

Namespace

Drupal\Tests\commerce_product_bundle\Kernel\Entity

Code

public function testBundleItemOrderItemOnOrderItem() {

  // Whether the config works and the order item type is available and
  // the bundle_item_order items field is attached.
  $orderItem = $this->orderItem;
  self::assertEquals('commerce_product_bundle_default', $this->orderItem
    ->bundle());
  self::assertTrue($orderItem
    ->hasField('bundle_item_order_items'));
  $bundleItemOrderItem0 = BundleItemOrderItem::create([
    'title' => $this
      ->randomString(),
    'bundle_item' => $this->productBundleItem,
    'purchased_entity' => $this->productVariation,
    'unit_price' => new Price('22.33', 'USD'),
    'quantity' => '3',
    'total_price' => new Price('66.99', 'USD'),
  ]);
  $bundleItemOrderItem1 = BundleItemOrderItem::create([
    'title' => $this
      ->randomString(),
    'bundle_item' => $this->productBundleItem,
    'purchased_entity' => $this->productVariation,
    'unit_price' => new Price('11.11', 'USD'),
    'quantity' => '1',
    'total_price' => new Price('22.22', 'USD'),
  ]);
  $orderItem
    ->set('bundle_item_order_items', [
    $bundleItemOrderItem0,
    $bundleItemOrderItem1,
  ]);
  $orderItem
    ->set('purchased_entity', $this->productBundle);
  $orderItem
    ->save();
  $orderItem = $this
    ->reloadEntity($orderItem);
  $bundleItemOrderItems = $orderItem
    ->get('bundle_item_order_items')
    ->referencedEntities();
  self::assertEquals('3', $bundleItemOrderItems[0]
    ->getQuantity());
  self::assertEquals('1', $bundleItemOrderItems[1]
    ->getQuantity());
  $orderItemId = $orderItem
    ->id();

  // Whether the order item id backreference is populated after order item save.
  self::assertEquals($orderItemId, $bundleItemOrderItems[0]
    ->getOrderItemId());
  self::assertEquals($orderItemId, $bundleItemOrderItems[1]
    ->getOrderItemId());

  // Whether recalculation of total is triggered by order item save.
  self::assertEquals('11.11', $bundleItemOrderItems[1]
    ->getTotalPrice()
    ->getNumber());

  // Tests bundle item order item deletion if the order item has been deleted.
  $orderItem
    ->delete();
  $bundleItemOrderItem0Exists = (bool) BundleItemOrderItem::load($bundleItemOrderItems[0]
    ->id());
  $bundleItemOrderItem1Exists = (bool) BundleItemOrderItem::load($bundleItemOrderItems[1]
    ->id());
  $this
    ->assertEmpty($bundleItemOrderItem0Exists, 'The bundle item order item 0 has been deleted from database.');
  $this
    ->assertEmpty($bundleItemOrderItem1Exists, 'The bundle item order item 1 has been deleted from database.');
}