';
$html .= '';
$html .= sprintf( '%s', __( 'WP Rocket license type', 'rocket' ) );
diff --git a/inc/functions/options.php b/inc/functions/options.php
index 423c5ac8dc..1c05119306 100755
--- a/inc/functions/options.php
+++ b/inc/functions/options.php
@@ -148,7 +148,7 @@ function is_rocket_generate_caching_mobile_files() { // phpcs:ignore WordPress.N
* return Array An array of domain names to DNS prefetch
*/
function rocket_get_dns_prefetch_domains() {
- $domains = (array) get_rocket_option( 'dns_prefetch' );
+ $domains = (array) get_rocket_option( 'dns_prefetch', [] );
/**
* Filter list of domains to prefetch DNS
diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Controller/addWPRImageDimensionQueryArg.php b/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Controller/addWPRImageDimensionQueryArg.php
index 4be03763c2..92c077c88e 100644
--- a/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Controller/addWPRImageDimensionQueryArg.php
+++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Controller/addWPRImageDimensionQueryArg.php
@@ -15,4 +15,12 @@
],
'expected' => 'http://example.com/?wpr_imagedimensions=1',
],
+ 'testShouldAddArgumentWhenNoFactoriesAndRUCSSEnabled' => [
+ 'config' => [
+ 'filter' => [],
+ 'url' => 'http://example.com',
+ 'remove_unused_css' => 1,
+ ],
+ 'expected' => 'http://example.com/?wpr_imagedimensions=1',
+ ],
];
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Admin/Data/collectData.php b/tests/Fixtures/inc/Engine/Media/Fonts/Admin/Data/collectData.php
new file mode 100644
index 0000000000..912e491ba4
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Admin/Data/collectData.php
@@ -0,0 +1,88 @@
+ [
+ 'wp-content' => [
+ 'cache' => [
+ 'fonts' => [
+ '1' => [
+ 'google-fonts' => [
+ 'fonts' => [
+ 's' => [
+ 'lato' => [
+ 'v24' => [
+ 'S6uyw4BMUTPHjx4wXg.woff2' => '',
+ ],
+ ],
+ 'montserrat' => [
+ 'v40' => [
+ 'memSYaGs126MiZpBA-UvWbX2vVnXBbObj2OVZyOOSr4dVJWUgsjZ0B4gaVI.woff2' => '',
+ ],
+ ],
+ 'oswald' => [
+ 'v53' => [
+ 'TK3_WkUHHAIjg75cFRf3bXL8LICs1_FvsUZiZQ.woff2' => '',
+ ],
+ ],
+ 'roboto' => [
+ 'v32' => [
+ 'KFOmCnqEu92Fr1Mu4mxK.woff2' => '',
+ ],
+ ],
+ ],
+ ],
+ ],
+ ],
+ ],
+ ],
+ ],
+ ],
+ 'test_data' => [
+ 'shouldDoNothingWhenOptionIsDisabled' => [
+ 'config' => [
+ 'options' => [
+ 'host_fonts_locally' => 0,
+ 'analytics_enabled' => 1,
+ ],
+ 'transient' => false,
+ ],
+ 'expected' => false,
+ ],
+ 'shouldDoNothingWhenAnalyticsDisabled' => [
+ 'config' => [
+ 'options' => [
+ 'host_fonts_locally' => 1,
+ 'analytics_enabled' => 0,
+ ],
+ 'transient' => false,
+ ],
+ 'expected' => false,
+ ],
+ 'shouldDoNothingWhenDataAlreadyExists' => [
+ 'config' => [
+ 'options' => [
+ 'host_fonts_locally' => 1,
+ 'analytics_enabled' => 1,
+ ],
+ 'transient' => [
+ 'fonts_total_number' => 4,
+ 'fonts_total_size' => '1.2 MB',
+ ],
+ ],
+ 'expected' => false,
+ ],
+ 'shouldCollectData' => [
+ 'config' => [
+ 'options' => [
+ 'host_fonts_locally' => 1,
+ 'analytics_enabled' => 1,
+ ],
+ 'transient' => false,
+ ],
+ 'expected' => [
+ 'fonts_total_number' => 4,
+ 'fonts_total_size' => '1.2 MB',
+ ],
+ ],
+ ],
+];
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.php b/tests/Fixtures/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.php
new file mode 100644
index 0000000000..8a4e8ee886
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.php
@@ -0,0 +1,27 @@
+ [
+ 'old_value' => [],
+ 'value' => [],
+ 'expected' => false,
+ ],
+ 'testShouldDoNothingWhenOldValueAndNewValueAreTheSame' => [
+ 'old_value' => [
+ 'host_fonts_locally' => 0,
+ ],
+ 'value' => [
+ 'host_fonts_locally' => 0,
+ ],
+ 'expected' => false,
+ ],
+ 'testShouldDeleteAllFilesWhenOldValueAndNewValueAreDifferent' => [
+ 'old_value' => [
+ 'host_fonts_locally' => 0,
+ ],
+ 'value' => [
+ 'host_fonts_locally' => 1,
+ ],
+ 'expected' => true,
+ ],
+];
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Context/OptimizationContext/isAllowed.php b/tests/Fixtures/inc/Engine/Media/Fonts/Context/OptimizationContext/isAllowed.php
new file mode 100644
index 0000000000..8dad0770f9
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Context/OptimizationContext/isAllowed.php
@@ -0,0 +1,36 @@
+ [
+ 'config' => [
+ 'bypass' => true,
+ 'do_not_optimize' => false,
+ 'option' => true,
+ ],
+ 'expected' => true,
+ ],
+ 'testShouldReturnTrueWhenDoNotOptimize' => [
+ 'config' => [
+ 'bypass' => false,
+ 'do_not_optimize' => true,
+ 'option' => true,
+ ],
+ 'expected' => true,
+ ],
+ 'testShouldReturnFalseWhenOptionDisabled' => [
+ 'config' => [
+ 'bypass' => false,
+ 'do_not_optimize' => true,
+ 'option' => false,
+ ],
+ 'expected' => false,
+ ],
+ 'testShouldReturnTrueWhenOptionEnabled' => [
+ 'config' => [
+ 'bypass' => false,
+ 'do_not_optimize' => false,
+ 'option' => true,
+ ],
+ 'expected' => true,
+ ],
+];
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Filesystem/writeFontCss.php b/tests/Fixtures/inc/Engine/Media/Fonts/Filesystem/writeFontCss.php
new file mode 100644
index 0000000000..a28fb2840d
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Filesystem/writeFontCss.php
@@ -0,0 +1,55 @@
+ [
+ 'public' => [
+ 'wp-content' => [
+ 'cache' => [
+ 'wp-rocket' => [
+ 'fonts' => [
+ '1' => [
+ 'google-font' => [
+ '1' => [
+ '5' => [
+ '9' => [
+ '5' => [
+ 'cb6ccb56826a802ed411cef875f0e.css',
+ 'cb6ccb56826a802ed411cef875f0es' => [
+ 'opensans' => [
+ 'v18' => 'mem8YaGs126MiZpBA-UFUK0Zdc0.woff2'
+ ]
+ ]
+ ]
+ ]
+ ],
+ ],
+ ],
+ ],
+ ],
+ ]
+ ],
+ ],
+ ],
+ ],
+ 'test_data' => [
+ 'shouldWriteFontCss' => [
+ 'config' => [
+ 'url' => 'https://fonts.googleapis.com/css?family=Open+Sans',
+ 'css_content' => 'url(https://fonts.gstatic.com/s/opensans/v18/mem8YaGs126MiZpBA-UFUK0Zdc0.woff2);',
+ 'provider' => 'google-font',
+ 'local_url' => 'http://example.org/wp-content',
+ 'response_code' => 200,
+ 'response' => [
+ 'headers' => [],
+ 'body' => json_encode( (object) [
+ 'success' => true,
+ 'result' => [],
+ ] ),
+ 'response' => [],
+ ],
+ ],
+ 'expected' => [
+ 'path' => 'vfs://public/wp-content/cache/wp-rocket/font/1/google-font/1/5/9/5/cb6ccb56826a802ed411cef875f0e.css',
+ ]
+ ],
+ ]
+];
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1.php
new file mode 100644
index 0000000000..d4174e5c84
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1.php
@@ -0,0 +1,33 @@
+
+
+
+
+
+
+
+ Google Font V1 Template
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1_v2.php
new file mode 100644
index 0000000000..bac4f4c3d3
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1_v2.php
@@ -0,0 +1,41 @@
+
+
+
+
+
+
+
+ Google Font V1 and V2 Template
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+ This is a subtitle
+ Enjoy your stay
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v2.php
new file mode 100644
index 0000000000..1a756bccc4
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v2.php
@@ -0,0 +1,33 @@
+
+
+
+
+
+
+
+ Google Font V2 Template
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1.php
new file mode 100644
index 0000000000..8765447423
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1.php
@@ -0,0 +1,35 @@
+
+
+
+
+
+
+
+ Google Font V1 Template
+
+
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1_v2.php
new file mode 100644
index 0000000000..cfa1f44954
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1_v2.php
@@ -0,0 +1,43 @@
+
+
+
+
+
+
+
+ Google Font V1 and V2 Template
+
+
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+ This is a subtitle
+ Enjoy your stay
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v2.php
new file mode 100644
index 0000000000..8b5f3277fc
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v2.php
@@ -0,0 +1,35 @@
+
+
+
+
+
+
+
+ Google Font V2 Template
+
+
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFontsForOptimizations.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFontsForOptimizations.php
new file mode 100644
index 0000000000..3e029b623a
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFontsForOptimizations.php
@@ -0,0 +1,54 @@
+ [
+ 'testShouldReturnOriginalWhenNotAllowed' => [
+ 'config' => [
+ 'is_allowed' => false,
+ 'write' => false,
+ ],
+ 'original' => '',
+ 'expected' => '',
+ ],
+ 'testShouldReturnOriginalWhenNoGoogleFonts' => [
+ 'config' => [
+ 'is_allowed' => true,
+ 'write' => false,
+ ],
+ 'original' => '',
+ 'expected' => '',
+ ],
+ 'testShouldReturnOriginalWhenWriteFailed' => [
+ 'config' => [
+ 'is_allowed' => true,
+ 'write' => false,
+ ],
+ 'original' => file_get_contents( __DIR__ . '/HTML/input_v1.php' ),
+ 'expected' => file_get_contents( __DIR__ . '/HTML/input_v1.php' ),
+ ],
+ 'testShouldRewriteV1Font' => [
+ 'config' => [
+ 'is_allowed' => true,
+ 'write' => true,
+ ],
+ 'original' => file_get_contents( __DIR__ . '/HTML/input_v1.php' ),
+ 'expected' => file_get_contents( __DIR__ . '/HTML/expected_v1.php' ),
+ ],
+ 'testShouldRewriteV2' => [
+ 'config' => [
+ 'is_allowed' => true,
+ 'write' => true,
+ ],
+ 'original' => file_get_contents( __DIR__ . '/HTML/input_v2.php' ),
+ 'expected' => file_get_contents( __DIR__ . '/HTML/expected_v2.php' ),
+ ],
+ 'testShouldRewriteV1AndV2' => [
+ 'config' => [
+ 'is_allowed' => true,
+ 'write' => true,
+ ],
+ 'original' => file_get_contents( __DIR__ . '/HTML/input_v1_v2.php' ),
+ 'expected' => file_get_contents( __DIR__ . '/HTML/expected_v1_v2.php' ),
+ ],
+ ],
+];
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1.php
new file mode 100644
index 0000000000..d4174e5c84
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1.php
@@ -0,0 +1,33 @@
+
+
+
+
+
+
+
+ Google Font V1 Template
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1_excluded_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1_excluded_v2.php
new file mode 100644
index 0000000000..415f310ee1
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1_excluded_v2.php
@@ -0,0 +1,41 @@
+
+
+
+
+
+
+
+ Google Font V1 and V2 Template
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+ This is a subtitle
+ Enjoy your stay
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1_style_tag.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1_style_tag.php
new file mode 100644
index 0000000000..bcbcee1f97
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1_style_tag.php
@@ -0,0 +1,33 @@
+
+
+
+
+
+
+
+ Google Font V1 Template
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1_v2.php
new file mode 100644
index 0000000000..bac4f4c3d3
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1_v2.php
@@ -0,0 +1,41 @@
+
+
+
+
+
+
+
+ Google Font V1 and V2 Template
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+ This is a subtitle
+ Enjoy your stay
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1_v2_regex.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1_v2_regex.php
new file mode 100644
index 0000000000..0d2c2d2350
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v1_v2_regex.php
@@ -0,0 +1,38 @@
+
+
+
+
+
+
+
+Google Font V2 Template
+
+
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v2.php
new file mode 100644
index 0000000000..1a756bccc4
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v2.php
@@ -0,0 +1,33 @@
+
+
+
+
+
+
+
+ Google Font V2 Template
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v2_from_combination.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v2_from_combination.php
new file mode 100644
index 0000000000..d48d6589af
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/expected_v2_from_combination.php
@@ -0,0 +1,12 @@
+
+
+
+
+ Sample Page
+
+
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v1.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v1.php
new file mode 100644
index 0000000000..e49961654e
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v1.php
@@ -0,0 +1,33 @@
+
+
+
+
+
+
+
+ Google Font V1 Template
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v1_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v1_v2.php
new file mode 100644
index 0000000000..34be74bed6
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v1_v2.php
@@ -0,0 +1,41 @@
+
+
+
+
+
+
+
+ Google Font V1 and V2 Template
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+ This is a subtitle
+ Enjoy your stay
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v1_v2_regex.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v1_v2_regex.php
new file mode 100644
index 0000000000..8def81e423
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v1_v2_regex.php
@@ -0,0 +1,38 @@
+
+
+
+
+
+
+
+Google Font V2 Template
+
+
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v2.php
new file mode 100644
index 0000000000..2abf18db1b
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v2.php
@@ -0,0 +1,33 @@
+
+
+
+
+
+
+
+ Google Font V2 Template
+
+
+
+
+
+
+
+
+ Hello World
+ Welcome to the world
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v2_from_combination.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v2_from_combination.php
new file mode 100644
index 0000000000..00d9bdf377
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/HTML/input_v2_from_combination.php
@@ -0,0 +1,12 @@
+
+
+
+
+ Sample Page
+
+
+
+
+
+
+
diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/rewriteFontsForOptimizations.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/rewriteFontsForOptimizations.php
new file mode 100644
index 0000000000..62c0b40858
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Subscriber/rewriteFontsForOptimizations.php
@@ -0,0 +1,163 @@
+ [
+ 'testShouldReturnOriginalWhenNoGoogleFonts' => [
+ 'config' => [
+ 'html' => '',
+ 'host_fonts_locally' => true,
+ 'locally_inline_css' => false,
+ ],
+ 'expected' => [
+ 'html' => ''
+ ],
+ ],
+ 'testShouldRewriteV1Font' => [
+ 'config' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/input_v1.php' ),
+ 'host_fonts_locally' => true,
+ 'locally_inline_css' => false,
+ 'http' => [
+ 'https://fonts.googleapis.com/css?family=Roboto' => [
+ 'body' => 'body { font-family: "Roboto"; }',
+ 'response' => ['code' => 200 ]
+ ],
+ 'https://fonts.googleapis.com/css?family=Open+Sans' => [
+ 'body' => 'body { font-family: "Open-San"; }',
+ 'response' => ['code' => 200 ]
+ ],
+ ],
+ ],
+ 'expected' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/expected_v1.php' ),
+ ],
+ ],
+ 'testShouldRewriteV2Font' => [
+ 'config' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/input_v2.php' ),
+ 'host_fonts_locally' => true,
+ 'locally_inline_css' => false,
+ 'http' => [
+ 'https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap' => [
+ 'body' => 'body { font-family: "Roboto"; }',
+ 'response' => ['code' => 200 ]
+ ],
+ 'https://fonts.googleapis.com/css2?family=Lato:wght@400;700&display=swap' => [
+ 'body' => 'body { font-family: "Lato"; }',
+ 'response' => ['code' => 200 ]
+ ],
+ ],
+ ],
+ 'expected' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/expected_v2.php' ),
+ ],
+ ],
+ 'testShouldRewriteV1AndV2Fonts' => [
+ 'config' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/input_v1_v2.php' ),
+ 'host_fonts_locally' => true,
+ 'locally_inline_css' => false,
+ 'http' => [
+ 'https://fonts.googleapis.com/css?family=Roboto|Open+Sans' => [
+ 'body' => '.roboto { font-family: "Roboto"; } .open-san { font-family: "Open-San"; }',
+ 'response' => ['code' => 200 ]
+ ],
+ 'https://fonts.googleapis.com/css2?family=Lato:wght@400;700&family=Montserrat:wght@400;700&display=swap' => [
+ 'body' => '.lato { font-family: "Lato"; } .montserrat { font-family: "Montserrat"; }',
+ 'response' => ['code' => 200 ]
+ ],
+ ],
+ ],
+ 'expected' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/expected_v1_v2.php' ),
+ ],
+ ],
+ 'testShouldRewriteV1AndExcludeV2Fonts' => [
+ 'config' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/input_v1_v2.php' ),
+ 'host_fonts_locally' => true,
+ 'locally_inline_css' => false,
+ 'http' => [
+ 'https://fonts.googleapis.com/css?family=Roboto|Open+Sans' => [
+ 'body' => '.roboto { font-family: "Roboto"; } .open-san { font-family: "Open-San"; }',
+ 'response' => ['code' => 200 ]
+ ],
+ 'https://fonts.googleapis.com/css2?family=Lato:wght@400;700&family=Montserrat:wght@400;700&display=swap' => [
+ 'body' => '.lato { font-family: "Lato"; } .montserrat { font-family: "Montserrat"; }',
+ 'response' => ['code' => 200 ]
+ ],
+ ],
+ 'exclude_locally_host_fonts' => [
+ 'Lato',
+ ]
+ ],
+ 'expected' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/expected_v1_excluded_v2.php' ),
+ ],
+ ],
+ 'testShouldRewriteFontV1PathInStyleTag' => [
+ 'config' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/input_v1.php' ),
+ 'host_fonts_locally' => true,
+ 'http' => [
+ 'https://fonts.googleapis.com/css?family=Roboto' => [
+ 'body' => 'body { font-family: "Roboto"; }',
+ 'response' => ['code' => 200 ]
+ ],
+ 'https://fonts.googleapis.com/css?family=Open+Sans' => [
+ 'body' => 'body { font-family: "Open-San"; }',
+ 'response' => ['code' => 200 ]
+ ],
+ ],
+ 'locally_inline_css' => true,
+ ],
+ 'expected' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/expected_v1_style_tag.php' ),
+ ],
+ ],
+ 'testShouldExcludeFont' => [
+ 'config' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/input_v2_from_combination.php' ),
+ 'host_fonts_locally' => true,
+ 'locally_inline_css' => false,
+ 'http' => [
+ 'https://fonts.googleapis.com/css2?family=Goldman:wght@700&family=Roboto:ital,wght@0,100;0,400;0,500;1,500;1,900&family=MontSerra:ital,wght@0,100;0,400;0,500;1,500;1,900&family=Comfortaa&display=optional' => [
+ 'body' => '',
+ 'response' => ['code' => 200 ]
+ ],
+ ],
+ 'exclude_locally_host_fonts' => [
+ 'Lato',
+ ]
+ ],
+ 'expected' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/expected_v2_from_combination.php' ),
+ ],
+ ],
+ 'testShouldExcludeFontWithRegex' => [
+ 'config' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/input_v1_v2_regex.php' ),
+ 'host_fonts_locally' => true,
+ 'locally_inline_css' => false,
+ 'http' => [
+ 'https://fonts.googleapis.com/css?family=Roboto|Open+Sans' => [
+ 'body' => '.roboto { font-family: "Roboto"; } .open-san { font-family: "Open-San"; }',
+ 'response' => ['code' => 200 ]
+ ],
+ 'https://fonts.googleapis.com/css2?family=Lato:wght@400;700&family=Montserrat:wght@400;700&display=swap' => [
+ 'body' => '.lato { font-family: "Lato"; } .montserrat { font-family: "Montserrat"; }',
+ 'response' => ['code' => 200 ]
+ ],
+ ],
+ 'exclude_locally_host_fonts' => [
+ 'family=Rob(.*)o',
+ 'family(.*)Montserrat(.*)display=swap',
+ 'astra-google-(.*)-css',
+ ]
+ ],
+ 'expected' => [
+ 'html' => file_get_contents( __DIR__ . '/HTML/expected_v1_v2_regex.php' ),
+ ],
+ ],
+ ]
+];
diff --git a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
index 7e5f9b8993..c78bf59210 100644
--- a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
+++ b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
@@ -2,6 +2,10 @@
return [
'testShouldCombineGoogleFontsWithoutSubsets' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -21,6 +25,10 @@
',
],
'testShouldUseFilteredDisplayValue' => [
+ 'config' => [
+ 'swap' => 'optional',
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -38,9 +46,12 @@
',
- 'filtered' => 'optional',
],
'testShouldCombineGoogleFontsWithSubsets' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -60,6 +71,10 @@
',
],
'testShouldCombineGoogleFontsWithoutSubsetsAndNoEnding|' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -79,6 +94,10 @@
',
],
'testShouldCombineGoogleFontsWithoutSubsetsWhenMalformedURL' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -98,6 +117,10 @@
',
],
'testShouldCombineGoogleFontsWithSubsetsWhenMalformedURL' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -119,6 +142,10 @@
',
],
'testShouldOptimizeSingleGoogleFontsWhenNoParam' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -138,14 +165,18 @@
',
],
'testShouldOptimizeSingleGoogleFontsWhenParam' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
-
- Sample Page
-
-
-
-
- ',
+
+ Sample Page
+
+
+
+
+ ',
'expected' => '
Sample Page
@@ -156,6 +187,10 @@
',
],
'testShouldOptimizeSingleGoogleFontsWhenInvalidParam' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -174,14 +209,18 @@
',
],
'testShouldOptimizeSingleGoogleFontsWhenEncodedParam' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
-
- Sample Page
-
-
-
-
- ',
+
+ Sample Page
+
+
+
+
+ ',
'expected' => '
Sample Page
@@ -192,17 +231,21 @@
',
],
'testShouldCombineGoogleFontsWhenMultipleTitleTags' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
-
- Sample Page
-
-
-
- Sample Title 2
-
-
-
- ',
+
+ Sample Page
+
+
+
+ Sample Title 2
+
+
+
+ ',
'expected' => '
Sample Page
@@ -213,6 +256,10 @@
',
],
'testShouldCombineGoogleFontsWhenTitleTagInsideBody' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -234,26 +281,91 @@
',
],
'testShouldCombineGoogleFontsWhenTitleTagInsideSvgTag' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
+
+ Sample Page
+
+
+
+
+
+
+
+
+ ',
+ 'expected' => '
- Sample Page
-
-
-
+ Sample Page
',
+ ],
+ 'testShouldOptimizeSingleGoogleFontsNoPreload' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => true,
+ ],
+ 'html' => '
+
+ Sample Page
+
+
+
+
+ ',
+ // Expected: Combined HTML.
'expected' => '
- Sample Page
+ Sample Page
-
-
',
],
+ 'shouldExcludeFontFromCombine' => [
+ 'config' => [
+ 'swap' => 'optional',
+ 'disable_preload' => false,
+ 'exclude_locally_host_fonts' => [
+ 'Lato',
+ ]
+ ],
+ 'html' => '
+
+
+ Sample Page
+
+
+
+
+
+
+
+
+ ',
+ 'expected' => '
+
+
+
+ Sample Page
+
+
+
+
+
+
+
+
+
+ ',
+ ]
];
diff --git a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV1V2/optimize.php b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV1V2/optimize.php
index 620e6f798c..88120d742f 100644
--- a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV1V2/optimize.php
+++ b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV1V2/optimize.php
@@ -2,7 +2,11 @@
return [
'shouldReturnOptimizedTagWhenSingleTagGiven' => [
- 'given' =>
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
'
@@ -29,7 +33,11 @@
'
],
'shouldUseFilteredDisplayValue' => [
- 'given' =>
+ 'config' => [
+ 'swap' => 'optional',
+ 'disable_preload' => false,
+ ],
+ 'html' =>
'
@@ -55,10 +63,13 @@
'
,
- 'filtered' => 'optional',
],
'shouldNotCombineMultipleTagsWithTextParam' => [
- 'given' =>
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
'
@@ -87,7 +98,11 @@
'
],
'shouldCombineMultipleTags' => [
- 'given' =>
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
'
@@ -115,7 +130,11 @@
'
],
'shouldCombineMultipleTagsWithMultipleFamiliesInTag' => [
- 'given' =>
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
'
@@ -145,7 +164,11 @@
'
],
'shouldRemovePreconnectWhenNoGoogleFontsPresentOnPage' => [
- 'given' =>
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
'
diff --git a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php
index 1436ce069d..7c827ede94 100644
--- a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php
+++ b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php
@@ -2,8 +2,12 @@
return [
'shouldReturnGivenHTMLWhenNoRelevantTags' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
+ '
Sample Page
@@ -11,8 +15,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -25,8 +28,12 @@
'
],
'shouldReturnTagWithFontDisplayWhenSingleTagGiven' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
+ '
Sample Page
@@ -34,8 +41,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -49,8 +55,11 @@
'
],
'shouldNotCombineMultipleTagsWithTextParam' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' => '
Sample Page
@@ -59,8 +68,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -74,8 +82,12 @@
'
],
'shouldCombineMultipleTags' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
+ '
Sample Page
@@ -84,8 +96,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -98,8 +109,12 @@
'
],
'shouldCombineMultipleTagsWithMultipleFamiliesInTag' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
+ '
Sample Page
@@ -109,8 +124,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -124,8 +138,12 @@
'
],
'shouldReplaceAnotherFontDisplayValueWithSwap' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
+ '
Sample Page
@@ -135,8 +153,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -150,8 +167,12 @@
'
],
'shouldReplaceDisplayValueWithFilteredValue' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => 'optional',
+ 'disable_preload' => false,
+ ],
+ 'html' =>
+ '
Sample Page
@@ -161,8 +182,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -175,6 +195,71 @@
'
,
- 'filtered' => 'optional'
],
+ 'shouldCombineMultipleTagsNoPreload' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => true,
+ ],
+ 'html' =>
+ '
+
+
+ Sample Page
+
+
+
+
+
+ ',
+ 'expected' =>
+ '
+
+
+ Sample Page
+
+
+
+
+ '
+ ],
+ 'shouldExcludeFontFromCombine' => [
+ 'config' => [
+ 'swap' => 'optional',
+ 'disable_preload' => false,
+ 'exclude_locally_host_fonts' => [
+ 'Lato',
+ ]
+ ],
+ 'html' => '
+
+
+ Sample Page
+
+
+
+
+
+
+
+
+ ',
+ 'expected' => '
+
+
+
+ Sample Page
+
+
+
+
+
+
+
+
+
+ ',
+ ]
];
diff --git a/tests/Fixtures/inc/admin/rocketFirstInstall.php b/tests/Fixtures/inc/admin/rocketFirstInstall.php
index 6784e04b54..23a232d6a7 100644
--- a/tests/Fixtures/inc/admin/rocketFirstInstall.php
+++ b/tests/Fixtures/inc/admin/rocketFirstInstall.php
@@ -77,6 +77,7 @@
$integration[ 'preload_links' ] = 1;
$integration[ 'image_dimensions' ] = 0;
$integration[ 'exclude_lazyload' ] = [];
+$integration['host_fonts_locally'] = 0;
return [
'test_data' => [
diff --git a/tests/Integration/inc/Engine/Media/Fonts/Frontend/Subscriber/rewriteFontsForOptimizations.php b/tests/Integration/inc/Engine/Media/Fonts/Frontend/Subscriber/rewriteFontsForOptimizations.php
new file mode 100644
index 0000000000..ed62825df3
--- /dev/null
+++ b/tests/Integration/inc/Engine/Media/Fonts/Frontend/Subscriber/rewriteFontsForOptimizations.php
@@ -0,0 +1,66 @@
+unregisterAllCallbacksExcept('rocket_buffer', 'rewrite_fonts', 18);
+ add_filter( 'pre_get_rocket_option_host_fonts_locally', [ $this, 'host_fonts_locally' ] );
+ add_filter( 'rocket_host_fonts_locally_inline_css', [ $this, 'locally_inline_css' ] );
+ add_filter('rocket_exclude_locally_host_fonts', [ $this, 'exclude_locally_host_fonts' ] );
+ $this->setup_http();
+
+ }
+
+ public function tear_down() {
+ remove_filter('pre_get_rocket_option_host_fonts_locally', [$this, 'host_fonts_locally']);
+ remove_filter('rocket_host_fonts_locally_inline_css', [$this, 'locally_inline_css']);
+ remove_filter('rocket_exclude_locally_host_fonts', [ $this, 'exclude_locally_host_fonts' ] );
+
+ $this->restoreWpHook('rocket_buffer');
+ $this->tear_down_http();
+
+
+ parent::tear_down();
+ }
+
+ /**
+ * @dataProvider providerTestData
+ */
+ public function testShouldReturnAsExpected( $config, $expected ) {
+ $this->config = $config;
+
+ $this->assertSame(
+ $expected['html'],
+ wpm_apply_filters_typed('string', 'rocket_buffer', $config['html'])
+ );
+ }
+
+ public function host_fonts_locally() {
+ return $this->config['host_fonts_locally'];
+ }
+
+ public function locally_inline_css() {
+ return $this->config['locally_inline_css'];
+ }
+
+ public function exclude_locally_host_fonts() {
+ return $this->config['exclude_locally_host_fonts'] ?? [];
+ }
+}
diff --git a/tests/Integration/inc/Engine/Media/ImageDimensions/AdminSubscriber/addOption.php b/tests/Integration/inc/Engine/Media/ImageDimensions/AdminSubscriber/addOption.php
index ffe184e47a..e1d19dcfcd 100644
--- a/tests/Integration/inc/Engine/Media/ImageDimensions/AdminSubscriber/addOption.php
+++ b/tests/Integration/inc/Engine/Media/ImageDimensions/AdminSubscriber/addOption.php
@@ -18,9 +18,9 @@ public function set_up() {
}
public function tear_down() {
- parent::tear_down();
-
$this->restoreWpHook( 'rocket_first_install_options' );
+
+ parent::tear_down();
}
/**
diff --git a/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php b/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
index 1a97e8b243..5b2e4a013d 100644
--- a/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
+++ b/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
@@ -13,11 +13,14 @@
* @group GoogleFonts
*/
class Test_Optimize extends TestCase {
+ private $display;
+ private $disable_preload;
- private $filter_value;
+ protected $config;
public function set_up() {
parent::set_up();
+
$GLOBALS['wp'] = (object) [
'query_vars' => [],
'request' => 'http://example.org',
@@ -25,47 +28,59 @@ public function set_up() {
'embed',
],
];
- $this->unregisterAllCallbacksExcept('rocket_buffer', 'process', 1001 );
+
+ $this->unregisterAllCallbacksExcept('rocket_buffer', 'process', 17 );
+ add_filter('rocket_exclude_locally_host_fonts', [ $this, 'exclude_locally_host_fonts' ] ); // @phpstan-ignore-line
}
public function tear_down() {
remove_filter( 'pre_get_rocket_option_minify_google_fonts', [ $this, 'return_true' ] );
remove_filter( 'rocket_combined_google_fonts_display', [ $this, 'set_display_value' ] );
+ remove_filter( 'rocket_disable_google_fonts_preload', [ $this, 'set_disable_preload' ] );
+ remove_filter('rocket_exclude_locally_host_fonts', [ $this, 'exclude_locally_host_fonts' ] );
+
- unset( $this->filter_value );
$this->restoreWpHook('rocket_buffer');
+
parent::tear_down();
}
/**
* @dataProvider addDataProviderV1
*/
- public function testShouldCombineGoogleFontsV1( $original, $combined, $filtered = false ) {
- $this->doTest( $original, $combined, $filtered );
+ public function testShouldCombineGoogleFontsV1( $config, $original, $combined ) {
+ $this->config = $config;
+ $this->doTest( $config, $original, $combined );
}
/**
* @dataProvider addDataProviderV2
*/
- public function testShouldCombineGoogleFontsV2( $original, $combined, $filtered = false ) {
- $this->doTest( $original, $combined, $filtered );
+ public function testShouldCombineGoogleFontsV2( $config, $original, $combined ) {
+ $this->config = $config;
+ $this->doTest( $config, $original, $combined );
}
/**
* @dataProvider addDataProviderV1V2
*/
- public function testShouldCombineGoogleFontsV1V2( $original, $combined, $filtered = false ) {
- $this->doTest( $original, $combined, $filtered );
+ public function testShouldCombineGoogleFontsV1V2( $config, $original, $combined ) {
+ $this->config = $config;
+ $this->doTest( $config, $original, $combined );
}
- private function doTest( $original, $expected, $filtered ) {
+ private function doTest( $config, $original, $expected ) {
+ $this->display = $config['swap'];
+ $this->disable_preload = $config['disable_preload'];
+
add_filter( 'pre_get_rocket_option_minify_google_fonts', [ $this, 'return_true' ] );
- if ( $filtered ) {
- $this->filter_value = $filtered;
+ if ( false !== $config['swap'] ) {
add_filter( 'rocket_combined_google_fonts_display', [ $this, 'set_display_value' ] );
}
+ add_filter( 'rocket_disable_google_fonts_preload', [ $this, 'set_disable_preload' ] );
+
$actual = apply_filters( 'rocket_buffer', $original );
$this->assertSame(
@@ -86,7 +101,15 @@ public function addDataProviderV1V2() {
return $this->getTestData( __DIR__ . 'V1V2', 'optimize' );
}
- public function set_display_value( $filtered ) {
- return $this->filter_value;
+ public function set_display_value() {
+ return $this->display;
+ }
+
+ public function set_disable_preload() {
+ return $this->disable_preload;
+ }
+
+ public function exclude_locally_host_fonts() {
+ return $this->config['exclude_locally_host_fonts'] ?? [];
}
}
diff --git a/tests/Unit/bootstrap.php b/tests/Unit/bootstrap.php
index d2651ce533..6a3a4ad4d7 100644
--- a/tests/Unit/bootstrap.php
+++ b/tests/Unit/bootstrap.php
@@ -9,7 +9,7 @@
// Set the path and URL to our virtual filesystem.
define( 'WP_ROCKET_CACHE_ROOT_PATH', 'vfs://public/wp-content/cache/' );
-define( 'WP_ROCKET_CACHE_ROOT_URL', 'vfs://public/wp-content/cache/' );
+define( 'WP_ROCKET_CACHE_ROOT_URL', 'http://example.org/wp-content/cache/' );
define( 'OBJECT', 'OBJECT' );
/**
* The original files need to loaded into memory before we mock them with Patchwork. Add files here before the unit
diff --git a/tests/Unit/inc/Engine/Common/PerformanceHints/WarmUp/Controller/addWPRImageDimensionQueryArg.php b/tests/Unit/inc/Engine/Common/PerformanceHints/WarmUp/Controller/addWPRImageDimensionQueryArg.php
index 159b35ab0b..8d9b7e4829 100644
--- a/tests/Unit/inc/Engine/Common/PerformanceHints/WarmUp/Controller/addWPRImageDimensionQueryArg.php
+++ b/tests/Unit/inc/Engine/Common/PerformanceHints/WarmUp/Controller/addWPRImageDimensionQueryArg.php
@@ -36,11 +36,16 @@ protected function setUp(): void {
public function testShouldReturnExpected( $config, $expected ) {
$this->controller = new Controller( $config['filter'], $this->options, $this->api_client, $this->user, $this->queue );
+ $this->options->shouldReceive('get')
+ ->with( 'remove_unused_css', 0 )
+ ->andReturn( $config['remove_unused_css'] ?? 0 );
+
Functions\expect( 'add_query_arg' )
->with(
- [ 'wpr_imagedimensions' => 1 ]
+ [ 'wpr_imagedimensions' => 1 ],
+ $config['url']
)
- ->andReturn( $expected );
+ ->andReturn( $config['url'] . '/?wpr_imagedimensions=1' );
$this->assertSame(
$expected,
diff --git a/tests/Unit/inc/Engine/Media/Fonts/Admin/Data/collectData.php b/tests/Unit/inc/Engine/Media/Fonts/Admin/Data/collectData.php
new file mode 100644
index 0000000000..4d42b8a3b0
--- /dev/null
+++ b/tests/Unit/inc/Engine/Media/Fonts/Admin/Data/collectData.php
@@ -0,0 +1,57 @@
+justReturn( 1 );
+
+ $this->options = Mockery::mock( Options_Data::class );
+ $this->data = new Data( $this->options );
+ }
+
+ /**
+ * @dataProvider providerTestData
+ */
+ public function testShouldDoExpected( $config, $expected ) {
+ $this->options->shouldReceive( 'get' )
+ ->with( 'host_fonts_locally', 0 )
+ ->andReturn( $config['options']['host_fonts_locally'] );
+
+ $this->options->shouldReceive( 'get')
+ ->with( 'analytics_enabled', 0 )
+ ->andReturn( $config['options']['analytics_enabled'] );
+
+ Functions\when( 'get_transient' )->justReturn( $config['transient'] );
+
+ Functions\when( 'size_format' )->justReturn( '1.2 MB' );
+
+ if ( $expected ) {
+ Functions\expect( 'set_transient' )
+ ->once()
+ ->with( 'rocket_fonts_data_collection', $expected, WEEK_IN_SECONDS );
+ } else {
+ Functions\expect( 'set_transient' )->never();
+ }
+
+ $this->data->collect_data();
+ }
+}
diff --git a/tests/Unit/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.php b/tests/Unit/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.php
new file mode 100644
index 0000000000..b5b1d84f11
--- /dev/null
+++ b/tests/Unit/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.php
@@ -0,0 +1,41 @@
+justReturn( 1 );
+
+ $this->filesystem = Mockery::mock( Filesystem::class );
+ $this->clean = new Clean( $this->filesystem );
+ }
+
+ /**
+ * @dataProvider configTestData
+ */
+ public function testShouldDoExpected( $old_value, $value, $expected ) {
+ if ( $expected ) {
+ $this->filesystem->shouldReceive( 'delete_all_files_from_directory' )->once();
+ } else {
+ $this->filesystem->shouldNotReceive( 'delete_all_files_from_directory' );
+ }
+
+ $this->clean->clean_on_option_change( $old_value, $value );
+ }
+}
diff --git a/tests/Unit/inc/Engine/Media/Fonts/Context/Context/isAllowed.php b/tests/Unit/inc/Engine/Media/Fonts/Context/Context/isAllowed.php
new file mode 100644
index 0000000000..201412162a
--- /dev/null
+++ b/tests/Unit/inc/Engine/Media/Fonts/Context/Context/isAllowed.php
@@ -0,0 +1,36 @@
+donotrocketoptimize = $config['do_not_optimize'];
+
+ $options = Mockery::mock( Options_Data::class );
+ $context = new OptimizationContext( $options );
+
+ Functions\when( 'rocket_bypass' )->justReturn( $config['bypass'] );
+
+ $options->shouldReceive( 'get' )
+ ->with( 'host_fonts_locally', 0 )
+ ->andReturn( $config['option'] );
+
+ $this->assertSame(
+ $expected,
+ $context->is_allowed( $config )
+ );
+ }
+}
diff --git a/tests/Unit/inc/Engine/Media/Fonts/Filesystem/writeFontCss.php b/tests/Unit/inc/Engine/Media/Fonts/Filesystem/writeFontCss.php
new file mode 100644
index 0000000000..e50481bf6d
--- /dev/null
+++ b/tests/Unit/inc/Engine/Media/Fonts/Filesystem/writeFontCss.php
@@ -0,0 +1,54 @@
+justReturn( 1 );
+
+ $this->stubWpParseUrl();
+ }
+
+ /**
+ * @dataProvider providerTestData
+ */
+ public function testShouldReturnExpected( $config, $expected ) {
+
+ $filesystem = new Filesystem();
+
+ Functions\when( 'wp_remote_retrieve_body' )
+ ->justReturn( $config['css_content'] );
+
+ Functions\when('rocket_mkdir_p')->alias(function($dir) {
+ if (!file_exists($dir)) {
+ mkdir($dir, 0777, true);
+ }
+ return true;
+ });
+
+ Functions\when( 'wp_safe_remote_get' )
+ ->justReturn( $config['response'] );
+
+ Functions\when( 'wp_remote_retrieve_response_code' )
+ ->justReturn( $config['response_code'] );
+
+ Functions\when( 'content_url' )->justReturn( $config['local_url']);
+
+ $this->assertTrue( $filesystem->write_font_css( $config['url'], $config['provider']) );
+
+ //$this->assertTrue( $this->filesystem->exists( $expected['path'] ) );
+ }
+}
diff --git a/tests/Unit/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFontsForOptimizations.php b/tests/Unit/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFontsForOptimizations.php
new file mode 100644
index 0000000000..5c5945e624
--- /dev/null
+++ b/tests/Unit/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFontsForOptimizations.php
@@ -0,0 +1,69 @@
+justReturn( 1 );
+
+ $this->optimization_context = Mockery::mock( OptimizationContext::class );
+ $this->saas_context = Mockery::mock( SaasContext::class );
+ $this->fonts_filesystem = Mockery::mock( FontsFilesystem::class );
+ $this->controller = new Controller( $this->optimization_context, $this->saas_context, $this->fonts_filesystem );
+
+ $this->stubWpParseUrl();
+ }
+
+ /**
+ * @dataProvider providerTestData
+ */
+ public function testShouldDoExpected( $config, $original, $expected ) {
+ $this->optimization_context->shouldReceive('is_allowed')
+ ->once()
+ ->andReturn( $config['is_allowed'] );
+
+ $this->fonts_filesystem->shouldReceive( 'exists' )
+ ->andReturn( false );
+
+ $this->fonts_filesystem->shouldReceive( 'write_font_css' )
+ ->andReturn( $config['write'] );
+ $this->fonts_filesystem->shouldReceive( 'hash_to_path' )
+ ->andReturnUsing( function( $hash ) {
+ $levels = 3;
+
+ $base = substr( $hash, 0, $levels );
+ $remain = substr( $hash, $levels );
+
+ $path_array = str_split( $base );
+ $path_array[] = $remain;
+
+ return implode( '/', $path_array );
+ } );
+
+ $this->assertSame(
+ $this->format_the_html( $expected ),
+ $this->format_the_html( $this->controller->rewrite_fonts_for_optimizations( $original ) )
+ );
+ }
+}
diff --git a/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php b/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
index b2c8e05ff4..4d54ad5fc5 100644
--- a/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
+++ b/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
@@ -1,12 +1,11 @@
alias( function( $url, $component ) {
- return parse_url( $url, $component );
- } );
+ public function testShouldCombineGoogleFonts( $config, $html, $expected ) {
+ $this->stubWpParseUrl();
Functions\when( 'wp_parse_args' )->alias( function( $value ) {
parse_str( $value, $r );
@@ -40,12 +37,20 @@ public function testShouldCombineGoogleFonts( $html, $expected, $filtered = fals
return str_replace( [ '&', '&' ], '&', $url );
} );
- if ( false !== $filtered ) {
- Filters\expectApplied('rocket_combined_google_fonts_display')
- ->with('swap', Mockery::type(AbstractGFOptimization::class))
- ->andReturn( $filtered );
+ if ( false !== $config['swap'] ) {
+ Filters\expectApplied( 'rocket_combined_google_fonts_display' )
+ ->with('swap', Mockery::type( AbstractGFOptimization::class ) )
+ ->andReturn( $config['swap'] );
}
+
+ Filters\expectApplied( 'rocket_disable_google_fonts_preload' )
+ ->andReturn( $config['disable_preload'] );
+
+ Filters\expectApplied( 'rocket_exclude_locally_host_fonts' )
+ ->andReturn( $config['exclude_locally_host_fonts'] ?? [] );
+
+
$combine = new Combine();
$this->assertSame(
diff --git a/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php b/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php
index 5fda6ddfc4..b9f57421b4 100644
--- a/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php
+++ b/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php
@@ -1,12 +1,11 @@
alias( function ( $url, $component ) {
- return parse_url( $url, $component );
- } );
+ public function testShouldCombineV2GoogleFonts( $config, $html, $expected ) {
+ $this->stubWpParseUrl();
Functions\when( 'wp_parse_args' )->alias( function ( $value ) {
parse_str( $value, $r );
@@ -39,12 +36,20 @@ public function testShouldCombineV2GoogleFonts( $html, $expected, $filtered = fa
return str_replace( [ '&', '&' ], '&', $url );
} );
- if ( false !== $filtered ) {
- Filters\expectApplied('rocket_combined_google_fonts_display')
- ->with('swap', Mockery::type(AbstractGFOptimization::class))
- ->andReturn( $filtered );
+ if ( false !== $config['swap'] ) {
+ Filters\expectApplied( 'rocket_combined_google_fonts_display' )
+ ->with('swap', Mockery::type( AbstractGFOptimization::class ) )
+ ->andReturn( $config['swap'] );
+ }
+
+ if ( false !== $config['disable_preload'] ) {
+ Filters\expectApplied( 'rocket_disable_google_fonts_preload' )
+ ->andReturn( $config['disable_preload'] );
}
+ Filters\expectApplied( 'rocket_exclude_locally_host_fonts' )
+ ->andReturn( $config['exclude_locally_host_fonts'] ?? [] );
+
$combiner = new CombineV2();
$this->assertSame(
diff --git a/wp-rocket.php b/wp-rocket.php
index f052da3521..c515de6293 100755
--- a/wp-rocket.php
+++ b/wp-rocket.php
@@ -3,7 +3,7 @@
* Plugin Name: WP Rocket
* Plugin URI: https://wp-rocket.me
* Description: The best WordPress performance plugin.
- * Version: 3.18
+ * Version: 3.18-alpha2
* Requires at least: 5.8
* Requires PHP: 7.3
* Code Name: Iego
@@ -20,7 +20,7 @@
defined( 'ABSPATH' ) || exit;
// Rocket defines.
-define( 'WP_ROCKET_VERSION', '3.18' );
+define( 'WP_ROCKET_VERSION', '3.18-alpha2' );
define( 'WP_ROCKET_WP_VERSION', '5.8' );
define( 'WP_ROCKET_WP_VERSION_TESTED', '6.3.1' );
define( 'WP_ROCKET_PHP_VERSION', '7.3' );
@@ -82,7 +82,7 @@
define( 'CHMOD_WP_ROCKET_CACHE_DIRS', 0755 ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
}
if ( ! defined( 'WP_ROCKET_LASTVERSION' ) ) {
- define( 'WP_ROCKET_LASTVERSION', '3.16.4' );
+ define( 'WP_ROCKET_LASTVERSION', '3.17.3.1' );
}
/**
|