-
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
Open
pbearne
wants to merge
6
commits into
WordPress:trunk
Choose a base branch
from
pbearne:65135-misc-got_url_rewrite
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−0
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
30e825d
Add unit tests for got_url_rewrite() in src/wp-admin/includes/misc.php
3deedc0
fixed white space
1b979a2
Declare return type for data_got_url_rewrite() in unit tests
3cc22a8
Merge branch 'trunk' into 65135-misc-got_url_rewrite
pbearne 12b5972
Fix formatting of docblock in gotUrlRewrite.php
pbearne 58f6e5b
Apply suggestions from code review
pbearne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
tests/phpunit/tests/admin/includes/misc/gotUrlRewrite.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Tests for the got_url_rewrite() function. | ||
| * | ||
| * @group admin | ||
| * @group rewrite | ||
| * | ||
| * @covers ::got_url_rewrite | ||
| */ | ||
| class Tests_got_url_rewrite extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Saved value of the $is_nginx global. | ||
| * @var bool | ||
| */ | ||
| private $prev_nginx; | ||
|
|
||
| /** | ||
| * Saved value of the $is_caddy global. | ||
| * @var bool | ||
| */ | ||
| private $prev_caddy; | ||
|
pbearne marked this conversation as resolved.
Outdated
|
||
|
|
||
| public function set_up() { | ||
| parent::set_up(); | ||
| global $is_nginx, $is_caddy; | ||
| $this->prev_nginx = $is_nginx; | ||
| $this->prev_caddy = $is_caddy; | ||
| } | ||
|
|
||
| public function tear_down() { | ||
| global $is_nginx, $is_caddy; | ||
| $is_nginx = $this->prev_nginx; | ||
| $is_caddy = $this->prev_caddy; | ||
| parent::tear_down(); | ||
| } | ||
|
|
||
| /** | ||
| * 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_val Value for the $is_nginx global. | ||
| * @param bool $is_caddy_val 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 ) { | ||
|
pbearne marked this conversation as resolved.
|
||
| global $is_nginx, $is_caddy; | ||
|
|
||
| $is_nginx = $is_nginx_val; | ||
| $is_caddy = $is_caddy_val; | ||
|
|
||
| // 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' ); | ||
| } | ||
|
pbearne marked this conversation as resolved.
Outdated
pbearne marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| * } | ||
| * | ||
| * @phpstan-return array<string, array{ | ||
|
pbearne marked this conversation as resolved.
Outdated
|
||
| * expected: bool, | ||
| * mod_rewrite: bool, | ||
| * is_nginx: bool, | ||
| * is_caddy: bool, | ||
| * iis7_perm: bool, | ||
| * filter_val?: bool|null, | ||
| * }> | ||
| */ | ||
| public function data_got_url_rewrite(): array { | ||
| 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, | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.