5
5
use Drupal \commerce_order \Entity \OrderItem ;
6
6
use Drupal \commerce_order \Entity \OrderItemType ;
7
7
use Drupal \commerce_order \Entity \Order ;
8
+ use Drupal \commerce_price \Price ;
8
9
use Drupal \commerce_promotion \Entity \Promotion ;
9
10
use Drupal \Tests \commerce \Kernel \CommerceKernelTestBase ;
10
11
@@ -141,12 +142,17 @@ public function testOrderCondition() {
141
142
$ this ->order ->save ();
142
143
$ result = $ promotion ->applies ($ this ->order );
143
144
$ this ->assertTrue ($ result );
145
+
146
+ // No order total can satisfy both conditions.
147
+ $ promotion ->setConditionOperator ('AND ' );
148
+ $ result = $ promotion ->applies ($ this ->order );
149
+ $ this ->assertFalse ($ result );
144
150
}
145
151
146
152
/**
147
- * Tests promotions with an order item condition .
153
+ * Tests promotions with both order and order item conditions .
148
154
*/
149
- public function testOrderItemCondition () {
155
+ public function testMixedCondition () {
150
156
// Starts now, enabled. No end time.
151
157
$ promotion = Promotion::create ([
152
158
'name ' => 'Promotion 1 ' ,
@@ -165,7 +171,7 @@ public function testOrderItemCondition() {
165
171
'target_plugin_configuration ' => [
166
172
'operator ' => '> ' ,
167
173
'amount ' => [
168
- 'number ' => '10 .00 ' ,
174
+ 'number ' => '30 .00 ' ,
169
175
'currency_code ' => 'USD ' ,
170
176
],
171
177
],
@@ -174,7 +180,7 @@ public function testOrderItemCondition() {
174
180
'target_plugin_id ' => 'order_item_quantity ' ,
175
181
'target_plugin_configuration ' => [
176
182
'operator ' => '> ' ,
177
- 'quantity ' => 2 ,
183
+ 'quantity ' => 1 ,
178
184
],
179
185
],
180
186
],
@@ -184,31 +190,62 @@ public function testOrderItemCondition() {
184
190
185
191
$ order_item = OrderItem::create ([
186
192
'type ' => 'test ' ,
187
- 'quantity ' => 2 ,
193
+ 'quantity ' => 4 ,
188
194
'unit_price ' => [
189
- 'number ' => '20 .00 ' ,
195
+ 'number ' => '10 .00 ' ,
190
196
'currency_code ' => 'USD ' ,
191
197
],
192
198
]);
193
199
$ order_item ->save ();
200
+
201
+ // AND: Both conditions apply.
194
202
$ this ->order ->addItem ($ order_item );
195
203
$ this ->order ->save ();
204
+ $ result = $ promotion ->applies ($ this ->order );
205
+ $ this ->assertTrue ($ result );
206
+
207
+ // OR: Both conditions apply.
208
+ $ promotion ->setConditionOperator ('OR ' );
209
+ $ result = $ promotion ->applies ($ this ->order );
210
+ $ this ->assertTrue ($ result );
196
211
212
+ // AND: Neither condition applies.
213
+ $ order_item ->setQuantity (1 );
214
+ $ order_item ->save ();
215
+ $ this ->order ->save ();
216
+ $ promotion ->setConditionOperator ('AND ' );
197
217
$ result = $ promotion ->applies ($ this ->order );
198
218
$ this ->assertFalse ($ result );
199
219
200
- $ order_item = OrderItem::create ([
201
- 'type ' => 'test ' ,
202
- 'quantity ' => 4 ,
203
- 'unit_price ' => [
204
- 'number ' => '20.00 ' ,
205
- 'currency_code ' => 'USD ' ,
206
- ],
207
- ]);
220
+ // OR: Neither condition applies.
221
+ $ promotion ->setConditionOperator ('OR ' );
222
+ $ result = $ promotion ->applies ($ this ->order );
223
+ $ this ->assertFalse ($ result );
224
+
225
+ // AND: Order condition fails, order item condition passes.
226
+ $ order_item ->setQuantity (2 );
227
+ $ order_item ->save ();
228
+ $ this ->order ->save ();
229
+ $ promotion ->setConditionOperator ('AND ' );
230
+ $ result = $ promotion ->applies ($ this ->order );
231
+ $ this ->assertFalse ($ result );
232
+
233
+ // OR: Order condition fails, order item condition passes.
234
+ $ promotion ->setConditionOperator ('OR ' );
235
+ $ result = $ promotion ->applies ($ this ->order );
236
+ $ this ->assertTrue ($ result );
237
+
238
+ // AND: Order condition passes, order item condition fails.
239
+ $ order_item ->setUnitPrice (new Price ('40 ' , 'USD ' ));
240
+ $ order_item ->setQuantity (1 );
208
241
$ order_item ->save ();
209
- $ this ->order ->addItem ($ order_item );
210
242
$ this ->order ->save ();
243
+ $ promotion ->setConditionOperator ('AND ' );
244
+ $ result = $ promotion ->applies ($ this ->order );
245
+ $ this ->assertFalse ($ result );
211
246
247
+ // OR: Order condition passes, order item condition fails.
248
+ $ promotion ->setConditionOperator ('OR ' );
212
249
$ result = $ promotion ->applies ($ this ->order );
213
250
$ this ->assertTrue ($ result );
214
251
}
0 commit comments