Use mocks when testing isSharingDisabledForUser

This commit is contained in:
Thomas Müller 2015-11-19 15:35:58 +01:00
parent 58eaeb267c
commit 9ec2850c78
6 changed files with 56 additions and 37 deletions

View File

@ -137,7 +137,7 @@ abstract class Common implements Storage {
} }
public function isSharable($path) { public function isSharable($path) {
if (\OC_Util::isSharingDisabledForUser()) { if (\OCP\Util::isSharingDisabledForUser()) {
return false; return false;
} }

View File

@ -214,6 +214,9 @@ class Manager extends PublicEmitter implements IGroupManager {
* @return \OC\Group\Group[] * @return \OC\Group\Group[]
*/ */
public function getUserGroups($user) { public function getUserGroups($user) {
if (is_null($user)) {
return false;
}
return $this->getUserIdGroups($user->getUID()); return $this->getUserIdGroups($user->getUID());
} }

View File

@ -1811,7 +1811,7 @@ class Share extends Constants {
} }
} }
// Check if resharing is allowed, if not remove share permission // Check if resharing is allowed, if not remove share permission
if (isset($row['permissions']) && (!self::isResharingAllowed() | \OC_Util::isSharingDisabledForUser())) { if (isset($row['permissions']) && (!self::isResharingAllowed() | \OCP\Util::isSharingDisabledForUser())) {
$row['permissions'] &= ~\OCP\Constants::PERMISSION_SHARE; $row['permissions'] &= ~\OCP\Constants::PERMISSION_SHARE;
} }
// Add display names to result // Add display names to result

View File

@ -54,6 +54,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/> * along with this program. If not, see <http://www.gnu.org/licenses/>
* *
*/ */
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUser;
class OC_Util { class OC_Util {
public static $scripts = array(); public static $scripts = array();
public static $styles = array(); public static $styles = array();
@ -218,20 +223,21 @@ class OC_Util {
/** /**
* check if sharing is disabled for the current user * check if sharing is disabled for the current user
* * @param IConfig $config
* @return boolean * @param IGroupManager $groupManager
* @param IUser|null $user
* @return bool
*/ */
public static function isSharingDisabledForUser() { public static function isSharingDisabledForUser(IConfig $config, IGroupManager $groupManager, $user) {
if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { if ($config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {
$user = \OCP\User::getUser(); $groupsList = $config->getAppValue('core', 'shareapi_exclude_groups_list', '');
$groupsList = \OC::$server->getAppConfig()->getValue('core', 'shareapi_exclude_groups_list', '');
$excludedGroups = json_decode($groupsList); $excludedGroups = json_decode($groupsList);
if (is_null($excludedGroups)) { if (is_null($excludedGroups)) {
$excludedGroups = explode(',', $groupsList); $excludedGroups = explode(',', $groupsList);
$newValue = json_encode($excludedGroups); $newValue = json_encode($excludedGroups);
\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups_list', $newValue); $config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue);
} }
$usersGroups = \OC_Group::getUserGroups($user); $usersGroups = $groupManager->getUserGroups($user);
if (!empty($usersGroups)) { if (!empty($usersGroups)) {
$remainingGroups = array_diff($usersGroups, $excludedGroups); $remainingGroups = array_diff($usersGroups, $excludedGroups);
// if the user is only in groups which are disabled for sharing then // if the user is only in groups which are disabled for sharing then

View File

@ -173,7 +173,11 @@ class Util {
* @since 7.0.0 * @since 7.0.0
*/ */
public static function isSharingDisabledForUser() { public static function isSharingDisabledForUser() {
return \OC_Util::isSharingDisabledForUser(); return \OC_Util::isSharingDisabledForUser(
\OC::$server->getConfig(),
\OC::$server->getGroupManager(),
\OC::$server->getUserSession()->getUser()
);
} }
/** /**

View File

@ -289,38 +289,44 @@ class Test_Util extends \Test\TestCase {
* @param bool $expected expected result * @param bool $expected expected result
*/ */
function testIsSharingDisabledForUser($groups, $membership, $excludedGroups, $expected) { function testIsSharingDisabledForUser($groups, $membership, $excludedGroups, $expected) {
$uid = "user1"; $config = $this->getMockBuilder('OCP\IConfig')->disableOriginalConstructor()->getMock();
\OC_User::setUserId($uid); $groupManager = $this->getMockBuilder('OCP\IGroupManager')->disableOriginalConstructor()->getMock();
$user = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock();
\OC_User::createUser($uid, "passwd"); $config
->expects($this->at(0))
->method('getAppValue')
->with('core', 'shareapi_exclude_groups', 'no')
->will($this->returnValue('yes'));
$config
->expects($this->at(1))
->method('getAppValue')
->with('core', 'shareapi_exclude_groups_list')
->will($this->returnValue(json_encode($excludedGroups)));
foreach ($groups as $group) { $groupManager
\OC_Group::createGroup($group); ->expects($this->at(0))
} ->method('getUserGroups')
->with($user)
->will($this->returnValue($membership));
foreach ($membership as $group) { // $uid = "user1";
\OC_Group::addToGroup($uid, $group); // \OC_User::setUserId($uid);
} //
// \OC_User::createUser($uid, "passwd");
//
// foreach ($groups as $group) {
// \OC_Group::createGroup($group);
// }
//
// foreach ($membership as $group) {
// \OC_Group::addToGroup($uid, $group);
// }
//
$appConfig = \OC::$server->getAppConfig(); $result = \OC_Util::isSharingDisabledForUser($config, $groupManager, $user);
$appConfig->setValue('core', 'shareapi_exclude_groups_list', json_encode($excludedGroups));
$appConfig->setValue('core', 'shareapi_exclude_groups', 'yes');
$result = \OCP\Util::isSharingDisabledForUser();
$this->assertSame($expected, $result); $this->assertSame($expected, $result);
// cleanup
\OC_User::deleteUser($uid);
\OC_User::setUserId('');
foreach ($groups as $group) {
\OC_Group::deleteGroup($group);
}
$appConfig->setValue('core', 'shareapi_exclude_groups_list', '');
$appConfig->setValue('core', 'shareapi_exclude_groups', 'no');
} }
public function dataProviderForTestIsSharingDisabledForUser() { public function dataProviderForTestIsSharingDisabledForUser() {