Skip to content

Commit 4f2779a

Browse files
committed
Add tests to ensure the REST API stores the new XPath format
1 parent 2922d4c commit 4f2779a

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

plugins/optimization-detective/tests/storage/test-class-od-url-metrics-post-type.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,16 @@ public function data_provider_test_get_url_metrics_from_post(): array {
9595
'height' => 480,
9696
),
9797
'timestamp' => microtime( true ),
98-
'elements' => array(),
98+
'elements' => array(
99+
array(
100+
'xpath' => '/HTML/BODY/DIV[@id=\'page\']/*[1][self::IMG]',
101+
'isLCP' => false,
102+
'isLCPCandidate' => true,
103+
'intersectionRatio' => 0.123,
104+
'intersectionRect' => $this->get_sample_dom_rect(),
105+
'boundingClientRect' => $this->get_sample_dom_rect(),
106+
),
107+
),
99108
),
100109
);
101110

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ function ( OD_URL_Metric_Store_Request_Context $context ) use ( &$stored_context
115115
$this->assertCount( 1, $url_metrics, 'Expected number of URL Metrics stored.' );
116116
$this->assertSame( $valid_params['elements'], $this->get_array_json_data( $url_metrics[0]->get( 'elements' ) ) );
117117
$this->assertSame( $valid_params['viewport']['width'], $url_metrics[0]->get_viewport_width() );
118+
$element = $url_metrics[0]->get( 'elements' )[0];
119+
$this->assertStringStartsWith( '/HTML/BODY/DIV[@id=\'page\']/', $element->jsonSerialize()['xpath'] );
120+
$this->assertStringStartsWith( '/HTML/BODY/DIV/', $element->get_xpath() ); // TODO: Remove once the XPath transitional period is over.
118121

119122
$expected_data = $valid_params;
120123
unset( $expected_data['hmac'], $expected_data['slug'], $expected_data['current_etag'], $expected_data['cache_purge_post_id'] );
@@ -678,7 +681,7 @@ private function get_valid_params( array $extras = array() ): array {
678681
array(
679682
'viewport_width' => 480,
680683
'element' => array(
681-
'xpath' => '/HTML/BODY/DIV/*[2][self::MAIN]/*[1][self::DIV]/*[1][self::FIGURE]/*[1][self::IMG]',
684+
'xpath' => '/HTML/BODY/DIV[@id=\'page\']/*[2][self::MAIN]/*[1][self::DIV]/*[1][self::FIGURE]/*[1][self::IMG]',
682685
),
683686
)
684687
)->jsonSerialize();

0 commit comments

Comments
 (0)