Skip to content

Commit

Permalink
jetpack connector test updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Aug 21, 2020
1 parent 4b6091c commit c952c6f
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 72 deletions.
5 changes: 5 additions & 0 deletions connectors/class-connector-jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ public function register() {
'label' => esc_html__( 'Tiled Galleries', 'stream' ),
'context' => 'tiled-gallery',
),
// Monitor.
'monitor_receive_notification' => array(
'label' => esc_html__( 'Monitor notifications', 'stream' ),
'context' => 'monitor',
),
);
}

Expand Down
3 changes: 2 additions & 1 deletion local/config/wp-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

$table_prefix = 'wptests_';

define( 'WP_DEBUG', false );
define( 'WP_DEBUG', true );
define( 'JETPACK_DEV_DEBUG', true );

define( 'ABSPATH', __DIR__ . '/' );

Expand Down
234 changes: 163 additions & 71 deletions tests/tests/connectors/test-class-connector-jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@

class Test_WP_Stream_Connector_Jetpack extends WP_StreamTestCase {

/**
* @var WP_REST_Server $wp_rest_server
*/
protected $server;

protected $namespaced_route = '/jetpack/v4';

/**
* Runs before each test
*/
public function setUp() {
parent::setUp();

global $wp_rest_server;
$this->server = $wp_rest_server = new \WP_REST_Server;
do_action( 'rest_api_init' );

// Make partial of Connector_Installer class, with mocked "log" function.
$this->mock = $this->getMockBuilder( Connector_Jetpack::class )
->setMethods( array( 'log' ) )
Expand Down Expand Up @@ -43,76 +54,24 @@ public function test_callback_jetpack_log_entry() {
}

public function test_callback_sharing_get_services_state() {
// Prepare scenario.
// Create sharing service instance and services object for later use.
require_once JETPACK__PLUGIN_DIR. 'modules/sharedaddy/sharing-service.php';
$sharer = new \Sharing_Service();
$services = $sharer->get_all_services();

// Expected log calls.
$this->mock->expects( $this->exactly( 2 ) )
$this->mock->expects( $this->exactly( 1 ) )
->method( 'log' )
->withConsecutive(
array(
$this->equalTo( __( '"%s" setting updated', 'stream' ) ),
$this->equalTo(
array(
'option_title' => 'Sharing options',
'option' => 'sharing-options',
'old_value' => null,
'new_value' => array(
'global' => array(
'button_style' => 'icon-text',
'sharing_label' => 'Share this:',
'open_links' => 'same',
'show' => array( 'post', 'page' ),
'custom' => array(),
),
)
)
),
$this->equalTo( null ),
$this->equalTo( 'sharedaddy' ),
$this->equalTo( 'updated' )
),
array(
$this->equalTo( __( 'Sharing services updated', 'stream' ) ),
$this->equalTo(
array(
'services' => array(
'print' => 'Share_Print',
'facebook' => 'Share_Facebook',
'linkedin' => 'Share_LinkedIn',
'reddit' => 'Share_Reddit',
'twitter' => 'Share_Twitter',
'tumblr' => 'Share_Tumblr',
'pinterest' => 'Share_Pinterest',
'pocket' => 'Share_Pocket',
'telegram' => 'Share_Telegram',
'jetpack-whatsapp' => 'Jetpack_Share_WhatsApp',
'skype' => 'Share_Skype',
),
'available' => array(
'print',
'facebook',
'linkedin',
'reddit',
'twitter',
'tumblr',
'pinterest',
'pocket',
'telegram',
'jetpack-whatsapp',
'skype',
),
'services' => $services,
'available' => array_keys( $services ),
'hidden' => array(),
'visible' => array(),
'currently_enabled' => array(
'visible' => array(
'twitter' => new \Share_Twitter(),
'facebook' => new \Share_Facebook(),
),
'hidden' => array(),
'all' => array( 'twitter', 'facebook' ),
),
'currently_enabled' => $sharer->get_blog_services(),
)
),
$this->equalTo( null ),
Expand All @@ -121,7 +80,7 @@ public function test_callback_sharing_get_services_state() {
)
);

// Do stuff.
// Update sharing services to trigger callback.
$sharer->set_blog_services(
array( 'facebook' => 'Share_Facebook' ),
array( 'reddit' => 'Share_Reddit' )
Expand All @@ -138,43 +97,176 @@ public function test_callback_jetpack_module_configuration_load_monitor() {
$this->mock->expects( $this->once() )
->method( 'log' )
->with(

$this->equalTo( __( 'Monitor notifications %s', 'stream' ) ),
$this->equalTo(
array(
'status' => esc_html__( 'activated', 'stream' ),
'option' => 'receive_jetpack_monitor_notification',
'old_value' => false,
'value' => true,
)
),
$this->equalTo( null ),
$this->equalTo( 'monitor' ),
$this->equalTo( 'updated' )
);

// Do stuff.
// Simulate "receive_jetpack_monitor_notification" option change to trigger callback.
$_POST['receive_jetpack_monitor_notification'] = true;
do_action( 'jetpack_module_configuration_load_monitor' );

// Check callback test action.
$this->assertFalse( 0 === did_action( 'wp_stream_test_callback_jetpack_module_configuration_load_monitor' ) );
}

public function test_check_function() {
public function test_check() {
// Prepare scenario
$admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
wp_set_current_user( $admin_id );

// Expected log calls.
$this->mock->expects( $this->once() )
$this->mock->expects( $this->exactly( 2 ) )
->method( 'log' )
->with(

->withConsecutive(
array(
$this->equalTo( __( '"%s" setting updated', 'stream' ) ),
$this->equalTo(
array(
'option_title' => esc_html__( 'Sharing options', 'stream' ),
'option' => 'sharing-options',
'old_value' => null,
'new_value' => array(
'global' => array(
'button_style' => 'icon-text',
'sharing_label' => 'Share this:',
'open_links' => 'same',
'show' => array( 'post', 'page' ),
'custom' => array(),
),
),
)
),
$this->equalTo( null ),
$this->equalTo( 'sharedaddy' ),
$this->equalTo( 'updated' ),
),
array(
$this->equalTo( __( '"%s" setting updated', 'stream' ) ),
$this->equalTo(
array(
'option_title' => esc_html__( 'Sharing options', 'stream' ),
'option' => 'sharing-options',
'old_value' => array(
'global' => array(
'button_style' => 'icon-text',
'sharing_label' => 'Share this:',
'open_links' => 'same',
'show' => array( 'post', 'page' ),
'custom' => array(),
),
),
'new_value' => array(
'global' => array(
'button_style' => 'icon-text',
'sharing_label' => 'Share what',
'open_links' => 'same',
'show' => array( 'post', 'page' ),
'custom' => array(),
),
),
)
),
$this->equalTo( null ),
$this->equalTo( 'sharedaddy' ),
$this->equalTo( 'updated' ),
)
);

// Do stuff.
// Test Jetpack REST route.
$routes = $this->server->get_routes();
$this->assertArrayHasKey( $this->namespaced_route, $routes );

// Execute REST requests and trigger callbacks.
$request = new \WP_Rest_Request( 'POST', "{$this->namespaced_route}/settings" );
$request->set_body_params(
array(
'carousel' => true,
'carousel_background_color' => 'white',
'sharedaddy' => true,
'sharing_label' => 'Share what'
)
);
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );

// Check callback test action.
$this->assertFalse( 0 === did_action( 'wp_stream_test_callback_add_option' ) );
$this->assertFalse( 0 === did_action( 'wp_stream_test_callback_update_option' ) );
}

public function test_track_post_by_email_function() {
public function test_track_post_by_email() {
// Prepare scenario
$admin_id = self::factory()->user->create(
array(
'role' => 'administrator',
'display_name' => 'TestGuy',
)
);
wp_set_current_user( $admin_id );

// Expected log calls.
$this->mock->expects( $this->once() )
$this->mock->expects( $this->exactly( 3 ) )
->method( 'log' )
->with(

->withConsecutive(
array(
$this->equalTo( __( '%1$s %2$s Post by Email', 'stream' ) ),
$this->equalTo(
array(
'user_displayname' => 'TestGuy',
'action' => esc_html__( 'enabled', 'stream' ),
'status' => true,
)
),
$this->equalTo( null ),
$this->equalTo( 'post-by-email' ),
$this->equalTo( 'updated' ),
),
array(
$this->equalTo( __( '%1$s %2$s Post by Email', 'stream' ) ),
$this->equalTo(
array(
'user_displayname' => 'TestGuy',
'action' => esc_html__( 'disabled', 'stream' ),
'status' => false,
)
),
$this->equalTo( null ),
$this->equalTo( 'post-by-email' ),
$this->equalTo( 'updated' ),
),
array(
$this->equalTo( __( '%1$s %2$s Post by Email', 'stream' ) ),
$this->equalTo(
array(
'user_displayname' => 'TestGuy',
'action' => esc_html__( 'regenerated', 'stream' ),
'status' => null,
)
),
$this->equalTo( null ),
$this->equalTo( 'post-by-email' ),
$this->equalTo( 'updated' ),
)
);

// Do stuff.
// Test Jetpack REST route.
$routes = $this->server->get_routes();
$this->assertArrayHasKey( $this->namespaced_route, $routes );

// Manually trigger callbacks.
do_action( 'wp_ajax_jetpack_post_by_email_enable' );
do_action( 'wp_ajax_jetpack_post_by_email_disable' );
do_action( 'wp_ajax_jetpack_post_by_email_regenerate' );

// Check callback test action.
$this->assertFalse( 0 === did_action( 'wp_stream_test_callback_wp_ajax_jetpack_post_by_email_enable' ) );
Expand Down

0 comments on commit c952c6f

Please sign in to comment.