From 25b070c67f2922d88329fd0449e4d34e73845595 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Wed, 5 Aug 2020 14:53:41 -0400 Subject: [PATCH] jetpack connector test updated. --- composer.lock | 36 +-- connectors/class-connector-jetpack.php | 5 + local/config/wp-config.php | 3 +- .../test-class-connector-jetpack.php | 234 ++++++++++++------ 4 files changed, 188 insertions(+), 90 deletions(-) diff --git a/composer.lock b/composer.lock index 57a1788ff..83b72cfaf 100644 --- a/composer.lock +++ b/composer.lock @@ -239,16 +239,16 @@ }, { "name": "composer/composer", - "version": "1.10.9", + "version": "1.10.10", "source": { "type": "git", "url": "https://github.com/composer/composer.git", - "reference": "83c3250093d5491600a822e176b107a945baf95a" + "reference": "32966a3b1d48bc01472a8321fd6472b44fad033a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/composer/zipball/83c3250093d5491600a822e176b107a945baf95a", - "reference": "83c3250093d5491600a822e176b107a945baf95a", + "url": "https://api.github.com/repos/composer/composer/zipball/32966a3b1d48bc01472a8321fd6472b44fad033a", + "reference": "32966a3b1d48bc01472a8321fd6472b44fad033a", "shasum": "" }, "require": { @@ -329,7 +329,7 @@ "type": "tidelift" } ], - "time": "2020-07-16T10:57:00+00:00" + "time": "2020-08-03T09:35:19+00:00" }, { "name": "composer/semver", @@ -3231,7 +3231,7 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.18.0", + "version": "v1.18.1", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", @@ -3307,16 +3307,16 @@ }, { "name": "symfony/polyfill-intl-idn", - "version": "v1.18.0", + "version": "v1.18.1", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-idn.git", - "reference": "bc6549d068d0160e0f10f7a5a23c7d1406b95ebe" + "reference": "5dcab1bc7146cf8c1beaa4502a3d9be344334251" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-intl-idn/zipball/bc6549d068d0160e0f10f7a5a23c7d1406b95ebe", - "reference": "bc6549d068d0160e0f10f7a5a23c7d1406b95ebe", + "url": "https://api.github.com/repos/symfony/polyfill-intl-idn/zipball/5dcab1bc7146cf8c1beaa4502a3d9be344334251", + "reference": "5dcab1bc7146cf8c1beaa4502a3d9be344334251", "shasum": "" }, "require": { @@ -3388,11 +3388,11 @@ "type": "tidelift" } ], - "time": "2020-07-14T12:35:20+00:00" + "time": "2020-08-04T06:02:08+00:00" }, { "name": "symfony/polyfill-intl-normalizer", - "version": "v1.18.0", + "version": "v1.18.1", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-normalizer.git", @@ -3473,7 +3473,7 @@ }, { "name": "symfony/polyfill-mbstring", - "version": "v1.18.0", + "version": "v1.18.1", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", @@ -3550,7 +3550,7 @@ }, { "name": "symfony/polyfill-php70", - "version": "v1.18.0", + "version": "v1.18.1", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php70.git", @@ -3627,7 +3627,7 @@ }, { "name": "symfony/polyfill-php72", - "version": "v1.18.0", + "version": "v1.18.1", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php72.git", @@ -6003,15 +6003,15 @@ }, { "name": "wpackagist-plugin/jetpack", - "version": "8.7.1", + "version": "8.8", "source": { "type": "svn", "url": "https://plugins.svn.wordpress.org/jetpack/", - "reference": "tags/8.7.1" + "reference": "tags/8.8" }, "dist": { "type": "zip", - "url": "https://downloads.wordpress.org/plugin/jetpack.8.7.1.zip" + "url": "https://downloads.wordpress.org/plugin/jetpack.8.8.zip" }, "require": { "composer/installers": "~1.0" diff --git a/connectors/class-connector-jetpack.php b/connectors/class-connector-jetpack.php index 34f353748..a425ee16c 100644 --- a/connectors/class-connector-jetpack.php +++ b/connectors/class-connector-jetpack.php @@ -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', + ), ); } diff --git a/local/config/wp-config.php b/local/config/wp-config.php index f94bb8325..183160f79 100644 --- a/local/config/wp-config.php +++ b/local/config/wp-config.php @@ -23,7 +23,8 @@ $table_prefix = 'wptests_'; -define( 'WP_DEBUG', false ); +define( 'WP_DEBUG', true ); +define( 'JETPACK_DEV_DEBUG', true ); define( 'ABSPATH', __DIR__ . '/' ); diff --git a/tests/tests/connectors/test-class-connector-jetpack.php b/tests/tests/connectors/test-class-connector-jetpack.php index 61579a7cd..b7ca775c8 100644 --- a/tests/tests/connectors/test-class-connector-jetpack.php +++ b/tests/tests/connectors/test-class-connector-jetpack.php @@ -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' ) ) @@ -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 ), @@ -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' ) @@ -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' ) );