Skip to content
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

Incorporate page state into ETag computation #1722

Merged
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
60740a8
Factor in IDs of posts in query for computing ETag
ShyamGadde Dec 5, 2024
2239317
Factor in last modified of posts in The Loop
ShyamGadde Dec 5, 2024
3c7842b
Merge logic to get posts in The Loop
ShyamGadde Dec 5, 2024
3c57a1d
Factor in active theme and current template
ShyamGadde Dec 5, 2024
6a5e421
Add missing `@global` tags
ShyamGadde Dec 5, 2024
cc8e252
Add null checks for global variables
ShyamGadde Dec 5, 2024
72f4e54
Add temporary fix for test cases
ShyamGadde Dec 5, 2024
8651d05
Add `queried_object` to ETag data
ShyamGadde Dec 7, 2024
f66012e
Merge branch 'trunk' into update/incorporate-page-state-for-etag
ShyamGadde Dec 7, 2024
56cbe8e
Use dependency injection instead of relying on globals
ShyamGadde Dec 9, 2024
38d0097
Update test_od_get_current_url_metrics_etag
ShyamGadde Dec 9, 2024
d61a8bc
Fix failing test cases due to missing globals
ShyamGadde Dec 9, 2024
072140b
Introduce filter `od_current_url_metrics_etag_active_theme` to improv…
ShyamGadde Dec 9, 2024
4ffb79f
Update test_od_get_current_url_metrics_etag
ShyamGadde Dec 9, 2024
1794cff
Factor in Block Template object
ShyamGadde Dec 9, 2024
3c3a1a3
Merge branch 'trunk' into update/incorporate-page-state-for-etag
ShyamGadde Dec 9, 2024
65dc803
Merge branch 'trunk' into update/incorporate-page-state-for-etag
ShyamGadde Dec 10, 2024
1c0bdac
Remove unnecessary filter `od_current_url_metrics_etag_active_theme`
ShyamGadde Dec 10, 2024
e562726
Merge branch 'trunk' into update/incorporate-page-state-for-etag
ShyamGadde Dec 12, 2024
6d80095
Merge branch 'trunk' into update/incorporate-page-state-for-etag
ShyamGadde Dec 12, 2024
73e4519
Refine `$current_template` assignment logic for block and classic themes
ShyamGadde Dec 12, 2024
59879f0
Update `$current_template` type for static analysis compliance
ShyamGadde Dec 12, 2024
df306e1
Fix indentation
ShyamGadde Dec 12, 2024
c9c0dd8
Move global setup to set_up and cleanup to tear_down
ShyamGadde Dec 12, 2024
4632350
Replace `$GLOBALS` access with explicit global variable declarations
ShyamGadde Dec 12, 2024
5b4e791
Limit queried_object fields to id, type, and last_modified
ShyamGadde Dec 12, 2024
1073780
Use WP_Query method instead of global function for determining post type
ShyamGadde Dec 12, 2024
9a19113
Add isset checks for global variables
ShyamGadde Dec 13, 2024
03d1683
Merge branch 'trunk' into update/incorporate-page-state-for-etag
westonruter Dec 13, 2024
8325cb4
Avoid sleeping in favor of setting an old initial modified time
westonruter Dec 13, 2024
1b3ba00
Reuse post_modified_gmt in queried_object
westonruter Dec 13, 2024
a71fe76
Reuse active theme in test and reduce code duplication
westonruter Dec 14, 2024
03ef8c8
Align post type archive check with other `instanceof` validations
ShyamGadde Dec 14, 2024
73edbdb
Add helper od_get_current_theme_template() function
westonruter Dec 15, 2024
55d56d3
Refactor queried_posts data to have better guaranteed shape
westonruter Dec 15, 2024
63bb613
Avoid adding WP_Block_Template object directly to ETag data in favor …
westonruter Dec 15, 2024
18e0476
Refactor od_get_current_url_metrics_etag to use data provider and add…
westonruter Dec 15, 2024
57a0ba6
Remove unnecessary setting of template global
westonruter Dec 15, 2024
804d71e
Set absolute path to template
westonruter Dec 15, 2024
8fe2f8f
Make additional adjustments to template
westonruter Dec 15, 2024
1869689
Do not rely on WP_Query global being set
westonruter Dec 15, 2024
7c0cfe1
Work around strange user creation issue in factory
westonruter Dec 15, 2024
1fc20af
Add template conditional assertions
westonruter Dec 15, 2024
b083a29
Avoid using pretty permalink for author URL due to GHA testing problem
westonruter Dec 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions plugins/embed-optimizer/tests/test-optimization-detective.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function set_up(): void {
if ( ! defined( 'OPTIMIZATION_DETECTIVE_VERSION' ) ) {
$this->markTestSkipped( 'Optimization Detective is not active.' );
}
$GLOBALS['template'] = '/path/to/theme/index.php';

// Normalize the data for computing the current URL Metrics ETag to work around the issue where there is no
// global variable storing the OD_Tag_Visitor_Registry instance along with any registered tag visitors, so
Expand All @@ -26,6 +27,14 @@ public function set_up(): void {
add_filter( 'od_current_url_metrics_etag_data', '__return_empty_array' );
}

/**
* Runs the routine after each test is executed.
*/
public function tear_down(): void {
unset( $GLOBALS['template'] );
parent::tear_down();
}

/**
* Tests embed_optimizer_register_tag_visitors().
*
Expand Down
9 changes: 9 additions & 0 deletions plugins/image-prioritizer/tests/test-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Test_Image_Prioritizer_Helper extends WP_UnitTestCase {
*/
public function set_up(): void {
parent::set_up();
$GLOBALS['template'] = '/path/to/theme/index.php';

// Normalize the data for computing the current URL Metrics ETag to work around the issue where there is no
// global variable storing the OD_Tag_Visitor_Registry instance along with any registered tag visitors, so
Expand All @@ -23,6 +24,14 @@ public function set_up(): void {
add_filter( 'od_current_url_metrics_etag_data', '__return_empty_array' );
}

/**
* Runs the routine after each test is executed.
*/
public function tear_down(): void {
unset( $GLOBALS['template'] );
parent::tear_down();
}

/**
* @return array<string, array<string, mixed>>
*/
Expand Down
16 changes: 15 additions & 1 deletion plugins/optimization-detective/optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,16 @@ function od_is_response_html_content_type(): bool {
* @since 0.1.0
* @access private
*
* @global WP_Query $wp_the_query WP_Query object.
* @global string $_wp_current_template_id Current template ID.
* @global string $template Template file path.
westonruter marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $buffer Template output buffer.
* @return string Filtered template output buffer.
*/
function od_optimize_template_output_buffer( string $buffer ): string {
global $wp_the_query, $_wp_current_template_id, $template;

// If the content-type is not HTML or the output does not start with '<', then abort since the buffer is definitely not HTML.
if (
! od_is_response_html_content_type() ||
Expand Down Expand Up @@ -206,7 +212,15 @@ function od_optimize_template_output_buffer( string $buffer ): string {
*/
do_action( 'od_register_tag_visitors', $tag_visitor_registry );

$current_etag = od_get_current_url_metrics_etag( $tag_visitor_registry );
$current_template = null;
if ( wp_is_block_theme() && isset( $_wp_current_template_id ) ) {
$current_template = get_block_template( $_wp_current_template_id, 'wp_template' );
}
if ( null === $current_template && isset( $template ) && is_string( $template ) ) {
$current_template = basename( $template );
}

$current_etag = od_get_current_url_metrics_etag( $tag_visitor_registry, $wp_the_query, $current_template );
$group_collection = new OD_URL_Metric_Group_Collection(
$post instanceof WP_Post ? OD_URL_Metrics_Post_Type::get_url_metrics_from_post( $post ) : array(),
$current_etag,
Expand Down
48 changes: 42 additions & 6 deletions plugins/optimization-detective/storage/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,55 @@ function od_get_url_metrics_slug( array $query_vars ): string {
/**
* Gets the current ETag for URL Metrics.
*
* The ETag is a hash based on the IDs of the registered tag visitors
* in the current environment. It is used for marking the URL Metrics as stale
* when its value changes.
* Generates a hash based on the IDs of registered tag visitors, the queried object,
* posts in The Loop, and theme information in the current environment. This ETag
* is used to assess if the URL Metrics are stale when its value changes.
*
* @since n.e.x.t
* @access private
*
* @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
* @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
* @param WP_Query $wp_query The WP_Query instance.
* @param string|WP_Block_Template|null $current_template The current template being used.
* @return non-empty-string Current ETag.
*/
function od_get_current_url_metrics_etag( OD_Tag_Visitor_Registry $tag_visitor_registry ): string {
function od_get_current_url_metrics_etag( OD_Tag_Visitor_Registry $tag_visitor_registry, WP_Query $wp_query, $current_template ): string {
$queried_object = $wp_query->get_queried_object();
$queried_object_data = array(
'id' => null,
'type' => null,
'last_modified' => null,
);

if ( $queried_object instanceof WP_Post ) {
$queried_object_data['id'] = $queried_object->ID;
$queried_object_data['type'] = 'post';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be the post type?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hummm. Then when WP_Term is the queried object, would the type be the taxonomy name? I think actually this isn't needed because all that is needed here is to uniquely identify the queried object with a hash of this data here. The point of the type of post is to differentiate it from a type of term since it is entirely possible for a post and term to have the same ID.

$queried_object_data['last_modified'] = $queried_object->post_modified_gmt;
} elseif ( $queried_object instanceof WP_Term ) {
$queried_object_data['id'] = $queried_object->term_id;
$queried_object_data['type'] = 'term';
} elseif ( $queried_object instanceof WP_User ) {
$queried_object_data['id'] = $queried_object->ID;
$queried_object_data['type'] = 'user';
} elseif ( $wp_query->is_post_type_archive() ) {
$queried_object_data['type'] = $wp_query->get( 'post_type' );
ShyamGadde marked this conversation as resolved.
Show resolved Hide resolved
}

$data = array(
'tag_visitors' => array_keys( iterator_to_array( $tag_visitor_registry ) ),
'tag_visitors' => array_keys( iterator_to_array( $tag_visitor_registry ) ),
'queried_object' => $queried_object_data,
'queried_posts' => wp_list_pluck( $wp_query->posts, 'post_modified_gmt', 'ID' ),
'active_theme' => array(
'template' => array(
'name' => get_template(),
'version' => wp_get_theme( get_template() )->get( 'Version' ),
),
'stylesheet' => array(
'name' => get_stylesheet(),
'version' => wp_get_theme()->get( 'Version' ),
),
),
'current_template' => $current_template,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of $current_template being a WP_Block_Template then this here would be:

{
    "type": "wp_template",
    "theme": "twentytwentythree",
    "slug": "home",
    "id": "twentytwentythree\/\/home",
    "title": "Blog Home",
    "content": "<!-- wp:template-part {\"slug\":\"header\",\"tagName\":\"header\",\"theme\":\"twentytwentythree\"} \/-->\n\n<!-- wp:group {\"tagName\":\"main\",\"style\":{\"spacing\":{\"margin\":{\"top\":\"var:preset|spacing|50\",\"bottom\":\"var:preset|spacing|70\"}}},\"layout\":{\"type\":\"constrained\"}} -->\n<main class=\"wp-block-group\" style=\"margin-top:var(--wp--preset--spacing--50);margin-bottom:var(--wp--preset--spacing--70)\">\n\t<!-- wp:pattern {\"slug\":\"twentytwentythree\/hidden-heading\"} \/-->\n\n\t<!-- wp:query {\"query\":{\"pages\":0,\"offset\":0,\"postType\":\"post\",\"order\":\"desc\",\"orderBy\":\"date\",\"author\":\"\",\"search\":\"\",\"exclude\":[],\"sticky\":\"\",\"inherit\":true},\"displayLayout\":{\"type\":\"flex\",\"columns\":3},\"align\":\"wide\",\"layout\":{\"type\":\"constrained\"}} -->\n\t<div class=\"wp-block-query alignwide\">\n\t\t<!-- wp:post-template {\"align\":\"wide\"} -->\n\t\t\t<!-- wp:post-featured-image {\"isLink\":true,\"width\":\"100%\",\"height\":\"clamp(15vw, 30vh, 400px)\",\"align\":\"wide\"} \/-->\n\t\t\t<!-- wp:post-title {\"isLink\":true} \/-->\n\t\t\t<!-- wp:post-excerpt \/-->\n\t\t\t<!-- wp:post-date {\"isLink\":true} \/-->\n\n\t\t\t<!-- wp:spacer {\"height\":\"var(\\u002d\\u002dwp\\u002d\\u002dpreset\\u002d\\u002dspacing\\u002d\\u002d40)\"} -->\n\t\t\t<div style=\"height:var(--wp--preset--spacing--40)\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\t\t\t<!-- \/wp:spacer -->\n\t\t<!-- \/wp:post-template -->\n\n\t\t<!-- wp:query-pagination {\"paginationArrow\":\"arrow\",\"align\":\"wide\",\"layout\":{\"type\":\"flex\",\"justifyContent\":\"space-between\"}} -->\n\t\t\t<!-- wp:query-pagination-previous \/-->\n\t\t\t<!-- wp:query-pagination-next \/-->\n\t\t<!-- \/wp:query-pagination -->\n\t<\/div>\n\t<!-- \/wp:query -->\n\n\t<!-- wp:spacer {\"height\":\"var(\\u002d\\u002dwp\\u002d\\u002dpreset\\u002d\\u002dspacing\\u002d\\u002d60)\"} -->\n\t<div style=\"height:var(--wp--preset--spacing--60)\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\t<!-- \/wp:spacer -->\n\n\t<!-- wp:pattern {\"slug\":\"twentytwentythree\/cta\"} \/-->\n<\/main>\n<!-- \/wp:group -->\n\n<!-- wp:template-part {\"slug\":\"footer\",\"tagName\":\"footer\",\"theme\":\"twentytwentythree\"} \/-->\n",
    "description": "Displays the latest posts as either the site homepage or as the \"Posts page\" as defined under reading settings. If it exists, the Front Page template overrides this template when posts are shown on the homepage.",
    "source": "theme",
    "origin": null,
    "wp_id": null,
    "status": "publish",
    "has_theme_file": true,
    "is_custom": false,
    "author": null,
    "plugin": null,
    "post_types": null,
    "area": null,
    "modified": null
}

That is expected?

Copy link
Contributor Author

@ShyamGadde ShyamGadde Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The primary goal here is to factor in the modified date if it’s present. Would it be better to have a separate key specifically for this, rather than including the entire object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, for the queried_object, should we account for the entire object, or would just the ID and modified date (if available) suffice? I believe the modified date is only available for WP_Post objects, so for other types like WP_Term or WP_User, this may not apply.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it doesn't hurt to have the entire template object included here. In particular, by including the content it ensures that whenever the template content changes the URL Metrics will become stale.

For the queried_object on the other hand, I think it would be better to limit the fields to: id, type, and last_modified. The last_modified would only be applicable to posts. In the cast of a post type archive, the id could be null as well, with the type then being not post, term, or user but rather the slug of the post type itself.

);

/**
Expand Down
166 changes: 154 additions & 12 deletions plugins/optimization-detective/tests/storage/test-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ public function test_od_get_url_metrics_slug(): void {
*/
public function test_od_get_current_url_metrics_etag(): void {
remove_all_filters( 'od_current_url_metrics_etag_data' );
$registry = new OD_Tag_Visitor_Registry();
$post_ids = self::factory()->post->create_many( 3 );
$registry = new OD_Tag_Visitor_Registry();
$wp_the_query = new WP_Query();
$current_template = 'index.php';

$captured_etag_data = array();
add_filter(
Expand All @@ -304,35 +307,174 @@ static function ( array $data ) use ( &$captured_etag_data ) {
},
PHP_INT_MAX
);
$etag1 = od_get_current_url_metrics_etag( $registry );
add_filter(
'od_current_url_metrics_etag_data',
static function ( $data ) {
$data['active_theme'] = array(
'template' => array(
'name' => 'od-theme',
'version' => '1.0.0',
),
'stylesheet' => array(
'name' => 'od-theme',
'version' => '1.0.0',
),
);
return $data;
}
);

$wp_the_query->query( array() );
$etag1 = od_get_current_url_metrics_etag( $registry, $wp_the_query, $current_template );
$this->assertMatchesRegularExpression( '/^[a-z0-9]{32}\z/', $etag1 );
$etag2 = od_get_current_url_metrics_etag( $registry );

$etag2 = od_get_current_url_metrics_etag( $registry, $wp_the_query, $current_template );
$this->assertSame( $etag1, $etag2 );
$this->assertCount( 2, $captured_etag_data );
$this->assertSame( array( 'tag_visitors' => array() ), $captured_etag_data[0] );
$this->assertSame(
array(
'tag_visitors' => array(),
'queried_object' => array(
'id' => null,
'type' => null,
'last_modified' => null,
),
'queried_posts' => wp_list_pluck( $wp_the_query->posts, 'post_modified_gmt', 'ID' ),
'active_theme' => array(
'template' => array(
'name' => 'od-theme',
'version' => '1.0.0',
),
'stylesheet' => array(
'name' => 'od-theme',
'version' => '1.0.0',
),
),
'current_template' => 'index.php',
),
$captured_etag_data[0]
);
$this->assertSame( $captured_etag_data[ count( $captured_etag_data ) - 2 ], $captured_etag_data[ count( $captured_etag_data ) - 1 ] );

// Add new post.
$post_ids[] = self::factory()->post->create();
$wp_the_query->query( array() );
$etag3 = od_get_current_url_metrics_etag( $registry, $wp_the_query, $current_template );
$this->assertNotEquals( $etag2, $etag3 ); // Etag should change.
$this->assertNotEquals( $captured_etag_data[ count( $captured_etag_data ) - 2 ], $captured_etag_data[ count( $captured_etag_data ) - 1 ] );

// Update the post content.
usleep( 1000000 ); // Sleep for 1 second to ensure the post_modified_gmt is different from the previous value.
wp_update_post(
array(
'ID' => $post_ids[0],
'post_content' => 'Updated post content',
)
);
$wp_the_query->query( array() );
$etag4 = od_get_current_url_metrics_etag( $registry, $wp_the_query, $current_template );
$this->assertNotEquals( $etag3, $etag4 );
$this->assertNotEquals( $captured_etag_data[ count( $captured_etag_data ) - 2 ], $captured_etag_data[ count( $captured_etag_data ) - 1 ] );

// Register new tag visitors.
$registry->register( 'foo', static function (): void {} );
$registry->register( 'bar', static function (): void {} );
$registry->register( 'baz', static function (): void {} );
$etag3 = od_get_current_url_metrics_etag( $registry );
$this->assertNotEquals( $etag2, $etag3 );
$etag5 = od_get_current_url_metrics_etag( $registry, $wp_the_query, $current_template );
$this->assertNotEquals( $etag4, $etag5 );
$this->assertNotEquals( $captured_etag_data[ count( $captured_etag_data ) - 2 ], $captured_etag_data[ count( $captured_etag_data ) - 1 ] );
$this->assertSame( array( 'tag_visitors' => array( 'foo', 'bar', 'baz' ) ), $captured_etag_data[ count( $captured_etag_data ) - 1 ] );
$this->assertSame(
array(
'tag_visitors' => array( 'foo', 'bar', 'baz' ),
'queried_object' => array(
'id' => null,
'type' => null,
'last_modified' => null,
),
'queried_posts' => wp_list_pluck( $wp_the_query->posts, 'post_modified_gmt', 'ID' ),
'active_theme' => array(
'template' => array(
'name' => 'od-theme',
'version' => '1.0.0',
),
'stylesheet' => array(
'name' => 'od-theme',
'version' => '1.0.0',
),
),
'current_template' => 'index.php',
),
$captured_etag_data[ count( $captured_etag_data ) - 1 ]
);

// Modify data using filters.
add_filter(
'od_current_url_metrics_etag_data',
static function ( $data ) {
$data['active_theme']['template']['version'] = '2.0.0';
$data['active_theme']['stylesheet']['version'] = '2.0.0';
return $data;
}
);
$etag6 = od_get_current_url_metrics_etag( $registry, $wp_the_query, $current_template );
$this->assertNotEquals( $etag5, $etag6 );
$this->assertNotEquals( $captured_etag_data[ count( $captured_etag_data ) - 2 ], $captured_etag_data[ count( $captured_etag_data ) - 1 ] );
$this->assertSame(
array(
'tag_visitors' => array( 'foo', 'bar', 'baz' ),
'queried_object' => array(
'id' => null,
'type' => null,
'last_modified' => null,
),
'queried_posts' => wp_list_pluck( $wp_the_query->posts, 'post_modified_gmt', 'ID' ),
'active_theme' => array(
'template' => array(
'name' => 'od-theme',
'version' => '2.0.0',
),
'stylesheet' => array(
'name' => 'od-theme',
'version' => '2.0.0',
),
),
'current_template' => 'index.php',
),
$captured_etag_data[ count( $captured_etag_data ) - 1 ]
);

add_filter(
'od_current_url_metrics_etag_data',
static function ( $data ): array {
$data['last_modified'] = '2024-03-02T01:00:00';
$data['queried_object'] = array(
'ID' => 99,
'last_modified' => '2024-03-02T01:00:00',
);
return $data;
}
);
$etag4 = od_get_current_url_metrics_etag( $registry );
$this->assertNotEquals( $etag3, $etag4 );
$etag7 = od_get_current_url_metrics_etag( $registry, $wp_the_query, $current_template );
$this->assertNotEquals( $etag6, $etag7 );
$this->assertNotEquals( $captured_etag_data[ count( $captured_etag_data ) - 2 ], $captured_etag_data[ count( $captured_etag_data ) - 1 ] );
$this->assertSame(
array(
'tag_visitors' => array( 'foo', 'bar', 'baz' ),
'last_modified' => '2024-03-02T01:00:00',
'tag_visitors' => array( 'foo', 'bar', 'baz' ),
'queried_object' => array(
'ID' => 99,
'last_modified' => '2024-03-02T01:00:00',
),
'queried_posts' => wp_list_pluck( $wp_the_query->posts, 'post_modified_gmt', 'ID' ),
'active_theme' => array(
'template' => array(
'name' => 'od-theme',
'version' => '2.0.0',
),
'stylesheet' => array(
'name' => 'od-theme',
'version' => '2.0.0',
),
),
'current_template' => 'index.php',
),
$captured_etag_data[ count( $captured_etag_data ) - 1 ]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ class Test_OD_Optimization extends WP_UnitTestCase {
private $default_mimetype;

public function set_up(): void {
parent::set_up();
$this->original_request_uri = $_SERVER['REQUEST_URI'];
$this->original_request_method = $_SERVER['REQUEST_METHOD'];
$this->default_mimetype = (string) ini_get( 'default_mimetype' );
parent::set_up();
$GLOBALS['template'] = '/path/to/theme/index.php';
}

public function tear_down(): void {
$_SERVER['REQUEST_URI'] = $this->original_request_uri;
$_SERVER['REQUEST_METHOD'] = $this->original_request_method;
ini_set( 'default_mimetype', $this->default_mimetype ); // phpcs:ignore WordPress.PHP.IniSet.Risky
unset( $GLOBALS['wp_customize'] );
unset( $GLOBALS['template'] );
parent::tear_down();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/class-optimization-detective-test-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait Optimization_Detective_Test_Helpers {
*/
public function populate_url_metrics( array $elements, bool $complete = true ): void {
$slug = od_get_url_metrics_slug( od_get_normalized_query_vars() );
$etag = od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() ); // Note: Tests rely on the od_current_url_metrics_etag_data filter to set the desired value.
$etag = od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry(), new WP_Query(), 'index.php' ); // Note: Tests rely on the od_current_url_metrics_etag_data filter to set the desired value.
$sample_size = $complete ? od_get_url_metrics_breakpoint_sample_size() : 1;
foreach ( array_merge( od_get_breakpoint_max_widths(), array( 1000 ) ) as $viewport_width ) {
for ( $i = 0; $i < $sample_size; $i++ ) {
Expand Down Expand Up @@ -81,7 +81,7 @@ public function get_sample_dom_rect(): array {
public function get_sample_url_metric( array $params ): OD_URL_Metric {
$params = array_merge(
array(
'etag' => od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() ), // Note: Tests rely on the od_current_url_metrics_etag_data filter to set the desired value.
'etag' => od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry(), new WP_Query(), 'index.php' ), // Note: Tests rely on the od_current_url_metrics_etag_data filter to set the desired value.
'url' => home_url( '/' ),
'viewport_width' => 480,
'elements' => array(),
Expand Down
Loading