Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/wp-admin/includes/misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ function extract_from_markers( $filename, $marker ) {
continue;
}

$result[] = $markerline;
if ( '' !== $markerline ) {
$result[] = $markerline;
}
}

if ( str_contains( $markerline, '# BEGIN ' . $marker ) ) {
Expand Down
91 changes: 91 additions & 0 deletions tests/phpunit/tests/admin/includes/misc/gotModRewrite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?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', array( $this, 'filter_got_rewrite' ) );
$this->temp_filter_value = $filter_value;
}

// 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() );

if ( null !== $filter_value ) {
remove_filter( 'got_rewrite', array( $this, 'filter_got_rewrite' ) );
unset( $this->temp_filter_value );
}
}

/**
* 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.
* }
*/
public function data_got_mod_rewrite() {
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,
),
);
}

/**
* Temporary property for filter value.
* @var bool
*/
private $temp_filter_value;

/**
* Filter callback for 'got_rewrite'.
*
* @return bool
*/
public function filter_got_rewrite() {
return $this->temp_filter_value;
}
}
Loading