Skip to content

Commit afaf3ce

Browse files
committed
Add test assertions for od_get_disabled_reasons
1 parent 2f24014 commit afaf3ce

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

plugins/optimization-detective/tests/test-optimization.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,20 @@ public function data_provider_test_od_maybe_add_template_output_buffer_filter():
178178
'expected_has_filter' => true,
179179
),
180180
'search_enabled_by_filter_using_flags' => array(
181-
'set_up' => static function (): string {
181+
'set_up' => function (): string {
182182
// This is needed because otherwise no_cache_purge_post_id will be true.
183183
self::factory()->post->create( array( 'post_title' => 'foo' ) );
184184

185185
add_filter(
186186
'od_can_optimize_response',
187-
static function ( $can_optimize, array $disabled_flags ): bool {
187+
function ( $can_optimize, array $disabled_flags ): bool {
188+
$expected_keys = array( 'is_search', 'is_embed', 'is_preview', 'is_customize_preview', 'not_get_request', 'no_cache_purge_post_id' );
189+
$this->assertCount( count( $expected_keys ), $disabled_flags );
190+
foreach ( $expected_keys as $key ) {
191+
$this->assertArrayHasKey( $key, $disabled_flags );
192+
$this->assertIsBool( $disabled_flags[ $key ] );
193+
}
194+
188195
if ( ! $can_optimize && $disabled_flags['is_search'] ) {
189196
unset( $disabled_flags['is_search'] );
190197
$can_optimize = count( array_filter( $disabled_flags ) ) === 0;
@@ -382,6 +389,27 @@ public function test_od_can_optimize_response( Closure $set_up, bool $expected )
382389
$url = $set_up();
383390
$this->go_to( $url );
384391
$this->assertSame( $expected, od_can_optimize_response() );
392+
$disabled_reasons = od_get_disabled_reasons();
393+
$possible_keys = array(
394+
'is_search',
395+
'is_embed',
396+
'is_preview',
397+
'is_customize_preview',
398+
'not_get_request',
399+
'no_cache_purge_post_id',
400+
'filter_disabled',
401+
'rest_api_unavailable',
402+
'query_param_disabled',
403+
);
404+
foreach ( $disabled_reasons as $key => $reason ) {
405+
$this->assertContains( $key, $possible_keys );
406+
$this->assertIsString( $reason );
407+
}
408+
if ( $expected ) {
409+
$this->assertCount( 0, $disabled_reasons );
410+
} else {
411+
$this->assertNotCount( 0, $disabled_reasons );
412+
}
385413
}
386414

387415
/**

0 commit comments

Comments
 (0)