-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathAbstractSettingsTest.php
More file actions
626 lines (441 loc) · 20.7 KB
/
AbstractSettingsTest.php
File metadata and controls
626 lines (441 loc) · 20.7 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
<?php
use SkyVerge\WooCommerce\PluginFramework\v6_0_2 as Framework;
use SkyVerge\WooCommerce\PluginFramework\v6_0_2\Settings_API\Abstract_Settings;
use SkyVerge\WooCommerce\PluginFramework\v6_0_2\Settings_API\Setting;
use SkyVerge\WooCommerce\PluginFramework\v6_0_2\Settings_API\Control;
use SkyVerge\WooCommerce\PluginFramework\v6_0_2\SV_WC_Plugin_Exception;
/**
* Tests for the Abstract_Settings class.
*
* @see \SkyVerge\WooCommerce\PluginFramework\v6_0_2\Settings_API\Abstract_Settings
*/
class AbstractSettingsTest extends \Codeception\TestCase\WPTestCase {
/** @var \IntegrationTester */
protected $tester;
/** @var Abstract_Settings */
protected $settings;
protected function _before() {
require_once 'woocommerce/Settings_API/Abstract_Settings.php';
require_once 'woocommerce/Settings_API/Control.php';
require_once 'woocommerce/Settings_API/Setting.php';
}
protected function _after() {
}
/** Tests *********************************************************************************************************/
/** @see Abstract_Settings::get_id() */
public function test_get_id() {
$this->assertEquals( 'test-plugin', $this->get_settings_instance()->get_id() );
}
/**
* @see Abstract_Settings::register_setting()
* @see Abstract_Settings::get_setting()
*/
public function test_register_setting() {
$this->assertTrue( $this->get_settings_instance()->register_setting( 'test-setting-d', Setting::TYPE_EMAIL, [
'name' => 'Test Setting D',
'description' => 'Description of setting D',
] ) );
$this->assertInstanceOf( Setting::class, $this->get_settings_instance()->get_setting( 'test-setting-d' ) );
// existing setting ID
$this->assertFalse( $this->get_settings_instance()->register_setting( 'test-setting-d', Setting::TYPE_EMAIL, [
'name' => 'Test Setting D',
'description' => 'Description of setting D',
] ) );
// invalid setting type
$this->assertFalse( $this->get_settings_instance()->register_setting( 'test-setting-e', 'invalid-type', [
'name' => 'Test Setting E',
'description' => 'Description of setting E',
] ) );
$this->assertNull( $this->get_settings_instance()->get_setting( 'test-setting-e' ) );
}
/**
* @see Abstract_Settings::load_settings()
*
* Stored values are defined in get_settings_instance().
*/
public function test_load_settings() {
$this->get_settings_instance()->register_setting( 'test-setting-d', Setting::TYPE_EMAIL, [
'name' => 'Test Setting D',
'description' => 'Description of setting D',
] );
$this->assertSame( 'something', $this->get_settings_instance()->get_setting( 'test-setting-a' )->get_value() );
$this->assertSame( 1729, $this->get_settings_instance()->get_setting( 'test-setting-b' )->get_value() );
$this->assertSame( true, $this->get_settings_instance()->get_setting( 'test-setting-c' )->get_value() );
$this->assertSame( null, $this->get_settings_instance()->get_setting( 'test-setting-d' )->get_value() );
}
/**
* @see Abstract_Settings::unregister_setting()
* @see Abstract_Settings::get_setting()
*/
public function test_unregister_setting() {
$this->assertInstanceOf( Setting::class, $this->get_settings_instance()->get_setting( 'test-setting-a' ) );
$this->get_settings_instance()->unregister_setting( 'test-setting-a' );
$this->assertNull( $this->get_settings_instance()->get_setting( 'test-setting-a' ) );
}
/**
* @see Abstract_Settings::register_control()
*
* @param string $setting_id the setting ID
* @param string $control_type the control type
* @param string[] $setting_control_types the control types valid for the setting
* @param bool $registered whether the control should be successfully registered or not
*
* @dataProvider provider_register_control
*/
public function test_register_control( $setting_id, $control_type, $setting_control_types, $registered ) {
$this->get_settings_instance()->register_setting( 'registered_setting', Setting::TYPE_STRING );
if ( ! empty( $setting_control_types ) ) {
add_filter( "wc_{$this->get_settings_instance()->get_id()}_settings_api_setting_control_types", function () use ( $setting_control_types ) {
return $setting_control_types;
} );
}
$this->assertSame( $registered, $this->get_settings_instance()->register_control( $setting_id, $control_type ) );
if ( $registered ) {
$this->assertInstanceOf( Control::class, $this->get_settings_instance()->get_setting( $setting_id )->get_control() );
} elseif ( $setting = $this->get_settings_instance()->get_setting( $setting_id ) ) {
$this->assertNull( $setting->get_control() );
}
}
/** @see test_register_control() */
public function provider_register_control() {
require_once 'woocommerce/Settings_API/Control.php';
return [
[ 'unknown_setting', Control::TYPE_TEXT, [], false ],
[ 'registered_setting', 'invalid_type', [], false ],
[ 'registered_setting', Control::TYPE_TEXT, [], true ],
[ 'registered_setting', Control::TYPE_TEXT, [ Control::TYPE_TEXT, Control::TYPE_SELECT ], true ],
[ 'registered_setting', Control::TYPE_EMAIL, [ Control::TYPE_TEXT, Control::TYPE_SELECT ], false ],
];
}
/** @see Abstract_Settings::register_control() */
public function test_register_control_args() {
$setting_args = [
'name' => 'Setting Name',
'description' => 'Setting Description',
'options' => [ 'black', 'white' ],
];
$this->get_settings_instance()->register_setting( 'color', Setting::TYPE_STRING, $setting_args );
$this->get_settings_instance()->register_control( 'color', Control::TYPE_SELECT, [
'options' => [
'black' => 'Black',
'white' => 'White',
'red' => 'Red',
],
] );
$control = $this->get_settings_instance()->get_setting( 'color' )->get_control();
// TODO: uncomment assert for $control->get_options when https://github.com/skyverge/wc-plugin-framework/pull/453 is merged {WV 2020-03-20}
$this->assertEquals( 'color', $control->get_setting_id() );
$this->assertEquals( Control::TYPE_SELECT, $control->get_type() );
$this->assertEquals( $setting_args['name'], $control->get_name() );
$this->assertEquals( $setting_args['description'], $control->get_description() );
// $this->assertEquals( $setting_args['options'], array_keys( $control->get_options() ) );
}
/**
* @see Abstract_Settings::get_settings()
*
* @param array $ids settings IDs to get
* @param array $expected_ids expected settings IDs to retrieve
*
* @dataProvider provider_get_settings
*/
public function test_get_settings( $ids, $expected_ids ) {
$settings = $this->get_settings_instance()->get_settings( $ids );
$this->assertEquals( array_keys( $settings ), $expected_ids );
}
/** @see test_get_settings() */
public function provider_get_settings() {
return [
[ [ 'test-setting-a', 'test-setting-b' ], [ 'test-setting-a', 'test-setting-b' ] ],
[ [], [ 'test-setting-a', 'test-setting-b', 'test-setting-c' ] ],
[ [ 'test-setting-x' ], [] ],
];
}
/** @see Abstract_Settings::get_value() */
public function test_get_value() {
$setting = $this->get_settings_instance()->get_setting( 'test-setting-b' );
$setting->set_value( 1000 );
$this->get_settings_instance()->save( $setting->get_id() );
$this->assertEquals( 1000, $this->get_settings_instance()->get_value( $setting->get_id() ) );
}
/**
* @see Abstract_Settings::get_value()
*
* @param mixed $expected_value the returned value
* @param bool $with_default whether to return the default value if nothing is stored
* @throws Framework\SV_WC_Plugin_Exception
*
* @dataProvider provider_get_value_nothing_stored
*/
public function test_get_value_nothing_stored( $expected_value, $with_default ) {
$setting = $this->get_settings_instance()->get_setting( 'test-setting-b' );
$this->get_settings_instance()->delete_value( $setting->get_id() );
$this->assertEquals( $expected_value, $this->get_settings_instance()->get_value( $setting->get_id(), $with_default ) );
}
/** @see test_get_value_nothing_stored() */
public function provider_get_value_nothing_stored() {
return [
[ 3600, true ],
[ null, false ],
];
}
/** @see Abstract_Settings::get_value() */
public function test_get_value_exception() {
$this->expectException( Framework\SV_WC_Plugin_Exception::class );
$this->get_settings_instance()->get_value( 'not_a_setting' );
}
/**
* @see Abstract_Settings::update_value()
*
* @param bool $register whether to register a new setting
* @param mixed $value value to pass to method
* @param string $type setting type
* @param array $options setting options
* @param mixed $expected setting value after execution
* @param bool $exception whether an exception is expected
* @throws Framework\SV_WC_Plugin_Exception
*
* @dataProvider provider_update_value
*/
public function test_update_value( $register, $value, $type, $options, $expected, $exception ) {
if ( $exception ) {
$this->expectException( SV_WC_Plugin_Exception::class );
}
$setting_id = 'test-setting';
$option_name = $this->get_settings_instance()->get_option_name_prefix() . '_' . $setting_id;
if ( $register ) {
$this->get_settings_instance()->register_setting( $setting_id, $type, [ 'options' => $options ] );
}
$this->get_settings_instance()->update_value( $setting_id, $value );
$this->assertSame( $expected, $this->get_settings_instance()->get_setting( $setting_id )->get_value() );
$setting = $this->get_settings_instance()->get_setting( $setting_id );
$method = new ReflectionMethod( Abstract_Settings::class, 'get_value_from_database' );
$method->setAccessible( true );
$this->assertEquals( $expected, $method->invokeArgs( $this->get_settings_instance(), [
get_option( $option_name ),
$setting
] ) );
}
/**
* Provider for test_update_value()
*
* @return array
*/
public function provider_update_value() {
require_once( 'woocommerce/Settings_API/Setting.php' );
return [
[ false, 'valid', Setting::TYPE_STRING, [], null, true ],
[ true, 'valid', Setting::TYPE_STRING, [], 'valid', false ],
[ true, 123, Setting::TYPE_STRING, [], null, true ],
[ true, 'green', Setting::TYPE_STRING, [ 'green', 'red' ], 'green', false ],
[ true, 'not an option', Setting::TYPE_STRING, [ 'green', 'red' ], null, true ],
[ true, 'https://skyverge.com/', Setting::TYPE_URL, [], 'https://skyverge.com/', false ],
[ true, 'file:///tmp/', Setting::TYPE_URL, [], null, true ],
[ true, 'https://skyverge.com/', Setting::TYPE_URL, [ 'https://skyverge.com/', 'http://skyverge.com/' ], 'https://skyverge.com/', false ],
[ true, 'https://google.com/', Setting::TYPE_URL, [ 'https://skyverge.com/', 'http://skyverge.com/' ], null, true ],
[ true, 'test@example.com', Setting::TYPE_EMAIL, [], 'test@example.com', false ],
[ true, 'not-an-email.com', Setting::TYPE_EMAIL, [], null, true ],
[ true, 'test@example.com', Setting::TYPE_EMAIL, [ 'test@example.com' ], 'test@example.com', false ],
[ true, 'another@example.com', Setting::TYPE_EMAIL, [ 'test@example.com' ], null, true ],
[ true, 12345, Setting::TYPE_INTEGER, [], 12345, false ],
[ true, 1.345, Setting::TYPE_INTEGER, [], null, true ],
[ true, '234', Setting::TYPE_INTEGER, [], null, true ],
[ true, 1, Setting::TYPE_INTEGER, [ 1, 2 ], 1, false ],
[ true, 3, Setting::TYPE_INTEGER, [ 1, 2 ], null, true ],
[ true, 12345, Setting::TYPE_FLOAT, [], 12345, false ],
[ true, 1.345, Setting::TYPE_FLOAT, [], 1.345, false ],
[ true, '234', Setting::TYPE_FLOAT, [], null, true ],
[ true, 1.5, Setting::TYPE_FLOAT, [ 1.5, 2.5 ], 1.5, false ],
[ true, 3.5, Setting::TYPE_FLOAT, [ 1.5, 2.5 ], null, true ],
[ true, true, Setting::TYPE_BOOLEAN, [], true, false ],
[ true, 'yes', Setting::TYPE_BOOLEAN, [], null, true ],
[ true, 1, Setting::TYPE_BOOLEAN, [], null, true ],
// it beats me why someone would have a boolean setting with only one option, but in theory it is possible
[ true, true, Setting::TYPE_BOOLEAN, [ true ], true, false ],
[ true, false, Setting::TYPE_BOOLEAN, [ true ], null, true ],
];
}
/** @see Abstract_Settings::delete_value() */
public function test_delete_value() {
$setting = $this->get_settings_instance()->get_setting( 'test-setting-a' );
$setting->set_value( 'something' );
$this->get_settings_instance()->save( 'test-setting-a' );
$option_name = $this->get_settings_instance()->get_option_name_prefix() . '_' . $setting->get_id();
$this->assertNotEmpty( $setting->get_value() );
$this->assertNotEmpty( get_option( $option_name ) );
$this->get_settings_instance()->delete_value( $setting->get_id() );
$this->assertNull( $setting->get_value() );
$this->assertFalse( get_option( $option_name ) );
}
/** @see Abstract_Settings::save() */
public function test_save() {
$setting_a = $this->get_settings_instance()->get_setting( 'test-setting-a' );
$setting_b = $this->get_settings_instance()->get_setting( 'test-setting-b' );
$option_name_a = $this->get_settings_instance()->get_option_name_prefix() . '_' . $setting_a->get_id();
update_option( $option_name_a, 'old value' );
$option_name_b = $this->get_settings_instance()->get_option_name_prefix() . '_' . $setting_b->get_id();
update_option( $option_name_b, - 1 );
$setting_a->set_value( 'new value' );
$setting_b->set_value( 2 );
$this->assertEquals( 'new value', $setting_a->get_value() );
$this->assertEquals( 'old value', get_option( $option_name_a ) );
$this->assertEquals( 2, $setting_b->get_value() );
$this->assertEquals( - 1, get_option( $option_name_b ) );
$this->get_settings_instance()->save();
$this->assertEquals( 'new value', $setting_a->get_value() );
$this->assertEquals( 'new value', get_option( $option_name_a ) );
$this->assertEquals( 2, $setting_b->get_value() );
$this->assertEquals( 2, get_option( $option_name_b ) );
}
/** @see Abstract_Settings::save() */
public function test_save_single_setting() {
$setting_a = $this->get_settings_instance()->get_setting( 'test-setting-a' );
$setting_b = $this->get_settings_instance()->get_setting( 'test-setting-b' );
$option_name_a = $this->get_settings_instance()->get_option_name_prefix() . '_' . $setting_a->get_id();
update_option( $option_name_a, 'old value' );
$option_name_b = $this->get_settings_instance()->get_option_name_prefix() . '_' . $setting_b->get_id();
update_option( $option_name_b, - 1 );
$setting_a->set_value( 'new value' );
$setting_b->set_value( 2 );
$this->assertEquals( 'new value', $setting_a->get_value() );
$this->assertEquals( 'old value', get_option( $option_name_a ) );
$this->assertEquals( 2, $setting_b->get_value() );
$this->assertEquals( - 1, get_option( $option_name_b ) );
$this->get_settings_instance()->save( 'test-setting-a' );
$this->assertEquals( 'new value', $setting_a->get_value() );
$this->assertEquals( 'new value', get_option( $option_name_a ) );
$this->assertEquals( 2, $setting_b->get_value() );
$this->assertEquals( - 1, get_option( $option_name_b ) );
}
/** @see Abstract_Settings::delete_value() */
public function test_delete_value_exception() {
$this->expectException( Framework\SV_WC_Plugin_Exception::class );
$this->get_settings_instance()->delete_value( 'not_a_setting' );
}
/**
* @see Abstract_Settings::get_value_for_database()
*
* @param string $type the setting type
* @param mixed $value the current setting value
* @param mixed $expected_value the converted value
*
* @dataProvider provider_get_value_for_database
*/
public function test_get_value_for_database( $type, $value, $expected_value ) {
$setting = new Setting();
$setting->set_type( $type );
$setting->set_value( $value );
$method = new ReflectionMethod( Abstract_Settings::class, 'get_value_for_database' );
$method->setAccessible( true );
$this->assertSame( $expected_value, $method->invoke( $this->get_settings_instance(), $setting ) );
}
/** @see test_get_value_for_database() */
public function provider_get_value_for_database() {
return [
'string' => [ Setting::TYPE_STRING, 'hello', 'hello' ],
'url' => [ Setting::TYPE_URL, 'https://skyverge.com', 'https://skyverge.com' ],
'email' => [ Setting::TYPE_EMAIL, 'hello@example.com', 'hello@example.com' ],
'integer' => [ Setting::TYPE_INTEGER, 1234, 1234 ],
'float' => [ Setting::TYPE_FLOAT, 12.4, 12.4 ],
'boolean true' => [ Setting::TYPE_BOOLEAN, true, 'yes' ],
'boolean false' => [ Setting::TYPE_BOOLEAN, false, 'no' ],
];
}
/**
* @see Abstract_Settings::get_value_from_database()
*
* @param mixed $value the value stored in the database
* @param mixed $expected_value the converted value
* @param string $type the setting type
*
* @dataProvider provider_get_value_from_database
*/
public function test_get_value_from_database( $value, $expected_value, $type ) {
$setting = new Setting();
$setting->set_type( $type );
$method = new ReflectionMethod( Abstract_Settings::class, 'get_value_from_database' );
$method->setAccessible( true );
$this->assertSame( $expected_value, $method->invokeArgs( $this->get_settings_instance(), [ $value, $setting ] ) );
}
/** @see test_get_value_from_database() */
public function provider_get_value_from_database() {
require_once 'woocommerce/Settings_API/Setting.php';
return [
[ '12345', 12345, Setting::TYPE_INTEGER ],
[ '12.45', 12, Setting::TYPE_INTEGER ],
[ '0', 0, Setting::TYPE_INTEGER ],
[ 'hello', null, Setting::TYPE_INTEGER ],
[ null, null, Setting::TYPE_INTEGER ],
[ '', null, Setting::TYPE_INTEGER ],
[ '12345', 12345.0, Setting::TYPE_FLOAT ],
[ '12.45', 12.45, Setting::TYPE_FLOAT ],
[ '0', 0.0, Setting::TYPE_FLOAT ],
[ 'hello', null, Setting::TYPE_FLOAT ],
[ null, null, Setting::TYPE_FLOAT ],
[ '', null, Setting::TYPE_FLOAT ],
[ 'yes', true, Setting::TYPE_BOOLEAN ],
[ 'no', false, Setting::TYPE_BOOLEAN ],
[ '1', true, Setting::TYPE_BOOLEAN ],
[ '0', false, Setting::TYPE_BOOLEAN ],
[ 'hey', false, Setting::TYPE_BOOLEAN ],
[ null, null, Setting::TYPE_BOOLEAN ],
[ '', false, Setting::TYPE_BOOLEAN ],
];
}
/** @see Abstract_Settings::get_setting_types() */
public function test_get_setting_types() {
$this->assertIsArray( $this->get_settings_instance()->get_setting_types() );
add_filter( "wc_{$this->get_settings_instance()->get_id()}_settings_api_setting_types", function() {
return [ 'my_type' ];
} );
$this->assertEquals( [ 'my_type' ], $this->get_settings_instance()->get_setting_types() );
}
/** @see Abstract_Settings::get_control_types() */
public function test_get_control_types() {
$this->assertIsArray( $this->get_settings_instance()->get_control_types() );
add_filter( "wc_{$this->get_settings_instance()->get_id()}_settings_api_control_types", function() {
return [ 'my_type' ];
} );
$this->assertEquals( [ 'my_type' ], $this->get_settings_instance()->get_control_types() );
}
/** @see Abstract_Settings::get_setting_control_types() */
public function test_get_setting_control_types() {
$setting = $this->get_settings_instance()->get_setting( 'test-setting-a' );
$this->assertIsArray( $this->get_settings_instance()->get_setting_control_types( $setting ) );
add_filter( "wc_{$this->get_settings_instance()->get_id()}_settings_api_setting_control_types", function() {
return [ 'my_type' ];
} );
$this->assertEquals( [ 'my_type' ], $this->get_settings_instance()->get_setting_control_types( $setting ) );
}
/** Helper methods ************************************************************************************************/
/**
* Gets the settings instance.
*
* @return Abstract_Settings
*/
protected function get_settings_instance() {
if ( null === $this->settings ) {
$this->settings = new class( 'test-plugin' ) extends Abstract_Settings {
protected function register_settings() {
$this->register_setting( 'test-setting-a', Setting::TYPE_STRING, [
'name' => 'Test Setting A',
'description' => 'Description of setting A',
] );
$this->register_setting( 'test-setting-b', Setting::TYPE_INTEGER, [
'name' => 'Test Setting B',
'description' => 'Description of setting B',
'default' => 3600,
] );
$this->register_setting( 'test-setting-c', Setting::TYPE_BOOLEAN, [
'name' => 'Test Setting C',
'description' => 'Description of setting C',
'default' => true,
] );
update_option( "{$this->get_option_name_prefix()}_{$this->settings['test-setting-a']->get_id()}", 'something' );
update_option( "{$this->get_option_name_prefix()}_{$this->settings['test-setting-b']->get_id()}", '1729' );
update_option( "{$this->get_option_name_prefix()}_{$this->settings['test-setting-c']->get_id()}", 'yes' );
}
};
}
return $this->settings;
}
}