Blame view

sources/apps/files_sharing/tests/cache.php 9.82 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
95
96
97
98
99
100
101
102
  		$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();
  	}
  
  	/**
  	 * Test searching by mime type
  	 */
  	function testSearchByMime() {
  		$results = $this->sharedStorage->getCache()->searchByMime('text');
  		$check = array(
  				array(
a293d369c   Kload   Update sources to...
103
  					'name' => 'bar.txt',
6d9380f96   Cédric Dupont   Update sources OC...
104
  					'path' => 'bar.txt'
a293d369c   Kload   Update sources to...
105
106
107
  				),
  				array(
  					'name' => 'another too.txt',
6d9380f96   Cédric Dupont   Update sources OC...
108
  					'path' => 'subdir/another too.txt'
a293d369c   Kload   Update sources to...
109
110
111
  				),
  				array(
  					'name' => 'another.txt',
6d9380f96   Cédric Dupont   Update sources OC...
112
  					'path' => 'subdir/another.txt'
a293d369c   Kload   Update sources to...
113
114
115
  				),
  			);
  		$this->verifyFiles($check, $results);
a293d369c   Kload   Update sources to...
116
117
118
119
  		$this->verifyFiles($check, $results);
  	}
  
  	function testGetFolderContentsInRoot() {
6d9380f96   Cédric Dupont   Update sources OC...
120
  		$results = $this->user2View->getDirectoryContent('/');
a293d369c   Kload   Update sources to...
121

6d9380f96   Cédric Dupont   Update sources OC...
122
123
124
  		// 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...
125
126
127
  		$this->verifyFiles(
  			array(
  				array(
6d9380f96   Cédric Dupont   Update sources OC...
128
129
130
131
132
  					'name' => 'welcome.txt',
  					'path' => 'files/welcome.txt',
  					'mimetype' => 'text/plain',
  				),
  				array(
a293d369c   Kload   Update sources to...
133
  					'name' => 'shareddir',
6d9380f96   Cédric Dupont   Update sources OC...
134
  					'path' => 'files/shareddir',
a293d369c   Kload   Update sources to...
135
  					'mimetype' => 'httpd/unix-directory',
6d9380f96   Cédric Dupont   Update sources OC...
136
137
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
138
139
140
  				),
  				array(
  					'name' => 'shared single file.txt',
6d9380f96   Cédric Dupont   Update sources OC...
141
  					'path' => 'files/shared single file.txt',
a293d369c   Kload   Update sources to...
142
  					'mimetype' => 'text/plain',
6d9380f96   Cédric Dupont   Update sources OC...
143
144
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
145
146
147
148
149
150
151
  				),
  			),
  			$results
  		);
  	}
  
  	function testGetFolderContentsInSubdir() {
6d9380f96   Cédric Dupont   Update sources OC...
152
  		$results = $this->user2View->getDirectoryContent('/shareddir');
a293d369c   Kload   Update sources to...
153
154
155
156
157
  
  		$this->verifyFiles(
  			array(
  				array(
  					'name' => 'bar.txt',
6d9380f96   Cédric Dupont   Update sources OC...
158
  					'path' => 'bar.txt',
a293d369c   Kload   Update sources to...
159
  					'mimetype' => 'text/plain',
6d9380f96   Cédric Dupont   Update sources OC...
160
161
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
162
163
164
  				),
  				array(
  					'name' => 'emptydir',
6d9380f96   Cédric Dupont   Update sources OC...
165
  					'path' => 'emptydir',
a293d369c   Kload   Update sources to...
166
  					'mimetype' => 'httpd/unix-directory',
6d9380f96   Cédric Dupont   Update sources OC...
167
168
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
169
170
171
  				),
  				array(
  					'name' => 'subdir',
6d9380f96   Cédric Dupont   Update sources OC...
172
  					'path' => 'subdir',
a293d369c   Kload   Update sources to...
173
  					'mimetype' => 'httpd/unix-directory',
6d9380f96   Cédric Dupont   Update sources OC...
174
175
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  				),
  			),
  			$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...
192
  		$results = $thirdView->getDirectoryContent('/subdir');
a293d369c   Kload   Update sources to...
193
194
195
196
197
  
  		$this->verifyFiles(
  			array(
  				array(
  					'name' => 'another too.txt',
6d9380f96   Cédric Dupont   Update sources OC...
198
  					'path' => 'another too.txt',
a293d369c   Kload   Update sources to...
199
  					'mimetype' => 'text/plain',
6d9380f96   Cédric Dupont   Update sources OC...
200
201
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
202
203
204
  				),
  				array(
  					'name' => 'another.txt',
6d9380f96   Cédric Dupont   Update sources OC...
205
  					'path' => 'another.txt',
a293d369c   Kload   Update sources to...
206
  					'mimetype' => 'text/plain',
6d9380f96   Cédric Dupont   Update sources OC...
207
208
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
209
210
211
  				),
  				array(
  					'name' => 'not a text file.xml',
6d9380f96   Cédric Dupont   Update sources OC...
212
  					'path' => 'not a text file.xml',
a293d369c   Kload   Update sources to...
213
  					'mimetype' => 'application/xml',
6d9380f96   Cédric Dupont   Update sources OC...
214
215
  					'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  					'displayname_owner' => 'User One',
a293d369c   Kload   Update sources to...
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  				),
  			),
  			$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;
  				}
  			}
  		}
  		$this->assertTrue(empty($results));
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
249
  	 * verify if each value from the result matches the expected result
a293d369c   Kload   Update sources to...
250
251
252
253
254
255
256
257
  	 * @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 ...
258
259
260
261
  	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...
262
  		\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 ...
263
264
265
  		\OC_Util::tearDownFS();
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
6d9380f96   Cédric Dupont   Update sources OC...
266
267
  		$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 ...
268
269
270
271
272
  		/**
  		 * @var \OC\Files\Storage\Shared $sharedStorage
  		 */
  
  		$sharedCache = $sharedStorage->getCache();
6d9380f96   Cédric Dupont   Update sources OC...
273
  		$this->assertEquals('', $sharedCache->getPathById($info->getId()));
837968727   Kload   [enh] Upgrade to ...
274
275
276
277
278
279
280
281
282
  	}
  
  	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...
283
  		\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 ...
284
285
286
  		\OC_Util::tearDownFS();
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
6d9380f96   Cédric Dupont   Update sources OC...
287
288
  		$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 ...
289
290
291
292
293
  		/**
  		 * @var \OC\Files\Storage\Shared $sharedStorage
  		 */
  
  		$sharedCache = $sharedStorage->getCache();
6d9380f96   Cédric Dupont   Update sources OC...
294
295
  		$this->assertEquals('', $sharedCache->getPathById($folderInfo->getId()));
  		$this->assertEquals('bar/test.txt', $sharedCache->getPathById($fileInfo->getId()));
837968727   Kload   [enh] Upgrade to ...
296
  	}
a293d369c   Kload   Update sources to...
297
  }