View source
<?php
namespace Drupal\Tests\commerce_product_bundle\Kernel\Entity;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\commerce_product_bundle\Entity\ProductBundle;
use Drupal\commerce_product_bundle\Entity\ProductBundleItem;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\commerce_product_bundle\Kernel\CommerceProductBundleKernelTestBase;
class CommerceProductBundleItemTest extends CommerceProductBundleKernelTestBase {
public function testBundleItem() {
$bundleItem = ProductBundleItem::create([
'type' => 'default',
]);
$bundleItem
->save();
$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());
$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);
$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());
$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());
}
public function testVariationsAndProductMethods() {
$bundleItem = ProductBundleItem::create([
'type' => 'default',
]);
$bundleItem
->save();
$this
->assertNull($bundleItem
->getVariations());
$this
->assertNull($bundleItem
->getCurrentVariation());
$this
->assertNull($bundleItem
->getVariationIds());
$this
->assertFalse($bundleItem
->hasProduct());
$variations = [];
for ($i = 1; $i <= 5; $i++) {
$variation = ProductVariation::create([
'type' => 'default',
'sku' => strtolower($this
->randomMachineName()),
'title' => $this
->randomString(),
'uid' => $this->user
->id(),
'status' => $i % 2,
]);
$variation
->save();
$variations[] = $variation;
}
$variations = array_reverse($variations);
$product = Product::create([
'type' => 'default',
'variations' => $variations,
'uid' => $this->user
->id(),
]);
$product
->save();
$product = $this
->reloadEntity($product);
$bundleItem
->setProduct($product);
$this
->assertTrue($bundleItem
->hasProduct());
$this
->assertEquals($product
->id(), $bundleItem
->getProductId());
$current = $bundleItem
->getCurrentVariation();
$this
->assertInstanceOf(ProductVariationInterface::class, $current);
$this
->assertFalse($bundleItem
->hasVariations());
$bundleItem
->setVariations($variations);
$this
->assertTrue($bundleItem
->hasVariations());
$this
->assertEquals($variations[0]
->id(), $bundleItem
->getDefaultVariation()
->id());
$this
->assertEquals($variations[0]
->id(), $bundleItem
->getCurrentVariation()
->id());
$bundleItem
->setCurrentVariation($variations[4]);
$this
->assertEquals($variations[4]
->id(), $bundleItem
->getCurrentVariation()
->id());
$bundleItem
->removeVariation($variations[0]);
$this
->assertNull($bundleItem
->getBundleId());
$bundle = ProductBundle::create([
'type' => 'default',
]);
$bundle
->addBundleItem($bundleItem);
$bundle
->save();
$bundleItem = $this
->reloadEntity($bundleItem);
$this
->assertEquals($bundle
->id(), $bundleItem
->getBundleId());
$bundleItem = ProductBundleItem::create([
'type' => 'default',
]);
$bundleItem
->save();
$bundleItem
->setVariations($variations);
$this
->assertEquals($product
->id(), $bundleItem
->getProduct()
->id());
$freshBundleItem = ProductBundleItem::create([
'type' => 'default',
]);
$bundleItem
->save();
$values = [
'id' => strtolower($this
->randomMachineName(8)),
'label' => $this
->randomMachineName(),
'orderItemType' => 'default',
];
$variationType = $this
->createEntity('commerce_product_variation_type', $values);
$otherVariation = ProductVariation::create([
'type' => $variationType
->getEntityTypeId(),
'sku' => strtolower($this
->randomMachineName()),
'title' => $this
->randomString(),
'status' => $i % 2,
]);
$freshBundleItem
->addVariation($otherVariation);
$this
->assertNull($freshBundleItem
->getProduct());
$this
->assertNull($freshBundleItem
->getVariations());
$this::expectException('\\InvalidArgumentException');
$bundleItem
->addVariation($otherVariation);
$this::expectException('\\InvalidArgumentException');
$bundleItem
->setVariations([
$otherVariation,
]);
$this::expectException('\\InvalidArgumentException');
$bundleItem
->setCurrentVariation($otherVariation);
}
}