Skip to content

Commit

Permalink
Merge changes from upstream master and resolved conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
tkalimullin-coludlinux authored and zetrider committed Jun 13, 2024
2 parents e67e769 + 72756c7 commit 62d3593
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
37 changes: 37 additions & 0 deletions inc/Engine/HealthCheck/PageCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace WP_Rocket\Engine\HealthCheck;

use WP_Rocket\Event_Management\Subscriber_Interface;

class PageCache implements Subscriber_Interface {
/**
* Returns an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'http_headers_useragent' => [ 'page_cache_useragent', 10, 2 ],
];
}

/**
* Pass plugin header to skip test "mandatory cookie".
*
* @param string $user_agent WordPress user agent string.
* @param string $url The request URL.
* @return string
*/
public function page_cache_useragent( $user_agent, $url = null ) {

Check warning on line 26 in inc/Engine/HealthCheck/PageCache.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/HealthCheck/PageCache.php#L26

Avoid unused parameters such as '$url'.
$uri = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) );
if (
strpos( $uri, 'wp-site-health' ) !== false &&
strpos( $uri, 'page-cache' ) !== false
) {
$user_agent = 'WP Rocket';
}

return $user_agent;
}
}
3 changes: 3 additions & 0 deletions inc/Engine/HealthCheck/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ServiceProvider extends AbstractServiceProvider {
*/
protected $provides = [
'health_check',
'health_check_page_cache',
'action_scheduler_check',
];

Expand All @@ -37,6 +38,8 @@ public function register(): void {
$this->getContainer()->addShared( 'health_check', HealthCheck::class )
->addArgument( $this->getContainer()->get( 'options' ) )
->addTag( 'admin_subscriber' );
$this->getContainer()->addShared( 'health_check_page_cache', PageCache::class )
->addTag( 'common_subscriber' );
$this->getContainer()->addShared( 'action_scheduler_check', ActionSchedulerCheck::class )
->addTag( 'common_subscriber' );
}
Expand Down
1 change: 1 addition & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ private function init_common_subscribers() {
'atf_cron_subscriber',
'saas_admin_subscriber',
'warmup_subscriber',
'health_check_page_cache',
];

$host_type = HostResolver::get_host_service();
Expand Down
19 changes: 19 additions & 0 deletions tests/Fixtures/inc/Engine/HealthCheck/PageCache/userAgent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

return [
'null' => [
'request_uri' => null,
'user_agent_default' => 'WordPress',
'user_agent_expected' => 'WordPress',
],
'default' => [
'request_uri' => '/wp-json/wp-site-health/v1/tests/https-status?_locale=user',
'user_agent_default' => 'WordPress',
'user_agent_expected' => 'WordPress',
],
'plugin' => [
'request_uri' => '/wp-json/wp-site-health/v1/tests/page-cache?_locale=user',
'user_agent_default' => 'WP Rocket',
'user_agent_expected' => 'WP Rocket',
],
];
47 changes: 47 additions & 0 deletions tests/Unit/inc/Engine/HealthCheck/PageCache/userAgent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace WP_Rocket\Tests\Unit\inc\Engine\HealthCheck\PageCache;

use Brain\Monkey\Functions;
use Mockery;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Admin\Settings\Page;
use WP_Rocket\Engine\HealthCheck\PageCache;
use WP_Rocket\Tests\Unit\TestCase;

/**
* @covers \WP_Rocket\Engine\HealthCheck\PageCache::page_cache_useragent
*
* @group HealthCheck
*/
class Test_UserAgent extends TestCase {
private $health;

public function setUp(): void {
parent::setUp();

$this->health = new PageCache();

Functions\when( 'sanitize_text_field' )->alias(
function ( $value ) {
return $value;
}
);

Functions\when( 'wp_unslash' )->alias(
function ( $value ) {
return stripslashes( $value );
}
);
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnExpected( $request_uri, $user_agent_default, $user_agent_expected ) {
$_SERVER['REQUEST_URI'] = $request_uri;

$user_agent = $this->health->page_cache_useragent( $user_agent_default );
$this->assertSame( $user_agent_expected, $user_agent );
}
}

0 comments on commit 62d3593

Please sign in to comment.