Skip to content

Commit

Permalink
Merge pull request #1411 from WordPress/fix/od-schema
Browse files Browse the repository at this point in the history
Implement full support for intersectionRect/boundingClientRect, fix viewportRect typing, and harden JSON schema
  • Loading branch information
westonruter authored Jul 30, 2024
2 parents ea02d4a + 7399131 commit e34711d
Show file tree
Hide file tree
Showing 23 changed files with 503 additions and 422 deletions.
5 changes: 3 additions & 2 deletions plugins/auto-sizes/tests/test-optimization-detective.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Tests for auto-sizes plugin's optimization-detective.php.
*
* @package auto-sizes
*
* @noinspection PhpUnhandledExceptionInspection
*/

class Test_Auto_Sizes_Optimization_Detective extends WP_UnitTestCase {
Expand Down Expand Up @@ -100,8 +102,7 @@ public function data_provider_test_od_optimize_template_output_buffer(): array {
* @covers ::auto_sizes_visit_tag
*
* @dataProvider data_provider_test_od_optimize_template_output_buffer
* @throws Exception But it won't.
* @phpstan-param array<string, mixed> $element_metrics
* @phpstan-param array{ xpath: string, isLCP: bool, intersectionRatio: int } $element_metrics
*/
public function test_od_optimize_template_output_buffer( array $element_metrics, string $buffer, string $expected ): void {
$this->populate_url_metrics( array( $element_metrics ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Tests for embed-optimizer plugin hooks.php.
*
* @package embed-optimizer
*
* @noinspection PhpUnhandledExceptionInspection
*/

class Test_Embed_Optimizer_Optimization_Detective extends WP_UnitTestCase {
Expand Down Expand Up @@ -51,7 +53,6 @@ public function data_provider_test_od_optimize_template_output_buffer(): array {
* @covers ::embed_optimizer_update_markup
*
* @dataProvider data_provider_test_od_optimize_template_output_buffer
* @throws Exception But it won't.
*/
public function test_od_optimize_template_output_buffer( Closure $set_up, string $buffer, string $expected ): void {
$set_up( $this );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,34 @@
for ( $i = 0; $i < $sample_size; $i++ ) {
OD_URL_Metrics_Post_Type::store_url_metric(
$slug,
$test_case->get_validated_url_metric(
$viewport_width,
$test_case->get_sample_url_metric(
array(
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
'isLCP' => true,
),
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[3][self::IMG]',
'isLCP' => false,
'intersectionRatio' => 0 === $i ? 0.5 : 0.0, // Make sure that the _max_ intersection ratio is considered.
),
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[5][self::IMG]',
'isLCP' => false,
'intersectionRatio' => 0.0,
),
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[6][self::IMG]',
'isLCP' => false,
'intersectionRatio' => 0.0,
),
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[7][self::IMG]',
'isLCP' => false,
'intersectionRatio' => 0.0,
'viewport_width' => $viewport_width,
'elements' => array(
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
'isLCP' => true,
),
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[3][self::IMG]',
'isLCP' => false,
'intersectionRatio' => 0 === $i ? 0.5 : 0.0, // Make sure that the _max_ intersection ratio is considered.
),
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[5][self::IMG]',
'isLCP' => false,
'intersectionRatio' => 0.0,
),
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[6][self::IMG]',
'isLCP' => false,
'intersectionRatio' => 0.0,
),
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[7][self::IMG]',
'isLCP' => false,
'intersectionRatio' => 0.0,
),
),
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
for ( $i = 0; $i < $sample_size; $i++ ) {
OD_URL_Metrics_Post_Type::store_url_metric(
$slug,
$test_case->get_validated_url_metric(
1000,
$test_case->get_sample_url_metric(
array(
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
'isLCP' => true,
),
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
'isLCP' => false,
'viewport_width' => 1000,
'elements' => array(
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
'isLCP' => true,
),
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
'isLCP' => false,
),
),
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,40 @@
'set_up' => static function ( Test_Image_Prioritizer_Helper $test_case ): void {
OD_URL_Metrics_Post_Type::store_url_metric(
od_get_url_metrics_slug( od_get_normalized_query_vars() ),
$test_case->get_validated_url_metric(
400,
$test_case->get_sample_url_metric(
array(
array(
'isLCP' => true,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
'intersectionRatio' => 1.0,
),
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
'intersectionRatio' => 0.0,
'viewport_width' => 400,
'elements' => array(
array(
'isLCP' => true,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
'intersectionRatio' => 1.0,
),
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
'intersectionRatio' => 0.0,
),
),
)
)
);
OD_URL_Metrics_Post_Type::store_url_metric(
od_get_url_metrics_slug( od_get_normalized_query_vars() ),
$test_case->get_validated_url_metric(
800,
$test_case->get_sample_url_metric(
array(
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
'intersectionRatio' => 0.0,
),
array(
'isLCP' => true,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
'intersectionRatio' => 1.0,
'viewport_width' => 800,
'elements' => array(
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
'intersectionRatio' => 0.0,
),
array(
'isLCP' => true,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
'intersectionRatio' => 1.0,
),
),
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,72 @@ static function () {

OD_URL_Metrics_Post_Type::store_url_metric(
od_get_url_metrics_slug( od_get_normalized_query_vars() ),
$test_case->get_validated_url_metric(
500,
$test_case->get_sample_url_metric(
array(
array(
'isLCP' => true,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
),
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
'viewport_width' => 500,
'elements' => array(
array(
'isLCP' => true,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
),
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
),
),
)
)
);
OD_URL_Metrics_Post_Type::store_url_metric(
od_get_url_metrics_slug( od_get_normalized_query_vars() ),
$test_case->get_validated_url_metric(
650,
$test_case->get_sample_url_metric(
array(
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
),
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
'viewport_width' => 650,
'elements' => array(
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
),
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
),
),
)
)
);
OD_URL_Metrics_Post_Type::store_url_metric(
od_get_url_metrics_slug( od_get_normalized_query_vars() ),
$test_case->get_validated_url_metric(
800,
$test_case->get_sample_url_metric(
array(
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
),
array(
'isLCP' => true,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
'viewport_width' => 800,
'elements' => array(
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
),
array(
'isLCP' => true,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
),
),
)
)
);
OD_URL_Metrics_Post_Type::store_url_metric(
od_get_url_metrics_slug( od_get_normalized_query_vars() ),
$test_case->get_validated_url_metric(
800,
$test_case->get_sample_url_metric(
array(
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
),
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
'viewport_width' => 800,
'elements' => array(
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
),
array(
'isLCP' => false,
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
),
),
)
)
Expand Down
Loading

0 comments on commit e34711d

Please sign in to comment.