Skip to content

Commit f4e5801

Browse files
committed
Tests: Cover directory copy skip lists.
Verify that `copy_dir()` skips exact file and directory matches while preserving other content. Protect the strict comparison that distinguishes `0019` from the skip-list entry `19`. Developed in: #14107 Props irozum, huzaifaalmesbah. See #65819, #46581. git-svn-id: https://develop.svn.wordpress.org/trunk@65093 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 87ff677 commit f4e5801

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

‎tests/phpunit/tests/filesystem/copyDir.php‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,61 @@ public function test_should_create_destination_it_if_does_not_exist() {
8080
$this->assertDirectoryExists( $to . 'subfolder1/', 'The destination subfolder was not created.' );
8181
$this->assertFileExists( $to . 'subfolder1/file2.txt', 'The destination subfolder file was not created.' );
8282
}
83+
84+
/**
85+
* Tests that files and directories named in the skip list are not copied.
86+
*
87+
* @ticket 46581
88+
*/
89+
public function test_should_skip_files_and_directories_in_the_skip_list() {
90+
global $wp_filesystem;
91+
92+
$from = self::$test_dir . 'folder1/';
93+
$to = self::$test_dir . 'folder2/';
94+
95+
$wp_filesystem->mkdir( $from );
96+
$wp_filesystem->touch( $from . 'keep-me.txt' );
97+
$wp_filesystem->touch( $from . 'skip-me.txt' );
98+
$wp_filesystem->mkdir( $from . 'keep-folder' );
99+
$wp_filesystem->touch( $from . 'keep-folder/file.txt' );
100+
$wp_filesystem->mkdir( $from . 'skip-folder' );
101+
$wp_filesystem->touch( $from . 'skip-folder/file.txt' );
102+
103+
$this->assertTrue( copy_dir( $from, $to, array( 'skip-me.txt', 'skip-folder' ) ), 'copy_dir() failed.' );
104+
105+
$this->assertFileExists( $to . 'keep-me.txt', 'The non-skip-listed file was not copied.' );
106+
$this->assertDirectoryExists( $to . 'keep-folder', 'The non-skip-listed directory was not copied.' );
107+
$this->assertFileExists( $to . 'keep-folder/file.txt', 'The contents of the non-skip-listed directory were not copied.' );
108+
109+
$this->assertFileDoesNotExist( $to . 'skip-me.txt', 'The skip-listed file was copied.' );
110+
$this->assertDirectoryDoesNotExist( $to . 'skip-folder', 'The skip-listed directory was copied.' );
111+
}
112+
113+
/**
114+
* Tests that a numeric-looking directory name is not skipped just because
115+
* it's loosely equal to an unrelated `$skip_list` entry.
116+
*
117+
* `in_array( $filename, $skip_list )` without strict type checking compares
118+
* two numeric strings numerically, so a directory named `0019` would be
119+
* considered equal to a `$skip_list` entry of `19` even though the two
120+
* strings are different paths. `copy_dir()` must use a strict comparison
121+
* so only an exact match is skipped.
122+
*
123+
* @ticket 46581
124+
*/
125+
public function test_should_not_skip_directory_loosely_equal_to_a_skip_list_entry() {
126+
global $wp_filesystem;
127+
128+
$from = self::$test_dir . 'folder1/';
129+
$to = self::$test_dir . 'folder2/';
130+
131+
$wp_filesystem->mkdir( $from );
132+
$wp_filesystem->mkdir( $from . '0019' );
133+
$wp_filesystem->touch( $from . '0019/file.txt' );
134+
135+
$this->assertTrue( copy_dir( $from, $to, array( '19' ) ), 'copy_dir() failed.' );
136+
137+
$this->assertDirectoryExists( $to . '0019', 'The directory was incorrectly skipped due to a loose comparison with an unrelated skip list entry.' );
138+
$this->assertFileExists( $to . '0019/file.txt', 'The contents of the incorrectly skipped directory were not copied.' );
139+
}
83140
}

0 commit comments

Comments
 (0)