-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Privacy: Introduce wp_count_user_requests() to publicly expose user request counts by type #12716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SainathPoojary
wants to merge
4
commits into
WordPress:trunk
Choose a base branch
from
SainathPoojary:fix/44034
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+230
−34
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
136d4ea
Privacy: Introduce wp_count_user_requests() to publicly expose user r…
SainathPoojary 2b7839c
fix: implement salted caching for wp_count_user_requests
SainathPoojary 6a99ce7
test: add unit tests for wp_count_user_requests() functionality
SainathPoojary a9ad21a
fix: update wp_count_user_requests to handle missing post statuses in…
SainathPoojary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| <?php | ||
| /** | ||
| * Test cases for the `wp_count_user_requests()` function. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage UnitTests | ||
| * @since 6.8.0 | ||
| * | ||
| * @group privacy | ||
| * @covers ::wp_count_user_requests | ||
| */ | ||
| class Tests_Privacy_wpCountUserRequests extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Export request ID. | ||
| * | ||
| * @since 6.8.0 | ||
| * | ||
| * @var int $export_request_id | ||
| */ | ||
| protected static $export_request_id; | ||
|
|
||
| /** | ||
| * Erase request ID. | ||
| * | ||
| * @since 6.8.0 | ||
| * | ||
| * @var int $erase_request_id | ||
| */ | ||
| protected static $erase_request_id; | ||
|
|
||
| /** | ||
| * Create fixtures. | ||
| * | ||
| * @since 6.8.0 | ||
| * | ||
| * @param WP_UnitTest_Factory $factory Factory. | ||
| */ | ||
| public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { | ||
| self::$export_request_id = $factory->post->create( | ||
| array( | ||
| 'post_type' => 'user_request', | ||
| 'post_name' => 'export_personal_data', | ||
| 'post_status' => 'request-pending', | ||
| 'post_title' => 'export@local.test', | ||
| ) | ||
| ); | ||
|
|
||
| self::$erase_request_id = $factory->post->create( | ||
| array( | ||
| 'post_type' => 'user_request', | ||
| 'post_name' => 'remove_personal_data', | ||
| 'post_status' => 'request-confirmed', | ||
| 'post_title' => 'erase@local.test', | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Set up before each test. | ||
| * | ||
| * @since 6.8.0 | ||
| */ | ||
| public function set_up() { | ||
| parent::set_up(); | ||
| // Clear counts cache before each test. | ||
| wp_cache_delete( 'user-request-export_personal_data', 'counts' ); | ||
| wp_cache_delete( 'user-request-remove_personal_data', 'counts' ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns an empty stdClass for an invalid or empty type string. | ||
| * | ||
| * @ticket 44034 | ||
| */ | ||
| public function test_returns_empty_object_for_invalid_type() { | ||
| $actual_empty = wp_count_user_requests( '' ); | ||
| $this->assertInstanceOf( 'stdClass', $actual_empty ); | ||
| $this->assertEmpty( (array) $actual_empty ); | ||
|
|
||
| $actual_invalid = wp_count_user_requests( 'invalid_type' ); | ||
| $this->assertInstanceOf( 'stdClass', $actual_invalid ); | ||
| $this->assertEmpty( (array) $actual_invalid ); | ||
| } | ||
|
|
||
| /** | ||
| * Counts export requests correctly by status. | ||
| * | ||
| * @ticket 44034 | ||
| */ | ||
| public function test_counts_export_requests_by_status() { | ||
| $actual = wp_count_user_requests( 'export_personal_data' ); | ||
|
|
||
| $this->assertSame( 1, (int) $actual->{'request-pending'} ); | ||
| $this->assertSame( 0, (int) $actual->{'request-confirmed'} ); | ||
| } | ||
|
|
||
| /** | ||
| * Counts erase requests correctly by status. | ||
| * | ||
| * @ticket 44034 | ||
| */ | ||
| public function test_counts_erase_requests_by_status() { | ||
| $actual = wp_count_user_requests( 'remove_personal_data' ); | ||
|
|
||
| $this->assertSame( 0, (int) $actual->{'request-pending'} ); | ||
| $this->assertSame( 1, (int) $actual->{'request-confirmed'} ); | ||
| } | ||
|
|
||
| /** | ||
| * Result contains all registered post statuses as keys. | ||
| * | ||
| * @ticket 44034 | ||
| */ | ||
| public function test_result_contains_all_post_statuses() { | ||
| $actual = wp_count_user_requests( 'export_personal_data' ); | ||
| $stati = get_post_stati(); | ||
| $actual_keys = array_keys( (array) $actual ); | ||
|
|
||
| foreach ( $stati as $status ) { | ||
| $this->assertContains( $status, $actual_keys, "Missing status: $status" ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Result is cached after first call. | ||
| * | ||
| * @ticket 44034 | ||
| */ | ||
| public function test_result_is_cached() { | ||
| wp_count_user_requests( 'export_personal_data' ); | ||
|
|
||
| $last_changed = wp_cache_get_last_changed( 'posts' ); | ||
| $cached = wp_cache_get_salted( 'user-request-export_personal_data', 'counts', $last_changed ); | ||
|
|
||
| $this->assertNotFalse( $cached ); | ||
| $this->assertInstanceOf( 'stdClass', $cached ); | ||
| } | ||
|
|
||
| /** | ||
| * The `wp_count_user_requests` filter can modify the returned counts. | ||
| * | ||
| * @ticket 44034 | ||
| */ | ||
| public function test_filter_can_modify_counts() { | ||
| add_filter( | ||
| 'wp_count_user_requests', | ||
| function ( $counts, $type ) { | ||
| $this->assertSame( 'export_personal_data', $type ); | ||
| $modified = new stdClass(); | ||
| $modified->{'request-pending'} = 999; | ||
| return $modified; | ||
| }, | ||
| 10, | ||
| 2 | ||
| ); | ||
|
|
||
| $actual = wp_count_user_requests( 'export_personal_data' ); | ||
|
|
||
| $this->assertSame( 999, (int) $actual->{'request-pending'} ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.