From a12f341f76a25e68eba383cce6b814fa9e5f96bf Mon Sep 17 00:00:00 2001 From: Andrew Matthews Date: Fri, 22 Oct 2021 17:09:30 -0400 Subject: [PATCH] Rename settings option to faustwp_settings (#607) * refactor: rename settings option to faustwp_settings * feat: add database upgrade functions --- plugins/faustwp/faustwp.php | 1 + .../faustwp/includes/settings/callbacks.php | 16 ++-- .../faustwp/includes/settings/functions.php | 4 +- .../settings/views/headless-settings.php | 2 +- .../includes/updates/upgrade-database.php | 58 ++++++++++++ .../tests/_support/AcceptanceTester.php | 10 +- .../faustwp/tests/acceptance/SettingsCest.php | 20 ++-- .../integration/settings/test-functions.php | 16 ++-- .../updates/test-upgrade-database.php | 93 +++++++++++++++++++ 9 files changed, 186 insertions(+), 34 deletions(-) create mode 100644 plugins/faustwp/includes/updates/upgrade-database.php create mode 100644 plugins/faustwp/tests/integration/updates/test-upgrade-database.php diff --git a/plugins/faustwp/faustwp.php b/plugins/faustwp/faustwp.php index 0f6b3750c..cfc294469 100644 --- a/plugins/faustwp/faustwp.php +++ b/plugins/faustwp/faustwp.php @@ -45,6 +45,7 @@ require FAUSTWP_DIR . '/includes/rest/callbacks.php'; require FAUSTWP_DIR . '/includes/settings/callbacks.php'; require FAUSTWP_DIR . '/includes/updates/callbacks.php'; +require FAUSTWP_DIR . '/includes/updates/upgrade-database.php'; require FAUSTWP_DIR . '/includes/utilities/callbacks.php'; if ( is_events_enabled() ) { diff --git a/plugins/faustwp/includes/settings/callbacks.php b/plugins/faustwp/includes/settings/callbacks.php index 73a50fac8..7b0e6a088 100644 --- a/plugins/faustwp/includes/settings/callbacks.php +++ b/plugins/faustwp/includes/settings/callbacks.php @@ -45,7 +45,7 @@ function register_settings_menu() { * @return void */ function register_settings() { - register_setting( 'wpe_headless', 'wpe_headless' ); + register_setting( 'faustwp_settings', 'faustwp_settings' ); } add_action( 'admin_init', __NAMESPACE__ . '\\register_settings_section' ); @@ -182,7 +182,7 @@ function display_menu_locations_field() { $menu_locations = faustwp_get_setting( 'menu_locations', 'Primary, Footer' ); ?> - +

@@ -209,7 +209,7 @@ function display_secret_key_field() { ?> - + - +

@@ -274,25 +274,25 @@ function display_enable_disable_fields() { ?>



diff --git a/plugins/faustwp/includes/settings/functions.php b/plugins/faustwp/includes/settings/functions.php index 6cdace9f3..244e1c6ef 100644 --- a/plugins/faustwp/includes/settings/functions.php +++ b/plugins/faustwp/includes/settings/functions.php @@ -106,7 +106,7 @@ function faustwp_update_setting( $name, $value ) { $settings = faustwp_get_settings(); $settings[ $name ] = $value; - update_option( 'wpe_headless', $settings ); + update_option( 'faustwp_settings', $settings ); } /** @@ -115,7 +115,7 @@ function faustwp_update_setting( $name, $value ) { * @return array An array of settings. */ function faustwp_get_settings() { - $settings = get_option( 'wpe_headless', array() ); + $settings = get_option( 'faustwp_settings', array() ); /** * Filter 'faustwp_get_settings'. diff --git a/plugins/faustwp/includes/settings/views/headless-settings.php b/plugins/faustwp/includes/settings/views/headless-settings.php index 0136a1cec..a7e8d802f 100644 --- a/plugins/faustwp/includes/settings/views/headless-settings.php +++ b/plugins/faustwp/includes/settings/views/headless-settings.php @@ -39,7 +39,7 @@
- + diff --git a/plugins/faustwp/includes/updates/upgrade-database.php b/plugins/faustwp/includes/updates/upgrade-database.php new file mode 100644 index 000000000..0d3749cef --- /dev/null +++ b/plugins/faustwp/includes/updates/upgrade-database.php @@ -0,0 +1,58 @@ + 'Version' ) ); + $plugin_version = $file_data['Version']; + + if ( 1 === version_compare( $plugin_version, $current_version ) ) { + + // Array of versions requiring update and their callbacks. + // Note these do not have to exactly match plugin version. + $update_versions = array( + '0.6.1' => 'upgrade_0_6_1', + ); + + foreach ( $update_versions as $version => $callback ) { + if ( 1 === version_compare( $version, $current_version ) ) { + call_user_func( __NAMESPACE__ . '\\' . $callback ); + } + } + + // Save the last updated version. + update_option( 'faustwp_current_version', $plugin_version ); + return true; + } + + return false; +} + +/** + * Update settings option name for versions after to 0.6.1. + * + * @return bool True if the database was updated or false. + */ +function upgrade_0_6_1(): bool { + $settings = get_option( 'wpe_headless', array() ); + + if ( empty( $settings ) ) { + return false; + } + + delete_option( 'wpe_headless' ); + + return update_option( 'faustwp_settings', $settings ); +} diff --git a/plugins/faustwp/tests/_support/AcceptanceTester.php b/plugins/faustwp/tests/_support/AcceptanceTester.php index e596348ef..d207b1611 100644 --- a/plugins/faustwp/tests/_support/AcceptanceTester.php +++ b/plugins/faustwp/tests/_support/AcceptanceTester.php @@ -47,16 +47,16 @@ public function amOnFaustWPSettingsPage() } /** - * Set a wpe_headless setting value. + * Set a faustwp_settings setting value. * - * @param string $name The wpe_headless setting name. - * @param string $value The wpe_headless setting value. + * @param string $name The faustwp_settings setting name. + * @param string $value The faustwp_settings setting value. */ public function haveFaustWPSetting($name, $value = '') { - $options = $this->grabOptionFromDatabase('wpe_headless'); + $options = $this->grabOptionFromDatabase('faustwp_settings') ?: []; $options[ $name ] = $value; - $this->haveOptionInDatabase('wpe_headless', $options); + $this->haveOptionInDatabase('faustwp_settings', $options); } } diff --git a/plugins/faustwp/tests/acceptance/SettingsCest.php b/plugins/faustwp/tests/acceptance/SettingsCest.php index 3ef1f45f7..e47acb0e4 100644 --- a/plugins/faustwp/tests/acceptance/SettingsCest.php +++ b/plugins/faustwp/tests/acceptance/SettingsCest.php @@ -23,17 +23,17 @@ public function i_can_see_the_default_settings(AcceptanceTester $I) // Deactivate plugin and remove settings. $I->deactivatePlugin('faustwp'); - $I->dontHaveOptionInDatabase('wpe_headless'); + $I->dontHaveOptionInDatabase('faustwp_settings'); // Reactivate plugin triggering default settings. $I->activatePlugin('faustwp'); - $settings = $I->grabOptionFromDatabase('wpe_headless'); + $settings = $I->grabOptionFromDatabase('faustwp_settings'); $I->amOnFaustWPSettingsPage(); - $I->seeInField('wpe_headless[frontend_uri]', ''); - $I->seeInField('wpe_headless[secret_key]', $settings['secret_key']); - $I->seeInField('wpe_headless[menu_locations]', 'Primary, Footer'); + $I->seeInField('faustwp_settings[frontend_uri]', ''); + $I->seeInField('faustwp_settings[secret_key]', $settings['secret_key']); + $I->seeInField('faustwp_settings[menu_locations]', 'Primary, Footer'); $I->seeCheckboxIsChecked('#disable_theme'); $I->seeCheckboxIsChecked('#enable_rewrites'); $I->seeCheckboxIsChecked('#enable_redirects'); @@ -48,12 +48,12 @@ public function i_can_regenerate_my_secret_key(AcceptanceTester $I) $I->loginAsAdmin(); $I->amOnFaustWPSettingsPage(); - $old_secret_key = $I->grabValueFrom('wpe_headless[secret_key]'); + $old_secret_key = $I->grabValueFrom('faustwp_settings[secret_key]'); $I->click('.content form a.field-action'); $I->acceptPopup(); - $I->dontSeeInField('wpe_headless[secret_key]', ''); - $I->dontSeeInField('wpe_headless[secret_key]', $old_secret_key); + $I->dontSeeInField('faustwp_settings[secret_key]', ''); + $I->dontSeeInField('faustwp_settings[secret_key]', $old_secret_key); } /** @@ -64,10 +64,10 @@ public function i_can_cancel_my_secret_key_from_being_regenerated(AcceptanceTest $I->loginAsAdmin(); $I->amOnFaustWPSettingsPage(); - $secret_key = $I->grabValueFrom('wpe_headless[secret_key]'); + $secret_key = $I->grabValueFrom('faustwp_settings[secret_key]'); $I->click('.content form a.field-action'); $I->cancelPopup(); - $I->seeInField('wpe_headless[secret_key]', $secret_key); + $I->seeInField('faustwp_settings[secret_key]', $secret_key); } } diff --git a/plugins/faustwp/tests/integration/settings/test-functions.php b/plugins/faustwp/tests/integration/settings/test-functions.php index c9d9ee5e0..282cfda77 100644 --- a/plugins/faustwp/tests/integration/settings/test-functions.php +++ b/plugins/faustwp/tests/integration/settings/test-functions.php @@ -21,44 +21,44 @@ class FunctionsTest extends \WP_UnitTestCase { /** @test */ public function is_redirects_enabled_will_return_true_if_enabled() { - delete_option( 'wpe_headless' ); + delete_option( 'faustwp_settings' ); $this->assertFalse( is_redirects_enabled() ); - update_option( 'wpe_headless', array( 'enable_redirects' => '1' ) ); + update_option( 'faustwp_settings', array( 'enable_redirects' => '1' ) ); $this->assertTrue( is_redirects_enabled() ); } /** @test */ public function is_rewrites_enabled_will_return_true_if_enabled() { - delete_option( 'wpe_headless' ); + delete_option( 'faustwp_settings' ); $this->assertFalse( is_rewrites_enabled() ); - update_option( 'wpe_headless', array( 'enable_rewrites' => '1' ) ); + update_option( 'faustwp_settings', array( 'enable_rewrites' => '1' ) ); $this->assertTrue( is_rewrites_enabled() ); } /** @test */ public function is_themes_disabled_will_return_true_if_disabled() { - delete_option( 'wpe_headless' ); + delete_option( 'faustwp_settings' ); $this->assertFalse( is_themes_disabled() ); - update_option( 'wpe_headless', array( 'disable_theme' => '1' ) ); + update_option( 'faustwp_settings', array( 'disable_theme' => '1' ) ); $this->assertTrue( is_themes_disabled() ); } /** @test */ public function is_image_source_replacement_enabled_will_return_true_if_disabled() { - delete_option( 'wpe_headless' ); + delete_option( 'faustwp_settings' ); $this->assertFalse( is_image_source_replacement_enabled() ); - update_option( 'wpe_headless', array( 'enable_image_source' => '1' ) ); + update_option( 'faustwp_settings', array( 'enable_image_source' => '1' ) ); $this->assertTrue( is_image_source_replacement_enabled() ); } diff --git a/plugins/faustwp/tests/integration/updates/test-upgrade-database.php b/plugins/faustwp/tests/integration/updates/test-upgrade-database.php new file mode 100644 index 000000000..7a22ac2b8 --- /dev/null +++ b/plugins/faustwp/tests/integration/updates/test-upgrade-database.php @@ -0,0 +1,93 @@ + 'Version' ) ); + $plugin_version = $file_data['Version']; + + $this->versions = array( + 'old' => '0.5.0', + 'current' => $plugin_version, + 'new' => $this->new_version( $plugin_version ), + ); + } + + public function tearDown(): void { + parent::tearDown(); + + delete_option( 'faustwp_current_version' ); + } + + /** + * @covers ::\WPE\FaustWP\Updates\upgrade_database() + */ + public function test_upgrade_database_installed_new_version(): void { + update_option( 'faustwp_current_version', $this->versions['old'] ); + + self::assertTrue( upgrade_database() ); + self::assertEquals( get_option( 'faustwp_current_version' ), $this->versions['current'] ); + } + + /** + * @covers ::\WPE\FaustWP\Updates\upgrade_database() + */ + public function test_upgrade_database_no_saved_version(): void { + self::assertTrue( upgrade_database() ); + self::assertEquals( get_option( 'faustwp_current_version' ), $this->versions['current'] ); + } + + /** + * @covers ::\WPE\FaustWP\Updates\upgrade_database() + */ + public function test_upgrade_database_current_version(): void { + update_option( 'faustwp_current_version', $this->versions['current'] ); + + self::assertFalse( upgrade_database() ); + self::assertEquals( get_option( 'faustwp_current_version' ), $this->versions['current'] ); + } + + + + /** + * @covers ::\WPE\FaustWP\Updates\upgrade_database() + */ + public function test_upgrade_database_installed_old_version(): void { + update_option( 'faustwp_current_version', $this->versions['new'] ); + + self::assertFalse( upgrade_database() ); + self::assertEquals( get_option( 'faustwp_current_version' ), $this->versions['new'] ); + } + + /** + * Increments the patch version supplied for easier testing + * + * @param string $old_version The version to increment. + * + * @return string + */ + protected function new_version( $old_version ) { + $version_parts = explode( '.', $old_version ); + + $version_parts[2] = strval( intval( $version_parts[2] ) + 1 ); + + return implode( '.', $version_parts ); + } +}