-
Notifications
You must be signed in to change notification settings - Fork 257
/
PromotionTest.php
188 lines (155 loc) · 7.95 KB
/
PromotionTest.php
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
<?php
namespace Drupal\Tests\commerce_promotion\FunctionalJavascript;
use Drupal\commerce_promotion\Entity\Promotion;
use Drupal\Tests\commerce\Functional\CommerceBrowserTestBase;
use Drupal\Tests\commerce\FunctionalJavascript\JavascriptTestTrait;
/**
* Create, view, edit, delete, and change promotion entities.
*
* @group commerce
*/
class PromotionTest extends CommerceBrowserTestBase {
use JavascriptTestTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'block',
'path',
'commerce_product',
'commerce_promotion',
];
/**
* {@inheritdoc}
*/
protected function getAdministratorPermissions() {
return array_merge([
'administer commerce_promotion',
], parent::getAdministratorPermissions());
}
/**
* Tests creating a promotion.
*/
public function testCreatePromotion() {
$this->drupalGet('admin/commerce/promotions');
$this->getSession()->getPage()->clickLink('Add a new promotion');
$this->drupalGet('promotion/add');
// Check the integrity of the form.
$this->assertSession()->fieldExists('name[0][value]');
$name = $this->randomMachineName(8);
$this->getSession()->getPage()->fillField('name[0][value]', $name);
$this->getSession()->getPage()->selectFieldOption('offer[0][plugin_select][target_plugin_id]', 'commerce_promotion_product_percentage_off');
$this->waitForAjaxToFinish();
$this->getSession()->getPage()->fillField('offer[0][plugin_select][target_plugin_configuration][form][amount]', '10.0');
// Change, assert any values reset.
$this->getSession()->getPage()->selectFieldOption('offer[0][plugin_select][target_plugin_id]', 'commerce_promotion_order_percentage_off');
$this->waitForAjaxToFinish();
$this->assertSession()->fieldValueNotEquals('offer[0][plugin_select][target_plugin_configuration][form][amount]', '10.0');
$this->getSession()->getPage()->fillField('offer[0][plugin_select][target_plugin_configuration][form][amount]', '10.0');
$this->getSession()->getPage()->selectFieldOption('conditions[0][plugin_select][target_plugin_id]', 'commerce_promotion_order_total_price');
$this->waitForAjaxToFinish();
$this->getSession()->getPage()->fillField('conditions[0][plugin_select][target_plugin_configuration][form][amount][number]', '50.00');
$this->getSession()->getPage()->checkField('conditions[0][plugin_select][target_plugin_configuration][form][negate]');
// Confirm that the usage limit widget works properly.
$this->getSession()->getPage()->hasCheckedField(' Unlimited');
$usage_limit_xpath = '//input[@type="number" and @name="usage_limit[0][usage_limit]"]';
$this->assertFalse($this->getSession()->getDriver()->isVisible($usage_limit_xpath));
$this->getSession()->getPage()->checkField('Limited number of uses');
$this->assertTrue($this->getSession()->getDriver()->isVisible($usage_limit_xpath));
$this->getSession()->getPage()->fillField('usage_limit[0][usage_limit]', '99');
$this->submitForm([], t('Save'));
$this->assertSession()->pageTextContains("Saved the $name promotion.");
$promotion_count = $this->getSession()->getPage()->find('xpath', '//table/tbody/tr/td[text()="' . $name . '"]');
$this->assertEquals(count($promotion_count), 1, 'promotions exists in the table.');
$promotion = Promotion::load(1);
/** @var \Drupal\commerce\Plugin\Field\FieldType\PluginItem $offer_field */
$offer_field = $promotion->get('offer')->first();
$this->assertEquals('0.10', $offer_field->target_plugin_configuration['amount']);
/** @var \Drupal\commerce\Plugin\Field\FieldType\PluginItem $condition_field */
$condition_field = $promotion->get('conditions')->first();
$this->assertEquals('50.00', $condition_field->target_plugin_configuration['amount']['number']);
$this->assertEquals(TRUE, $condition_field->target_plugin_configuration['negate']);
$this->assertEquals('99', $promotion->getUsageLimit());
$this->drupalGet($promotion->toUrl('edit-form'));
$this->getSession()->getPage()->hasCheckedField('Limited number of uses');
$this->assertTrue($this->getSession()->getDriver()->isVisible($usage_limit_xpath));
}
/**
* Tests creating a promotion with an end date.
*/
public function testCreatePromotionWithEndDate() {
$this->drupalGet('admin/commerce/promotions');
$this->getSession()->getPage()->clickLink('Add a new promotion');
$this->drupalGet('promotion/add');
// Check the integrity of the form.
$this->assertSession()->fieldExists('name[0][value]');
$this->getSession()->getPage()->fillField('offer[0][plugin_select][target_plugin_id]', 'commerce_promotion_order_percentage_off');
$this->waitForAjaxToFinish();
$name = $this->randomMachineName(8);
$edit = [
'name[0][value]' => $name,
'offer[0][plugin_select][target_plugin_configuration][form][amount]' => '10.0',
];
$this->getSession()->getPage()->fillField('conditions[0][plugin_select][target_plugin_id]', 'commerce_promotion_order_total_price');
$this->waitForAjaxToFinish();
$edit['conditions[0][plugin_select][target_plugin_configuration][form][amount][number]'] = '50.00';
// Set an end date.
$this->getSession()->getPage()->checkField('end_date[0][has_value]');
$edit['end_date[0][container][value][date]'] = date("Y") + 1 . '-01-01';
$this->submitForm($edit, t('Save'));
$this->assertSession()->pageTextContains("Saved the $name promotion.");
$promotion_count = $this->getSession()->getPage()->find('xpath', '//table/tbody/tr/td[text()="' . $name . '"]');
$this->assertEquals(count($promotion_count), 1, 'promotions exists in the table.');
/** @var \Drupal\commerce\Plugin\Field\FieldType\PluginItem $offer_field */
$offer_field = Promotion::load(1)->get('offer')->first();
$this->assertEquals('0.10', $offer_field->target_plugin_configuration['amount']);
}
/**
* Tests editing a promotion.
*/
public function testEditPromotion() {
$promotion = $this->createEntity('commerce_promotion', [
'name' => $this->randomMachineName(8),
'status' => TRUE,
'offer' => [
'target_plugin_id' => 'commerce_promotion_product_percentage_off',
'target_plugin_configuration' => [
'amount' => '0.10',
],
],
]);
/** @var \Drupal\commerce\Plugin\Field\FieldType\PluginItem $offer_field */
$offer_field = $promotion->get('offer')->first();
$this->assertEquals('0.10', $offer_field->target_plugin_configuration['amount']);
$this->drupalGet($promotion->toUrl('edit-form'));
$new_promotion_name = $this->randomMachineName(8);
$edit = [
'name[0][value]' => $new_promotion_name,
'offer[0][plugin_select][target_plugin_configuration][form][amount]' => '20',
];
$this->submitForm($edit, 'Save');
\Drupal::service('entity_type.manager')->getStorage('commerce_promotion')->resetCache([$promotion->id()]);
$promotion_changed = Promotion::load($promotion->id());
$this->assertEquals($new_promotion_name, $promotion_changed->getName(), 'The promotion name successfully updated.');
/** @var \Drupal\commerce\Plugin\Field\FieldType\PluginItem $offer_field */
$offer_field = $promotion_changed->get('offer')->first();
$this->assertEquals('0.20', $offer_field->target_plugin_configuration['amount']);
}
/**
* Tests deleting a promotion.
*/
public function testDeletePromotion() {
$promotion = $this->createEntity('commerce_promotion', [
'name' => $this->randomMachineName(8),
]);
$this->drupalGet($promotion->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_promotion')->resetCache([$promotion->id()]);
$promotion_exists = (bool) Promotion::load($promotion->id());
$this->assertEmpty($promotion_exists, 'The new promotion has been deleted from the database using UI.');
}
}