-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest-woocommerce-connect-client.php
More file actions
595 lines (483 loc) · 21.6 KB
/
Copy pathtest-woocommerce-connect-client.php
File metadata and controls
595 lines (483 loc) · 21.6 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
<?php
require_once __DIR__ . '/class-wcservices-throwing-store-api-extend-schema.php';
class WP_Test_WC_Connect_Loader extends WC_Unit_Test_Case {
const SERVICE_SCRIPT_HANDLE = 'wc_connect_admin';
public function tear_down() {
wp_deregister_script( self::SERVICE_SCRIPT_HANDLE );
remove_all_actions( 'wc_connect_shipping_zone_method_added' );
remove_all_actions( 'wc_connect_shipping_zone_method_deleted' );
remove_all_actions( 'wc_connect_shipping_zone_method_status_toggled' );
remove_all_actions( 'wc_connect_saved_service_settings' );
}
protected function mockLoader( $store = false, $api_client = false, $logger = false, $tracks = false ) {
if ( ! $store ) {
$store = $this->getMockBuilder( 'WC_Connect_Service_Schemas_Store' )
->disableOriginalConstructor()
->setMethods( null )
->getMock();
}
$loader = $this->getMockBuilder( 'WC_Connect_Loader' )
->disableOriginalConstructor()
->setMethods( array( 'get_service_schemas_store', 'get_api_client', 'get_logger', 'get_shipping_logger', 'get_tracks' ) )
->getMock();
$loader->expects( $this->any() )
->method( 'get_service_schemas_store' )
->will( $this->returnValue( $store ) );
if ( ! $api_client ) {
$api_client = $this->getMockBuilder( 'WC_Connect_API_Client_Live' )
->disableOriginalConstructor()
->getMock();
}
$loader->expects( $this->any() )
->method( 'get_api_client' )
->will( $this->returnValue( $api_client ) );
if ( ! $logger ) {
$logger = $this->getMockBuilder( 'WC_Connect_Logger' )
->disableOriginalConstructor()
->getMock();
}
$loader->expects( $this->any() )
->method( 'get_logger' )
->will( $this->returnValue( $logger ) );
$loader->expects( $this->any() )
->method( 'get_shipping_logger' )
->will( $this->returnValue( $logger ) );
if ( ! $tracks ) {
$tracks = $this->getMockBuilder( 'WC_Connect_Tracks' )
->disableOriginalConstructor()
->getMock();
}
$loader->expects( $this->any() )
->method( 'get_tracks' )
->will( $this->returnValue( $tracks ) );
return $loader;
}
public function mockLoaderAndActiveShippingMethods() {
$service_data = array(
'test_method_that_is_from_wc_connect',
);
$store = $this->getMockBuilder( 'WC_Connect_Service_Schemas_Store' )
->disableOriginalConstructor()
->setMethods( array( 'get_all_shipping_method_ids' ) )
->getMock();
$store->expects( $this->any() )
->method( 'get_all_shipping_method_ids' )
->will( $this->returnValue( $service_data ) );
return $this->mockLoader( $store );
}
public function test_class_exists() {
$this->assertTrue( class_exists( 'WC_Connect_Loader' ) );
}
/**
* The bundled wc-api-dev copy of /wc/v3/data/continents is a fallback for WooCommerce versions
* that predate the endpoint. Core has shipped it since WC 3.5, so on any supported WooCommerce
* the fallback must stay dormant.
*
* This is the regression guard for the lazy-namespace interaction: since WC 9.2 core skips
* registering the whole wc/v3 namespace on requests aimed at another namespace, so the route
* table alone cannot tell us whether core provides the endpoint.
*
* Asserted against the route table rather than against class-load state: whether the bundled
* class has been required is process-global and outlives whichever test loaded it, so it cannot
* carry this contract under an arbitrary execution order.
*
* @covers WC_Connect_Loader::wc_api_dev_init
*/
public function test_wc_api_dev_init_defers_to_core_continents_controller() {
global $wp, $wp_rest_server;
if ( ! function_exists( 'wc_rest_should_load_namespace' ) ) {
$this->markTestSkipped( 'This WooCommerce does not register REST namespaces lazily.' );
}
$this->assertTrue(
class_exists( 'WC_REST_Data_Continents_Controller' ),
'WooCommerce core is expected to provide WC_REST_Data_Continents_Controller.'
);
$original_server = $wp_rest_server;
$original_route = isset( $wp->query_vars['rest_route'] ) ? $wp->query_vars['rest_route'] : null;
// Reproduce a Store API request: core then skips the whole wc/v3 namespace, so
// /wc/v3/data/continents is missing from the route table even though core provides it.
$wp->query_vars['rest_route'] = '/wc/store/v1/cart';
$wp_rest_server = null;
try {
$this->assertArrayNotHasKey(
'/wc/v3/data/continents',
rest_get_server()->get_routes(),
'Expected WooCommerce to defer the wc/v3 namespace on a wc/store request.'
);
$this->mockLoader()->wc_api_dev_init();
$this->assertArrayNotHasKey(
'/wc/v3/data/continents',
rest_get_server()->get_routes(),
'The bundled wc-api-dev continents controller must not register a route when WooCommerce core provides its own.'
);
} finally {
$wp_rest_server = $original_server;
if ( null === $original_route ) {
unset( $wp->query_vars['rest_route'] );
} else {
$wp->query_vars['rest_route'] = $original_route;
}
}
}
/**
* Pins the fix shipped in 2.5.1: the bundled controller used to call parent::__construct(),
* but no class in its ancestry (WC_REST_Dev_Data_Controller, WC_REST_Controller,
* WP_REST_Controller) declares a constructor, so that call raised
* "Uncaught Error: Cannot call constructor" every time the fallback branch was reached.
*/
public function test_bundled_continents_controller_is_constructible() {
require_once __DIR__ . '/../../classes/class-wc-connect-continents.php';
require_once __DIR__ . '/../../classes/wc-api-dev/class-wc-rest-dev-data-controller.php';
require_once __DIR__ . '/../../classes/wc-api-dev/class-wc-rest-dev-data-continents-controller.php';
$controller = new WC_REST_Dev_Data_Continents_Controller();
$this->assertInstanceOf( 'WC_REST_Dev_Data_Continents_Controller', $controller );
}
/**
* @covers WC_Connect_Loader::__construct
*/
public function test_init_hook_attached_in_constructor() {
$loader = $this->getMockBuilder( 'WC_Connect_Loader' )
->setMethods( array( 'pre_wc_init' ) )
->getMock();
$attached = has_action( 'plugins_loaded', array( $loader, 'on_plugins_loaded' ) );
$this->assertNotFalse( $attached, 'WC_Connect_Loader::on_plugins_loaded() not attached to `plugins_loaded`.' );
do_action( 'plugins_loaded' );
$attached = has_action( 'before_woocommerce_init', array( $loader, 'pre_wc_init' ) );
$this->assertNotFalse( $attached, 'WC_Connect_Loader::pre_wc_init() not attached to `before_woocommerce_init`.' );
}
/**
* @covers WC_Connect_Loader::get_logger
* @covers WC_Connect_Loader::set_logger
*/
public function test_logger_getter_setter() {
$loader = $this->mockLoader();
$logger = $this->getMockBuilder( 'WC_Connect_Logger' )
->disableOriginalConstructor()
->getMock();
$loader->set_logger( $logger );
$this->assertEquals( $logger, $loader->get_logger() );
}
/**
* @covers WC_Connect_Loader::get_api_client
* @covers WC_Connect_Loader::set_api_client
*/
public function test_api_client_getter_setter() {
$client = $this->getMockBuilder( 'WC_Connect_API_Client_Live' )
->disableOriginalConstructor()
->getMock();
$loader = $this->mockLoader( false, $client );
$loader->set_api_client( $client );
$this->assertEquals( $client, $loader->get_api_client() );
}
/**
* @covers WC_Connect_Loader::get_service_schemas_store
* @covers WC_Connect_Loader::set_service_schemas_store
*/
public function test_services_store_getter_setter() {
$store = $this->getMockBuilder( 'WC_Connect_Service_Schemas_Store' )
->disableOriginalConstructor()
->getMock();
$loader = $this->mockLoader( $store );
$loader->set_service_schemas_store( $store );
$this->assertEquals( $store, $loader->get_service_schemas_store() );
}
/**
* @covers WC_Connect_Loader::get_service_schemas_validator
* @covers WC_Connect_Loader::set_service_schemas_validator
*/
public function test_services_validator_getter_setter() {
$loader = $this->mockLoader();
$validator = $this->getMockBuilder( 'WC_Connect_Service_Schemas_Validator' )
->disableOriginalConstructor()
->getMock();
$loader->set_service_schemas_validator( $validator );
$this->assertEquals( $validator, $loader->get_service_schemas_validator() );
}
/**
* @covers WC_Connect_Loader::load_dependencies
*/
public function test_load_dependencies() {
$loader = $this->mockLoader();
$loader->load_dependencies();
$this->assertInstanceOf( 'WC_Connect_Logger', $loader->get_logger() );
$this->assertInstanceOf( 'WC_Connect_API_Client', $loader->get_api_client() );
$this->assertInstanceOf( 'WC_Connect_Service_Schemas_Validator', $loader->get_service_schemas_validator() );
$this->assertInstanceOf( 'WC_Connect_Service_Schemas_Store', $loader->get_service_schemas_store() );
$this->assertInstanceOf( 'WC_Connect_Tracks', $loader->get_tracks() );
}
/**
* @covers WC_Connect_Loader::init_service
*/
public function test_init_service() {
$store = $this->getMockBuilder( 'WC_Connect_Service_Schemas_Store' )
->disableOriginalConstructor()
->setMethods( array( 'get_service_schema_by_id_or_instance_id' ) )
->getMock();
$service_data = array(
'method_id' => 'test_method',
);
$store->expects( $this->any() )
->method( 'get_service_schema_by_id_or_instance_id' )
->will( $this->returnValue( $service_data ) );
$loader = $this->mockLoader( $store );
$method = new WC_Connect_Shipping_Method();
$loader->init_service( $method, 1 );
$this->assertEquals( $loader->get_logger(), $method->get_logger() );
$this->assertEquals( $loader->get_api_client(), $method->get_api_client() );
$this->assertEquals( $service_data, $method->get_service_schema() );
}
/**
* @covers WC_Connect_Loader::is_wc_connect_shipping_service
*/
public function test_is_wc_connect_shipping_service() {
$loader = $this->mockLoaderAndActiveShippingMethods();
$this->assertTrue( $loader->is_wc_connect_shipping_service( 'test_method_that_is_from_wc_connect' ) );
$this->assertFalse( $loader->is_wc_connect_shipping_service( 'test_method_that_is_not_from_wc_connect' ) );
}
/**
* @covers WC_Connect_Loader::shipping_zone_method_added
*/
public function test_shipping_zone_method_added() {
$loader = $this->mockLoaderAndActiveShippingMethods();
$this->assertEquals( 0, did_action( 'wc_connect_shipping_zone_method_added' ) );
$loader->shipping_zone_method_added( 3, 'test_method_that_is_from_wc_connect', 2 );
$this->assertEquals( 1, did_action( 'wc_connect_shipping_zone_method_added' ) );
$loader->shipping_zone_method_added( 3, 'test_method_that_is_not_from_wc_connect', 2 );
$this->assertEquals( 1, did_action( 'wc_connect_shipping_zone_method_added' ) );
$loader->shipping_zone_method_added( 3, 'test_method_that_is_from_wc_connect', 2 );
$this->assertEquals( 2, did_action( 'wc_connect_shipping_zone_method_added' ) );
}
/**
* When the StoreApi class is present, extend_store_api() registers the
* plugin's Store API extensions.
*
* @covers WC_Connect_Loader::extend_store_api
*/
public function test_extend_store_api_registers_when_store_api_available() {
$sut = $this->getMockBuilder( 'WC_Connect_Loader' )
->disableOriginalConstructor()
->setMethods( array( 'is_store_api_available', 'register_store_api_extensions' ) )
->getMock();
$sut->method( 'is_store_api_available' )->willReturn( true );
$sut->expects( $this->once() )->method( 'register_store_api_extensions' );
$sut->extend_store_api();
}
/**
* On WooCommerce versions without the StoreApi class, extend_store_api()
* skips registration instead of fataling on the missing class. This is the
* guard that prevents the `Class "…\StoreApi" not found` fatal (WOOTAX-298).
*
* @covers WC_Connect_Loader::extend_store_api
*/
public function test_extend_store_api_skips_registration_when_store_api_unavailable() {
$sut = $this->getMockBuilder( 'WC_Connect_Loader' )
->disableOriginalConstructor()
->setMethods( array( 'is_store_api_available', 'register_store_api_extensions' ) )
->getMock();
$sut->method( 'is_store_api_available' )->willReturn( false );
$sut->expects( $this->never() )->method( 'register_store_api_extensions' );
// Must not throw when the StoreApi class is absent.
$this->assertNull( $sut->extend_store_api() );
}
/**
* is_store_api_available() returns true when the StoreApi class the plugin
* depends on is present. The PHPUnit harness loads WooCommerce, so the class
* exists here - pinning to true means a typo or rename in the guarded class
* string would fail this test rather than silently tracking the change.
*
* @covers WC_Connect_Loader::is_store_api_available
*/
public function test_is_store_api_available_returns_true_when_store_api_class_present() {
// Precondition: the harness must actually load the StoreApi class for the
// assertion below to be meaningful.
$this->assertTrue(
class_exists( '\Automattic\WooCommerce\StoreApi\StoreApi' ),
'Test precondition: the StoreApi class must be loaded in the test harness.'
);
$sut = $this->getMockBuilder( 'WC_Connect_Loader' )
->disableOriginalConstructor()
->setMethods( null )
->getMock();
$method = new ReflectionMethod( 'WC_Connect_Loader', 'is_store_api_available' );
$method->setAccessible( true );
$this->assertTrue(
$method->invoke( $sut ),
'is_store_api_available() should return true when the StoreApi class is present.'
);
}
/**
* On the unavailable path, extend_store_api() logs a notice - but at most
* once per day, because it runs on `woocommerce_blocks_loaded` (nearly every
* request). The throttle transient must suppress the second same-day call.
*
* @covers WC_Connect_Loader::extend_store_api
* @covers WC_Connect_Loader::log_store_api_unavailable
*/
public function test_extend_store_api_logs_unavailable_notice_once_per_day() {
delete_transient( 'wcservices_store_api_unavailable_logged' );
// Spy logger injected via the woocommerce_logging_class filter. Returning
// an object bypasses wc_get_logger()'s static cache.
$logger = $this->getMockBuilder( 'WC_Logger_Interface' )->getMock();
$logger->expects( $this->once() )
->method( 'notice' )
->with(
'StoreApi class not found. Store API extensions will not be registered.',
array( 'source' => 'woocommerce-services' )
);
$inject_logger = function () use ( $logger ) {
return $logger;
};
add_filter( 'woocommerce_logging_class', $inject_logger );
$sut = $this->getMockBuilder( 'WC_Connect_Loader' )
->disableOriginalConstructor()
->setMethods( array( 'is_store_api_available', 'register_store_api_extensions' ) )
->getMock();
$sut->method( 'is_store_api_available' )->willReturn( false );
$sut->expects( $this->never() )->method( 'register_store_api_extensions' );
// First skip logs the notice; the second same-day skip is throttled.
$sut->extend_store_api();
$sut->extend_store_api();
remove_filter( 'woocommerce_logging_class', $inject_logger );
delete_transient( 'wcservices_store_api_unavailable_logged' );
}
/**
* When the container cannot resolve ExtendSchema, get_store_api_extend_schema()
* returns null and register_store_api_extensions() must skip quietly instead of
* passing null into StoreApiExtensionController's typed constructor (WOOTAX-303).
*
* @covers WC_Connect_Loader::register_store_api_extensions
*/
public function test_register_store_api_extensions_skips_when_schema_unavailable() {
$sut = $this->getMockBuilder( 'WC_Connect_Loader' )
->disableOriginalConstructor()
->setMethods( array( 'get_store_api_extend_schema' ) )
->getMock();
$sut->method( 'get_store_api_extend_schema' )->willReturn( null );
$method = new ReflectionMethod( 'WC_Connect_Loader', 'register_store_api_extensions' );
$method->setAccessible( true );
// Must not throw a TypeError; the early return yields void (null).
$this->assertNull( $method->invoke( $sut ) );
}
/**
* When resolution has already been attempted and failed, instance() returns
* null instead of fataling on an uninitialized typed static, and the $attempted
* guard prevents a re-resolution (which, against the working test-harness
* container, would otherwise return a non-null instance) (WOOTAX-303).
*
* @covers Automattic\WCServices\StoreApi\StoreApiExtendSchema::instance
*/
public function test_instance_returns_null_when_resolution_failed() {
$class = '\Automattic\WCServices\StoreApi\StoreApiExtendSchema';
$attempted = new ReflectionProperty( $class, 'attempted' );
$instance = new ReflectionProperty( $class, 'instance' );
$attempted->setAccessible( true );
$instance->setAccessible( true );
// Static state leaks across tests; capture and restore it.
$orig_attempted = $attempted->getValue();
$orig_instance = $instance->getValue();
try {
$attempted->setValue( true ); // resolution already attempted...
$instance->setValue( null ); // ...and it failed.
// Non-vacuity relies on the test harness container resolving a real
// instance on re-entry: if the $attempted guard were removed, instance()
// would re-run the constructor and return that non-null instance,
// failing this assertion.
$this->assertNull( \Automattic\WCServices\StoreApi\StoreApiExtendSchema::instance() );
} finally {
$attempted->setValue( $orig_attempted );
$instance->setValue( $orig_instance );
}
}
/**
* The $attempted latch is what stops container resolution (and its debug log)
* from re-running on every request. This asserts the latch engages on the first
* call regardless of whether resolution succeeds or fails (WOOTAX-303).
*
* @testdox instance() sets the attempted latch on the first call.
* @covers Automattic\WCServices\StoreApi\StoreApiExtendSchema::instance
*/
public function test_instance_latches_attempted_on_first_call() {
$class = '\Automattic\WCServices\StoreApi\StoreApiExtendSchema';
$attempted = new ReflectionProperty( $class, 'attempted' );
$instance = new ReflectionProperty( $class, 'instance' );
$attempted->setAccessible( true );
$instance->setAccessible( true );
$orig_attempted = $attempted->getValue();
$orig_instance = $instance->getValue();
try {
$attempted->setValue( false );
$instance->setValue( null );
\Automattic\WCServices\StoreApi\StoreApiExtendSchema::instance();
$this->assertTrue( $attempted->getValue(), 'instance() must latch $attempted on the first call so resolution runs at most once per request.' );
} finally {
$attempted->setValue( $orig_attempted );
$instance->setValue( $orig_instance );
}
}
/**
* Once resolution has been attempted, instance() must return the already-resolved
* instance without re-entering the container. A distinct ExtendSchema sentinel
* (built without the constructor, so it is never the container's shared instance)
* makes this deterministic: if the $attempted guard were removed, instance() would
* re-resolve and return a different object, failing the identity assertion. This
* avoids the working-container dependency of the null-path test (WOOTAX-303).
*
* @testdox instance() returns the cached instance without re-resolving once attempted.
* @covers Automattic\WCServices\StoreApi\StoreApiExtendSchema::instance
*/
public function test_instance_returns_cached_instance_without_reresolving() {
$class = '\Automattic\WCServices\StoreApi\StoreApiExtendSchema';
$attempted = new ReflectionProperty( $class, 'attempted' );
$instance = new ReflectionProperty( $class, 'instance' );
$attempted->setAccessible( true );
$instance->setAccessible( true );
$orig_attempted = $attempted->getValue();
$orig_instance = $instance->getValue();
// ExtendSchema is final; build a distinct real instance without its constructor.
$sentinel = ( new ReflectionClass( '\Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema' ) )->newInstanceWithoutConstructor();
try {
$attempted->setValue( true );
$instance->setValue( $sentinel );
$this->assertSame( $sentinel, \Automattic\WCServices\StoreApi\StoreApiExtendSchema::instance(), 'instance() must return the cached instance without re-resolving once resolution has been attempted.' );
} finally {
$attempted->setValue( $orig_attempted );
$instance->setValue( $orig_instance );
}
}
/**
* When the container throws while resolving ExtendSchema, instance() must catch it
* and return null instead of fataling. A TypeError is used deliberately: it is a
* Throwable but not an Exception, so pre-fix code (catch Exception) would let it
* propagate. The failure must also be logged once (WOOTAX-303).
*
* @testdox instance() returns null and logs when the container throws a non-Exception Throwable.
* @covers Automattic\WCServices\StoreApi\StoreApiExtendSchema::instance
*/
public function test_instance_returns_null_when_container_throws() {
$class = '\Automattic\WCServices\StoreApi\StoreApiExtendSchema';
$attempted = new ReflectionProperty( $class, 'attempted' );
$instance = new ReflectionProperty( $class, 'instance' );
$attempted->setAccessible( true );
$instance->setAccessible( true );
$orig_attempted = $attempted->getValue();
$orig_instance = $instance->getValue();
$logger = $this->getMockBuilder( 'WC_Logger_Interface' )->getMock();
$logger->expects( $this->once() )
->method( 'debug' )
->with( 'Failed to get ExtendSchema instance.', $this->anything() );
$inject_logger = function () use ( $logger ) {
return $logger;
};
add_filter( 'woocommerce_logging_class', $inject_logger );
try {
$attempted->setValue( false );
$instance->setValue( null );
$result = WCServices_Throwing_Store_Api_Extend_Schema::instance();
$this->assertNull( $result, 'instance() must return null - not fatal - when the container throws a non-Exception Throwable.' );
} finally {
remove_filter( 'woocommerce_logging_class', $inject_logger );
$attempted->setValue( $orig_attempted );
$instance->setValue( $orig_instance );
}
}
}