|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the iis7_rewrite_rule_exists() function. |
| 5 | + * |
| 6 | + * @group admin |
| 7 | + * |
| 8 | + * @covers ::iis7_rewrite_rule_exists |
| 9 | + */ |
| 10 | +class Tests_iis7_rewrite_rule_exists extends WP_UnitTestCase { |
| 11 | + |
| 12 | + /** |
| 13 | + * Path to the temporary file. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + private $temp_file; |
| 18 | + |
| 19 | + public function set_up() { |
| 20 | + parent::set_up(); |
| 21 | + |
| 22 | + require_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 23 | + |
| 24 | + $this->temp_file = wp_tempnam( 'web.config' ); |
| 25 | + } |
| 26 | + |
| 27 | + public function tear_down() { |
| 28 | + if ( file_exists( $this->temp_file ) ) { |
| 29 | + unlink( $this->temp_file ); |
| 30 | + } |
| 31 | + |
| 32 | + parent::tear_down(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Tests that iis7_rewrite_rule_exists() correctly identifies the existence of rules. |
| 37 | + * |
| 38 | + * @ticket 65148 |
| 39 | + * |
| 40 | + * @dataProvider data_iis7_rewrite_rule_exists |
| 41 | + * |
| 42 | + * @phpstan-return array<string, array{expected: bool, content: string}> |
| 43 | + * |
| 44 | + * @param bool $expected Whether the rule is expected to exist. |
| 45 | + * @param string $content The XML content of the file. |
| 46 | + */ |
| 47 | + public function test_iis7_rewrite_rule_exists( $expected, $content ) { |
| 48 | + if ( 'Not XML' === $content ) { |
| 49 | + @file_put_contents( $this->temp_file, $content ); |
| 50 | + $this->assertSame( $expected, @iis7_rewrite_rule_exists( $this->temp_file ) ); |
| 51 | + } else { |
| 52 | + file_put_contents( $this->temp_file, $content ); |
| 53 | + $this->assertSame( $expected, iis7_rewrite_rule_exists( $this->temp_file ) ); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Data provider for test_iis7_rewrite_rule_exists. |
| 59 | + * |
| 60 | + * @return array<string, array{expected: bool, content: string}> |
| 61 | + */ |
| 62 | + public function data_iis7_rewrite_rule_exists() { |
| 63 | + return array( |
| 64 | + 'Rule with name "wordpress" exists' => array( |
| 65 | + 'expected' => true, |
| 66 | + 'content' => '<configuration><system.webServer><rewrite><rules><rule name="wordpress" /></rules></rewrite></system.webServer></configuration>', |
| 67 | + ), |
| 68 | + 'Rule with name "WordPress" exists' => array( |
| 69 | + 'expected' => true, |
| 70 | + 'content' => '<configuration><system.webServer><rewrite><rules><rule name="WordPress" /></rules></rewrite></system.webServer></configuration>', |
| 71 | + ), |
| 72 | + 'Rule with name starting with "wordpress" exists' => array( |
| 73 | + 'expected' => true, |
| 74 | + 'content' => '<configuration><system.webServer><rewrite><rules><rule name="wordpress-rules" /></rules></rewrite></system.webServer></configuration>', |
| 75 | + ), |
| 76 | + 'Rule does not exist' => array( |
| 77 | + 'expected' => false, |
| 78 | + 'content' => '<configuration><system.webServer><rewrite><rules><rule name="other-rule" /></rules></rewrite></system.webServer></configuration>', |
| 79 | + ), |
| 80 | + 'Empty configuration' => array( |
| 81 | + 'expected' => false, |
| 82 | + 'content' => '<configuration />', |
| 83 | + ), |
| 84 | + 'Invalid XML' => array( |
| 85 | + 'expected' => false, |
| 86 | + 'content' => 'Not XML', |
| 87 | + ), |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Tests that iis7_rewrite_rule_exists() returns false if the file does not exist. |
| 93 | + * |
| 94 | + * @ticket 65148 |
| 95 | + */ |
| 96 | + public function test_iis7_rewrite_rule_exists_non_existent_file() { |
| 97 | + $this->assertFalse( iis7_rewrite_rule_exists( '/non/existent/file' ) ); |
| 98 | + } |
| 99 | +} |
0 commit comments