Merge pull request #9156 from owncloud/solve-8959

search term for users and groups may occur anywhere in the name or displ...
This commit is contained in:
blizzz 2014-07-01 14:37:09 +02:00
commit 616f9b1e03
8 changed files with 115 additions and 15 deletions

View File

@ -169,7 +169,7 @@ class OC_Group_Database extends OC_Group_Backend {
*/
public function getGroups($search = '', $limit = null, $offset = null) {
$stmt = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` LIKE ?', $limit, $offset);
$result = $stmt->execute(array($search.'%'));
$result = $stmt->execute(array('%' . $search . '%'));
$groups = array();
while ($row = $result->fetchRow()) {
$groups[] = $row['gid'];
@ -203,7 +203,7 @@ class OC_Group_Database extends OC_Group_Backend {
$stmt = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` LIKE ?',
$limit,
$offset);
$result = $stmt->execute(array($gid, $search.'%'));
$result = $stmt->execute(array($gid, '%'.$search.'%'));
$users = array();
while ($row = $result->fetchRow()) {
$users[] = $row['uid'];
@ -220,8 +220,12 @@ class OC_Group_Database extends OC_Group_Backend {
*/
public function countUsersInGroup($gid, $search = '') {
$stmt = OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` LIKE ?');
$result = $stmt->execute(array($gid, $search.'%'));
return $result->fetchOne();
$result = $stmt->execute(array($gid, '%' . $search . '%'));
$count = $result->fetchOne();
if($count !== false) {
$count = intval($count);
}
return $count;
}
}

View File

@ -143,7 +143,16 @@ class OC_Group_Dummy extends OC_Group_Backend {
* @return array an array of group names
*/
public function getGroups($search = '', $limit = -1, $offset = 0) {
return array_keys($this->groups);
if(empty($search)) {
return array_keys($this->groups);
}
$result = array();
foreach(array_keys($this->groups) as $group) {
if(stripos($group, $search) !== false) {
$result[] = $group;
}
}
return $result;
}
/**
@ -156,7 +165,16 @@ class OC_Group_Dummy extends OC_Group_Backend {
*/
public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
if(isset($this->groups[$gid])) {
return $this->groups[$gid];
if(empty($search)) {
return $this->groups[$gid];
}
$result = array();
foreach($this->groups[$gid] as $user) {
if(stripos($user, $search) !== false) {
$result[] = $user;
}
}
return $result;
}else{
return array();
}
@ -172,7 +190,16 @@ class OC_Group_Dummy extends OC_Group_Backend {
*/
public function countUsersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
if(isset($this->groups[$gid])) {
return count($this->groups[$gid]);
if(empty($search)) {
return count($this->groups[$gid]);
}
$count = 0;
foreach($this->groups[$gid] as $user) {
if(stripos($user, $search) !== false) {
$count++;
}
}
return $count;
}
}

View File

@ -158,7 +158,7 @@ class OC_User_Database extends OC_User_Backend {
$query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users`'
. ' WHERE LOWER(`displayname`) LIKE LOWER(?) OR '
. 'LOWER(`uid`) LIKE LOWER(?)', $limit, $offset);
$result = $query->execute(array($search . '%', $search . '%'));
$result = $query->execute(array('%' . $search . '%', '%' . $search . '%'));
$users = array();
while ($row = $result->fetchRow()) {
$displayNames[$row['uid']] = $row['displayname'];
@ -232,7 +232,7 @@ class OC_User_Database extends OC_User_Backend {
*/
public function getUsers($search = '', $limit = null, $offset = null) {
$query = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*users` WHERE LOWER(`uid`) LIKE LOWER(?)', $limit, $offset);
$result = $query->execute(array($search . '%'));
$result = $query->execute(array('%' . $search . '%'));
$users = array();
while ($row = $result->fetchRow()) {
$users[] = $row['uid'];

View File

@ -105,7 +105,16 @@ class OC_User_Dummy extends OC_User_Backend {
* Get a list of all users.
*/
public function getUsers($search = '', $limit = null, $offset = null) {
return array_keys($this->users);
if(empty($search)) {
return array_keys($this->users);
}
$result = array();
foreach(array_keys($this->users) as $user) {
if(stripos($user, $search) !== false) {
$result[] = $user;
}
}
return $result;
}
/**

View File

@ -31,8 +31,12 @@ abstract class Test_Group_Backend extends PHPUnit_Framework_TestCase {
* test cases can override this in order to clean up created groups
* @return string
*/
public function getGroupName() {
return uniqid('test_');
public function getGroupName($name = null) {
if(is_null($name)) {
return uniqid('test_');
} else {
return $name;
}
}
/**
@ -88,7 +92,7 @@ abstract class Test_Group_Backend extends PHPUnit_Framework_TestCase {
$this->assertFalse($this->backend->inGroup($user2, $group1));
$this->assertFalse($this->backend->inGroup($user1, $group2));
$this->assertFalse($this->backend->inGroup($user2, $group2));
$this->assertFalse($this->backend->addToGroup($user1, $group1));
$this->assertEquals(array($user1), $this->backend->usersInGroup($group1));
@ -102,4 +106,38 @@ abstract class Test_Group_Backend extends PHPUnit_Framework_TestCase {
$this->assertEquals(array(), $this->backend->usersInGroup($group1));
$this->assertFalse($this->backend->inGroup($user1, $group1));
}
public function testSearchGroups() {
$name1 = $this->getGroupName('foobarbaz');
$name2 = $this->getGroupName('bazbarfoo');
$name3 = $this->getGroupName('notme');
$this->backend->createGroup($name1);
$this->backend->createGroup($name2);
$this->backend->createGroup($name3);
$result = $this->backend->getGroups('bar');
$this->assertSame(2, count($result));
}
public function testSearchUsers() {
$group = $this->getGroupName();
$this->backend->createGroup($group);
$name1 = 'foobarbaz';
$name2 = 'bazbarfoo';
$name3 = 'notme';
$this->backend->addToGroup($name1, $group);
$this->backend->addToGroup($name2, $group);
$this->backend->addToGroup($name3, $group);
$result = $this->backend->usersInGroup($group, 'bar');
$this->assertSame(2, count($result));
$result = $this->backend->countUsersInGroup($group, 'bar');
$this->assertSame(2, $result);
}
}

View File

@ -28,8 +28,10 @@ class Test_Group_Database extends Test_Group_Backend {
* test cases can override this in order to clean up created groups
* @return string
*/
public function getGroupName() {
$name=uniqid('test_');
public function getGroupName($name = null) {
if(is_null($name)) {
$name=uniqid('test_');
}
$this->groups[]=$name;
return $name;
}

View File

@ -96,4 +96,21 @@ abstract class Test_User_Backend extends PHPUnit_Framework_TestCase {
$this->assertSame($name1, $this->backend->checkPassword($name1, 'newpass1'));
$this->assertFalse($this->backend->checkPassword($name2, 'newpass1'));
}
public function testSearch() {
$name1 = 'foobarbaz';
$name2 = 'bazbarfoo';
$name3 = 'notme';
$this->backend->createUser($name1, 'pass1');
$this->backend->createUser($name2, 'pass2');
$this->backend->createUser($name3, 'pass3');
$result = $this->backend->getUsers('bar');
$this->assertSame(2, count($result));
$result = $this->backend->getDisplayNames('bar');
$this->assertSame(2, count($result));
}
}

View File

@ -32,6 +32,9 @@ class Test_User_Database extends Test_User_Backend {
}
public function tearDown() {
if(!isset($this->users)) {
return;
}
foreach($this->users as $user) {
$this->backend->deleteUser($user);
}