@@ -10,11 +10,11 @@ |
||
10 | 10 | use OC\Files\Storage\Temporary; |
11 | 11 | |
12 | 12 | class LocalTest extends ObjectStoreTestCase { |
13 | - /** |
|
14 | - * @return \OCP\Files\ObjectStore\IObjectStore |
|
15 | - */ |
|
16 | - protected function getInstance() { |
|
17 | - $storage = new Temporary(); |
|
18 | - return new StorageObjectStore($storage); |
|
19 | - } |
|
13 | + /** |
|
14 | + * @return \OCP\Files\ObjectStore\IObjectStore |
|
15 | + */ |
|
16 | + protected function getInstance() { |
|
17 | + $storage = new Temporary(); |
|
18 | + return new StorageObjectStore($storage); |
|
19 | + } |
|
20 | 20 | } |
@@ -15,27 +15,27 @@ |
||
15 | 15 | * @group DB |
16 | 16 | */ |
17 | 17 | class ObjectStoreStoragesDifferentBucketTest extends StoragesTestCase { |
18 | - /** |
|
19 | - * @var \OCP\Files\ObjectStore\IObjectStore |
|
20 | - */ |
|
21 | - private $objectStore1; |
|
18 | + /** |
|
19 | + * @var \OCP\Files\ObjectStore\IObjectStore |
|
20 | + */ |
|
21 | + private $objectStore1; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @var \OCP\Files\ObjectStore\IObjectStore |
|
25 | - */ |
|
26 | - private $objectStore2; |
|
23 | + /** |
|
24 | + * @var \OCP\Files\ObjectStore\IObjectStore |
|
25 | + */ |
|
26 | + private $objectStore2; |
|
27 | 27 | |
28 | - protected function setUp(): void { |
|
29 | - parent::setUp(); |
|
28 | + protected function setUp(): void { |
|
29 | + parent::setUp(); |
|
30 | 30 | |
31 | - $baseStorage1 = new Temporary(); |
|
32 | - $this->objectStore1 = new StorageObjectStore($baseStorage1); |
|
33 | - $config['objectstore'] = $this->objectStore1; |
|
34 | - $this->storage1 = new ObjectStoreStorageOverwrite($config); |
|
31 | + $baseStorage1 = new Temporary(); |
|
32 | + $this->objectStore1 = new StorageObjectStore($baseStorage1); |
|
33 | + $config['objectstore'] = $this->objectStore1; |
|
34 | + $this->storage1 = new ObjectStoreStorageOverwrite($config); |
|
35 | 35 | |
36 | - $baseStorage2 = new Temporary(); |
|
37 | - $this->objectStore2 = new StorageObjectStore($baseStorage2); |
|
38 | - $config['objectstore'] = $this->objectStore2; |
|
39 | - $this->storage2 = new ObjectStoreStorageOverwrite($config); |
|
40 | - } |
|
36 | + $baseStorage2 = new Temporary(); |
|
37 | + $this->objectStore2 = new StorageObjectStore($baseStorage2); |
|
38 | + $config['objectstore'] = $this->objectStore2; |
|
39 | + $this->storage2 = new ObjectStoreStorageOverwrite($config); |
|
40 | + } |
|
41 | 41 | } |
@@ -13,19 +13,19 @@ |
||
13 | 13 | * @group PRIMARY-swift |
14 | 14 | */ |
15 | 15 | class SwiftTest extends ObjectStoreTestCase { |
16 | - /** |
|
17 | - * @return \OCP\Files\ObjectStore\IObjectStore |
|
18 | - */ |
|
19 | - protected function getInstance() { |
|
20 | - $config = \OC::$server->getConfig()->getSystemValue('objectstore'); |
|
21 | - if (!is_array($config) || $config['class'] !== 'OC\\Files\\ObjectStore\\Swift') { |
|
22 | - $this->markTestSkipped('objectstore not configured for swift'); |
|
23 | - } |
|
16 | + /** |
|
17 | + * @return \OCP\Files\ObjectStore\IObjectStore |
|
18 | + */ |
|
19 | + protected function getInstance() { |
|
20 | + $config = \OC::$server->getConfig()->getSystemValue('objectstore'); |
|
21 | + if (!is_array($config) || $config['class'] !== 'OC\\Files\\ObjectStore\\Swift') { |
|
22 | + $this->markTestSkipped('objectstore not configured for swift'); |
|
23 | + } |
|
24 | 24 | |
25 | - return new Swift($config['arguments']); |
|
26 | - } |
|
25 | + return new Swift($config['arguments']); |
|
26 | + } |
|
27 | 27 | |
28 | - public function testFseekSize(): void { |
|
29 | - $this->markTestSkipped('Swift does not support seeking at the moment'); |
|
30 | - } |
|
28 | + public function testFseekSize(): void { |
|
29 | + $this->markTestSkipped('Swift does not support seeking at the moment'); |
|
30 | + } |
|
31 | 31 | } |
@@ -12,16 +12,16 @@ |
||
12 | 12 | * @group PRIMARY-azure |
13 | 13 | */ |
14 | 14 | class AzureTest extends ObjectStoreTestCase { |
15 | - protected function getInstance() { |
|
16 | - $config = \OC::$server->getConfig()->getSystemValue('objectstore'); |
|
17 | - if (!is_array($config) || $config['class'] !== 'OC\\Files\\ObjectStore\\Azure') { |
|
18 | - $this->markTestSkipped('objectstore not configured for azure'); |
|
19 | - } |
|
15 | + protected function getInstance() { |
|
16 | + $config = \OC::$server->getConfig()->getSystemValue('objectstore'); |
|
17 | + if (!is_array($config) || $config['class'] !== 'OC\\Files\\ObjectStore\\Azure') { |
|
18 | + $this->markTestSkipped('objectstore not configured for azure'); |
|
19 | + } |
|
20 | 20 | |
21 | - return new Azure($config['arguments']); |
|
22 | - } |
|
21 | + return new Azure($config['arguments']); |
|
22 | + } |
|
23 | 23 | |
24 | - public function testFseekSize(): void { |
|
25 | - $this->markTestSkipped('azure does not support seeking at the moment'); |
|
26 | - } |
|
24 | + public function testFseekSize(): void { |
|
25 | + $this->markTestSkipped('azure does not support seeking at the moment'); |
|
26 | + } |
|
27 | 27 | } |
@@ -15,19 +15,19 @@ |
||
15 | 15 | * @group DB |
16 | 16 | */ |
17 | 17 | class ObjectStoreStoragesSameBucketTest extends StoragesTestCase { |
18 | - /** |
|
19 | - * @var \OCP\Files\ObjectStore\IObjectStore |
|
20 | - */ |
|
21 | - private $objectStore; |
|
18 | + /** |
|
19 | + * @var \OCP\Files\ObjectStore\IObjectStore |
|
20 | + */ |
|
21 | + private $objectStore; |
|
22 | 22 | |
23 | - protected function setUp(): void { |
|
24 | - parent::setUp(); |
|
23 | + protected function setUp(): void { |
|
24 | + parent::setUp(); |
|
25 | 25 | |
26 | - $baseStorage = new Temporary(); |
|
27 | - $this->objectStore = new StorageObjectStore($baseStorage); |
|
28 | - $config['objectstore'] = $this->objectStore; |
|
29 | - // storage1 and storage2 share the same object store. |
|
30 | - $this->storage1 = new ObjectStoreStorageOverwrite($config); |
|
31 | - $this->storage2 = new ObjectStoreStorageOverwrite($config); |
|
32 | - } |
|
26 | + $baseStorage = new Temporary(); |
|
27 | + $this->objectStore = new StorageObjectStore($baseStorage); |
|
28 | + $config['objectstore'] = $this->objectStore; |
|
29 | + // storage1 and storage2 share the same object store. |
|
30 | + $this->storage1 = new ObjectStoreStorageOverwrite($config); |
|
31 | + $this->storage2 = new ObjectStoreStorageOverwrite($config); |
|
32 | + } |
|
33 | 33 | } |
@@ -10,164 +10,164 @@ |
||
10 | 10 | use OC\Files\ObjectStore\S3; |
11 | 11 | |
12 | 12 | class MultiPartUploadS3 extends S3 { |
13 | - public function writeObject($urn, $stream, ?string $mimetype = null) { |
|
14 | - $this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [ |
|
15 | - 'mup_threshold' => 1, |
|
16 | - ]); |
|
17 | - } |
|
13 | + public function writeObject($urn, $stream, ?string $mimetype = null) { |
|
14 | + $this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [ |
|
15 | + 'mup_threshold' => 1, |
|
16 | + ]); |
|
17 | + } |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | class NonSeekableStream extends Wrapper { |
21 | - public static function wrap($source) { |
|
22 | - $context = stream_context_create([ |
|
23 | - 'nonseek' => [ |
|
24 | - 'source' => $source, |
|
25 | - ], |
|
26 | - ]); |
|
27 | - return Wrapper::wrapSource($source, $context, 'nonseek', self::class); |
|
28 | - } |
|
29 | - |
|
30 | - public function dir_opendir($path, $options) { |
|
31 | - return false; |
|
32 | - } |
|
33 | - |
|
34 | - public function stream_open($path, $mode, $options, &$opened_path) { |
|
35 | - $this->loadContext('nonseek'); |
|
36 | - return true; |
|
37 | - } |
|
38 | - |
|
39 | - public function stream_seek($offset, $whence = SEEK_SET) { |
|
40 | - return false; |
|
41 | - } |
|
21 | + public static function wrap($source) { |
|
22 | + $context = stream_context_create([ |
|
23 | + 'nonseek' => [ |
|
24 | + 'source' => $source, |
|
25 | + ], |
|
26 | + ]); |
|
27 | + return Wrapper::wrapSource($source, $context, 'nonseek', self::class); |
|
28 | + } |
|
29 | + |
|
30 | + public function dir_opendir($path, $options) { |
|
31 | + return false; |
|
32 | + } |
|
33 | + |
|
34 | + public function stream_open($path, $mode, $options, &$opened_path) { |
|
35 | + $this->loadContext('nonseek'); |
|
36 | + return true; |
|
37 | + } |
|
38 | + |
|
39 | + public function stream_seek($offset, $whence = SEEK_SET) { |
|
40 | + return false; |
|
41 | + } |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | /** |
45 | 45 | * @group PRIMARY-s3 |
46 | 46 | */ |
47 | 47 | class S3Test extends ObjectStoreTestCase { |
48 | - public function setUp(): void { |
|
49 | - parent::setUp(); |
|
50 | - $s3 = $this->getInstance(); |
|
51 | - $s3->deleteObject('multiparttest'); |
|
52 | - } |
|
53 | - |
|
54 | - protected function getInstance() { |
|
55 | - $config = \OC::$server->getConfig()->getSystemValue('objectstore'); |
|
56 | - if (!is_array($config) || $config['class'] !== S3::class) { |
|
57 | - $this->markTestSkipped('objectstore not configured for s3'); |
|
58 | - } |
|
59 | - |
|
60 | - return new S3($config['arguments']); |
|
61 | - } |
|
48 | + public function setUp(): void { |
|
49 | + parent::setUp(); |
|
50 | + $s3 = $this->getInstance(); |
|
51 | + $s3->deleteObject('multiparttest'); |
|
52 | + } |
|
53 | + |
|
54 | + protected function getInstance() { |
|
55 | + $config = \OC::$server->getConfig()->getSystemValue('objectstore'); |
|
56 | + if (!is_array($config) || $config['class'] !== S3::class) { |
|
57 | + $this->markTestSkipped('objectstore not configured for s3'); |
|
58 | + } |
|
59 | + |
|
60 | + return new S3($config['arguments']); |
|
61 | + } |
|
62 | 62 | |
63 | - public function testUploadNonSeekable(): void { |
|
64 | - $this->cleanupAfter('multiparttest'); |
|
65 | - |
|
66 | - $s3 = $this->getInstance(); |
|
67 | - |
|
68 | - $s3->writeObject('multiparttest', NonSeekableStream::wrap(fopen(__FILE__, 'r'))); |
|
69 | - |
|
70 | - $result = $s3->readObject('multiparttest'); |
|
71 | - |
|
72 | - $this->assertEquals(file_get_contents(__FILE__), stream_get_contents($result)); |
|
73 | - } |
|
74 | - |
|
75 | - public function testSeek(): void { |
|
76 | - $this->cleanupAfter('seek'); |
|
77 | - |
|
78 | - $data = file_get_contents(__FILE__); |
|
79 | - |
|
80 | - $instance = $this->getInstance(); |
|
81 | - $instance->writeObject('seek', $this->stringToStream($data)); |
|
82 | - |
|
83 | - $read = $instance->readObject('seek'); |
|
84 | - $this->assertEquals(substr($data, 0, 100), fread($read, 100)); |
|
85 | - |
|
86 | - fseek($read, 10); |
|
87 | - $this->assertEquals(substr($data, 10, 100), fread($read, 100)); |
|
88 | - |
|
89 | - fseek($read, 100, SEEK_CUR); |
|
90 | - $this->assertEquals(substr($data, 210, 100), fread($read, 100)); |
|
91 | - } |
|
92 | - |
|
93 | - public function assertNoUpload($objectUrn) { |
|
94 | - /** @var \OC\Files\ObjectStore\S3 */ |
|
95 | - $s3 = $this->getInstance(); |
|
96 | - $s3client = $s3->getConnection(); |
|
97 | - $uploads = $s3client->listMultipartUploads([ |
|
98 | - 'Bucket' => $s3->getBucket(), |
|
99 | - 'Prefix' => $objectUrn, |
|
100 | - ]); |
|
101 | - $this->assertArrayNotHasKey('Uploads', $uploads, 'Assert is not uploaded'); |
|
102 | - } |
|
103 | - |
|
104 | - public function testEmptyUpload(): void { |
|
105 | - $s3 = $this->getInstance(); |
|
106 | - |
|
107 | - $emptyStream = fopen('php://memory', 'r'); |
|
108 | - fwrite($emptyStream, ''); |
|
109 | - |
|
110 | - $s3->writeObject('emptystream', $emptyStream); |
|
111 | - |
|
112 | - $this->assertNoUpload('emptystream'); |
|
113 | - $this->assertTrue($s3->objectExists('emptystream'), 'Object exists on S3'); |
|
114 | - |
|
115 | - $thrown = false; |
|
116 | - try { |
|
117 | - self::assertFalse($s3->readObject('emptystream'), 'Reading empty stream object should return false'); |
|
118 | - } catch (\Exception $e) { |
|
119 | - // An exception is expected here since 0 byte files are wrapped |
|
120 | - // to be read from an empty memory stream in the ObjectStoreStorage |
|
121 | - $thrown = true; |
|
122 | - } |
|
123 | - self::assertTrue($thrown, 'readObject with range requests are not expected to work on empty objects'); |
|
124 | - |
|
125 | - $s3->deleteObject('emptystream'); |
|
126 | - } |
|
127 | - |
|
128 | - /** File size to upload in bytes */ |
|
129 | - public static function dataFileSizes(): array { |
|
130 | - return [ |
|
131 | - [1000000], [2000000], [5242879], [5242880], [5242881], [10000000] |
|
132 | - ]; |
|
133 | - } |
|
134 | - |
|
135 | - /** @dataProvider dataFileSizes */ |
|
136 | - public function testFileSizes($size): void { |
|
137 | - if (str_starts_with(PHP_VERSION, '8.3') && getenv('CI')) { |
|
138 | - $this->markTestSkipped('Test is unreliable and skipped on 8.3'); |
|
139 | - } |
|
140 | - |
|
141 | - $this->cleanupAfter('testfilesizes'); |
|
142 | - $s3 = $this->getInstance(); |
|
143 | - |
|
144 | - $sourceStream = fopen('php://memory', 'wb+'); |
|
145 | - $writeChunkSize = 1024; |
|
146 | - $chunkCount = $size / $writeChunkSize; |
|
147 | - for ($i = 0; $i < $chunkCount; $i++) { |
|
148 | - fwrite($sourceStream, str_repeat('A', |
|
149 | - ($i < $chunkCount - 1) ? $writeChunkSize : $size - ($i * $writeChunkSize) |
|
150 | - )); |
|
151 | - } |
|
152 | - rewind($sourceStream); |
|
153 | - $s3->writeObject('testfilesizes', $sourceStream); |
|
63 | + public function testUploadNonSeekable(): void { |
|
64 | + $this->cleanupAfter('multiparttest'); |
|
65 | + |
|
66 | + $s3 = $this->getInstance(); |
|
67 | + |
|
68 | + $s3->writeObject('multiparttest', NonSeekableStream::wrap(fopen(__FILE__, 'r'))); |
|
69 | + |
|
70 | + $result = $s3->readObject('multiparttest'); |
|
71 | + |
|
72 | + $this->assertEquals(file_get_contents(__FILE__), stream_get_contents($result)); |
|
73 | + } |
|
74 | + |
|
75 | + public function testSeek(): void { |
|
76 | + $this->cleanupAfter('seek'); |
|
77 | + |
|
78 | + $data = file_get_contents(__FILE__); |
|
79 | + |
|
80 | + $instance = $this->getInstance(); |
|
81 | + $instance->writeObject('seek', $this->stringToStream($data)); |
|
82 | + |
|
83 | + $read = $instance->readObject('seek'); |
|
84 | + $this->assertEquals(substr($data, 0, 100), fread($read, 100)); |
|
85 | + |
|
86 | + fseek($read, 10); |
|
87 | + $this->assertEquals(substr($data, 10, 100), fread($read, 100)); |
|
88 | + |
|
89 | + fseek($read, 100, SEEK_CUR); |
|
90 | + $this->assertEquals(substr($data, 210, 100), fread($read, 100)); |
|
91 | + } |
|
92 | + |
|
93 | + public function assertNoUpload($objectUrn) { |
|
94 | + /** @var \OC\Files\ObjectStore\S3 */ |
|
95 | + $s3 = $this->getInstance(); |
|
96 | + $s3client = $s3->getConnection(); |
|
97 | + $uploads = $s3client->listMultipartUploads([ |
|
98 | + 'Bucket' => $s3->getBucket(), |
|
99 | + 'Prefix' => $objectUrn, |
|
100 | + ]); |
|
101 | + $this->assertArrayNotHasKey('Uploads', $uploads, 'Assert is not uploaded'); |
|
102 | + } |
|
103 | + |
|
104 | + public function testEmptyUpload(): void { |
|
105 | + $s3 = $this->getInstance(); |
|
106 | + |
|
107 | + $emptyStream = fopen('php://memory', 'r'); |
|
108 | + fwrite($emptyStream, ''); |
|
109 | + |
|
110 | + $s3->writeObject('emptystream', $emptyStream); |
|
111 | + |
|
112 | + $this->assertNoUpload('emptystream'); |
|
113 | + $this->assertTrue($s3->objectExists('emptystream'), 'Object exists on S3'); |
|
114 | + |
|
115 | + $thrown = false; |
|
116 | + try { |
|
117 | + self::assertFalse($s3->readObject('emptystream'), 'Reading empty stream object should return false'); |
|
118 | + } catch (\Exception $e) { |
|
119 | + // An exception is expected here since 0 byte files are wrapped |
|
120 | + // to be read from an empty memory stream in the ObjectStoreStorage |
|
121 | + $thrown = true; |
|
122 | + } |
|
123 | + self::assertTrue($thrown, 'readObject with range requests are not expected to work on empty objects'); |
|
124 | + |
|
125 | + $s3->deleteObject('emptystream'); |
|
126 | + } |
|
127 | + |
|
128 | + /** File size to upload in bytes */ |
|
129 | + public static function dataFileSizes(): array { |
|
130 | + return [ |
|
131 | + [1000000], [2000000], [5242879], [5242880], [5242881], [10000000] |
|
132 | + ]; |
|
133 | + } |
|
134 | + |
|
135 | + /** @dataProvider dataFileSizes */ |
|
136 | + public function testFileSizes($size): void { |
|
137 | + if (str_starts_with(PHP_VERSION, '8.3') && getenv('CI')) { |
|
138 | + $this->markTestSkipped('Test is unreliable and skipped on 8.3'); |
|
139 | + } |
|
140 | + |
|
141 | + $this->cleanupAfter('testfilesizes'); |
|
142 | + $s3 = $this->getInstance(); |
|
143 | + |
|
144 | + $sourceStream = fopen('php://memory', 'wb+'); |
|
145 | + $writeChunkSize = 1024; |
|
146 | + $chunkCount = $size / $writeChunkSize; |
|
147 | + for ($i = 0; $i < $chunkCount; $i++) { |
|
148 | + fwrite($sourceStream, str_repeat('A', |
|
149 | + ($i < $chunkCount - 1) ? $writeChunkSize : $size - ($i * $writeChunkSize) |
|
150 | + )); |
|
151 | + } |
|
152 | + rewind($sourceStream); |
|
153 | + $s3->writeObject('testfilesizes', $sourceStream); |
|
154 | 154 | |
155 | - $this->assertNoUpload('testfilesizes'); |
|
156 | - self::assertTrue($s3->objectExists('testfilesizes'), 'Object exists on S3'); |
|
155 | + $this->assertNoUpload('testfilesizes'); |
|
156 | + self::assertTrue($s3->objectExists('testfilesizes'), 'Object exists on S3'); |
|
157 | 157 | |
158 | - $result = $s3->readObject('testfilesizes'); |
|
158 | + $result = $s3->readObject('testfilesizes'); |
|
159 | 159 | |
160 | - // compare first 100 bytes |
|
161 | - self::assertEquals(str_repeat('A', 100), fread($result, 100), 'Compare first 100 bytes'); |
|
160 | + // compare first 100 bytes |
|
161 | + self::assertEquals(str_repeat('A', 100), fread($result, 100), 'Compare first 100 bytes'); |
|
162 | 162 | |
163 | - // compare last 100 bytes |
|
164 | - fseek($result, $size - 100); |
|
165 | - self::assertEquals(str_repeat('A', 100), fread($result, 100), 'Compare last 100 bytes'); |
|
163 | + // compare last 100 bytes |
|
164 | + fseek($result, $size - 100); |
|
165 | + self::assertEquals(str_repeat('A', 100), fread($result, 100), 'Compare last 100 bytes'); |
|
166 | 166 | |
167 | - // end of file reached |
|
168 | - fseek($result, $size); |
|
169 | - self::assertTrue(feof($result), 'End of file reached'); |
|
170 | - |
|
171 | - $this->assertNoUpload('testfilesizes'); |
|
172 | - } |
|
167 | + // end of file reached |
|
168 | + fseek($result, $size); |
|
169 | + self::assertTrue(feof($result), 'End of file reached'); |
|
170 | + |
|
171 | + $this->assertNoUpload('testfilesizes'); |
|
172 | + } |
|
173 | 173 | } |
@@ -12,52 +12,52 @@ |
||
12 | 12 | use OCP\IUser; |
13 | 13 | |
14 | 14 | class MapperTest extends \Test\TestCase { |
15 | - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */ |
|
16 | - private $user; |
|
17 | - |
|
18 | - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ |
|
19 | - private $config; |
|
20 | - |
|
21 | - /** @var Mapper */ |
|
22 | - private $mapper; |
|
23 | - |
|
24 | - protected function setUp(): void { |
|
25 | - parent::setUp(); |
|
26 | - |
|
27 | - $this->user = $this->createMock(IUser::class); |
|
28 | - $this->config = $this->createMock(IConfig::class); |
|
29 | - $this->mapper = new Mapper($this->user, $this->config); |
|
30 | - } |
|
31 | - |
|
32 | - public static function dataGetBucket(): array { |
|
33 | - return [ |
|
34 | - ['user', 64, 0, '17'], |
|
35 | - ['USER', 64, 0, '0'], |
|
36 | - ['bc0e8b52-a66c-1035-90c6-d9663bda9e3f', 64, 0, '56'], |
|
37 | - ['user', 8, 0, '1'], |
|
38 | - ['user', 2, 0, '1'], |
|
39 | - ['USER', 2, 0, '0'], |
|
40 | - ['user', 128, 64, '81'], |
|
41 | - ]; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * @dataProvider dataGetBucket |
|
46 | - * @param string $username |
|
47 | - * @param int $numBuckets |
|
48 | - * @param string $expectedBucket |
|
49 | - */ |
|
50 | - public function testGetBucket($username, $numBuckets, $bucketShift, $expectedBucket): void { |
|
51 | - $this->user->expects($this->once()) |
|
52 | - ->method('getUID') |
|
53 | - ->willReturn($username); |
|
54 | - |
|
55 | - $this->config->expects($this->once()) |
|
56 | - ->method('getSystemValue') |
|
57 | - ->with('objectstore_multibucket') |
|
58 | - ->willReturn(['arguments' => ['min_bucket' => $bucketShift]]); |
|
59 | - |
|
60 | - $result = $this->mapper->getBucket($numBuckets); |
|
61 | - $this->assertEquals($expectedBucket, $result); |
|
62 | - } |
|
15 | + /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */ |
|
16 | + private $user; |
|
17 | + |
|
18 | + /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ |
|
19 | + private $config; |
|
20 | + |
|
21 | + /** @var Mapper */ |
|
22 | + private $mapper; |
|
23 | + |
|
24 | + protected function setUp(): void { |
|
25 | + parent::setUp(); |
|
26 | + |
|
27 | + $this->user = $this->createMock(IUser::class); |
|
28 | + $this->config = $this->createMock(IConfig::class); |
|
29 | + $this->mapper = new Mapper($this->user, $this->config); |
|
30 | + } |
|
31 | + |
|
32 | + public static function dataGetBucket(): array { |
|
33 | + return [ |
|
34 | + ['user', 64, 0, '17'], |
|
35 | + ['USER', 64, 0, '0'], |
|
36 | + ['bc0e8b52-a66c-1035-90c6-d9663bda9e3f', 64, 0, '56'], |
|
37 | + ['user', 8, 0, '1'], |
|
38 | + ['user', 2, 0, '1'], |
|
39 | + ['USER', 2, 0, '0'], |
|
40 | + ['user', 128, 64, '81'], |
|
41 | + ]; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * @dataProvider dataGetBucket |
|
46 | + * @param string $username |
|
47 | + * @param int $numBuckets |
|
48 | + * @param string $expectedBucket |
|
49 | + */ |
|
50 | + public function testGetBucket($username, $numBuckets, $bucketShift, $expectedBucket): void { |
|
51 | + $this->user->expects($this->once()) |
|
52 | + ->method('getUID') |
|
53 | + ->willReturn($username); |
|
54 | + |
|
55 | + $this->config->expects($this->once()) |
|
56 | + ->method('getSystemValue') |
|
57 | + ->with('objectstore_multibucket') |
|
58 | + ->willReturn(['arguments' => ['min_bucket' => $bucketShift]]); |
|
59 | + |
|
60 | + $result = $this->mapper->getBucket($numBuckets); |
|
61 | + $this->assertEquals($expectedBucket, $result); |
|
62 | + } |
|
63 | 63 | } |
@@ -26,486 +26,486 @@ |
||
26 | 26 | |
27 | 27 | class FilenameValidatorTest extends TestCase { |
28 | 28 | |
29 | - protected IFactory&MockObject $l10n; |
|
30 | - protected IConfig&MockObject $config; |
|
31 | - protected IDBConnection&MockObject $database; |
|
32 | - protected LoggerInterface&MockObject $logger; |
|
33 | - |
|
34 | - protected function setUp(): void { |
|
35 | - parent::setUp(); |
|
36 | - $l10n = $this->createMock(IL10N::class); |
|
37 | - $l10n->method('t') |
|
38 | - ->willReturnCallback(fn ($string, $params) => sprintf($string, ...$params)); |
|
39 | - $this->l10n = $this->createMock(IFactory::class); |
|
40 | - $this->l10n |
|
41 | - ->method('get') |
|
42 | - ->with('core') |
|
43 | - ->willReturn($l10n); |
|
44 | - |
|
45 | - $this->config = $this->createMock(IConfig::class); |
|
46 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
47 | - $this->database = $this->createMock(IDBConnection::class); |
|
48 | - $this->database->method('supports4ByteText')->willReturn(true); |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @dataProvider dataValidateFilename |
|
53 | - */ |
|
54 | - public function testValidateFilename( |
|
55 | - string $filename, |
|
56 | - array $forbiddenNames, |
|
57 | - array $forbiddenBasenames, |
|
58 | - array $forbiddenExtensions, |
|
59 | - array $forbiddenCharacters, |
|
60 | - ?string $exception, |
|
61 | - ): void { |
|
62 | - /** @var FilenameValidator&MockObject */ |
|
63 | - $validator = $this->getMockBuilder(FilenameValidator::class) |
|
64 | - ->onlyMethods([ |
|
65 | - 'getForbiddenBasenames', |
|
66 | - 'getForbiddenCharacters', |
|
67 | - 'getForbiddenExtensions', |
|
68 | - 'getForbiddenFilenames', |
|
69 | - ]) |
|
70 | - ->setConstructorArgs([$this->l10n, $this->database, $this->config, $this->logger]) |
|
71 | - ->getMock(); |
|
72 | - |
|
73 | - $validator->method('getForbiddenBasenames') |
|
74 | - ->willReturn($forbiddenBasenames); |
|
75 | - $validator->method('getForbiddenCharacters') |
|
76 | - ->willReturn($forbiddenCharacters); |
|
77 | - $validator->method('getForbiddenExtensions') |
|
78 | - ->willReturn($forbiddenExtensions); |
|
79 | - $validator->method('getForbiddenFilenames') |
|
80 | - ->willReturn($forbiddenNames); |
|
81 | - |
|
82 | - if ($exception !== null) { |
|
83 | - $this->expectException($exception); |
|
84 | - } else { |
|
85 | - $this->expectNotToPerformAssertions(); |
|
86 | - } |
|
87 | - $validator->validateFilename($filename); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @dataProvider dataValidateFilename |
|
92 | - */ |
|
93 | - public function testIsFilenameValid( |
|
94 | - string $filename, |
|
95 | - array $forbiddenNames, |
|
96 | - array $forbiddenBasenames, |
|
97 | - array $forbiddenExtensions, |
|
98 | - array $forbiddenCharacters, |
|
99 | - ?string $exception, |
|
100 | - ): void { |
|
101 | - /** @var FilenameValidator&MockObject */ |
|
102 | - $validator = $this->getMockBuilder(FilenameValidator::class) |
|
103 | - ->onlyMethods([ |
|
104 | - 'getForbiddenBasenames', |
|
105 | - 'getForbiddenExtensions', |
|
106 | - 'getForbiddenFilenames', |
|
107 | - 'getForbiddenCharacters', |
|
108 | - ]) |
|
109 | - ->setConstructorArgs([$this->l10n, $this->database, $this->config, $this->logger]) |
|
110 | - ->getMock(); |
|
111 | - |
|
112 | - $validator->method('getForbiddenBasenames') |
|
113 | - ->willReturn($forbiddenBasenames); |
|
114 | - $validator->method('getForbiddenCharacters') |
|
115 | - ->willReturn($forbiddenCharacters); |
|
116 | - $validator->method('getForbiddenExtensions') |
|
117 | - ->willReturn($forbiddenExtensions); |
|
118 | - $validator->method('getForbiddenFilenames') |
|
119 | - ->willReturn($forbiddenNames); |
|
120 | - |
|
121 | - |
|
122 | - $this->assertEquals($exception === null, $validator->isFilenameValid($filename)); |
|
123 | - } |
|
124 | - |
|
125 | - public static function dataValidateFilename(): array { |
|
126 | - return [ |
|
127 | - 'valid name' => [ |
|
128 | - 'a: b.txt', ['.htaccess'], [], [], [], null |
|
129 | - ], |
|
130 | - 'forbidden name in the middle is ok' => [ |
|
131 | - 'a.htaccess.txt', ['.htaccess'], [], [], [], null |
|
132 | - ], |
|
133 | - 'valid name with some more parameters' => [ |
|
134 | - 'a: b.txt', ['.htaccess'], [], ['exe'], ['~'], null |
|
135 | - ], |
|
136 | - 'valid name checks only the full name' => [ |
|
137 | - '.htaccess.sample', ['.htaccess'], [], [], [], null |
|
138 | - ], |
|
139 | - 'forbidden name' => [ |
|
140 | - '.htaccess', ['.htaccess'], [], [], [], ReservedWordException::class |
|
141 | - ], |
|
142 | - 'forbidden name - name is case insensitive' => [ |
|
143 | - 'COM1', ['.htaccess', 'com1'], [], [], [], ReservedWordException::class |
|
144 | - ], |
|
145 | - 'forbidden basename' => [ |
|
146 | - // needed for Windows namespaces |
|
147 | - 'com1.suffix', ['.htaccess'], ['com1'], [], [], ReservedWordException::class |
|
148 | - ], |
|
149 | - 'forbidden basename case insensitive' => [ |
|
150 | - // needed for Windows namespaces |
|
151 | - 'COM1.suffix', ['.htaccess'], ['com1'], [], [], ReservedWordException::class |
|
152 | - ], |
|
153 | - 'forbidden basename for hidden files' => [ |
|
154 | - // needed for Windows namespaces |
|
155 | - '.thumbs.db', ['.htaccess'], ['.thumbs'], [], [], ReservedWordException::class |
|
156 | - ], |
|
157 | - 'invalid character' => [ |
|
158 | - 'a: b.txt', ['.htaccess'], [], [], [':'], InvalidCharacterInPathException::class |
|
159 | - ], |
|
160 | - 'invalid path' => [ |
|
161 | - '../../foo.bar', ['.htaccess'], [], [], ['/', '\\'], InvalidCharacterInPathException::class, |
|
162 | - ], |
|
163 | - 'invalid extension' => [ |
|
164 | - 'a: b.txt', ['.htaccess'], [], ['.txt'], [], InvalidPathException::class |
|
165 | - ], |
|
166 | - 'invalid extension case insensitive' => [ |
|
167 | - 'a: b.TXT', ['.htaccess'], [], ['.txt'], [], InvalidPathException::class |
|
168 | - ], |
|
169 | - 'empty filename' => [ |
|
170 | - '', [], [], [], [], EmptyFileNameException::class |
|
171 | - ], |
|
172 | - 'reserved unix name "."' => [ |
|
173 | - '.', [], [], [], [], InvalidDirectoryException::class |
|
174 | - ], |
|
175 | - 'reserved unix name ".."' => [ |
|
176 | - '..', [], [], [], [], InvalidDirectoryException::class |
|
177 | - ], |
|
178 | - 'weird but valid tripple dot name' => [ |
|
179 | - '...', [], [], [], [], null // is valid |
|
180 | - ], |
|
181 | - 'too long filename "."' => [ |
|
182 | - str_repeat('a', 251), [], [], [], [], FileNameTooLongException::class |
|
183 | - ], |
|
184 | - // make sure to not split the list entries as they migh contain Unicode sequences |
|
185 | - // in this example the "face in clouds" emoji contains the clouds emoji so only having clouds is ok |
|
186 | - [' |
@@ -17,289 +17,289 @@ |
||
17 | 17 | * @package Test\Files\Node |
18 | 18 | */ |
19 | 19 | class FileTest extends NodeTestCase { |
20 | - protected function createTestNode($root, $view, $path, array $data = [], $internalPath = '', $storage = null) { |
|
21 | - if ($data || $internalPath || $storage) { |
|
22 | - return new \OC\Files\Node\File($root, $view, $path, $this->getFileInfo($data, $internalPath, $storage)); |
|
23 | - } else { |
|
24 | - return new \OC\Files\Node\File($root, $view, $path); |
|
25 | - } |
|
26 | - } |
|
27 | - |
|
28 | - protected function getNodeClass() { |
|
29 | - return '\OC\Files\Node\File'; |
|
30 | - } |
|
31 | - |
|
32 | - protected function getNonExistingNodeClass() { |
|
33 | - return '\OC\Files\Node\NonExistingFile'; |
|
34 | - } |
|
35 | - |
|
36 | - protected function getViewDeleteMethod() { |
|
37 | - return 'unlink'; |
|
38 | - } |
|
39 | - |
|
40 | - public function testGetContent(): void { |
|
41 | - /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ |
|
42 | - $root = $this->getMockBuilder(Root::class) |
|
43 | - ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
44 | - ->getMock(); |
|
45 | - |
|
46 | - $hook = function ($file) { |
|
47 | - throw new \Exception('Hooks are not supposed to be called'); |
|
48 | - }; |
|
49 | - |
|
50 | - $root->listen('\OC\Files', 'preWrite', $hook); |
|
51 | - $root->listen('\OC\Files', 'postWrite', $hook); |
|
52 | - |
|
53 | - $this->view->expects($this->once()) |
|
54 | - ->method('file_get_contents') |
|
55 | - ->with('/bar/foo') |
|
56 | - ->willReturn('bar'); |
|
57 | - |
|
58 | - $this->view->expects($this->once()) |
|
59 | - ->method('getFileInfo') |
|
60 | - ->with('/bar/foo') |
|
61 | - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); |
|
62 | - |
|
63 | - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
64 | - $this->assertEquals('bar', $node->getContent()); |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - public function testGetContentNotPermitted(): void { |
|
69 | - $this->expectException(\OCP\Files\NotPermittedException::class); |
|
70 | - |
|
71 | - /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ |
|
72 | - $root = $this->getMockBuilder(Root::class) |
|
73 | - ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
74 | - ->getMock(); |
|
75 | - |
|
76 | - $root->expects($this->any()) |
|
77 | - ->method('getUser') |
|
78 | - ->willReturn($this->user); |
|
79 | - |
|
80 | - $this->view->expects($this->once()) |
|
81 | - ->method('getFileInfo') |
|
82 | - ->with('/bar/foo') |
|
83 | - ->willReturn($this->getFileInfo(['permissions' => 0])); |
|
84 | - |
|
85 | - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
86 | - $node->getContent(); |
|
87 | - } |
|
88 | - |
|
89 | - public function testPutContent(): void { |
|
90 | - /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ |
|
91 | - $root = $this->getMockBuilder(Root::class) |
|
92 | - ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
93 | - ->getMock(); |
|
94 | - |
|
95 | - $root->expects($this->any()) |
|
96 | - ->method('getUser') |
|
97 | - ->willReturn($this->user); |
|
98 | - |
|
99 | - $this->view->expects($this->once()) |
|
100 | - ->method('getFileInfo') |
|
101 | - ->with('/bar/foo') |
|
102 | - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); |
|
103 | - |
|
104 | - $this->view->expects($this->once()) |
|
105 | - ->method('file_put_contents') |
|
106 | - ->with('/bar/foo', 'bar') |
|
107 | - ->willReturn(true); |
|
108 | - |
|
109 | - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
110 | - $node->putContent('bar'); |
|
111 | - } |
|
112 | - |
|
113 | - |
|
114 | - public function testPutContentNotPermitted(): void { |
|
115 | - $this->expectException(\OCP\Files\NotPermittedException::class); |
|
116 | - |
|
117 | - /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ |
|
118 | - $root = $this->getMockBuilder(Root::class) |
|
119 | - ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
120 | - ->getMock(); |
|
121 | - |
|
122 | - $this->view->expects($this->once()) |
|
123 | - ->method('getFileInfo') |
|
124 | - ->with('/bar/foo') |
|
125 | - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); |
|
126 | - |
|
127 | - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
128 | - $node->putContent('bar'); |
|
129 | - } |
|
130 | - |
|
131 | - public function testGetMimeType(): void { |
|
132 | - /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ |
|
133 | - $root = $this->getMockBuilder(Root::class) |
|
134 | - ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
135 | - ->getMock(); |
|
136 | - |
|
137 | - $this->view->expects($this->once()) |
|
138 | - ->method('getFileInfo') |
|
139 | - ->with('/bar/foo') |
|
140 | - ->willReturn($this->getFileInfo(['mimetype' => 'text/plain'])); |
|
141 | - |
|
142 | - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
143 | - $this->assertEquals('text/plain', $node->getMimeType()); |
|
144 | - } |
|
145 | - |
|
146 | - public function testFOpenRead(): void { |
|
147 | - $stream = fopen('php://memory', 'w+'); |
|
148 | - fwrite($stream, 'bar'); |
|
149 | - rewind($stream); |
|
150 | - |
|
151 | - $root = new \OC\Files\Node\Root( |
|
152 | - $this->manager, |
|
153 | - $this->view, |
|
154 | - $this->user, |
|
155 | - $this->userMountCache, |
|
156 | - $this->logger, |
|
157 | - $this->userManager, |
|
158 | - $this->eventDispatcher, |
|
159 | - $this->cacheFactory, |
|
160 | - ); |
|
161 | - |
|
162 | - $hook = function ($file) { |
|
163 | - throw new \Exception('Hooks are not supposed to be called'); |
|
164 | - }; |
|
165 | - |
|
166 | - $root->listen('\OC\Files', 'preWrite', $hook); |
|
167 | - $root->listen('\OC\Files', 'postWrite', $hook); |
|
168 | - |
|
169 | - $this->view->expects($this->once()) |
|
170 | - ->method('fopen') |
|
171 | - ->with('/bar/foo', 'r') |
|
172 | - ->willReturn($stream); |
|
173 | - |
|
174 | - $this->view->expects($this->once()) |
|
175 | - ->method('getFileInfo') |
|
176 | - ->with('/bar/foo') |
|
177 | - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); |
|
178 | - |
|
179 | - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
180 | - $fh = $node->fopen('r'); |
|
181 | - $this->assertEquals($stream, $fh); |
|
182 | - $this->assertEquals('bar', fread($fh, 3)); |
|
183 | - } |
|
184 | - |
|
185 | - public function testFOpenWrite(): void { |
|
186 | - $stream = fopen('php://memory', 'w+'); |
|
187 | - |
|
188 | - $root = new \OC\Files\Node\Root( |
|
189 | - $this->manager, |
|
190 | - $this->view, |
|
191 | - $this->user, |
|
192 | - $this->userMountCache, |
|
193 | - $this->logger, |
|
194 | - $this->userManager, |
|
195 | - $this->eventDispatcher, |
|
196 | - $this->cacheFactory, |
|
197 | - ); |
|
198 | - $hooksCalled = 0; |
|
199 | - $hook = function ($file) use (&$hooksCalled) { |
|
200 | - $hooksCalled++; |
|
201 | - }; |
|
202 | - |
|
203 | - $root->listen('\OC\Files', 'preWrite', $hook); |
|
204 | - $root->listen('\OC\Files', 'postWrite', $hook); |
|
205 | - |
|
206 | - $this->view->expects($this->once()) |
|
207 | - ->method('fopen') |
|
208 | - ->with('/bar/foo', 'w') |
|
209 | - ->willReturn($stream); |
|
210 | - |
|
211 | - $this->view->expects($this->once()) |
|
212 | - ->method('getFileInfo') |
|
213 | - ->with('/bar/foo') |
|
214 | - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); |
|
215 | - |
|
216 | - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
217 | - $fh = $node->fopen('w'); |
|
218 | - $this->assertEquals($stream, $fh); |
|
219 | - fwrite($fh, 'bar'); |
|
220 | - rewind($fh); |
|
221 | - $this->assertEquals('bar', fread($stream, 3)); |
|
222 | - $this->assertEquals(2, $hooksCalled); |
|
223 | - } |
|
224 | - |
|
225 | - |
|
226 | - public function testFOpenReadNotPermitted(): void { |
|
227 | - $this->expectException(\OCP\Files\NotPermittedException::class); |
|
228 | - |
|
229 | - $root = new \OC\Files\Node\Root( |
|
230 | - $this->manager, |
|
231 | - $this->view, |
|
232 | - $this->user, |
|
233 | - $this->userMountCache, |
|
234 | - $this->logger, |
|
235 | - $this->userManager, |
|
236 | - $this->eventDispatcher, |
|
237 | - $this->cacheFactory, |
|
238 | - ); |
|
239 | - $hook = function ($file) { |
|
240 | - throw new \Exception('Hooks are not supposed to be called'); |
|
241 | - }; |
|
242 | - |
|
243 | - $this->view->expects($this->once()) |
|
244 | - ->method('getFileInfo') |
|
245 | - ->with('/bar/foo') |
|
246 | - ->willReturn($this->getFileInfo(['permissions' => 0])); |
|
247 | - |
|
248 | - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
249 | - $node->fopen('r'); |
|
250 | - } |
|
251 | - |
|
252 | - |
|
253 | - public function testFOpenReadWriteNoReadPermissions(): void { |
|
254 | - $this->expectException(\OCP\Files\NotPermittedException::class); |
|
255 | - |
|
256 | - $root = new \OC\Files\Node\Root( |
|
257 | - $this->manager, |
|
258 | - $this->view, |
|
259 | - $this->user, |
|
260 | - $this->userMountCache, |
|
261 | - $this->logger, |
|
262 | - $this->userManager, |
|
263 | - $this->eventDispatcher, |
|
264 | - $this->cacheFactory, |
|
265 | - ); |
|
266 | - $hook = function () { |
|
267 | - throw new \Exception('Hooks are not supposed to be called'); |
|
268 | - }; |
|
269 | - |
|
270 | - $this->view->expects($this->once()) |
|
271 | - ->method('getFileInfo') |
|
272 | - ->with('/bar/foo') |
|
273 | - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_UPDATE])); |
|
274 | - |
|
275 | - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
276 | - $node->fopen('w'); |
|
277 | - } |
|
278 | - |
|
279 | - |
|
280 | - public function testFOpenReadWriteNoWritePermissions(): void { |
|
281 | - $this->expectException(\OCP\Files\NotPermittedException::class); |
|
282 | - |
|
283 | - $root = new \OC\Files\Node\Root( |
|
284 | - $this->manager, |
|
285 | - $this->view, |
|
286 | - $this->user, |
|
287 | - $this->userMountCache, |
|
288 | - $this->logger, |
|
289 | - $this->userManager, |
|
290 | - $this->eventDispatcher, |
|
291 | - $this->cacheFactory, |
|
292 | - ); |
|
293 | - $hook = function () { |
|
294 | - throw new \Exception('Hooks are not supposed to be called'); |
|
295 | - }; |
|
296 | - |
|
297 | - $this->view->expects($this->once()) |
|
298 | - ->method('getFileInfo') |
|
299 | - ->with('/bar/foo') |
|
300 | - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); |
|
301 | - |
|
302 | - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
303 | - $node->fopen('w'); |
|
304 | - } |
|
20 | + protected function createTestNode($root, $view, $path, array $data = [], $internalPath = '', $storage = null) { |
|
21 | + if ($data || $internalPath || $storage) { |
|
22 | + return new \OC\Files\Node\File($root, $view, $path, $this->getFileInfo($data, $internalPath, $storage)); |
|
23 | + } else { |
|
24 | + return new \OC\Files\Node\File($root, $view, $path); |
|
25 | + } |
|
26 | + } |
|
27 | + |
|
28 | + protected function getNodeClass() { |
|
29 | + return '\OC\Files\Node\File'; |
|
30 | + } |
|
31 | + |
|
32 | + protected function getNonExistingNodeClass() { |
|
33 | + return '\OC\Files\Node\NonExistingFile'; |
|
34 | + } |
|
35 | + |
|
36 | + protected function getViewDeleteMethod() { |
|
37 | + return 'unlink'; |
|
38 | + } |
|
39 | + |
|
40 | + public function testGetContent(): void { |
|
41 | + /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ |
|
42 | + $root = $this->getMockBuilder(Root::class) |
|
43 | + ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
44 | + ->getMock(); |
|
45 | + |
|
46 | + $hook = function ($file) { |
|
47 | + throw new \Exception('Hooks are not supposed to be called'); |
|
48 | + }; |
|
49 | + |
|
50 | + $root->listen('\OC\Files', 'preWrite', $hook); |
|
51 | + $root->listen('\OC\Files', 'postWrite', $hook); |
|
52 | + |
|
53 | + $this->view->expects($this->once()) |
|
54 | + ->method('file_get_contents') |
|
55 | + ->with('/bar/foo') |
|
56 | + ->willReturn('bar'); |
|
57 | + |
|
58 | + $this->view->expects($this->once()) |
|
59 | + ->method('getFileInfo') |
|
60 | + ->with('/bar/foo') |
|
61 | + ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); |
|
62 | + |
|
63 | + $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
64 | + $this->assertEquals('bar', $node->getContent()); |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + public function testGetContentNotPermitted(): void { |
|
69 | + $this->expectException(\OCP\Files\NotPermittedException::class); |
|
70 | + |
|
71 | + /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ |
|
72 | + $root = $this->getMockBuilder(Root::class) |
|
73 | + ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
74 | + ->getMock(); |
|
75 | + |
|
76 | + $root->expects($this->any()) |
|
77 | + ->method('getUser') |
|
78 | + ->willReturn($this->user); |
|
79 | + |
|
80 | + $this->view->expects($this->once()) |
|
81 | + ->method('getFileInfo') |
|
82 | + ->with('/bar/foo') |
|
83 | + ->willReturn($this->getFileInfo(['permissions' => 0])); |
|
84 | + |
|
85 | + $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
86 | + $node->getContent(); |
|
87 | + } |
|
88 | + |
|
89 | + public function testPutContent(): void { |
|
90 | + /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ |
|
91 | + $root = $this->getMockBuilder(Root::class) |
|
92 | + ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
93 | + ->getMock(); |
|
94 | + |
|
95 | + $root->expects($this->any()) |
|
96 | + ->method('getUser') |
|
97 | + ->willReturn($this->user); |
|
98 | + |
|
99 | + $this->view->expects($this->once()) |
|
100 | + ->method('getFileInfo') |
|
101 | + ->with('/bar/foo') |
|
102 | + ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); |
|
103 | + |
|
104 | + $this->view->expects($this->once()) |
|
105 | + ->method('file_put_contents') |
|
106 | + ->with('/bar/foo', 'bar') |
|
107 | + ->willReturn(true); |
|
108 | + |
|
109 | + $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
110 | + $node->putContent('bar'); |
|
111 | + } |
|
112 | + |
|
113 | + |
|
114 | + public function testPutContentNotPermitted(): void { |
|
115 | + $this->expectException(\OCP\Files\NotPermittedException::class); |
|
116 | + |
|
117 | + /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ |
|
118 | + $root = $this->getMockBuilder(Root::class) |
|
119 | + ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
120 | + ->getMock(); |
|
121 | + |
|
122 | + $this->view->expects($this->once()) |
|
123 | + ->method('getFileInfo') |
|
124 | + ->with('/bar/foo') |
|
125 | + ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); |
|
126 | + |
|
127 | + $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
128 | + $node->putContent('bar'); |
|
129 | + } |
|
130 | + |
|
131 | + public function testGetMimeType(): void { |
|
132 | + /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ |
|
133 | + $root = $this->getMockBuilder(Root::class) |
|
134 | + ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
135 | + ->getMock(); |
|
136 | + |
|
137 | + $this->view->expects($this->once()) |
|
138 | + ->method('getFileInfo') |
|
139 | + ->with('/bar/foo') |
|
140 | + ->willReturn($this->getFileInfo(['mimetype' => 'text/plain'])); |
|
141 | + |
|
142 | + $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
143 | + $this->assertEquals('text/plain', $node->getMimeType()); |
|
144 | + } |
|
145 | + |
|
146 | + public function testFOpenRead(): void { |
|
147 | + $stream = fopen('php://memory', 'w+'); |
|
148 | + fwrite($stream, 'bar'); |
|
149 | + rewind($stream); |
|
150 | + |
|
151 | + $root = new \OC\Files\Node\Root( |
|
152 | + $this->manager, |
|
153 | + $this->view, |
|
154 | + $this->user, |
|
155 | + $this->userMountCache, |
|
156 | + $this->logger, |
|
157 | + $this->userManager, |
|
158 | + $this->eventDispatcher, |
|
159 | + $this->cacheFactory, |
|
160 | + ); |
|
161 | + |
|
162 | + $hook = function ($file) { |
|
163 | + throw new \Exception('Hooks are not supposed to be called'); |
|
164 | + }; |
|
165 | + |
|
166 | + $root->listen('\OC\Files', 'preWrite', $hook); |
|
167 | + $root->listen('\OC\Files', 'postWrite', $hook); |
|
168 | + |
|
169 | + $this->view->expects($this->once()) |
|
170 | + ->method('fopen') |
|
171 | + ->with('/bar/foo', 'r') |
|
172 | + ->willReturn($stream); |
|
173 | + |
|
174 | + $this->view->expects($this->once()) |
|
175 | + ->method('getFileInfo') |
|
176 | + ->with('/bar/foo') |
|
177 | + ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); |
|
178 | + |
|
179 | + $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
180 | + $fh = $node->fopen('r'); |
|
181 | + $this->assertEquals($stream, $fh); |
|
182 | + $this->assertEquals('bar', fread($fh, 3)); |
|
183 | + } |
|
184 | + |
|
185 | + public function testFOpenWrite(): void { |
|
186 | + $stream = fopen('php://memory', 'w+'); |
|
187 | + |
|
188 | + $root = new \OC\Files\Node\Root( |
|
189 | + $this->manager, |
|
190 | + $this->view, |
|
191 | + $this->user, |
|
192 | + $this->userMountCache, |
|
193 | + $this->logger, |
|
194 | + $this->userManager, |
|
195 | + $this->eventDispatcher, |
|
196 | + $this->cacheFactory, |
|
197 | + ); |
|
198 | + $hooksCalled = 0; |
|
199 | + $hook = function ($file) use (&$hooksCalled) { |
|
200 | + $hooksCalled++; |
|
201 | + }; |
|
202 | + |
|
203 | + $root->listen('\OC\Files', 'preWrite', $hook); |
|
204 | + $root->listen('\OC\Files', 'postWrite', $hook); |
|
205 | + |
|
206 | + $this->view->expects($this->once()) |
|
207 | + ->method('fopen') |
|
208 | + ->with('/bar/foo', 'w') |
|
209 | + ->willReturn($stream); |
|
210 | + |
|
211 | + $this->view->expects($this->once()) |
|
212 | + ->method('getFileInfo') |
|
213 | + ->with('/bar/foo') |
|
214 | + ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); |
|
215 | + |
|
216 | + $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
217 | + $fh = $node->fopen('w'); |
|
218 | + $this->assertEquals($stream, $fh); |
|
219 | + fwrite($fh, 'bar'); |
|
220 | + rewind($fh); |
|
221 | + $this->assertEquals('bar', fread($stream, 3)); |
|
222 | + $this->assertEquals(2, $hooksCalled); |
|
223 | + } |
|
224 | + |
|
225 | + |
|
226 | + public function testFOpenReadNotPermitted(): void { |
|
227 | + $this->expectException(\OCP\Files\NotPermittedException::class); |
|
228 | + |
|
229 | + $root = new \OC\Files\Node\Root( |
|
230 | + $this->manager, |
|
231 | + $this->view, |
|
232 | + $this->user, |
|
233 | + $this->userMountCache, |
|
234 | + $this->logger, |
|
235 | + $this->userManager, |
|
236 | + $this->eventDispatcher, |
|
237 | + $this->cacheFactory, |
|
238 | + ); |
|
239 | + $hook = function ($file) { |
|
240 | + throw new \Exception('Hooks are not supposed to be called'); |
|
241 | + }; |
|
242 | + |
|
243 | + $this->view->expects($this->once()) |
|
244 | + ->method('getFileInfo') |
|
245 | + ->with('/bar/foo') |
|
246 | + ->willReturn($this->getFileInfo(['permissions' => 0])); |
|
247 | + |
|
248 | + $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
249 | + $node->fopen('r'); |
|
250 | + } |
|
251 | + |
|
252 | + |
|
253 | + public function testFOpenReadWriteNoReadPermissions(): void { |
|
254 | + $this->expectException(\OCP\Files\NotPermittedException::class); |
|
255 | + |
|
256 | + $root = new \OC\Files\Node\Root( |
|
257 | + $this->manager, |
|
258 | + $this->view, |
|
259 | + $this->user, |
|
260 | + $this->userMountCache, |
|
261 | + $this->logger, |
|
262 | + $this->userManager, |
|
263 | + $this->eventDispatcher, |
|
264 | + $this->cacheFactory, |
|
265 | + ); |
|
266 | + $hook = function () { |
|
267 | + throw new \Exception('Hooks are not supposed to be called'); |
|
268 | + }; |
|
269 | + |
|
270 | + $this->view->expects($this->once()) |
|
271 | + ->method('getFileInfo') |
|
272 | + ->with('/bar/foo') |
|
273 | + ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_UPDATE])); |
|
274 | + |
|
275 | + $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
276 | + $node->fopen('w'); |
|
277 | + } |
|
278 | + |
|
279 | + |
|
280 | + public function testFOpenReadWriteNoWritePermissions(): void { |
|
281 | + $this->expectException(\OCP\Files\NotPermittedException::class); |
|
282 | + |
|
283 | + $root = new \OC\Files\Node\Root( |
|
284 | + $this->manager, |
|
285 | + $this->view, |
|
286 | + $this->user, |
|
287 | + $this->userMountCache, |
|
288 | + $this->logger, |
|
289 | + $this->userManager, |
|
290 | + $this->eventDispatcher, |
|
291 | + $this->cacheFactory, |
|
292 | + ); |
|
293 | + $hook = function () { |
|
294 | + throw new \Exception('Hooks are not supposed to be called'); |
|
295 | + }; |
|
296 | + |
|
297 | + $this->view->expects($this->once()) |
|
298 | + ->method('getFileInfo') |
|
299 | + ->with('/bar/foo') |
|
300 | + ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); |
|
301 | + |
|
302 | + $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); |
|
303 | + $node->fopen('w'); |
|
304 | + } |
|
305 | 305 | } |