View source
<?php
namespace Drupal\Tests\commerce_wishlist\Functional;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\commerce_wishlist\Entity\WishlistItem;
use Drupal\Core\Url;
class WishlistItemTest extends WishlistBrowserTestBase {
protected $wishList;
protected $firstVariation;
protected $secondVariation;
protected $wishListItemCollectionUri;
protected function setUp() : void {
parent::setUp();
$variation_type = ProductVariationType::load('default');
$variation_type
->setGenerateTitle(FALSE);
$variation_type
->save();
$this->wishList = $this
->createEntity('commerce_wishlist', [
'type' => 'default',
'name' => 'Secret gifts',
'public' => TRUE,
'keep_purchased_items' => TRUE,
]);
$this->firstVariation = $this
->createEntity('commerce_product_variation', [
'type' => 'default',
'sku' => 'S-DESK',
'title' => 'Standing desk',
'price' => new Price('120.00', 'USD'),
]);
$this->secondVariation = $this
->createEntity('commerce_product_variation', [
'type' => 'default',
'sku' => 'R-DESK',
'title' => 'Regular desk',
'price' => new Price('70.00', 'USD'),
]);
$this
->createEntity('commerce_product', [
'type' => 'default',
'title' => $this
->randomMachineName(),
'stores' => [
$this->store,
],
'variations' => [
$this->firstVariation,
$this->secondVariation,
],
]);
$this->wishListItemCollectionUri = Url::fromRoute('entity.commerce_wishlist_item.collection', [
'commerce_wishlist' => $this->wishList
->id(),
])
->toString();
}
public function testAdd() {
$this
->drupalGet($this->wishListItemCollectionUri);
$this
->clickLink('Add item');
$this
->submitForm([
'purchasable_entity[0][target_id]' => 'Standing desk (1)',
'quantity[0][value]' => '1',
'comment[0][value]' => 'Love this desk',
], 'Save');
$this
->assertSession()
->pageTextContains('The item Standing desk has been successfully saved.');
$wishlist_item = WishlistItem::load(1);
$this
->assertEquals($this->wishList
->id(), $wishlist_item
->getWishListId());
$this
->assertEquals($this->firstVariation
->id(), $wishlist_item
->getPurchasableEntityId());
$this
->assertEquals('1', $wishlist_item
->getQuantity());
$this
->assertNotEmpty($wishlist_item
->getComment());
}
public function testEdit() {
$wishlist_item = $this
->createEntity('commerce_wishlist_item', [
'type' => 'commerce_product_variation',
'wishlist_id' => $this->wishList
->id(),
'purchasable_entity' => $this->firstVariation
->id(),
'quantity' => '1',
]);
$this
->drupalGet($wishlist_item
->toUrl('edit-form'));
$this
->submitForm([
'purchasable_entity[0][target_id]' => 'Regular desk (2)',
'quantity[0][value]' => '5',
'comment[0][value]' => 'My updated comment',
], 'Save');
$this
->assertSession()
->pageTextNotContains('The item Regular desk has been successfully saved.');
\Drupal::service('entity_type.manager')
->getStorage('commerce_wishlist_item')
->resetCache([
$wishlist_item
->id(),
]);
$wishlist_item = WishlistItem::load(1);
$this
->assertNotEquals($this->secondVariation
->id(), $wishlist_item
->getPurchasableEntityId());
$this
->assertEqual($wishlist_item
->getComment(), 'My updated comment');
$this
->assertEquals('5', $wishlist_item
->getQuantity());
}
public function testDuplicate() {
$wishlist_item = $this
->createEntity('commerce_wishlist_item', [
'type' => 'commerce_product_variation',
'wishlist_id' => $this->wishList
->id(),
'purchasable_entity' => $this->firstVariation
->id(),
'quantity' => '2',
]);
$this
->drupalGet($wishlist_item
->toUrl('duplicate-form'));
$this
->assertSession()
->pageTextContains('Duplicate Standing desk');
$this
->submitForm([
'purchasable_entity[0][target_id]' => 'Regular desk (2)',
'quantity[0][value]' => '5',
'comment[0][value]' => 'Still regular is cheaper',
], 'Save');
$this
->assertSession()
->pageTextContains('The item Regular desk has been successfully saved.');
\Drupal::service('entity_type.manager')
->getStorage('commerce_wishlist_item')
->resetCache([
$wishlist_item
->id(),
]);
$wishlist_item_1 = WishlistItem::load(1);
$this
->assertEquals($this->wishList
->id(), $wishlist_item_1
->getWishListId());
$this
->assertEquals($this->firstVariation
->id(), $wishlist_item_1
->getPurchasableEntityId());
$this
->assertEquals('2', $wishlist_item_1
->getQuantity());
$this
->assertEmpty($wishlist_item_1
->getComment());
$wishlist_item_2 = WishlistItem::load(2);
$this
->assertEquals($this->wishList
->id(), $wishlist_item_2
->getWishListId());
$this
->assertEquals($this->secondVariation
->id(), $wishlist_item_2
->getPurchasableEntityId());
$this
->assertEquals('5', $wishlist_item_2
->getQuantity());
$this
->assertNotEmpty($wishlist_item_2
->getComment());
$this
->assertEqual($wishlist_item_2
->getComment(), 'Still regular is cheaper');
}
public function testDelete() {
$wishlist_item = $this
->createEntity('commerce_wishlist_item', [
'type' => 'commerce_product_variation',
'wishlist_id' => $this->wishList
->id(),
'purchasable_entity' => $this->firstVariation
->id(),
'quantity' => '1',
'comment' => 'You are gonna delete me',
]);
$this
->drupalGet($wishlist_item
->toUrl('delete-form'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('This action cannot be undone.');
$this
->submitForm([], t('Delete'));
\Drupal::service('entity_type.manager')
->getStorage('commerce_wishlist_item')
->resetCache([
$wishlist_item
->id(),
]);
$wishlist_item_exists = (bool) WishlistItem::load($wishlist_item
->id());
$this
->assertFalse($wishlist_item_exists);
}
}