-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add unit tests for insert_with_markers() in src/wp-admin/includes/mis… #11659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pbearne
wants to merge
2
commits into
WordPress:trunk
Choose a base branch
from
pbearne:65137-misc-insert_with_markers
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
tests/phpunit/tests/admin/includes/misc/insertWithMarkers.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,121 @@ | ||||||||||||||
| <?php | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Tests for the insert_with_markers() function. | ||||||||||||||
| * | ||||||||||||||
| * @group admin | ||||||||||||||
| * | ||||||||||||||
| * @covers ::insert_with_markers | ||||||||||||||
| */ | ||||||||||||||
| class Tests_insert_with_markers extends WP_UnitTestCase { | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Path to the temporary file used for testing. | ||||||||||||||
| * @var string | ||||||||||||||
| */ | ||||||||||||||
| private $temp_file; | ||||||||||||||
|
|
||||||||||||||
| public function set_up() { | ||||||||||||||
| parent::set_up(); | ||||||||||||||
| $this->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 ); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+45
to
+49
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary? Isn't it removed after each test already?
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| $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() { | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| $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' ) ); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.