You are here

public function CommerceProductBundleItemTest::testBundleItem in Commerce Product Bundle 8

@covers ::getTitle @covers ::setTitle @covers ::isRequired @covers ::setRequired @covers ::getCreatedTime @covers ::setCreatedTime @covers ::setMaximumQuantity @covers ::getMaximumQuantity @covers ::setMinimumQuantity @covers ::getMinimumQuantity @covers ::setQuantity @covers ::getQuantity @covers ::hasUnitPrice @covers ::getUnitPrice @covers ::setUnitPrice

File

tests/src/Kernel/Entity/CommerceProductBundleItemTest.php, line 40

Class

CommerceProductBundleItemTest
Test the Product Bundle Item entity.

Namespace

Drupal\Tests\commerce_product_bundle\Kernel\Entity

Code

public function testBundleItem() {
  $bundleItem = ProductBundleItem::create([
    'type' => 'default',
  ]);
  $bundleItem
    ->save();

  // Confirm the attached fields are there.
  $this
    ->assertTrue($bundleItem
    ->hasField('variations'));
  $created_field = $bundleItem
    ->getFieldDefinition('variations');
  $this
    ->assertInstanceOf(FieldConfig::class, $created_field);
  $this
    ->assertEquals('commerce_product_variation', $created_field
    ->getSetting('target_type'));
  $this
    ->assertEquals('default:commerce_product_variation', $created_field
    ->getSetting('handler'));
  $bundleItem
    ->setTitle('My testtitle');
  $this
    ->assertEquals('My testtitle', $bundleItem
    ->getTitle());
  $this
    ->assertEquals(TRUE, $bundleItem
    ->isRequired());
  $bundleItem
    ->setRequired(FALSE);
  $this
    ->assertEquals(FALSE, $bundleItem
    ->isRequired());

  // Wether edge case of 0.0 price value works.
  $this
    ->assertNull($bundleItem
    ->getUnitPrice());
  $this
    ->assertFalse($bundleItem
    ->hasUnitPrice());
  $price = new Price('0.00', 'USD');
  $bundleItem
    ->setUnitPrice($price);
  $this
    ->assertTrue($bundleItem
    ->hasUnitPrice());
  $this
    ->assertEquals($price, $bundleItem
    ->getUnitPrice());
  $this
    ->assertEquals('0.0', $price
    ->getNumber());
  $this
    ->assertEquals('USD', $price
    ->getCurrencyCode());
  $price = new Price('55.55', 'USD');
  $bundleItem
    ->setUnitPrice($price);
  $this
    ->assertTrue($bundleItem
    ->hasUnitPrice());
  $this
    ->assertEquals($price, $bundleItem
    ->getUnitPrice());
  $this
    ->assertEquals('55.55', $price
    ->getNumber());
  $this
    ->assertEquals('USD', $price
    ->getCurrencyCode());
  $bundleItem
    ->setCreatedTime(635879700);
  $this
    ->assertEquals(635879700, $bundleItem
    ->getCreatedTime());
  $bundleItem
    ->setOwner($this->user);
  $this
    ->assertEquals($this->user, $bundleItem
    ->getOwner());
  $this
    ->assertEquals($this->user
    ->id(), $bundleItem
    ->getOwnerId());
  $bundleItem
    ->setOwnerId(0);
  $this
    ->assertEquals(NULL, $bundleItem
    ->getOwner());
  $bundleItem
    ->setOwnerId($this->user
    ->id());
  $this
    ->assertEquals($this->user, $bundleItem
    ->getOwner());
  $this
    ->assertEquals($this->user
    ->id(), $bundleItem
    ->getOwnerId());
  $bundleItem
    ->setMaximumQuantity(0);
  $violations = $bundleItem
    ->validate()
    ->getByField("max_quantity");
  $this
    ->assertCount(1, $violations);
  $bundleItem
    ->setMaximumQuantity(55);
  $this
    ->assertEquals(55, $bundleItem
    ->getMaximumQuantity());
  $violations = $bundleItem
    ->validate()
    ->getByField("max_quantity");
  $this
    ->assertCount(0, $violations);
  $bundleItem
    ->save();
  $bundleItem = $this
    ->reloadEntity($bundleItem);
  $bundleItem
    ->setMinimumQuantity(-1);
  $violations = $bundleItem
    ->validate()
    ->getByField("min_quantity");
  $this
    ->assertCount(2, $violations);
  $bundleItem
    ->setMinimumQuantity(11);
  $this
    ->assertEquals(11, $bundleItem
    ->getMinimumQuantity());
  $violations = $bundleItem
    ->validate()
    ->getByField("min_quantity");
  $this
    ->assertCount(0, $violations);

  // Set a product, to prevent counting the required product reference
  // field into the violations when calling ::validate().
  $variation = ProductVariation::create([
    'type' => 'default',
    'sku' => strtolower($this
      ->randomMachineName()),
    'title' => $this
      ->randomString(),
    'status' => 1,
  ]);
  $variation
    ->save();
  $product = Product::create([
    'type' => 'default',
    'variations' => [
      $variation,
    ],
  ]);
  $product
    ->save();
  $product = $this
    ->reloadEntity($product);
  $bundleItem
    ->setProduct($product);
  $this
    ->assertTrue($bundleItem
    ->hasProduct());
  $this
    ->assertEquals($product
    ->id(), $bundleItem
    ->getProductId());

  // I have no clue why we need to save and reload the bundleItem to pass
  // the next assertion. But otherwise it has two violations - one is the
  // product reference.
  $bundleItem
    ->save();
  $bundleItem = $this
    ->reloadEntity($bundleItem);
  $bundleItem
    ->setMinimumQuantity(111);
  $violations = $bundleItem
    ->validate();
  $this
    ->assertCount(1, $violations);
  $bundleItem
    ->setMaximumQuantity(222);
  $bundleItem
    ->setMinimumQuantity(222);
  $violations = $bundleItem
    ->validate();
  $this
    ->assertCount(0, $violations);
  $bundleItem
    ->setQuantity(12);
  $this
    ->assertEquals(12, $bundleItem
    ->getQuantity());
}