From 939596ad418526d0acfef51701294548932c6100 Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Wed, 8 Nov 2023 18:36:56 +0100 Subject: [PATCH] Add tests --- .../publicize/src/class-publicize-base.php | 2 +- .../test-auto-conversion.php | 37 ++-- ...etpack-social-settings-rest-controller.php | 207 ++++++++++++++++++ .../test-jetpack-social-settings.php | 144 ++++++++++++ .../test-social-image-generator-settings.php} | 32 ++- .../php/test-auto-conversion-controller.php | 192 ---------------- .../test-rest-settings-controller.php | 173 --------------- .../test-setup.php | 2 +- 8 files changed, 388 insertions(+), 401 deletions(-) rename projects/packages/publicize/tests/php/{ => jetpack-social-settings}/test-auto-conversion.php (56%) create mode 100644 projects/packages/publicize/tests/php/jetpack-social-settings/test-jetpack-social-settings-rest-controller.php create mode 100644 projects/packages/publicize/tests/php/jetpack-social-settings/test-jetpack-social-settings.php rename projects/packages/publicize/tests/php/{test-social-image-generator/test-settings.php => jetpack-social-settings/test-social-image-generator-settings.php} (65%) delete mode 100644 projects/packages/publicize/tests/php/test-auto-conversion-controller.php delete mode 100644 projects/packages/publicize/tests/php/test-social-image-generator/test-rest-settings-controller.php diff --git a/projects/packages/publicize/src/class-publicize-base.php b/projects/packages/publicize/src/class-publicize-base.php index d346ff80ee608..71057588154e8 100644 --- a/projects/packages/publicize/src/class-publicize-base.php +++ b/projects/packages/publicize/src/class-publicize-base.php @@ -1133,7 +1133,7 @@ public function register_post_meta() { 'single' => true, 'default' => array( 'image_generator_settings' => array( - 'template' => ( new Social_Image_Generator\Settings() )->get_default_template(), + 'template' => ( new Jetpack_Social_Settings\Settings() )->sig_get_default_template(), 'enabled' => false, ), ), diff --git a/projects/packages/publicize/tests/php/test-auto-conversion.php b/projects/packages/publicize/tests/php/jetpack-social-settings/test-auto-conversion.php similarity index 56% rename from projects/packages/publicize/tests/php/test-auto-conversion.php rename to projects/packages/publicize/tests/php/jetpack-social-settings/test-auto-conversion.php index 49ee34b619a6e..2e15888734b83 100644 --- a/projects/packages/publicize/tests/php/test-auto-conversion.php +++ b/projects/packages/publicize/tests/php/jetpack-social-settings/test-auto-conversion.php @@ -7,7 +7,7 @@ namespace Automattic\Jetpack\Publicize; -use Automattic\Jetpack\Publicize\Auto_Conversion\Settings as Auto_Conversion_Settings; +use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings as SocialSettings; use WorDBless\BaseTestCase; use WorDBless\Options as WorDBless_Options; use WorDBless\Posts as WorDBless_Posts; @@ -36,7 +36,7 @@ public function set_up() { $publicize->method( 'has_social_auto_conversion_feature' ) ->withAnyParameters() ->willReturn( true ); - $this->settings = new Auto_Conversion_Settings(); + $this->settings = new SocialSettings(); } /** @@ -66,41 +66,32 @@ public function mock_publicize_being_active() { } /** - * Test that SIG is available based on the plan check. + * Test that Auto-Conversion is available based on the plan check. */ public function test_correctly_returns_available_status() { - $this->assertTrue( $this->settings->is_available( 'image' ) ); - $this->assertTrue( $this->settings->is_available( 'video' ) ); + $this->assertTrue( $this->settings->is_auto_conversion_available() ); } /** * Test that it correctly returns enabled or disabled. */ public function test_correctly_returns_enabled_status() { - $this->assertTrue( $this->settings->is_enabled( 'image' ) ); - $this->assertFalse( $this->settings->is_enabled( 'video' ) ); + $auto_conversion_settings = $this->settings->get_settings()['autoConversionSettings']; + $this->assertTrue( $auto_conversion_settings['image'] ); + $this->assertFalse( isset( $auto_conversion_settings['video'] ) ? $auto_conversion_settings['video'] : false ); } /** * Test that it correctly returns enabled or disabled. */ public function test_correctly_updates_enabled_status() { - $this->settings->set_enabled( 'image', true ); - $this->settings->set_enabled( 'auto-conversion', true ); - $this->assertTrue( $this->settings->is_enabled( 'auto-conversion' ) ); - $this->assertTrue( $this->settings->is_enabled( 'image' ) ); - $this->assertFalse( $this->settings->is_enabled( 'video' ) ); - $this->settings->set_enabled( 'video', true ); - $this->assertTrue( $this->settings->is_enabled( 'image' ) ); - $this->assertTrue( $this->settings->is_enabled( 'video' ) ); + $this->settings->update_auto_conversion_settings( array( 'image' => false ) ); + $auto_conversion_settings = $this->settings->get_settings()['autoConversionSettings']; + $this->assertFalse( $auto_conversion_settings['image'] ); - $this->settings->set_enabled( 'image', false ); - $this->assertFalse( $this->settings->is_enabled( 'image' ) ); - $this->assertTrue( $this->settings->is_enabled( 'video' ) ); - $this->settings->set_enabled( 'video', false ); - $this->assertFalse( $this->settings->is_enabled( 'video' ) ); - $this->assertTrue( $this->settings->is_enabled( 'auto-conversion' ) ); - $this->settings->set_enabled( 'auto-conversion', false ); - $this->assertFalse( $this->settings->is_enabled( 'auto-conversion' ) ); + $this->settings->update_auto_conversion_settings( array( 'video' => true ) ); + $auto_conversion_settings = $this->settings->get_settings()['autoConversionSettings']; + $this->assertFalse( $auto_conversion_settings['image'] ); + $this->assertTrue( $auto_conversion_settings['video'] ); } } diff --git a/projects/packages/publicize/tests/php/jetpack-social-settings/test-jetpack-social-settings-rest-controller.php b/projects/packages/publicize/tests/php/jetpack-social-settings/test-jetpack-social-settings-rest-controller.php new file mode 100644 index 0000000000000..61d36a323b958 --- /dev/null +++ b/projects/packages/publicize/tests/php/jetpack-social-settings/test-jetpack-social-settings-rest-controller.php @@ -0,0 +1,207 @@ +clear_options(); + WorDBless_Posts::init()->clear_all_posts(); + WorDBless_Users::init()->clear_all_users(); + + add_filter( 'jetpack_active_modules', array( $this, 'mock_publicize_being_active' ) ); + global $publicize; + $publicize = $this->getMockBuilder( Publicize::class )->setMethods( array( 'has_social_auto_conversion_feature', 'has_social_image_generator_feature' ) )->getMock(); + $publicize->method( 'has_social_auto_conversion_feature' ) + ->withAnyParameters() + ->willReturn( true ); + $publicize->method( 'has_social_image_generator_feature' ) + ->withAnyParameters() + ->willReturn( true ); + $publicize->register_post_meta(); + + global $wp_rest_server; + + $wp_rest_server = new WP_REST_Server(); + $this->server = $wp_rest_server; + $this->admin_id = wp_insert_user( + array( + 'user_login' => 'dummy_user', + 'user_pass' => 'dummy_password', + 'role' => 'administrator', + ) + ); + + wp_set_current_user( 0 ); + + // Register REST routes. + add_action( 'rest_api_init', array( new Jetpack_Social_Settings\REST_Settings_Controller(), 'register_routes' ) ); + + do_action( 'rest_api_init' ); + } + + /** + * Returning the environment into its initial state. + * + * @after + */ + public function tear_down() { + wp_set_current_user( 0 ); + + remove_filter( 'jetpack_active_modules', array( $this, 'mock_publicize_being_active' ) ); + + WorDBless_Options::init()->clear_options(); + WorDBless_Posts::init()->clear_all_posts(); + WorDBless_Users::init()->clear_all_users(); + } + + /** + * Mock Publicize being active. + * + * @return array + */ + public function mock_publicize_being_active() { + return array( 'publicize' ); + } + + /** + * Testing the `GET /jetpack/v4/jetpack-social/settings` endpoint without proper permissions. + */ + public function test_get_settings_without_proper_permission() { + $request = new WP_REST_Request( 'GET', '/jetpack/v4/jetpack-social/settings' ); + $response = $this->server->dispatch( $request ); + $this->assertEquals( 401, $response->get_status() ); + $this->assertEquals( 'rest_forbidden_context', $response->get_data()['code'] ); + } + + /** + * Testing the `GET /jetpack/v4/jetpack-social/settings` endpoint with proper permissions. + */ + public function test_get_settings_with_proper_permission() { + wp_set_current_user( $this->admin_id ); + $request = new WP_REST_Request( 'GET', '/jetpack/v4/jetpack-social/settings' ); + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + + $this->assertArrayHasKey( 'autoConversionSettings', $response->data ); + $this->assertArrayHasKey( 'socialImageGeneratorSettings', $response->data ); + $this->assertArrayHasKey( 'defaults', $response->data['socialImageGeneratorSettings'] ); + + $this->assertTrue( $response->data['autoConversionSettings']['image'] ); + $this->assertFalse( $response->data['socialImageGeneratorSettings']['enabled'] ); + } + + /** + * Private function just to test the endpoint response against values. + * + * @param WP_REST_Response $response The response object. + * @param bool|null $image The expected value for the image setting. + * @param bool|null $video The expected value for the video setting. + * @param bool|null $sig_enabled The expected value for the SIG enabled setting. + * @param string|null $sig_template The expected value for the SIG template setting. + */ + private function test_response( $response, $image, $video, $sig_enabled, $sig_template ) { + $this->assertEquals( 200, $response->get_status() ); + + $response_data = $response->data; + if ( $image !== null ) { + $this->assertEquals( $image, $response_data['autoConversionSettings']['image'] ); + } + if ( $video !== null ) { + $this->assertEquals( $video, $response_data['autoConversionSettings']['video'] ); + } + if ( $sig_enabled !== null ) { + $this->assertEquals( $sig_enabled, $response_data['socialImageGeneratorSettings']['enabled'] ); + } + if ( $sig_template !== null ) { + $this->assertEquals( $sig_template, $response_data['socialImageGeneratorSettings']['defaults']['template'] ); + } + } + + /** + * Testing the `GET /jetpack/v4/jetpack-social/settings` endpoint with proper permissions. + */ + public function test_update_settings() { + wp_set_current_user( $this->admin_id ); + $request = new WP_REST_Request( 'POST', '/jetpack/v4/jetpack-social/settings' ); + $request->set_body_params( + array( + 'autoConversionSettings' => array( + 'image' => false, + ), + ) + ); + $response = $this->server->dispatch( $request ); + $this->test_response( $response, false, null, false, TEMPLATES::DEFAULT_TEMPLATE ); + + $request = new WP_REST_Request( 'POST', '/jetpack/v4/jetpack-social/settings' ); + $request->set_body_params( + array( + 'autoConversionSettings' => array( + 'image' => true, + 'video' => true, + ), + ) + ); + $response = $this->server->dispatch( $request ); + $this->test_response( $response, true, true, false, TEMPLATES::DEFAULT_TEMPLATE ); + + $request = new WP_REST_Request( 'POST', '/jetpack/v4/jetpack-social/settings' ); + $request->set_body_params( + array( + 'socialImageGeneratorSettings' => array( + 'enabled' => true, + ), + ) + ); + $response = $this->server->dispatch( $request ); + $this->test_response( $response, true, true, true, TEMPLATES::DEFAULT_TEMPLATE ); + + $request = new WP_REST_Request( 'POST', '/jetpack/v4/jetpack-social/settings' ); + $request->set_body_params( + array( + 'socialImageGeneratorSettings' => array( + 'defaults' => array( + 'template' => 'fullscreen', + ), + ), + ) + ); + $response = $this->server->dispatch( $request ); + $this->test_response( $response, true, true, true, 'fullscreen' ); + } +} diff --git a/projects/packages/publicize/tests/php/jetpack-social-settings/test-jetpack-social-settings.php b/projects/packages/publicize/tests/php/jetpack-social-settings/test-jetpack-social-settings.php new file mode 100644 index 0000000000000..2d7959c065fb1 --- /dev/null +++ b/projects/packages/publicize/tests/php/jetpack-social-settings/test-jetpack-social-settings.php @@ -0,0 +1,144 @@ +getMockBuilder( Publicize::class )->setMethods( array( 'has_social_auto_conversion_feature', 'has_social_image_generator_feature' ) )->getMock(); + $publicize->method( 'has_social_auto_conversion_feature' ) + ->withAnyParameters() + ->willReturn( true ); + $publicize->method( 'has_social_image_generator_feature' ) + ->withAnyParameters() + ->willReturn( true ); + $this->settings = new SocialSettings(); + } + + /** + * Tear down + * + * @after + */ + public function tear_down() { + wp_set_current_user( 0 ); + + global $publicize; + $publicize = new Publicize(); + + remove_filter( 'jetpack_active_modules', array( $this, 'mock_publicize_being_active' ) ); + WorDBless_Options::init()->clear_options(); + WorDBless_Posts::init()->clear_all_posts(); + WorDBless_Users::init()->clear_all_users(); + } + + /** + * Mock Publicize being active. + * + * @return array + */ + public function mock_publicize_being_active() { + return array( 'publicize' ); + } + + /** + * Tests that the settings are returned correctly with the availability parameter. + */ + public function test_get_settings_with_availability() { + $settings = $this->settings->get_settings( true ); + + $this->assertArrayHasKey( 'autoConversionSettings', $settings ); + $this->assertArrayHasKey( 'socialImageGeneratorSettings', $settings ); + $this->assertArrayHasKey( 'available', $settings['autoConversionSettings'] ); + $this->assertArrayHasKey( 'available', $settings['socialImageGeneratorSettings'] ); + + $this->assertTrue( $settings['autoConversionSettings']['available'] ); + $this->assertTrue( $settings['socialImageGeneratorSettings']['available'] ); + } + + /** + * Tests that the settings are returned correctly on new sites without the option. + */ + public function test_settings_on_new_site() { + $settings = $this->settings->get_settings(); + + $this->assertArrayHasKey( 'autoConversionSettings', $settings ); + $this->assertArrayHasKey( 'socialImageGeneratorSettings', $settings ); + $this->assertArrayHasKey( 'defaults', $settings['socialImageGeneratorSettings'] ); + + $this->assertTrue( $settings['autoConversionSettings']['image'] ); + $this->assertFalse( $settings['socialImageGeneratorSettings']['enabled'] ); + $this->assertEquals( Templates::DEFAULT_TEMPLATE, $settings['socialImageGeneratorSettings']['defaults']['template'] ); + } + + /** + * Tests that the sites can be migrated from the old set of options + */ + public function test_migrate_old_options() { + update_option( 'jetpack_social_settings', array( 'image' => true ) ); + update_option( + 'jetpack_social_image_generator_settings', + array( + 'enabled' => true, + 'defaults' => array( 'template' => 'example_template' ), + ) + ); + + $expected_options = array( + 'autoConversionSettings' => array( 'image' => true ), + 'socialImageGeneratorSettings' => array( + 'enabled' => true, + 'defaults' => array( 'template' => 'example_template' ), + ), + ); + + $this->settings = new SocialSettings(); + $this->assertEquals( $expected_options, $this->settings->get_settings() ); + } + + /** + * Tests that the sites can be migrated from the old set of options with missing template option + */ + public function test_migrate_old_options_with_missing() { + update_option( 'jetpack_social_settings', array( 'image' => true ) ); + + $expected_options = array( + 'autoConversionSettings' => array( 'image' => true ), + 'socialImageGeneratorSettings' => array( + 'enabled' => false, + 'defaults' => array( 'template' => Templates::DEFAULT_TEMPLATE ), + ), + ); + + $this->settings = new SocialSettings(); + $this->assertEquals( $expected_options, $this->settings->get_settings() ); + } +} diff --git a/projects/packages/publicize/tests/php/test-social-image-generator/test-settings.php b/projects/packages/publicize/tests/php/jetpack-social-settings/test-social-image-generator-settings.php similarity index 65% rename from projects/packages/publicize/tests/php/test-social-image-generator/test-settings.php rename to projects/packages/publicize/tests/php/jetpack-social-settings/test-social-image-generator-settings.php index 0f0606de30019..08a3cd511f461 100644 --- a/projects/packages/publicize/tests/php/test-social-image-generator/test-settings.php +++ b/projects/packages/publicize/tests/php/jetpack-social-settings/test-social-image-generator-settings.php @@ -8,7 +8,7 @@ namespace Automattic\Jetpack\Publicize; use Automattic\Jetpack\Current_Plan; -use Automattic\Jetpack\Publicize\Social_Image_Generator\Settings; +use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings as SocialSettings; use Automattic\Jetpack\Publicize\Social_Image_Generator\Templates; use WorDBless\BaseTestCase; use WorDBless\Options as WorDBless_Options; @@ -18,7 +18,7 @@ /** * Testing the Settings class. */ -class Settings_Test extends BaseTestCase { +class Social_Image_Generator_Settings_Test extends BaseTestCase { /** * Instance of the Settings class. * @@ -41,7 +41,7 @@ public function set_up() { $plan = Current_Plan::PLAN_DATA['free']; $plan['features']['active'] = array( 'social-image-generator' ); update_option( Current_Plan::PLAN_OPTION, $plan, true ); - $this->settings = new Settings(); + $this->settings = new SocialSettings(); } /** @@ -72,36 +72,44 @@ public function mock_publicize_being_active() { * Test that SIG is available based on the plan check. */ public function test_correctly_returns_available_status() { - $this->assertTrue( $this->settings->is_available() ); + $this->assertTrue( $this->settings->is_sig_available() ); } /** * Test that it correctly returns enabled or disabled. */ public function test_correctly_returns_enabled_status() { - $this->assertFalse( $this->settings->is_enabled() ); + $sig_settings = $this->settings->get_settings()['socialImageGeneratorSettings']; + $this->assertFalse( $sig_settings['enabled'] ); } /** * Test that it correctly updates the enabled status. */ public function test_correctly_updates_enabled_status() { - $this->settings->set_enabled( true ); - $this->assertTrue( $this->settings->is_enabled() ); + $sig_settings = $this->settings->get_settings()['socialImageGeneratorSettings']; + $this->assertFalse( $sig_settings['enabled'] ); + + $this->settings->update_social_image_generator_settings( array( 'enabled' => true ) ); + + $sig_settings = $this->settings->get_settings()['socialImageGeneratorSettings']; + $this->assertTrue( $sig_settings['enabled'] ); } /** * Test that it returns the default template if a template is not set. */ public function test_returns_default_template_if_not_set() { - $this->assertEquals( Templates::DEFAULT_TEMPLATE, $this->settings->get_default_template() ); + $sig_settings = $this->settings->get_settings()['socialImageGeneratorSettings']; + $this->assertEquals( Templates::DEFAULT_TEMPLATE, $sig_settings['defaults']['template'] ); } /** * Test that it returns all the correct defaults. */ public function test_defaults_have_all_required_keys() { - $defaults = $this->settings->get_defaults(); + $sig_settings = $this->settings->get_settings()['socialImageGeneratorSettings']; + $defaults = $sig_settings['defaults']; $this->assertArrayHasKey( 'template', $defaults ); $this->assertCount( 1, $defaults ); } @@ -110,7 +118,9 @@ public function test_defaults_have_all_required_keys() { * Test that it returns correct template if set. */ public function test_returns_correct_template_if_set() { - $this->settings->set_default_template( 'example_template' ); - $this->assertEquals( 'example_template', $this->settings->get_default_template() ); + $this->settings->update_social_image_generator_settings( array( 'defaults' => array( 'template' => 'example_template' ) ) ); + $sig_settings = $this->settings->get_settings()['socialImageGeneratorSettings']; + + $this->assertEquals( 'example_template', $sig_settings['defaults']['template'] ); } } diff --git a/projects/packages/publicize/tests/php/test-auto-conversion-controller.php b/projects/packages/publicize/tests/php/test-auto-conversion-controller.php deleted file mode 100644 index 633141c9b456a..0000000000000 --- a/projects/packages/publicize/tests/php/test-auto-conversion-controller.php +++ /dev/null @@ -1,192 +0,0 @@ -clear_options(); - WorDBless_Posts::init()->clear_all_posts(); - WorDBless_Users::init()->clear_all_users(); - - add_filter( 'jetpack_active_modules', array( $this, 'mock_publicize_being_active' ) ); - global $publicize; - $publicize = $this->getMockBuilder( Publicize::class )->setMethods( array( 'has_social_auto_conversion_feature' ) )->getMock(); - $publicize->method( 'has_social_auto_conversion_feature' ) - ->withAnyParameters() - ->willReturn( true ); - $publicize->register_post_meta(); - - global $wp_rest_server; - - $wp_rest_server = new WP_REST_Server(); - $this->server = $wp_rest_server; - $this->admin_id = wp_insert_user( - array( - 'user_login' => 'dummy_user', - 'user_pass' => 'dummy_password', - 'role' => 'administrator', - ) - ); - - wp_set_current_user( 0 ); - - // Register REST routes. - add_action( 'rest_api_init', array( new Auto_Conversion\REST_Settings_Controller(), 'register_routes' ) ); - - do_action( 'rest_api_init' ); - } - - /** - * Returning the environment into its initial state. - * - * @after - */ - public function tear_down() { - wp_set_current_user( 0 ); - - remove_filter( 'jetpack_active_modules', array( $this, 'mock_publicize_being_active' ) ); - - WorDBless_Options::init()->clear_options(); - WorDBless_Posts::init()->clear_all_posts(); - WorDBless_Users::init()->clear_all_users(); - } - - /** - * Mock Publicize being active. - * - * @return array - */ - public function mock_publicize_being_active() { - return array( 'publicize' ); - } - - /** - * Testing the `GET /jetpack/v4/auto-conversion/settings` endpoint without proper permissions. - */ - public function test_get_settings_without_proper_permission() { - $request = new WP_REST_Request( 'GET', '/jetpack/v4/auto-conversion/settings' ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - $this->assertEquals( 'rest_forbidden_context', $response->get_data()['code'] ); - } - - /** - * Testing the `GET /jetpack/v4/auto-conversion/settings` endpoint with proper permissions. - */ - public function test_get_settings_with_proper_permission() { - wp_set_current_user( $this->admin_id ); - $request = new WP_REST_Request( 'GET', '/jetpack/v4/auto-conversion/settings' ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - $this->assertArrayHasKey( 'image', $response->data ); - $this->assertArrayHasKey( 'video', $response->data ); - $this->assertArrayHasKey( 'auto-conversion', $response->data ); - } - - /** - * Testing the `GET /jetpack/v4/auto-conversion/settings` endpoint with proper permissions. - */ - public function test_update_settings() { - wp_set_current_user( $this->admin_id ); - $request = new WP_REST_Request( 'POST', '/jetpack/v4/auto-conversion/settings' ); - $request->set_body_params( - array( - 'image' => true, - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - $this->assertTrue( $response->data['image'] ); - $this->assertFalse( $response->data['video'] ); - $this->assertFalse( $response->data['auto-conversion'] ); - - $request = new WP_REST_Request( 'POST', '/jetpack/v4/auto-conversion/settings' ); - $request->set_body_params( - array( - 'image' => true, - 'video' => true, - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - $this->assertTrue( $response->data['image'] ); - $this->assertTrue( $response->data['video'] ); - $this->assertFalse( $response->data['auto-conversion'] ); - - $request = new WP_REST_Request( 'POST', '/jetpack/v4/auto-conversion/settings' ); - $request->set_body_params( - array( - 'image' => true, - 'video' => true, - 'auto-conversion' => true, - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - $this->assertTrue( $response->data['image'] ); - $this->assertTrue( $response->data['auto-conversion'] ); - $this->assertTrue( $response->data['video'] ); - - $request = new WP_REST_Request( 'POST', '/jetpack/v4/auto-conversion/settings' ); - $request->set_body_params( - array( - 'image' => false, - 'video' => true, - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - $this->assertFalse( $response->data['image'] ); - $this->assertTrue( $response->data['video'] ); - $this->assertTrue( $response->data['auto-conversion'] ); - - $request = new WP_REST_Request( 'POST', '/jetpack/v4/auto-conversion/settings' ); - $request->set_body_params( - array( - 'image' => false, - 'video' => false, - 'auto-conversion' => false, - - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - $this->assertFalse( $response->data['image'] ); - $this->assertFalse( $response->data['video'] ); - $this->assertFalse( $response->data['auto-conversion'] ); - } -} diff --git a/projects/packages/publicize/tests/php/test-social-image-generator/test-rest-settings-controller.php b/projects/packages/publicize/tests/php/test-social-image-generator/test-rest-settings-controller.php deleted file mode 100644 index 031f24cb1a501..0000000000000 --- a/projects/packages/publicize/tests/php/test-social-image-generator/test-rest-settings-controller.php +++ /dev/null @@ -1,173 +0,0 @@ -clear_options(); - WorDBless_Posts::init()->clear_all_posts(); - WorDBless_Users::init()->clear_all_users(); - - $plan = Current_Plan::PLAN_DATA['free']; - $plan['features']['active'] = array( 'social-image-generator' ); - update_option( Current_Plan::PLAN_OPTION, $plan, true ); - add_filter( 'jetpack_active_modules', array( $this, 'mock_publicize_being_active' ) ); - - global $wp_rest_server; - - $wp_rest_server = new WP_REST_Server(); - $this->server = $wp_rest_server; - $this->admin_id = wp_insert_user( - array( - 'user_login' => 'dummy_user', - 'user_pass' => 'dummy_password', - 'role' => 'administrator', - ) - ); - - wp_set_current_user( 0 ); - - // Register REST routes. - add_action( 'rest_api_init', array( new Social_Image_Generator\REST_Settings_Controller(), 'register_routes' ) ); - - do_action( 'rest_api_init' ); - } - - /** - * Returning the environment into its initial state. - * - * @after - */ - public function tear_down() { - wp_set_current_user( 0 ); - - remove_filter( 'jetpack_active_modules', array( $this, 'mock_publicize_being_active' ) ); - - WorDBless_Options::init()->clear_options(); - WorDBless_Posts::init()->clear_all_posts(); - WorDBless_Users::init()->clear_all_users(); - - $plan = Current_Plan::PLAN_DATA['free']; - $plan['features']['active'] = array(); - update_option( Current_Plan::PLAN_OPTION, $plan, true ); - } - - /** - * Mock Publicize being active. - * - * @return array - */ - public function mock_publicize_being_active() { - return array( 'publicize' ); - } - - /** - * Testing the `GET /jetpack/v4/social-image-generator/settings` endpoint without proper permissions. - */ - public function test_get_settings_without_proper_permission() { - $request = new WP_REST_Request( 'GET', '/jetpack/v4/social-image-generator/settings' ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - $this->assertEquals( 'rest_forbidden_context', $response->get_data()['code'] ); - } - - /** - * Testing the `GET /jetpack/v4/social-image-generator/settings` endpoint with proper permissions. - */ - public function test_get_settings_with_proper_permission() { - wp_set_current_user( $this->admin_id ); - $request = new WP_REST_Request( 'GET', '/jetpack/v4/social-image-generator/settings' ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - $this->assertArrayHasKey( 'enabled', $response->data ); - $this->assertArrayHasKey( 'defaults', $response->data ); - } - - /** - * Testing the `POST /jetpack/v4/social-image-generator/settings` endpoint to update a setting. - */ - public function test_update_settings() { - wp_set_current_user( $this->admin_id ); - $request = new WP_REST_Request( 'POST', '/jetpack/v4/social-image-generator/settings' ); - $request->set_body_params( - array( - 'enabled' => true, - 'defaults' => array( - 'template' => 'edge', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - $this->assertTrue( $response->data['enabled'] ); - $this->assertEquals( 'edge', $response->data['defaults']['template'] ); - } - - /** - * Testing the `POST /jetpack/v4/social-image-generator/settings` endpoint with an non-boolean for enabled. - */ - public function test_update_settings_with_non_boolean() { - wp_set_current_user( $this->admin_id ); - $request = new WP_REST_Request( 'POST', '/jetpack/v4/social-image-generator/settings' ); - $request->set_body_params( array( 'enabled' => 'string' ) ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - $this->assertEquals( 'rest_invalid_param', $response->get_data()['code'] ); - $this->assertEquals( 'rest_invalid_type', $response->get_data()['data']['details']['enabled']['code'] ); - } - - /** - * Testing the `POST /jetpack/v4/social-image-generator/settings` endpoint with an invalid template. - */ - public function test_update_settings_with_invalid_template() { - wp_set_current_user( $this->admin_id ); - $request = new WP_REST_Request( 'POST', '/jetpack/v4/social-image-generator/settings' ); - $request->set_body_params( - array( - 'enabled' => true, - 'defaults' => array( - 'template' => 'invalid_template', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - $this->assertEquals( 'rest_invalid_param', $response->get_data()['code'] ); - $this->assertEquals( 'rest_not_in_enum', $response->get_data()['data']['details']['defaults']['code'] ); - } -} diff --git a/projects/packages/publicize/tests/php/test-social-image-generator/test-setup.php b/projects/packages/publicize/tests/php/test-social-image-generator/test-setup.php index ff4c460a70ee9..b75bf8c5bd63c 100644 --- a/projects/packages/publicize/tests/php/test-social-image-generator/test-setup.php +++ b/projects/packages/publicize/tests/php/test-social-image-generator/test-setup.php @@ -35,7 +35,7 @@ public function set_up() { update_option( Current_Plan::PLAN_OPTION, $plan, true ); add_filter( 'jetpack_active_modules', array( $this, 'mock_publicize_being_active' ) ); // Enable SIG. - ( new Social_Image_Generator\Settings() )->set_enabled( true ); + ( new Jetpack_Social_Settings\Settings() )->update_social_image_generator_settings( array( 'enabled' => true ) ); $this->sig = new Social_Image_Generator\Setup(); $this->sig->init(); // Mock site connection.