Skip to content

Commit 5307e2d

Browse files
Tests: Add unit tests for wp_admin_canonical_url().
Follow-up to [31736]. Props pbearne. Fixes #65192. git-svn-id: https://develop.svn.wordpress.org/trunk@62371 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5a96ff4 commit 5307e2d

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
/**
4+
* @group admin
5+
*
6+
* @covers ::wp_admin_canonical_url
7+
*/
8+
class Tests_Admin_Includes_Misc_WpAdminCanonicalUrl_Test extends WP_UnitTestCase {
9+
10+
/**
11+
* Original `$_SERVER` values.
12+
*
13+
* @var array
14+
*/
15+
private $server_orig;
16+
17+
public function set_up() {
18+
parent::set_up();
19+
20+
$this->server_orig = $_SERVER;
21+
}
22+
23+
public function tear_down() {
24+
$_SERVER = $this->server_orig;
25+
26+
parent::tear_down();
27+
}
28+
29+
/**
30+
* Tests wp_admin_canonical_url().
31+
*
32+
* @ticket 65192
33+
*
34+
* @dataProvider data_wp_admin_canonical_url
35+
*
36+
* @param array $server_vars `$_SERVER` variables to set.
37+
* @param string $expected Expected output substring.
38+
*/
39+
public function test_wp_admin_canonical_url( array $server_vars, $expected ) {
40+
foreach ( $server_vars as $key => $value ) {
41+
$_SERVER[ $key ] = $value;
42+
}
43+
44+
ob_start();
45+
wp_admin_canonical_url();
46+
$output = ob_get_clean();
47+
48+
$this->assertStringContainsString( $expected, $output );
49+
$this->assertStringContainsString( '<script>', $output );
50+
}
51+
52+
/**
53+
* Data provider for test_wp_admin_canonical_url().
54+
*
55+
* @return array<string, array{
56+
* server_vars: array<string, string>,
57+
* expected: string
58+
* }>
59+
*/
60+
public function data_wp_admin_canonical_url(): array {
61+
return array(
62+
'no removable query args' => array(
63+
'server_vars' => array(
64+
'HTTP_HOST' => 'example.org',
65+
'REQUEST_URI' => '/wp-admin/index.php',
66+
),
67+
'expected' => 'href="http://example.org/wp-admin/index.php"',
68+
),
69+
'removable query args' => array(
70+
'server_vars' => array(
71+
'HTTP_HOST' => 'example.org',
72+
'REQUEST_URI' => '/wp-admin/index.php?settings-updated=true&other=arg',
73+
),
74+
'expected' => 'href="http://example.org/wp-admin/index.php?other=arg"',
75+
),
76+
'multiple removable query args' => array(
77+
'server_vars' => array(
78+
'HTTP_HOST' => 'example.org',
79+
'REQUEST_URI' => '/wp-admin/edit.php?trashed=1&locked=1&paged=2',
80+
),
81+
'expected' => 'href="http://example.org/wp-admin/edit.php?paged=2"',
82+
),
83+
'https' => array(
84+
'server_vars' => array(
85+
'HTTP_HOST' => 'example.org',
86+
'REQUEST_URI' => '/wp-admin/index.php',
87+
'HTTPS' => 'on',
88+
),
89+
'expected' => 'href="https://example.org/wp-admin/index.php"',
90+
),
91+
);
92+
}
93+
94+
/**
95+
* Tests wp_admin_canonical_url() when removable query args are filtered to be empty.
96+
*
97+
* @ticket 65192
98+
*/
99+
public function test_wp_admin_canonical_url_with_empty_removable_args() {
100+
add_filter( 'removable_query_args', '__return_empty_array' );
101+
102+
ob_start();
103+
wp_admin_canonical_url();
104+
$output = ob_get_clean();
105+
106+
remove_filter( 'removable_query_args', '__return_empty_array' );
107+
108+
$this->assertEmpty( $output, 'Output should be empty when removable query args are filtered to be empty.' );
109+
}
110+
111+
/**
112+
* Tests the `wp_admin_canonical_url` filter.
113+
*
114+
* @ticket 65192
115+
*/
116+
public function test_wp_admin_canonical_url_filter() {
117+
$_SERVER['HTTP_HOST'] = 'example.org';
118+
$_SERVER['REQUEST_URI'] = '/wp-admin/index.php?settings-updated=true';
119+
120+
add_action(
121+
'wp_admin_canonical_url',
122+
static function () {
123+
return 'https://custom.example.org/canonical';
124+
}
125+
);
126+
127+
ob_start();
128+
wp_admin_canonical_url();
129+
$output = ob_get_clean();
130+
131+
$this->assertStringContainsString( 'href="https://custom.example.org/canonical"', $output );
132+
}
133+
}

0 commit comments

Comments
 (0)