-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathCartIntegrationTest.php
More file actions
180 lines (155 loc) · 5.22 KB
/
CartIntegrationTest.php
File metadata and controls
180 lines (155 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Drupal\Tests\commerce_log\Kernel;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
/**
* Tests integration with cart events.
*
* @group commerce
*/
class CartIntegrationTest extends CommerceKernelTestBase {
/**
* The variation to test against.
*
* @var \Drupal\commerce_product\Entity\ProductVariation
*/
protected $variation;
/**
* The cart manager.
*
* @var \Drupal\commerce_cart\CartManagerInterface
*/
protected $cartManager;
/**
* The cart provider.
*
* @var \Drupal\commerce_cart\CartProviderInterface
*/
protected $cartProvider;
/**
* The log storage.
*
* @var \Drupal\commerce_log\LogStorageInterface
*/
protected $logStorage;
/**
* The log view builder.
*
* @var \Drupal\commerce_log\LogViewBuilder
*/
protected $logViewBuilder;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'entity_reference_revisions',
'path',
'profile',
'state_machine',
'commerce_log',
'commerce_product',
'commerce_order',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('profile');
$this->installEntitySchema('commerce_log');
$this->installEntitySchema('commerce_order');
$this->installEntitySchema('commerce_order_item');
$this->installEntitySchema('commerce_product');
$this->installEntitySchema('commerce_product_variation');
$this->installConfig(['commerce_product', 'commerce_order']);
$this->createUser(['mail' => $this->randomString() . '@example.com']);
$this->logStorage = $this->container->get('entity_type.manager')->getStorage('commerce_log');
$this->logViewBuilder = $this->container->get('entity_type.manager')->getViewBuilder('commerce_log');
// Turn off title generation to allow explicit values to be used.
$variation_type = ProductVariationType::load('default');
$variation_type->setGenerateTitle(FALSE);
$variation_type->save();
$product = Product::create([
'type' => 'default',
'title' => 'Default testing product',
]);
$product->save();
$variation1 = ProductVariation::create([
'type' => 'default',
'sku' => 'TEST_' . strtolower($this->randomMachineName()),
'title' => 'Testing product',
'status' => 1,
'price' => new Price('12.00', 'USD'),
]);
$variation1->save();
$product->addVariation($variation1)->save();
$this->variation = $variation1;
}
/**
* Tests that a log is generated when an order is placed.
*/
public function testAddedToCart() {
$this->enableCommerceCart();
$cart = $this->cartProvider->createCart('default', $this->store, $this->createUser());
$this->cartManager->addEntity($cart, $this->variation);
$logs = $this->logStorage->loadByEntity($cart);
$this->assertEquals(1, count($logs));
$log = reset($logs);
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText("{$this->variation->label()} added to the cart.");
}
/**
* Tests that a log is generated when an order is placed.
*/
public function testRemovedFromCart() {
$this->enableCommerceCart();
$cart = $this->cartProvider->createCart('default', $this->store, $this->createUser());
$order_item = $this->cartManager->addEntity($cart, $this->variation);
$this->cartManager->removeOrderItem($cart, $order_item);
$logs = $this->logStorage->loadByEntity($cart);
$this->assertEquals(2, count($logs));
$log = end($logs);
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText("{$this->variation->label()} removed from the cart.");
}
/**
* Tests that a log is generated when a quantity changed for an order item.
*/
public function testQuantityChangedFromCart() {
$this->enableCommerceCart();
$cart = $this->cartProvider->createCart('default', $this->store, $this->createUser());
$order_item = $this->cartManager->addEntity($cart, $this->variation);
$order_item->setQuantity(2);
$this->cartManager->updateOrderItem($cart, $order_item);
$logs = $this->logStorage->loadByEntity($cart);
$log = end($logs);
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText("Quantity of {$this->variation->label()} changed in the cart from 1.0 to 2.");
}
/**
* Enables commerce_cart for tests.
*
* Due to issues with hook_entity_bundle_create, we need to run this here
* and can't put commerce_cart in $modules.
*
* See https://www.drupal.org/node/2711645
*
* @todo patch core so it doesn't explode in Kernel tests.
* @todo remove Cart dependency in Checkout
*/
protected function enableCommerceCart() {
$this->enableModules(['commerce_cart']);
$this->installConfig('commerce_cart');
$this->container->get('entity.definition_update_manager')->applyUpdates();
$this->cartManager = $this->container->get('commerce_cart.cart_manager');
$this->cartProvider = $this->container->get('commerce_cart.cart_provider');
}
}