-
Notifications
You must be signed in to change notification settings - Fork 886
Cookie Consent: Record consent log versions #49996
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
858b04c
feat(cookie-consent): record consent log versions
chihsuan f23774f
test(cookie-consent): cover log version normalization
chihsuan 5a8bbb6
fix(cookie-consent): resolve phan suppressions
chihsuan bc9258a
refactor(cookie-consent): truncate log versions with mb_substr and ad…
chihsuan 1dec233
test(cookie-consent): avoid reflection deprecation on PHP 8.5
chihsuan cfc7ad5
merge(trunk): resolve cookie consent log version conflict
chihsuan 9faaf89
fix(cookie-consent): warn on dropped log version, add insert coverage
chihsuan 42edc81
test(cookie-consent): pre-declare insert capture vars to satisfy phan
chihsuan 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
4 changes: 4 additions & 0 deletions
4
projects/packages/cookie-consent/changelog/add-consent-log-versions
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,4 @@ | ||
| Significance: patch | ||
| Type: changed | ||
|
|
||
| Consent log: Record policy and banner versions. |
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
105 changes: 105 additions & 0 deletions
105
projects/packages/cookie-consent/tests/php/Consent_Log_Controller_Log_Versions_Test.php
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,105 @@ | ||
| <?php | ||
| /** | ||
| * Tests for Consent_Log_Controller log-version handling. | ||
| * | ||
| * @package automattic/jetpack-cookie-consent | ||
| */ | ||
|
|
||
| namespace Automattic\Jetpack\CookieConsent; | ||
|
|
||
| use PHPUnit\Framework\Attributes\CoversMethod; | ||
| use ReflectionMethod; | ||
|
|
||
| /** | ||
| * @covers \Automattic\Jetpack\CookieConsent\Consent_Log_Controller::get_log_versions | ||
| * @covers \Automattic\Jetpack\CookieConsent\Consent_Log_Controller::truncate_log_version | ||
| */ | ||
| #[CoversMethod( Consent_Log_Controller::class, 'get_log_versions' )] | ||
| #[CoversMethod( Consent_Log_Controller::class, 'truncate_log_version' )] | ||
| class Consent_Log_Controller_Log_Versions_Test extends TestCase { | ||
|
|
||
| /** | ||
| * Tear down: clear cookie-consent config filters. | ||
| */ | ||
| public function tearDown(): void { | ||
| remove_all_filters( 'jetpack_cookie_consent_config' ); | ||
| parent::tearDown(); | ||
| } | ||
|
|
||
| /** | ||
| * Invoke a private method on a fresh controller instance. | ||
| * | ||
| * @param string $method Method name. | ||
| * @param mixed ...$args Arguments. | ||
| * @return mixed | ||
| */ | ||
| private function invoke( $method, ...$args ) { | ||
| $controller = new Consent_Log_Controller(); | ||
| $reflection = new ReflectionMethod( Consent_Log_Controller::class, $method ); | ||
| if ( PHP_VERSION_ID < 80100 ) { | ||
| $reflection->setAccessible( true ); | ||
| } | ||
|
|
||
| return $reflection->invoke( $controller, ...$args ); | ||
| } | ||
|
|
||
| /** | ||
| * Versions within the column length are returned unchanged. | ||
| */ | ||
| public function test_truncate_log_version_keeps_short_values() { | ||
| $this->assertSame( 'policy-2026-06', $this->invoke( 'truncate_log_version', 'policy-2026-06' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Over-length values are truncated to the varchar(191) column limit. | ||
| */ | ||
| public function test_truncate_log_version_truncates_to_column_limit() { | ||
| $truncated = $this->invoke( 'truncate_log_version', str_repeat( 'a', 300 ) ); | ||
|
|
||
| $this->assertSame( 191, strlen( $truncated ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Truncation counts characters, not bytes, so multibyte sequences are not split. | ||
| */ | ||
| public function test_truncate_log_version_does_not_split_multibyte_characters() { | ||
| $truncated = $this->invoke( 'truncate_log_version', str_repeat( 'é', 300 ) ); | ||
|
|
||
| // 191 characters kept, and no multibyte sequence was split mid-byte. | ||
| $this->assertSame( 191, mb_strlen( $truncated ) ); | ||
| $this->assertSame( $truncated, mb_convert_encoding( $truncated, 'UTF-8', 'UTF-8' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Normalized defaults are returned when no config is supplied. | ||
| */ | ||
| public function test_get_log_versions_returns_defaults() { | ||
| $this->assertSame( | ||
| array( | ||
| 'policy_version' => '1', | ||
| 'banner_version' => '1', | ||
| ), | ||
| $this->invoke( 'get_log_versions' ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Configured values are normalized and truncated end to end. | ||
| */ | ||
| public function test_get_log_versions_normalizes_and_truncates() { | ||
| add_filter( | ||
| 'jetpack_cookie_consent_config', | ||
| static function ( $config ) { | ||
| $config['log']['policy_version'] = ' policy-trimmed '; | ||
| $config['log']['banner_version'] = str_repeat( 'b', 300 ); | ||
|
|
||
| return $config; | ||
| } | ||
| ); | ||
|
|
||
| $versions = $this->invoke( 'get_log_versions' ); | ||
|
|
||
| $this->assertSame( 'policy-trimmed', $versions['policy_version'] ); | ||
| $this->assertSame( 191, strlen( $versions['banner_version'] ) ); | ||
| } | ||
| } |
Oops, something went wrong.
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.