Skip to content

Commit

Permalink
Issue #2905147 by mglaman: Use order item label for purchased entity …
Browse files Browse the repository at this point in the history
…label
  • Loading branch information
mglaman authored and mglaman committed Sep 9, 2017
1 parent 5ebd762 commit 7507595
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
6 changes: 2 additions & 4 deletions modules/log/src/EventSubscriber/CartEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ public static function getSubscribedEvents() {
*/
public function onCartEntityAdd(CartEntityAddEvent $event) {
$cart = $event->getCart();
$purchased_entity = $event->getOrderItem()->getPurchasedEntity();
$this->logStorage->generate($cart, 'cart_entity_added', [
'purchased_entity_label' => $purchased_entity ? $purchased_entity->label() : NULL,
'purchased_entity_label' => $event->getOrderItem()->label(),
])->save();
}

Expand All @@ -60,9 +59,8 @@ public function onCartEntityAdd(CartEntityAddEvent $event) {
*/
public function onCartOrderItemRemove(CartOrderItemRemoveEvent $event) {
$cart = $event->getCart();
$purchased_entity = $event->getOrderItem()->getPurchasedEntity();
$this->logStorage->generate($cart, 'cart_item_removed', [
'purchased_entity_label' => $purchased_entity ? $purchased_entity->label() : NULL,
'purchased_entity_label' => $event->getOrderItem()->label(),
])->save();
}

Expand Down
62 changes: 62 additions & 0 deletions modules/log/tests/src/Kernel/CartIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Drupal\Tests\commerce_log\Kernel;

use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_order\Entity\OrderItemType;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\ProductVariationType;
Expand Down Expand Up @@ -100,6 +102,13 @@ protected function setUp() {
'status' => 1,
'price' => new Price('12.00', 'USD'),
]);

// An order item type that doesn't need a purchasable entity.
OrderItemType::create([
'id' => 'test',
'label' => 'Test',
'orderType' => 'default',
])->save();
}

/**
Expand All @@ -118,6 +127,31 @@ public function testAddedToCart() {
$this->assertText("{$this->variation->label()} added to the cart.");
}

/**
* Tests that a log is not generated when a non-purchasable entity added.
*
* The cart manager does not fire the `CartEvents::CART_ENTITY_ADD` event
* unless there is a purchasable entity.
*/
public function testAddedToCartNoPurchasableEntity() {
$this->enableCommerceCart();
$cart = $this->cartProvider->createCart('default', $this->store, $this->user);
$order_item = OrderItem::create([
'title' => 'Membership subscription',
'type' => 'test',
'quantity' => 1,
'unit_price' => [
'number' => '10.00',
'currency_code' => 'USD',
],
]);
$order_item->save();
$this->cartManager->addOrderItem($cart, $order_item);

$logs = $this->logStorage->loadByEntity($cart);
$this->assertEquals(0, count($logs));
}

/**
* Tests that a log is generated when an order is placed.
*/
Expand All @@ -136,6 +170,34 @@ public function testRemovedFromCart() {
$this->assertText("{$this->variation->label()} removed from the cart.");
}

/**
* Tests that a log generated when a non-purchasable entity removed.
*/
public function testRemovedFromCartNoPurchasableEntity() {
$this->enableCommerceCart();
$cart = $this->cartProvider->createCart('default', $this->store, $this->user);
$order_item = OrderItem::create([
'title' => 'Membership subscription',
'type' => 'test',
'quantity' => 1,
'unit_price' => [
'number' => '10.00',
'currency_code' => 'USD',
],
]);
$order_item->save();
$order_item = $this->cartManager->addOrderItem($cart, $order_item);
$this->cartManager->removeOrderItem($cart, $order_item);

$logs = $this->logStorage->loadByEntity($cart);
$this->assertEquals(1, count($logs));
$log = end($logs);
$build = $this->logViewBuilder->view($log);
$this->render($build);

$this->assertText("{$order_item->label()} removed from the cart.");
}

/**
* Enables commerce_cart for tests.
*
Expand Down

0 comments on commit 7507595

Please sign in to comment.