share.php 45.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
/**
* ownCloud
*
* @author Michael Gapczynski
* @copyright 2012 Michael Gapczynski mtgap@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/>.
*/

22
class Test_Share extends \Test\TestCase {
23
24
25
26
27

	protected $itemType;
	protected $userBackend;
	protected $user1;
	protected $user2;
28
29
	protected $user3;
	protected $user4;
30
31
	protected $user5;
	protected $user6;
32
	protected $groupAndUser;
33
34
35
	protected $groupBackend;
	protected $group1;
	protected $group2;
Michael Gapczynski's avatar
Michael Gapczynski committed
36
	protected $resharing;
37
38
	protected $dateInFuture;
	protected $dateInPast;
39

40
41
	protected function setUp() {
		parent::setUp();
42

43
44
		OC_User::clearBackends();
		OC_User::useBackend('dummy');
45
46
47
48
		$this->user1 = $this->getUniqueID('user1_');
		$this->user2 = $this->getUniqueID('user2_');
		$this->user3 = $this->getUniqueID('user3_');
		$this->user4 = $this->getUniqueID('user4_');
49
		$this->user5 = $this->getUniqueID('user5_');
50
		$this->user6 = $this->getUniqueID('user6_');
51
		$this->groupAndUser = $this->getUniqueID('groupAndUser_');
52
53
54
		OC_User::createUser($this->user1, 'pass');
		OC_User::createUser($this->user2, 'pass');
		OC_User::createUser($this->user3, 'pass');
55
		OC_User::createUser($this->user4, 'pass');
56
		OC_User::createUser($this->user5, 'pass');
57
		OC_User::createUser($this->user6, 'pass'); // no group
58
		OC_User::createUser($this->groupAndUser, 'pass');
59
		OC_User::setUserId($this->user1);
60
61
		OC_Group::clearBackends();
		OC_Group::useBackend(new OC_Group_Dummy);
62
63
		$this->group1 = $this->getUniqueID('group1_');
		$this->group2 = $this->getUniqueID('group2_');
64
65
		OC_Group::createGroup($this->group1);
		OC_Group::createGroup($this->group2);
66
		OC_Group::createGroup($this->groupAndUser);
67
68
69
		OC_Group::addToGroup($this->user1, $this->group1);
		OC_Group::addToGroup($this->user2, $this->group1);
		OC_Group::addToGroup($this->user3, $this->group1);
70
71
		OC_Group::addToGroup($this->user2, $this->group2);
		OC_Group::addToGroup($this->user4, $this->group2);
72
73
		OC_Group::addToGroup($this->user2, $this->groupAndUser);
		OC_Group::addToGroup($this->user3, $this->groupAndUser);
74
		OCP\Share::registerBackend('test', 'Test_Share_Backend');
75
		OC_Hook::clear('OCP\\Share');
76
		OC::registerShareHooks();
Michael Gapczynski's avatar
Michael Gapczynski committed
77
78
		$this->resharing = OC_Appconfig::getValue('core', 'shareapi_allow_resharing', 'yes');
		OC_Appconfig::setValue('core', 'shareapi_allow_resharing', 'yes');
79
80
81
82
83
84

		// 20 Minutes in the past, 20 minutes in the future.
		$now = time();
		$dateFormat = 'Y-m-d H:i:s';
		$this->dateInPast = date($dateFormat, $now - 20 * 60);
		$this->dateInFuture = date($dateFormat, $now + 20 * 60);
85
86
	}

87
	protected function tearDown() {
88
		$query = OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `item_type` = ?');
89
		$query->execute(array('test'));
Michael Gapczynski's avatar
Michael Gapczynski committed
90
		OC_Appconfig::setValue('core', 'shareapi_allow_resharing', $this->resharing);
91

92
93
94
95
96
97
98
99
100
101
102
103
		OC_User::deleteUser($this->user1);
		OC_User::deleteUser($this->user2);
		OC_User::deleteUser($this->user3);
		OC_User::deleteUser($this->user4);
		OC_User::deleteUser($this->user5);
		OC_User::deleteUser($this->user6);
		OC_User::deleteUser($this->groupAndUser);

		OC_Group::deleteGroup($this->group1);
		OC_Group::deleteGroup($this->group2);
		OC_Group::deleteGroup($this->groupAndUser);

104
		parent::tearDown();
105
106
	}

107
	public function testShareInvalidShareType() {
108
109
		$message = 'Share type foobar is not valid for test.txt';
		try {
110
			OCP\Share::shareItem('test', 'test.txt', 'foobar', $this->user2, \OCP\Constants::PERMISSION_READ);
111
		} catch (Exception $exception) {
112
			$this->assertEquals($message, $exception->getMessage());
113
		}
114
115
	}

116
117
118
	public function testInvalidItemType() {
		$message = 'Sharing backend for foobar not found';
		try {
119
			OCP\Share::shareItem('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ);
120
121
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
122
			$this->assertEquals($message, $exception->getMessage());
123
124
125
126
127
		}
		try {
			OCP\Share::getItemsSharedWith('foobar');
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
128
			$this->assertEquals($message, $exception->getMessage());
129
130
131
132
133
		}
		try {
			OCP\Share::getItemSharedWith('foobar', 'test.txt');
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
134
			$this->assertEquals($message, $exception->getMessage());
135
136
137
138
139
		}
		try {
			OCP\Share::getItemSharedWithBySource('foobar', 'test.txt');
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
140
			$this->assertEquals($message, $exception->getMessage());
141
142
143
144
145
		}
		try {
			OCP\Share::getItemShared('foobar', 'test.txt');
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
146
			$this->assertEquals($message, $exception->getMessage());
147
148
149
150
151
		}
		try {
			OCP\Share::unshare('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2);
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
152
			$this->assertEquals($message, $exception->getMessage());
153
154
		}
		try {
155
			OCP\Share::setPermissions('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_UPDATE);
156
157
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
158
			$this->assertEquals($message, $exception->getMessage());
159
		}
160
161
	}

162
	protected function shareUserOneTestFileWithUserTwo() {
163
164
		OC_User::setUserId($this->user1);
		$this->assertTrue(
165
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ),
166
167
			'Failed asserting that user 1 successfully shared text.txt with user 2.'
		);
168
169
		$this->assertContains(
			'test.txt',
170
171
172
173
174
			OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
			'Failed asserting that test.txt is a shared file of user 1.'
		);

		OC_User::setUserId($this->user2);
175
176
		$this->assertContains(
			'test.txt',
177
178
179
180
181
			OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
			'Failed asserting that user 2 has access to test.txt after initial sharing.'
		);
	}

Bjoern Schiessle's avatar
Bjoern Schiessle committed
182
183
	protected function shareUserTestFileAsLink() {
		OC_User::setUserId($this->user1);
184
		$result = OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ);
Bjoern Schiessle's avatar
Bjoern Schiessle committed
185
186
187
		$this->assertTrue(is_string($result));
	}

188
189
190
191
	/**
	 * @param string $sharer
	 * @param string $receiver
	 */
192
193
194
	protected function shareUserTestFileWithUser($sharer, $receiver) {
		OC_User::setUserId($sharer);
		$this->assertTrue(
195
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $receiver, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE),
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
			'Failed asserting that ' . $sharer . ' successfully shared text.txt with ' . $receiver . '.'
		);
		$this->assertContains(
			'test.txt',
			OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
			'Failed asserting that test.txt is a shared file of ' . $sharer . '.'
		);

		OC_User::setUserId($receiver);
		$this->assertContains(
			'test.txt',
			OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
			'Failed asserting that ' . $receiver . ' has access to test.txt after initial sharing.'
		);
	}

212
213
214
215
	public function testShareWithUser() {
		// Invalid shares
		$message = 'Sharing test.txt failed, because the user '.$this->user1.' is the item owner';
		try {
216
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, \OCP\Constants::PERMISSION_READ);
217
218
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
219
			$this->assertEquals($message, $exception->getMessage());
220
221
222
		}
		$message = 'Sharing test.txt failed, because the user foobar does not exist';
		try {
223
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, 'foobar', \OCP\Constants::PERMISSION_READ);
224
225
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
226
			$this->assertEquals($message, $exception->getMessage());
227
		}
228
229
		$message = 'Sharing foobar failed, because the sharing backend for test could not find its source';
		try {
230
			OCP\Share::shareItem('test', 'foobar', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ);
231
232
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
233
			$this->assertEquals($message, $exception->getMessage());
234
		}
235

236
		// Valid share
237
		$this->shareUserOneTestFileWithUserTwo();
238

239
240
241
242
		// Attempt to share again
		OC_User::setUserId($this->user1);
		$message = 'Sharing test.txt failed, because this item is already shared with '.$this->user2;
		try {
243
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ);
244
245
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
246
			$this->assertEquals($message, $exception->getMessage());
247
		}
248

249
250
251
		// Attempt to share back
		OC_User::setUserId($this->user2);
		$message = 'Sharing test.txt failed, because the user '.$this->user1.' is the original sharer';
252
		try {
253
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, \OCP\Constants::PERMISSION_READ);
254
255
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
256
			$this->assertEquals($message, $exception->getMessage());
257
		}
258

259
260
261
		// Unshare
		OC_User::setUserId($this->user1);
		$this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
262

263
		// Attempt reshare without share permission
264
		$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ));
265
266
267
		OC_User::setUserId($this->user2);
		$message = 'Sharing test.txt failed, because resharing is not allowed';
		try {
268
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, \OCP\Constants::PERMISSION_READ);
269
270
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
271
			$this->assertEquals($message, $exception->getMessage());
272
		}
273

274
		// Owner grants share and update permission
275
		OC_User::setUserId($this->user1);
276
		$this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE));
277

278
279
280
281
		// Attempt reshare with escalated permissions
		OC_User::setUserId($this->user2);
		$message = 'Sharing test.txt failed, because the permissions exceed permissions granted to '.$this->user2;
		try {
282
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE);
283
284
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
285
			$this->assertEquals($message, $exception->getMessage());
286
		}
287

288
		// Valid reshare
289
		$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE));
290
		$this->assertEquals(array('test.txt'), OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
291
		OC_User::setUserId($this->user3);
292
		$this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
293
		$this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
294

295
296
297
298
		// Attempt to escalate permissions
		OC_User::setUserId($this->user2);
		$message = 'Setting permissions for test.txt failed, because the permissions exceed permissions granted to '.$this->user2;
		try {
299
			OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE);
300
301
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
302
			$this->assertEquals($message, $exception->getMessage());
303
		}
304

305
306
		// Remove update permission
		OC_User::setUserId($this->user1);
307
		$this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE));
308
		OC_User::setUserId($this->user2);
309
		$this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
310
		OC_User::setUserId($this->user3);
311
		$this->assertEquals(array(\OCP\Constants::PERMISSION_READ), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
312

313
314
		// Remove share permission
		OC_User::setUserId($this->user1);
315
		$this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ));
316
		OC_User::setUserId($this->user2);
317
		$this->assertEquals(array(\OCP\Constants::PERMISSION_READ), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
318
		OC_User::setUserId($this->user3);
Bjoern Schiessle's avatar
Bjoern Schiessle committed
319
		$this->assertSame(array(), OCP\Share::getItemSharedWith('test', 'test.txt'));
320

321
322
		// Reshare again, and then have owner unshare
		OC_User::setUserId($this->user1);
323
		$this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE));
324
		OC_User::setUserId($this->user2);
325
		$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, \OCP\Constants::PERMISSION_READ));
326
327
328
		OC_User::setUserId($this->user1);
		$this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
		OC_User::setUserId($this->user2);
Bjoern Schiessle's avatar
Bjoern Schiessle committed
329
		$this->assertSame(array(), OCP\Share::getItemSharedWith('test', 'test.txt'));
330
		OC_User::setUserId($this->user3);
Bjoern Schiessle's avatar
Bjoern Schiessle committed
331
		$this->assertSame(array(), OCP\Share::getItemSharedWith('test', 'test.txt'));
332

333
334
		// Attempt target conflict
		OC_User::setUserId($this->user1);
335
		$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ));
336
		OC_User::setUserId($this->user3);
337
		$this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ));
338

339
		OC_User::setUserId($this->user2);
340
		$to_test = OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET);
341
		$this->assertEquals(2, count($to_test));
342
343
		$this->assertTrue(in_array('test.txt', $to_test));
		$this->assertTrue(in_array('test1.txt', $to_test));
344

345
346
347
348
349
350
351
352
353
		// Unshare from self
		$this->assertTrue(OCP\Share::unshareFromSelf('test', 'test.txt'));
		$this->assertEquals(array('test1.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));

		// Unshare from self via source
		$this->assertTrue(OCP\Share::unshareFromSelf('test', 'share.txt', true));
		$this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));

		OC_User::setUserId($this->user1);
354
		$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ));
355
		OC_User::setUserId($this->user3);
356
		$this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ));
357
358
359
360
361
362
363

		OC_User::setUserId($this->user2);
		$to_test = OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET);
		$this->assertEquals(2, count($to_test));
		$this->assertTrue(in_array('test.txt', $to_test));
		$this->assertTrue(in_array('test1.txt', $to_test));

364
		// Remove user
365
		OC_User::setUserId($this->user1);
366
367
		OC_User::deleteUser($this->user1);
		OC_User::setUserId($this->user2);
368
		$this->assertEquals(array('test1.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
369
	}
370

371
	public function testShareWithUserExpirationExpired() {
Bjoern Schiessle's avatar
Bjoern Schiessle committed
372
		OC_User::setUserId($this->user1);
373
		$this->shareUserOneTestFileWithUserTwo();
Bjoern Schiessle's avatar
Bjoern Schiessle committed
374
		$this->shareUserTestFileAsLink();
375

376
377
378
379
380
381
382
383
		// manipulate share table and set expire date to the past
		$query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `expiration` = ? WHERE `item_type` = ? AND `item_source` = ?  AND `uid_owner` = ? AND `share_type` = ?');
		$query->bindValue(1, new \DateTime($this->dateInPast), 'datetime');
		$query->bindValue(2, 'test');
		$query->bindValue(3, 'test.txt');
		$query->bindValue(4, $this->user1);
		$query->bindValue(5, \OCP\Share::SHARE_TYPE_LINK);
		$query->execute();
384

Bjoern Schiessle's avatar
Bjoern Schiessle committed
385
386
387
388
		$shares = OCP\Share::getItemsShared('test');
		$this->assertSame(1, count($shares));
		$share = reset($shares);
		$this->assertSame(\OCP\Share::SHARE_TYPE_USER, $share['share_type']);
389
390
	}

391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
	public function testSetExpireDateInPast() {
		OC_User::setUserId($this->user1);
		$this->shareUserOneTestFileWithUserTwo();
		$this->shareUserTestFileAsLink();

		$setExpireDateFailed = false;
		try {
			$this->assertTrue(
					OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInPast, ''),
					'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
			);
		} catch (\Exception $e) {
			$setExpireDateFailed = true;
		}

		$this->assertTrue($setExpireDateFailed);
	}

409
	public function testShareWithUserExpirationValid() {
Bjoern Schiessle's avatar
Bjoern Schiessle committed
410
		OC_User::setUserId($this->user1);
411
		$this->shareUserOneTestFileWithUserTwo();
Bjoern Schiessle's avatar
Bjoern Schiessle committed
412
413
		$this->shareUserTestFileAsLink();

414
415

		$this->assertTrue(
416
			OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInFuture, ''),
417
			'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
418
419
		);

Bjoern Schiessle's avatar
Bjoern Schiessle committed
420
421
422
		$shares = OCP\Share::getItemsShared('test');
		$this->assertSame(2, count($shares));

423
	}
424

425
426
427
428
429
430
431
432
	/*
	 * if user is in a group excluded from resharing, then the share permission should
	 * be removed
	 */
	public function testShareWithUserAndUserIsExcludedFromResharing() {

		OC_User::setUserId($this->user1);
		$this->assertTrue(
433
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, \OCP\Constants::PERMISSION_ALL),
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
			'Failed asserting that user 1 successfully shared text.txt with user 4.'
		);
		$this->assertContains(
			'test.txt',
			OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
			'Failed asserting that test.txt is a shared file of user 1.'
		);

		// exclude group2 from sharing
		\OC_Appconfig::setValue('core', 'shareapi_exclude_groups_list', $this->group2);
		\OC_Appconfig::setValue('core', 'shareapi_exclude_groups', "yes");

		OC_User::setUserId($this->user4);

		$share = OCP\Share::getItemSharedWith('test', 'test.txt');

450
		$this->assertSame(\OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_SHARE, $share['permissions'],
451
452
453
454
455
456
457
				'Failed asserting that user 4 is excluded from re-sharing');

		\OC_Appconfig::deleteKey('core', 'shareapi_exclude_groups_list');
		\OC_Appconfig::deleteKey('core', 'shareapi_exclude_groups');

	}

458
	protected function shareUserOneTestFileWithGroupOne() {
459
460
		OC_User::setUserId($this->user1);
		$this->assertTrue(
461
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ),
462
463
			'Failed asserting that user 1 successfully shared text.txt with group 1.'
		);
464
465
		$this->assertContains(
			'test.txt',
466
467
468
469
470
			OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
			'Failed asserting that test.txt is a shared file of user 1.'
		);

		OC_User::setUserId($this->user2);
471
472
		$this->assertContains(
			'test.txt',
473
474
475
476
477
			OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
			'Failed asserting that user 2 has access to test.txt after initial sharing.'
		);

		OC_User::setUserId($this->user3);
478
479
		$this->assertContains(
			'test.txt',
480
481
482
483
484
			OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
			'Failed asserting that user 3 has access to test.txt after initial sharing.'
		);
	}

485
486
487
488
	public function testShareWithGroup() {
		// Invalid shares
		$message = 'Sharing test.txt failed, because the group foobar does not exist';
		try {
489
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, 'foobar', \OCP\Constants::PERMISSION_READ);
490
491
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
492
			$this->assertEquals($message, $exception->getMessage());
493
		}
Bjoern Schiessle's avatar
Bjoern Schiessle committed
494
495
		$policy = OC_Appconfig::getValue('core', 'shareapi_only_share_with_group_members', 'no');
		OC_Appconfig::setValue('core', 'shareapi_only_share_with_group_members', 'yes');
496
497
		$message = 'Sharing test.txt failed, because '.$this->user1.' is not a member of the group '.$this->group2;
		try {
498
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group2, \OCP\Constants::PERMISSION_READ);
499
500
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
501
			$this->assertEquals($message, $exception->getMessage());
502
		}
Bjoern Schiessle's avatar
Bjoern Schiessle committed
503
		OC_Appconfig::setValue('core', 'shareapi_only_share_with_group_members', $policy);
504

505
		// Valid share
506
		$this->shareUserOneTestFileWithGroupOne();
507

508
509
510
511
		// Attempt to share again
		OC_User::setUserId($this->user1);
		$message = 'Sharing test.txt failed, because this item is already shared with '.$this->group1;
		try {
512
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ);
513
514
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
515
			$this->assertEquals($message, $exception->getMessage());
516
		}
517

518
519
520
521
		// Attempt to share back to owner of group share
		OC_User::setUserId($this->user2);
		$message = 'Sharing test.txt failed, because the user '.$this->user1.' is the original sharer';
		try {
522
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, \OCP\Constants::PERMISSION_READ);
523
524
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
525
			$this->assertEquals($message, $exception->getMessage());
526
		}
527

528
		// Attempt to share back to group
529
		$message = 'Sharing test.txt failed, because this item is already shared with '.$this->group1;
530
		try {
531
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ);
532
533
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
534
			$this->assertEquals($message, $exception->getMessage());
535
		}
536

537
		// Attempt to share back to member of group
538
		$message ='Sharing test.txt failed, because this item is already shared with '.$this->user3;
539
		try {
540
			OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, \OCP\Constants::PERMISSION_READ);
541
542
			$this->fail('Exception was expected: '.$message);
		} catch (Exception $exception) {
543
			$this->assertEquals($message, $exception->getMessage());
544
		}
545

546
		// Unshare
547
		OC_User::setUserId($this->user1);
548
		$this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1));
549

550
		// Valid share with same person - user then group
551
552
		$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_SHARE));
		$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE));
553
		OC_User::setUserId($this->user2);
554
		$this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
555
		$this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_SHARE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
556
		OC_User::setUserId($this->user3);
557
		$this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
558
		$this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
559

560
561
		// Valid reshare
		OC_User::setUserId($this->user2);
562
		$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, \OCP\Constants::PERMISSION_READ));
563
		OC_User::setUserId($this->user4);
564
		$this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
565

566
567
568
569
		// Unshare from user only
		OC_User::setUserId($this->user1);
		$this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
		OC_User::setUserId($this->user2);
570
		$this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
571
		OC_User::setUserId($this->user4);
572
		$this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
573

574
575
		// Valid share with same person - group then user
		OC_User::setUserId($this->user1);
576
		$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE));
577
		OC_User::setUserId($this->user2);
578
		$this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
579
		$this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
580

581
582
583
584
		// Unshare from group only
		OC_User::setUserId($this->user1);
		$this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1));
		OC_User::setUserId($this->user2);
585
		$this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
586

587
588
		// Attempt user specific target conflict
		OC_User::setUserId($this->user3);
589
590
		\OCP\Util::connectHook('OCP\\Share', 'post_shared', 'DummyHookListener', 'listen');

591
		$this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE));
592
		$this->assertEquals(OCP\Share::SHARE_TYPE_GROUP, DummyHookListener::$shareType);
593
		OC_User::setUserId($this->user2);
594
		$to_test = OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET);
595
		$this->assertEquals(2, count($to_test));
596
597
		$this->assertTrue(in_array('test.txt', $to_test));
		$this->assertTrue(in_array('test1.txt', $to_test));
598

599
		// Valid reshare
600
		$this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE));
601
		OC_User::setUserId($this->user4);
602
		$this->assertEquals(array('test1.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
603

604
605
606
		// Remove user from group
		OC_Group::removeFromGroup($this->user2, $this->group1);
		OC_User::setUserId($this->user2);
607
		$this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
608
		OC_User::setUserId($this->user4);
609
		$this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
610

611
		// Add user to group
612
		OC_Group::addToGroup($this->user4, $this->group1);
613
		$this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
614

615
616
		// Unshare from self
		$this->assertTrue(OCP\Share::unshareFromSelf('test', 'test.txt'));
617
		$this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
618
		OC_User::setUserId($this->user2);
619
		$this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
620

621
622
623
624
		// Unshare from self via source
		OC_User::setUserId($this->user1);
		$this->assertTrue(OCP\Share::unshareFromSelf('test', 'share.txt', true));
		$this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
625

626
		// Remove group
627
		OC_Group::deleteGroup($this->group1);
628
		OC_User::setUserId($this->user4);
629
		$this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
630
		OC_User::setUserId($this->user3);
631
		$this->assertEquals(array(), OCP\Share::getItemsShared('test'));
632
633
	}

634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
	/**
	 * Test that unsharing from group will also delete all
	 * child entries
	 */
	public function testShareWithGroupThenUnshare() {
		OC_User::setUserId($this->user5);
		OCP\Share::shareItem(
			'test',
			'test.txt',
			OCP\Share::SHARE_TYPE_GROUP,
			$this->group1,
			\OCP\Constants::PERMISSION_ALL
		);

		$targetUsers = array($this->user1, $this->user2, $this->user3);

		foreach($targetUsers as $targetUser) {
			OC_User::setUserId($targetUser);
			$items = OCP\Share::getItemsSharedWithUser(
				'test',
				$targetUser,
				Test_Share_Backend::FORMAT_TARGET
			);
			$this->assertEquals(1, count($items));
		}

		OC_User::setUserId($this->user5);
		OCP\Share::unshare(
			'test',
			'test.txt',
			OCP\Share::SHARE_TYPE_GROUP,
			$this->group1
		);

		// verify that all were deleted
		foreach($targetUsers as $targetUser) {
			OC_User::setUserId($targetUser);
			$items = OCP\Share::getItemsSharedWithUser(
				'test',
				$targetUser,
				Test_Share_Backend::FORMAT_TARGET
			);
			$this->assertEquals(0, count($items));
		}
	}
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696

	public function testShareWithGroupAndUserBothHaveTheSameId() {

		$this->shareUserTestFileWithUser($this->user1, $this->groupAndUser);

		OC_User::setUserId($this->groupAndUser);

		$this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
				'"groupAndUser"-User does not see the file but it was shared with him');

		OC_User::setUserId($this->user2);
		$this->assertEquals(array(), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
				'User2 sees test.txt but it was only shared with the user "groupAndUser" and not with group');

		OC_User::setUserId($this->user1);
		$this->assertTrue(OCP\Share::unshareAll('test', 'test.txt'));

		$this->assertTrue(
697
				OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->groupAndUser, \OCP\Constants::PERMISSION_READ),
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
				'Failed asserting that user 1 successfully shared text.txt with group 1.'
		);

		OC_User::setUserId($this->groupAndUser);
		$this->assertEquals(array(), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
				'"groupAndUser"-User sees test.txt but it was only shared with the group "groupAndUser" and not with the user');

		OC_User::setUserId($this->user2);
		$this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
				'User2 does not see test.txt but it was shared with the group "groupAndUser"');

		OC_User::setUserId($this->user1);
		$this->assertTrue(OCP\Share::unshareAll('test', 'test.txt'));

	}

714
715
716
	/**
	 * @param boolean|string $token
	 */
717
718
719
720
721
722
723
724
725
726
	protected function getShareByValidToken($token) {
		$row = OCP\Share::getShareByToken($token);
		$this->assertInternalType(
			'array',
			$row,
			"Failed asserting that a share for token $token exists."
		);
		return $row;
	}

727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
	public function testGetItemSharedWithUser() {
		OC_User::setUserId($this->user1);

		//add dummy values to the share table
		$query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
			.' `item_type`, `item_source`, `item_target`, `share_type`,'
			.' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)');
		$args = array('test', 99, 'target1', OCP\Share::SHARE_TYPE_USER, $this->user2, $this->user1);
		$query->execute($args);
		$args = array('test', 99, 'target2', OCP\Share::SHARE_TYPE_USER, $this->user4, $this->user1);
		$query->execute($args);
		$args = array('test', 99, 'target3', OCP\Share::SHARE_TYPE_USER, $this->user3, $this->user2);
		$query->execute($args);
		$args = array('test', 99, 'target4', OCP\Share::SHARE_TYPE_USER, $this->user3, $this->user4);
		$query->execute($args);
742
743
		$args = array('test', 99, 'target4', OCP\Share::SHARE_TYPE_USER, $this->user6, $this->user4);
		$query->execute($args);
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758


		$result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2, $this->user1);
		$this->assertSame(1, count($result1));
		$this->verifyResult($result1, array('target1'));

		$result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1);
		$this->assertSame(2, count($result2));
		$this->verifyResult($result2, array('target1', 'target2'));

		$result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3);
		$this->assertSame(2, count($result3));
		$this->verifyResult($result3, array('target3', 'target4'));

		$result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null);
759
		$this->assertSame(5, count($result4)); // 5 because target4 appears twice
760
		$this->verifyResult($result4, array('target1', 'target2', 'target3', 'target4'));
761
762
763
764

		$result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6, null);
		$this->assertSame(1, count($result6));
		$this->verifyResult($result6, array('target4'));
765
766
	}

767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
	public function testGetItemSharedWithUserFromGroupShare() {
		OC_User::setUserId($this->user1);

		//add dummy values to the share table
		$query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
			.' `item_type`, `item_source`, `item_target`, `share_type`,'
			.' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)');
		$args = array('test', 99, 'target1', OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user1);
		$query->execute($args);
		$args = array('test', 99, 'target2', OCP\Share::SHARE_TYPE_GROUP, $this->group2, $this->user1);
		$query->execute($args);
		$args = array('test', 99, 'target3', OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user2);
		$query->execute($args);
		$args = array('test', 99, 'target4', OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user4);
		$query->execute($args);

		// user2 is in group1 and group2
		$result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2, $this->user1);
		$this->assertSame(2, count($result1));
		$this->verifyResult($result1, array('target1', 'target2'));

		$result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1);
		$this->assertSame(2, count($result2));
		$this->verifyResult($result2, array('target1', 'target2'));

		// user3 is in group1 and group2
		$result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3);
		$this->assertSame(3, count($result3));
		$this->verifyResult($result3, array('target1', 'target3', 'target4'));

		$result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null);
		$this->assertSame(4, count($result4));
		$this->verifyResult($result4, array('target1', 'target2', 'target3', 'target4'));
800
801
802

		$result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6, null);
		$this->assertSame(0, count($result6));
803
804
	}

805
806
807
808
809
810
811
812
813
814
	public function verifyResult($result, $expected) {
		foreach ($result as $r) {
			if (in_array($r['item_target'], $expected)) {
				$key = array_search($r['item_target'], $expected);
				unset($expected[$key]);
			}
		}
		$this->assertEmpty($expected, 'did not found all expected values');
	}

815
816
	public function testShareItemWithLink() {
		OC_User::setUserId($this->user1);
817
		$token = OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ);
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
		$this->assertInternalType(
			'string',
			$token,
			'Failed asserting that user 1 successfully shared text.txt as link with token.'
		);

		// testGetShareByTokenNoExpiration
		$row = $this->getShareByValidToken($token);
		$this->assertEmpty(
			$row['expiration'],
			'Failed asserting that the returned row does not have an expiration date.'
		);

		// testGetShareByTokenExpirationValid
		$this->assertTrue(
833
			OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInFuture, ''),
834
835
836
837
838
839
840
841
			'Failed asserting that user 1 successfully set a future expiration date for the test.txt share.'
		);
		$row = $this->getShareByValidToken($token);
		$this->assertNotEmpty(
			$row['expiration'],
			'Failed asserting that the returned row has an expiration date.'
		);

842
843
844
845
846
847
848
849
850
		// manipulate share table and set expire date to the past
		$query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `expiration` = ? WHERE `item_type` = ? AND `item_source` = ?  AND `uid_owner` = ? AND `share_type` = ?');
		$query->bindValue(1, new \DateTime($this->dateInPast), 'datetime');
		$query->bindValue(2, 'test');
		$query->bindValue(3, 'test.txt');
		$query->bindValue(4, $this->user1);
		$query->bindValue(5, \OCP\Share::SHARE_TYPE_LINK);
		$query->execute();

851
852
853
854
855
		$this->assertFalse(
			OCP\Share::getShareByToken($token),
			'Failed asserting that an expired share could not be found.'
		);
	}
856

Bjoern Schiessle's avatar
Bjoern Schiessle committed
857
858
859
	public function testShareItemWithLinkAndDefaultExpireDate() {
		OC_User::setUserId($this->user1);

Morris Jobke's avatar
Morris Jobke committed
860
861
862
863
		$config = \OC::$server->getConfig();

		$config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
		$config->setAppValue('core', 'shareapi_expire_after_n_days', '2');
Bjoern Schiessle's avatar
Bjoern Schiessle committed
864

865
		$token = OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ);
Bjoern Schiessle's avatar
Bjoern Schiessle committed
866
867
868
869
870
871
872
873
874
875
876
877
878
879
		$this->assertInternalType(
			'string',
			$token,
			'Failed asserting that user 1 successfully shared text.txt as link with token.'
		);

		// share should have default expire date

		$row = $this->getShareByValidToken($token);
		$this->assertNotEmpty(
			$row['expiration'],
			'Failed asserting that the returned row has an default expiration date.'
		);

Morris Jobke's avatar
Morris Jobke committed
880
881
		$config->deleteAppValue('core', 'shareapi_default_expire_date');
		$config->deleteAppValue('core', 'shareapi_expire_after_n_days');
Bjoern Schiessle's avatar
Bjoern Schiessle committed
882
883
884

	}

885
	public function testUnshareAll() {
886
887
888
		$this->shareUserTestFileWithUser($this->user1, $this->user2);
		$this->shareUserTestFileWithUser($this->user2, $this->user3);
		$this->shareUserTestFileWithUser($this->user3, $this->user4);
889
890
891
892
893
		$this->shareUserOneTestFileWithGroupOne();

		OC_User::setUserId($this->user1);
		$this->assertEquals(
			array('test.txt', 'test.txt'),
Bjoern Schiessle's avatar
Bjoern Schiessle committed
894
			OCP\Share::getItemsShared('test', Test_Share_Backend::FORMAT_SOURCE),
895
896
897
898
899
900
			'Failed asserting that the test.txt file is shared exactly two times by user1.'
		);

		OC_User::setUserId($this->user2);
		$this->assertEquals(
			array('test.txt'),
Bjoern Schiessle's avatar
Bjoern Schiessle committed
901
			OCP\Share::getItemsShared('test', Test_Share_Backend::FORMAT_SOURCE),
902
903
904
905
906
907
			'Failed asserting that the test.txt file is shared exactly once by user2.'
		);

		OC_User::setUserId($this->user3);
		$this->assertEquals(
			array('test.txt'),
Bjoern Schiessle's avatar
Bjoern Schiessle committed
908
			OCP\Share::getItemsShared('test', Test_Share_Backend::FORMAT_SOURCE),
909
			'Failed asserting that the test.txt file is shared exactly once by user3.'
910
911
912
913
		);

		$this->assertTrue(
			OCP\Share::unshareAll('test', 'test.txt'),
914
			'Failed asserting that user 3 successfully unshared all shares of the test.txt share.'