Skip to content

Commit

Permalink
Add data removal setting
Browse files Browse the repository at this point in the history
  • Loading branch information
timarney committed Jul 18, 2022
1 parent 64c67ff commit 56756c1
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 0 deletions.
12 changes: 12 additions & 0 deletions includes/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function register(): void {
add_filter( 'default_content', [ $this, 'prefill_post_content' ], 10, 2 );
add_filter( 'default_title', [ $this, 'prefill_post_title' ] );
add_filter( 'display_media_states', [ $this, 'media_states' ], 10, 2 );
add_filter( 'web_stories_erase_data_on_uninstall', [ $this, 'data_removal' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -258,4 +259,15 @@ public function media_states( $media_states, $post ): array {
}
return $media_states;
}

/**
* Returns data removal setting.
*
* @since 1.23.0
*
* @return bool data removal value.
*/
public function data_removal(): array {
return absint( $this->settings->get_setting( $this->settings::SETTING_NAME_DATA_REMOVAL ) );
}
}
16 changes: 16 additions & 0 deletions includes/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ class Settings extends Service_Base {
*/
public const SETTING_NAME_VIDEO_CACHE = 'web_stories_video_cache';

/**
* Data removal setting name.
*/
public const SETTING_NAME_DATA_REMOVAL = 'web_stories_data_removal';

/**
* Web Stories archive setting name.
*/
Expand Down Expand Up @@ -253,6 +258,17 @@ public function register(): void {
]
);

register_setting(
self::SETTING_GROUP,
self::SETTING_NAME_DATA_REMOVAL,
[
'description' => __( 'Data Removal', 'web-stories' ),
'type' => 'boolean',
'default' => false,
'show_in_rest' => true,
]
);

register_setting(
self::SETTING_GROUP,
self::SETTING_NAME_ARCHIVE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import {
disableCheckbox,
enableCheckbox,
visitSettings,
} from '@web-stories-wp/e2e-test-utils';

/**
* Internal dependencies
*/
import { dataRemovalCheckboxSelector } from '../../../../utils';

describe('Admin User', () => {
beforeEach(async () => {
await visitSettings();
await enableCheckbox(dataRemovalCheckboxSelector);
});

it('should let me see and update data removal settings', async () => {
// verify that the data removal checkbox can be changed
await expect(page).toMatchElement(`${dataRemovalCheckboxSelector}:checked`);

await disableCheckbox(dataRemovalCheckboxSelector);

// verify that the data removal checkbox can be changed
await expect(page).toMatchElement(`${dataRemovalCheckboxSelector}:checked`);
await disableCheckbox(dataRemovalCheckboxSelector);
});
});
3 changes: 3 additions & 0 deletions packages/e2e-tests/src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ export const videoOptimizationCheckboxSelector =

export const videoCacheCheckboxSelector =
'input[data-testid="video-cache-settings-checkbox"]';

export const dataRemovalCheckboxSelector =
'input[data-testid="data-removal-settings-checkbox"]';
2 changes: 2 additions & 0 deletions packages/wp-dashboard/src/api/reducers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const defaultSettingsState = {
archive: ARCHIVE_TYPE.DEFAULT,
archivePageId: 0,
videoCache: false,
dataRemoval: false,
settingSaved: false,
shoppingProvider: SHOPPING_PROVIDER_TYPE.NONE,
shopifyHost: '',
Expand Down Expand Up @@ -76,6 +77,7 @@ function settingsReducer(state, action) {
adManagerSlotId: action.payload.adManagerSlotId,
adNetwork: action.payload.adNetwork,
videoCache: action.payload.videoCache,
dataRemoval: action.payload.dataRemoval,
archive: action.payload.archive,
archivePageId: action.payload.archivePageId,
shoppingProvider: action.payload.shoppingProvider,
Expand Down
6 changes: 6 additions & 0 deletions packages/wp-dashboard/src/api/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const transformSettingResponse = (response) => ({
adManagerSlotId: response.web_stories_ad_manager_slot_id,
adNetwork: response.web_stories_ad_network,
videoCache: response.web_stories_video_cache,
dataRemoval: response.web_stories_data_removal,
archive: response.web_stories_archive,
archivePageId: response.web_stories_archive_page_id,
shoppingProvider: response.web_stories_shopping_provider,
Expand Down Expand Up @@ -73,6 +74,7 @@ export function updateSettings(apiPath, queryParams) {
adManagerSlotId,
adNetwork,
videoCache,
dataRemoval,
archive,
archivePageId,
shoppingProvider,
Expand Down Expand Up @@ -110,6 +112,10 @@ export function updateSettings(apiPath, queryParams) {
query.web_stories_video_cache = Boolean(videoCache);
}

if (dataRemoval !== undefined) {
query.web_stories_data_removal = Boolean(dataRemoval);
}

if (archive !== undefined) {
query.web_stories_archive = archive;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import { useCallback } from '@googleforcreators/react';
import { __ } from '@googleforcreators/i18n';
import { Checkbox, THEME_CONSTANTS } from '@googleforcreators/design-system';

/**
* Internal dependencies
*/
import {
SettingForm,
SettingHeading,
CheckboxLabel,
CheckboxLabelText,
} from '../components';

export default function DataRemovalSettings({
isEnabled = false,
updateSettings,
}) {
const onChange = useCallback(
() => updateSettings({ dataRemoval: !isEnabled }),
[updateSettings, isEnabled]
);

return (
<SettingForm>
<div>
<SettingHeading>{__('Data removal', 'web-stories')}</SettingHeading>
</div>
<div>
<CheckboxLabel forwardedAs="label" htmlFor="data-removal-settings">
<Checkbox
id="data-removal-settings"
data-testid="data-removal-settings-checkbox"
onChange={onChange}
checked={Boolean(isEnabled)}
/>
<CheckboxLabelText
size={THEME_CONSTANTS.TYPOGRAPHY.PRESET_SIZES.SMALL}
aria-checked={Boolean(isEnabled)}
forwardedAs="span"
>
{__(
'Remove data when uninstalling the Web Story plugin.',
'web-stories'
)}
</CheckboxLabelText>
</CheckboxLabel>
</div>
</SettingForm>
);
}

DataRemovalSettings.propTypes = {
isEnabled: PropTypes.bool,
updateSettings: PropTypes.func.isRequired,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Internal dependencies
*/
import DataRemovalSettings from '..';

export default {
title: 'Dashboard/Views/EditorSettings/DataRemovalSettings',
component: DataRemovalSettings,
args: {
isEnabled: true,
},
argTypes: {
updateSettings: { action: 'updateSettings fired' },
},
};

export const _default = (args) => {
return <DataRemovalSettings {...args} />;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* External dependencies
*/
import { fireEvent, screen } from '@testing-library/react';

/**
* Internal dependencies
*/
import DataRemovalSettings from '..';
import { renderWithProviders } from '../../../../testUtils';

describe('Editor Settings: <DataRemovalSettings />', function () {
it('should render the data removal setting as checked when isEnabled', function () {
renderWithProviders(
<DataRemovalSettings updateSettings={jest.fn()} isEnabled />
);

expect(screen.getByRole('checkbox')).toBeChecked();
});

it('should not render the data removal setting as checked when not isEnabled', function () {
renderWithProviders(<DataRemovalSettings updateSettings={jest.fn()} />);

expect(screen.getByRole('checkbox')).not.toBeChecked();
});

it('should update settings when the checkbox is clicked.', function () {
const onChange = jest.fn();
renderWithProviders(<DataRemovalSettings updateSettings={onChange} />);

const checkbox = screen.getByRole('checkbox');
fireEvent.click(checkbox);

expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith({ dataRemoval: true });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import PublisherLogoSettings from './publisherLogo';
import TelemetrySettings from './telemetry';
import MediaOptimizationSettings from './mediaOptimization';
import VideoCacheSettings from './videoCache';
import DataRemovalSettings from './dataRemoval';
import ArchiveSettings from './archive';
import GoogleAnalyticsSettings from './googleAnalytics';
import { Wrapper, Main } from './components';
Expand All @@ -58,6 +59,7 @@ function EditorSettings() {
newlyCreatedMediaIds,
isMediaLoading,
videoCache,
dataRemoval,
archive,
archivePageId,
searchPages,
Expand Down Expand Up @@ -97,6 +99,7 @@ function EditorSettings() {
adManagerSlotId,
adNetwork,
videoCache,
dataRemoval,
archive,
archivePageId,
shoppingProvider,
Expand All @@ -120,6 +123,7 @@ function EditorSettings() {
isMediaLoading,
newlyCreatedMediaIds,
videoCache,
dataRemoval,
archive,
archivePageId,
searchPages,
Expand Down Expand Up @@ -368,6 +372,10 @@ function EditorSettings() {
isEnabled={videoCache}
updateSettings={updateSettings}
/>
<DataRemovalSettings
isEnabled={dataRemoval}
updateSettings={updateSettings}
/>
<ArchiveSettings
archive={archive}
archiveURL={archiveURL}
Expand Down
21 changes: 21 additions & 0 deletions tests/phpunit/integration/tests/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function test_register(): void {
$this->assertSame( 99, has_filter( 'admin_body_class', [ $this->instance, 'admin_body_class' ] ) );
$this->assertSame( 10, has_filter( 'default_content', [ $this->instance, 'prefill_post_content' ] ) );
$this->assertSame( 10, has_filter( 'default_title', [ $this->instance, 'prefill_post_title' ] ) );
$this->assertSame( 10, has_filter( 'data_removal', [ $this->instance, 'data_removal' ] ) );

}

Expand Down Expand Up @@ -249,4 +250,24 @@ public function test_media_states_with_active_logo(): void {
$result = $this->instance->media_states( [], $post );
$this->assertEqualSets( [ 'Web Stories Publisher Logo' ], $result );
}

/**
* @covers ::data_removal
*/
public function test_data_removal_on(): void {
$post = self::factory()->post->create_and_get( [] );
$this->settings->update_setting( $this->settings::SETTING_NAME_DATA_REMOVAL, true );
$result = $this->instance->data_removal();
$this->assertEqualSets( true, $result );
}

/**
* @covers ::data_removal
*/
public function test_data_removal_off(): void {
$post = self::factory()->post->create_and_get( [] );
$this->settings->update_setting( $this->settings::SETTING_NAME_DATA_REMOVAL, false );
$result = $this->instance->data_removal();
$this->assertEqualSets( false, $result );
}
}
Loading

0 comments on commit 56756c1

Please sign in to comment.