Skip to content

Commit 746a410

Browse files
committed
Use maximum instead of max in PHP; add test coverage
1 parent ba383c6 commit 746a410

File tree

4 files changed

+63
-31
lines changed

4 files changed

+63
-31
lines changed

plugins/optimization-detective/detection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static function ( OD_URL_Metric_Group $group ): array {
141141
'freshnessTTL' => od_get_url_metric_freshness_ttl(),
142142
'webVitalsLibrarySrc' => $web_vitals_lib_src,
143143
'gzdecodeAvailable' => function_exists( 'gzdecode' ),
144-
'maxUrlMetricSize' => od_get_max_url_metric_size(),
144+
'maxUrlMetricSize' => od_get_maximum_url_metric_size(),
145145
);
146146
if ( is_user_logged_in() ) {
147147
$detect_args['restApiNonce'] = wp_create_nonce( 'wp_rest' );

plugins/optimization-detective/helper.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function od_get_asset_path( string $src_path, ?string $min_path = null ): string
131131
*
132132
* @return positive-int Maximum allowed byte size.
133133
*/
134-
function od_get_max_url_metric_size(): int {
134+
function od_get_maximum_url_metric_size(): int {
135135
/**
136136
* Filters the maximum allowed size in bytes for a URL Metric serialized to JSON.
137137
*
@@ -142,18 +142,18 @@ function od_get_max_url_metric_size(): int {
142142
* @param int $max_size Maximum allowed byte size.
143143
* @return int Filtered maximum allowed byte size.
144144
*/
145-
$size = (int) apply_filters( 'od_max_url_metric_size', MB_IN_BYTES );
145+
$size = (int) apply_filters( 'od_maximum_url_metric_size', MB_IN_BYTES );
146146
if ( $size <= 0 ) {
147147
_doing_it_wrong(
148-
__FUNCTION__,
148+
esc_html( "Filter: 'od_maximum_url_metric_size'" ),
149149
esc_html(
150150
sprintf(
151-
/* translators: 1: filter name, 2: size */
152-
__( 'Filter %1$s returned invalid "%2$s". Must be greater than zero.', 'optimization-detective' ),
151+
/* translators: %s: size */
152+
__( 'Invalid size "%s". Must be greater than zero.', 'optimization-detective' ),
153153
$size
154154
)
155155
),
156-
''
156+
'Optimization Detective 1.0.0'
157157
);
158158
$size = MB_IN_BYTES;
159159
}

plugins/optimization-detective/storage/rest-api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ function od_handle_rest_request( WP_REST_Request $request ) {
219219
}
220220

221221
// Limit JSON payload size to safeguard against clients sending possibly malicious payloads much larger than allowed.
222-
$max_size = od_get_max_url_metric_size();
222+
$max_size = od_get_maximum_url_metric_size();
223223
$content_length = strlen( (string) wp_json_encode( $url_metric ) );
224224
if ( $content_length > $max_size ) {
225225
return new WP_Error(

plugins/optimization-detective/tests/storage/test-rest-api.php

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -910,21 +910,25 @@ public function test_od_decompress_rest_request_body_modifies_request(): void {
910910
}
911911

912912
/**
913-
* Test that the `od_max_url_metric_size` filter can be used to modify the maximum size of URL Metrics.
913+
* Test that the `od_maximum_url_metric_size` filter can be used to modify the maximum size of URL Metrics.
914914
*
915-
* @dataProvider data_provider_max_url_metrics_size_filter
915+
* @dataProvider data_provider_maximum_url_metrics_size_filter
916916
*
917917
* @covers ::od_register_endpoint
918918
* @covers ::od_handle_rest_request
919-
* @covers ::od_get_max_url_metric_size
919+
* @covers ::od_get_maximum_url_metric_size
920920
*
921-
* @param Closure $set_up Set up function.
922-
* @param array<string, mixed> $params Params.
923-
* @param int $expected_status Expected status.
924-
* @param string|null $expected_code Expected code.
921+
* @param Closure $set_up Set up function.
922+
* @param array<string, mixed> $params Params.
923+
* @param int $expected_status Expected status.
924+
* @param string|null $expected_code Expected code.
925+
* @param bool $expected_incorrect_usage Expected incorrect usage.
925926
*/
926-
public function test_max_url_metrics_size_filter( Closure $set_up, array $params, int $expected_status, ?string $expected_code ): void {
927+
public function test_maximum_url_metrics_size_filter( Closure $set_up, array $params, int $expected_status, ?string $expected_code, bool $expected_incorrect_usage ): void {
927928
$set_up();
929+
if ( $expected_incorrect_usage ) {
930+
$this->setExpectedIncorrectUsage( 'Filter: &#039;od_maximum_url_metric_size&#039;' );
931+
}
928932
$request = $this->create_request( $params );
929933
unset( $params['hmac'], $params['slug'], $params['current_etag'], $params['cache_purge_post_id'] );
930934
$request->set_header( 'Content-Type', 'application/json' );
@@ -938,25 +942,25 @@ public function test_max_url_metrics_size_filter( Closure $set_up, array $params
938942
}
939943

940944
/**
941-
* Data provider for test_max_url_metrics_size_filter.
945+
* Data provider for test_maximum_url_metrics_size_filter.
942946
*
943947
* @return array<string, mixed> Test data.
944948
*/
945-
public function data_provider_max_url_metrics_size_filter(): array {
949+
public function data_provider_maximum_url_metrics_size_filter(): array {
946950
$valid_params = $this->get_valid_params();
947951
$valid_element = $valid_params['elements'][0];
948952

949953
return array(
950-
'url_metrics_should_be_accepted_because_of_increased_max_url_metrics_size' => array(
951-
'set_up' => static function (): void {
954+
'url_metrics_should_be_accepted_because_of_increased_maximum_url_metrics_size' => array(
955+
'set_up' => static function (): void {
952956
add_filter(
953-
'od_max_url_metric_size',
957+
'od_maximum_url_metric_size',
954958
static function (): int {
955959
return MB_IN_BYTES * 2;
956960
}
957961
);
958962
},
959-
'params' => array_merge(
963+
'params' => array_merge(
960964
$valid_params,
961965
array(
962966
// Fill the JSON with more than 1MB of data.
@@ -970,19 +974,20 @@ static function (): int {
970974
),
971975
)
972976
),
973-
'expected_status' => 200,
974-
'expected_code' => null,
977+
'expected_status' => 200,
978+
'expected_code' => null,
979+
'expected_incorrect_usage' => false,
975980
),
976-
'url_metrics_should_be_rejected_because_of_decreased_max_url_metrics_size' => array(
977-
'set_up' => static function (): void {
981+
'url_metrics_should_be_rejected_because_of_decreased_maximum_url_metrics_size' => array(
982+
'set_up' => static function (): void {
978983
add_filter(
979-
'od_max_url_metric_size',
984+
'od_maximum_url_metric_size',
980985
static function (): int {
981986
return MB_IN_BYTES / 2;
982987
}
983988
);
984989
},
985-
'params' => array_merge(
990+
'params' => array_merge(
986991
$valid_params,
987992
array(
988993
// Fill the JSON with more than 1MB of data.
@@ -996,10 +1001,37 @@ static function (): int {
9961001
),
9971002
)
9981003
),
999-
'expected_status' => 413,
1000-
'expected_code' => 'rest_content_too_large',
1004+
'expected_status' => 413,
1005+
'expected_code' => 'rest_content_too_large',
1006+
'expected_incorrect_usage' => false,
1007+
),
1008+
'negative_maximum_url_metric_size_is_treated_as_1mb' => array(
1009+
'set_up' => static function (): void {
1010+
add_filter(
1011+
'od_maximum_url_metric_size',
1012+
static function (): int {
1013+
return -1;
1014+
}
1015+
);
1016+
},
1017+
'params' => array_merge(
1018+
$valid_params,
1019+
array(
1020+
// Fill the JSON with more than 1MB of data.
1021+
'elements' => array(
1022+
array_merge(
1023+
$valid_element,
1024+
array(
1025+
'xpath' => sprintf( '/HTML/BODY/DIV[@id=\'%s\']/*[1][self::DIV]', str_repeat( 'A', MB_IN_BYTES / 2 ) ),
1026+
)
1027+
),
1028+
),
1029+
)
1030+
),
1031+
'expected_status' => 200,
1032+
'expected_code' => null,
1033+
'expected_incorrect_usage' => true,
10011034
),
1002-
10031035
);
10041036
}
10051037

0 commit comments

Comments
 (0)