Skip to content

Commit

Permalink
start fixing integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wordpressfan committed Feb 19, 2025
1 parent fcd2721 commit bc3e7e1
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 31 deletions.
28 changes: 27 additions & 1 deletion inc/Engine/Common/Head/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class Subscriber implements Subscriber_Interface {
*/
public static function get_subscribed_events() {
return [
'rocket_head' => 'print_head_elements',
'rocket_buffer' => [ 'insert_rocket_head', 100000 ],
'rocket_head' => 'print_head_elements',
];
}

Expand Down Expand Up @@ -136,4 +137,29 @@ private function prepare_element( $element ) {

return $open_tag . ' ' . $attributes_html . '>' . $inner_content . $close_tag;
}

/**
* Insert rocket_head into the buffer HTML
*
* @param string $html Buffer HTML.
* @return string
*/
public function insert_rocket_head( $html ) {
if ( empty( $html ) ) {
return $html;
}

$filtered_buffer = preg_replace(
'#</title>#iU',
'</title>' . wpm_apply_filters_typed( 'string', 'rocket_head', '' ),
$html,
1
);

if ( empty( $filtered_buffer ) ) {
return $html;
}

return $filtered_buffer;
}
}
2 changes: 1 addition & 1 deletion inc/Engine/CriticalPath/CriticalCSSSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ public function insert_css_in_head( $items ) {
* @return string
*/
public function get_critical_css_content() {
return $this->critical_css_content;
return $this->should_async_css() ? $this->critical_css_content : '';
}

/**
Expand Down
26 changes: 0 additions & 26 deletions inc/Engine/Optimization/Buffer/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function __construct( Optimization $optimizer ) {
public static function get_subscribed_events() {
return [
'template_redirect' => [ 'start_content_process', 2 ],
'rocket_buffer' => [ 'insert_rocket_head', 100000 ],
];
}

Expand All @@ -45,29 +44,4 @@ public static function get_subscribed_events() {
public function start_content_process() {
return $this->optimizer->maybe_init_process();
}

/**
* Insert rocket_head into the buffer HTML
*
* @param string $html Buffer HTML.
* @return string
*/
public function insert_rocket_head( $html ) {
if ( empty( $html ) ) {
return $html;
}

$filtered_buffer = preg_replace(
'#</title>#iU',
'</title>' . wpm_apply_filters_typed( 'string', 'rocket_head', '' ),
$html,
1
);

if ( empty( $filtered_buffer ) ) {
return $html;
}

return $filtered_buffer;
}
}
2 changes: 1 addition & 1 deletion inc/Engine/Optimization/GoogleFonts/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function optimize( $html ): string {
function ( $font ) use ( $exclusions ) {
return ! $this->is_excluded( $font[0], $exclusions );
}
);
);

$num_fonts = count( $filtered_fonts );

Expand Down
43 changes: 43 additions & 0 deletions tests/Integration/IsolateHookTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,49 @@

protected $original_wp_priorities;

protected function unregisterAllCallbacksExceptMulti( $event_name, $methods = [] ) {
global $wp_filter;
$this->original_wp_filter = $wp_filter[ $event_name ]->callbacks;

foreach ( $methods as $priority => $method_name ) {
foreach ( $this->original_wp_filter[ $priority ] as $key => $config ) {

// Skip if not this tests callback.
if ( substr( $key, - strlen( $method_name ) ) !== $method_name ) {
continue;
}

$wp_filter[ $event_name ]->callbacks = [
$priority => [ $key => $config ],
];
}
}

try {
$wp_hooks = $wp_filter[ $event_name ];
$reflection = new ReflectionClass($wp_hooks);
$priorities_property = $reflection->getProperty('priorities');
$priorities_property->setAccessible(true);
$this->original_wp_priorities = $priorities_property->getValue($wp_hooks);
$priorities = $priorities_property->getValue($wp_hooks);
} catch (ReflectionException $e) {
return;
}

foreach ($this->original_wp_priorities as $priority) {
if ( key_exists($priority, $wp_filter[ $event_name ]->callbacks)) {
continue;
}

$priorities = array_values(array_filter($priorities, function ($current) use ($priority) {
return $current !== $priority;
}));

}

$priorities_property->setValue($wp_hooks, $priorities);
}

protected function unregisterAllCallbacksExcept( $event_name, $method_name, $priority = 10 ) {
global $wp_filter;
$this->original_wp_filter = $wp_filter[ $event_name ]->callbacks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function set_up() {
],
];

$this->unregisterAllCallbacksExcept('rocket_buffer', 'process', 17 );
$this->unregisterAllCallbacksExceptMulti('rocket_buffer', [ 17 => 'process', 100000 => 'insert_rocket_head' ] );
add_filter('rocket_exclude_locally_host_fonts', [ $this, 'exclude_locally_host_fonts' ] ); // @phpstan-ignore-line
}

Expand Down Expand Up @@ -84,7 +84,7 @@ private function doTest( $config, $original, $expected ) {
$actual = apply_filters( 'rocket_buffer', $original );

$this->assertSame(
$this->format_the_html( $expected ),
$this->format_the_html( $expected['html'] ),
$this->format_the_html( $actual )
);
}
Expand Down

0 comments on commit bc3e7e1

Please sign in to comment.