Skip to content

Commit fce7677

Browse files
committed
Add test for perflab_aao_query_autoloaded_options
1 parent 5ab33b2 commit fce7677

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

plugins/performance-lab/tests/includes/site-health/audit-autoloaded-options/test-audit-autoloaded-options.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,92 @@ static function ( $option ) {
115115
$this->assertSame( $autoloaded_options_size + $expected_size_increase, perflab_aao_autoloaded_options_size() );
116116
}
117117

118+
/**
119+
* Data provider.
120+
*
121+
* @return array<string, mixed>
122+
*/
123+
public function data_provider_test_perflab_aao_query_autoloaded_options(): array {
124+
return array(
125+
'none_over_threshold' => array(
126+
'alloptions' => array(
127+
'foo' => 'bar',
128+
'o100' => str_repeat( 'a', 100 ),
129+
),
130+
'threshold' => null,
131+
'expected' => array(),
132+
),
133+
'one_over_threshold' => array(
134+
'alloptions' => array(
135+
'foo' => 'bar',
136+
'o101' => str_repeat( 'a', 101 ),
137+
),
138+
'threshold' => null,
139+
'expected' => array( 'o101' ),
140+
),
141+
'three_over_custom_threshold' => array(
142+
'alloptions' => array(
143+
'foo' => 'bar',
144+
'o51' => str_repeat( 'a', 51 ),
145+
'o100' => str_repeat( 'a', 100 ),
146+
'o101' => str_repeat( 'a', 101 ),
147+
'int' => 123,
148+
'bool' => true,
149+
),
150+
'threshold' => 50,
151+
'expected' => array( 'o51', 'o100', 'o101' ),
152+
),
153+
'two_non_scalar_over_threshold' => array(
154+
'alloptions' => array(
155+
'foo' => 'bar',
156+
'array' => array(
157+
'key' => str_repeat( 'a', 101 ),
158+
),
159+
'object' => (object) array(
160+
'key' => str_repeat( 'a', 101 ),
161+
),
162+
),
163+
'threshold' => null,
164+
'expected' => array( 'array', 'object' ),
165+
),
166+
);
167+
}
168+
169+
/**
170+
* Tests perflab_aao_query_autoloaded_options().
171+
*
172+
* @dataProvider data_provider_test_perflab_aao_query_autoloaded_options
173+
* @covers ::perflab_aao_query_autoloaded_options
174+
*
175+
* @param array<string, mixed> $alloptions All options.
176+
* @param int|null $threshold Custom threshold.
177+
* @param string[] $expected Expected option names to be over the threshold.
178+
*/
179+
public function test_perflab_aao_query_autoloaded_options( array $alloptions, ?int $threshold, array $expected ): void {
180+
add_filter(
181+
'pre_wp_load_alloptions',
182+
static function () use ( $alloptions ): array {
183+
return $alloptions;
184+
}
185+
);
186+
187+
if ( isset( $threshold ) ) {
188+
add_filter(
189+
'perflab_aao_autoloaded_options_table_threshold',
190+
static function () use ( $threshold ): int {
191+
return $threshold;
192+
}
193+
);
194+
}
195+
196+
$query = perflab_aao_query_autoloaded_options();
197+
$this->assertCount( count( $expected ), $query );
198+
$this->assertSameSets( $expected, wp_list_pluck( $query, 'option_name' ) );
199+
foreach ( wp_list_pluck( $query, 'option_value_length' ) as $option_value_length ) {
200+
$this->assertIsInt( $option_value_length );
201+
}
202+
}
203+
118204
public function test_perflab_aao_autoloaded_options_disable_revert_functionality(): void {
119205

120206
$user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );

0 commit comments

Comments
 (0)