2014-05-27 13:05:31 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2014-05-27 13:05:31 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2014-05-27 13:05:31 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2014-05-27 13:05:31 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-05-27 13:05:31 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-05-27 13:05:31 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-05-27 13:05:31 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2016-05-17 12:42:03 +03:00
|
|
|
namespace OCA\Files_Sharing\Tests;
|
2014-05-27 13:05:31 +04:00
|
|
|
|
|
|
|
/**
|
2016-05-17 12:42:03 +03:00
|
|
|
* Class ShareTest
|
2015-11-03 03:52:41 +03:00
|
|
|
*
|
|
|
|
* @group DB
|
2014-05-27 13:05:31 +04:00
|
|
|
*/
|
2016-05-17 12:42:03 +03:00
|
|
|
class ShareTest extends TestCase {
|
2014-05-27 13:05:31 +04:00
|
|
|
|
|
|
|
const TEST_FOLDER_NAME = '/folder_share_api_test';
|
|
|
|
|
|
|
|
private static $tempStorage;
|
|
|
|
|
2014-11-11 00:28:12 +03:00
|
|
|
protected function setUp() {
|
2014-05-27 13:05:31 +04:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->folder = self::TEST_FOLDER_NAME;
|
|
|
|
$this->subfolder = '/subfolder_share_api_test';
|
|
|
|
$this->subsubfolder = '/subsubfolder_share_api_test';
|
|
|
|
|
|
|
|
$this->filename = '/share-api-test.txt';
|
|
|
|
|
|
|
|
// save file with content
|
|
|
|
$this->view->file_put_contents($this->filename, $this->data);
|
|
|
|
$this->view->mkdir($this->folder);
|
|
|
|
$this->view->mkdir($this->folder . $this->subfolder);
|
|
|
|
$this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder);
|
|
|
|
$this->view->file_put_contents($this->folder.$this->filename, $this->data);
|
|
|
|
$this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data);
|
|
|
|
}
|
|
|
|
|
2014-11-11 00:28:12 +03:00
|
|
|
protected function tearDown() {
|
2015-01-14 23:06:26 +03:00
|
|
|
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
|
2014-05-27 13:05:31 +04:00
|
|
|
$this->view->unlink($this->filename);
|
|
|
|
$this->view->deleteAll($this->folder);
|
|
|
|
|
|
|
|
self::$tempStorage = null;
|
|
|
|
|
2014-08-01 18:24:19 +04:00
|
|
|
// clear database table
|
|
|
|
$query = \OCP\DB::prepare('DELETE FROM `*PREFIX*share`');
|
|
|
|
$query->execute();
|
|
|
|
|
2014-05-27 13:05:31 +04:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2015-07-07 12:07:11 +03:00
|
|
|
public function testUnshareFromSelf() {
|
2014-06-04 20:41:47 +04:00
|
|
|
|
|
|
|
\OC_Group::createGroup('testGroup');
|
|
|
|
\OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup');
|
|
|
|
\OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER3, 'testGroup');
|
|
|
|
|
2016-04-15 10:02:17 +03:00
|
|
|
$share1 = $this->share(
|
|
|
|
\OCP\Share::SHARE_TYPE_USER,
|
|
|
|
$this->filename,
|
|
|
|
self::TEST_FILES_SHARING_API_USER1,
|
|
|
|
self::TEST_FILES_SHARING_API_USER2,
|
|
|
|
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
|
|
|
|
);
|
2014-06-04 20:41:47 +04:00
|
|
|
|
2016-04-15 10:02:17 +03:00
|
|
|
$share2 = $this->share(
|
|
|
|
\OCP\Share::SHARE_TYPE_GROUP,
|
|
|
|
$this->filename,
|
|
|
|
self::TEST_FILES_SHARING_API_USER1,
|
|
|
|
'testGroup',
|
|
|
|
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
|
|
|
|
);
|
2014-06-04 20:41:47 +04:00
|
|
|
|
|
|
|
self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
$this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
|
|
|
|
|
|
|
|
self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
|
|
|
|
$this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
|
|
|
|
|
|
|
|
self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
\OC\Files\Filesystem::unlink($this->filename);
|
|
|
|
self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
|
2014-08-01 18:24:19 +04:00
|
|
|
// both group share and user share should be gone
|
2014-06-04 20:41:47 +04:00
|
|
|
$this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
|
|
|
|
|
|
|
|
// for user3 nothing should change
|
|
|
|
self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
|
|
|
|
$this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
|
2014-09-25 15:45:23 +04:00
|
|
|
|
2016-04-15 10:02:17 +03:00
|
|
|
$this->shareManager->deleteShare($share1);
|
|
|
|
$this->shareManager->deleteShare($share2);
|
2014-09-25 15:45:23 +04:00
|
|
|
}
|
|
|
|
|
2015-12-07 23:08:20 +03:00
|
|
|
/**
|
2016-05-17 12:42:03 +03:00
|
|
|
* @param \OC\Files\FileInfo[] $content
|
2015-12-07 23:08:20 +03:00
|
|
|
* @param string[] $expected
|
|
|
|
*/
|
2015-07-07 12:07:11 +03:00
|
|
|
public function verifyDirContent($content, $expected) {
|
2014-09-25 15:45:23 +04:00
|
|
|
foreach ($content as $c) {
|
|
|
|
if (!in_array($c['name'], $expected)) {
|
|
|
|
$this->assertTrue(false, "folder should only contain '" . implode(',', $expected) . "', found: " .$c['name']);
|
|
|
|
}
|
2014-08-01 18:24:19 +04:00
|
|
|
}
|
2014-09-25 15:45:23 +04:00
|
|
|
}
|
2014-06-04 20:41:47 +04:00
|
|
|
|
2015-07-07 12:07:11 +03:00
|
|
|
public function testShareWithDifferentShareFolder() {
|
2014-08-14 15:08:58 +04:00
|
|
|
|
|
|
|
$fileinfo = $this->view->getFileInfo($this->filename);
|
|
|
|
$folderinfo = $this->view->getFileInfo($this->folder);
|
|
|
|
|
2016-04-15 10:02:17 +03:00
|
|
|
$share = $this->share(
|
|
|
|
\OCP\Share::SHARE_TYPE_USER,
|
|
|
|
$this->filename,
|
|
|
|
self::TEST_FILES_SHARING_API_USER1,
|
|
|
|
self::TEST_FILES_SHARING_API_USER2,
|
|
|
|
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
|
|
|
|
);
|
2014-08-14 15:08:58 +04:00
|
|
|
|
|
|
|
\OCA\Files_Sharing\Helper::setShareFolder('/Shared/subfolder');
|
|
|
|
|
2016-04-15 10:02:17 +03:00
|
|
|
$share = $this->share(
|
|
|
|
\OCP\Share::SHARE_TYPE_USER,
|
|
|
|
$this->folder,
|
|
|
|
self::TEST_FILES_SHARING_API_USER1,
|
|
|
|
self::TEST_FILES_SHARING_API_USER2,
|
|
|
|
\OCP\Constants::PERMISSION_ALL
|
|
|
|
);
|
2014-08-14 15:08:58 +04:00
|
|
|
|
|
|
|
self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
|
|
|
|
$this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
|
|
|
|
$this->assertTrue(\OC\Files\Filesystem::file_exists('/Shared/subfolder/' . $this->folder));
|
|
|
|
|
|
|
|
//cleanup
|
2014-12-17 13:12:37 +03:00
|
|
|
\OC::$server->getConfig()->deleteSystemValue('share_folder');
|
2014-08-14 15:08:58 +04:00
|
|
|
}
|
|
|
|
|
2015-07-07 12:07:11 +03:00
|
|
|
public function testShareWithGroupUniqueName() {
|
2014-12-23 19:10:05 +03:00
|
|
|
$this->loginHelper(self::TEST_FILES_SHARING_API_USER1);
|
|
|
|
\OC\Files\Filesystem::file_put_contents('test.txt', 'test');
|
|
|
|
|
2016-04-15 10:02:17 +03:00
|
|
|
$share = $this->share(
|
|
|
|
\OCP\Share::SHARE_TYPE_GROUP,
|
|
|
|
'test.txt',
|
|
|
|
self::TEST_FILES_SHARING_API_USER1,
|
|
|
|
self::TEST_FILES_SHARING_API_GROUP1,
|
|
|
|
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
|
2014-12-23 19:10:05 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
|
|
|
|
|
2016-04-15 10:02:17 +03:00
|
|
|
$shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP);
|
|
|
|
$share = $shares[0];
|
|
|
|
$this->assertSame('/test.txt' ,$share->getTarget());
|
|
|
|
$this->assertSame(19, $share->getPermissions());
|
2014-12-23 19:10:05 +03:00
|
|
|
|
|
|
|
\OC\Files\Filesystem::rename('test.txt', 'new test.txt');
|
|
|
|
|
2016-04-15 10:02:17 +03:00
|
|
|
$shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP);
|
|
|
|
$share = $shares[0];
|
|
|
|
$this->assertSame('/new test.txt' ,$share->getTarget());
|
|
|
|
$this->assertSame(19, $share->getPermissions());
|
2014-12-23 19:10:05 +03:00
|
|
|
|
2016-04-15 10:02:17 +03:00
|
|
|
$share->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE);
|
|
|
|
$this->shareManager->updateShare($share);
|
2014-12-23 19:10:05 +03:00
|
|
|
|
|
|
|
$this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
|
2016-04-15 10:02:17 +03:00
|
|
|
$shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP);
|
|
|
|
$share = $shares[0];
|
2014-12-23 19:10:05 +03:00
|
|
|
|
2016-04-15 10:02:17 +03:00
|
|
|
$this->assertSame('/new test.txt' ,$share->getTarget());
|
|
|
|
$this->assertSame(3, $share->getPermissions());
|
2014-12-23 19:10:05 +03:00
|
|
|
}
|
|
|
|
|
2014-05-27 13:05:31 +04:00
|
|
|
/**
|
|
|
|
* shared files should never have delete permissions
|
2015-07-07 12:07:11 +03:00
|
|
|
* @dataProvider dataProviderTestFileSharePermissions
|
2014-05-27 13:05:31 +04:00
|
|
|
*/
|
2016-04-15 10:02:17 +03:00
|
|
|
public function testFileSharePermissions($permission, $expectedvalid) {
|
|
|
|
|
|
|
|
$pass = true;
|
|
|
|
try {
|
|
|
|
$this->share(
|
|
|
|
\OCP\Share::SHARE_TYPE_USER,
|
|
|
|
$this->filename,
|
|
|
|
self::TEST_FILES_SHARING_API_USER1,
|
|
|
|
self::TEST_FILES_SHARING_API_USER2,
|
|
|
|
$permission
|
|
|
|
);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$pass = false;
|
|
|
|
}
|
2014-05-27 13:05:31 +04:00
|
|
|
|
2016-04-15 10:02:17 +03:00
|
|
|
$this->assertEquals($expectedvalid, $pass);
|
2014-05-27 13:05:31 +04:00
|
|
|
}
|
|
|
|
|
2015-07-07 12:07:11 +03:00
|
|
|
public function dataProviderTestFileSharePermissions() {
|
2014-11-25 18:28:41 +03:00
|
|
|
$permission1 = \OCP\Constants::PERMISSION_ALL;
|
|
|
|
$permission3 = \OCP\Constants::PERMISSION_READ;
|
|
|
|
$permission4 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE;
|
|
|
|
$permission5 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE;
|
|
|
|
$permission6 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
|
2014-05-27 13:05:31 +04:00
|
|
|
|
|
|
|
return array(
|
2016-04-15 10:02:17 +03:00
|
|
|
array($permission1, false),
|
|
|
|
array($permission3, true),
|
|
|
|
array($permission4, true),
|
|
|
|
array($permission5, false),
|
|
|
|
array($permission6, false),
|
2014-05-27 13:05:31 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-10 13:06:48 +03:00
|
|
|
public function testFileOwner() {
|
|
|
|
|
2016-04-15 10:02:17 +03:00
|
|
|
$this->share(
|
|
|
|
\OCP\Share::SHARE_TYPE_USER,
|
|
|
|
$this->filename,
|
|
|
|
self::TEST_FILES_SHARING_API_USER1,
|
|
|
|
self::TEST_FILES_SHARING_API_USER2,
|
|
|
|
\OCP\Constants::PERMISSION_READ
|
|
|
|
);
|
2015-11-10 13:06:48 +03:00
|
|
|
|
2016-05-17 12:42:03 +03:00
|
|
|
$this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
|
2015-11-10 13:06:48 +03:00
|
|
|
|
|
|
|
$info = \OC\Files\Filesystem::getFileInfo($this->filename);
|
|
|
|
|
2016-05-17 12:42:03 +03:00
|
|
|
$this->assertSame(self::TEST_FILES_SHARING_API_USER1, $info->getOwner()->getUID());
|
2015-11-10 13:06:48 +03:00
|
|
|
}
|
2014-05-27 13:05:31 +04:00
|
|
|
}
|