-
Notifications
You must be signed in to change notification settings - Fork 475
/
tests-cart.php
628 lines (473 loc) · 14.6 KB
/
tests-cart.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
<?php
/**
* @group edd_cart
*/
class Test_Cart extends EDD_UnitTestCase {
protected $_rewrite = null;
protected $_post = null;
protected $_discount = null;
public function setUp() {
parent::setUp();
$this->_user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
wp_set_current_user( $this->_user_id );
global $wp_rewrite;
$GLOBALS['wp_rewrite']->init();
flush_rewrite_rules( false );
edd_add_rewrite_endpoints($wp_rewrite);
$this->_rewrite = $wp_rewrite;
global $current_user;
$current_user = new WP_User(1);
$current_user->set_role('administrator');
$post_id = $this->factory->post->create( array( 'post_title' => 'Test Download', 'post_type' => 'download', 'post_status' => 'publish' ) );
$_variable_pricing = array(
array(
'name' => 'Simple',
'amount' => 20
),
array(
'name' => 'Advanced',
'amount' => 100
)
);
$_download_files = array(
array(
'name' => 'File 1',
'file' => 'http://localhost/file1.jpg',
'condition' => 0
),
array(
'name' => 'File 2',
'file' => 'http://localhost/file2.jpg',
'condition' => 'all'
)
);
$meta = array(
'edd_price' => '0.00',
'_variable_pricing' => 1,
'_edd_price_options_mode' => 'on',
'edd_variable_prices' => array_values( $_variable_pricing ),
'edd_download_files' => array_values( $_download_files ),
'_edd_download_limit' => 20,
'_edd_hide_purchase_link' => 1,
'edd_product_notes' => 'Purchase Notes',
'_edd_product_type' => 'default',
'_edd_download_earnings' => 129.43,
'_edd_download_sales' => 59,
'_edd_download_limit_override_1' => 1
);
foreach( $meta as $key => $value ) {
update_post_meta( $post_id, $key, $value );
}
$this->_post = get_post( $post_id );
$discount = array(
'code' => '20OFF',
'uses' => 54,
'max' => 10,
'name' => '20 Percent Off',
'type' => 'percent',
'amount' => '20',
'start' => '12/12/2010 00:00:00',
'expiration' => '12/31/2050 00:00:00',
'min_price' => 128,
'status' => 'active',
'product_condition' => 'all'
);
$this->_discount = edd_store_discount( $discount );
}
public function tearDown() {
parent::tearDown();
edd_empty_cart();
}
public function test_endpoints() {
$this->assertEquals('edd-add', $this->_rewrite->endpoints[0][1]);
$this->assertEquals('edd-remove', $this->_rewrite->endpoints[1][1]);
}
public function test_add_to_cart() {
$options = array(
'price_id' => 0
);
$this->assertEquals( 0, edd_add_to_cart( $this->_post->ID, $options ) );
}
public function test_empty_cart_is_array() {
$cart_contents = edd_get_cart_contents();
$this->assertInternalType( 'array', $cart_contents );
$this->assertEmpty( $cart_contents );
}
public function test_add_to_cart_multiple_price_ids_array() {
$options = array(
'price_id' => array( 0, 1 )
);
edd_add_to_cart( $this->_post->ID, $options );
$this->assertEquals( 2, count( edd_get_cart_contents() ) );
}
public function test_add_to_cart_multiple_price_ids_array_with_quantity() {
add_filter( 'edd_item_quantities_enabled', '__return_true' );
$options = array(
'price_id' => array( 0, 1 ),
'quantity' => array( 2, 3 ),
);
edd_add_to_cart( $this->_post->ID, $options );
$this->assertEquals( 2, count( edd_get_cart_contents() ) );
$this->assertEquals( 2, edd_get_cart_item_quantity( $this->_post->ID, array( 'price_id' => 0 ) ) );
$this->assertEquals( 3, edd_get_cart_item_quantity( $this->_post->ID, array( 'price_id' => 1 ) ) );
remove_filter( 'edd_item_quantities_enabled', '__return_true' );
}
public function test_add_to_cart_multiple_price_ids_string() {
$options = array(
'price_id' => '0,1'
);
edd_add_to_cart( $this->_post->ID, $options );
$this->assertEquals( 2, count( edd_get_cart_contents() ) );
}
public function test_get_cart_contents() {
$options = array(
'price_id' => 0
);
edd_add_to_cart( $this->_post->ID, $options );
$expected = array(
'0' => array(
'id' => $this->_post->ID,
'options' => array(
'price_id' => 0
),
'quantity' => 1
)
);
$this->assertEquals($expected, edd_get_cart_contents());
}
public function test_get_cart_content_details() {
$options = array(
'price_id' => 0
);
edd_add_to_cart( $this->_post->ID, $options );
$expected = array(
'0' => array(
'name' => 'Test Download',
'id' => $this->_post->ID,
'item_number' => array(
'options' => array(
'price_id' => '0'
),
'id' => $this->_post->ID,
'quantity' => 1,
),
'item_price' => '20.0',
'quantity' => 1,
'discount' => '0.0',
'subtotal' => '20.0',
'tax' => 0,
'fees' => array(),
'price' => '20.0'
)
);
$this->assertEquals( $expected, edd_get_cart_content_details() );
// Now set a discount and test again
edd_set_cart_discount( '20OFF' );
$expected = array(
'0' => array(
'name' => 'Test Download',
'id' => $this->_post->ID,
'item_number' => array(
'options' => array(
'price_id' => '0'
),
'id' => $this->_post->ID,
'quantity' => 1,
),
'item_price' => '20.0',
'quantity' => 1,
'discount' => '4.0',
'subtotal' => '20.0',
'tax' => 0,
'fees' => array(),
'price' => '16.0'
)
);
$this->assertEquals( $expected, edd_get_cart_content_details() );
// Now turn on taxes and do it again
add_filter( 'edd_use_taxes', '__return_true' );
add_filter( 'edd_tax_rate', function() {
return 0.20;
} );
$expected = array(
'0' => array(
'name' => 'Test Download',
'id' => $this->_post->ID,
'item_number' => array(
'options' => array(
'price_id' => '0'
),
'id' => $this->_post->ID,
'quantity' => 1,
),
'item_price' => '20.0',
'quantity' => 1,
'discount' => '4.0',
'subtotal' => '20.0',
'tax' => '3.2',
'fees' => array(),
'price' => '19.2'
)
);
$this->assertEquals( $expected, edd_get_cart_content_details() );
// Now remove the discount code and test with taxes again
edd_unset_cart_discount( '20OFF' );
$expected = array(
'0' => array(
'name' => 'Test Download',
'id' => $this->_post->ID,
'item_number' => array(
'options' => array(
'price_id' => '0'
),
'id' => $this->_post->ID,
'quantity' => 1,
),
'item_price' => '20.0',
'quantity' => 1,
'discount' => '0.0',
'subtotal' => '20.0',
'tax' => '4.0',
'fees' => array(),
'price' => '24.0'
)
);
$this->assertEquals( $expected, edd_get_cart_content_details() );
}
public function test_get_cart_item_discounted_amount() {
// Call without any arguments
$expected = edd_get_cart_item_discount_amount();
$this->assertEquals( 0.00, $expected );
// Call with an array but missing 'id'
$expected = edd_get_cart_item_discount_amount( array( 'foo' => 'bar' ) );
$this->assertEquals( 0.00, $expected );
$options = array(
'price_id' => 0
);
edd_add_to_cart( $this->_post->ID, $options );
// Now set a discount and test again
edd_set_cart_discount( '20OFF' );
// Test it without a quantity
$cart_item_args = array( 'id' => $this->_post->ID );
$this->assertEquals( 0.00, edd_get_cart_item_discount_amount( $cart_item_args ) );
// Test it without an options array on an item with variable pricing to make sure we get 0
$cart_item_args = array( 'id' => $this->_post->ID, 'quantity' => 1 );
$this->assertEquals( 0.00, edd_get_cart_item_discount_amount( $cart_item_args ) );
// Now test it with an options array properly set
$cart_item_args['options'] = $options;
$this->assertEquals( 4, edd_get_cart_item_discount_amount( $cart_item_args ) );
edd_unset_cart_discount( '20OFF' );
}
public function test_cart_quantity() {
$options = array(
'price_id' => 0
);
edd_add_to_cart( $this->_post->ID, $options );
$this->assertEquals(1, edd_get_cart_quantity());
}
public function test_get_cart_item_quantity() {
edd_empty_cart();
$options = array(
'price_id' => 0
);
edd_add_to_cart( $this->_post->ID, $options );
$this->assertEquals( 1, edd_get_cart_item_quantity( $this->_post->ID, $options ) );
edd_update_option( 'item_quantities', true );
// Add the item to the cart again
edd_add_to_cart( $this->_post->ID, $options );
$this->assertEquals( 2, edd_get_cart_item_quantity( $this->_post->ID, $options ) );
edd_delete_option( 'item_quantities' );
// Now add a different price option to the cart
$options = array(
'price_id' => 1
);
edd_add_to_cart( $this->_post->ID, $options );
$this->assertEquals( 1, edd_get_cart_item_quantity( $this->_post->ID, $options ) );
}
public function test_add_to_cart_with_quantities_enabled_on_product() {
add_filter( 'edd_item_quantities_enabled', '__return_true' );
$options = array(
'price_id' => 0,
'quantity' => 2
);
edd_add_to_cart( $this->_post->ID, $options );
$this->assertEquals( 2, edd_get_cart_item_quantity( $this->_post->ID, $options ) );
}
public function test_add_to_cart_with_quantities_disabled_on_product() {
add_filter( 'edd_item_quantities_enabled', '__return_true' );
update_post_meta( $this->_post->ID, '_edd_quantities_disabled', 1 );
$options = array(
'price_id' => 0,
'quantity' => 2
);
edd_add_to_cart( $this->_post->ID, $options );
$this->assertEquals( 1, edd_get_cart_item_quantity( $this->_post->ID, $options ) );
}
public function test_set_cart_item_quantity() {
edd_update_option( 'item_quantities', true );
$options = array(
'price_id' => 0
);
edd_add_to_cart( $this->_post->ID, $options );
edd_set_cart_item_quantity( $this->_post->ID, 3, $options );
$this->assertEquals( 3, edd_get_cart_item_quantity( $this->_post->ID, $options ) );
edd_delete_option( 'item_quantities' );
}
public function test_item_in_cart() {
$this->assertFalse(edd_item_in_cart($this->_post->ID));
}
public function test_cart_item_price() {
$this->assertEquals( '$0.00' , edd_cart_item_price( 0 ) );
}
public function test_get_cart_item_price() {
$this->assertEquals( false , edd_get_cart_item_price( 0 ) );
}
public function test_remove_from_cart() {
edd_empty_cart();
edd_add_to_cart( $this->_post->ID );
$expected = array();
$this->assertEquals( $expected, edd_remove_from_cart( 0 ) );
}
public function test_set_purchase_session() {
$this->assertNull( edd_set_purchase_session() );
}
public function test_get_purchase_session() {
$this->assertEmpty( edd_get_purchase_session() );
}
public function test_cart_saving_disabled() {
$this->assertTrue( edd_is_cart_saving_disabled() );
}
public function test_is_cart_saved_false() {
// Test for no saved cart
$this->assertFalse( edd_is_cart_saved() );
// Create a saved cart then test again
$cart = array(
'0' => array(
'id' => $this->_post->ID,
'options' => array(
'price_id' => 0
),
'quantity' => 1
)
);
update_user_meta( get_current_user_id(), 'edd_saved_cart', $cart );
edd_update_option( 'enable_cart_saving', '1' );
$this->assertTrue( edd_is_cart_saved() );
}
public function test_restore_cart() {
// Create a saved cart
$saved_cart = array(
'0' => array(
'id' => $this->_post->ID,
'options' => array(
'price_id' => 0
),
'quantity' => 1
)
);
update_user_meta( get_current_user_id(), 'edd_saved_cart', $saved_cart );
// Set the current cart contents (different from saved)
$cart = array(
'0' => array(
'id' => $this->_post->ID,
'options' => array(
'price_id' => 1
),
'quantity' => 1
)
);
EDD()->session->set( 'edd_cart', $cart );
EDD()->cart->contents = $cart;
edd_update_option( 'enable_cart_saving', '1' );
$this->assertTrue( edd_restore_cart() );
$this->assertEquals( edd_get_cart_contents(), $saved_cart );
}
public function test_generate_cart_token() {
$this->assertInternalType( 'string', edd_generate_cart_token() );
$this->assertTrue( 32 === strlen( edd_generate_cart_token() ) );
}
public function test_edd_get_cart_item_name() {
edd_add_to_cart( $this->_post->ID );
$items = edd_get_cart_content_details();
$this->assertEquals( 'Test Download - Simple', edd_get_cart_item_name( $items[0] ) );
}
public function test_cart_total_with_global_fee() {
edd_empty_cart();
edd_add_to_cart( $this->_post->ID, array( 'price_id' => 0 ) );
EDD()->fees->add_fee( 10, 'test', 'Test' );
$this->assertEquals( 30, EDD()->cart->get_total() );
}
public function test_cart_fess_total_with_global_fee() {
edd_empty_cart();
edd_add_to_cart( $this->_post->ID );
EDD()->fees->add_fee( 10, 'test', 'Test' );
$this->assertEquals( 10, edd_get_cart_fee_total() );
}
public function test_cart_total_with_download_fee() {
edd_empty_cart();
edd_add_to_cart( $this->_post->ID, array( 'price_id' => 0 ) );
EDD()->fees->add_fee( array(
'amount' => 10,
'id' => 'test',
'label' => 'Test',
'download_id' => $this->_post->ID
) );
$this->assertEquals( 30, edd_get_cart_total() );
}
public function test_cart_fee_total_with_download_fee() {
edd_empty_cart();
edd_add_to_cart( $this->_post->ID, array( 'price_id' => 0 ) );
EDD()->fees->add_fee( array(
'amount' => 10,
'id' => 'test',
'label' => 'Test',
'download_id' => $this->_post->ID
) );
// Since it's a fee associated with an item in the cart, it affects it's pricing, not the total cart fees.
$this->assertEquals( 0, edd_get_cart_fee_total() );
}
public function test_cart_total_with_global_item_fee() {
edd_empty_cart();
edd_add_to_cart( $this->_post->ID, array( 'price_id' => 0 ) );
EDD()->fees->add_fee( array(
'amount' => 10,
'id' => 'test',
'label' => 'Test',
'type' => 'item'
) );
$this->assertEquals( 30, edd_get_cart_total() );
}
public function test_cart_fee_total_with_global_item_fee() {
edd_empty_cart();
edd_add_to_cart( $this->_post->ID );
EDD()->fees->add_fee( array(
'amount' => 10,
'id' => 'test',
'label' => 'Test',
'type' => 'item'
) );
$this->assertEquals( 10, edd_get_cart_fee_total() );
}
public function test_unset_cart_discount_case_insensitive() {
edd_set_cart_discount( '20off' );
$this->assertEmpty( edd_unset_cart_discount( '20OFF' ) );
}
public function test_negative_fees_cart_tax() {
edd_update_option( 'enable_taxes', true );
edd_update_option( 'tax_rate', '10' );
$options = array(
'price_id' => 0,
);
edd_add_to_cart( $this->_post->ID, $options );
$fee = array(
'amount' => -10,
'label' => 'Sale - ' . get_the_title( $this->_post->ID ),
'id' => 'dp_0',
'download_id' => $this->_post->ID,
'price_id' => 0,
);
EDD()->fees->add_fee( $fee );
$this->assertEquals( "1", EDD()->cart->get_tax() );
edd_update_option( 'enable_taxes', false );
}
}