Skip to content

Commit 5bcfa81

Browse files
committed
Tests: Add coverage for settings_fields().
Verify settings group and action fields, attribute escaping, group-specific nonces, and the referer field. Developed in: #14625 Props shailu25, huzaifaalmesbah. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64809 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 3393393 commit 5bcfa81

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
/**
4+
* Tests for settings_fields().
5+
*
6+
* @group admin
7+
* @group plugins
8+
*
9+
* @covers ::settings_fields
10+
*/
11+
class Tests_Admin_Includes_Plugin_SettingsFields extends WP_UnitTestCase {
12+
13+
public function set_up() {
14+
parent::set_up();
15+
16+
require_once ABSPATH . 'wp-admin/includes/plugin.php';
17+
}
18+
19+
/**
20+
* @ticket 65819
21+
*
22+
* @dataProvider data_option_groups
23+
*
24+
* @param string $option_group Settings group name.
25+
*/
26+
public function test_outputs_option_page_and_action_fields( $option_group ) {
27+
ob_start();
28+
settings_fields( $option_group );
29+
$output = ob_get_clean();
30+
31+
$this->assertStringContainsString(
32+
"<input type='hidden' name='option_page' value='" . $option_group . "' />",
33+
$output,
34+
'Should output the option_page hidden field.'
35+
);
36+
$this->assertStringContainsString(
37+
'<input type="hidden" name="action" value="update" />',
38+
$output,
39+
'Should output the action hidden field.'
40+
);
41+
}
42+
43+
/**
44+
* Data provider.
45+
*
46+
* @return array[]
47+
*/
48+
public static function data_option_groups() {
49+
return array(
50+
'general' => array( 'general' ),
51+
'discussion' => array( 'discussion' ),
52+
'custom group' => array( 'my_plugin_settings' ),
53+
'empty string' => array( '' ),
54+
);
55+
}
56+
57+
/**
58+
* @ticket 65819
59+
*/
60+
public function test_escapes_option_group_in_option_page_value() {
61+
$option_group = 'general&"\'<>';
62+
63+
ob_start();
64+
settings_fields( $option_group );
65+
$output = ob_get_clean();
66+
67+
$this->assertStringContainsString(
68+
"<input type='hidden' name='option_page' value='general&amp;&quot;&#039;&lt;&gt;' />",
69+
$output,
70+
'option_page value should escape special characters for an HTML attribute.'
71+
);
72+
$this->assertStringNotContainsString(
73+
'value=\'general&"\'',
74+
$output,
75+
'Unescaped special characters should not appear in the option_page value.'
76+
);
77+
}
78+
79+
/**
80+
* @ticket 65819
81+
*
82+
* @dataProvider data_nonce_option_groups
83+
*
84+
* @param string $option_group Settings group name.
85+
*/
86+
public function test_outputs_valid_nonce_for_option_group( $option_group ) {
87+
ob_start();
88+
settings_fields( $option_group );
89+
$output = ob_get_clean();
90+
91+
$this->assertMatchesRegularExpression(
92+
'/<input type="hidden" id="_wpnonce" name="_wpnonce" value="[^"]+" \/>/',
93+
$output,
94+
'Should output the _wpnonce field.'
95+
);
96+
97+
preg_match( '/name="_wpnonce" value="([^"]+)"/', $output, $matches );
98+
99+
$this->assertNotEmpty( $matches[1], 'Nonce value should be present.' );
100+
$this->assertNotFalse(
101+
wp_verify_nonce( $matches[1], $option_group . '-options' ),
102+
'Nonce should verify for the "{option_group}-options" action.'
103+
);
104+
}
105+
106+
/**
107+
* Data provider for nonce action coverage across option groups.
108+
*
109+
* @return array[]
110+
*/
111+
public static function data_nonce_option_groups() {
112+
return array(
113+
'general' => array( 'general' ),
114+
'reading' => array( 'reading' ),
115+
);
116+
}
117+
118+
/**
119+
* @ticket 65819
120+
*/
121+
public function test_outputs_referer_field() {
122+
ob_start();
123+
settings_fields( 'general' );
124+
$output = ob_get_clean();
125+
126+
$this->assertStringContainsString(
127+
'name="_wp_http_referer"',
128+
$output,
129+
'Should include the referer field from wp_nonce_field().'
130+
);
131+
}
132+
}

0 commit comments

Comments
 (0)