Blame view

sources/apps/files_sharing/tests/cache.php 12 KB
a293d369c   Kload   Update sources to...
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
  <?php
  /**
   * ownCloud
   *
   * @author Vincent Petry, Bjoern Schiessle
   * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
   *            2014 Bjoern Schiessle <schiessle@owncloud.com>
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
  require_once __DIR__ . '/base.php';
  
  class Test_Files_Sharing_Cache extends Test_Files_Sharing_Base {
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
28
  	 * @var OC\Files\View
a293d369c   Kload   Update sources to...
29
30
31
32
33
  	 */
  	public $user2View;
  
  	function setUp() {
  		parent::setUp();
6d9380f96   Cédric Dupont   Update sources OC...
34
35
  		\OC_User::setDisplayName(self::TEST_FILES_SHARING_API_USER1, 'User One');
  		\OC_User::setDisplayName(self::TEST_FILES_SHARING_API_USER2, 'User Two');
a293d369c   Kload   Update sources to...
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
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  
  		$this->user2View = new \OC\Files\View('/'. self::TEST_FILES_SHARING_API_USER2 . '/files');
  
  		// prepare user1's dir structure
  		$this->view->mkdir('container');
  		$this->view->mkdir('container/shareddir');
  		$this->view->mkdir('container/shareddir/subdir');
  		$this->view->mkdir('container/shareddir/emptydir');
  
  		$textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  		$this->view->file_put_contents('container/not shared.txt', $textData);
  		$this->view->file_put_contents('container/shared single file.txt', $textData);
  		$this->view->file_put_contents('container/shareddir/bar.txt', $textData);
  		$this->view->file_put_contents('container/shareddir/subdir/another.txt', $textData);
  		$this->view->file_put_contents('container/shareddir/subdir/another too.txt', $textData);
  		$this->view->file_put_contents('container/shareddir/subdir/not a text file.xml', '<xml></xml>');
  
  		list($this->ownerStorage, $internalPath) = $this->view->resolvePath('');
  		$this->ownerCache = $this->ownerStorage->getCache();
  		$this->ownerStorage->getScanner()->scan('');
  
  		// share "shareddir" with user2
  		$fileinfo = $this->view->getFileInfo('container/shareddir');
  		\OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  			self::TEST_FILES_SHARING_API_USER2, 31);
  
  		$fileinfo = $this->view->getFileInfo('container/shared single file.txt');
  		\OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  			self::TEST_FILES_SHARING_API_USER2, 31);
  
  		// login as user2
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  
  		// retrieve the shared storage
  		$secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
6d9380f96   Cédric Dupont   Update sources OC...
72
  		list($this->sharedStorage, $internalPath) = $secondView->resolvePath('files/shareddir');
a293d369c   Kload   Update sources to...
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  		$this->sharedCache = $this->sharedStorage->getCache();
  	}
  
  	function tearDown() {
  		$this->sharedCache->clear();
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  
  		$fileinfo = $this->view->getFileInfo('container/shareddir');
  		\OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  			self::TEST_FILES_SHARING_API_USER2);
  
  		$fileinfo = $this->view->getFileInfo('container/shared single file.txt');
  		\OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  			self::TEST_FILES_SHARING_API_USER2);
  
  		$this->view->deleteAll('container');
  
  		$this->ownerCache->clear();
  
  		parent::tearDown();
  	}
f7d878ff1   kload   [enh] Update to 7...
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
  	function searchDataProvider() {
  		return array(
  			array('%another%',
  				array(
  					array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  					array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  				)
  			),
  			array('%Another%',
  				array(
  					array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  					array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  				)
  			),
  			array('%dir%',
  				array(
  					array('name' => 'emptydir', 'path' => 'emptydir'),
  					array('name' => 'subdir', 'path' => 'subdir'),
  					array('name' => 'shareddir', 'path' => ''),
  				)
  			),
  			array('%Dir%',
  				array(
  					array('name' => 'emptydir', 'path' => 'emptydir'),
  					array('name' => 'subdir', 'path' => 'subdir'),
  					array('name' => 'shareddir', 'path' => ''),
  				)
  			),
  			array('%txt%',
  				array(
  					array('name' => 'bar.txt', 'path' => 'bar.txt'),
  					array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  					array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  				)
  			),
  			array('%Txt%',
  				array(
  					array('name' => 'bar.txt', 'path' => 'bar.txt'),
  					array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  					array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  				)
  			),
  			array('%',
  				array(
  					array('name' => 'bar.txt', 'path' => 'bar.txt'),
  					array('name' => 'emptydir', 'path' => 'emptydir'),
  					array('name' => 'subdir', 'path' => 'subdir'),
  					array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  					array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  					array('name' => 'not a text file.xml', 'path' => 'subdir/not a text file.xml'),
  					array('name' => 'shareddir', 'path' => ''),
  				)
  			),
  			array('%nonexistant%',
  				array(
  				)
  			),
  		);
  	}
  
  	/**
  	 * we cannot use a dataProvider because that would cause the stray hook detection to remove the hooks
  	 * that were added in setUpBeforeClass.
  	 */
  	function testSearch() {
  		foreach ($this->searchDataProvider() as $data) {
  			list($pattern, $expectedFiles) = $data;
  
  			$results = $this->sharedStorage->getCache()->search($pattern);
  
  			$this->verifyFiles($expectedFiles, $results);
  		}
  
  	}
a293d369c   Kload   Update sources to...
169
170
171
172
173
174
175
  	/**
  	 * Test searching by mime type
  	 */
  	function testSearchByMime() {
  		$results = $this->sharedStorage->getCache()->searchByMime('text');
  		$check = array(
  				array(
a293d369c   Kload   Update sources to...
176
  					'name' => 'bar.txt',
6d9380f96   Cédric Dupont   Update sources OC...
177
  					'path' => 'bar.txt'
a293d369c   Kload   Update sources to...
178
179
180
  				),
  				array(
  					'name' => 'another too.txt',
6d9380f96   Cédric Dupont   Update sources OC...
181
  					'path' => 'subdir/another too.txt'
a293d369c   Kload   Update sources to...
182
183
184
  				),
  				array(
  					'name' => 'another.txt',
6d9380f96   Cédric Dupont   Update sources OC...
185
  					'path' => 'subdir/another.txt'
a293d369c   Kload   Update sources to...
186
187
188
  				),
  			);
  		$this->verifyFiles($check, $results);
a293d369c   Kload   Update sources to...
189
190
191
  	}
  
  	function testGetFolderContentsInRoot() {
6d9380f96   Cédric Dupont   Update sources OC...
192
  		$results = $this->user2View->getDirectoryContent('/');
a293d369c   Kload   Update sources to...
193

6d9380f96   Cédric Dupont   Update sources OC...
194
195
196
  		// we should get the shared items "shareddir" and "shared single file.txt"
  		// additional root will always contain the example file "welcome.txt",
  		//  so this will be part of the result
a293d369c   Kload   Update sources to...
197
198
199
  		$this->verifyFiles(
  			array(
  				array(
6d9380f96   Cédric Dupont   Update sources OC...
200
201
202
203
204
  					'name' => 'welcome.txt',
  					'path' => 'files/welcome.txt',
  					'mimetype' => 'text/plain',
  				),
  				array(
a293d369c   Kload   Update sources to...
205
  					'name' => 'shareddir',
6d9380f96   Cédric Dupont   Update sources OC...
206
  					'path' => 'files/shareddir',
a293d369c   Kload   Update sources to...
207
  					'mimetype' => 'httpd/unix-directory',
6d9380f96   Cédric Dupont   Update sources OC...
208
209
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
210
211
212
  				),
  				array(
  					'name' => 'shared single file.txt',
6d9380f96   Cédric Dupont   Update sources OC...
213
  					'path' => 'files/shared single file.txt',
a293d369c   Kload   Update sources to...
214
  					'mimetype' => 'text/plain',
6d9380f96   Cédric Dupont   Update sources OC...
215
216
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
217
218
219
220
221
222
223
  				),
  			),
  			$results
  		);
  	}
  
  	function testGetFolderContentsInSubdir() {
6d9380f96   Cédric Dupont   Update sources OC...
224
  		$results = $this->user2View->getDirectoryContent('/shareddir');
a293d369c   Kload   Update sources to...
225
226
227
228
229
  
  		$this->verifyFiles(
  			array(
  				array(
  					'name' => 'bar.txt',
6d9380f96   Cédric Dupont   Update sources OC...
230
  					'path' => 'bar.txt',
a293d369c   Kload   Update sources to...
231
  					'mimetype' => 'text/plain',
6d9380f96   Cédric Dupont   Update sources OC...
232
233
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
234
235
236
  				),
  				array(
  					'name' => 'emptydir',
6d9380f96   Cédric Dupont   Update sources OC...
237
  					'path' => 'emptydir',
a293d369c   Kload   Update sources to...
238
  					'mimetype' => 'httpd/unix-directory',
6d9380f96   Cédric Dupont   Update sources OC...
239
240
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
241
242
243
  				),
  				array(
  					'name' => 'subdir',
6d9380f96   Cédric Dupont   Update sources OC...
244
  					'path' => 'subdir',
a293d369c   Kload   Update sources to...
245
  					'mimetype' => 'httpd/unix-directory',
6d9380f96   Cédric Dupont   Update sources OC...
246
247
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  				),
  			),
  			$results
  		);
  	}
  
  	function testGetFolderContentsWhenSubSubdirShared() {
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  
  		$fileinfo = $this->view->getFileInfo('container/shareddir/subdir');
  		\OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  			self::TEST_FILES_SHARING_API_USER3, 31);
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  
  		$thirdView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files');
6d9380f96   Cédric Dupont   Update sources OC...
264
  		$results = $thirdView->getDirectoryContent('/subdir');
a293d369c   Kload   Update sources to...
265
266
267
268
269
  
  		$this->verifyFiles(
  			array(
  				array(
  					'name' => 'another too.txt',
6d9380f96   Cédric Dupont   Update sources OC...
270
  					'path' => 'another too.txt',
a293d369c   Kload   Update sources to...
271
  					'mimetype' => 'text/plain',
6d9380f96   Cédric Dupont   Update sources OC...
272
273
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
274
275
276
  				),
  				array(
  					'name' => 'another.txt',
6d9380f96   Cédric Dupont   Update sources OC...
277
  					'path' => 'another.txt',
a293d369c   Kload   Update sources to...
278
  					'mimetype' => 'text/plain',
6d9380f96   Cédric Dupont   Update sources OC...
279
280
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
281
282
283
  				),
  				array(
  					'name' => 'not a text file.xml',
6d9380f96   Cédric Dupont   Update sources OC...
284
  					'path' => 'not a text file.xml',
a293d369c   Kload   Update sources to...
285
  					'mimetype' => 'application/xml',
6d9380f96   Cédric Dupont   Update sources OC...
286
287
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
  				),
  			),
  			$results
  		);
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  
  		\OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  			self::TEST_FILES_SHARING_API_USER3);
  	}
  
  	/**
  	 * Check if 'results' contains the expected 'examples' only.
  	 *
  	 * @param array $examples array of example files
  	 * @param array $results array of files
  	 */
  	private function verifyFiles($examples, $results) {
  		$this->assertEquals(count($examples), count($results));
  
  		foreach ($examples as $example) {
  			foreach ($results as $key => $result) {
  				if ($result['name'] === $example['name']) {
  					$this->verifyKeys($example, $result);
  					unset($results[$key]);
  					break;
  				}
  			}
  		}
f7d878ff1   kload   [enh] Update to 7...
317
  		$this->assertEquals(array(), $results);
a293d369c   Kload   Update sources to...
318
319
320
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
321
  	 * verify if each value from the result matches the expected result
a293d369c   Kload   Update sources to...
322
323
324
325
326
327
328
329
  	 * @param array $example array with the expected results
  	 * @param array $result array with the results
  	 */
  	private function verifyKeys($example, $result) {
  		foreach ($example as $key => $value) {
  			$this->assertEquals($value, $result[$key]);
  		}
  	}
837968727   Kload   [enh] Upgrade to ...
330
331
332
333
  	public function testGetPathByIdDirectShare() {
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  		\OC\Files\Filesystem::file_put_contents('test.txt', 'foo');
  		$info = \OC\Files\Filesystem::getFileInfo('test.txt');
6d9380f96   Cédric Dupont   Update sources OC...
334
  		\OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\PERMISSION_ALL);
837968727   Kload   [enh] Upgrade to ...
335
336
337
  		\OC_Util::tearDownFS();
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
6d9380f96   Cédric Dupont   Update sources OC...
338
339
  		$this->assertTrue(\OC\Files\Filesystem::file_exists('/test.txt'));
  		list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/test.txt');
837968727   Kload   [enh] Upgrade to ...
340
341
342
343
344
  		/**
  		 * @var \OC\Files\Storage\Shared $sharedStorage
  		 */
  
  		$sharedCache = $sharedStorage->getCache();
6d9380f96   Cédric Dupont   Update sources OC...
345
  		$this->assertEquals('', $sharedCache->getPathById($info->getId()));
837968727   Kload   [enh] Upgrade to ...
346
347
348
349
350
351
352
353
354
  	}
  
  	public function testGetPathByIdShareSubFolder() {
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  		\OC\Files\Filesystem::mkdir('foo');
  		\OC\Files\Filesystem::mkdir('foo/bar');
  		\OC\Files\Filesystem::touch('foo/bar/test.txt', 'bar');
  		$folderInfo = \OC\Files\Filesystem::getFileInfo('foo');
  		$fileInfo = \OC\Files\Filesystem::getFileInfo('foo/bar/test.txt');
6d9380f96   Cédric Dupont   Update sources OC...
355
  		\OCP\Share::shareItem('folder', $folderInfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\PERMISSION_ALL);
837968727   Kload   [enh] Upgrade to ...
356
357
358
  		\OC_Util::tearDownFS();
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
6d9380f96   Cédric Dupont   Update sources OC...
359
360
  		$this->assertTrue(\OC\Files\Filesystem::file_exists('/foo'));
  		list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo');
837968727   Kload   [enh] Upgrade to ...
361
362
363
364
365
  		/**
  		 * @var \OC\Files\Storage\Shared $sharedStorage
  		 */
  
  		$sharedCache = $sharedStorage->getCache();
6d9380f96   Cédric Dupont   Update sources OC...
366
367
  		$this->assertEquals('', $sharedCache->getPathById($folderInfo->getId()));
  		$this->assertEquals('bar/test.txt', $sharedCache->getPathById($fileInfo->getId()));
837968727   Kload   [enh] Upgrade to ...
368
  	}
a293d369c   Kload   Update sources to...
369
  }