|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the iis7_delete_rewrite_rule() function. |
| 5 | + * |
| 6 | + * @group admin |
| 7 | + * |
| 8 | + * @covers ::iis7_delete_rewrite_rule |
| 9 | + */ |
| 10 | +class Tests_iis7_delete_rewrite_rule extends WP_UnitTestCase { |
| 11 | + |
| 12 | + /** |
| 13 | + * Path to the temporary file. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + private $temp_file; |
| 18 | + |
| 19 | + /** |
| 20 | + * Set up the test environment. |
| 21 | + */ |
| 22 | + public function set_up() { |
| 23 | + parent::set_up(); |
| 24 | + |
| 25 | + require_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 26 | + |
| 27 | + $this->temp_file = wp_tempnam( 'web.config' ); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Clean up the test environment. |
| 32 | + */ |
| 33 | + public function tear_down() { |
| 34 | + if ( file_exists( $this->temp_file ) ) { |
| 35 | + unlink( $this->temp_file ); |
| 36 | + } |
| 37 | + |
| 38 | + parent::tear_down(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Tests that iis7_delete_rewrite_rule() correctly deletes WordPress rules. |
| 43 | + * |
| 44 | + * @ticket 65170 |
| 45 | + * |
| 46 | + * @dataProvider data_iis7_delete_rewrite_rule |
| 47 | + * |
| 48 | + * @param string $initial_content The initial XML content of the file. |
| 49 | + * @param string $expected_content The expected XML content after deletion. |
| 50 | + */ |
| 51 | + public function test_iis7_delete_rewrite_rule( $initial_content, $expected_content ) { |
| 52 | + file_put_contents( $this->temp_file, $initial_content ); |
| 53 | + |
| 54 | + $result = iis7_delete_rewrite_rule( $this->temp_file ); |
| 55 | + |
| 56 | + $this->assertTrue( $result, 'iis7_delete_rewrite_rule() should return true on success.' ); |
| 57 | + |
| 58 | + if ( '' === $expected_content ) { |
| 59 | + $this->assertStringEqualsFile( $this->temp_file, '', 'The file should be empty.' ); |
| 60 | + } else { |
| 61 | + $this->assertXmlStringEqualsXmlFile( $this->temp_file, $expected_content, 'The XML content should match the expected output.' ); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Data provider for test_iis7_delete_rewrite_rule. |
| 67 | + * |
| 68 | + * @return array[] |
| 69 | + */ |
| 70 | + public function data_iis7_delete_rewrite_rule(): array { |
| 71 | + return array( |
| 72 | + 'Rule with name "wordpress" exists' => array( |
| 73 | + 'initial_content' => '<configuration><system.webServer><rewrite><rules><rule name="wordpress" /></rules></rewrite></system.webServer></configuration>', |
| 74 | + 'expected_content' => '<configuration><system.webServer><rewrite><rules/></rewrite></system.webServer></configuration>', |
| 75 | + ), |
| 76 | + 'Rule with name starting with "wordpress" exists' => array( |
| 77 | + 'initial_content' => '<configuration><system.webServer><rewrite><rules><rule name="wordpress-rules" /></rules></rewrite></system.webServer></configuration>', |
| 78 | + 'expected_content' => '<configuration><system.webServer><rewrite><rules/></rewrite></system.webServer></configuration>', |
| 79 | + ), |
| 80 | + 'Rule with name "WordPress" exists' => array( |
| 81 | + 'initial_content' => '<configuration><system.webServer><rewrite><rules><rule name="WordPress" /></rules></rewrite></system.webServer></configuration>', |
| 82 | + 'expected_content' => '<configuration><system.webServer><rewrite><rules/></rewrite></system.webServer></configuration>', |
| 83 | + ), |
| 84 | + 'Multiple rules, only WordPress rule is deleted' => array( |
| 85 | + 'initial_content' => '<configuration><system.webServer><rewrite><rules><rule name="other-rule" /><rule name="wordpress" /></rules></rewrite></system.webServer></configuration>', |
| 86 | + 'expected_content' => '<configuration><system.webServer><rewrite><rules><rule name="other-rule" /></rules></rewrite></system.webServer></configuration>', |
| 87 | + ), |
| 88 | + 'WordPress rule is first among multiple rules' => array( |
| 89 | + 'initial_content' => '<configuration><system.webServer><rewrite><rules><rule name="wordpress" /><rule name="other-rule" /></rules></rewrite></system.webServer></configuration>', |
| 90 | + 'expected_content' => '<configuration><system.webServer><rewrite><rules><rule name="other-rule" /></rules></rewrite></system.webServer></configuration>', |
| 91 | + ), |
| 92 | + 'No WordPress rule exists' => array( |
| 93 | + 'initial_content' => '<configuration><system.webServer><rewrite><rules><rule name="other-rule" /></rules></rewrite></system.webServer></configuration>', |
| 94 | + 'expected_content' => '<configuration><system.webServer><rewrite><rules><rule name="other-rule" /></rules></rewrite></system.webServer></configuration>', |
| 95 | + ), |
| 96 | + 'Empty rules tag' => array( |
| 97 | + 'initial_content' => '<configuration><system.webServer><rewrite><rules/></rewrite></system.webServer></configuration>', |
| 98 | + 'expected_content' => '<configuration><system.webServer><rewrite><rules/></rewrite></system.webServer></configuration>', |
| 99 | + ), |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Tests that iis7_delete_rewrite_rule() returns true if the file does not exist. |
| 105 | + * |
| 106 | + * @ticket 65170 |
| 107 | + */ |
| 108 | + public function test_iis7_delete_rewrite_rule_non_existent_file() { |
| 109 | + $non_existent_file = dirname( $this->temp_file ) . '/non-existent-web.config'; |
| 110 | + if ( file_exists( $non_existent_file ) ) { |
| 111 | + unlink( $non_existent_file ); |
| 112 | + } |
| 113 | + |
| 114 | + $this->assertTrue( iis7_delete_rewrite_rule( $non_existent_file ), 'Should return true if the file does not exist.' ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Tests that iis7_delete_rewrite_rule() returns false for invalid XML. |
| 119 | + * |
| 120 | + * @ticket 65170 |
| 121 | + */ |
| 122 | + public function test_iis7_delete_rewrite_rule_invalid_xml() { |
| 123 | + file_put_contents( $this->temp_file, 'Not XML' ); |
| 124 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 125 | + $this->assertFalse( @iis7_delete_rewrite_rule( $this->temp_file ), 'Should return false for invalid XML.' ); |
| 126 | + } |
| 127 | +} |
0 commit comments