Skip to content
76 changes: 76 additions & 0 deletions tests/phpunit/tests/admin/includes/misc/gotModRewrite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* Tests for the got_mod_rewrite() function.
*
* @group admin
* @group rewrite
*
* @covers ::got_mod_rewrite
*/
class Tests_got_mod_rewrite extends WP_UnitTestCase {

/**
* Tests that got_mod_rewrite() correctly detects mod_rewrite based on server and filters.
*
* @ticket 65134
*
* @dataProvider data_got_mod_rewrite
*
* @param bool $expected The expected result from got_mod_rewrite().
* @param bool $apache_loaded Whether mod_rewrite is reported as loaded by Apache.
* @param bool|null $filter_value Optional value to return via the 'got_rewrite' filter.
*/
public function test_got_mod_rewrite( $expected, $apache_loaded, $filter_value = null ) {
// Mock the Apache module check by filtering 'got_rewrite' if needed,
// but since got_mod_rewrite calls apache_mod_loaded which we can't easily mock
// without a framework, we rely on the filter for full control.

if ( null !== $filter_value ) {
add_filter( 'got_rewrite', function() use ( $filter_value ) { return $filter_value; } );

Check failure on line 30 in tests/phpunit/tests/admin/includes/misc/gotModRewrite.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Each PHP statement must be on a line by itself

Check failure on line 30 in tests/phpunit/tests/admin/includes/misc/gotModRewrite.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Spaces must be used for mid-line alignment; tabs are not allowed

Check failure on line 30 in tests/phpunit/tests/admin/includes/misc/gotModRewrite.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Opening brace must be the last content on the line

Check failure on line 30 in tests/phpunit/tests/admin/includes/misc/gotModRewrite.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Expected 1 space after FUNCTION keyword; 0 found
Comment thread
pbearne marked this conversation as resolved.
Outdated
}

// If we are NOT filtering, we need to be aware of the environment.
// However, the function's internal logic is:
// return apply_filters( 'got_rewrite', apache_mod_loaded( 'mod_rewrite', true ) );
// Since we want to test the function's behavior across different scenarios,
// we use the filter to simulate the different outcomes of the internal check.

$this->assertSame( $expected, got_mod_rewrite() );
}

/**
* Data provider for test_got_mod_rewrite.
*
* @return array[] {
* @type bool $expected The expected result.
* @type bool $apache_loaded Whether mod_rewrite is loaded (simulated via filter).
* @type bool|null $filter_value The value to return from the filter.
* }
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 shape syntax since there's no need to target WordPress Developer docs.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not sure of the ask here
So I asked AI to help
Did it get it right?

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.

Yeah. That looks right. We can remove the old one though. Added a suggestion.

*
* @phpstan-return array<string, array{
* expected: bool,
* apache_loaded: bool,
* filter_value: bool|null,
* }>
Comment thread
pbearne marked this conversation as resolved.
Outdated
*/
public function data_got_mod_rewrite(): array {
return array(
'Default behavior (should match filter or internal check)' => array(
'expected' => true,
'apache_loaded' => true,
'filter_value' => true,
),
'Filter returns false' => array(
'expected' => false,
'apache_loaded' => true,
'filter_value' => false,
),
'Filter returns true even if Apache check might be false' => array(
'expected' => true,
'apache_loaded' => false,
'filter_value' => true,
),
);
}
}
Loading