Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions tests/phpunit/tests/admin/includes/misc/gotUrlRewrite.php
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 ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function test_got_url_rewrite( $expected, $mod_rewrite, $is_nginx_val, $is_caddy_val, $iis7_perm, $filter_val = null ) {
public function test_got_url_rewrite( bool $expected, bool $mod_rewrite, bool $is_nginx_val, bool $is_caddy_val, bool $iis7_perm, ?bool $filter_val = null ): void {

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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backup should be done in set_up.


// 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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filters are automatically removed after tests run.

Suggested change
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' );
}

$is_nginx = $prev_nginx;
$is_caddy = $prev_caddy;
Comment on lines +57 to +58
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done in tear_down.

}

/**
* 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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function data_got_url_rewrite() {
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,
),
);
}
}
Loading