Skip to content

Commit

Permalink
Merge pull request #1165 from xwp/tests/settings-connector
Browse files Browse the repository at this point in the history
Settings connector test implemented.
  • Loading branch information
kidunot89 authored Jan 4, 2021
2 parents c4ebbac + ec330c8 commit d778c7c
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 120 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"license": "GPL-2.0-or-later",
"repositories": [
{
"type":"composer",
"url":"https://wpackagist.org"
"type": "composer",
"url": "https://wpackagist.org"
}
],
"require": {
Expand Down
166 changes: 81 additions & 85 deletions composer.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions connectors/class-connector-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Connector_Settings extends Connector {
* @var array
*/
public $actions = array(
'whitelist_options',
'allowed_options',
'update_option',
'update_site_option',
'update_option_permalink_structure',
Expand Down Expand Up @@ -618,12 +618,12 @@ function( $rule ) use ( $submenu, $record ) {
* @action update_option
*
* @param string $option Option name.
* @param mixed $value Option new value.
* @param mixed $old_value Option old value.
* @param mixed $value Option new value.
*/
public function callback_update_option( $option, $value, $old_value ) {
public function callback_update_option( $option, $old_value, $value ) {
if ( ( defined( '\WP_CLI' ) && \WP_CLI || did_action( 'customize_save' ) ) && array_key_exists( $option, $this->labels ) ) {
$this->callback_updated_option( $option, $value, $old_value );
$this->callback_updated_option( $option, $old_value, $value );
}
}

Expand All @@ -636,7 +636,7 @@ public function callback_update_option( $option, $value, $old_value ) {
*
* @return array
*/
public function callback_whitelist_options( $options ) {
public function callback_allowed_options( $options ) {
add_action( 'updated_option', array( $this, 'callback' ), 10, 3 );

return $options;
Expand Down Expand Up @@ -664,7 +664,7 @@ public function callback_update_option_permalink_structure( $old_value, $value )
* @param mixed $old_value Option old value.
*/
public function callback_update_site_option( $option, $value, $old_value ) {
$this->callback_updated_option( $option, $value, $old_value );
$this->callback_updated_option( $option, $old_value, $value );
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/tests/connectors/test-class-connector-acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
/**
* WP Integration Test w/ Advanced Custom Fields
*
* Tests for ACF Connector class callbacks.
* Tests for ACF connector class callbacks.
*
* @package WP_Stream
*/
namespace WP_Stream;

Expand Down
9 changes: 7 additions & 2 deletions tests/tests/connectors/test-class-connector-blogs.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* Tests for Blogs connector class callbacks.
*
* @package WP_Stream
*/
namespace WP_Stream;

class Test_WP_Stream_Connector_Blogs extends WP_StreamTestCase {
Expand All @@ -16,11 +21,11 @@ public function setUp() {
$this->connector_blogs = new Connector_Blogs;
$this->assertNotEmpty( $this->connector_blogs );
}

/**
* Test for get_context_labels().
*
* @group ms-required
* @group ms-required
*/
public function test_get_context_labels() {
if ( ! is_multisite() ) {
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/connectors/test-class-connector-edd.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* WP Integration Test w/ Easy Digital Downloads
*
* Tests for EDD Connector class callbacks.
*
* @package WP_Stream
*/

namespace WP_Stream;
Expand Down
7 changes: 6 additions & 1 deletion tests/tests/connectors/test-class-connector-mercator.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* Tests for Mercator connector class callbacks.
*
* @package WP_Stream
*/
namespace WP_Stream;

class Test_WP_Stream_Connector_Mercator extends WP_StreamTestCase {
Expand All @@ -20,7 +25,7 @@ public function setUp() {
/**
* Test for get_context_labels().
*
* @group ms-required
* @group ms-required
*/
public function test_get_context_labels() {
if ( ! is_multisite() ) {
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/connectors/test-class-connector-posts.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Tests for Post connector class callbacks.
* Tests for Posts connector class callbacks.
*
* @package WP_Stream
*/
Expand Down
150 changes: 128 additions & 22 deletions tests/tests/connectors/test-class-connector-settings.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,153 @@
<?php
namespace WP_Stream;
/**
* Class Test_Connector_Settings
* Tests for Settings connector class callbacks.
*
* @package WP_Stream
*/
class Test_Connector_Settings extends WP_StreamTestCase {

/**
* Holds the connector settings base class.
*
* @var Connector_Settings
* Runs before each test.
*/
protected $connector;

public function setUp() {
parent::setUp();
$this->connector = new Connector_Settings();

$this->assertNotEmpty( $this->connector );
$this->plugin->connectors->unload_connectors();

// Make partial of Connector_Settings class, with mocked "log" function.
$this->mock = $this->getMockBuilder( Connector_Settings::class )
->setMethods( array( 'log' ) )
->getMock();

// Register connector.
$this->mock->register();
}

public function test_is_option_ignored() {
$this->assertTrue( $this->connector->is_option_ignored( '_transient_option_name' ) );
$this->assertTrue( $this->connector->is_option_ignored( '_site_transient_option_name' ) );
$this->assertTrue( $this->connector->is_option_ignored( 'option_name$' ) );
$this->assertTrue( $this->connector->is_option_ignored( 'image_default_link_type' ) );
$this->assertTrue( $this->connector->is_option_ignored( 'medium_large_size_w' ) );
$this->assertTrue( $this->connector->is_option_ignored( 'medium_large_size_h' ) );

$this->assertFalse( $this->connector->is_option_ignored( 'option_site_transient_name' ) );
$this->assertFalse( $this->connector->is_option_ignored( 'option_transient_name' ) );
$this->assertFalse( $this->connector->is_option_ignored( 'option_$_name' ) );
$this->assertFalse( $this->connector->is_option_ignored( 'not_ignored' ) );
$this->assertTrue( $this->mock->is_option_ignored( '_transient_option_name' ) );
$this->assertTrue( $this->mock->is_option_ignored( '_site_transient_option_name' ) );
$this->assertTrue( $this->mock->is_option_ignored( 'option_name$' ) );
$this->assertTrue( $this->mock->is_option_ignored( 'image_default_link_type' ) );
$this->assertTrue( $this->mock->is_option_ignored( 'medium_large_size_w' ) );
$this->assertTrue( $this->mock->is_option_ignored( 'medium_large_size_h' ) );

$this->assertFalse( $this->mock->is_option_ignored( 'option_site_transient_name' ) );
$this->assertFalse( $this->mock->is_option_ignored( 'option_transient_name' ) );
$this->assertFalse( $this->mock->is_option_ignored( 'option_$_name' ) );
$this->assertFalse( $this->mock->is_option_ignored( 'not_ignored' ) );

// Test custom ignores.
$this->assertFalse( $this->connector->is_option_ignored( 'ignore_me' ) );
$this->assertFalse( $this->mock->is_option_ignored( 'ignore_me' ) );

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

$this->assertTrue( $this->connector->is_option_ignored( 'ignore_me' ) );
$this->assertTrue( $this->mock->is_option_ignored( 'ignore_me' ) );
}

public function test_callback_updated_option() {
// If multisite use site_option methods and test "update_site_option" callback
// instead of the update_option callback.
$add_method = is_multisite() ? 'add_site_option' : 'add_option';
$update_method = is_multisite() ? 'update_site_option' : 'update_option';

// Create options in database for later use.
call_user_func( $add_method, 'users_can_register', 0 );
call_user_func( $add_method, 'permalink_structure', '' );
call_user_func( $add_method, 'category_base', '' );
call_user_func( $add_method, 'tag_base', '' );

$this->mock->expects( $this->exactly( 4 ) )
->method( 'log' )
->withConsecutive(
array(
$this->equalTo( __( '"%s" setting was updated', 'stream' ) ),
$this->equalTo(
array(
'label' => 'Membership',
'option' => 'users_can_register',
'context' => 'settings',
'old_value' => '0',
'value' => '1',
)
),
$this->equalTo( null ),
$this->equalTo( 'settings' ),
$this->equalTo( 'updated' )
),
array(
$this->equalTo( __( '"%s" setting was updated', 'stream' ) ),
$this->equalTo(
array(
'label' => 'Permalink Settings',
'option' => 'permalink_structure',
'context' => 'permalink',
'old_value' => '',
'value' => '/%year%/%postname%/',
)
),
$this->equalTo( null ),
$this->equalTo( 'permalink' ),
$this->equalTo( 'updated' )
),
array(
$this->equalTo( __( '"%s" setting was updated', 'stream' ) ),
$this->equalTo(
array(
'label' => 'Category base',
'option' => 'category_base',
'context' => 'permalink',
'old_value' => '',
'value' => 'cat/',
)
),
$this->equalTo( null ),
$this->equalTo( 'permalink' ),
$this->equalTo( 'updated' )
),
array(
$this->equalTo( __( '"%s" setting was updated', 'stream' ) ),
$this->equalTo(
array(
'label' => 'Tag base',
'option' => 'tag_base',
'context' => 'permalink',
'old_value' => '',
'value' => 'tag/',
)
),
$this->equalTo( null ),
$this->equalTo( 'permalink' ),
$this->equalTo( 'updated' )
)
);

// Simulate being on the WP Customizr page.
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
do_action( 'customize_save', new \WP_Customize_Manager( array() ) );

// Update options to trigger callback.
call_user_func( $update_method, 'users_can_register', 1 );

// Use this to prevent repeated log calls.
global $wp_actions;
unset( $wp_actions['customize_save'] );

call_user_func( $update_method, 'permalink_structure', '/%year%/%postname%/' );
call_user_func( $update_method, 'category_base', 'cat/' );
call_user_func( $update_method, 'tag_base', 'tag/' );

// If multisite only check update_site_option test callback.
if ( is_multisite() ) {
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_site_option' ) );
} else {
// Check callback test action.
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_option_tag_base' ) );
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_option_category_base' ) );
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_option_permalink_structure' ) );
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_updated_option' ) );
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_option' ) );
}
}
}

0 comments on commit d778c7c

Please sign in to comment.