From f37dd1a70f2259ef236166a183b2eec1c1c1f2c5 Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Tue, 14 Nov 2023 15:39:38 +0100 Subject: [PATCH] Add the new settings object + tests --- .../class-settings.php | 251 ++++++++++++++++++ .../test-auto-conversion.php | 98 +++++++ .../test-jetpack-social-settings.php | 147 ++++++++++ .../test-social-image-generator-settings.php | 117 ++++++++ 4 files changed, 613 insertions(+) create mode 100644 projects/packages/publicize/src/jetpack-social-settings/class-settings.php create mode 100644 projects/packages/publicize/tests/php/jetpack-social-settings/test-auto-conversion.php create mode 100644 projects/packages/publicize/tests/php/jetpack-social-settings/test-jetpack-social-settings.php create mode 100644 projects/packages/publicize/tests/php/jetpack-social-settings/test-social-image-generator-settings.php diff --git a/projects/packages/publicize/src/jetpack-social-settings/class-settings.php b/projects/packages/publicize/src/jetpack-social-settings/class-settings.php new file mode 100644 index 0000000000000..71fe8a6374de9 --- /dev/null +++ b/projects/packages/publicize/src/jetpack-social-settings/class-settings.php @@ -0,0 +1,251 @@ + false, + 'template' => Templates::DEFAULT_TEMPLATE, + ); + + const DEFAULT_AUTOCONVERT_IMAGES_SETTINGS = array( + 'enabled' => true, + ); + + /** + * Migrate old options to the new settings. Previously SIG settings were stored in the + * jetpack_social_image_generator_settings option. Now they are stored in the jetpack_social_settings + * together with the auto conversion settings. + * + * TODO: Work out if this is possible on plugin upgrade + * + * @return void + */ + private function migrate_old_option() { + $auto_conversion_settings = get_option( 'jetpack_social_settings' ); + if ( ! empty( $auto_conversion_settings['image'] ) ) { + update_option( self::OPTION_PREFIX . self::AUTOCONVERT_IMAGES, array( 'enabled' => $auto_conversion_settings['image'] ) ); + delete_option( 'jetpack_social_settings' ); + } + + $sig_settings = get_option( 'jetpack_social_image_generator_settings' ); + if ( isset( $sig_settings['defaults']['template'] ) ) { + update_option( + self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS, + array( + 'enabled' => $sig_settings['enabled'], + 'template' => $sig_settings['defaults']['template'], + ) + ); + } + } + + /** + * Register the settings. + * + * @return void + */ + public function register_settings() { + register_setting( + 'general', + self::OPTION_PREFIX . self::AUTOCONVERT_IMAGES, + array( + 'default' => array( + 'enabled' => true, + ), + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'enabled' => array( + 'type' => 'boolean', + ), + ), + ), + ), + 'type' => 'boolean', + ) + ); + + register_setting( + 'general', + self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS, + array( + 'type' => 'object', + 'default' => array( + 'enabled' => false, + 'template' => Templates::DEFAULT_TEMPLATE, + ), + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'enabled' => array( + 'type' => 'boolean', + ), + 'template' => array( + 'type' => 'string', + ), + ), + ), + ), + ) + ); + + add_filter( 'rest_pre_update_setting', array( $this, 'update_settings' ), 10, 3 ); + } + + /** + * Get the current settings. + * + * @param bool $with_available Whether to include the available status of the features. + * + * @return array + */ + public function get_settings( $with_available = false ) { + $this->migrate_old_option(); + + $settings = array( + 'autoConversionSettings' => get_option( self::OPTION_PREFIX . self::AUTOCONVERT_IMAGES, self::DEFAULT_AUTOCONVERT_IMAGES_SETTINGS ), + 'socialImageGeneratorSettings' => get_option( self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS, self::DEFAULT_IMAGE_GENERATOR_SETTINGS ), + ); + + // The feature cannot be enabled without Publicize. + if ( ! ( new Modules() )->is_active( 'publicize' ) ) { + $settings['autoConversionSettings']['enabled'] = false; + $settings['socialImageGeneratorSettings']['enabled'] = false; + } + + if ( $with_available ) { + $settings['autoConversionSettings']['available'] = $this->is_auto_conversion_available(); + $settings['socialImageGeneratorSettings']['available'] = $this->is_sig_available(); + } + + return $settings; + } + + /** + * Update the settings. + * + * @param bool $updated The updated settings. + * @param string $name The name of the setting. + * @param mixed $value The value of the setting. + * + * @return bool + */ + public function update_settings( $updated, $name, $value ) { + if ( self::OPTION_PREFIX . self::AUTOCONVERT_IMAGES === $name ) { + return $this->update_auto_conversion_setting( $value ); + } + + if ( self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS === $name ) { + return $this->update_social_image_generator_settings( $value ); + } + return $updated; + } + + /** + * Update the auto conversion settings. + * + * @param array $new_setting The new settings. + * + * @return bool + */ + public function update_auto_conversion_setting( $new_setting ) { + $this->migrate_old_option(); + $auto_conversion_settings = get_option( self::OPTION_PREFIX . self::AUTOCONVERT_IMAGES ); + + if ( empty( $auto_conversion_settings ) || ! is_array( $auto_conversion_settings ) ) { + $auto_conversion_settings = self::DEFAULT_AUTOCONVERT_IMAGES_SETTINGS; + } + + return update_option( self::OPTION_PREFIX . self::AUTOCONVERT_IMAGES, array_replace_recursive( $auto_conversion_settings, $new_setting ) ); + } + + /** + * Update the social image generator settings. + * + * @param array $new_setting The new settings. + * + * @return bool + */ + public function update_social_image_generator_settings( $new_setting ) { + $this->migrate_old_option(); + $sig_settings = get_option( self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS ); + + if ( empty( $sig_settings ) || ! is_array( $sig_settings ) ) { + $sig_settings = self::DEFAULT_IMAGE_GENERATOR_SETTINGS; + } + + return update_option( self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS, array_replace_recursive( $sig_settings, $new_setting ) ); + } + + /** + * Check if SIG is available. + * + * @return bool True if SIG is available, false otherwise. + */ + public function is_sig_available() { + global $publicize; + + if ( ! $publicize ) { + return false; + } + + return $publicize->has_social_image_generator_feature(); + } + + /** + * Check if the auto conversion feature is available. + * + * @param string $type Whether video or image. + + * @return bool True if available, false otherwise. + */ + public function is_auto_conversion_available( $type = 'image' ) { + global $publicize; + + if ( ! $publicize ) { + return false; + } + + return $publicize->has_social_auto_conversion_feature( $type ); + } + + /** + * Get the default template. + * + * @return string + */ + public function sig_get_default_template() { + $sig_settings = get_option( self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS ); + if ( empty( $sig_settings ) || ! is_array( $sig_settings ) ) { + $sig_settings = self::DEFAULT_IMAGE_GENERATOR_SETTINGS; + } + return $sig_settings['template']; + } +} diff --git a/projects/packages/publicize/tests/php/jetpack-social-settings/test-auto-conversion.php b/projects/packages/publicize/tests/php/jetpack-social-settings/test-auto-conversion.php new file mode 100644 index 0000000000000..533f01732ecaf --- /dev/null +++ b/projects/packages/publicize/tests/php/jetpack-social-settings/test-auto-conversion.php @@ -0,0 +1,98 @@ +getMockBuilder( Publicize::class )->setMethods( array( 'has_social_auto_conversion_feature' ) )->getMock(); + $publicize->method( 'has_social_auto_conversion_feature' ) + ->withAnyParameters() + ->willReturn( true ); + $this->settings = new SocialSettings(); + $this->settings->register_settings(); + } + + /** + * 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' ); + } + + /** + * Test that Auto-Conversion is available based on the plan check. + */ + public function test_correctly_returns_available_status() { + $this->assertTrue( $this->settings->is_auto_conversion_available() ); + } + + /** + * Test that it correctly returns enabled or disabled. + */ + public function test_correctly_returns_enabled_status() { + $auto_conversion_settings = $this->settings->get_settings()['autoConversionSettings']; + $this->assertTrue( $auto_conversion_settings['enabled'] ); + $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->update_auto_conversion_setting( array( 'enabled' => false ) ); + $auto_conversion_settings = $this->settings->get_settings()['autoConversionSettings']; + $this->assertFalse( $auto_conversion_settings['enabled'] ); + + $this->settings->update_auto_conversion_setting( array( 'video' => true ) ); + $auto_conversion_settings = $this->settings->get_settings()['autoConversionSettings']; + $this->assertFalse( $auto_conversion_settings['enabled'] ); + $this->assertTrue( $auto_conversion_settings['video'] ); + } +} 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..bcb40754a0e19 --- /dev/null +++ b/projects/packages/publicize/tests/php/jetpack-social-settings/test-jetpack-social-settings.php @@ -0,0 +1,147 @@ +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(); + $this->settings->register_settings(); + } + + /** + * 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( 'enabled', $settings['socialImageGeneratorSettings'] ); + $this->assertArrayHasKey( 'template', $settings['socialImageGeneratorSettings'] ); + + $this->assertTrue( $settings['autoConversionSettings']['enabled'] ); + $this->assertFalse( $settings['socialImageGeneratorSettings']['enabled'] ); + $this->assertEquals( Templates::DEFAULT_TEMPLATE, $settings['socialImageGeneratorSettings']['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( 'enabled' => true ), + 'socialImageGeneratorSettings' => array( + 'enabled' => true, + '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( 'enabled' => true ), + 'socialImageGeneratorSettings' => array( + 'enabled' => false, + 'template' => Templates::DEFAULT_TEMPLATE, + ), + ); + + $this->settings = new SocialSettings(); + $this->assertEquals( $expected_options, $this->settings->get_settings() ); + } +} diff --git a/projects/packages/publicize/tests/php/jetpack-social-settings/test-social-image-generator-settings.php b/projects/packages/publicize/tests/php/jetpack-social-settings/test-social-image-generator-settings.php new file mode 100644 index 0000000000000..9efa8e4c72625 --- /dev/null +++ b/projects/packages/publicize/tests/php/jetpack-social-settings/test-social-image-generator-settings.php @@ -0,0 +1,117 @@ +settings = new SocialSettings(); + $this->settings->register_settings(); + } + + /** + * Tear down + * + * @after + */ + public function tear_down() { + remove_filter( 'jetpack_active_modules', array( $this, 'mock_publicize_being_active' ) ); + $plan = Current_Plan::PLAN_DATA['free']; + $plan['features']['active'] = array(); + update_option( Current_Plan::PLAN_OPTION, $plan, true ); + 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' ); + } + + /** + * Test that SIG is available based on the plan check. + */ + public function test_correctly_returns_available_status() { + $this->assertTrue( $this->settings->is_sig_available() ); + } + + /** + * Test that it correctly returns enabled or disabled. + */ + public function test_correctly_returns_enabled_status() { + $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() { + $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() { + $sig_settings = $this->settings->get_settings()['socialImageGeneratorSettings']; + $this->assertEquals( Templates::DEFAULT_TEMPLATE, $sig_settings['template'] ); + } + + /** + * Test that it returns correct template if set. + */ + public function test_returns_correct_template_if_set() { + $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['template'] ); + } +}