|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the extract_from_markers() function. |
| 5 | + * |
| 6 | + * @group admin |
| 7 | + * |
| 8 | + * @covers ::extract_from_markers |
| 9 | + */ |
| 10 | +class Tests_extract_from_markers extends WP_UnitTestCase { |
| 11 | + |
| 12 | + /** |
| 13 | + * Path to the temporary file used for testing. |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + private $temp_file; |
| 17 | + |
| 18 | + public function set_up() { |
| 19 | + parent::set_up(); |
| 20 | + $this->temp_file = wp_tempnam( 'markers.txt' ); |
| 21 | + } |
| 22 | + |
| 23 | + public function tear_down() { |
| 24 | + if ( file_exists( $this->temp_file ) ) { |
| 25 | + unlink( $this->temp_file ); |
| 26 | + } |
| 27 | + parent::tear_down(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Tests that extract_from_markers() correctly extracts content between markers. |
| 32 | + * |
| 33 | + * @ticket 65136 |
| 34 | + * |
| 35 | + * @dataProvider data_extract_from_markers |
| 36 | + * |
| 37 | + * @param array $expected The expected lines extracted. |
| 38 | + * @param string $content The content of the file. |
| 39 | + * @param string $marker The marker to extract. |
| 40 | + */ |
| 41 | + public function test_extract_from_markers( $expected, $content, $marker ) { |
| 42 | + file_put_contents( $this->temp_file, $content ); |
| 43 | + |
| 44 | + $this->assertSame( $expected, extract_from_markers( $this->temp_file, $marker ) ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Data provider for test_extract_from_markers. |
| 49 | + * |
| 50 | + * @return array[] |
| 51 | + */ |
| 52 | + public function data_extract_from_markers() { |
| 53 | + return array( |
| 54 | + 'Empty file' => array( |
| 55 | + 'expected' => array(), |
| 56 | + 'content' => '', |
| 57 | + 'marker' => 'WordPress', |
| 58 | + ), |
| 59 | + 'Standard WordPress markers' => array( |
| 60 | + 'expected' => array( 'Line 1', 'Line 2' ), |
| 61 | + 'content' => "Outside\n# BEGIN WordPress\nLine 1\nLine 2\n# END WordPress\nOutside", |
| 62 | + 'marker' => 'WordPress', |
| 63 | + ), |
| 64 | + 'Comments inside markers are ignored' => array( |
| 65 | + 'expected' => array( 'Real Line' ), |
| 66 | + 'content' => "# BEGIN Test\n# Comment\nReal Line\n# END Test", |
| 67 | + 'marker' => 'Test', |
| 68 | + ), |
| 69 | + 'Multiple marker blocks' => array( |
| 70 | + 'expected' => array( 'Block 1', 'Block 2' ), |
| 71 | + 'content' => "# BEGIN X\nBlock 1\n# END X\n# BEGIN X\nBlock 2\n# END X", |
| 72 | + 'marker' => 'X', |
| 73 | + ), |
| 74 | + 'Marker not found' => array( |
| 75 | + 'expected' => array(), |
| 76 | + 'content' => "Some content\nwithout markers", |
| 77 | + 'marker' => 'WordPress', |
| 78 | + ), |
| 79 | + 'Partial markers' => array( |
| 80 | + 'expected' => array( 'Partial' ), |
| 81 | + 'content' => "# BEGIN Y\nPartial", |
| 82 | + 'marker' => 'Y', |
| 83 | + ), |
| 84 | + 'Full .htaccess example' => array( |
| 85 | + 'expected' => array( |
| 86 | + '<IfModule mod_rewrite.c>', |
| 87 | + 'RewriteEngine On', |
| 88 | + 'RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]', |
| 89 | + 'RewriteBase /', |
| 90 | + 'RewriteRule ^index\.php$ - [L]', |
| 91 | + 'RewriteCond %{REQUEST_FILENAME} !-f', |
| 92 | + 'RewriteCond %{REQUEST_FILENAME} !-d', |
| 93 | + 'RewriteRule . /index.php [L]', |
| 94 | + '</IfModule>', |
| 95 | + ), |
| 96 | + 'content' => "RewriteCond %{HTTPS} !=on\nRewriteCond %{HTTP:X-Forwarded-Proto} !=https\nRewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]\n\n# BEGIN WordPress\n# The directives (lines) between \"BEGIN WordPress\" and \"END WordPress\" are\n# dynamically generated, and should only be modified via WordPress filters.\n# Any changes to the directives between these markers will be overwritten.\n<IfModule mod_rewrite.c>\nRewriteEngine On\nRewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]\nRewriteBase /\nRewriteRule ^index\.php$ - [L]\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule . /index.php [L]\n</IfModule>\n\n# END WordPress", |
| 97 | + 'marker' => 'WordPress', |
| 98 | + ), |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Tests extraction when file does not exist. |
| 104 | + * |
| 105 | + * @ticket 65136 |
| 106 | + */ |
| 107 | + public function test_extract_from_markers_non_existent_file() { |
| 108 | + $this->assertSame( array(), extract_from_markers( '/non/existent/path', 'WordPress' ) ); |
| 109 | + } |
| 110 | +} |
0 commit comments