2012-07-26 01:08:18 +04:00
< ? 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 />.
*/
2016-05-19 10:15:10 +03:00
namespace Test\Share ;
2015-11-03 03:52:41 +03:00
/**
* Class Test_Share
*
* @ group DB
*/
2016-05-20 16:38:20 +03:00
class ShareTest extends \Test\TestCase {
2012-07-26 01:08:18 +04:00
protected $itemType ;
protected $userBackend ;
protected $user1 ;
protected $user2 ;
2014-01-21 15:07:08 +04:00
protected $user3 ;
protected $user4 ;
2015-01-19 16:39:00 +03:00
protected $user5 ;
protected $user6 ;
2014-11-17 15:09:13 +03:00
protected $groupAndUser ;
2012-07-26 01:08:18 +04:00
protected $groupBackend ;
protected $group1 ;
protected $group2 ;
2013-02-26 22:31:15 +04:00
protected $resharing ;
2013-09-13 01:37:43 +04:00
protected $dateInFuture ;
protected $dateInPast ;
2013-09-05 04:27:29 +04:00
2014-11-18 13:08:01 +03:00
protected function setUp () {
parent :: setUp ();
2014-11-11 01:30:38 +03:00
2016-05-19 10:15:10 +03:00
\OC_User :: clearBackends ();
\OC_User :: useBackend ( 'dummy' );
2014-11-18 13:08:01 +03:00
$this -> user1 = $this -> getUniqueID ( 'user1_' );
$this -> user2 = $this -> getUniqueID ( 'user2_' );
$this -> user3 = $this -> getUniqueID ( 'user3_' );
$this -> user4 = $this -> getUniqueID ( 'user4_' );
2015-01-20 15:09:39 +03:00
$this -> user5 = $this -> getUniqueID ( 'user5_' );
2015-01-19 16:39:00 +03:00
$this -> user6 = $this -> getUniqueID ( 'user6_' );
2014-11-18 13:08:01 +03:00
$this -> groupAndUser = $this -> getUniqueID ( 'groupAndUser_' );
2015-12-17 17:10:11 +03:00
\OC :: $server -> getUserManager () -> createUser ( $this -> user1 , 'pass' );
\OC :: $server -> getUserManager () -> createUser ( $this -> user2 , 'pass' );
\OC :: $server -> getUserManager () -> createUser ( $this -> user3 , 'pass' );
\OC :: $server -> getUserManager () -> createUser ( $this -> user4 , 'pass' );
\OC :: $server -> getUserManager () -> createUser ( $this -> user5 , 'pass' );
\OC :: $server -> getUserManager () -> createUser ( $this -> user6 , 'pass' ); // no group
\OC :: $server -> getUserManager () -> createUser ( $this -> groupAndUser , 'pass' );
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
\OC_Group :: clearBackends ();
\OC_Group :: useBackend ( new \Test\Util\Group\Dummy ());
2014-11-18 13:08:01 +03:00
$this -> group1 = $this -> getUniqueID ( 'group1_' );
$this -> group2 = $this -> getUniqueID ( 'group2_' );
2016-05-19 10:15:10 +03:00
\OC_Group :: createGroup ( $this -> group1 );
\OC_Group :: createGroup ( $this -> group2 );
\OC_Group :: createGroup ( $this -> groupAndUser );
\OC_Group :: addToGroup ( $this -> user1 , $this -> group1 );
\OC_Group :: addToGroup ( $this -> user2 , $this -> group1 );
\OC_Group :: addToGroup ( $this -> user3 , $this -> group1 );
\OC_Group :: addToGroup ( $this -> user2 , $this -> group2 );
\OC_Group :: addToGroup ( $this -> user4 , $this -> group2 );
\OC_Group :: addToGroup ( $this -> user2 , $this -> groupAndUser );
\OC_Group :: addToGroup ( $this -> user3 , $this -> groupAndUser );
2016-05-19 11:11:42 +03:00
\OCP\Share :: registerBackend ( 'test' , 'Test\Share\Backend' );
2016-05-19 10:15:10 +03:00
\OC_Hook :: clear ( 'OCP\\Share' );
\OC :: registerShareHooks ();
2015-07-03 15:16:29 +03:00
$this -> resharing = \OC :: $server -> getAppConfig () -> getValue ( 'core' , 'shareapi_allow_resharing' , 'yes' );
\OC :: $server -> getAppConfig () -> setValue ( 'core' , 'shareapi_allow_resharing' , 'yes' );
2013-09-13 01:37:43 +04:00
// 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 );
2012-07-26 01:08:18 +04:00
}
2014-11-18 13:08:01 +03:00
protected function tearDown () {
2016-05-19 11:11:42 +03:00
$query = \OC_DB :: prepare ( 'DELETE FROM `*PREFIX*share` WHERE `item_type` = ?' );
2012-08-15 19:55:54 +04:00
$query -> execute ( array ( 'test' ));
2015-07-03 15:16:29 +03:00
\OC :: $server -> getAppConfig () -> setValue ( 'core' , 'shareapi_allow_resharing' , $this -> resharing );
2014-11-11 01:30:38 +03:00
2015-12-17 17:59:23 +03:00
$user = \OC :: $server -> getUserManager () -> get ( $this -> user1 );
if ( $user !== null ) { $user -> delete (); }
$user = \OC :: $server -> getUserManager () -> get ( $this -> user2 );
if ( $user !== null ) { $user -> delete (); }
$user = \OC :: $server -> getUserManager () -> get ( $this -> user3 );
if ( $user !== null ) { $user -> delete (); }
$user = \OC :: $server -> getUserManager () -> get ( $this -> user4 );
if ( $user !== null ) { $user -> delete (); }
$user = \OC :: $server -> getUserManager () -> get ( $this -> user5 );
if ( $user !== null ) { $user -> delete (); }
$user = \OC :: $server -> getUserManager () -> get ( $this -> user6 );
if ( $user !== null ) { $user -> delete (); }
$user = \OC :: $server -> getUserManager () -> get ( $this -> groupAndUser );
if ( $user !== null ) { $user -> delete (); }
2015-01-19 16:39:00 +03:00
2016-05-19 10:15:10 +03:00
\OC_Group :: deleteGroup ( $this -> group1 );
\OC_Group :: deleteGroup ( $this -> group2 );
\OC_Group :: deleteGroup ( $this -> groupAndUser );
2015-01-19 16:39:00 +03:00
2015-03-27 17:54:29 +03:00
$this -> logout ();
2014-11-18 13:08:01 +03:00
parent :: tearDown ();
2012-07-26 01:08:18 +04:00
}
2015-04-27 23:07:35 +03:00
protected function setHttpHelper ( $httpHelper ) {
2015-04-28 09:13:49 +03:00
\OC :: $server -> registerService ( 'HTTPHelper' , function () use ( $httpHelper ) {
2015-04-27 23:07:35 +03:00
return $httpHelper ;
});
}
2012-08-15 19:55:54 +04:00
public function testShareInvalidShareType () {
2012-10-12 21:28:24 +04:00
$message = 'Share type foobar is not valid for test.txt' ;
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , 'foobar' , $this -> user2 , \OCP\Constants :: PERMISSION_READ );
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-10-12 21:28:24 +04:00
}
2012-07-26 01:08:18 +04:00
}
2012-08-15 19:55:54 +04:00
public function testInvalidItemType () {
$message = 'Sharing backend for foobar not found' ;
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'foobar' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemsSharedWith ( 'foobar' );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemSharedWith ( 'foobar' , 'test.txt' );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemSharedWithBySource ( 'foobar' , 'test.txt' );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemShared ( 'foobar' , 'test.txt' );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: unshare ( 'foobar' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: setPermissions ( 'foobar' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_UPDATE );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
2012-07-26 01:08:18 +04:00
}
2013-09-05 04:45:52 +04:00
protected function shareUserOneTestFileWithUserTwo () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2013-09-05 04:31:54 +04:00
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ ),
2013-09-05 04:31:54 +04:00
'Failed asserting that user 1 successfully shared text.txt with user 2.'
);
2013-09-23 18:16:48 +04:00
$this -> assertContains (
'test.txt' ,
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemShared ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ),
2013-09-05 04:31:54 +04:00
'Failed asserting that test.txt is a shared file of user 1.'
);
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
2013-09-23 18:16:48 +04:00
$this -> assertContains (
'test.txt' ,
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ),
2013-09-05 04:31:54 +04:00
'Failed asserting that user 2 has access to test.txt after initial sharing.'
);
}
2014-06-03 17:15:04 +04:00
protected function shareUserTestFileAsLink () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
$result = \OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_LINK , null , \OCP\Constants :: PERMISSION_READ );
2014-06-03 17:15:04 +04:00
$this -> assertTrue ( is_string ( $result ));
}
2014-02-19 12:31:54 +04:00
/**
* @ param string $sharer
* @ param string $receiver
*/
2014-01-10 05:08:29 +04:00
protected function shareUserTestFileWithUser ( $sharer , $receiver ) {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $sharer );
2014-01-10 05:08:29 +04:00
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $receiver , \OCP\Constants :: PERMISSION_READ | \OCP\Constants :: PERMISSION_SHARE ),
2014-01-10 05:08:29 +04:00
'Failed asserting that ' . $sharer . ' successfully shared text.txt with ' . $receiver . '.'
);
$this -> assertContains (
'test.txt' ,
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemShared ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ),
2014-01-10 05:08:29 +04:00
'Failed asserting that test.txt is a shared file of ' . $sharer . '.'
);
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $receiver );
2014-01-10 05:08:29 +04:00
$this -> assertContains (
'test.txt' ,
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ),
2014-01-10 05:08:29 +04:00
'Failed asserting that ' . $receiver . ' has access to test.txt after initial sharing.'
);
}
2012-08-15 19:55:54 +04:00
public function testShareWithUser () {
// Invalid shares
2015-10-16 11:00:33 +03:00
$message = 'Sharing test.txt failed, because you can not share with yourself' ;
2012-08-15 19:55:54 +04:00
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user1 , \OCP\Constants :: PERMISSION_READ );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
$message = 'Sharing test.txt failed, because the user foobar does not exist' ;
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , 'foobar' , \OCP\Constants :: PERMISSION_READ );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
2012-08-20 00:30:38 +04:00
$message = 'Sharing foobar failed, because the sharing backend for test could not find its source' ;
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'foobar' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ );
2012-08-20 00:30:38 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-20 00:30:38 +04:00
}
2012-11-11 22:58:54 +04:00
2012-08-15 19:55:54 +04:00
// Valid share
2013-09-05 04:31:54 +04:00
$this -> shareUserOneTestFileWithUserTwo ();
2012-11-11 22:58:54 +04:00
2012-08-15 19:55:54 +04:00
// Attempt to share again
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2012-08-15 19:55:54 +04:00
$message = 'Sharing test.txt failed, because this item is already shared with ' . $this -> user2 ;
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
2012-11-11 22:58:54 +04:00
2012-08-20 00:30:38 +04:00
// Attempt to share back
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
2015-10-16 11:00:33 +03:00
$message = 'Sharing failed, because the user ' . $this -> user1 . ' is the original sharer' ;
2012-08-15 19:55:54 +04:00
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user1 , \OCP\Constants :: PERMISSION_READ );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
2012-11-11 22:58:54 +04:00
2012-08-20 00:30:38 +04:00
// Unshare
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
$this -> assertTrue ( \OCP\Share :: unshare ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 ));
2012-11-11 22:58:54 +04:00
2012-08-15 19:55:54 +04:00
// Attempt reshare without share permission
2016-05-19 10:15:10 +03:00
$this -> assertTrue ( \OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ ));
\OC_User :: setUserId ( $this -> user2 );
2012-08-15 19:55:54 +04:00
$message = 'Sharing test.txt failed, because resharing is not allowed' ;
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user3 , \OCP\Constants :: PERMISSION_READ );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
2012-11-11 22:58:54 +04:00
2012-11-05 01:16:04 +04:00
// Owner grants share and update permission
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
$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 ));
2012-11-11 22:58:54 +04:00
2012-08-15 19:55:54 +04:00
// Attempt reshare with escalated permissions
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
2012-08-15 19:55:54 +04:00
$message = 'Sharing test.txt failed, because the permissions exceed permissions granted to ' . $this -> user2 ;
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user3 , \OCP\Constants :: PERMISSION_READ | \OCP\Constants :: PERMISSION_DELETE );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
2012-11-11 22:58:54 +04:00
2012-08-15 19:55:54 +04:00
// Valid reshare
2016-05-19 10:15:10 +03:00
$this -> assertTrue ( \OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user3 , \OCP\Constants :: PERMISSION_READ | \OCP\Constants :: PERMISSION_UPDATE ));
$this -> assertEquals ( array ( 'test.txt' ), \OCP\Share :: getItemShared ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ));
\OC_User :: setUserId ( $this -> user3 );
$this -> assertEquals ( array ( 'test.txt' ), \OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ));
$this -> assertEquals ( array ( \OCP\Constants :: PERMISSION_READ | \OCP\Constants :: PERMISSION_UPDATE ), \OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' , Backend :: FORMAT_PERMISSIONS ));
2012-11-11 22:58:54 +04:00
2012-08-15 19:55:54 +04:00
// Attempt to escalate permissions
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
2012-08-15 19:55:54 +04:00
$message = 'Setting permissions for test.txt failed, because the permissions exceed permissions granted to ' . $this -> user2 ;
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: setPermissions ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user3 , \OCP\Constants :: PERMISSION_READ | \OCP\Constants :: PERMISSION_DELETE );
2012-08-15 19:55:54 +04:00
$this -> fail ( 'Exception was expected: ' . $message );
2016-05-19 10:15:10 +03:00
} catch ( \Exception $exception ) {
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( $message , $exception -> getMessage ());
2012-08-15 19:55:54 +04:00
}
2012-11-11 22:58:54 +04:00
2012-08-15 19:55:54 +04:00
// Remove update permission
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
$this -> assertTrue ( \OCP\Share :: setPermissions ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ | \OCP\Constants :: PERMISSION_SHARE ));
\OC_User :: setUserId ( $this -> user2 );
$this -> assertEquals ( array ( \OCP\Constants :: PERMISSION_READ | \OCP\Constants :: PERMISSION_SHARE ), \OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' , Backend :: FORMAT_PERMISSIONS ));
\OC_User :: setUserId ( $this -> user3 );
$this -> assertEquals ( array ( \OCP\Constants :: PERMISSION_READ ), \OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' , Backend :: FORMAT_PERMISSIONS ));
2012-11-11 22:58:54 +04:00
2012-08-15 19:55:54 +04:00
// Remove share permission
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
$this -> assertTrue ( \OCP\Share :: setPermissions ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ ));
\OC_User :: setUserId ( $this -> user2 );
$this -> assertEquals ( array ( \OCP\Constants :: PERMISSION_READ ), \OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' , Backend :: FORMAT_PERMISSIONS ));
\OC_User :: setUserId ( $this -> user3 );
$this -> assertSame ( array (), \OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' ));
2012-11-11 22:58:54 +04:00
2012-08-15 19:55:54 +04:00
// Reshare again, and then have owner unshare
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
$this -> assertTrue ( \OCP\Share :: setPermissions ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ | \OCP\Constants :: PERMISSION_SHARE ));
\OC_User :: setUserId ( $this -> user2 );
$this -> assertTrue ( \OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user3 , \OCP\Constants :: PERMISSION_READ ));
\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 );
$this -> assertSame ( array (), \OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' ));
\OC_User :: setUserId ( $this -> user3 );
$this -> assertSame ( array (), \OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' ));
2012-11-11 22:58:54 +04:00
2012-08-15 19:55:54 +04:00
// Attempt target conflict
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
$this -> assertTrue ( \OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ ));
\OC_User :: setUserId ( $this -> user3 );
$this -> assertTrue ( \OCP\Share :: shareItem ( 'test' , 'share.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ ));
2012-08-28 02:12:01 +04:00
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
$to_test = \OCP\Share :: getItemsSharedWith ( 'test' , Backend :: FORMAT_TARGET );
2012-10-12 22:05:45 +04:00
$this -> assertEquals ( 2 , count ( $to_test ));
2012-08-28 02:12:01 +04:00
$this -> assertTrue ( in_array ( 'test.txt' , $to_test ));
$this -> assertTrue ( in_array ( 'test1.txt' , $to_test ));
2012-08-22 19:35:30 +04:00
2014-08-23 14:05:19 +04:00
// Unshare from self
2016-05-19 10:15:10 +03:00
$this -> assertTrue ( \OCP\Share :: unshareFromSelf ( 'test' , 'test.txt' ));
$this -> assertEquals ( array ( 'test1.txt' ), \OCP\Share :: getItemsSharedWith ( 'test' , Backend :: FORMAT_TARGET ));
2014-08-23 14:05:19 +04:00
// Unshare from self via source
2016-05-19 10:15:10 +03:00
$this -> assertTrue ( \OCP\Share :: unshareFromSelf ( 'test' , 'share.txt' , true ));
$this -> assertEquals ( array (), \OCP\Share :: getItemsSharedWith ( 'test' , Backend :: FORMAT_TARGET ));
2014-08-23 14:05:19 +04:00
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
$this -> assertTrue ( \OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ ));
\OC_User :: setUserId ( $this -> user3 );
$this -> assertTrue ( \OCP\Share :: shareItem ( 'test' , 'share.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ ));
2014-08-23 14:05:19 +04:00
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
$to_test = \OCP\Share :: getItemsSharedWith ( 'test' , Backend :: FORMAT_TARGET );
2014-08-23 14:05:19 +04:00
$this -> assertEquals ( 2 , count ( $to_test ));
$this -> assertTrue ( in_array ( 'test.txt' , $to_test ));
$this -> assertTrue ( in_array ( 'test1.txt' , $to_test ));
2012-08-16 20:20:14 +04:00
// Remove user
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2015-12-17 17:59:23 +03:00
$user = \OC :: $server -> getUserManager () -> get ( $this -> user1 );
if ( $user !== null ) { $user -> delete (); }
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
$this -> assertEquals ( array ( 'test1.txt' ), \OCP\Share :: getItemsSharedWith ( 'test' , Backend :: FORMAT_TARGET ));
2012-08-15 19:55:54 +04:00
}
2012-07-26 01:08:18 +04:00
2013-09-05 04:45:52 +04:00
public function testShareWithUserExpirationExpired () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2013-09-04 19:26:30 +04:00
$this -> shareUserOneTestFileWithUserTwo ();
2014-06-03 17:15:04 +04:00
$this -> shareUserTestFileAsLink ();
2013-09-04 19:26:30 +04:00
2014-07-23 18:42:33 +04:00
// 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 ();
2013-09-04 19:26:30 +04:00
2016-05-19 10:15:10 +03:00
$shares = \OCP\Share :: getItemsShared ( 'test' );
2014-06-03 17:15:04 +04:00
$this -> assertSame ( 1 , count ( $shares ));
$share = reset ( $shares );
$this -> assertSame ( \OCP\Share :: SHARE_TYPE_USER , $share [ 'share_type' ]);
2013-09-04 19:26:30 +04:00
}
2015-03-24 13:08:19 +03:00
public function testGetShareFromOutsideFilesFolder () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2015-03-24 13:08:19 +03:00
$view = new \OC\Files\View ( '/' . $this -> user1 . '/' );
$view -> mkdir ( 'files/test' );
$view -> mkdir ( 'files/test/sub' );
$view -> mkdir ( 'files_trashbin' );
$view -> mkdir ( 'files_trashbin/files' );
$fileInfo = $view -> getFileInfo ( 'files/test/sub' );
2015-09-28 17:38:01 +03:00
$this -> assertInstanceOf ( '\OC\Files\FileInfo' , $fileInfo );
2015-03-24 13:08:19 +03:00
$fileId = $fileInfo -> getId ();
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'folder' , $fileId , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ ),
2015-03-24 13:08:19 +03:00
'Failed asserting that user 1 successfully shared "test/sub" with user 2.'
);
2016-05-19 10:15:10 +03:00
$result = \OCP\Share :: getItemShared ( 'folder' , $fileId , Backend :: FORMAT_SOURCE );
2015-03-24 13:08:19 +03:00
$this -> assertNotEmpty ( $result );
2016-05-19 10:15:10 +03:00
$result = \OCP\Share :: getItemSharedWithUser ( 'folder' , $fileId , $this -> user2 );
2015-03-24 13:08:19 +03:00
$this -> assertNotEmpty ( $result );
2016-05-19 10:15:10 +03:00
$result = \OCP\Share :: getItemsSharedWithUser ( 'folder' , $this -> user2 );
2015-03-24 13:08:19 +03:00
$this -> assertNotEmpty ( $result );
// move to trash (keeps file id)
$view -> rename ( 'files/test' , 'files_trashbin/files/test' );
2016-05-19 10:15:10 +03:00
$result = \OCP\Share :: getItemShared ( 'folder' , $fileId , Backend :: FORMAT_SOURCE );
2015-03-24 13:08:19 +03:00
$this -> assertEmpty ( $result , 'Share must not be returned for files outside of "files"' );
2016-05-19 10:15:10 +03:00
$result = \OCP\Share :: getItemSharedWithUser ( 'folder' , $fileId , $this -> user2 );
2015-03-24 13:08:19 +03:00
$this -> assertEmpty ( $result , 'Share must not be returned for files outside of "files"' );
2016-05-19 10:15:10 +03:00
$result = \OCP\Share :: getItemsSharedWithUser ( 'folder' , $this -> user2 );
2015-03-24 13:08:19 +03:00
$this -> assertEmpty ( $result , 'Share must not be returned for files outside of "files"' );
}
2014-07-23 18:42:33 +04:00
public function testSetExpireDateInPast () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2014-07-23 18:42:33 +04:00
$this -> shareUserOneTestFileWithUserTwo ();
$this -> shareUserTestFileAsLink ();
$setExpireDateFailed = false ;
try {
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: setExpirationDate ( 'test' , 'test.txt' , $this -> dateInPast , '' ),
2014-07-23 18:42:33 +04:00
'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
);
} catch ( \Exception $e ) {
$setExpireDateFailed = true ;
}
$this -> assertTrue ( $setExpireDateFailed );
}
2013-09-05 04:45:52 +04:00
public function testShareWithUserExpirationValid () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2013-09-04 19:26:30 +04:00
$this -> shareUserOneTestFileWithUserTwo ();
2014-06-03 17:15:04 +04:00
$this -> shareUserTestFileAsLink ();
2013-09-04 19:26:30 +04:00
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: setExpirationDate ( 'test' , 'test.txt' , $this -> dateInFuture , '' ),
2013-09-04 19:26:30 +04:00
'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
2013-09-04 19:15:08 +04:00
);
2016-05-19 10:15:10 +03:00
$shares = \OCP\Share :: getItemsShared ( 'test' );
2014-06-03 17:15:04 +04:00
$this -> assertSame ( 2 , count ( $shares ));
2013-09-04 19:26:30 +04:00
}
2013-09-04 19:15:08 +04:00
2014-07-29 00:35:11 +04:00
/*
* if user is in a group excluded from resharing , then the share permission should
* be removed
*/
public function testShareWithUserAndUserIsExcludedFromResharing () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2014-07-29 00:35:11 +04:00
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_USER , $this -> user4 , \OCP\Constants :: PERMISSION_ALL ),
2014-07-29 00:35:11 +04:00
'Failed asserting that user 1 successfully shared text.txt with user 4.'
);
$this -> assertContains (
'test.txt' ,
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemShared ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ),
2014-07-29 00:35:11 +04:00
'Failed asserting that test.txt is a shared file of user 1.'
);
// exclude group2 from sharing
2015-07-03 15:16:29 +03:00
\OC :: $server -> getAppConfig () -> setValue ( 'core' , 'shareapi_exclude_groups_list' , $this -> group2 );
\OC :: $server -> getAppConfig () -> setValue ( 'core' , 'shareapi_exclude_groups' , " yes " );
2014-07-29 00:35:11 +04:00
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user4 );
2014-07-29 00:35:11 +04:00
2016-05-19 10:15:10 +03:00
$share = \OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' );
2014-07-29 00:35:11 +04:00
2014-11-25 18:28:41 +03:00
$this -> assertSame ( \OCP\Constants :: PERMISSION_ALL & ~ \OCP\Constants :: PERMISSION_SHARE , $share [ 'permissions' ],
2014-07-29 00:35:11 +04:00
'Failed asserting that user 4 is excluded from re-sharing' );
2015-07-03 15:16:29 +03:00
\OC :: $server -> getAppConfig () -> deleteKey ( 'core' , 'shareapi_exclude_groups_list' );
\OC :: $server -> getAppConfig () -> deleteKey ( 'core' , 'shareapi_exclude_groups' );
2014-07-29 00:35:11 +04:00
}
2015-04-29 14:33:20 +03:00
public function testSharingAFolderThatIsSharedWithAGroupOfTheOwner () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2015-04-29 14:33:20 +03:00
$view = new \OC\Files\View ( '/' . $this -> user1 . '/' );
$view -> mkdir ( 'files/test' );
$view -> mkdir ( 'files/test/sub1' );
$view -> mkdir ( 'files/test/sub1/sub2' );
$fileInfo = $view -> getFileInfo ( 'files/test/sub1' );
2015-09-28 17:38:01 +03:00
$this -> assertInstanceOf ( '\OC\Files\FileInfo' , $fileInfo );
2015-04-29 14:33:20 +03:00
$fileId = $fileInfo -> getId ();
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'folder' , $fileId , \OCP\Share :: SHARE_TYPE_GROUP , $this -> group1 , \OCP\Constants :: PERMISSION_READ + \OCP\Constants :: PERMISSION_CREATE ),
2015-04-29 14:33:20 +03:00
'Failed asserting that user 1 successfully shared "test/sub1" with group 1.'
);
2016-05-19 10:15:10 +03:00
$result = \OCP\Share :: getItemShared ( 'folder' , $fileId , Backend :: FORMAT_SOURCE );
2015-04-29 14:33:20 +03:00
$this -> assertNotEmpty ( $result );
$this -> assertEquals ( \OCP\Constants :: PERMISSION_READ + \OCP\Constants :: PERMISSION_CREATE , $result [ 'permissions' ]);
$fileInfo = $view -> getFileInfo ( 'files/test/sub1/sub2' );
2015-09-28 17:38:01 +03:00
$this -> assertInstanceOf ( '\OC\Files\FileInfo' , $fileInfo );
2015-04-29 14:33:20 +03:00
$fileId = $fileInfo -> getId ();
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'folder' , $fileId , \OCP\Share :: SHARE_TYPE_USER , $this -> user4 , \OCP\Constants :: PERMISSION_READ ),
2015-04-29 14:33:20 +03:00
'Failed asserting that user 1 successfully shared "test/sub1/sub2" with user 4.'
);
2016-05-19 10:15:10 +03:00
$result = \OCP\Share :: getItemShared ( 'folder' , $fileId , Backend :: FORMAT_SOURCE );
2015-04-29 14:33:20 +03:00
$this -> assertNotEmpty ( $result );
$this -> assertEquals ( \OCP\Constants :: PERMISSION_READ , $result [ 'permissions' ]);
}
2015-10-05 13:36:16 +03:00
public function testSharingAFileInsideAFolderThatIsAlreadyShared () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2015-10-05 13:36:16 +03:00
$view = new \OC\Files\View ( '/' . $this -> user1 . '/' );
$view -> mkdir ( 'files/test' );
$view -> mkdir ( 'files/test/sub1' );
$view -> file_put_contents ( 'files/test/sub1/file.txt' , 'abc' );
$folderInfo = $view -> getFileInfo ( 'files/test/sub1' );
2015-09-28 17:38:01 +03:00
$this -> assertInstanceOf ( '\OC\Files\FileInfo' , $folderInfo );
2015-10-05 13:36:16 +03:00
$folderId = $folderInfo -> getId ();
$fileInfo = $view -> getFileInfo ( 'files/test/sub1/file.txt' );
2015-09-28 17:38:01 +03:00
$this -> assertInstanceOf ( '\OC\Files\FileInfo' , $fileInfo );
2015-10-05 13:36:16 +03:00
$fileId = $fileInfo -> getId ();
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'folder' , $folderId , \OCP\Share :: SHARE_TYPE_GROUP , $this -> group2 , \OCP\Constants :: PERMISSION_READ + \OCP\Constants :: PERMISSION_UPDATE ),
2015-10-05 13:36:16 +03:00
'Failed asserting that user 1 successfully shared "test/sub1" with group 2.'
);
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'file' , $fileId , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_READ ),
2015-10-05 13:36:16 +03:00
'Failed asserting that user 1 successfully shared "test/sub1/file.txt" with user 2.'
);
$result = \OCP\Share :: getItemsSharedWithUser ( 'file' , $this -> user2 );
$this -> assertCount ( 2 , $result );
foreach ( $result as $share ) {
$itemName = substr ( $share [ 'path' ], strrpos ( $share [ 'path' ], '/' ));
$this -> assertSame ( $itemName , $share [ 'file_target' ], 'Asserting that the file_target is the last segment of the path' );
$this -> assertSame ( $share [ 'item_target' ], '/' . $share [ 'item_source' ], 'Asserting that the item is the item that was shared' );
}
}
2013-09-05 04:45:52 +04:00
protected function shareUserOneTestFileWithGroupOne () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2013-09-05 04:41:24 +04:00
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_GROUP , $this -> group1 , \OCP\Constants :: PERMISSION_READ ),
2013-09-05 04:41:24 +04:00
'Failed asserting that user 1 successfully shared text.txt with group 1.'
);
2013-09-23 18:16:48 +04:00
$this -> assertContains (
'test.txt' ,
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemShared ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ),
2013-09-05 04:41:24 +04:00
'Failed asserting that test.txt is a shared file of user 1.'
);
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
2013-09-23 18:16:48 +04:00
$this -> assertContains (
'test.txt' ,
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ),
2013-09-05 04:41:24 +04:00
'Failed asserting that user 2 has access to test.txt after initial sharing.'
);
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user3 );
2013-09-23 18:16:48 +04:00
$this -> assertContains (
'test.txt' ,
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ),
2013-09-05 04:41:24 +04:00
'Failed asserting that user 3 has access to test.txt after initial sharing.'
);
}
2015-01-20 15:09:39 +03:00
/**
* Test that unsharing from group will also delete all
* child entries
*/
public function testShareWithGroupThenUnshare () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user5 );
\OCP\Share :: shareItem (
2015-01-20 15:09:39 +03:00
'test' ,
'test.txt' ,
2016-05-19 10:15:10 +03:00
\OCP\Share :: SHARE_TYPE_GROUP ,
2015-01-20 15:09:39 +03:00
$this -> group1 ,
\OCP\Constants :: PERMISSION_ALL
);
$targetUsers = array ( $this -> user1 , $this -> user2 , $this -> user3 );
foreach ( $targetUsers as $targetUser ) {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $targetUser );
$items = \OCP\Share :: getItemsSharedWithUser (
2015-01-20 15:09:39 +03:00
'test' ,
$targetUser ,
2016-05-19 10:15:10 +03:00
Backend :: FORMAT_TARGET
2015-01-20 15:09:39 +03:00
);
$this -> assertEquals ( 1 , count ( $items ));
}
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user5 );
\OCP\Share :: unshare (
2015-01-20 15:09:39 +03:00
'test' ,
'test.txt' ,
2016-05-19 10:15:10 +03:00
\OCP\Share :: SHARE_TYPE_GROUP ,
2015-01-20 15:09:39 +03:00
$this -> group1
);
// verify that all were deleted
foreach ( $targetUsers as $targetUser ) {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $targetUser );
$items = \OCP\Share :: getItemsSharedWithUser (
2015-01-20 15:09:39 +03:00
'test' ,
$targetUser ,
2016-05-19 10:15:10 +03:00
Backend :: FORMAT_TARGET
2015-01-20 15:09:39 +03:00
);
$this -> assertEquals ( 0 , count ( $items ));
}
}
2014-11-17 15:09:13 +03:00
public function testShareWithGroupAndUserBothHaveTheSameId () {
$this -> shareUserTestFileWithUser ( $this -> user1 , $this -> groupAndUser );
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> groupAndUser );
2014-11-17 15:09:13 +03:00
2016-05-19 10:15:10 +03:00
$this -> assertEquals ( array ( 'test.txt' ), \OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ),
2014-11-17 15:09:13 +03:00
'"groupAndUser"-User does not see the file but it was shared with him' );
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
$this -> assertEquals ( array (), \OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ),
2014-11-17 15:09:13 +03:00
'User2 sees test.txt but it was only shared with the user "groupAndUser" and not with group' );
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
$this -> assertTrue ( \OCP\Share :: unshareAll ( 'test' , 'test.txt' ));
2014-11-17 15:09:13 +03:00
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_GROUP , $this -> groupAndUser , \OCP\Constants :: PERMISSION_READ ),
2014-11-17 15:09:13 +03:00
'Failed asserting that user 1 successfully shared text.txt with group 1.'
);
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> groupAndUser );
$this -> assertEquals ( array (), \OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ),
2014-11-17 15:09:13 +03:00
'"groupAndUser"-User sees test.txt but it was only shared with the group "groupAndUser" and not with the user' );
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
$this -> assertEquals ( array ( 'test.txt' ), \OCP\Share :: getItemSharedWith ( 'test' , 'test.txt' , Backend :: FORMAT_SOURCE ),
2014-11-17 15:09:13 +03:00
'User2 does not see test.txt but it was shared with the group "groupAndUser"' );
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
$this -> assertTrue ( \OCP\Share :: unshareAll ( 'test' , 'test.txt' ));
2014-11-17 15:09:13 +03:00
}
2014-02-19 12:31:54 +04:00
/**
* @ param boolean | string $token
2015-04-29 14:33:20 +03:00
* @ return array
2014-02-19 12:31:54 +04:00
*/
2013-09-14 19:56:55 +04:00
protected function getShareByValidToken ( $token ) {
2016-05-19 10:15:10 +03:00
$row = \OCP\Share :: getShareByToken ( $token );
2013-09-14 19:56:55 +04:00
$this -> assertInternalType (
'array' ,
$row ,
" Failed asserting that a share for token $token exists. "
);
return $row ;
}
2014-11-10 15:08:45 +03:00
public function testGetItemSharedWithUser () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2014-11-10 15:08:45 +03:00
//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 (?,?,?,?,?,?)' );
2016-05-19 10:15:10 +03:00
$args = array ( 'test' , 99 , 'target1' , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , $this -> user1 );
2014-11-10 15:08:45 +03:00
$query -> execute ( $args );
2016-05-19 10:15:10 +03:00
$args = array ( 'test' , 99 , 'target2' , \OCP\Share :: SHARE_TYPE_USER , $this -> user4 , $this -> user1 );
2014-11-10 15:08:45 +03:00
$query -> execute ( $args );
2016-05-19 10:15:10 +03:00
$args = array ( 'test' , 99 , 'target3' , \OCP\Share :: SHARE_TYPE_USER , $this -> user3 , $this -> user2 );
2014-11-10 15:08:45 +03:00
$query -> execute ( $args );
2016-05-19 10:15:10 +03:00
$args = array ( 'test' , 99 , 'target4' , \OCP\Share :: SHARE_TYPE_USER , $this -> user3 , $this -> user4 );
2014-11-10 15:08:45 +03:00
$query -> execute ( $args );
2016-05-19 10:15:10 +03:00
$args = array ( 'test' , 99 , 'target4' , \OCP\Share :: SHARE_TYPE_USER , $this -> user6 , $this -> user4 );
2015-01-19 16:39:00 +03:00
$query -> execute ( $args );
2014-11-10 15:08:45 +03:00
$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 );
2015-01-19 16:39:00 +03:00
$this -> assertSame ( 5 , count ( $result4 )); // 5 because target4 appears twice
2014-11-10 15:08:45 +03:00
$this -> verifyResult ( $result4 , array ( 'target1' , 'target2' , 'target3' , 'target4' ));
2015-01-19 16:39:00 +03:00
$result6 = \OCP\Share :: getItemSharedWithUser ( 'test' , 99 , $this -> user6 , null );
$this -> assertSame ( 1 , count ( $result6 ));
$this -> verifyResult ( $result6 , array ( 'target4' ));
2014-11-10 15:08:45 +03:00
}
2015-01-16 20:11:13 +03:00
public function testGetItemSharedWithUserFromGroupShare () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2015-01-16 20:11:13 +03:00
//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 (?,?,?,?,?,?)' );
2016-05-19 10:15:10 +03:00
$args = array ( 'test' , 99 , 'target1' , \OCP\Share :: SHARE_TYPE_GROUP , $this -> group1 , $this -> user1 );
2015-01-16 20:11:13 +03:00
$query -> execute ( $args );
2016-05-19 10:15:10 +03:00
$args = array ( 'test' , 99 , 'target2' , \OCP\Share :: SHARE_TYPE_GROUP , $this -> group2 , $this -> user1 );
2015-01-16 20:11:13 +03:00
$query -> execute ( $args );
2016-05-19 10:15:10 +03:00
$args = array ( 'test' , 99 , 'target3' , \OCP\Share :: SHARE_TYPE_GROUP , $this -> group1 , $this -> user2 );
2015-01-16 20:11:13 +03:00
$query -> execute ( $args );
2016-05-19 10:15:10 +03:00
$args = array ( 'test' , 99 , 'target4' , \OCP\Share :: SHARE_TYPE_GROUP , $this -> group1 , $this -> user4 );
2015-01-16 20:11:13 +03:00
$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' ));
2015-01-19 16:39:00 +03:00
$result6 = \OCP\Share :: getItemSharedWithUser ( 'test' , 99 , $this -> user6 , null );
$this -> assertSame ( 0 , count ( $result6 ));
2015-01-16 20:11:13 +03:00
}
2014-11-10 15:08:45 +03:00
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' );
}
2015-10-05 12:27:47 +03:00
public function testGetShareSubItemsWhenUserNotInGroup () {
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_GROUP , $this -> group1 , \OCP\Constants :: PERMISSION_READ );
2015-10-05 12:27:47 +03:00
$result = \OCP\Share :: getItemsSharedWithUser ( 'test' , $this -> user2 );
$this -> assertCount ( 1 , $result );
$groupShareId = array_keys ( $result )[ 0 ];
// remove user from group
$userObject = \OC :: $server -> getUserManager () -> get ( $this -> user2 );
\OC :: $server -> getGroupManager () -> get ( $this -> group1 ) -> removeUser ( $userObject );
$result = \OCP\Share :: getItemsSharedWithUser ( 'test' , $this -> user2 );
$this -> assertCount ( 0 , $result );
// test with buggy data
$qb = \OC :: $server -> getDatabaseConnection () -> getQueryBuilder ();
$qb -> insert ( 'share' )
-> values ([
'share_type' => $qb -> expr () -> literal ( 2 ), // group sub-share
'share_with' => $qb -> expr () -> literal ( $this -> user2 ),
'parent' => $qb -> expr () -> literal ( $groupShareId ),
'uid_owner' => $qb -> expr () -> literal ( $this -> user1 ),
'item_type' => $qb -> expr () -> literal ( 'test' ),
'item_source' => $qb -> expr () -> literal ( 'test.txt' ),
'item_target' => $qb -> expr () -> literal ( 'test.txt' ),
'file_target' => $qb -> expr () -> literal ( 'test2.txt' ),
'permissions' => $qb -> expr () -> literal ( 1 ),
'stime' => $qb -> expr () -> literal ( time ()),
]) -> execute ();
$result = \OCP\Share :: getItemsSharedWithUser ( 'test' , $this -> user2 );
$this -> assertCount ( 0 , $result );
$qb -> delete ( 'share' ) -> execute ();
}
2013-09-14 19:56:55 +04:00
public function testShareItemWithLink () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
$token = \OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_LINK , null , \OCP\Constants :: PERMISSION_READ );
2013-09-14 19:56:55 +04:00
$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 (
2016-05-19 10:15:10 +03:00
\OCP\Share :: setExpirationDate ( 'test' , 'test.txt' , $this -> dateInFuture , '' ),
2013-09-14 19:56:55 +04:00
'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.'
);
2014-07-23 18:42:33 +04:00
// 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 ();
2013-09-14 19:56:55 +04:00
$this -> assertFalse (
2016-05-19 10:15:10 +03:00
\OCP\Share :: getShareByToken ( $token ),
2013-09-14 19:56:55 +04:00
'Failed asserting that an expired share could not be found.'
);
}
2013-09-23 18:16:48 +04:00
2014-07-28 19:19:19 +04:00
public function testShareItemWithLinkAndDefaultExpireDate () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2014-07-28 19:19:19 +04:00
2015-03-17 19:26:03 +03:00
$config = \OC :: $server -> getConfig ();
$config -> setAppValue ( 'core' , 'shareapi_default_expire_date' , 'yes' );
$config -> setAppValue ( 'core' , 'shareapi_expire_after_n_days' , '2' );
2014-07-28 19:19:19 +04:00
2016-05-19 10:15:10 +03:00
$token = \OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_LINK , null , \OCP\Constants :: PERMISSION_READ );
2014-07-28 19:19:19 +04:00
$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.'
);
2015-03-17 19:26:03 +03:00
$config -> deleteAppValue ( 'core' , 'shareapi_default_expire_date' );
$config -> deleteAppValue ( 'core' , 'shareapi_expire_after_n_days' );
2014-07-28 19:19:19 +04:00
}
2015-06-18 13:46:52 +03:00
public function dataShareWithRemoteUserAndRemoteIsInvalid () {
return [
// Invalid path
array ( 'user@' ),
// Invalid user
array ( '@server' ),
array ( 'us/er@server' ),
array ( 'us:er@server' ),
// Invalid splitting
array ( 'user' ),
array ( '' ),
array ( 'us/erserver' ),
array ( 'us:erserver' ),
];
}
/**
* @ dataProvider dataShareWithRemoteUserAndRemoteIsInvalid
*
* @ param string $remoteId
* @ expectedException \OC\HintException
*/
public function testShareWithRemoteUserAndRemoteIsInvalid ( $remoteId ) {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_REMOTE , $remoteId , \OCP\Constants :: PERMISSION_ALL );
2015-06-18 13:46:52 +03:00
}
2013-09-23 18:16:48 +04:00
public function testUnshareAll () {
2014-01-10 05:08:29 +04:00
$this -> shareUserTestFileWithUser ( $this -> user1 , $this -> user2 );
$this -> shareUserTestFileWithUser ( $this -> user2 , $this -> user3 );
$this -> shareUserTestFileWithUser ( $this -> user3 , $this -> user4 );
2013-09-23 18:16:48 +04:00
$this -> shareUserOneTestFileWithGroupOne ();
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2013-09-23 18:16:48 +04:00
$this -> assertEquals (
array ( 'test.txt' , 'test.txt' ),
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemsShared ( 'test' , Backend :: FORMAT_SOURCE ),
2014-01-10 05:08:29 +04:00
'Failed asserting that the test.txt file is shared exactly two times by user1.'
);
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
2014-01-10 05:08:29 +04:00
$this -> assertEquals (
array ( 'test.txt' ),
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemsShared ( 'test' , Backend :: FORMAT_SOURCE ),
2014-01-10 05:08:29 +04:00
'Failed asserting that the test.txt file is shared exactly once by user2.'
);
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user3 );
2014-01-10 05:08:29 +04:00
$this -> assertEquals (
array ( 'test.txt' ),
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemsShared ( 'test' , Backend :: FORMAT_SOURCE ),
2014-01-10 05:08:29 +04:00
'Failed asserting that the test.txt file is shared exactly once by user3.'
2013-09-23 18:16:48 +04:00
);
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: unshareAll ( 'test' , 'test.txt' ),
2014-01-10 05:08:29 +04:00
'Failed asserting that user 3 successfully unshared all shares of the test.txt share.'
2013-09-23 18:16:48 +04:00
);
$this -> assertEquals (
array (),
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemsShared ( 'test' ),
2014-01-10 05:08:29 +04:00
'Failed asserting that the share of the test.txt file by user 3 has been removed.'
);
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2014-01-10 05:08:29 +04:00
$this -> assertEquals (
array (),
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemsShared ( 'test' ),
2014-01-10 05:08:29 +04:00
'Failed asserting that both shares of the test.txt file by user 1 have been removed.'
);
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
2014-01-10 05:08:29 +04:00
$this -> assertEquals (
array (),
2016-05-19 10:15:10 +03:00
\OCP\Share :: getItemsShared ( 'test' ),
2014-01-10 05:08:29 +04:00
'Failed asserting that the share of the test.txt file by user 2 has been removed.'
2013-09-23 18:16:48 +04:00
);
}
2014-01-21 15:07:08 +04:00
/**
* @ dataProvider checkPasswordProtectedShareDataProvider
* @ param $expected
* @ param $item
*/
public function testCheckPasswordProtectedShare ( $expected , $item ) {
2016-05-31 07:53:28 +03:00
\OC :: $server -> getSession () -> set ( 'public_link_authenticated' , '100' );
2014-01-21 15:07:08 +04:00
$result = \OCP\Share :: checkPasswordProtectedShare ( $item );
$this -> assertEquals ( $expected , $result );
}
function checkPasswordProtectedShareDataProvider () {
return array (
array ( true , array ()),
array ( true , array ( 'share_with' => null )),
array ( true , array ( 'share_with' => '' )),
array ( true , array ( 'share_with' => '1234567890' , 'share_type' => '1' )),
array ( true , array ( 'share_with' => '1234567890' , 'share_type' => 1 )),
2016-05-31 07:53:28 +03:00
array ( true , array ( 'share_with' => '1234567890' , 'share_type' => '3' , 'id' => '100' )),
array ( true , array ( 'share_with' => '1234567890' , 'share_type' => 3 , 'id' => '100' )),
2014-01-21 15:07:08 +04:00
array ( true , array ( 'share_with' => '1234567890' , 'share_type' => '3' , 'id' => 100 )),
array ( true , array ( 'share_with' => '1234567890' , 'share_type' => 3 , 'id' => 100 )),
2016-05-31 07:53:28 +03:00
array ( false , array ( 'share_with' => '1234567890' , 'share_type' => '3' , 'id' => '101' )),
array ( false , array ( 'share_with' => '1234567890' , 'share_type' => 3 , 'id' => '101' )),
2014-01-21 15:07:08 +04:00
array ( false , array ( 'share_with' => '1234567890' , 'share_type' => '3' , 'id' => 101 )),
array ( false , array ( 'share_with' => '1234567890' , 'share_type' => 3 , 'id' => 101 )),
);
2014-08-01 18:24:19 +04:00
}
2014-01-21 15:07:08 +04:00
2014-12-04 21:51:04 +03:00
/**
* @ dataProvider urls
*/
function testRemoveProtocolFromUrl ( $url , $expectedResult ) {
$share = new \OC\Share\Share ();
2015-06-03 13:03:02 +03:00
$result = self :: invokePrivate ( $share , 'removeProtocolFromUrl' , array ( $url ));
2014-12-04 21:51:04 +03:00
$this -> assertSame ( $expectedResult , $result );
}
function urls () {
return array (
array ( 'http://owncloud.org' , 'owncloud.org' ),
array ( 'https://owncloud.org' , 'owncloud.org' ),
array ( 'owncloud.org' , 'owncloud.org' ),
);
}
2015-04-27 23:07:35 +03:00
public function dataRemoteShareUrlCalls () {
return [
[ 'admin@localhost' , 'localhost' ],
[ 'admin@https://localhost' , 'localhost' ],
[ 'admin@http://localhost' , 'localhost' ],
[ 'admin@localhost/subFolder' , 'localhost/subFolder' ],
];
}
/**
* @ dataProvider dataRemoteShareUrlCalls
*
* @ param string $shareWith
* @ param string $urlHost
*/
public function testRemoteShareUrlCalls ( $shareWith , $urlHost ) {
$oldHttpHelper = \OC :: $server -> query ( 'HTTPHelper' );
$httpHelperMock = $this -> getMockBuilder ( 'OC\HttpHelper' )
-> disableOriginalConstructor ()
-> getMock ();
$this -> setHttpHelper ( $httpHelperMock );
$httpHelperMock -> expects ( $this -> at ( 0 ))
-> method ( 'post' )
-> with ( $this -> stringStartsWith ( 'https://' . $urlHost . '/ocs/v1.php/cloud/shares' ), $this -> anything ())
-> willReturn ([ 'success' => false , 'result' => 'Exception' ]);
$httpHelperMock -> expects ( $this -> at ( 1 ))
-> method ( 'post' )
-> with ( $this -> stringStartsWith ( 'http://' . $urlHost . '/ocs/v1.php/cloud/shares' ), $this -> anything ())
-> willReturn ([ 'success' => true , 'result' => json_encode ([ 'ocs' => [ 'meta' => [ 'statuscode' => 100 ]]])]);
2015-04-28 15:56:13 +03:00
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_REMOTE , $shareWith , \OCP\Constants :: PERMISSION_READ );
$shares = \OCP\Share :: getItemShared ( 'test' , 'test.txt' );
$share = array_shift ( $shares );
$httpHelperMock -> expects ( $this -> at ( 0 ))
2015-04-27 23:07:35 +03:00
-> method ( 'post' )
2015-04-28 15:56:13 +03:00
-> with ( $this -> stringStartsWith ( 'https://' . $urlHost . '/ocs/v1.php/cloud/shares/' . $share [ 'id' ] . '/unshare' ), $this -> anything ())
2015-04-27 23:07:35 +03:00
-> willReturn ([ 'success' => false , 'result' => 'Exception' ]);
2015-04-28 15:56:13 +03:00
$httpHelperMock -> expects ( $this -> at ( 1 ))
2015-04-27 23:07:35 +03:00
-> method ( 'post' )
2015-04-28 15:56:13 +03:00
-> with ( $this -> stringStartsWith ( 'http://' . $urlHost . '/ocs/v1.php/cloud/shares/' . $share [ 'id' ] . '/unshare' ), $this -> anything ())
2015-04-27 23:07:35 +03:00
-> willReturn ([ 'success' => true , 'result' => json_encode ([ 'ocs' => [ 'meta' => [ 'statuscode' => 100 ]]])]);
\OCP\Share :: unshare ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_REMOTE , $shareWith );
$this -> setHttpHelper ( $oldHttpHelper );
}
2014-08-01 18:24:19 +04:00
/**
* @ dataProvider dataProviderTestGroupItems
2015-04-27 23:07:35 +03:00
* @ param array $ungrouped
* @ param array $grouped
2014-08-01 18:24:19 +04:00
*/
function testGroupItems ( $ungrouped , $grouped ) {
2014-01-21 15:07:08 +04:00
2014-08-01 18:24:19 +04:00
$result = DummyShareClass :: groupItemsTest ( $ungrouped );
2014-01-21 15:07:08 +04:00
2014-08-01 18:24:19 +04:00
$this -> compareArrays ( $grouped , $result );
}
function compareArrays ( $result , $expectedResult ) {
foreach ( $expectedResult as $key => $value ) {
if ( is_array ( $value )) {
$this -> compareArrays ( $result [ $key ], $value );
} else {
$this -> assertSame ( $value , $result [ $key ]);
}
2014-01-21 15:07:08 +04:00
}
2014-08-01 18:24:19 +04:00
}
function dataProviderTestGroupItems () {
return array (
// one array with one share
array (
array ( // input
2014-11-25 18:28:41 +03:00
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_ALL , 'item_target' => 't1' )),
2014-08-01 18:24:19 +04:00
array ( // expected result
2014-11-25 18:28:41 +03:00
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_ALL , 'item_target' => 't1' ))),
2014-08-01 18:24:19 +04:00
// two shares both point to the same source
array (
array ( // input
2014-11-25 18:28:41 +03:00
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_READ , 'item_target' => 't1' ),
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_UPDATE , 'item_target' => 't1' ),
2014-08-01 18:24:19 +04:00
),
array ( // expected result
2014-11-25 18:28:41 +03:00
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_READ | \OCP\Constants :: PERMISSION_UPDATE , 'item_target' => 't1' ,
2014-08-01 18:24:19 +04:00
'grouped' => array (
2014-11-25 18:28:41 +03:00
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_READ , 'item_target' => 't1' ),
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_UPDATE , 'item_target' => 't1' ),
2014-08-01 18:24:19 +04:00
)
),
)
),
// two shares both point to the same source but with different targets
array (
array ( // input
2014-11-25 18:28:41 +03:00
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_READ , 'item_target' => 't1' ),
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_UPDATE , 'item_target' => 't2' ),
2014-08-01 18:24:19 +04:00
),
array ( // expected result
2014-11-25 18:28:41 +03:00
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_READ , 'item_target' => 't1' ),
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_UPDATE , 'item_target' => 't2' ),
2014-08-01 18:24:19 +04:00
)
),
// three shares two point to the same source
array (
array ( // input
2014-11-25 18:28:41 +03:00
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_READ , 'item_target' => 't1' ),
array ( 'item_source' => 2 , 'permissions' => \OCP\Constants :: PERMISSION_CREATE , 'item_target' => 't2' ),
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_UPDATE , 'item_target' => 't1' ),
2014-08-01 18:24:19 +04:00
),
array ( // expected result
2014-11-25 18:28:41 +03:00
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_READ | \OCP\Constants :: PERMISSION_UPDATE , 'item_target' => 't1' ,
2014-08-01 18:24:19 +04:00
'grouped' => array (
2014-11-25 18:28:41 +03:00
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_READ , 'item_target' => 't1' ),
array ( 'item_source' => 1 , 'permissions' => \OCP\Constants :: PERMISSION_UPDATE , 'item_target' => 't1' ),
2014-08-01 18:24:19 +04:00
)
),
2014-11-25 18:28:41 +03:00
array ( 'item_source' => 2 , 'permissions' => \OCP\Constants :: PERMISSION_CREATE , 'item_target' => 't2' ),
2014-08-01 18:24:19 +04:00
)
),
);
}
2015-03-19 12:47:09 +03:00
/**
* Ensure that we do not allow removing a an expiration date from a link share if this
* is enforced by the settings .
*/
public function testClearExpireDateWhileEnforced () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2015-03-19 12:47:09 +03:00
2015-07-03 15:16:29 +03:00
\OC :: $server -> getAppConfig () -> setValue ( 'core' , 'shareapi_default_expire_date' , 'yes' );
\OC :: $server -> getAppConfig () -> setValue ( 'core' , 'shareapi_expire_after_n_days' , '2' );
\OC :: $server -> getAppConfig () -> setValue ( 'core' , 'shareapi_enforce_expire_date' , 'yes' );
2015-03-19 12:47:09 +03:00
2016-05-19 10:15:10 +03:00
$token = \OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_LINK , null , \OCP\Constants :: PERMISSION_READ );
2015-03-19 12:47:09 +03:00
$this -> assertInternalType (
'string' ,
$token ,
'Failed asserting that user 1 successfully shared text.txt as link with token.'
);
$setExpireDateFailed = false ;
try {
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: setExpirationDate ( 'test' , 'test.txt' , '' , '' ),
2015-03-19 12:47:09 +03:00
'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
);
} catch ( \Exception $e ) {
$setExpireDateFailed = true ;
}
$this -> assertTrue ( $setExpireDateFailed );
2015-07-03 15:16:29 +03:00
\OC :: $server -> getAppConfig () -> deleteKey ( 'core' , 'shareapi_default_expire_date' );
\OC :: $server -> getAppConfig () -> deleteKey ( 'core' , 'shareapi_expire_after_n_days' );
\OC :: $server -> getAppConfig () -> deleteKey ( 'core' , 'shareapi_enforce_expire_date' );
2015-03-19 12:47:09 +03:00
}
2015-03-13 14:29:13 +03:00
/**
* Cannot set password is there is no user
*
2016-05-19 10:15:10 +03:00
* @ expectedException \Exception
2015-03-13 14:29:13 +03:00
* @ expectedExceptionMessage User not logged in
*/
public function testSetPasswordNoUser () {
$userSession = $this -> getMockBuilder ( '\OCP\IUserSession' )
-> disableOriginalConstructor ()
-> getMock ();
$connection = $this -> getMockBuilder ( '\OC\DB\Connection' )
-> disableOriginalConstructor ()
-> getMock ();
$config = $this -> getMockBuilder ( '\OCP\IConfig' )
-> disableOriginalConstructor ()
-> getMock ();
\OC\Share\Share :: setPassword ( $userSession , $connection , $config , 1 , 'pass' );
}
2015-04-28 15:00:36 +03:00
public function testPasswords () {
$pass = 'secret' ;
$this -> shareUserTestFileAsLink ();
$userSession = \OC :: $server -> getUserSession ();
$connection = \OC :: $server -> getDatabaseConnection ();
$config = $this -> getMockBuilder ( '\OCP\IConfig' )
-> disableOriginalConstructor ()
-> getMock ();
// Find the share ID in the db
2015-07-06 13:34:19 +03:00
$qb = $connection -> getQueryBuilder ();
2015-07-07 23:08:21 +03:00
$qb -> select ( 'id' )
2015-08-10 17:21:41 +03:00
-> from ( 'share' )
2015-07-07 23:08:21 +03:00
-> where ( $qb -> expr () -> eq ( 'item_type' , $qb -> createParameter ( 'type' )))
-> andWhere ( $qb -> expr () -> eq ( 'item_source' , $qb -> createParameter ( 'source' )))
-> andWhere ( $qb -> expr () -> eq ( 'uid_owner' , $qb -> createParameter ( 'owner' )))
-> andWhere ( $qb -> expr () -> eq ( 'share_type' , $qb -> createParameter ( 'share_type' )))
2015-04-28 15:00:36 +03:00
-> setParameter ( 'type' , 'test' )
-> setParameter ( 'source' , 'test.txt' )
-> setParameter ( 'owner' , $this -> user1 )
-> setParameter ( 'share_type' , \OCP\Share :: SHARE_TYPE_LINK );
$res = $qb -> execute () -> fetchAll ();
$this -> assertCount ( 1 , $res );
$id = $res [ 0 ][ 'id' ];
// Set password on share
$res = \OC\Share\Share :: setPassword ( $userSession , $connection , $config , $id , $pass );
$this -> assertTrue ( $res );
// Fetch the hash from the database
2015-07-06 13:34:19 +03:00
$qb = $connection -> getQueryBuilder ();
2015-07-07 23:08:21 +03:00
$qb -> select ( 'share_with' )
2015-08-10 17:21:41 +03:00
-> from ( 'share' )
2015-07-07 23:08:21 +03:00
-> where ( $qb -> expr () -> eq ( 'id' , $qb -> createParameter ( 'id' )))
2015-04-28 15:00:36 +03:00
-> setParameter ( 'id' , $id );
$hash = $qb -> execute () -> fetch ()[ 'share_with' ];
$hasher = \OC :: $server -> getHasher ();
// Verify hash
$this -> assertTrue ( $hasher -> verify ( $pass , $hash ));
}
2015-03-13 14:29:13 +03:00
/**
* Test setting a password when everything is fine
*/
public function testSetPassword () {
$user = $this -> getMockBuilder ( '\OCP\IUser' )
-> disableOriginalConstructor ()
-> getMock ();
$user -> method ( 'getUID' ) -> willReturn ( 'user' );
$userSession = $this -> getMockBuilder ( '\OCP\IUserSession' )
-> disableOriginalConstructor ()
-> getMock ();
$userSession -> method ( 'getUser' ) -> willReturn ( $user );
2016-02-05 17:32:34 +03:00
$ex = $this -> getMockBuilder ( '\OC\DB\QueryBuilder\ExpressionBuilder\ExpressionBuilder' )
2015-03-13 14:29:13 +03:00
-> disableOriginalConstructor ()
-> getMock ();
2015-07-07 18:30:26 +03:00
$qb = $this -> getMockBuilder ( '\OC\DB\QueryBuilder\QueryBuilder' )
2015-03-13 14:29:13 +03:00
-> disableOriginalConstructor ()
-> getMock ();
$qb -> method ( 'update' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'set' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'where' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'andWhere' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'select' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'from' ) -> will ( $this -> returnSelf ());
2015-03-30 22:18:24 +03:00
$qb -> method ( 'setParameter' ) -> will ( $this -> returnSelf ());
2015-03-13 14:29:13 +03:00
$qb -> method ( 'expr' ) -> willReturn ( $ex );
$ret = $this -> getMockBuilder ( '\Doctrine\DBAL\Driver\ResultStatement' )
-> disableOriginalConstructor ()
-> getMock ();
$ret -> method ( 'fetch' ) -> willReturn ([ 'uid_owner' => 'user' ]);
$qb -> method ( 'execute' ) -> willReturn ( $ret );
$connection = $this -> getMockBuilder ( '\OC\DB\Connection' )
-> disableOriginalConstructor ()
-> getMock ();
2015-07-06 13:34:19 +03:00
$connection -> method ( 'getQueryBuilder' ) -> willReturn ( $qb );
2015-03-13 14:29:13 +03:00
$config = $this -> getMockBuilder ( '\OCP\IConfig' )
-> disableOriginalConstructor ()
-> getMock ();
$res = \OC\Share\Share :: setPassword ( $userSession , $connection , $config , 1 , 'pass' );
$this -> assertTrue ( $res );
}
/**
2016-05-19 10:15:10 +03:00
* @ expectedException \Exception
2015-03-13 14:29:13 +03:00
* @ expectedExceptionMessage Cannot remove password
*
* Test removing a password when password is enforced
*/
public function testSetPasswordRemove () {
$user = $this -> getMockBuilder ( '\OCP\IUser' )
-> disableOriginalConstructor ()
-> getMock ();
$user -> method ( 'getUID' ) -> willReturn ( 'user' );
$userSession = $this -> getMockBuilder ( '\OCP\IUserSession' )
-> disableOriginalConstructor ()
-> getMock ();
$userSession -> method ( 'getUser' ) -> willReturn ( $user );
2016-02-05 17:32:34 +03:00
$ex = $this -> getMockBuilder ( '\OC\DB\QueryBuilder\ExpressionBuilder\ExpressionBuilder' )
2015-03-13 14:29:13 +03:00
-> disableOriginalConstructor ()
-> getMock ();
2015-07-07 18:30:26 +03:00
$qb = $this -> getMockBuilder ( '\OC\DB\QueryBuilder\QueryBuilder' )
2015-03-13 14:29:13 +03:00
-> disableOriginalConstructor ()
-> getMock ();
$qb -> method ( 'update' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'select' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'from' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'set' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'where' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'andWhere' ) -> will ( $this -> returnSelf ());
2015-03-30 22:18:24 +03:00
$qb -> method ( 'setParameter' ) -> will ( $this -> returnSelf ());
2015-03-13 14:29:13 +03:00
$qb -> method ( 'expr' ) -> willReturn ( $ex );
$ret = $this -> getMockBuilder ( '\Doctrine\DBAL\Driver\ResultStatement' )
-> disableOriginalConstructor ()
-> getMock ();
$ret -> method ( 'fetch' ) -> willReturn ([ 'uid_owner' => 'user' ]);
$qb -> method ( 'execute' ) -> willReturn ( $ret );
$connection = $this -> getMockBuilder ( '\OC\DB\Connection' )
-> disableOriginalConstructor ()
-> getMock ();
2015-07-06 13:34:19 +03:00
$connection -> method ( 'getQueryBuilder' ) -> willReturn ( $qb );
2015-03-13 14:29:13 +03:00
$config = $this -> getMockBuilder ( '\OCP\IConfig' )
-> disableOriginalConstructor ()
-> getMock ();
$config -> method ( 'getAppValue' ) -> willReturn ( 'yes' );
\OC\Share\Share :: setPassword ( $userSession , $connection , $config , 1 , '' );
}
/**
2016-05-19 10:15:10 +03:00
* @ expectedException \Exception
2015-03-13 14:29:13 +03:00
* @ expectedExceptionMessage Share not found
*
* Test modification of invaid share
*/
public function testSetPasswordInvalidShare () {
$user = $this -> getMockBuilder ( '\OCP\IUser' )
-> disableOriginalConstructor ()
-> getMock ();
$user -> method ( 'getUID' ) -> willReturn ( 'user' );
$userSession = $this -> getMockBuilder ( '\OCP\IUserSession' )
-> disableOriginalConstructor ()
-> getMock ();
$userSession -> method ( 'getUser' ) -> willReturn ( $user );
2016-02-05 17:32:34 +03:00
$ex = $this -> getMockBuilder ( '\OC\DB\QueryBuilder\ExpressionBuilder\ExpressionBuilder' )
2015-03-13 14:29:13 +03:00
-> disableOriginalConstructor ()
-> getMock ();
2015-07-07 18:30:26 +03:00
$qb = $this -> getMockBuilder ( '\OC\DB\QueryBuilder\QueryBuilder' )
2015-03-13 14:29:13 +03:00
-> disableOriginalConstructor ()
-> getMock ();
$qb -> method ( 'update' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'set' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'where' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'andWhere' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'select' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'from' ) -> will ( $this -> returnSelf ());
2015-03-30 22:18:24 +03:00
$qb -> method ( 'setParameter' ) -> will ( $this -> returnSelf ());
2015-03-13 14:29:13 +03:00
$qb -> method ( 'expr' ) -> willReturn ( $ex );
$ret = $this -> getMockBuilder ( '\Doctrine\DBAL\Driver\ResultStatement' )
-> disableOriginalConstructor ()
-> getMock ();
$ret -> method ( 'fetch' ) -> willReturn ([]);
$qb -> method ( 'execute' ) -> willReturn ( $ret );
$connection = $this -> getMockBuilder ( '\OC\DB\Connection' )
-> disableOriginalConstructor ()
-> getMock ();
2015-07-06 13:34:19 +03:00
$connection -> method ( 'getQueryBuilder' ) -> willReturn ( $qb );
2015-03-13 14:29:13 +03:00
$config = $this -> getMockBuilder ( '\OCP\IConfig' )
-> disableOriginalConstructor ()
-> getMock ();
\OC\Share\Share :: setPassword ( $userSession , $connection , $config , 1 , 'pass' );
}
/**
2016-05-19 10:15:10 +03:00
* @ expectedException \Exception
2015-03-13 14:29:13 +03:00
* @ expectedExceptionMessage Cannot update share of a different user
*
* Test modification of share of another user
*/
public function testSetPasswordShareOtherUser () {
$user = $this -> getMockBuilder ( '\OCP\IUser' )
-> disableOriginalConstructor ()
-> getMock ();
$user -> method ( 'getUID' ) -> willReturn ( 'user' );
$userSession = $this -> getMockBuilder ( '\OCP\IUserSession' )
-> disableOriginalConstructor ()
-> getMock ();
$userSession -> method ( 'getUser' ) -> willReturn ( $user );
2016-02-05 17:32:34 +03:00
$ex = $this -> getMockBuilder ( '\OC\DB\QueryBuilder\ExpressionBuilder\ExpressionBuilder' )
2015-03-13 14:29:13 +03:00
-> disableOriginalConstructor ()
-> getMock ();
2015-07-07 18:30:26 +03:00
$qb = $this -> getMockBuilder ( '\OC\DB\QueryBuilder\QueryBuilder' )
2015-03-13 14:29:13 +03:00
-> disableOriginalConstructor ()
-> getMock ();
$qb -> method ( 'update' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'set' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'where' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'andWhere' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'select' ) -> will ( $this -> returnSelf ());
$qb -> method ( 'from' ) -> will ( $this -> returnSelf ());
2015-03-30 22:18:24 +03:00
$qb -> method ( 'setParameter' ) -> will ( $this -> returnSelf ());
2015-03-13 14:29:13 +03:00
$qb -> method ( 'expr' ) -> willReturn ( $ex );
$ret = $this -> getMockBuilder ( '\Doctrine\DBAL\Driver\ResultStatement' )
-> disableOriginalConstructor ()
-> getMock ();
$ret -> method ( 'fetch' ) -> willReturn ([ 'uid_owner' => 'user2' ]);
$qb -> method ( 'execute' ) -> willReturn ( $ret );
$connection = $this -> getMockBuilder ( '\OC\DB\Connection' )
-> disableOriginalConstructor ()
-> getMock ();
2015-07-06 13:34:19 +03:00
$connection -> method ( 'getQueryBuilder' ) -> willReturn ( $qb );
2015-03-13 14:29:13 +03:00
$config = $this -> getMockBuilder ( '\OCP\IConfig' )
-> disableOriginalConstructor ()
-> getMock ();
\OC\Share\Share :: setPassword ( $userSession , $connection , $config , 1 , 'pass' );
}
2015-07-15 13:49:26 +03:00
/**
* Make sure that a user cannot have multiple identical shares to remote users
*/
public function testOnlyOneRemoteShare () {
$oldHttpHelper = \OC :: $server -> query ( 'HTTPHelper' );
$httpHelperMock = $this -> getMockBuilder ( 'OC\HttpHelper' )
-> disableOriginalConstructor ()
-> getMock ();
$this -> setHttpHelper ( $httpHelperMock );
$httpHelperMock -> expects ( $this -> at ( 0 ))
-> method ( 'post' )
-> with ( $this -> stringStartsWith ( 'https://localhost/ocs/v1.php/cloud/shares' ), $this -> anything ())
-> willReturn ([ 'success' => true , 'result' => json_encode ([ 'ocs' => [ 'meta' => [ 'statuscode' => 100 ]]])]);
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_REMOTE , 'foo@localhost' , \OCP\Constants :: PERMISSION_READ );
$shares = \OCP\Share :: getItemShared ( 'test' , 'test.txt' );
$share = array_shift ( $shares );
//Try share again
try {
\OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_REMOTE , 'foo@localhost' , \OCP\Constants :: PERMISSION_READ );
$this -> fail ( 'Identical remote shares are not allowed' );
} catch ( \Exception $e ) {
$this -> assertEquals ( 'Sharing test.txt failed, because this item is already shared with foo@localhost' , $e -> getMessage ());
}
$httpHelperMock -> expects ( $this -> at ( 0 ))
-> method ( 'post' )
-> with ( $this -> stringStartsWith ( 'https://localhost/ocs/v1.php/cloud/shares/' . $share [ 'id' ] . '/unshare' ), $this -> anything ())
-> willReturn ([ 'success' => true , 'result' => json_encode ([ 'ocs' => [ 'meta' => [ 'statuscode' => 100 ]]])]);
\OCP\Share :: unshare ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_REMOTE , 'foo@localhost' );
$this -> setHttpHelper ( $oldHttpHelper );
}
2015-09-23 11:27:35 +03:00
/**
* Test case for #19119
*/
public function testReshareWithLinkDefaultExpirationDate () {
$config = \OC :: $server -> getConfig ();
$config -> setAppValue ( 'core' , 'shareapi_default_expire_date' , 'yes' );
$config -> setAppValue ( 'core' , 'shareapi_expire_after_n_days' , '2' );
// Expiration date
$expireAt = time () + 2 * 24 * 60 * 60 ;
2016-05-19 10:15:10 +03:00
$date = new \DateTime ();
2015-09-23 11:27:35 +03:00
$date -> setTimestamp ( $expireAt );
$date -> setTime ( 0 , 0 , 0 );
//Share a file from user 1 to user 2
$this -> shareUserTestFileWithUser ( $this -> user1 , $this -> user2 );
//User 2 shares as link
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
$result = \OCP\Share :: shareItem ( 'test' , 'test.txt' , \OCP\Share :: SHARE_TYPE_LINK , null , \OCP\Constants :: PERMISSION_READ );
2015-09-23 11:27:35 +03:00
$this -> assertTrue ( is_string ( $result ));
//Check if expire date is correct
2016-05-19 10:15:10 +03:00
$result = \OCP\Share :: getItemShared ( 'test' , 'test.txt' );
2015-09-23 11:27:35 +03:00
$this -> assertCount ( 1 , $result );
$result = reset ( $result );
$this -> assertNotEmpty ( $result [ 'expiration' ]);
2016-05-19 10:15:10 +03:00
$expireDate = new \DateTime ( $result [ 'expiration' ]);
2015-09-23 11:27:35 +03:00
$this -> assertEquals ( $date , $expireDate );
//Unshare
2016-05-19 10:15:10 +03:00
$this -> assertTrue ( \OCP\Share :: unshareAll ( 'test' , 'test.txt' ));
2015-09-23 11:27:35 +03:00
//Reset config
$config -> deleteAppValue ( 'core' , 'shareapi_default_expire_date' );
$config -> deleteAppValue ( 'core' , 'shareapi_expire_after_n_days' );
}
2015-10-13 11:05:49 +03:00
/**
* Test case for #17560
*/
public function testAccesToSharedSubFolder () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2015-10-13 11:05:49 +03:00
$view = new \OC\Files\View ( '/' . $this -> user1 . '/' );
$view -> mkdir ( 'files/folder1' );
$fileInfo = $view -> getFileInfo ( 'files/folder1' );
$this -> assertInstanceOf ( '\OC\Files\FileInfo' , $fileInfo );
$fileId = $fileInfo -> getId ();
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'folder' , $fileId , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_ALL ),
2015-10-13 11:05:49 +03:00
'Failed asserting that user 1 successfully shared "test" with user 2.'
);
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'folder' , $fileId , \OCP\Share :: SHARE_TYPE_USER , $this -> user3 , \OCP\Constants :: PERMISSION_ALL ),
2015-10-13 11:05:49 +03:00
'Failed asserting that user 1 successfully shared "test" with user 3.'
);
$view -> mkdir ( 'files/folder1/folder2' );
$fileInfo = $view -> getFileInfo ( 'files/folder1/folder2' );
$this -> assertInstanceOf ( '\OC\Files\FileInfo' , $fileInfo );
$fileId = $fileInfo -> getId ();
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'folder' , $fileId , \OCP\Share :: SHARE_TYPE_USER , $this -> user4 , \OCP\Constants :: PERMISSION_ALL ),
2015-10-13 11:05:49 +03:00
'Failed asserting that user 1 successfully shared "test" with user 4.'
);
2016-05-19 10:15:10 +03:00
$res = \OCP\Share :: getItemShared (
2015-10-13 11:05:49 +03:00
'folder' ,
$fileId ,
2016-05-19 10:15:10 +03:00
\OCP\Share :: FORMAT_NONE ,
2015-10-13 11:05:49 +03:00
null ,
true
);
$this -> assertCount ( 3 , $res );
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'folder' , $fileId , \OCP\Share :: SHARE_TYPE_USER , $this -> user5 , \OCP\Constants :: PERMISSION_ALL ),
2015-10-13 11:05:49 +03:00
'Failed asserting that user 1 successfully shared "test" with user 5.'
);
2016-05-19 10:15:10 +03:00
$res = \OCP\Share :: getItemShared (
2015-10-13 11:05:49 +03:00
'folder' ,
$fileId ,
2016-05-19 10:15:10 +03:00
\OCP\Share :: FORMAT_NONE ,
2015-10-13 11:05:49 +03:00
null ,
true
);
$this -> assertCount ( 4 , $res );
}
2015-10-16 10:27:02 +03:00
2015-10-16 11:00:33 +03:00
public function testShareWithSelfError () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2015-10-16 11:00:33 +03:00
$view = new \OC\Files\View ( '/' . $this -> user1 . '/' );
$view -> mkdir ( 'files/folder1' );
$fileInfo = $view -> getFileInfo ( 'files/folder1' );
$this -> assertInstanceOf ( '\OC\Files\FileInfo' , $fileInfo );
$fileId = $fileInfo -> getId ();
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'folder' , $fileId , \OCP\Share :: SHARE_TYPE_USER , $this -> user1 , \OCP\Constants :: PERMISSION_ALL );
2015-10-16 11:00:33 +03:00
$this -> fail ();
} catch ( \Exception $e ) {
$this -> assertEquals ( 'Sharing /folder1 failed, because you can not share with yourself' , $e -> getMessage ());
}
}
2015-10-16 10:27:02 +03:00
public function testShareWithOwnerError () {
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user1 );
2015-10-16 10:27:02 +03:00
$view = new \OC\Files\View ( '/' . $this -> user1 . '/' );
$view -> mkdir ( 'files/folder1' );
$fileInfo = $view -> getFileInfo ( 'files/folder1' );
$this -> assertInstanceOf ( '\OC\Files\FileInfo' , $fileInfo );
$fileId = $fileInfo -> getId ();
$this -> assertTrue (
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'folder' , $fileId , \OCP\Share :: SHARE_TYPE_USER , $this -> user2 , \OCP\Constants :: PERMISSION_ALL ),
2015-10-16 10:27:02 +03:00
'Failed asserting that user 1 successfully shared "test" with user 2.'
);
2016-05-19 10:15:10 +03:00
\OC_User :: setUserId ( $this -> user2 );
2015-10-16 10:27:02 +03:00
try {
2016-05-19 10:15:10 +03:00
\OCP\Share :: shareItem ( 'folder' , $fileId , \OCP\Share :: SHARE_TYPE_USER , $this -> user1 , \OCP\Constants :: PERMISSION_ALL );
2015-10-16 10:27:02 +03:00
$this -> fail ();
} catch ( \Exception $e ) {
2015-10-16 11:00:33 +03:00
$this -> assertEquals ( 'Sharing failed, because the user ' . $this -> user1 . ' is the original sharer' , $e -> getMessage ());
2015-10-16 10:27:02 +03:00
}
}
2014-08-01 18:24:19 +04:00
}
class DummyShareClass extends \OC\Share\Share {
public static function groupItemsTest ( $items ) {
return parent :: groupItems ( $items , 'test' );
2014-01-21 15:07:08 +04:00
}
2012-07-26 01:08:18 +04:00
}
2015-02-09 15:12:34 +03:00
class DummyHookListener {
static $shareType = null ;
public static function listen ( $params ) {
self :: $shareType = $params [ 'shareType' ];
}
}