forked from humanmade/S3-Uploads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-s3-uploads-stream-wrapper.php
157 lines (115 loc) · 5 KB
/
test-s3-uploads-stream-wrapper.php
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
<?php
class Test_S3_Uploads_Stream_Wrapper extends WP_UnitTestCase {
protected $s3 = null;
public function setUp() {
}
public function tearDown() {
stream_wrapper_unregister( 's3' );
S3_Uploads\Plugin::get_instance()->register_stream_wrapper();
}
public function test_stream_wrapper_is_registered() {
$this->assertTrue( in_array( 's3', stream_get_wrappers() ) );
}
public function test_copy_via_stream_wrapper() {
$local_path = dirname( __FILE__ ) . '/data/sunflower.jpg';
$remote_path = 's3://' . S3_UPLOADS_BUCKET . '/sunflower.jpg';
$result = copy( $local_path, $remote_path );
$this->assertTrue( $result );
$this->assertEquals( file_get_contents( $local_path ), file_get_contents( $remote_path ) );
}
public function test_rename_via_stream_wrapper() {
copy( dirname( __FILE__ ) . '/data/sunflower.jpg', 's3://' . S3_UPLOADS_BUCKET . '/sunflower.jpg' );
$result = rename( 's3://' . S3_UPLOADS_BUCKET . '/sunflower.jpg', 's3://' . S3_UPLOADS_BUCKET . '/sunflower-test.jpg' );
$this->assertTrue( $result );
$this->assertTrue( file_exists( 's3://' . S3_UPLOADS_BUCKET . '/sunflower-test.jpg' ) );
}
public function test_unlink_via_stream_wrapper() {
copy( dirname( __FILE__ ) . '/data/sunflower.jpg', 's3://' . S3_UPLOADS_BUCKET . '/sunflower.jpg' );
$result = unlink( 's3://' . S3_UPLOADS_BUCKET . '/sunflower.jpg' );
$this->assertTrue( $result );
$this->assertFalse( file_exists( 's3://' . S3_UPLOADS_BUCKET . '/sunflower.jpg' ) );
}
public function test_copy_via_stream_wrapper_fails_on_invalid_permission() {
stream_wrapper_unregister( 's3' );
$uploads = new S3_Uploads\Plugin( S3_UPLOADS_BUCKET, S3_UPLOADS_KEY, '123', null, S3_UPLOADS_REGION );
$uploads->register_stream_wrapper();
$bucket_root = strtok( S3_UPLOADS_BUCKET, '/' );
$result = @copy( dirname( __FILE__ ) . '/data/sunflower.jpg', 's3://' . $bucket_root . '/sunflower.jpg' );
$this->assertFalse( $result );
}
public function test_rename_via_stream_wrapper_fails_on_invalid_permission() {
copy( dirname( __FILE__ ) . '/data/sunflower.jpg', 's3://' . S3_UPLOADS_BUCKET . '/sunflower.jpg' );
stream_wrapper_unregister( 's3' );
$uploads = new S3_Uploads\Plugin( S3_UPLOADS_BUCKET, S3_UPLOADS_KEY, '123', null, S3_UPLOADS_REGION );
$uploads->register_stream_wrapper();
$bucket_root = strtok( S3_UPLOADS_BUCKET, '/' );
$result = @rename( 's3://' . S3_UPLOADS_BUCKET . '/sunflower.jpg', 's3://' . $bucket_root . '/sunflower.jpg' );
$this->assertFalse( $result );
}
/**
* As s3 doesn't have directories, we expect that mkdir does not cause any s3
* connectivity.
*/
public function test_file_exists_on_dir_does_not_cause_network_activity() {
$bucket_root = strtok( S3_UPLOADS_BUCKET, '/' );
// result would fail as we don't have permission to write here.
$result = file_exists( 's3://' . $bucket_root . '/some_dir' );
$this->assertTrue( $result );
$result = is_dir( 's3://' . $bucket_root . '/some_dir' );
$this->assertTrue( $result );
}
public function get_file_exists_via_stream_wrapper() {
copy( dirname( __FILE__ ) . '/data/sunflower.jpg', 's3://' . S3_UPLOADS_BUCKET . '/sunflower.jpg' );
$this->assertTrue( file_exists( 's3://' . S3_UPLOADS_BUCKET . '/sunflower.jpg' ) );
$this->assertFalse( file_exists( 's3://' . S3_UPLOADS_BUCKET . '/sunflower-missing.jpg' ) );
}
public function test_getimagesize_via_stream_wrapper() {
copy( dirname( __FILE__ ) . '/data/sunflower.jpg', 's3://' . S3_UPLOADS_BUCKET . '/sunflower.jpg' );
$file = 's3://' . S3_UPLOADS_BUCKET . '/sunflower.jpg';
$image = getimagesize( $file );
$this->assertEquals( array(
640,
419,
2,
'width="640" height="419"',
'bits' => 8,
'channels' => 3,
'mime' => 'image/jpeg',
), $image );
}
public function test_stream_wrapper_supports_seeking() {
$file = 's3://' . S3_UPLOADS_BUCKET . '/sunflower.jpg';
copy( dirname( __FILE__ ) . '/data/sunflower.jpg', $file );
$f = fopen( $file, 'r' );
$result = fseek( $f, 0, SEEK_END );
fclose( $f );
$this->assertEquals( 0, $result );
}
public function test_wp_handle_upload() {
$path = tempnam( sys_get_temp_dir(), 'sunflower' ) . '.jpg';
copy( dirname( __FILE__ ) . '/data/sunflower.jpg', $path );
$contents = file_get_contents( $path );
$file = array(
'error' => null,
'tmp_name' => $path,
'name' => 'can.jpg',
'size' => filesize( $path ),
);
$result = wp_handle_upload( $file, array( 'test_form' => false, 'test_size' => false, 'action' => 'wp_handle_sideload' ) );
$this->assertTrue( empty( $result['error'] ) );
$this->assertTrue( file_exists( $result['file'] ) );
$this->assertEquals( $contents, file_get_contents( $result['file'] ) );
}
public function test_list_directory_with_wildcard() {
$upload_dir = wp_upload_dir();
file_put_contents( $upload_dir['path'] . '/my-file-scaled.jpg', '' );
file_put_contents( $upload_dir['path'] . '/some-file-scaled.jpg', '' );
$files = scandir( $upload_dir['path'] . '/my-file*' );
$this->assertEquals(
[
'my-file-scaled.jpg',
],
$files
);
}
}