22
33namespace Drupal \Tests \commerce_log \Kernel ;
44
5+ use Drupal \commerce_order \Entity \OrderItem ;
6+ use Drupal \commerce_order \Entity \OrderItemType ;
57use Drupal \commerce_price \Price ;
68use Drupal \commerce_product \Entity \ProductVariation ;
79use Drupal \commerce_product \Entity \ProductVariationType ;
@@ -100,6 +102,13 @@ protected function setUp() {
100102 'status ' => 1 ,
101103 'price ' => new Price ('12.00 ' , 'USD ' ),
102104 ]);
105+
106+ // An order item type that doesn't need a purchasable entity.
107+ OrderItemType::create ([
108+ 'id ' => 'test ' ,
109+ 'label ' => 'Test ' ,
110+ 'orderType ' => 'default ' ,
111+ ])->save ();
103112 }
104113
105114 /**
@@ -118,6 +127,31 @@ public function testAddedToCart() {
118127 $ this ->assertText ("{$ this ->variation ->label ()} added to the cart. " );
119128 }
120129
130+ /**
131+ * Tests that a log is not generated when a non-purchasable entity added.
132+ *
133+ * The cart manager does not fire the `CartEvents::CART_ENTITY_ADD` event
134+ * unless there is a purchasable entity.
135+ */
136+ public function testAddedToCartNoPurchasableEntity () {
137+ $ this ->enableCommerceCart ();
138+ $ cart = $ this ->cartProvider ->createCart ('default ' , $ this ->store , $ this ->user );
139+ $ order_item = OrderItem::create ([
140+ 'title ' => 'Membership subscription ' ,
141+ 'type ' => 'test ' ,
142+ 'quantity ' => 1 ,
143+ 'unit_price ' => [
144+ 'number ' => '10.00 ' ,
145+ 'currency_code ' => 'USD ' ,
146+ ],
147+ ]);
148+ $ order_item ->save ();
149+ $ this ->cartManager ->addOrderItem ($ cart , $ order_item );
150+
151+ $ logs = $ this ->logStorage ->loadByEntity ($ cart );
152+ $ this ->assertEquals (0 , count ($ logs ));
153+ }
154+
121155 /**
122156 * Tests that a log is generated when an order is placed.
123157 */
@@ -136,6 +170,34 @@ public function testRemovedFromCart() {
136170 $ this ->assertText ("{$ this ->variation ->label ()} removed from the cart. " );
137171 }
138172
173+ /**
174+ * Tests that a log generated when a non-purchasable entity removed.
175+ */
176+ public function testRemovedFromCartNoPurchasableEntity () {
177+ $ this ->enableCommerceCart ();
178+ $ cart = $ this ->cartProvider ->createCart ('default ' , $ this ->store , $ this ->user );
179+ $ order_item = OrderItem::create ([
180+ 'title ' => 'Membership subscription ' ,
181+ 'type ' => 'test ' ,
182+ 'quantity ' => 1 ,
183+ 'unit_price ' => [
184+ 'number ' => '10.00 ' ,
185+ 'currency_code ' => 'USD ' ,
186+ ],
187+ ]);
188+ $ order_item ->save ();
189+ $ order_item = $ this ->cartManager ->addOrderItem ($ cart , $ order_item );
190+ $ this ->cartManager ->removeOrderItem ($ cart , $ order_item );
191+
192+ $ logs = $ this ->logStorage ->loadByEntity ($ cart );
193+ $ this ->assertEquals (1 , count ($ logs ));
194+ $ log = end ($ logs );
195+ $ build = $ this ->logViewBuilder ->view ($ log );
196+ $ this ->render ($ build );
197+
198+ $ this ->assertText ("{$ order_item ->label ()} removed from the cart. " );
199+ }
200+
139201 /**
140202 * Enables commerce_cart for tests.
141203 *
0 commit comments