|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the insert_with_markers() function. |
| 5 | + * |
| 6 | + * @group admin |
| 7 | + * |
| 8 | + * @covers ::insert_with_markers |
| 9 | + */ |
| 10 | +class Tests_insert_with_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 insert_with_markers() correctly inserts or replaces content. |
| 32 | + * |
| 33 | + * @ticket 65137 |
| 34 | + * |
| 35 | + * @dataProvider data_insert_with_markers |
| 36 | + * |
| 37 | + * @param string $initial_content The initial content of the file. |
| 38 | + * @param string $marker The marker to use. |
| 39 | + * @param array|string $insertion The content to insert. |
| 40 | + * @param string $expected The expected final content of the file. |
| 41 | + */ |
| 42 | + public function test_insert_with_markers( $initial_content, $marker, $insertion, $expected ) { |
| 43 | + if ( '' !== $initial_content ) { |
| 44 | + file_put_contents( $this->temp_file, str_replace( "\n", PHP_EOL, $initial_content ) ); |
| 45 | + } else { |
| 46 | + if ( file_exists( $this->temp_file ) ) { |
| 47 | + unlink( $this->temp_file ); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + $result = insert_with_markers( $this->temp_file, $marker, $insertion ); |
| 52 | + |
| 53 | + $this->assertTrue( $result, 'insert_with_markers should return true on success.' ); |
| 54 | + |
| 55 | + $actual_content = file_get_contents( $this->temp_file ); |
| 56 | + $actual_content = str_replace( array( "\r\n", "\r" ), "\n", $actual_content ); |
| 57 | + $expected = str_replace( array( "\r\n", "\r" ), "\n", $expected ); |
| 58 | + |
| 59 | + $this->assertSame( $expected, $actual_content ); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Data provider for test_insert_with_markers. |
| 64 | + * |
| 65 | + * @return array[] |
| 66 | + */ |
| 67 | + public function data_insert_with_markers() { |
| 68 | + $wp_comments = array( |
| 69 | + '# The directives (lines) between "BEGIN %s" and "END %s" are', |
| 70 | + '# dynamically generated, and should only be modified via WordPress filters.', |
| 71 | + '# Any changes to the directives between these markers will be overwritten.', |
| 72 | + ); |
| 73 | + |
| 74 | + $wordpress_desc = sprintf( $wp_comments[0], 'WordPress', 'WordPress' ) . "\n" . $wp_comments[1] . "\n" . $wp_comments[2]; |
| 75 | + $test_desc = sprintf( $wp_comments[0], 'Test', 'Test' ) . "\n" . $wp_comments[1] . "\n" . $wp_comments[2]; |
| 76 | + $string_desc = sprintf( $wp_comments[0], 'StringTest', 'StringTest' ) . "\n" . $wp_comments[1] . "\n" . $wp_comments[2]; |
| 77 | + |
| 78 | + return array( |
| 79 | + 'New file creation' => array( |
| 80 | + 'initial_content' => '', |
| 81 | + 'marker' => 'WordPress', |
| 82 | + 'insertion' => array( 'Line 1', 'Line 2' ), |
| 83 | + 'expected' => "\n# BEGIN WordPress\n{$wordpress_desc}\nLine 1\nLine 2\n# END WordPress", |
| 84 | + ), |
| 85 | + 'Insertion into existing file without block' => array( |
| 86 | + 'initial_content' => "Existing content\n", |
| 87 | + 'marker' => 'WordPress', |
| 88 | + 'insertion' => array( 'New Line' ), |
| 89 | + 'expected' => "Existing content\n\n# BEGIN WordPress\n{$wordpress_desc}\nNew Line\n# END WordPress", |
| 90 | + ), |
| 91 | + 'Replacement of existing block' => array( |
| 92 | + 'initial_content' => "Top\n# BEGIN WordPress\nOld\n# END WordPress\nBottom", |
| 93 | + 'marker' => 'WordPress', |
| 94 | + 'insertion' => array( 'New' ), |
| 95 | + 'expected' => "Top\n# BEGIN WordPress\n{$wordpress_desc}\nNew\n# END WordPress\nBottom", |
| 96 | + ), |
| 97 | + 'Empty insertion removes content but keeps markers' => array( |
| 98 | + 'initial_content' => "# BEGIN Test\nContent\n# END Test", |
| 99 | + 'marker' => 'Test', |
| 100 | + 'insertion' => array(), |
| 101 | + 'expected' => "# BEGIN Test\n{$test_desc}\n# END Test", |
| 102 | + ), |
| 103 | + 'String insertion instead of array' => array( |
| 104 | + 'initial_content' => '', |
| 105 | + 'marker' => 'StringTest', |
| 106 | + 'insertion' => "Single Line", |
| 107 | + 'expected' => "\n# BEGIN StringTest\n{$string_desc}\nSingle Line\n# END StringTest", |
| 108 | + ), |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Tests that insert_with_markers() returns false if the file is not writable. |
| 114 | + * |
| 115 | + * @ticket 65137 |
| 116 | + */ |
| 117 | + public function test_insert_with_markers_non_writable() { |
| 118 | + $non_writable = '/non/existent/dir/file.txt'; |
| 119 | + $this->assertFalse( insert_with_markers( $non_writable, 'Test', 'Content' ) ); |
| 120 | + } |
| 121 | +} |
0 commit comments