Skip to content

Commit 52fd0d4

Browse files
committed
Settings connector test implemented.
1 parent 32e6cd0 commit 52fd0d4

8 files changed

+224
-113
lines changed

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: 2 additions & 2 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',
@@ -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;

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
22
/**
3+
<<<<<<< HEAD
34
* Tests for Post connector class callbacks.
5+
=======
6+
* Tests for Posts connector class callbacks.
7+
>>>>>>> Settings connector test implemented.
48
*
59
* @package WP_Stream
610
*/
Lines changed: 119 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,144 @@
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+
// Create options in database for later use.
51+
add_option( 'users_can_register', false );
52+
add_option( 'permalink_structure', '' );
53+
add_option( 'category_base', '' );
54+
add_option( 'tag_base', '' );
55+
56+
$this->mock->expects( $this->exactly( 4 ) )
57+
->method( 'log' )
58+
->withConsecutive(
59+
array(
60+
$this->equalTo( __( '"%s" setting was updated', 'stream' ) ),
61+
$this->equalTo(
62+
array(
63+
'label' => 'Membership',
64+
'option' => 'users_can_register',
65+
'context' => 'settings',
66+
'old_value' => '0',
67+
'value' => '1',
68+
)
69+
),
70+
$this->equalTo( null ),
71+
$this->equalTo( 'settings' ),
72+
$this->equalTo( 'updated' )
73+
),
74+
array(
75+
$this->equalTo( __( '"%s" setting was updated', 'stream' ) ),
76+
$this->equalTo(
77+
array(
78+
'label' => 'Permalink Settings',
79+
'option' => 'permalink_structure',
80+
'context' => 'permalink',
81+
'old_value' => '',
82+
'value' => '/%year%/%postname%/',
83+
)
84+
),
85+
$this->equalTo( null ),
86+
$this->equalTo( 'permalink' ),
87+
$this->equalTo( 'updated' )
88+
),
89+
array(
90+
$this->equalTo( __( '"%s" setting was updated', 'stream' ) ),
91+
$this->equalTo(
92+
array(
93+
'label' => 'Category base',
94+
'option' => 'category_base',
95+
'context' => 'permalink',
96+
'old_value' => '',
97+
'value' => 'cat/',
98+
)
99+
),
100+
$this->equalTo( null ),
101+
$this->equalTo( 'permalink' ),
102+
$this->equalTo( 'updated' )
103+
),
104+
array(
105+
$this->equalTo( __( '"%s" setting was updated', 'stream' ) ),
106+
$this->equalTo(
107+
array(
108+
'label' => 'Tag base',
109+
'option' => 'tag_base',
110+
'context' => 'permalink',
111+
'old_value' => '',
112+
'value' => 'tag/',
113+
)
114+
),
115+
$this->equalTo( null ),
116+
$this->equalTo( 'permalink' ),
117+
$this->equalTo( 'updated' )
118+
)
119+
);
120+
121+
// Simulate being on the WP Customizr page.
122+
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
123+
do_action( 'customize_save', new \WP_Customize_Manager( array() ) );
124+
125+
// Update options to trigger callback.
126+
update_option( 'users_can_register', true );
127+
128+
// Use this to prevent repeated log calls.
129+
global $wp_actions;
130+
unset( $wp_actions['customize_save'] );
131+
132+
update_option( 'permalink_structure', '/%year%/%postname%/' );
133+
update_option( 'category_base', 'cat/' );
134+
update_option( 'tag_base', 'tag/' );
135+
136+
137+
// Check callback test action.
138+
$this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_updated_option' ) );
139+
$this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_update_option_tag_base' ) );
140+
$this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_update_option_category_base' ) );
141+
$this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_update_option_permalink_structure' ) );
142+
$this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_update_option' ) );
143+
}
47144
}

0 commit comments

Comments
 (0)