Skip to content

Commit

Permalink
Hide DevTools toggles when dependency_support is absent
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Dec 13, 2021
1 parent 779104f commit ff7e7b7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
4 changes: 3 additions & 1 deletion assets/src/settings-page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ function Root( { appRoot } ) {
initialOpen={ 'other-settings' === focusedSection }
>
<MobileRedirection />
<DeveloperTools />
{ HAS_DEPENDENCY_SUPPORT && (
<DeveloperTools />
) }
<DeleteDataAtUninstall />
</AMPDrawer>
<SettingsFooter />
Expand Down
20 changes: 18 additions & 2 deletions src/DevTools/UserAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
?>
Expand All @@ -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.
Expand Down
15 changes: 11 additions & 4 deletions tests/php/src/DevTools/UserAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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 ) );
}

Expand Down

0 comments on commit ff7e7b7

Please sign in to comment.