Skip to content

Commit ed6ea73

Browse files
committed
Add tests for new functionality
1 parent d6c9335 commit ed6ea73

File tree

2 files changed

+170
-12
lines changed

2 files changed

+170
-12
lines changed

plugins/speculation-rules/tests/test-speculation-rules-settings.php

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Test_Speculation_Rules_Settings extends WP_UnitTestCase {
1111
* @covers ::plsr_register_setting
1212
* @covers ::plsr_get_mode_labels
1313
* @covers ::plsr_get_eagerness_labels
14+
* @covers ::plsr_get_authentication_labels
1415
* @covers ::plsr_get_setting_default
1516
*/
1617
public function test_plsr_register_setting(): void {
@@ -25,6 +26,7 @@ public function test_plsr_register_setting(): void {
2526
$settings = plsr_get_setting_default();
2627
$this->assertArrayHasKey( 'mode', $settings );
2728
$this->assertArrayHasKey( 'eagerness', $settings );
29+
$this->assertArrayHasKey( 'authentication', $settings );
2830

2931
// Test default settings applied correctly.
3032
$default_settings = plsr_get_setting_default();
@@ -33,6 +35,9 @@ public function test_plsr_register_setting(): void {
3335

3436
/**
3537
* @covers ::plsr_sanitize_setting
38+
* @covers ::plsr_get_mode_labels
39+
* @covers ::plsr_get_eagerness_labels
40+
* @covers ::plsr_get_authentication_labels
3641
* @dataProvider data_plsr_sanitize_setting
3742
*
3843
* @param mixed $input Input.
@@ -54,19 +59,19 @@ public function data_plsr_sanitize_setting(): array {
5459
);
5560

5661
return array(
57-
'invalid type null' => array(
62+
'invalid type null' => array(
5863
null,
5964
$default_value,
6065
),
61-
'invalid type string' => array(
66+
'invalid type string' => array(
6267
'prerender',
6368
$default_value,
6469
),
65-
'missing fields' => array(
70+
'missing fields' => array(
6671
array(),
6772
$default_value,
6873
),
69-
'missing mode' => array(
74+
'missing mode' => array(
7075
array( 'eagerness' => 'conservative' ),
7176
array_merge(
7277
$default_value,
@@ -75,7 +80,7 @@ public function data_plsr_sanitize_setting(): array {
7580
)
7681
),
7782
),
78-
'missing eagerness' => array(
83+
'missing eagerness' => array(
7984
array( 'mode' => 'prefetch' ),
8085
array_merge(
8186
$default_value,
@@ -84,7 +89,7 @@ public function data_plsr_sanitize_setting(): array {
8489
)
8590
),
8691
),
87-
'invalid mode' => array(
92+
'invalid mode' => array(
8893
array(
8994
'mode' => 'something',
9095
'eagerness' => 'eager',
@@ -97,7 +102,7 @@ public function data_plsr_sanitize_setting(): array {
97102
)
98103
),
99104
),
100-
'invalid eagerness' => array(
105+
'invalid eagerness' => array(
101106
array(
102107
'mode' => 'prefetch',
103108
'eagerness' => 'something',
@@ -110,16 +115,57 @@ public function data_plsr_sanitize_setting(): array {
110115
)
111116
),
112117
),
113-
'valid fields' => array(
118+
'invalid authentication' => array(
114119
array(
115-
'mode' => 'prefetch',
116-
'eagerness' => 'conservative',
120+
'authentication' => 'bad',
121+
),
122+
$default_value,
123+
),
124+
'valid auth logged_out' => array(
125+
array(
126+
'authentication' => 'logged_out',
117127
),
118128
array_merge(
119129
$default_value,
120130
array(
121-
'mode' => 'prefetch',
122-
'eagerness' => 'conservative',
131+
'authentication' => 'logged_out',
132+
)
133+
),
134+
),
135+
'valid auth logged_out_or_admins' => array(
136+
array(
137+
'authentication' => 'logged_out_or_admins',
138+
),
139+
array_merge(
140+
$default_value,
141+
array(
142+
'authentication' => 'logged_out_or_admins',
143+
)
144+
),
145+
),
146+
'valid auth any' => array(
147+
array(
148+
'authentication' => 'any',
149+
),
150+
array_merge(
151+
$default_value,
152+
array(
153+
'authentication' => 'any',
154+
)
155+
),
156+
),
157+
'valid fields' => array(
158+
array(
159+
'mode' => 'prefetch',
160+
'eagerness' => 'conservative',
161+
'authentication' => 'logged_out_or_admins',
162+
),
163+
array_merge(
164+
$default_value,
165+
array(
166+
'mode' => 'prefetch',
167+
'eagerness' => 'conservative',
168+
'authentication' => 'logged_out_or_admins',
123169
)
124170
),
125171
),

plugins/speculation-rules/tests/test-speculation-rules.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,118 @@
77

88
class Test_Speculation_Rules extends WP_UnitTestCase {
99

10+
/**
11+
* Data provider for test_plsr_is_speculative_loading_enabled.
12+
*
13+
* @return array<string, mixed>
14+
*/
15+
public function data_provider_to_test_plsr_is_speculative_loading_enabled(): array {
16+
return array(
17+
'default_settings_no_auth_with_pretty_permalinks' => array(
18+
'settings' => null,
19+
'pretty_permalinks' => true,
20+
'current_user_role' => null,
21+
'set_up' => null,
22+
'expected' => true,
23+
),
24+
'default_settings_no_auth_without_pretty_permalinks' => array(
25+
'settings' => null,
26+
'pretty_permalinks' => false,
27+
'current_user_role' => null,
28+
'set_up' => null,
29+
'expected' => false,
30+
),
31+
'default_settings_no_auth_with_pretty_permalinks_filtered_on' => array(
32+
'settings' => null,
33+
'pretty_permalinks' => false,
34+
'current_user_role' => null,
35+
'set_up' => static function (): void {
36+
add_filter( 'plsr_enabled_without_pretty_permalinks', '__return_true' );
37+
},
38+
'expected' => true,
39+
),
40+
'default_settings_auth_admin_auth_with_pretty_permalinks' => array(
41+
'settings' => null,
42+
'pretty_permalinks' => true,
43+
'current_user_role' => 'administrator',
44+
'set_up' => null,
45+
'expected' => false,
46+
),
47+
'default_settings_auth_subscriber_auth_with_pretty_permalinks' => array(
48+
'settings' => null,
49+
'pretty_permalinks' => true,
50+
'current_user_role' => 'subscriber',
51+
'set_up' => null,
52+
'expected' => false,
53+
),
54+
'authentication_any_auth_subscriber_auth_with_pretty_permalinks' => array(
55+
'settings' => array(
56+
'authentication' => 'any',
57+
),
58+
'pretty_permalinks' => true,
59+
'current_user_role' => 'subscriber',
60+
'set_up' => null,
61+
'expected' => true,
62+
),
63+
'authentication_logged_out_or_admins_auth_subscriber_auth_with_pretty_permalinks' => array(
64+
'settings' => array(
65+
'authentication' => 'logged_out_or_admins',
66+
),
67+
'pretty_permalinks' => true,
68+
'current_user_role' => 'subscriber',
69+
'set_up' => null,
70+
'expected' => false,
71+
),
72+
'authentication_logged_out_or_admins_auth_administrator_auth_with_pretty_permalinks' => array(
73+
'settings' => array(
74+
'authentication' => 'logged_out_or_admins',
75+
),
76+
'pretty_permalinks' => true,
77+
'current_user_role' => 'administrator',
78+
'set_up' => null,
79+
'expected' => true,
80+
),
81+
);
82+
}
83+
84+
/**
85+
* @covers ::plsr_is_speculative_loading_enabled
86+
* @covers ::plsr_print_speculation_rules
87+
*
88+
* @dataProvider data_provider_to_test_plsr_is_speculative_loading_enabled
89+
*
90+
* @phpstan-param array<string, mixed> $settings
91+
*/
92+
public function test_plsr_is_speculative_loading_enabled( ?array $settings, bool $pretty_permalinks, ?string $current_user_role, ?Closure $set_up, bool $expected ): void {
93+
if ( $pretty_permalinks ) {
94+
update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%hour%/%minute%/%second%' );
95+
} else {
96+
update_option( 'permalink_structure', '' );
97+
}
98+
if ( null !== $settings ) {
99+
update_option( 'plsr_speculation_rules', plsr_sanitize_setting( $settings ) );
100+
}
101+
102+
if ( null !== $current_user_role ) {
103+
$user_id = self::factory()->user->create( array( 'role' => $current_user_role ) );
104+
wp_set_current_user( $user_id );
105+
}
106+
if ( $set_up instanceof Closure ) {
107+
$set_up();
108+
}
109+
$this->assertSame( $expected, plsr_is_speculative_loading_enabled() );
110+
111+
$output = get_echo( function_exists( 'wp_print_speculation_rules' ) ? 'wp_print_speculation_rules' : 'plsr_print_speculation_rules' );
112+
if ( $expected ) {
113+
$this->assertNotSame( '', $output );
114+
$p = new WP_HTML_Tag_Processor( $output );
115+
$this->assertTrue( $p->next_tag( array( 'tag_name' => 'SCRIPT' ) ) );
116+
$this->assertSame( 'speculationrules', $p->get_attribute( 'type' ) );
117+
} else {
118+
$this->assertSame( '', $output );
119+
}
120+
}
121+
10122
public function test_hooks(): void {
11123
if ( function_exists( 'wp_get_speculation_rules_configuration' ) ) {
12124
$this->assertSame( 10, has_filter( 'wp_speculation_rules_configuration', 'plsr_filter_speculation_rules_configuration' ) );

0 commit comments

Comments
 (0)