Skip to content

Commit da08f1b

Browse files
committed
Improve CDN and lazy load test coverage
Add PHPUnit coverage for CDN URL rewriting and lazy load HTML transforms. Improve test bootstrap constants, ensure utility tests initialize the base test case, and fix CDN extension matching plus local-site handling under mocked site URLs.
1 parent 30ff965 commit da08f1b

6 files changed

Lines changed: 191 additions & 2 deletions

File tree

includes/classes/CDN.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public static function rewriter( $contents ) {
155155
return $contents;
156156
}
157157

158-
$included_file_extensions_regex = quotemeta( implode( '|', self::get_file_extensions() ) );
158+
$included_file_extensions_regex = implode( '|', array_map( 'preg_quote', self::get_file_extensions() ) );
159159
$urls_regex = '#(?:(?:[\"\'\s=>,;]|url\()\K|^)[^\"\'\s(=>,;]+(' . $included_file_extensions_regex . ')(\?[^\/?\\\"\'\s)>,]+)?(?:(?=\/?[?\\\"\'\s)>,&])|$)#i';
160160
$rewritten_contents = preg_replace_callback( $urls_regex, [ '\PoweredCache\CDN', 'rewrite_url' ], $contents );
161161

@@ -401,4 +401,3 @@ function ( $ext ) {
401401
}
402402

403403
}
404-

includes/utils.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,9 @@ function powered_cache_is_mobile() {
14091409
*/
14101410
function is_local_site() {
14111411
$site_url = site_url();
1412+
if ( ! is_string( $site_url ) ) {
1413+
$site_url = '';
1414+
}
14121415

14131416
// Check for localhost and sites using an IP only first.
14141417
$is_local = $site_url && false === strpos( $site_url, '.' );

tests/bootstrap.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
define( 'PROJECT', __DIR__ . '/../includes/' );
44
}
55

6+
if ( ! defined( 'ABSPATH' ) ) {
7+
define( 'ABSPATH', dirname( __DIR__ ) . '/' );
8+
}
9+
10+
if ( ! defined( 'WP_CONTENT_DIR' ) ) {
11+
define( 'WP_CONTENT_DIR', sys_get_temp_dir() . '/powered-cache-wp-content' );
12+
}
13+
614
if ( ! defined( 'POWERED_CACHE_DIR' ) ) {
715
define( 'POWERED_CACHE_DIR', __DIR__ . '/' );
816
}
@@ -14,6 +22,18 @@
1422
if ( ! defined( 'POWERED_CACHE_PATH' ) ) {
1523
define( 'POWERED_CACHE_PATH', 'path' );
1624
}
25+
if ( ! defined( 'POWERED_CACHE_URL' ) ) {
26+
define( 'POWERED_CACHE_URL', 'https://example.test/wp-content/plugins/powered-cache/' );
27+
}
28+
if ( ! defined( 'POWERED_CACHE_VERSION' ) ) {
29+
define( 'POWERED_CACHE_VERSION', 'test-version' );
30+
}
31+
if ( ! defined( 'POWERED_CACHE_DROPIN_DIR' ) ) {
32+
define( 'POWERED_CACHE_DROPIN_DIR', dirname( __DIR__ ) . '/includes/dropins/' );
33+
}
34+
if ( ! defined( 'POWERED_CACHE_IS_NETWORK' ) ) {
35+
define( 'POWERED_CACHE_IS_NETWORK', false );
36+
}
1737

1838
if ( ! file_exists( __DIR__ . '/../vendor/autoload.php' ) ) {
1939
throw new PHPUnit_Framework_Exception(
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* CDN tests
4+
*/
5+
6+
namespace PoweredCache;
7+
8+
class CDN_Tests extends TestCase {
9+
10+
protected $testFiles = [
11+
'utils.php',
12+
'constants.php',
13+
];
14+
15+
public function setUp(): void {
16+
parent::setUp();
17+
18+
global $powered_cache_cdn_addresses, $is_apache;
19+
20+
$is_apache = false;
21+
$powered_cache_cdn_addresses = [
22+
'all' => [ 'cdn.example.test' ],
23+
'image' => [ 'img.example.test' ],
24+
'css' => [ 'css.example.test' ],
25+
];
26+
27+
$_SERVER['HTTP_HOST'] = 'example.test';
28+
29+
\WP_Mock::userFunction(
30+
'get_option',
31+
[
32+
'return' => [
33+
'cdn_rejected_files' => 'skip-this',
34+
],
35+
]
36+
);
37+
\WP_Mock::userFunction(
38+
'wp_parse_args',
39+
[
40+
'return' => function ( $args, $defaults ) {
41+
return array_merge( $defaults, $args );
42+
},
43+
]
44+
);
45+
\WP_Mock::userFunction(
46+
'home_url',
47+
[
48+
'return' => 'https://example.test',
49+
]
50+
);
51+
}
52+
53+
public function test_rewriter_replaces_full_and_relative_asset_urls(): void {
54+
$html = '<img src="https://example.test/wp-content/uploads/photo.jpg"><link href="/wp-content/themes/app/style.css">';
55+
56+
$rewritten = CDN::rewriter( $html );
57+
58+
$this->assertStringContainsString( 'https://img.example.test/wp-content/uploads/photo.jpg', $rewritten );
59+
$this->assertStringContainsString( '//css.example.test/wp-content/themes/app/style.css', $rewritten );
60+
}
61+
62+
public function test_rewriter_keeps_excluded_and_existing_cdn_urls(): void {
63+
$html = '<img src="https://example.test/wp-content/uploads/skip-this.jpg"><script src="https://cdn.example.test/app.js"></script>';
64+
65+
$rewritten = CDN::rewriter( $html );
66+
67+
$this->assertStringContainsString( 'https://example.test/wp-content/uploads/skip-this.jpg', $rewritten );
68+
$this->assertStringContainsString( 'https://cdn.example.test/app.js', $rewritten );
69+
}
70+
71+
public function test_cdn_optimizer_url_uses_zone_specific_hostname(): void {
72+
$cdn = new CDN();
73+
74+
$optimized_url = $cdn->cdn_optimizer_url( 'https://example.test/wp-content/cache/min/app.css', 'app.css' );
75+
76+
$this->assertSame( '//css.example.test/wp-content/cache/min/app.css', $optimized_url );
77+
}
78+
79+
public function tearDown(): void {
80+
unset( $_SERVER['HTTP_HOST'], $GLOBALS['powered_cache_cdn_addresses'] );
81+
82+
parent::tearDown();
83+
}
84+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* LazyLoad tests
4+
*/
5+
6+
namespace PoweredCache;
7+
8+
class LazyLoad_Tests extends TestCase {
9+
10+
protected $testFiles = [
11+
'utils.php',
12+
'constants.php',
13+
];
14+
15+
public function setUp(): void {
16+
parent::setUp();
17+
18+
global $is_apache;
19+
20+
$is_apache = false;
21+
22+
\WP_Mock::passthruFunction( 'esc_attr' );
23+
\WP_Mock::userFunction(
24+
'get_option',
25+
[
26+
'return' => [
27+
'lazy_load_exclusions' => 'skip-lazy',
28+
],
29+
]
30+
);
31+
\WP_Mock::userFunction(
32+
'wp_parse_args',
33+
[
34+
'return' => function ( $args, $defaults ) {
35+
return array_merge( $defaults, $args );
36+
},
37+
]
38+
);
39+
}
40+
41+
public function test_filter_images_rewrites_supported_images(): void {
42+
$html = '<p><img src="https://example.test/image.jpg" class="alignwide" srcset="https://example.test/image-2x.jpg 2x"></p>';
43+
44+
$filtered = LazyLoad::filter_images( $html );
45+
46+
$this->assertStringContainsString( 'data-lazy-type="image"', $filtered );
47+
$this->assertStringContainsString( 'data-lazy-src="https://example.test/image.jpg"', $filtered );
48+
$this->assertStringContainsString( 'data-lazy-srcset="https://example.test/image-2x.jpg 2x"', $filtered );
49+
$this->assertStringContainsString( 'class="lazy lazy-hidden alignwide"', $filtered );
50+
$this->assertStringContainsString( '<noscript><img src="https://example.test/image.jpg"', $filtered );
51+
}
52+
53+
public function test_filter_images_skips_data_uri_and_configured_exclusions(): void {
54+
$html = '<img src="data:image/png;base64,abc"><img src="https://example.test/skip.jpg" class="skip-lazy">';
55+
56+
$filtered = LazyLoad::filter_images( $html );
57+
58+
$this->assertSame( $html, $filtered );
59+
}
60+
61+
public function test_filter_iframes_rewrites_regular_iframes_and_skips_gravity_forms(): void {
62+
$html = '<iframe src="https://player.example.test/embed"></iframe><iframe id="gform_ajax_frame_1" src="about:blank"></iframe>';
63+
64+
$filtered = LazyLoad::filter_iframes( $html );
65+
66+
$this->assertStringContainsString( 'data-lazy-type="iframe"', $filtered );
67+
$this->assertStringContainsString( 'data-lazy-src="<iframe src="https://player.example.test/embed"></iframe>"', $filtered );
68+
$this->assertStringContainsString( 'id="gform_ajax_frame_1"', $filtered );
69+
}
70+
71+
public function test_replace_youtube_iframe_with_thumbnail(): void {
72+
$html = '<iframe src="https://www.youtube.com/embed/abc123_XY?start=30"></iframe>';
73+
74+
$filtered = LazyLoad::replace_youtube_iframe_with_thumbnail( $html );
75+
76+
$this->assertStringContainsString( 'class="pcll-youtube-player"', $filtered );
77+
$this->assertStringContainsString( 'data-src="https://www.youtube.com/embed/abc123_XY?start=30"', $filtered );
78+
$this->assertStringContainsString( 'https://img.youtube.com/vi/abc123_XY/0.jpg', $filtered );
79+
$this->assertStringNotContainsString( '<iframe', $filtered );
80+
}
81+
}

tests/phpunit/test-tools/Util_Tests.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Util_Tests extends Base\TestCase {
1919
];
2020

2121
public function setUp(): void {
22+
parent::setUp();
23+
2224
// Create a temporary directory for testing
2325
$this->tempDir = sys_get_temp_dir() . '/powered-cache-tests';
2426
mkdir( $this->tempDir );

0 commit comments

Comments
 (0)