diff --git a/assets/src/settings-page/index.js b/assets/src/settings-page/index.js index 0dd3a18e54b..fc22400a130 100644 --- a/assets/src/settings-page/index.js +++ b/assets/src/settings-page/index.js @@ -277,7 +277,9 @@ function Root( { appRoot } ) { initialOpen={ 'other-settings' === focusedSection } > - + { HAS_DEPENDENCY_SUPPORT && ( + + ) } diff --git a/src/DevTools/UserAccess.php b/src/DevTools/UserAccess.php index 0da4ed94c83..76b9fd9fc3a 100644 --- a/src/DevTools/UserAccess.php +++ b/src/DevTools/UserAccess.php @@ -146,13 +146,29 @@ public function register_rest_field() { ); } + /** + * Determine whether the option can be modified. + * + * @param int $user_id User ID. + * @return bool Whether the option can be modified. + */ + private function can_modify_option( $user_id ) { + return ( + $this->dependency_support->has_support() + && + current_user_can( 'edit_user', $user_id ) + && + AMP_Validation_Manager::has_cap( $user_id ) + ); + } + /** * Add the developer tools checkbox to the user edit screen. * * @param WP_User $profile_user Current user being edited. */ public function print_personal_options( $profile_user ) { - if ( ! current_user_can( 'edit_user', $profile_user->ID ) || ! AMP_Validation_Manager::has_cap( $profile_user ) ) { + if ( ! $this->can_modify_option( $profile_user->ID ) ) { return; } ?> @@ -177,7 +193,7 @@ public function print_personal_options( $profile_user ) { * @return bool Whether update was successful. */ public function update_user_setting( $user_id ) { - if ( ! current_user_can( 'edit_user', $user_id ) || ! AMP_Validation_Manager::has_cap( $user_id ) ) { + if ( ! $this->can_modify_option( $user_id ) ) { return false; } $enabled = isset( $_POST[ self::USER_FIELD_DEVELOPER_TOOLS_ENABLED ] ) && rest_sanitize_boolean( wp_unslash( $_POST[ self::USER_FIELD_DEVELOPER_TOOLS_ENABLED ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce handled by user-edit.php; sanitization used is sanitized. diff --git a/tests/php/src/DevTools/UserAccessTest.php b/tests/php/src/DevTools/UserAccessTest.php index f74eae473ea..61bc5a36f9e 100644 --- a/tests/php/src/DevTools/UserAccessTest.php +++ b/tests/php/src/DevTools/UserAccessTest.php @@ -165,6 +165,7 @@ public function test_register_rest_field() { /** * Tests UserAccess::print_personal_options * + * @covers ::can_modify_option * @covers ::print_personal_options */ public function test_print_personal_options() { @@ -182,12 +183,18 @@ public function test_print_personal_options() { ob_start(); $this->dev_tools_user_access->print_personal_options( $admin_user ); - $this->assertStringContainsString( 'checkbox', ob_get_clean() ); + $output = ob_get_clean(); + if ( ( new DependencySupport() )->has_support() ) { + $this->assertStringContainsString( 'checkbox', $output ); + } else { + $this->assertStringNotContainsString( 'checkbox', $output ); + } } /** * Tests UserAccess::update_user_setting * + * @covers ::can_modify_option * @covers ::update_user_setting */ public function test_update_user_setting() { @@ -201,10 +208,10 @@ public function test_update_user_setting() { wp_set_current_user( $admin_user->ID ); $this->assertFalse( $this->dev_tools_user_access->update_user_setting( $editor_user->ID ) ); - $this->assertTrue( $this->dev_tools_user_access->update_user_setting( $admin_user->ID ) ); - $this->assertTrue( $this->dev_tools_user_access->get_user_enabled( $admin_user ) ); + $this->assertEquals( ( new DependencySupport() )->has_support(), $this->dev_tools_user_access->update_user_setting( $admin_user->ID ) ); + $this->assertEquals( ( new DependencySupport() )->has_support(), $this->dev_tools_user_access->get_user_enabled( $admin_user ) ); $_POST[ UserAccess::USER_FIELD_DEVELOPER_TOOLS_ENABLED ] = null; - $this->assertTrue( $this->dev_tools_user_access->update_user_setting( $admin_user->ID ) ); + $this->assertEquals( ( new DependencySupport() )->has_support(), $this->dev_tools_user_access->update_user_setting( $admin_user->ID ) ); $this->assertFalse( $this->dev_tools_user_access->get_user_enabled( $admin_user ) ); }