From a72a8759c159005155268d4e1ea774db3e7353d2 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 27 Apr 2026 16:13:46 -0400 Subject: [PATCH 1/5] Add unit tests for insert_with_markers() in src/wp-admin/includes/misc.php --- .../admin/includes/misc/insertWithMarkers.php | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php diff --git a/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php b/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php new file mode 100644 index 0000000000000..6de70a801ce1c --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php @@ -0,0 +1,121 @@ +temp_file = wp_tempnam( 'markers.txt' ); + } + + public function tear_down() { + if ( file_exists( $this->temp_file ) ) { + unlink( $this->temp_file ); + } + parent::tear_down(); + } + + /** + * Tests that insert_with_markers() correctly inserts or replaces content. + * + * @ticket 65137 + * + * @dataProvider data_insert_with_markers + * + * @param string $initial_content The initial content of the file. + * @param string $marker The marker to use. + * @param array|string $insertion The content to insert. + * @param string $expected The expected final content of the file. + */ + public function test_insert_with_markers( $initial_content, $marker, $insertion, $expected ) { + if ( '' !== $initial_content ) { + file_put_contents( $this->temp_file, str_replace( "\n", PHP_EOL, $initial_content ) ); + } else { + if ( file_exists( $this->temp_file ) ) { + unlink( $this->temp_file ); + } + } + + $result = insert_with_markers( $this->temp_file, $marker, $insertion ); + + $this->assertTrue( $result, 'insert_with_markers should return true on success.' ); + + $actual_content = file_get_contents( $this->temp_file ); + $actual_content = str_replace( array( "\r\n", "\r" ), "\n", $actual_content ); + $expected = str_replace( array( "\r\n", "\r" ), "\n", $expected ); + + $this->assertSame( $expected, $actual_content ); + } + + /** + * Data provider for test_insert_with_markers. + * + * @return array[] + */ + public function data_insert_with_markers() { + $wp_comments = array( + '# The directives (lines) between "BEGIN %s" and "END %s" are', + '# dynamically generated, and should only be modified via WordPress filters.', + '# Any changes to the directives between these markers will be overwritten.', + ); + + $wordpress_desc = sprintf( $wp_comments[0], 'WordPress', 'WordPress' ) . "\n" . $wp_comments[1] . "\n" . $wp_comments[2]; + $test_desc = sprintf( $wp_comments[0], 'Test', 'Test' ) . "\n" . $wp_comments[1] . "\n" . $wp_comments[2]; + $string_desc = sprintf( $wp_comments[0], 'StringTest', 'StringTest' ) . "\n" . $wp_comments[1] . "\n" . $wp_comments[2]; + + return array( + 'New file creation' => array( + 'initial_content' => '', + 'marker' => 'WordPress', + 'insertion' => array( 'Line 1', 'Line 2' ), + 'expected' => "\n# BEGIN WordPress\n{$wordpress_desc}\nLine 1\nLine 2\n# END WordPress", + ), + 'Insertion into existing file without block' => array( + 'initial_content' => "Existing content\n", + 'marker' => 'WordPress', + 'insertion' => array( 'New Line' ), + 'expected' => "Existing content\n\n# BEGIN WordPress\n{$wordpress_desc}\nNew Line\n# END WordPress", + ), + 'Replacement of existing block' => array( + 'initial_content' => "Top\n# BEGIN WordPress\nOld\n# END WordPress\nBottom", + 'marker' => 'WordPress', + 'insertion' => array( 'New' ), + 'expected' => "Top\n# BEGIN WordPress\n{$wordpress_desc}\nNew\n# END WordPress\nBottom", + ), + 'Empty insertion removes content but keeps markers' => array( + 'initial_content' => "# BEGIN Test\nContent\n# END Test", + 'marker' => 'Test', + 'insertion' => array(), + 'expected' => "# BEGIN Test\n{$test_desc}\n# END Test", + ), + 'String insertion instead of array' => array( + 'initial_content' => '', + 'marker' => 'StringTest', + 'insertion' => "Single Line", + 'expected' => "\n# BEGIN StringTest\n{$string_desc}\nSingle Line\n# END StringTest", + ), + ); + } + + /** + * Tests that insert_with_markers() returns false if the file is not writable. + * + * @ticket 65137 + */ + public function test_insert_with_markers_non_writable() { + $non_writable = '/non/existent/dir/file.txt'; + $this->assertFalse( insert_with_markers( $non_writable, 'Test', 'Content' ) ); + } +} From 476d5590054b17776a22c545b154b6b4af1b6927 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 27 Apr 2026 16:19:47 -0400 Subject: [PATCH 2/5] String "Single Line" does not require double quotes; use single quotes instead --- .../admin/includes/misc/insertWithMarkers.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php b/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php index 6de70a801ce1c..95476e323328e 100644 --- a/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php +++ b/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php @@ -59,11 +59,18 @@ public function test_insert_with_markers( $initial_content, $marker, $insertion, $this->assertSame( $expected, $actual_content ); } - /** - * Data provider for test_insert_with_markers. - * - * @return array[] - */ + /** + * Data provider for test_insert_with_markers. + * + * @return array[] + * + * @phpstan-return array + */ public function data_insert_with_markers() { $wp_comments = array( '# The directives (lines) between "BEGIN %s" and "END %s" are', @@ -103,7 +110,7 @@ public function data_insert_with_markers() { 'String insertion instead of array' => array( 'initial_content' => '', 'marker' => 'StringTest', - 'insertion' => "Single Line", + 'insertion' => 'Single Line', 'expected' => "\n# BEGIN StringTest\n{$string_desc}\nSingle Line\n# END StringTest", ), ); From 58822938754c791ac70c4c75619e68eeaa69257b Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 28 Apr 2026 16:01:19 -0400 Subject: [PATCH 3/5] Update tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php Co-authored-by: Weston Ruter --- tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php b/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php index 95476e323328e..40fbcdf3abac6 100644 --- a/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php +++ b/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php @@ -11,9 +11,8 @@ class Tests_insert_with_markers extends WP_UnitTestCase { /** * Path to the temporary file used for testing. - * @var string */ - private $temp_file; + private string $temp_file; public function set_up() { parent::set_up(); From 772bec7be8822bacc7bb6e3579220582d80481f5 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 28 Apr 2026 16:01:33 -0400 Subject: [PATCH 4/5] Update tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php Co-authored-by: Weston Ruter --- tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php b/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php index 40fbcdf3abac6..b6f578fd865c3 100644 --- a/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php +++ b/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php @@ -41,10 +41,6 @@ public function tear_down() { public function test_insert_with_markers( $initial_content, $marker, $insertion, $expected ) { if ( '' !== $initial_content ) { file_put_contents( $this->temp_file, str_replace( "\n", PHP_EOL, $initial_content ) ); - } else { - if ( file_exists( $this->temp_file ) ) { - unlink( $this->temp_file ); - } } $result = insert_with_markers( $this->temp_file, $marker, $insertion ); From 80376bc68c6b5474f307d6ddef9134acefb7a308 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 28 Apr 2026 16:01:52 -0400 Subject: [PATCH 5/5] Update tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php Co-authored-by: Weston Ruter --- tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php b/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php index b6f578fd865c3..b3896e115ef87 100644 --- a/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php +++ b/tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php @@ -66,7 +66,7 @@ public function test_insert_with_markers( $initial_content, $marker, $insertion, * expected: string, * }> */ - public function data_insert_with_markers() { + public function data_insert_with_markers(): array { $wp_comments = array( '# The directives (lines) between "BEGIN %s" and "END %s" are', '# dynamically generated, and should only be modified via WordPress filters.',