-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add unit tests for got_url_rewrite() in src/wp-admin/includes/misc.php #11657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,121 @@ | ||||||||||
| <?php | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Tests for the got_url_rewrite() function. | ||||||||||
| * | ||||||||||
| * @group admin | ||||||||||
| * @group rewrite | ||||||||||
| * | ||||||||||
| * @covers ::got_url_rewrite | ||||||||||
| */ | ||||||||||
| class Tests_got_url_rewrite extends WP_UnitTestCase { | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Tests that got_url_rewrite() correctly detects URL rewrite support based on server and filters. | ||||||||||
| * | ||||||||||
| * @ticket 65135 | ||||||||||
| * | ||||||||||
| * @dataProvider data_got_url_rewrite | ||||||||||
| * | ||||||||||
| * @param bool $expected The expected result from got_url_rewrite(). | ||||||||||
| * @param bool $mod_rewrite Whether mod_rewrite is reported as supported. | ||||||||||
| * @param bool $is_nginx Value for the $is_nginx global. | ||||||||||
| * @param bool $is_caddy Value for the $is_caddy global. | ||||||||||
| * @param bool $iis7_perm Whether IIS7 supports permalinks (simulated). | ||||||||||
| * @param bool|null $filter_val Optional value for the 'got_url_rewrite' filter. | ||||||||||
| */ | ||||||||||
| public function test_got_url_rewrite( $expected, $mod_rewrite, $is_nginx_val, $is_caddy_val, $iis7_perm, $filter_val = null ) { | ||||||||||
| global $is_nginx, $is_caddy; | ||||||||||
|
|
||||||||||
| // Backup globals. | ||||||||||
| $prev_nginx = $is_nginx; | ||||||||||
| $prev_caddy = $is_caddy; | ||||||||||
|
|
||||||||||
| $is_nginx = $is_nginx_val; | ||||||||||
| $is_caddy = $is_caddy_val; | ||||||||||
|
Comment on lines
+30
to
+35
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The backup should be done in |
||||||||||
|
|
||||||||||
| // Mock got_mod_rewrite and iis7_supports_permalinks via filters if possible. | ||||||||||
| // However, got_url_rewrite calls got_mod_rewrite() which calls apache_mod_loaded. | ||||||||||
| // We can use the 'got_rewrite' filter to control got_mod_rewrite()'s output. | ||||||||||
| add_filter( 'got_rewrite', $mod_rewrite ? '__return_true' : '__return_false' ); | ||||||||||
|
|
||||||||||
| // iis7_supports_permalinks() uses iis7_rewrite_rule_exists() which checks files. | ||||||||||
| // For simplicity, if we are on a non-IIS environment, it returns false. | ||||||||||
| // If we need to test IIS path, we might need more complex mocking or just rely on the final filter. | ||||||||||
|
|
||||||||||
| if ( null !== $filter_val ) { | ||||||||||
| add_filter( 'got_url_rewrite', $filter_val ? '__return_true' : '__return_false' ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| $this->assertSame( $expected, got_url_rewrite() ); | ||||||||||
|
|
||||||||||
| // Cleanup. | ||||||||||
| remove_filter( 'got_rewrite', $mod_rewrite ? '__return_true' : '__return_false' ); | ||||||||||
| if ( null !== $filter_val ) { | ||||||||||
| remove_filter( 'got_url_rewrite', $filter_val ? '__return_true' : '__return_false' ); | ||||||||||
| } | ||||||||||
|
Comment on lines
+53
to
+56
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Filters are automatically removed after tests run.
Suggested change
|
||||||||||
| $is_nginx = $prev_nginx; | ||||||||||
| $is_caddy = $prev_caddy; | ||||||||||
|
Comment on lines
+57
to
+58
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be done in |
||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Data provider for test_got_url_rewrite. | ||||||||||
| * | ||||||||||
| * @return array[] { | ||||||||||
| * @type bool $expected The expected result. | ||||||||||
| * @type bool $mod_rewrite Whether mod_rewrite is supported. | ||||||||||
| * @type bool $is_nginx Whether server is nginx. | ||||||||||
| * @type bool $is_caddy Whether server is Caddy. | ||||||||||
| * @type bool $iis7_perm Whether IIS7 supports permalinks. | ||||||||||
| * @type bool|null $filter_val Optional filter value. | ||||||||||
| * } | ||||||||||
|
Comment on lines
+64
to
+71
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use PHPStan array shapes since there's no need to target WP Developer Docs |
||||||||||
| */ | ||||||||||
| public function data_got_url_rewrite() { | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| return array( | ||||||||||
| 'All false' => array( | ||||||||||
| 'expected' => false, | ||||||||||
| 'mod_rewrite' => false, | ||||||||||
| 'is_nginx' => false, | ||||||||||
| 'is_caddy' => false, | ||||||||||
| 'iis7_perm' => false, | ||||||||||
| ), | ||||||||||
| 'Apache mod_rewrite supported' => array( | ||||||||||
| 'expected' => true, | ||||||||||
| 'mod_rewrite' => true, | ||||||||||
| 'is_nginx' => false, | ||||||||||
| 'is_caddy' => false, | ||||||||||
| 'iis7_perm' => false, | ||||||||||
| ), | ||||||||||
| 'Nginx supported' => array( | ||||||||||
| 'expected' => true, | ||||||||||
| 'mod_rewrite' => false, | ||||||||||
| 'is_nginx' => true, | ||||||||||
| 'is_caddy' => false, | ||||||||||
| 'iis7_perm' => false, | ||||||||||
| ), | ||||||||||
| 'Caddy supported' => array( | ||||||||||
| 'expected' => true, | ||||||||||
| 'mod_rewrite' => false, | ||||||||||
| 'is_nginx' => false, | ||||||||||
| 'is_caddy' => true, | ||||||||||
| 'iis7_perm' => false, | ||||||||||
| ), | ||||||||||
| 'Filter overrides to true' => array( | ||||||||||
| 'expected' => true, | ||||||||||
| 'mod_rewrite' => false, | ||||||||||
| 'is_nginx' => false, | ||||||||||
| 'is_caddy' => false, | ||||||||||
| 'iis7_perm' => false, | ||||||||||
| 'filter_val' => true, | ||||||||||
| ), | ||||||||||
| 'Filter overrides to false' => array( | ||||||||||
| 'expected' => false, | ||||||||||
| 'mod_rewrite' => true, | ||||||||||
| 'is_nginx' => true, | ||||||||||
| 'is_caddy' => true, | ||||||||||
| 'iis7_perm' => true, | ||||||||||
| 'filter_val' => false, | ||||||||||
| ), | ||||||||||
| ); | ||||||||||
| } | ||||||||||
| } | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.