-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve plugin compatibility with HealthCheck - PageCache
- Loading branch information
1 parent
6f7824a
commit 72756c7
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) { | ||
$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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tests/Fixtures/inc/Engine/HealthCheck/PageCache/userAgent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |