View source
<?php
namespace Drupal\Tests\commerce_wishlist\Kernel\Entity;
use Drupal\commerce_wishlist\Entity\Wishlist;
use Drupal\commerce_wishlist\WishlistPurchase;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\Tests\commerce_cart\Kernel\CartKernelTestBase;
class WishlistItemTest extends CartKernelTestBase {
public static $modules = [
'commerce_wishlist',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('commerce_wishlist');
$this
->installEntitySchema('commerce_wishlist_item');
$this
->installConfig([
'commerce_wishlist',
]);
}
public function testWishlistItem() {
$product = Product::create([
'type' => 'default',
'title' => 'My Product Title',
]);
$product
->save();
$variation = ProductVariation::create([
'type' => 'default',
'product_id' => $product
->id(),
]);
$variation
->save();
$variation = $this
->reloadEntity($variation);
$wishlist_item_storage = $this->container
->get('entity_type.manager')
->getStorage('commerce_wishlist_item');
$wishlist = Wishlist::create([
'type' => 'default',
'state' => 'completed',
]);
$wishlist_item = $wishlist_item_storage
->createFromPurchasableEntity($variation, [
'quantity' => 2,
]);
$wishlist_item
->save();
$wishlist
->setItems([
$wishlist_item,
]);
$wishlist
->save();
$wishlist_item = $this
->reloadEntity($wishlist_item);
$this
->assertEquals($wishlist_item
->getWishlistId(), $wishlist
->id());
$this
->assertEquals($wishlist_item
->getWishlist()
->id(), $wishlist
->id());
$this
->assertEquals($wishlist_item
->getPurchasableEntity()
->id(), $variation
->id());
$this
->assertEquals($wishlist_item
->getPurchasableEntityId(), $variation
->id());
$this
->assertEquals($wishlist_item
->getTitle(), $variation
->label());
$this
->assertEquals($wishlist_item
->getQuantity(), 2);
$wishlist_item
->setQuantity(3);
$this
->assertEquals($wishlist_item
->getQuantity(), 3);
$wishlist_item
->setComment('My comment');
$this
->assertEquals($wishlist_item
->getComment(), 'My comment');
$wishlist_item
->setPriority(100);
$this
->assertEquals($wishlist_item
->getPriority(), 100);
$time = 635879700;
$this
->assertNull($wishlist_item
->getLastPurchasedTime());
$purchase = new WishlistPurchase(10, 2, $time + 10);
$wishlist_item
->setPurchases([
$purchase,
]);
$purchases = $wishlist_item
->getPurchases();
$this
->assertEquals([
$purchase,
], $purchases);
$this
->assertEquals(2, $wishlist_item
->getPurchasedQuantity());
$this
->assertEquals($time + 10, $wishlist_item
->getLastPurchasedTime());
$another_purchase = new WishlistPurchase(11, 3, $time + 5);
$wishlist_item
->addPurchase($another_purchase);
$purchases = $wishlist_item
->getPurchases();
$this
->assertEquals([
$purchase,
$another_purchase,
], $purchases);
$this
->assertEquals(5, $wishlist_item
->getPurchasedQuantity());
$this
->assertEquals($time + 10, $wishlist_item
->getLastPurchasedTime());
$wishlist_item
->removePurchase($purchase);
$purchases = $wishlist_item
->getPurchases();
$this
->assertEquals([
$another_purchase,
], $purchases);
$this
->assertEquals(3, $wishlist_item
->getPurchasedQuantity());
$this
->assertEquals($time + 5, $wishlist_item
->getLastPurchasedTime());
$wishlist_item
->setCreatedTime($time);
$this
->assertEquals($wishlist_item
->getCreatedTime(), $time);
}
}