Skip to content

Commit

Permalink
Add private od_print_disabled_reasons() and test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jan 23, 2025
1 parent a6c7cf1 commit e9c0618
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
33 changes: 24 additions & 9 deletions plugins/optimization-detective/optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,13 @@ function od_maybe_add_template_output_buffer_filter(): void {
add_action(
'wp_print_footer_scripts',
static function () use ( $reasons ): void {
foreach ( $reasons as $reason ) {
wp_print_inline_script_tag(
sprintf(
'console.info( %s );',
(string) wp_json_encode( '[Optimization Detective] ' . $reason )
),
array( 'type' => 'module' )
);
}
od_print_disabled_reasons( $reasons );
}
);

Check warning on line 110 in plugins/optimization-detective/optimization.php

View check run for this annotation

Codecov / codecov/patch

plugins/optimization-detective/optimization.php#L105-L110

Added lines #L105 - L110 were not covered by tests
}
return;
}

$callback = 'od_optimize_template_output_buffer';
if (
function_exists( 'perflab_wrap_server_timing' )
Expand All @@ -132,6 +125,28 @@ function_exists( 'perflab_server_timing_use_output_buffer' )
add_filter( 'od_template_output_buffer', $callback );
}

/**
* Prints the reasons why Optimization Detective is not optimizing the current page.
*
* This is only used when WP_DEBUG is enabled.
*
* @since n.e.x.t
* @access private
*
* @param string[] $reasons Reason messages.
*/
function od_print_disabled_reasons( array $reasons ): void {
foreach ( $reasons as $reason ) {
wp_print_inline_script_tag(
sprintf(
'console.info( %s );',
(string) wp_json_encode( '[Optimization Detective] ' . $reason )
),
array( 'type' => 'module' )
);
}
}

/**
* Determines whether the current response can be optimized.
*
Expand Down
19 changes: 19 additions & 0 deletions plugins/optimization-detective/tests/test-optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ public function test_od_maybe_add_template_output_buffer_filter( Closure $set_up
$this->assertSame( $expected_has_filter, has_filter( 'od_template_output_buffer' ) );
}

/**
* Test od_print_disabled_reasons().
*
* @covers ::od_print_disabled_reasons
*/
public function test_od_print_disabled_reasons(): void {
$this->assertSame( '', get_echo( 'od_print_disabled_reasons', array( array() ) ) );
$foo_message = get_echo( 'od_print_disabled_reasons', array( array( 'Foo' ) ) );
$this->assertStringContainsString( '<script', $foo_message );
$this->assertStringContainsString( 'console.info(', $foo_message );
$this->assertStringContainsString( '[Optimization Detective] Foo', $foo_message );

$foo_and_bar_messages = get_echo( 'od_print_disabled_reasons', array( array( 'Foo', 'Bar' ) ) );
$this->assertStringContainsString( '<script', $foo_and_bar_messages );
$this->assertStringContainsString( 'console.info(', $foo_and_bar_messages );
$this->assertStringContainsString( '[Optimization Detective] Foo', $foo_and_bar_messages );
$this->assertStringContainsString( '[Optimization Detective] Bar', $foo_and_bar_messages );
}

/**
* Data provider.
*
Expand Down

0 comments on commit e9c0618

Please sign in to comment.