From 0ba0596341779100db37e25e59ac34f5a57bd2c3 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 14 Feb 2014 14:25:45 +0100 Subject: [PATCH 1/2] remove duplicate call to groupExists --- lib/private/group/manager.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/private/group/manager.php b/lib/private/group/manager.php index bf469d51d1..29453c5da1 100644 --- a/lib/private/group/manager.php +++ b/lib/private/group/manager.php @@ -76,12 +76,7 @@ class Manager extends PublicEmitter { if (isset($this->cachedGroups[$gid])) { return $this->cachedGroups[$gid]; } - foreach ($this->backends as $backend) { - if ($backend->groupExists($gid)) { - return $this->getGroupObject($gid); - } - } - return null; + return $this->getGroupObject($gid); } protected function getGroupObject($gid) { @@ -91,6 +86,9 @@ class Manager extends PublicEmitter { $backends[] = $backend; } } + if (count($backends) === 0) { + return null; + } $this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this); return $this->cachedGroups[$gid]; } From b35f679483f2b8f1dab56b903ca2e942ac4606ff Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 14 Feb 2014 15:07:08 +0100 Subject: [PATCH 2/2] Fix test cases for group manager --- lib/private/group/manager.php | 4 ++-- tests/lib/group/manager.php | 37 +++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/lib/private/group/manager.php b/lib/private/group/manager.php index 29453c5da1..9b433b64fd 100644 --- a/lib/private/group/manager.php +++ b/lib/private/group/manager.php @@ -108,8 +108,8 @@ class Manager extends PublicEmitter { public function createGroup($gid) { if (!$gid) { return false; - } else if ($this->groupExists($gid)) { - return $this->get($gid); + } else if ($group = $this->get($gid)) { + return $group; } else { $this->emit('\OC\Group', 'preCreate', array($gid)); foreach ($this->backends as $backend) { diff --git a/tests/lib/group/manager.php b/tests/lib/group/manager.php index 9d3adf51a0..90f0e1b35e 100644 --- a/tests/lib/group/manager.php +++ b/tests/lib/group/manager.php @@ -116,16 +116,22 @@ class Manager extends \PHPUnit_Framework_TestCase { /** * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend */ + $backendGroupCreated = false; $backend = $this->getMock('\OC_Group_Database'); $backend->expects($this->any()) ->method('groupExists') ->with('group1') - ->will($this->returnValue(false)); + ->will($this->returnCallback(function () use (&$backendGroupCreated) { + return $backendGroupCreated; + })); $backend->expects($this->once()) ->method('implementsActions') ->will($this->returnValue(true)); $backend->expects($this->once()) - ->method('createGroup'); + ->method('createGroup') + ->will($this->returnCallback(function () use (&$backendGroupCreated) { + $backendGroupCreated = true; + }));; /** * @var \OC\User\Manager $userManager @@ -170,6 +176,10 @@ class Manager extends \PHPUnit_Framework_TestCase { ->method('getGroups') ->with('1') ->will($this->returnValue(array('group1'))); + $backend->expects($this->once()) + ->method('groupExists') + ->with('group1') + ->will($this->returnValue(true)); /** * @var \OC\User\Manager $userManager @@ -193,6 +203,9 @@ class Manager extends \PHPUnit_Framework_TestCase { ->method('getGroups') ->with('1') ->will($this->returnValue(array('group1'))); + $backend1->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); /** * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2 @@ -202,6 +215,9 @@ class Manager extends \PHPUnit_Framework_TestCase { ->method('getGroups') ->with('1') ->will($this->returnValue(array('group12', 'group1'))); + $backend2->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); /** * @var \OC\User\Manager $userManager @@ -228,6 +244,9 @@ class Manager extends \PHPUnit_Framework_TestCase { ->method('getGroups') ->with('1', 2, 1) ->will($this->returnValue(array('group1'))); + $backend1->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); /** * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2 @@ -237,6 +256,9 @@ class Manager extends \PHPUnit_Framework_TestCase { ->method('getGroups') ->with('1', 1, 0) ->will($this->returnValue(array('group12'))); + $backend2->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); /** * @var \OC\User\Manager $userManager @@ -263,6 +285,10 @@ class Manager extends \PHPUnit_Framework_TestCase { ->method('getUserGroups') ->with('user1') ->will($this->returnValue(array('group1'))); + $backend->expects($this->any()) + ->method('groupExists') + ->with('group1') + ->will($this->returnValue(true)); /** * @var \OC\User\Manager $userManager @@ -286,6 +312,10 @@ class Manager extends \PHPUnit_Framework_TestCase { ->method('getUserGroups') ->with('user1') ->will($this->returnValue(array('group1'))); + $backend1->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); + /** * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2 */ @@ -294,6 +324,9 @@ class Manager extends \PHPUnit_Framework_TestCase { ->method('getUserGroups') ->with('user1') ->will($this->returnValue(array('group1', 'group2'))); + $backend1->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); /** * @var \OC\User\Manager $userManager