PurchasedEntitySaveTest.php in Commerce Pricelist 8
File
tests/src/Kernel/PurchasedEntitySaveTest.php
View source
<?php
namespace Drupal\Tests\commerce_pricelist\Kernel;
use Drupal\commerce_pricelist\Entity\PriceList;
use Drupal\commerce_pricelist\Entity\PriceListItem;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
class PurchasedEntitySaveTest extends PriceListKernelTestBase {
public function testPurchasedEntitySave() {
$variationa = ProductVariation::create([
'type' => 'default',
]);
$variationa
->save();
$variationb = ProductVariation::create([
'type' => 'default',
]);
$variationb
->save();
$product = Product::create([
'type' => 'default',
'title' => 'My Product Title',
'variations' => [
$variationa,
$variationb,
],
]);
$product
->save();
$priceListItem = PriceListItem::create([
'type' => 'default',
'purchased_entity' => $variationb,
]);
$priceListItem
->save();
$this
->assertEquals(NULL, $priceListItem
->getName());
$variationb
->set('field_price_list_item', [
$priceListItem,
]);
$variationb
->save();
$this
->assertEquals($variationb
->getTitle(), $priceListItem
->getName());
$priceList = PriceList::create([
'type' => 'default',
'title' => 'My Price list Title',
'field_price_list_item' => [
$priceListItem,
],
]);
$priceList
->save();
$product
->setTitle('Llama');
$variationb
->save();
$this
->assertEquals('My Product Title', $priceListItem
->getName());
$product
->save();
$this
->assertEquals($variationb
->id(), $priceListItem
->getPurchasedEntity()
->id());
}
}