You are here

public function CommerceProductBundleTest::testBundle in Commerce Product Bundle 8

@covers ::getTitle @covers ::setTitle @covers ::isPublished @covers ::setPublished @covers ::getCreatedTime @covers ::setCreatedTime @covers ::postDelete @covers ::setBundleItems @covers ::addBundleItem @covers ::removeBundleItem @covers ::getBundleItemIds @covers ::hasBundleItem @covers ::hasBundleItems

File

tests/src/Kernel/Entity/CommerceProductBundleTest.php, line 37

Class

CommerceProductBundleTest
Test the Product Bundle Item entity.

Namespace

Drupal\Tests\commerce_product_bundle\Kernel\Entity

Code

public function testBundle() {
  $variations = [];
  for ($i = 1; $i <= 5; $i++) {
    $variation = ProductVariation::create([
      'type' => 'default',
      'sku' => strtolower($this
        ->randomMachineName()),
      'title' => $this
        ->randomString(),
      'status' => $i % 2,
      'uid' => $this->user
        ->id(),
    ]);
    $variation
      ->save();
    $variations[] = $variation;
  }
  $variations = array_reverse($variations);
  $product = Product::create([
    'type' => 'default',
    'variations' => $variations,
    'uid' => $this->user
      ->id(),
  ]);
  $product
    ->save();
  $product1 = $this
    ->reloadEntity($product);
  $variations = [];
  for ($i = 1; $i <= 3; $i++) {
    $variation = ProductVariation::create([
      'type' => 'default',
      'sku' => strtolower($this
        ->randomMachineName()),
      'title' => $this
        ->randomString(),
      'status' => TRUE,
      'uid' => $this->user
        ->id(),
    ]);
    $variation
      ->save();
    $variations[] = $variation;
  }
  $variations = array_reverse($variations);
  $product = Product::create([
    'type' => 'default',
    'variations' => $variations,
    'uid' => $this->user
      ->id(),
  ]);
  $product
    ->save();
  $product2 = $this
    ->reloadEntity($product);
  $bundleItem = ProductBundleItem::create([
    'type' => 'default',
    'uid' => $this->user
      ->id(),
    'title' => 'testBundle1',
    'status' => TRUE,
  ]);
  $bundleItem
    ->setProduct($product1);
  $bundleItem
    ->save();
  $bundleItem = $this
    ->reloadEntity($bundleItem);
  $bundleItem2 = ProductBundleItem::create([
    'type' => 'default',
    'uid' => $this->user
      ->id(),
    'title' => 'testBundle2',
    'status' => TRUE,
  ]);
  $bundleItem2
    ->setProduct($product2);
  $bundleItem2
    ->save();
  $bundleItem2 = $this
    ->reloadEntity($bundleItem2);
  $bundle = ProductBundle::create([
    'type' => 'default',
    'uid' => $this->user
      ->id(),
    'status' => TRUE,
  ]);
  $bundle
    ->save();
  $bundle
    ->setTitle('My testtitle');
  $this
    ->assertEquals('My testtitle', $bundle
    ->getTitle());

  // Confirm the attached fields are there.
  $this
    ->assertTrue($bundle
    ->hasField('bundle_items'));
  $created_field = $bundle
    ->getFieldDefinition('bundle_items');
  $this
    ->assertInstanceOf(FieldConfig::class, $created_field);
  $this
    ->assertEquals('commerce_product_bundle_i', $created_field
    ->getSetting('target_type'));
  $this
    ->assertEquals('default:commerce_product_bundle_i', $created_field
    ->getSetting('handler'));
  $this
    ->assertTrue($bundle
    ->hasField('stores'));
  $created_field = $bundle
    ->getFieldDefinition('stores');
  $this
    ->assertInstanceOf(FieldConfig::class, $created_field);
  $this
    ->assertEquals('commerce_store', $created_field
    ->getSetting('target_type'));
  $this
    ->assertEquals('default:commerce_store', $created_field
    ->getSetting('handler'));
  $this
    ->assertTrue($bundle
    ->hasField('body'));
  $created_field = $bundle
    ->getFieldDefinition('body');
  $this
    ->assertInstanceOf(FieldConfig::class, $created_field);
  $this
    ->assertEquals(TRUE, $bundle
    ->isPublished());
  $bundle
    ->setPublished(FALSE);
  $this
    ->assertEquals(FALSE, $bundle
    ->isPublished());
  $bundle
    ->setCreatedTime(635879700);
  $this
    ->assertEquals(635879700, $bundle
    ->getCreatedTime());
  $bundle
    ->setOwner($this->user);
  $this
    ->assertEquals($this->user, $bundle
    ->getOwner());
  $this
    ->assertEquals($this->user
    ->id(), $bundle
    ->getOwnerId());
  $bundle
    ->setOwnerId(0);
  $this
    ->assertEquals(NULL, $bundle
    ->getOwner());
  $bundle
    ->setOwnerId($this->user
    ->id());
  $this
    ->assertEquals($this->user, $bundle
    ->getOwner());
  $this
    ->assertEquals($this->user
    ->id(), $bundle
    ->getOwnerId());
  $this
    ->assertFalse($bundle
    ->hasBundleItems());
  $bundle
    ->setBundleItems([
    $bundleItem,
  ]);
  $bundle
    ->save();

  /** @var \Drupal\commerce_product_bundle\Entity\BundleInterface $bundle */
  $bundle = $this
    ->reloadEntity($bundle);
  $items = $bundle
    ->getBundleItems();
  $this
    ->assertEquals($items[0]
    ->Id(), $bundleItem
    ->Id());
  $this
    ->assertTrue($bundle
    ->hasBundleItems());
  $this
    ->assertTrue($bundle
    ->hasBundleItem($bundleItem));
  $this
    ->assertFalse($bundle
    ->hasBundleItem($bundleItem2));
  $bundle
    ->addBundleItem($bundleItem2);
  $bundle
    ->save();

  /** @var \Drupal\commerce_product_bundle\Entity\BundleInterface $bundle */
  $bundle = $this
    ->reloadEntity($bundle);
  $items = $bundle
    ->getBundleItems();
  $ids = $bundle
    ->getBundleItemIds();
  $this
    ->assertEquals($bundleItem
    ->Id(), $items[0]
    ->Id());
  $this
    ->assertEquals($bundleItem
    ->Id(), $ids[0]);
  $this
    ->assertEquals($bundleItem2
    ->Id(), $items[1]
    ->Id());
  $this
    ->assertEquals($bundleItem2
    ->Id(), $ids[1]);
  $this
    ->assertTrue($bundle
    ->hasBundleItem($bundleItem2));
  $test = array_map(function ($item) {

    /** @var \Drupal\commerce_product_bundle\Entity\BundleItemInterface $item */
    $test_item = $item
      ->getCurrentVariation();
    return $test_item;
  }, $items);
  $bundle
    ->removeBundleItem($bundleItem);
  $bundle
    ->save();

  /** @var \Drupal\commerce_product_bundle\Entity\BundleInterface $bundle */
  $bundle = $this
    ->reloadEntity($bundle);
  $items = $bundle
    ->getBundleItems();
  $this
    ->assertEquals(1, count($items));
  $this
    ->assertEquals($bundleItem2
    ->Id(), $items[0]
    ->Id());
  $this
    ->assertFalse($bundle
    ->hasBundleItem($bundleItem));
  $this
    ->assertNull($bundle
    ->getPrice());

  // 0.00 is a valid Price. Check that we don't inadvertently filter it by some
  // conditionals.
  $bundle
    ->setPrice(new Price('0.00', 'USD'));
  $this
    ->assertEquals($bundle
    ->getPrice(), new Price('0.00', 'USD'));
  $bundle
    ->setPrice(new Price('3.33', 'USD'));
  $this
    ->assertEquals($bundle
    ->getPrice(), new Price('3.33', 'USD'));
  $bundle
    ->delete();
  $this
    ->assertNull(ProductBundle::load($bundle
    ->Id()));
  $this
    ->assertNull(ProductBundleItem::load($bundleItem2
    ->Id()));
}