forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileCacheTest.php
More file actions
207 lines (163 loc) · 6.17 KB
/
FileCacheTest.php
File metadata and controls
207 lines (163 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
use WP_CLI\FileCache;
use WP_CLI\Loggers;
use WP_CLI\Tests\TestCase;
use WP_CLI\Utils;
class FileCacheTest extends TestCase {
public static function set_up_before_class() {
require_once dirname( __DIR__ ) . '/php/class-wp-cli.php';
}
/**
* Test get_root() deals with backslashed directory.
*/
public function testGetRoot() {
$max_size = 32;
$ttl = 60;
$cache_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-file-cache', true );
$cache = new FileCache( $cache_dir, $ttl, $max_size );
$this->assertSame( $cache_dir . '/', $cache->get_root() );
unset( $cache );
$cache = new FileCache( $cache_dir . '/', $ttl, $max_size );
$this->assertSame( $cache_dir . '/', $cache->get_root() );
unset( $cache );
$cache = new FileCache( $cache_dir . '\\', $ttl, $max_size );
$this->assertSame( $cache_dir . '/', $cache->get_root() );
unset( $cache );
rmdir( $cache_dir );
}
public function test_ensure_dir_exists() {
$prev_logger = WP_CLI::get_logger();
$logger = new Loggers\Execution();
WP_CLI::set_logger( $logger );
$max_size = 32;
$ttl = 60;
$cache_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-file-cache', true );
$cache = new FileCache( $cache_dir, $ttl, $max_size );
$test_class = new ReflectionClass( $cache );
$method = $test_class->getMethod( 'ensure_dir_exists' );
$method->setAccessible( true );
// Cache directory should be created.
$result = $method->invokeArgs( $cache, [ $cache_dir . '/test1' ] );
$this->assertTrue( $result );
$this->assertTrue( is_dir( $cache_dir . '/test1' ) );
// Try to create the same directory again. it should return true.
$result = $method->invokeArgs( $cache, [ $cache_dir . '/test1' ] );
$this->assertTrue( $result );
// `chmod()` doesn't work on Windows.
if ( ! Utils\is_windows() ) {
// It should be failed because permission denied.
$logger->stderr = '';
chmod( $cache_dir . '/test1', 0000 );
$result = $method->invokeArgs( $cache, [ $cache_dir . '/test1/error' ] );
$expected = "/^Warning: Failed to create directory '.+': mkdir\(\): Permission denied\.$/";
$this->assertMatchesRegularExpression( $expected, $logger->stderr );
}
// It should be failed because file exists.
$logger->stderr = '';
file_put_contents( $cache_dir . '/test2', '' );
$result = $method->invokeArgs( $cache, [ $cache_dir . '/test2' ] );
$expected = "/^Warning: Failed to create directory '.+': mkdir\(\): File exists\.$/";
$this->assertMatchesRegularExpression( $expected, $logger->stderr );
// Restore.
chmod( $cache_dir . '/test1', 0755 );
rmdir( $cache_dir . '/test1' );
unlink( $cache_dir . '/test2' );
rmdir( $cache_dir );
WP_CLI::set_logger( $prev_logger );
}
public function test_export() {
$max_size = 32;
$ttl = 60;
$cache_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-file-cache', true );
$target_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-file-cache-export/nonexistant-subdirectory', true );
$target = $target_dir . '/foo';
$key = 'foo';
$contents = 'bar';
$cache = new FileCache( $cache_dir, $ttl, $max_size );
// Assert subdirectory is created.
$cache->write( $key, $contents );
$cache->export( $key, $target );
$this->assertEquals( $contents, file_get_contents( $target ) );
// Clean up.
$cache->clear();
unlink( $target );
rmdir( $target_dir );
}
public function test_import() {
$max_size = 32;
$ttl = 60;
$cache_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-file-cache', true );
$cache = new FileCache( $cache_dir, $ttl, $max_size );
$tmp_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-file-cache-import', true );
mkdir( $tmp_dir );
// "$group/$slug-$version.$ext";
$key = 'plugin/my-fixture-plugin-1.0.0.zip';
$fixture_filepath = $tmp_dir . '/my-downloaded-fixture-plugin-1.0.0.zip';
$zip = new ZipArchive();
$zip->open( $fixture_filepath, ZIPARCHIVE::CREATE );
$zip->addFile( __FILE__ );
$zip->close();
$result = $cache->import( $key, $fixture_filepath );
// Assert file is imported.
$this->assertTrue( $result );
$this->assertFileExists( "{$cache_dir}/{$key}" );
// Clean up.
$cache->clear();
unlink( $fixture_filepath );
rmdir( $tmp_dir );
}
/**
* @see https://github.com/wp-cli/wp-cli/pull/5947
*/
public function test_import_do_not_use_cache_file_cannot_be_read() {
$max_size = 32;
$ttl = 60;
$cache_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-file-cache', true );
$cache = new FileCache( $cache_dir, $ttl, $max_size );
$tmp_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-file-cache-import', true );
mkdir( $tmp_dir );
$key = 'plugin/my-fixture-plugin-1.0.0.zip';
$fixture_filepath = $tmp_dir . '/my-bad-permissions-fixture-plugin-1.0.0.zip';
$zip = new ZipArchive();
$zip->open( $fixture_filepath, ZIPARCHIVE::CREATE );
$zip->addFile( __FILE__ );
$zip->close();
chmod( $fixture_filepath, 0000 );
// "Warning: copy(-.): Failed to open stream: Permission denied".
$error = null;
set_error_handler(
function ( $errno, $errstr ) use ( &$error ) {
$error = $errstr;
}
);
$result = $cache->import( $key, $fixture_filepath );
restore_error_handler();
$this->assertNull( $error );
$this->assertFalse( $result );
// Clean up.
$cache->clear();
unlink( $fixture_filepath );
rmdir( $tmp_dir );
}
/**
* Windows filenames cannot end in periods.
*
* @covers \WP_CLI\FileCache::validate_key
*
* @see https://github.com/wp-cli/wp-cli/pull/5947
* @see https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
*/
public function test_validate_key_ending_in_period() {
$max_size = 32;
$ttl = 60;
$cache_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-file-cache', true );
$cache = new FileCache( $cache_dir, $ttl, $max_size );
$key = 'plugin/advanced-sidebar-menu-pro-9.5.7.';
$reflection = new ReflectionClass( $cache );
$method = $reflection->getMethod( 'validate_key' );
$method->setAccessible( true );
$result = $method->invoke( $cache, $key );
$this->assertStringEndsNotWith( '.', $result );
$this->assertSame( 'plugin/advanced-sidebar-menu-pro-9.5.7', $result );
}
}