Skip to content

Commit 0f781a9

Browse files
author
Paul Bearne
committed
Add unit tests for got_mod_rewrite() in src/wp-admin/includes/misc.php
Reference: https://core.trac.wordpress.org/ticket/65134
1 parent 8270db8 commit 0f781a9

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/**
4+
* Tests for the got_mod_rewrite() function.
5+
*
6+
* @group admin
7+
* @group rewrite
8+
*
9+
* @covers ::got_mod_rewrite
10+
*/
11+
class Tests_got_mod_rewrite extends WP_UnitTestCase {
12+
13+
/**
14+
* Tests that got_mod_rewrite() correctly detects mod_rewrite based on server and filters.
15+
*
16+
* @ticket 65134
17+
*
18+
* @dataProvider data_got_mod_rewrite
19+
*
20+
* @param bool $expected The expected result from got_mod_rewrite().
21+
* @param bool $apache_loaded Whether mod_rewrite is reported as loaded by Apache.
22+
* @param bool|null $filter_value Optional value to return via the 'got_rewrite' filter.
23+
*/
24+
public function test_got_mod_rewrite( $expected, $apache_loaded, $filter_value = null ) {
25+
// Mock the Apache module check by filtering 'got_rewrite' if needed,
26+
// but since got_mod_rewrite calls apache_mod_loaded which we can't easily mock
27+
// without a framework, we rely on the filter for full control.
28+
29+
if ( null !== $filter_value ) {
30+
add_filter( 'got_rewrite', array( $this, 'filter_got_rewrite' ) );
31+
$this->temp_filter_value = $filter_value;
32+
}
33+
34+
// If we are NOT filtering, we need to be aware of the environment.
35+
// However, the function's internal logic is:
36+
// return apply_filters( 'got_rewrite', apache_mod_loaded( 'mod_rewrite', true ) );
37+
// Since we want to test the function's behavior across different scenarios,
38+
// we use the filter to simulate the different outcomes of the internal check.
39+
40+
$this->assertSame( $expected, got_mod_rewrite() );
41+
42+
if ( null !== $filter_value ) {
43+
remove_filter( 'got_rewrite', array( $this, 'filter_got_rewrite' ) );
44+
unset( $this->temp_filter_value );
45+
}
46+
}
47+
48+
/**
49+
* Data provider for test_got_mod_rewrite.
50+
*
51+
* @return array[] {
52+
* @type bool $expected The expected result.
53+
* @type bool $apache_loaded Whether mod_rewrite is loaded (simulated via filter).
54+
* @type bool|null $filter_value The value to return from the filter.
55+
* }
56+
*/
57+
public function data_got_mod_rewrite() {
58+
return array(
59+
'Default behavior (should match filter or internal check)' => array(
60+
'expected' => true,
61+
'apache_loaded' => true,
62+
'filter_value' => true,
63+
),
64+
'Filter returns false' => array(
65+
'expected' => false,
66+
'apache_loaded' => true,
67+
'filter_value' => false,
68+
),
69+
'Filter returns true even if Apache check might be false' => array(
70+
'expected' => true,
71+
'apache_loaded' => false,
72+
'filter_value' => true,
73+
),
74+
);
75+
}
76+
77+
/**
78+
* Temporary property for filter value.
79+
* @var bool
80+
*/
81+
private $temp_filter_value;
82+
83+
/**
84+
* Filter callback for 'got_rewrite'.
85+
*
86+
* @return bool
87+
*/
88+
public function filter_got_rewrite() {
89+
return $this->temp_filter_value;
90+
}
91+
}

0 commit comments

Comments
 (0)