Skip to content

Commit d778c7c

Browse files
authored
Merge pull request #1165 from xwp/tests/settings-connector
Settings connector test implemented.
2 parents c4ebbac + ec330c8 commit d778c7c

9 files changed

+236
-120
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"license": "GPL-2.0-or-later",
77
"repositories": [
88
{
9-
"type":"composer",
10-
"url":"https://wpackagist.org"
9+
"type": "composer",
10+
"url": "https://wpackagist.org"
1111
}
1212
],
1313
"require": {

composer.lock

Lines changed: 81 additions & 85 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

connectors/class-connector-settings.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Connector_Settings extends Connector {
3131
* @var array
3232
*/
3333
public $actions = array(
34-
'whitelist_options',
34+
'allowed_options',
3535
'update_option',
3636
'update_site_option',
3737
'update_option_permalink_structure',
@@ -618,12 +618,12 @@ function( $rule ) use ( $submenu, $record ) {
618618
* @action update_option
619619
*
620620
* @param string $option Option name.
621-
* @param mixed $value Option new value.
622621
* @param mixed $old_value Option old value.
622+
* @param mixed $value Option new value.
623623
*/
624-
public function callback_update_option( $option, $value, $old_value ) {
624+
public function callback_update_option( $option, $old_value, $value ) {
625625
if ( ( defined( '\WP_CLI' ) && \WP_CLI || did_action( 'customize_save' ) ) && array_key_exists( $option, $this->labels ) ) {
626-
$this->callback_updated_option( $option, $value, $old_value );
626+
$this->callback_updated_option( $option, $old_value, $value );
627627
}
628628
}
629629

@@ -636,7 +636,7 @@ public function callback_update_option( $option, $value, $old_value ) {
636636
*
637637
* @return array
638638
*/
639-
public function callback_whitelist_options( $options ) {
639+
public function callback_allowed_options( $options ) {
640640
add_action( 'updated_option', array( $this, 'callback' ), 10, 3 );
641641

642642
return $options;
@@ -664,7 +664,7 @@ public function callback_update_option_permalink_structure( $old_value, $value )
664664
* @param mixed $old_value Option old value.
665665
*/
666666
public function callback_update_site_option( $option, $value, $old_value ) {
667-
$this->callback_updated_option( $option, $value, $old_value );
667+
$this->callback_updated_option( $option, $old_value, $value );
668668
}
669669

670670
/**

tests/tests/connectors/test-class-connector-acf.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
/**
33
* WP Integration Test w/ Advanced Custom Fields
44
*
5-
* Tests for ACF Connector class callbacks.
5+
* Tests for ACF connector class callbacks.
6+
*
7+
* @package WP_Stream
68
*/
79
namespace WP_Stream;
810

tests/tests/connectors/test-class-connector-blogs.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<?php
2+
/**
3+
* Tests for Blogs connector class callbacks.
4+
*
5+
* @package WP_Stream
6+
*/
27
namespace WP_Stream;
38

49
class Test_WP_Stream_Connector_Blogs extends WP_StreamTestCase {
@@ -16,11 +21,11 @@ public function setUp() {
1621
$this->connector_blogs = new Connector_Blogs;
1722
$this->assertNotEmpty( $this->connector_blogs );
1823
}
19-
24+
2025
/**
2126
* Test for get_context_labels().
2227
*
23-
* @group ms-required
28+
* @group ms-required
2429
*/
2530
public function test_get_context_labels() {
2631
if ( ! is_multisite() ) {

tests/tests/connectors/test-class-connector-edd.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* WP Integration Test w/ Easy Digital Downloads
44
*
55
* Tests for EDD Connector class callbacks.
6+
*
7+
* @package WP_Stream
68
*/
79

810
namespace WP_Stream;

tests/tests/connectors/test-class-connector-mercator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<?php
2+
/**
3+
* Tests for Mercator connector class callbacks.
4+
*
5+
* @package WP_Stream
6+
*/
27
namespace WP_Stream;
38

49
class Test_WP_Stream_Connector_Mercator extends WP_StreamTestCase {
@@ -20,7 +25,7 @@ public function setUp() {
2025
/**
2126
* Test for get_context_labels().
2227
*
23-
* @group ms-required
28+
* @group ms-required
2429
*/
2530
public function test_get_context_labels() {
2631
if ( ! is_multisite() ) {

tests/tests/connectors/test-class-connector-posts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Tests for Post connector class callbacks.
3+
* Tests for Posts connector class callbacks.
44
*
55
* @package WP_Stream
66
*/
Lines changed: 128 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,153 @@
11
<?php
22
namespace WP_Stream;
33
/**
4-
* Class Test_Connector_Settings
4+
* Tests for Settings connector class callbacks.
55
*
66
* @package WP_Stream
77
*/
88
class Test_Connector_Settings extends WP_StreamTestCase {
9-
109
/**
11-
* Holds the connector settings base class.
12-
*
13-
* @var Connector_Settings
10+
* Runs before each test.
1411
*/
15-
protected $connector;
16-
1712
public function setUp() {
1813
parent::setUp();
19-
$this->connector = new Connector_Settings();
2014

21-
$this->assertNotEmpty( $this->connector );
15+
$this->plugin->connectors->unload_connectors();
16+
17+
// Make partial of Connector_Settings class, with mocked "log" function.
18+
$this->mock = $this->getMockBuilder( Connector_Settings::class )
19+
->setMethods( array( 'log' ) )
20+
->getMock();
21+
22+
// Register connector.
23+
$this->mock->register();
2224
}
2325

2426
public function test_is_option_ignored() {
25-
$this->assertTrue( $this->connector->is_option_ignored( '_transient_option_name' ) );
26-
$this->assertTrue( $this->connector->is_option_ignored( '_site_transient_option_name' ) );
27-
$this->assertTrue( $this->connector->is_option_ignored( 'option_name$' ) );
28-
$this->assertTrue( $this->connector->is_option_ignored( 'image_default_link_type' ) );
29-
$this->assertTrue( $this->connector->is_option_ignored( 'medium_large_size_w' ) );
30-
$this->assertTrue( $this->connector->is_option_ignored( 'medium_large_size_h' ) );
31-
32-
$this->assertFalse( $this->connector->is_option_ignored( 'option_site_transient_name' ) );
33-
$this->assertFalse( $this->connector->is_option_ignored( 'option_transient_name' ) );
34-
$this->assertFalse( $this->connector->is_option_ignored( 'option_$_name' ) );
35-
$this->assertFalse( $this->connector->is_option_ignored( 'not_ignored' ) );
27+
$this->assertTrue( $this->mock->is_option_ignored( '_transient_option_name' ) );
28+
$this->assertTrue( $this->mock->is_option_ignored( '_site_transient_option_name' ) );
29+
$this->assertTrue( $this->mock->is_option_ignored( 'option_name$' ) );
30+
$this->assertTrue( $this->mock->is_option_ignored( 'image_default_link_type' ) );
31+
$this->assertTrue( $this->mock->is_option_ignored( 'medium_large_size_w' ) );
32+
$this->assertTrue( $this->mock->is_option_ignored( 'medium_large_size_h' ) );
33+
34+
$this->assertFalse( $this->mock->is_option_ignored( 'option_site_transient_name' ) );
35+
$this->assertFalse( $this->mock->is_option_ignored( 'option_transient_name' ) );
36+
$this->assertFalse( $this->mock->is_option_ignored( 'option_$_name' ) );
37+
$this->assertFalse( $this->mock->is_option_ignored( 'not_ignored' ) );
3638

3739
// Test custom ignores.
38-
$this->assertFalse( $this->connector->is_option_ignored( 'ignore_me' ) );
40+
$this->assertFalse( $this->mock->is_option_ignored( 'ignore_me' ) );
3941

4042
add_filter( 'wp_stream_is_option_ignored', function( $is_ignored, $option_name, $default_ignored ) {
4143
return in_array( $option_name, array_merge( [ 'ignore_me' ], $default_ignored ), true );
4244
}, 10, 3 );
4345

44-
$this->assertTrue( $this->connector->is_option_ignored( 'ignore_me' ) );
46+
$this->assertTrue( $this->mock->is_option_ignored( 'ignore_me' ) );
4547
}
4648

49+
public function test_callback_updated_option() {
50+
// If multisite use site_option methods and test "update_site_option" callback
51+
// instead of the update_option callback.
52+
$add_method = is_multisite() ? 'add_site_option' : 'add_option';
53+
$update_method = is_multisite() ? 'update_site_option' : 'update_option';
54+
55+
// Create options in database for later use.
56+
call_user_func( $add_method, 'users_can_register', 0 );
57+
call_user_func( $add_method, 'permalink_structure', '' );
58+
call_user_func( $add_method, 'category_base', '' );
59+
call_user_func( $add_method, 'tag_base', '' );
60+
61+
$this->mock->expects( $this->exactly( 4 ) )
62+
->method( 'log' )
63+
->withConsecutive(
64+
array(
65+
$this->equalTo( __( '"%s" setting was updated', 'stream' ) ),
66+
$this->equalTo(
67+
array(
68+
'label' => 'Membership',
69+
'option' => 'users_can_register',
70+
'context' => 'settings',
71+
'old_value' => '0',
72+
'value' => '1',
73+
)
74+
),
75+
$this->equalTo( null ),
76+
$this->equalTo( 'settings' ),
77+
$this->equalTo( 'updated' )
78+
),
79+
array(
80+
$this->equalTo( __( '"%s" setting was updated', 'stream' ) ),
81+
$this->equalTo(
82+
array(
83+
'label' => 'Permalink Settings',
84+
'option' => 'permalink_structure',
85+
'context' => 'permalink',
86+
'old_value' => '',
87+
'value' => '/%year%/%postname%/',
88+
)
89+
),
90+
$this->equalTo( null ),
91+
$this->equalTo( 'permalink' ),
92+
$this->equalTo( 'updated' )
93+
),
94+
array(
95+
$this->equalTo( __( '"%s" setting was updated', 'stream' ) ),
96+
$this->equalTo(
97+
array(
98+
'label' => 'Category base',
99+
'option' => 'category_base',
100+
'context' => 'permalink',
101+
'old_value' => '',
102+
'value' => 'cat/',
103+
)
104+
),
105+
$this->equalTo( null ),
106+
$this->equalTo( 'permalink' ),
107+
$this->equalTo( 'updated' )
108+
),
109+
array(
110+
$this->equalTo( __( '"%s" setting was updated', 'stream' ) ),
111+
$this->equalTo(
112+
array(
113+
'label' => 'Tag base',
114+
'option' => 'tag_base',
115+
'context' => 'permalink',
116+
'old_value' => '',
117+
'value' => 'tag/',
118+
)
119+
),
120+
$this->equalTo( null ),
121+
$this->equalTo( 'permalink' ),
122+
$this->equalTo( 'updated' )
123+
)
124+
);
125+
126+
// Simulate being on the WP Customizr page.
127+
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
128+
do_action( 'customize_save', new \WP_Customize_Manager( array() ) );
129+
130+
// Update options to trigger callback.
131+
call_user_func( $update_method, 'users_can_register', 1 );
132+
133+
// Use this to prevent repeated log calls.
134+
global $wp_actions;
135+
unset( $wp_actions['customize_save'] );
136+
137+
call_user_func( $update_method, 'permalink_structure', '/%year%/%postname%/' );
138+
call_user_func( $update_method, 'category_base', 'cat/' );
139+
call_user_func( $update_method, 'tag_base', 'tag/' );
140+
141+
// If multisite only check update_site_option test callback.
142+
if ( is_multisite() ) {
143+
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_site_option' ) );
144+
} else {
145+
// Check callback test action.
146+
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_option_tag_base' ) );
147+
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_option_category_base' ) );
148+
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_option_permalink_structure' ) );
149+
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_updated_option' ) );
150+
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_option' ) );
151+
}
152+
}
47153
}

0 commit comments

Comments
 (0)