add/explat experiment name validation (#44535)

This commit is contained in:
RJ 2024-02-15 13:35:23 +09:00 committed by GitHub
parent 94c0578029
commit 141c2e4399
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 56 additions and 20 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Bumped explat-client and explat-client-react-helper versions

View File

@ -54,8 +54,8 @@
]
},
"dependencies": {
"@automattic/explat-client": "^0.0.3",
"@automattic/explat-client-react-helpers": "^0.0.4",
"@automattic/explat-client": "^0.0.5",
"@automattic/explat-client-react-helpers": "^0.0.6",
"@wordpress/api-fetch": "wp-6.0",
"@wordpress/hooks": "wp-6.0",
"cookie": "^0.4.2",

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Bumped explat-client and explat-client-react-helper versions

View File

@ -41,8 +41,8 @@
},
"dependencies": {
"@automattic/components": "^2.0.1",
"@automattic/explat-client": "^0.0.3",
"@automattic/explat-client-react-helpers": "^0.0.4",
"@automattic/explat-client": "^0.0.5",
"@automattic/explat-client-react-helpers": "^0.0.6",
"@automattic/interpolate-components": "^1.2.1",
"@react-spring/web": "^9.7.3",
"@types/wordpress__blocks": "11.0.7",

View File

@ -0,0 +1,5 @@
Significance: patch
Type: dev
Comment: Adjusted explat experiment name validation to match server side. Also made it fail more prominently in non-production environments.

View File

@ -121,6 +121,7 @@ final class Experimental_Abtest {
*
* @param string $test_name Name of the A/B test.
* @return mixed|null A/B test variation, or null on failure.
* @throws \Exception If there is an error retrieving the variation and the environment is not production.
*/
public function get_variation( $test_name ) {
// Default to the control variation when users haven't consented to tracking.
@ -131,7 +132,11 @@ final class Experimental_Abtest {
$variation = $this->fetch_variation( $test_name );
// If there was an error retrieving a variation, conceal the error for the consumer.
// If there was an error retrieving a variation, throw an exception in non-production environments.
if ( is_wp_error( $variation ) ) {
if ( 'production' !== wp_get_environment_type() ) {
throw new \Exception( $variation->get_error_message() );
}
return 'control';
}
@ -194,7 +199,7 @@ final class Experimental_Abtest {
}
// Make sure test name is a valid one.
if ( ! preg_match( '/^[A-Za-z0-9_]+$/', $test_name ) ) {
if ( ! preg_match( '/^[a-z0-9_]+$/', $test_name ) ) {
return new \WP_Error( 'invalid_test_name', 'Invalid A/B test name.' );
}

View File

@ -103,4 +103,23 @@ class Experimental_Abtest_Test extends WC_Unit_Test_Case {
true
);
}
/**
* Test get_variation with valid experiment name.
*/
public function test_fetch_variation_with_valid_name() {
$exp = new Experimental_Abtest( 'anon', 'platform', true );
$variation = $exp->get_variation( 'valid_experiment_name_2' );
$this->assertNotInstanceOf( 'WP_Error', $variation );
}
/**
* Test get_variation with invalid experiment name.
*/
public function test_fetch_variation_with_invalid_name() {
$exp = new Experimental_Abtest( 'anon', 'platform', true );
$this->expectException( 'Exception' );
$this->expectExceptionMessage( 'Invalid A/B test name.' );
$exp->get_variation( 'Invalid-Experiment-Name!' );
}
}

View File

@ -1854,11 +1854,11 @@ importers:
packages/js/explat:
dependencies:
'@automattic/explat-client':
specifier: ^0.0.3
version: 0.0.3
specifier: ^0.0.5
version: 0.0.5
'@automattic/explat-client-react-helpers':
specifier: ^0.0.4
version: 0.0.4
specifier: ^0.0.6
version: 0.0.6
'@wordpress/api-fetch':
specifier: wp-6.0
version: 6.3.1
@ -3147,11 +3147,11 @@ importers:
specifier: ^2.0.1
version: 2.0.1(@types/react@17.0.71)(@wordpress/data@6.6.1)(react-dom@17.0.2)(react@17.0.2)
'@automattic/explat-client':
specifier: ^0.0.3
version: 0.0.3
specifier: ^0.0.5
version: 0.0.5
'@automattic/explat-client-react-helpers':
specifier: ^0.0.4
version: 0.0.4
specifier: ^0.0.6
version: 0.0.6
'@automattic/interpolate-components':
specifier: ^1.2.1
version: 1.2.1(@types/react@17.0.71)(react@17.0.2)
@ -5173,16 +5173,16 @@ packages:
resolution: {integrity: sha512-g+6cYJCi0m9OHU+E4146og8piMY/gtB7iTzQu1NsOkcLzeLjdAdIk0w+jNf+DE6XYnbgUOSnnoTw/iDbYNKwZw==}
dev: false
/@automattic/explat-client-react-helpers@0.0.4:
resolution: {integrity: sha512-rS9cVNvWa54oZijIONk0pO/6/xdBBzDtETnRDDAyBQwbVNKlgQ1Ueu7bfGWPq9MXcR0pJ/dfSSlMcNIgR06Hnw==}
/@automattic/explat-client-react-helpers@0.0.6:
resolution: {integrity: sha512-rYVVi1C2SG/SYnQaESIwdBkou2tkS4aqk4rWx3H6JiTzXa3pyRPEcNG0u+Y/J56AQ8AUUKIGcBon6Rv5lhh3lw==}
dependencies:
'@automattic/explat-client': 0.0.3
'@automattic/explat-client': 0.0.5
react: 17.0.2
tslib: 2.6.2
dev: false
/@automattic/explat-client@0.0.3:
resolution: {integrity: sha512-N2/H9l3JZLZLSIyZJMnHKUWZWFjeakU40vm3k3EHdCHdKh8pu2Mz/BrMbtWImYBzaEJnbUZrHM/fAuhFy4sORg==}
/@automattic/explat-client@0.0.5:
resolution: {integrity: sha512-ql/d7qQ9q2J7K5g9LGd6mDOw8BOhdb05cajWU71Y6KJpvRD8h/zTNGNb+mN9JulNufT+7LxxxF2Kpzui4CFJqw==}
dependencies:
tslib: 2.6.2
dev: false
@ -33617,6 +33617,7 @@ packages:
graceful-fs: 4.2.11
jsonfile: 6.1.0
universalify: 2.0.1
dev: true
/fs-extra@11.1.1:
resolution: {integrity: sha512-MGIE4HOvQCeUCzmlHs0vXpih4ysz4wg9qiSAu6cd42lVwPbTM1TjV7RusoyQqMmk/95gdQZX72u+YW+c3eEpFQ==}
@ -45204,7 +45205,6 @@ packages:
prop-types: 15.8.1
react: 17.0.2
scheduler: 0.19.1
bundledDependencies: false
/react-dom@17.0.2(react@17.0.2):
resolution: {integrity: sha512-s4h96KtLDUQlsENhMn1ar8t2bEa+q/YAtj8pPPdIjPDGBDIVNsrD9aXNWqspUe6AzKCIG0C1HZZLqLV7qpOBGA==}
@ -45737,7 +45737,6 @@ packages:
react-is: 16.13.1
scheduler: 0.19.1
dev: true
bundledDependencies: false
/react-test-renderer@17.0.2(react@17.0.2):
resolution: {integrity: sha512-yaQ9cB89c17PUb0x6UfWRs7kQCorVdHlutU1boVPEsB8IDZH6n9tHxMacc3y0JoXOJUsZb/t/Mb8FUWMKaM7iQ==}