-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest-class-wc-connect-taxjar-integration.php
More file actions
4564 lines (3912 loc) · 162 KB
/
Copy pathtest-class-wc-connect-taxjar-integration.php
File metadata and controls
4564 lines (3912 loc) · 162 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
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Tests for WC_Connect_TaxJar_Integration
*
* @package WooCommerce\Tests
*/
/**
* Class WP_Test_WC_Connect_TaxJar_Integration
*
* Tests for the TaxJar integration class.
*/
class WP_Test_WC_Connect_TaxJar_Integration extends WC_Unit_Test_Case {
/**
* TaxJar integration instance.
*
* @var WC_Connect_TaxJar_Integration
*/
private $integration;
/**
* Test product.
*
* @var WC_Product
*/
private $product;
/**
* Closure registered on `taxjar_store_settings` by the local-pickup helper.
*
* @var Closure|null
*/
private $store_settings_filter;
/**
* Error messages logged during the last `capture_taxjar_request_json()` run.
*
* @var string[]
*/
private $captured_taxjar_errors = array();
/**
* Load required classes before running tests.
*/
public static function set_up_before_class() {
parent::set_up_before_class();
require_once __DIR__ . '/../../classes/class-wc-connect-taxjar-integration.php';
require_once __DIR__ . '/../../classes/class-wc-connect-api-client.php';
require_once __DIR__ . '/../../classes/class-wc-connect-logger.php';
require_once __DIR__ . '/../../classes/class-wc-connect-tracks.php';
require_once __DIR__ . '/../../classes/class-wc-connect-custom-surcharge.php';
}
/**
* Set up before each test.
*/
public function set_up() {
parent::set_up();
// Create mock dependencies.
$api_client = $this->getMockBuilder( 'WC_Connect_API_Client' )
->disableOriginalConstructor()
->getMock();
$logger = $this->getMockBuilder( 'WC_Connect_Logger' )
->disableOriginalConstructor()
->getMock();
$tracks = $this->getMockBuilder( 'WC_Connect_Tracks' )
->disableOriginalConstructor()
->getMock();
$this->integration = new WC_Connect_TaxJar_Integration(
$api_client,
$logger,
'https://example.com',
$tracks
);
}
/**
* Tear down after each test.
*/
public function tear_down() {
// Remove any filters added during tests.
remove_all_filters( 'woocommerce_tax_line_item_location' );
remove_all_filters( 'woocommerce_services_override_tax_rate' );
remove_all_filters( 'woocommerce_product_is_taxable' );
delete_option( 'woocommerce_calc_taxes' );
// Clean up products.
if ( $this->product ) {
WC_Helper_Product::delete_product( $this->product->get_id() );
$this->product = null;
}
// Clear cart.
if ( WC()->cart ) {
WC()->cart->empty_cart();
}
parent::tear_down();
}
/**
* Test that the preserve_order_taxes_on_recalculation hook is registered after init().
*/
public function test_preserve_order_taxes_hook_registered_after_init() {
// Arrange: enable automated taxes option so init() does not bail early.
update_option( WC_Connect_TaxJar_Integration::OPTION_NAME, 'yes' );
update_option( 'woocommerce_calc_taxes', 'yes' );
// Act.
$this->integration->init();
// Assert.
$this->assertNotFalse(
has_action( 'woocommerce_order_before_calculate_taxes', array( $this->integration, 'preserve_order_taxes_on_recalculation' ) ),
'Hook woocommerce_order_before_calculate_taxes should be registered'
);
// Clean up.
remove_all_actions( 'woocommerce_order_before_calculate_taxes' );
delete_option( WC_Connect_TaxJar_Integration::OPTION_NAME );
delete_option( 'woocommerce_calc_taxes' );
}
/**
* Helper to invoke protected methods via reflection.
*
* @param string $method_name Method name.
* @param array $args Method arguments.
* @return mixed Method result.
*/
private function invoke_protected_method( $method_name, $args = array() ) {
$reflection = new ReflectionMethod( 'WC_Connect_TaxJar_Integration', $method_name );
$reflection->setAccessible( true );
return $reflection->invokeArgs( $this->integration, $args );
}
/**
* Helper to set private properties via reflection.
*
* @param string $property_name Property name.
* @param mixed $value Value to set.
*/
private function set_private_property( $property_name, $value ) {
$reflection = new ReflectionProperty( 'WC_Connect_TaxJar_Integration', $property_name );
$reflection->setAccessible( true );
$reflection->setValue( $this->integration, $value );
}
/**
* Helper to read private properties via reflection.
*
* @param string $property_name Property name.
* @return mixed Property value.
*/
private function get_private_property( $property_name ) {
$reflection = new ReflectionProperty( 'WC_Connect_TaxJar_Integration', $property_name );
$reflection->setAccessible( true );
return $reflection->getValue( $this->integration );
}
/**
* Test that get_line_items includes tax_location key with default value.
*/
public function test_get_line_items_includes_tax_location_with_default() {
// Create a simple product.
$this->product = WC_Helper_Product::create_simple_product();
$this->product->save();
// Add to cart.
WC()->cart->add_to_cart( $this->product->get_id(), 1 );
// Get line items.
$line_items = $this->invoke_protected_method( 'get_line_items', array( WC()->cart ) );
// Assert we have line items.
$this->assertNotEmpty( $line_items );
$this->assertCount( 1, $line_items );
// Line items are keyed by cart item key, so take the first value.
$first_item = reset( $line_items );
// Assert tax_location key exists.
$this->assertArrayHasKey( 'tax_location', $first_item );
// Default should be 'shipping' (the WooCommerce default).
$this->assertEquals( 'shipping', $first_item['tax_location'] );
}
/**
* Test that get_line_items respects woocommerce_tax_based_on option.
*/
public function test_get_line_items_respects_tax_based_on_option() {
// Set tax based on billing address.
update_option( 'woocommerce_tax_based_on', 'billing' );
// Create a simple product.
$this->product = WC_Helper_Product::create_simple_product();
$this->product->save();
// Add to cart.
WC()->cart->add_to_cart( $this->product->get_id(), 1 );
// Get line items.
$line_items = $this->invoke_protected_method( 'get_line_items', array( WC()->cart ) );
// Assert tax_location uses billing.
$first_item = reset( $line_items );
$this->assertEquals( 'billing', $first_item['tax_location'] );
// Clean up option.
delete_option( 'woocommerce_tax_based_on' );
}
/**
* Test that woocommerce_tax_line_item_location filter can override location.
*/
public function test_get_line_items_filter_can_override_location() {
// Create a simple product.
$this->product = WC_Helper_Product::create_simple_product();
$this->product->save();
// Add filter to return 'base' for all products.
add_filter(
'woocommerce_tax_line_item_location',
function ( $location, $product ) {
return 'base';
},
10,
2
);
// Add to cart.
WC()->cart->add_to_cart( $this->product->get_id(), 1 );
// Get line items.
$line_items = $this->invoke_protected_method( 'get_line_items', array( WC()->cart ) );
// Assert filter overrode the location.
$first_item = reset( $line_items );
$this->assertEquals( 'base', $first_item['tax_location'] );
}
/**
* Test that filter receives correct product object.
*/
public function test_get_line_items_filter_receives_product() {
// Create a simple product.
$this->product = WC_Helper_Product::create_simple_product();
$this->product->save();
$received_product = null;
$received_location = null;
// Add filter to capture parameters.
add_filter(
'woocommerce_tax_line_item_location',
function ( $location, $product ) use ( &$received_product, &$received_location ) {
$received_location = $location;
$received_product = $product;
return $location;
},
10,
2
);
// Add to cart.
WC()->cart->add_to_cart( $this->product->get_id(), 1 );
// Get line items.
$this->invoke_protected_method( 'get_line_items', array( WC()->cart ) );
// Assert filter received correct parameters.
$this->assertNotNull( $received_product );
$this->assertInstanceOf( 'WC_Product', $received_product );
$this->assertEquals( $this->product->get_id(), $received_product->get_id() );
$this->assertEquals( 'shipping', $received_location );
}
/**
* Test filter can return different locations for different products.
*/
public function test_get_line_items_filter_per_product_location() {
// Create two products.
$product_a = WC_Helper_Product::create_simple_product();
$product_a->save();
$product_b = WC_Helper_Product::create_simple_product();
$product_b->save();
$product_a_id = $product_a->get_id();
// Add filter to return 'base' only for product A.
add_filter(
'woocommerce_tax_line_item_location',
function ( $location, $product ) use ( $product_a_id ) {
if ( $product && $product->get_id() === $product_a_id ) {
return 'base';
}
return $location;
},
10,
2
);
// Add both to cart.
WC()->cart->add_to_cart( $product_a->get_id(), 1 );
WC()->cart->add_to_cart( $product_b->get_id(), 1 );
// Get line items.
$line_items = $this->invoke_protected_method( 'get_line_items', array( WC()->cart ) );
// Assert we have 2 line items.
$this->assertCount( 2, $line_items );
// Find items by product ID (they're in format "product_id-fingerprint-occurrence").
$item_a = null;
$item_b = null;
foreach ( $line_items as $item ) {
if ( strpos( $item['id'], $product_a->get_id() . '-' ) === 0 ) {
$item_a = $item;
} elseif ( strpos( $item['id'], $product_b->get_id() . '-' ) === 0 ) {
$item_b = $item;
}
}
// Assert different locations.
$this->assertNotNull( $item_a );
$this->assertNotNull( $item_b );
$this->assertEquals( 'base', $item_a['tax_location'] );
$this->assertEquals( 'shipping', $item_b['tax_location'] );
// Clean up.
WC_Helper_Product::delete_product( $product_a->get_id() );
WC_Helper_Product::delete_product( $product_b->get_id() );
}
/**
* Test that get_backend_line_items includes tax_location key.
*/
public function test_get_backend_line_items_includes_tax_location() {
// Create an order with a product.
$order = WC_Helper_Order::create_order();
$order->save();
// Get line items.
$line_items = $this->invoke_protected_method( 'get_backend_line_items', array( $order ) );
// Assert we have line items.
$this->assertNotEmpty( $line_items );
// Assert tax_location key exists on all items.
foreach ( $line_items as $item ) {
$this->assertArrayHasKey( 'tax_location', $item );
}
}
/**
* Test that get_backend_line_items filter can override location.
*/
public function test_get_backend_line_items_filter_can_override_location() {
// Create an order.
$order = WC_Helper_Order::create_order();
$order->save();
// Add filter to return 'base'.
add_filter(
'woocommerce_tax_line_item_location',
function ( $location, $product ) {
return 'base';
},
10,
2
);
// Get line items.
$line_items = $this->invoke_protected_method( 'get_backend_line_items', array( $order ) );
// Assert all items have 'base' location.
foreach ( $line_items as $item ) {
$this->assertEquals( 'base', $item['tax_location'] );
}
}
/**
* Test that filter receives product object in backend context.
*/
public function test_get_backend_line_items_filter_receives_product() {
// Create an order.
$order = WC_Helper_Order::create_order();
$order->save();
$received_product = null;
// Add filter to capture product.
add_filter(
'woocommerce_tax_line_item_location',
function ( $location, $product ) use ( &$received_product ) {
$received_product = $product;
return $location;
},
10,
2
);
// Get line items.
$this->invoke_protected_method( 'get_backend_line_items', array( $order ) );
// Assert filter received a product.
$this->assertNotNull( $received_product );
$this->assertInstanceOf( 'WC_Product', $received_product );
}
/**
* Test that line items contain all expected keys.
*/
public function test_get_line_items_structure() {
// Create a simple product.
$this->product = WC_Helper_Product::create_simple_product();
$this->product->save();
// Add to cart.
WC()->cart->add_to_cart( $this->product->get_id(), 2 );
// Get line items.
$line_items = $this->invoke_protected_method( 'get_line_items', array( WC()->cart ) );
// Assert structure.
$this->assertCount( 1, $line_items );
$item = reset( $line_items );
$this->assertArrayHasKey( 'id', $item );
$this->assertArrayHasKey( 'quantity', $item );
$this->assertArrayHasKey( 'product_tax_code', $item );
$this->assertArrayHasKey( 'unit_price', $item );
$this->assertArrayHasKey( 'discount', $item );
$this->assertArrayHasKey( 'tax_location', $item );
// Assert quantity.
$this->assertEquals( 2, $item['quantity'] );
}
// -------------------------------------------------------------------------
// group_items_by_location() tests
// -------------------------------------------------------------------------
/**
* Test grouping items by tax_location key.
*/
public function test_group_items_by_location_groups_by_tax_location() {
$line_items = array(
array(
'id' => 'item-1',
'tax_location' => 'shipping',
),
array(
'id' => 'item-2',
'tax_location' => 'base',
),
array(
'id' => 'item-3',
'tax_location' => 'shipping',
),
);
$groups = $this->invoke_protected_method( 'group_items_by_location', array( $line_items, false ) );
$this->assertCount( 2, $groups );
$this->assertArrayHasKey( 'shipping', $groups );
$this->assertArrayHasKey( 'base', $groups );
$this->assertCount( 2, $groups['shipping'] );
$this->assertCount( 1, $groups['base'] );
$this->assertEquals( 'item-2', $groups['base'][0]['id'] );
}
/**
* Test local pickup forces all items to base.
*/
public function test_group_items_by_location_local_pickup_forces_base() {
$line_items = array(
array(
'id' => 'item-1',
'tax_location' => 'shipping',
),
array(
'id' => 'item-2',
'tax_location' => 'billing',
),
);
$groups = $this->invoke_protected_method( 'group_items_by_location', array( $line_items, true ) );
$this->assertCount( 1, $groups );
$this->assertArrayHasKey( 'base', $groups );
$this->assertCount( 2, $groups['base'] );
}
/**
* Test missing tax_location uses woocommerce_tax_based_on default.
*/
public function test_group_items_by_location_uses_default_when_missing() {
update_option( 'woocommerce_tax_based_on', 'billing' );
$line_items = array(
array( 'id' => 'item-1' ), // No tax_location key.
);
$groups = $this->invoke_protected_method( 'group_items_by_location', array( $line_items, false ) );
$this->assertArrayHasKey( 'billing', $groups );
$this->assertCount( 1, $groups['billing'] );
delete_option( 'woocommerce_tax_based_on' );
}
/**
* Test empty input returns empty groups.
*/
public function test_group_items_by_location_empty_input() {
$groups = $this->invoke_protected_method( 'group_items_by_location', array( array(), false ) );
$this->assertIsArray( $groups );
$this->assertEmpty( $groups );
}
// -------------------------------------------------------------------------
// get_taxable_address() tests
// -------------------------------------------------------------------------
/**
* Test that location_type 'base' returns store address.
*/
public function test_get_taxable_address_base_returns_store_address() {
$store_country = WC()->countries->get_base_country();
$store_state = WC()->countries->get_base_state();
$address = $this->invoke_protected_method( 'get_taxable_address', array( 'base' ) );
$this->assertIsArray( $address );
$this->assertEquals( $store_country, $address[0] );
$this->assertEquals( $store_state, $address[1] );
}
/**
* A `taxjar_store_settings` callback returning a non-array must not fatal.
*
* The filter is public and unguarded. Before `get_store_settings()` enforced its own
* documented `@return array`, a callback returning null / false / a string reached
* `Address::from_store_settings( array $settings )` and raised an uncaught TypeError
* on every cart and checkout render resolving a base address. The unfiltered store
* address is used instead.
*
* @dataProvider provider_non_array_store_settings
*
* @param mixed $returned Value the filter callback returns.
*/
public function test_get_taxable_address_survives_non_array_store_settings( $returned ) {
$callback = static function () use ( $returned ) {
return $returned;
};
add_filter( 'taxjar_store_settings', $callback );
$store_country = WC()->countries->get_base_country();
$store_state = WC()->countries->get_base_state();
try {
$address = $this->invoke_protected_method( 'get_taxable_address', array( 'base' ) );
} finally {
remove_filter( 'taxjar_store_settings', $callback );
}
$this->assertIsArray( $address );
$this->assertEquals( $store_country, $address[0] );
$this->assertEquals( $store_state, $address[1] );
}
/**
* Non-array return values a `taxjar_store_settings` callback might produce.
*
* @return array<string, array{0: mixed}>
*/
public function provider_non_array_store_settings() {
return array(
'null' => array( null ),
'false' => array( false ),
'string' => array( 'not-an-address' ),
'object' => array( new stdClass() ),
);
}
/**
* A `taxjar_store_settings` callback returning a partial array must not drop keys.
*
* Callers index the result directly, so a missing key used to raise undefined
* key warnings and feed null into strtoupper(). Missing keys are backfilled
* from the unfiltered store settings; keys the callback did set still win.
*/
public function test_get_store_settings_backfills_partial_array() {
$callback = static function () {
return array( 'postcode' => '90210' );
};
add_filter( 'taxjar_store_settings', $callback );
try {
$settings = $this->integration->get_store_settings();
} finally {
remove_filter( 'taxjar_store_settings', $callback );
}
$this->assertSame( '90210', $settings['postcode'] );
foreach ( array( 'street', 'city', 'state', 'country' ) as $key ) {
$this->assertArrayHasKey( $key, $settings );
}
$this->assertSame( WC()->countries->get_base_country(), $settings['country'] );
$this->assertSame( WC()->countries->get_base_city(), $settings['city'] );
}
/**
* Test that location_type 'shipping' returns customer shipping address.
*/
public function test_get_taxable_address_shipping_returns_customer_address() {
WC()->customer->set_shipping_country( 'US' );
WC()->customer->set_shipping_state( 'NY' );
WC()->customer->set_shipping_postcode( '10001' );
$address = $this->invoke_protected_method( 'get_taxable_address', array( 'shipping' ) );
$this->assertEquals( 'US', $address[0] );
$this->assertEquals( 'NY', $address[1] );
$this->assertEquals( '10001', $address[2] );
}
/**
* Test that location_type 'billing' returns customer billing address.
*/
public function test_get_taxable_address_billing_returns_billing_address() {
WC()->customer->set_billing_country( 'US' );
WC()->customer->set_billing_state( 'TX' );
WC()->customer->set_billing_postcode( '73301' );
$address = $this->invoke_protected_method( 'get_taxable_address', array( 'billing' ) );
$this->assertEquals( 'US', $address[0] );
$this->assertEquals( 'TX', $address[1] );
$this->assertEquals( '73301', $address[2] );
}
/**
* Test that null location_type uses woocommerce_tax_based_on option (backward compat).
*/
public function test_get_taxable_address_null_uses_option() {
update_option( 'woocommerce_tax_based_on', 'base' );
$store_country = WC()->countries->get_base_country();
$address = $this->invoke_protected_method( 'get_taxable_address', array( null ) );
$this->assertEquals( $store_country, $address[0] );
delete_option( 'woocommerce_tax_based_on' );
}
/**
* Test get_taxable_address returns early with empty array when WC()->customer is null.
*/
public function test_get_taxable_address_returns_empty_when_customer_is_null() {
$original_customer = WC()->customer;
WC()->customer = null;
$address = $this->invoke_protected_method( 'get_taxable_address', array( 'shipping' ) );
$this->assertEquals( array( '', '', '', '', '' ), $address );
WC()->customer = $original_customer;
}
/**
* Test get_taxable_address returns early for billing type when WC()->customer is null.
*/
public function test_get_taxable_address_billing_returns_empty_when_customer_is_null() {
$original_customer = WC()->customer;
WC()->customer = null;
$address = $this->invoke_protected_method( 'get_taxable_address', array( 'billing' ) );
$this->assertEquals( array( '', '', '', '', '' ), $address );
WC()->customer = $original_customer;
}
/**
* Test get_taxable_address base type still works when WC()->customer is null.
*/
public function test_get_taxable_address_base_works_when_customer_is_null() {
$original_customer = WC()->customer;
WC()->customer = null;
$store_country = WC()->countries->get_base_country();
$address = $this->invoke_protected_method( 'get_taxable_address', array( 'base' ) );
$this->assertEquals( $store_country, $address[0] );
WC()->customer = $original_customer;
}
// -------------------------------------------------------------------------
// aggregate_tax_totals() tests
// -------------------------------------------------------------------------
/**
* Test aggregation combines entries with same label.
*/
public function test_aggregate_tax_totals_combines_same_labels() {
$tax_a = new stdClass();
$tax_a->label = 'County Tax';
$tax_a->amount = 0.25;
$tax_b = new stdClass();
$tax_b->label = 'County Tax';
$tax_b->amount = 0.12;
$tax_c = new stdClass();
$tax_c->label = 'State Tax';
$tax_c->amount = 1.50;
$tax_totals = array(
'US-CA-1' => $tax_a,
'US-NY-1' => $tax_b,
'US-CA-2' => $tax_c,
);
$result = $this->integration->aggregate_tax_totals( $tax_totals, WC()->cart );
$this->assertCount( 2, $result );
$this->assertArrayHasKey( 'County Tax', $result );
$this->assertArrayHasKey( 'State Tax', $result );
$this->assertEquals( 0.37, $result['County Tax']->amount );
$this->assertEquals( 1.50, $result['State Tax']->amount );
}
/**
* Test aggregation returns input unchanged when count <= 1.
*/
public function test_aggregate_tax_totals_single_entry_unchanged() {
$tax = new stdClass();
$tax->label = 'Tax';
$tax->amount = 1.00;
$tax_totals = array( 'US-1' => $tax );
$result = $this->integration->aggregate_tax_totals( $tax_totals, WC()->cart );
$this->assertSame( $tax_totals, $result );
}
/**
* Test aggregation returns input unchanged when not an array.
*/
public function test_aggregate_tax_totals_non_array_unchanged() {
$result = $this->integration->aggregate_tax_totals( null, WC()->cart );
$this->assertNull( $result );
}
/**
* Test aggregation does not mutate original objects.
*/
public function test_aggregate_tax_totals_clones_objects() {
$tax_a = new stdClass();
$tax_a->label = 'Tax';
$tax_a->amount = 1.00;
$tax_b = new stdClass();
$tax_b->label = 'Tax';
$tax_b->amount = 2.00;
$tax_totals = array(
'A' => $tax_a,
'B' => $tax_b,
);
$result = $this->integration->aggregate_tax_totals( $tax_totals, WC()->cart );
// Original objects should not be modified.
$this->assertEquals( 1.00, $tax_a->amount );
$this->assertEquals( 2.00, $tax_b->amount );
// Aggregated result should have combined amount.
$this->assertEquals( 3.00, $result['Tax']->amount );
}
// -------------------------------------------------------------------------
// override_cart_item_tax_rates() tests
// -------------------------------------------------------------------------
/**
* Test returns original rates when response_rate_ids is empty.
*/
public function test_override_cart_item_tax_rates_returns_original_when_no_response() {
$original_rates = array( 1 => array( 'rate' => 8.5 ) );
$item = new stdClass();
$item->product = null;
$result = $this->integration->override_cart_item_tax_rates( $original_rates, $item, WC()->cart );
$this->assertSame( $original_rates, $result );
}
/**
* Test returns original rates when item has no product.
*/
public function test_override_cart_item_tax_rates_returns_original_when_no_product() {
$this->set_private_property( 'response_rate_ids', array( '10-abc' => array( 1, 2 ) ) );
$original_rates = array( 1 => array( 'rate' => 8.5 ) );
$item = new stdClass();
$result = $this->integration->override_cart_item_tax_rates( $original_rates, $item, WC()->cart );
$this->assertSame( $original_rates, $result );
}
/**
* Test returns original rates when no matching product in response_rate_ids.
*/
public function test_override_cart_item_tax_rates_returns_original_when_no_match() {
$this->set_private_property( 'response_rate_ids', array( '999-abc' => array( 1, 2 ) ) );
$this->product = WC_Helper_Product::create_simple_product();
$this->product->save();
$original_rates = array( 1 => array( 'rate' => 8.5 ) );
$item = new stdClass();
$item->product = $this->product;
$result = $this->integration->override_cart_item_tax_rates( $original_rates, $item, WC()->cart );
$this->assertSame( $original_rates, $result );
}
/**
* Test returns TaxJar rates when matching product found.
*/
public function test_override_cart_item_tax_rates_returns_taxjar_rates_when_matched() {
$this->product = WC_Helper_Product::create_simple_product();
$this->product->save();
// Insert a tax rate into the database.
$rate_id = WC_Tax::_insert_tax_rate(
array(
'tax_rate_country' => 'US',
'tax_rate_state' => 'CA',
'tax_rate' => '8.5',
'tax_rate_name' => 'CA Tax',
'tax_rate_shipping' => 'no',
'tax_rate_compound' => 'no',
'tax_rate_priority' => 1,
'tax_rate_class' => '',
)
);
$product_id = $this->product->get_id();
$this->set_private_property( 'response_rate_ids', array( $product_id . '-abc123' => array( $rate_id ) ) );
$original_rates = array();
$item = new stdClass();
$item->product = $this->product;
$result = $this->integration->override_cart_item_tax_rates( $original_rates, $item, WC()->cart );
$this->assertNotEmpty( $result );
$this->assertArrayHasKey( $rate_id, $result );
$this->assertEquals( 8.5, $result[ $rate_id ]['rate'] );
$this->assertEquals( 'CA Tax', $result[ $rate_id ]['label'] );
// Clean up.
WC_Tax::_delete_tax_rate( $rate_id );
}
// -------------------------------------------------------------------------
// override_order_item_taxes() tests
// -------------------------------------------------------------------------
/**
* Test no-op when response_rate_ids is empty (no TaxJar calc this request).
*/
public function test_override_order_item_taxes_noop_when_no_response() {
$order = WC_Helper_Order::create_order();
$items = $order->get_items();
$item = reset( $items );
$orig = $item->get_taxes();
$this->integration->override_order_item_taxes( $item, array() );
$this->assertSame( $orig, $item->get_taxes() );
$order->delete( true );
}
/**
* Test no-op when item is not a WC_Order_Item_Product (e.g. fee or shipping).
*/
public function test_override_order_item_taxes_skips_non_product_items() {
$this->set_private_property( 'response_rate_ids', array( '10-abc' => array( 1 ) ) );
$fee = new WC_Order_Item_Fee();
$fee->set_total( '5.00' );
// Should not throw or modify.
$this->integration->override_order_item_taxes( $fee, array() );
$this->assertEmpty( $fee->get_taxes()['total'] );
}
/**
* Test no-op when the product is not found in response_rate_ids (cross-state item).
*/
public function test_override_order_item_taxes_skips_unmatched_product() {
$this->set_private_property( 'response_rate_ids', array( '999-abc' => array( 1 ) ) );
$order = WC_Helper_Order::create_order();
$items = $order->get_items();
$item = reset( $items );
$orig = $item->get_taxes();
$this->integration->override_order_item_taxes( $item, array() );
$this->assertSame( $orig, $item->get_taxes() );
$order->delete( true );
}
/**
* Test that TaxJar rates are applied for a matched base-group item.
*/
public function test_override_order_item_taxes_applies_taxjar_rates_when_matched() {
$this->product = WC_Helper_Product::create_simple_product();
$this->product->set_regular_price( '20.00' );
$this->product->save();
// Insert a tax rate into the database.
$rate_id = WC_Tax::_insert_tax_rate(
array(
'tax_rate_country' => 'US',
'tax_rate_state' => 'CA',
'tax_rate' => '10.0000',
'tax_rate_name' => 'CA Tax',
'tax_rate_shipping' => 'no',
'tax_rate_compound' => 'no',
'tax_rate_priority' => 1,
'tax_rate_class' => '',
)
);
$product_id = $this->product->get_id();
$this->set_private_property( 'response_rate_ids', array( $product_id . '-abc123' => array( $rate_id ) ) );
// Create an order item.
$item = new WC_Order_Item_Product();
$item->set_product( $this->product );
$item->set_quantity( 1 );
$item->set_total( '20.00' );
$item->set_subtotal( '20.00' );
$this->integration->override_order_item_taxes( $item, array() );
$taxes = $item->get_taxes();
$this->assertArrayHasKey( $rate_id, $taxes['total'] );
$this->assertEquals( 2.0, (float) $taxes['total'][ $rate_id ] );
$this->assertArrayHasKey( $rate_id, $taxes['subtotal'] );
$this->assertEquals( 2.0, (float) $taxes['subtotal'][ $rate_id ] );
// Clean up.
WC_Tax::_delete_tax_rate( $rate_id );
}
// -------------------------------------------------------------------------
// calculate_taxes_by_location() tests
// -------------------------------------------------------------------------
/**
* Test merges results from multiple location groups.
*/
public function test_calculate_taxes_by_location_merges_groups() {
// Create a partial mock that stubs calculate_tax and get_address.
$integration = $this->getMockBuilder( 'WC_Connect_TaxJar_Integration' )
->disableOriginalConstructor()
->onlyMethods( array( 'calculate_tax', 'get_address', '_log' ) )
->getMock();
$integration->method( 'get_address' )->willReturnCallback(
function ( $type ) {
return array(
'to_country' => 'US',
'to_state' => 'CA',
'to_zip' => '94110',