diff options
author | Joas Schilling <nickvergessen@gmx.de> | 2016-05-20 15:38:20 +0200 |
---|---|---|
committer | Thomas Müller <DeepDiver1975@users.noreply.github.com> | 2016-05-20 15:38:20 +0200 |
commit | 94ad54ec9b96d41a614fbbad4a97b34c41a6901f (patch) | |
tree | f3eb7cdda2704aaf0cd59d58efe66bcbd34cb67d /tests/lib/Util | |
parent | 2ef751b1ec28f7b5c7113af60ec8c9fa0ae1cf87 (diff) | |
download | nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.tar.gz nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.zip |
Move tests/ to PSR-4 (#24731)
* Move a-b to PSR-4
* Move c-d to PSR-4
* Move e+g to PSR-4
* Move h-l to PSR-4
* Move m-r to PSR-4
* Move s-u to PSR-4
* Move files/ to PSR-4
* Move remaining tests to PSR-4
* Remove Test\ from old autoloader
Diffstat (limited to 'tests/lib/Util')
-rw-r--r-- | tests/lib/Util/Group/Dummy.php | 217 | ||||
-rw-r--r-- | tests/lib/Util/User/Dummy.php | 173 |
2 files changed, 390 insertions, 0 deletions
diff --git a/tests/lib/Util/Group/Dummy.php b/tests/lib/Util/Group/Dummy.php new file mode 100644 index 00000000000..8f06316ca03 --- /dev/null +++ b/tests/lib/Util/Group/Dummy.php @@ -0,0 +1,217 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Felix Moeller <mail@felixmoeller.de> + * @author Lukas Reschke <lukas@owncloud.com> + * @author Michael Gapczynski <GapczynskiM@gmail.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Robin Appelman <icewind@owncloud.com> + * @author Robin McCorkell <robin@mccorkell.me.uk> + * @author Roeland Jago Douma <rullzer@owncloud.com> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Test\Util\Group; + +use OC\Group\Backend; + +/** + * dummy group backend, does not keep state, only for testing use + */ +class Dummy extends Backend { + private $groups=array(); + /** + * Try to create a new group + * @param string $gid The name of the group to create + * @return bool + * + * Tries to create a new group. If the group name already exists, false will + * be returned. + */ + public function createGroup($gid) { + if(!isset($this->groups[$gid])) { + $this->groups[$gid]=array(); + return true; + }else{ + return false; + } + } + + /** + * delete a group + * @param string $gid gid of the group to delete + * @return bool + * + * Deletes a group and removes it from the group_user-table + */ + public function deleteGroup($gid) { + if(isset($this->groups[$gid])) { + unset($this->groups[$gid]); + return true; + }else{ + return false; + } + } + + /** + * is user in group? + * @param string $uid uid of the user + * @param string $gid gid of the group + * @return bool + * + * Checks whether the user is member of a group or not. + */ + public function inGroup($uid, $gid) { + if(isset($this->groups[$gid])) { + return (array_search($uid, $this->groups[$gid])!==false); + }else{ + return false; + } + } + + /** + * Add a user to a group + * @param string $uid Name of the user to add to group + * @param string $gid Name of the group in which add the user + * @return bool + * + * Adds a user to a group. + */ + public function addToGroup($uid, $gid) { + if(isset($this->groups[$gid])) { + if(array_search($uid, $this->groups[$gid])===false) { + $this->groups[$gid][]=$uid; + return true; + }else{ + return false; + } + }else{ + return false; + } + } + + /** + * Removes a user from a group + * @param string $uid Name of the user to remove from group + * @param string $gid Name of the group from which remove the user + * @return bool + * + * removes the user from a group. + */ + public function removeFromGroup($uid, $gid) { + if(isset($this->groups[$gid])) { + if(($index=array_search($uid, $this->groups[$gid]))!==false) { + unset($this->groups[$gid][$index]); + return true; + }else{ + return false; + } + }else{ + return false; + } + } + + /** + * Get all groups a user belongs to + * @param string $uid Name of the user + * @return array an array of group names + * + * This function fetches all groups a user belongs to. It does not check + * if the user exists at all. + */ + public function getUserGroups($uid) { + $groups=array(); + $allGroups=array_keys($this->groups); + foreach($allGroups as $group) { + if($this->inGroup($uid, $group)) { + $groups[]=$group; + } + } + return $groups; + } + + /** + * Get a list of all groups + * @param string $search + * @param int $limit + * @param int $offset + * @return array an array of group names + */ + public function getGroups($search = '', $limit = -1, $offset = 0) { + 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; + } + + /** + * Get a list of all users in a group + * @param string $gid + * @param string $search + * @param int $limit + * @param int $offset + * @return array an array of user IDs + */ + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { + if(isset($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(); + } + } + + /** + * get the number of all users in a group + * @param string $gid + * @param string $search + * @param int $limit + * @param int $offset + * @return int + */ + public function countUsersInGroup($gid, $search = '', $limit = -1, $offset = 0) { + if(isset($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; + } + } + +} diff --git a/tests/lib/Util/User/Dummy.php b/tests/lib/Util/User/Dummy.php new file mode 100644 index 00000000000..ea47f5d7d15 --- /dev/null +++ b/tests/lib/Util/User/Dummy.php @@ -0,0 +1,173 @@ +<?php +/** + * @author Andreas Fischer <bantu@owncloud.com> + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Jörn Friedrich Dreyer <jfd@butonic.de> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Robin Appelman <icewind@owncloud.com> + * @author Robin McCorkell <rmccorkell@karoshi.org.uk> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Test\Util\User; + +use \OC\User\Backend; + +/** + * dummy user backend, does not keep state, only for testing use + */ +class Dummy extends Backend implements \OCP\IUserBackend { + private $users = array(); + private $displayNames = array(); + + /** + * Create a new user + * + * @param string $uid The username of the user to create + * @param string $password The password of the new user + * @return bool + * + * Creates a new user. Basic checking of username is done in OC_User + * itself, not in its subclasses. + */ + public function createUser($uid, $password) { + if (isset($this->users[$uid])) { + return false; + } else { + $this->users[$uid] = $password; + return true; + } + } + + /** + * delete a user + * + * @param string $uid The username of the user to delete + * @return bool + * + * Deletes a user + */ + public function deleteUser($uid) { + if (isset($this->users[$uid])) { + unset($this->users[$uid]); + return true; + } else { + return false; + } + } + + /** + * Set password + * + * @param string $uid The username + * @param string $password The new password + * @return bool + * + * Change the password of a user + */ + public function setPassword($uid, $password) { + if (isset($this->users[$uid])) { + $this->users[$uid] = $password; + return true; + } else { + return false; + } + } + + /** + * Check if the password is correct + * + * @param string $uid The username + * @param string $password The password + * @return string + * + * Check if the password is correct without logging in the user + * returns the user id or false + */ + public function checkPassword($uid, $password) { + if (isset($this->users[$uid]) && $this->users[$uid] === $password) { + return $uid; + } else { + return false; + } + } + + /** + * Get a list of all users + * + * @param string $search + * @param null|int $limit + * @param null|int $offset + * @return string[] an array of all uids + */ + public function getUsers($search = '', $limit = null, $offset = null) { + 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; + } + + /** + * check if a user exists + * + * @param string $uid the username + * @return boolean + */ + public function userExists($uid) { + return isset($this->users[$uid]); + } + + /** + * @return bool + */ + public function hasUserListings() { + return true; + } + + /** + * counts the users in the database + * + * @return int|bool + */ + public function countUsers() { + return 0; + } + + public function setDisplayName($uid, $displayName) { + $this->displayNames[$uid] = $displayName; + } + + public function getDisplayName($uid) { + return isset($this->displayNames[$uid])? $this->displayNames[$uid]: $uid; + } + + /** + * Backend name to be shown in user management + * @return string the name of the backend to be shown + */ + public function getBackendName(){ + return 'Dummy'; + } +} |