-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathOrderAdminTest.php
More file actions
267 lines (238 loc) · 10.1 KB
/
OrderAdminTest.php
File metadata and controls
267 lines (238 loc) · 10.1 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
namespace Drupal\Tests\commerce_order\Functional;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_price\Price;
use Drupal\profile\Entity\Profile;
/**
* Tests the commerce_order entity forms.
*
* @group commerce
*/
class OrderAdminTest extends OrderBrowserTestBase {
/**
* The profile to test against.
*
* @var \Drupal\profile\Entity\Profile
*/
protected $billingProfile;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
\Drupal::service('module_installer')->install(['profile']);
$profile_values = [
'type' => 'customer',
'uid' => 1,
'status' => 1,
];
$this->billingProfile = Profile::create($profile_values);
$this->billingProfile->save();
}
/**
* Tests creating/editing an Order.
*
* @group failing
*/
public function testCreateOrder() {
// Create an order through the add form.
$this->drupalGet('/admin/commerce/orders');
$this->getSession()->getPage()->clickLink('Create a new order');
$user = $this->loggedInUser->getAccountName() . ' (' . $this->loggedInUser->id() . ')';
$edit = [
'customer_type' => 'existing',
'uid' => $user,
];
$this->submitForm($edit, t('Create'));
// Check the integrity of the edit form.
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->fieldExists('billing_profile[0][profile][address][0][address][given_name]');
$this->assertSession()->fieldExists('order_items[form][inline_entity_form][purchased_entity][0][target_id]');
$this->assertSession()->fieldExists('order_items[form][inline_entity_form][quantity][0][value]');
$this->assertSession()->fieldExists('order_items[form][inline_entity_form][unit_price][0][amount][number]');
$this->assertSession()->buttonExists('Create order item');
$entity = $this->variation->getSku() . ' (' . $this->variation->id() . ')';
// Test that commerce_order_test_field_widget_form_alter() has the expected
// outcome.
$this->assertSame([], \Drupal::state()->get("commerce_order_test_field_widget_form_alter"));
$checkbox = $this->getSession()->getPage()->findField('Override the unit price');
if ($checkbox) {
$checkbox->check();
}
$edit = [
'order_items[form][inline_entity_form][purchased_entity][0][target_id]' => $entity,
'order_items[form][inline_entity_form][quantity][0][value]' => '1',
'order_items[form][inline_entity_form][unit_price][0][amount][number]' => '9.99',
];
$this->submitForm($edit, 'Create order item');
$this->submitForm([], t('Edit'));
$this->assertSession()->fieldExists('order_items[form][inline_entity_form][entities][0][form][purchased_entity][0][target_id]');
$this->assertSession()->fieldExists('order_items[form][inline_entity_form][entities][0][form][quantity][0][value]');
$this->assertSession()->fieldExists('order_items[form][inline_entity_form][entities][0][form][unit_price][0][amount][number]');
$this->assertSession()->buttonExists('Update order item');
$checkbox = $this->getSession()->getPage()->findField('Override the unit price');
if ($checkbox) {
$checkbox->check();
}
$edit = [
'order_items[form][inline_entity_form][entities][0][form][quantity][0][value]' => 3,
'order_items[form][inline_entity_form][entities][0][form][unit_price][0][amount][number]' => '1.11',
];
$this->submitForm($edit, 'Update order item');
$edit = [
'billing_profile[0][profile][address][0][address][given_name]' => 'John',
'billing_profile[0][profile][address][0][address][family_name]' => 'Smith',
'billing_profile[0][profile][address][0][address][address_line1]' => '123 street',
'billing_profile[0][profile][address][0][address][postal_code]' => '94043',
'billing_profile[0][profile][address][0][address][locality]' => 'Mountain View',
'billing_profile[0][profile][address][0][address][administrative_area]' => 'CA',
];
// There is no adjustment - the order should save successfully.
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('The order has been successfully saved.');
// Use an adjustment that is not locked by default.
$this->clickLink('Edit');
$edit = [
'adjustments[0][type]' => 'fee',
'adjustments[0][definition][label]' => '',
'adjustments[0][definition][amount][number]' => '2.00',
];
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('The adjustment label field is required.');
$edit['adjustments[0][definition][label]'] = 'Test fee';
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('The order has been successfully saved.');
$this->drupalGet('/admin/commerce/orders');
$order_number = $this->getSession()->getPage()->find('css', 'tr td.views-field-order-number');
$this->assertEquals(1, count($order_number), 'Order exists in the table.');
$order = Order::load(1);
$this->assertEquals(1, count($order->getItems()));
$this->assertEquals(new Price('5.33', 'USD'), $order->getTotalPrice());
$this->assertCount(1, $order->getAdjustments());
}
/**
* Tests editing an order.
*/
public function testEditOrder() {
$order = Order::create([
'type' => 'default',
'state' => 'completed',
'uid' => $this->loggedInUser,
'store_id' => $this->store,
]);
$order->save();
$adjustments = [];
$adjustments[] = new Adjustment([
'type' => 'custom',
'label' => '10% off',
'amount' => new Price('-1.00', 'USD'),
'percentage' => '0.1',
]);
$adjustments[] = new Adjustment([
'type' => 'custom',
'label' => 'Handling fee',
'amount' => new Price('10.00', 'USD'),
]);
$order->addAdjustment($adjustments[0]);
$order->addAdjustment($adjustments[1]);
$order->save();
$this->drupalGet($order->toUrl('edit-form'));
$this->assertSession()->fieldValueEquals('adjustments[0][definition][label]', '10% off');
$this->assertSession()->fieldValueEquals('adjustments[1][definition][label]', 'Handling fee');
$this->assertSession()->optionExists('adjustments[2][type]', 'Custom');
$this->assertSession()->optionNotExists('adjustments[2][type]', 'Test order adjustment type');
}
/**
* Tests deleting an order.
*/
public function testDeleteOrder() {
$order = $this->createEntity('commerce_order', [
'type' => 'default',
'mail' => $this->loggedInUser->getEmail(),
'uid' => $this->loggedInUser,
'store_id' => $this->store,
]);
$this->drupalGet($order->toUrl('delete-form'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains(t('Are you sure you want to delete the order @label?', [
'@label' => $order->label(),
]));
$this->assertSession()->pageTextContains('This action cannot be undone.');
$this->submitForm([], t('Delete'));
\Drupal::service('entity_type.manager')->getStorage('commerce_order')->resetCache([$order->id()]);
$order_exists = (bool) Order::load($order->id());
$this->assertEmpty($order_exists, 'The order has been deleted from the database.');
}
/**
* Tests unlocking an order.
*/
public function testUnlockOrder() {
$order = $this->createEntity('commerce_order', [
'type' => 'default',
'mail' => $this->loggedInUser->getEmail(),
'uid' => $this->loggedInUser,
'store_id' => $this->store,
'locked' => TRUE,
]);
$this->drupalGet($order->toUrl('unlock-form'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains(t('Are you sure you want to unlock the order @label?', [
'@label' => $order->label(),
]));
$this->submitForm([], t('Unlock'));
\Drupal::service('entity_type.manager')->getStorage('commerce_order')->resetCache([$order->id()]);
$order = Order::load($order->id());
$this->assertFalse($order->isLocked());
}
/**
* Tests that an admin can view an order's details.
*/
public function testAdminOrderView() {
// Start from an order without any order items.
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $this->createEntity('commerce_order', [
'type' => 'default',
'store_id' => $this->store->id(),
'mail' => $this->loggedInUser->getEmail(),
'state' => 'draft',
'uid' => $this->loggedInUser,
]);
// First test that the current admin user can see the order.
$this->drupalGet($order->toUrl()->toString());
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($this->loggedInUser->getEmail());
// Confirm that the order item table is showing the empty text.
$this->assertSession()->pageTextContains('There are no order items yet.');
$this->assertSession()->pageTextNotContains('Subtotal');
// Confirm that the transition buttons are visible and functional.
$workflow = $order->getState()->getWorkflow();
$transitions = $workflow->getAllowedTransitions($order->getState()->value, $order);
foreach ($transitions as $transition) {
$this->assertSession()->buttonExists($transition->getLabel());
}
$this->click('input.js-form-submit#edit-place');
$this->assertSession()->buttonNotExists('Place order');
$this->assertSession()->buttonNotExists('Cancel order');
// Add an order item, confirm that it is displayed.
$order_item = $this->createEntity('commerce_order_item', [
'type' => 'default',
'unit_price' => [
'number' => '999',
'currency_code' => 'USD',
],
]);
$order->setItems([$order_item]);
$order->save();
$this->drupalGet($order->toUrl()->toString());
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextNotContains('There are no order items yet.');
$this->assertSession()->pageTextContains('$999.00');
$this->assertSession()->pageTextContains('Subtotal');
// Logout and check that anonymous users cannot see the order admin screen
// and receive a 403 error code.
$this->drupalLogout();
$this->drupalGet($order->toUrl()->toString());
$this->assertSession()->statusCodeEquals(403);
}
}