|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the got_url_rewrite() function. |
| 5 | + * |
| 6 | + * @group admin |
| 7 | + * @group rewrite |
| 8 | + * |
| 9 | + * @covers ::got_url_rewrite |
| 10 | + */ |
| 11 | +class Tests_got_url_rewrite extends WP_UnitTestCase { |
| 12 | + |
| 13 | + /** |
| 14 | + * Tests that got_url_rewrite() correctly detects URL rewrite support based on server and filters. |
| 15 | + * |
| 16 | + * @ticket 65135 |
| 17 | + * |
| 18 | + * @dataProvider data_got_url_rewrite |
| 19 | + * |
| 20 | + * @param bool $expected The expected result from got_url_rewrite(). |
| 21 | + * @param bool $mod_rewrite Whether mod_rewrite is reported as supported. |
| 22 | + * @param bool $is_nginx Value for the $is_nginx global. |
| 23 | + * @param bool $is_caddy Value for the $is_caddy global. |
| 24 | + * @param bool $iis7_perm Whether IIS7 supports permalinks (simulated). |
| 25 | + * @param bool|null $filter_val Optional value for the 'got_url_rewrite' filter. |
| 26 | + */ |
| 27 | + public function test_got_url_rewrite( $expected, $mod_rewrite, $is_nginx, $is_caddy, $iis7_perm, $filter_val = null ) { |
| 28 | + global $is_nginx, $is_caddy; |
| 29 | + |
| 30 | + // Backup globals. |
| 31 | + $prev_nginx = $is_nginx; |
| 32 | + $prev_caddy = $is_caddy; |
| 33 | + |
| 34 | + $is_nginx = $is_nginx; |
| 35 | + $is_caddy = $is_caddy; |
| 36 | + |
| 37 | + // Mock got_mod_rewrite and iis7_supports_permalinks via filters if possible. |
| 38 | + // However, got_url_rewrite calls got_mod_rewrite() which calls apache_mod_loaded. |
| 39 | + // We can use the 'got_rewrite' filter to control got_mod_rewrite()'s output. |
| 40 | + add_filter( 'got_rewrite', $mod_rewrite ? '__return_true' : '__return_false' ); |
| 41 | + |
| 42 | + // iis7_supports_permalinks() uses iis7_rewrite_rule_exists() which checks files. |
| 43 | + // For simplicity, if we are on a non-IIS environment, it returns false. |
| 44 | + // If we need to test IIS path, we might need more complex mocking or just rely on the final filter. |
| 45 | + |
| 46 | + if ( null !== $filter_val ) { |
| 47 | + add_filter( 'got_url_rewrite', $filter_val ? '__return_true' : '__return_false' ); |
| 48 | + } |
| 49 | + |
| 50 | + $this->assertSame( $expected, got_url_rewrite() ); |
| 51 | + |
| 52 | + // Cleanup. |
| 53 | + remove_filter( 'got_rewrite', $mod_rewrite ? '__return_true' : '__return_false' ); |
| 54 | + if ( null !== $filter_val ) { |
| 55 | + remove_filter( 'got_url_rewrite', $filter_val ? '__return_true' : '__return_false' ); |
| 56 | + } |
| 57 | + $is_nginx = $prev_nginx; |
| 58 | + $is_caddy = $prev_caddy; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Data provider for test_got_url_rewrite. |
| 63 | + * |
| 64 | + * @return array[] { |
| 65 | + * @type bool $expected The expected result. |
| 66 | + * @type bool $mod_rewrite Whether mod_rewrite is supported. |
| 67 | + * @type bool $is_nginx Whether server is nginx. |
| 68 | + * @type bool $is_caddy Whether server is Caddy. |
| 69 | + * @type bool $iis7_perm Whether IIS7 supports permalinks. |
| 70 | + * @type bool|null $filter_val Optional filter value. |
| 71 | + * } |
| 72 | + */ |
| 73 | + public function data_got_url_rewrite() { |
| 74 | + return array( |
| 75 | + 'All false' => array( |
| 76 | + 'expected' => false, |
| 77 | + 'mod_rewrite' => false, |
| 78 | + 'is_nginx' => false, |
| 79 | + 'is_caddy' => false, |
| 80 | + 'iis7_perm' => false, |
| 81 | + ), |
| 82 | + 'Apache mod_rewrite supported' => array( |
| 83 | + 'expected' => true, |
| 84 | + 'mod_rewrite' => true, |
| 85 | + 'is_nginx' => false, |
| 86 | + 'is_caddy' => false, |
| 87 | + 'iis7_perm' => false, |
| 88 | + ), |
| 89 | + 'Nginx supported' => array( |
| 90 | + 'expected' => true, |
| 91 | + 'mod_rewrite' => false, |
| 92 | + 'is_nginx' => true, |
| 93 | + 'is_caddy' => false, |
| 94 | + 'iis7_perm' => false, |
| 95 | + ), |
| 96 | + 'Caddy supported' => array( |
| 97 | + 'expected' => true, |
| 98 | + 'mod_rewrite' => false, |
| 99 | + 'is_nginx' => false, |
| 100 | + 'is_caddy' => true, |
| 101 | + 'iis7_perm' => false, |
| 102 | + ), |
| 103 | + 'Filter overrides to true' => array( |
| 104 | + 'expected' => true, |
| 105 | + 'mod_rewrite' => false, |
| 106 | + 'is_nginx' => false, |
| 107 | + 'is_caddy' => false, |
| 108 | + 'iis7_perm' => false, |
| 109 | + 'filter_val' => true, |
| 110 | + ), |
| 111 | + 'Filter overrides to false' => array( |
| 112 | + 'expected' => false, |
| 113 | + 'mod_rewrite' => true, |
| 114 | + 'is_nginx' => true, |
| 115 | + 'is_caddy' => true, |
| 116 | + 'iis7_perm' => true, |
| 117 | + 'filter_val' => false, |
| 118 | + ), |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
0 commit comments