Skip to content

Commit

Permalink
Ensure update_post method handles empty URL Metric group collection
Browse files Browse the repository at this point in the history
Signed-off-by: Shyamsundar Gadde <[email protected]>
  • Loading branch information
ShyamGadde committed Feb 13, 2025
1 parent aaf48f6 commit 2182053
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ static function ( $url_metric_data ) use ( $trigger_error ) {
*/
public static function update_post( string $slug, OD_URL_Metric_Group_Collection $url_metric_group_collection ) {
$url_metrics = $url_metric_group_collection->get_flattened_url_metrics();
if ( 0 === count( $url_metrics ) ) {
return new WP_Error( 'no_url_metrics', __( 'No URL Metrics in the group collection.', 'optimization-detective' ) );
}

// Sort URL Metrics in descending order by timestamp.
usort(
$url_metrics,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ public function test_update_post(): void {
od_get_url_metric_freshness_ttl()
);

// Test with empty URL Metric group collection.
$result = OD_URL_Metrics_Post_Type::update_post( $slug, $url_metric_group_collection );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertSame( 'no_url_metrics', $result->get_error_code() );

$validated_url_metric = $this->get_sample_url_metric(
array(
'url' => home_url( '/' ),
Expand Down

0 comments on commit 2182053

Please sign in to comment.