Skip to content

Commit 7017607

Browse files
author
costdev
committed
Add tests for ::move().
1 parent 4076b01 commit 7017607

File tree

1 file changed

+151
-0
lines changed
  • tests/phpunit/tests/filesystem/wpFilesystemDirect

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
/**
3+
* Tests for the WP_Filesystem_Direct::move() method.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
require_once __DIR__ . '/base.php';
9+
10+
/**
11+
* @group admin
12+
* @group filesystem
13+
* @group filesystem-direct
14+
*
15+
* @covers WP_Filesystem_Direct::move
16+
*/
17+
class Tests_Filesystem_WpFilesystemDirect_Move extends WP_Filesystem_Direct_UnitTestCase {
18+
19+
/**
20+
* Tests that `WP_Filesystem_Direct::copy()` overwrites an existing
21+
* destination when overwriting is enabled.
22+
*
23+
* @ticket 57774
24+
*/
25+
public function test_should_overwrite_an_existing_file_when_overwriting_is_enabled() {
26+
$source = self::$file_structure['visible_file']['path'];
27+
$destination = self::$file_structure['test_dir']['path'] . 'a_file_that_exists.dest';
28+
$actual = self::$filesystem->move( $source, $destination, true );
29+
30+
rename( $destination, $source );
31+
32+
$this->assertTrue( $actual );
33+
}
34+
35+
/**
36+
* Tests that `WP_Filesystem_Direct::move()` does not overwrite
37+
* an existing destination when overwriting is disabled.
38+
*
39+
* @ticket 57774
40+
*/
41+
public function test_should_not_overwrite_an_existing_file_when_overwriting_is_disabled() {
42+
$source = self::$file_structure['visible_file']['path'];
43+
$destination = self::$file_structure['subfile']['path'];
44+
$actual = self::$filesystem->move( $source, $destination );
45+
46+
$this->assertFalse( $actual );
47+
}
48+
49+
/**
50+
* Tests that `WP_Filesystem_Direct::move()` moves directories.
51+
*
52+
* @ticket 57774
53+
*/
54+
public function test_should_move_directories() {
55+
$source = self::$file_structure['test_dir']['path'];
56+
$destination = untrailingslashit( self::$file_structure['test_dir']['path'] ) . '-dest';
57+
$actual = self::$filesystem->move( $source, $destination, true );
58+
59+
$source_exists = is_dir( $source );
60+
$destination_exists = is_dir( $destination );
61+
62+
if ( $actual ) {
63+
$restored = rename( $destination, $source );
64+
}
65+
66+
$this->assertTrue( $actual, 'The directory was not moved.' );
67+
$this->assertFalse( $source_exists, 'The source still exists.' );
68+
$this->assertTrue( $destination_exists, 'The destination does not exist.' );
69+
$this->assertTrue( $restored, 'The test assets were not cleaned up after the test.' );
70+
}
71+
72+
/**
73+
* Tests that `WP_Filesystem_Direct::move()` returns false for an
74+
* invalid destination.
75+
*
76+
* @ticket 57774
77+
*/
78+
public function test_should_return_false_for_invalid_destination() {
79+
$source = self::$file_structure['test_dir']['path'];
80+
$destination = 'http://example.org';
81+
82+
$this->assertFalse( self::$filesystem->move( $source, $destination, true ) );
83+
}
84+
85+
/**
86+
* Tests that `WP_Filesystem_Direct::move()` returns false for an
87+
* invalid destination.
88+
*
89+
* @ticket 57774
90+
*/
91+
public function test_should_return_false_when_overwriting_is_enabled_the_destination_exists_but_cannot_be_deleted() {
92+
global $wp_filesystem;
93+
$wpfilesystem_backup = $wp_filesystem;
94+
95+
// Force failure conditions.
96+
$filesystem_mock = $this->getMockBuilder( 'WP_Filesystem_Direct' )
97+
// Note: setMethods() is deprecated in PHPUnit 9, but still supported.
98+
->setMethods( array( 'exists', 'delete' ) )
99+
->setConstructorArgs( array( null ) )
100+
->getMock();
101+
102+
$filesystem_mock->expects( $this->once() )->method( 'exists' )->willReturn( true );
103+
$filesystem_mock->expects( $this->once() )->method( 'delete' )->willReturn( false );
104+
$wp_filesystem = $filesystem_mock;
105+
106+
$actual = $wp_filesystem->move(
107+
self::$file_structure['test_dir']['path'],
108+
self::$file_structure['subdir']['path'],
109+
true
110+
);
111+
112+
// Restore the filesystem.
113+
$wp_filesystem = $wpfilesystem_backup;
114+
115+
$this->assertFalse( $actual );
116+
}
117+
118+
/**
119+
* Tests that `WP_Filesystem_Direct::move()` falls back to a single
120+
* file copy when the source and destination do not exist.
121+
*
122+
* @ticket 57774
123+
*/
124+
public function test_should_fall_back_to_single_file_copy_when_source_and_destination_do_not_exist() {
125+
global $wp_filesystem;
126+
127+
$source = self::$file_structure['test_dir']['path'] . 'a_file_that_does_not_exist.txt';
128+
$destination = self::$file_structure['test_dir']['path'] . 'another_file_that_does_not_exist.txt';
129+
130+
// Set up mock filesystem.
131+
$filesystem_mock = $this->getMockBuilder( 'WP_Filesystem_Direct' )
132+
->setConstructorArgs( array( null ) )
133+
// Note: setMethods() is deprecated in PHPUnit 9, but still supported.
134+
->setMethods( array( 'exists', 'delete', 'is_file', 'copy' ) )
135+
->getMock();
136+
137+
$filesystem_mock->expects( $this->exactly( 2 ) )->method( 'exists' )->willReturn( array( true, true ) );
138+
$filesystem_mock->expects( $this->exactly( 2 ) )->method( 'delete' )->willReturn( array( true, false ) );
139+
$filesystem_mock->expects( $this->once() )->method( 'is_file' )->willReturn( true );
140+
$filesystem_mock->expects( $this->once() )->method( 'copy' )->willReturn( true );
141+
142+
$wp_filesystem_backup = $wp_filesystem;
143+
$wp_filesystem = $filesystem_mock;
144+
145+
$actual = $filesystem_mock->move( $source, $destination, true );
146+
$wp_filesystem = $wp_filesystem_backup;
147+
148+
$this->assertTrue( $actual );
149+
}
150+
151+
}

0 commit comments

Comments
 (0)