User 改良 & GroupAdmin建立

This commit is contained in:
Yuan Chiu 2014-10-03 02:45:27 +08:00
parent c77746bbf6
commit f724d77820
5 changed files with 340 additions and 4 deletions

View File

@ -230,7 +230,8 @@ class User {
* @since 2.0.0 * @since 2.0.0
*/ */
public function isEnable(){ public function isEnable(){
return $this->queryResultArray['enable']; if($this->queryResultArray['enable'] == 0) return false;
else return true;
} }
/** /**
@ -379,7 +380,7 @@ class User {
*/ */
public function setEmail($input){ public function setEmail($input){
// 將新設定寫進資料庫裡 // 將新設定寫進資料庫裡
$db->changeUserData($this->uId, 'email', $input); $this->setUpdate('email', $input);
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------

View File

@ -4,4 +4,156 @@
*/ */
namespace UElearning\User; namespace UElearning\User;
// TODO: write this code
/**
* 管理使用者權限群組的操作
*
* @author Yuan Chiu <chyuaner@gmail.com>
* @version 2.0.0
* @package UElearning
* @subpackage User
*/
class UserGroupAdmin {
/**
* 建立群組
*
* 建立使用者範例:
*
* try {
* $userAdmin = new User\UserAdmin();
* $userAdmin->create(
* array( 'group_id' => 'student',
* 'name' => '學生',
* 'memo' => null,
* 'auth_server_admin' => false,
* 'auth_client_admin' => false
* ));
*
* }
* // 若已有重複帳號名稱
* catch (User\Exception\GroupIdExistException $e) {
* echo 'Is exist group: ', $e->getGroupId();
* }
*
* @param array $groupArray 使用者資訊陣列,格式為:
* array( 'group_id' => 'student',
* 'name' => '學生',
* 'memo' => null, // (optional) 預設為null
* 'auth_server_admin' => false, // (optional) 預設為false
* 'auth_client_admin' => false ) // (optional) 預設為false
* @throw UElearning\User\Exception\GroupIdExistException
* @since 2.0.0
*/
public function create($groupArray) {
/*// 檢查必填項目有無填寫
if(isset($userInfoArray)) {
// 若無填寫
if( !isset($userInfoArray['user_id']) ||
!isset($userInfoArray['password']) ||
!isset($userInfoArray['group_id']) ) {
throw new UElearning\Exception\NoDataException();
}
// 若此id已存在
else if($this->isExist($userInfoArray['user_id'])) {
throw new Exception\UserIdExistException(
$userInfoArray['user_id'] );
}
// 沒有問題
else {
// 處理未帶入的資料
if( !isset($userInfoArray['class_id']) ){
$userInfoArray['class_id'] = null;
}
if( !isset($userInfoArray['enable']) ){
$userInfoArray['enable'] = true;
}
if( !isset($userInfoArray['learnStyle_mode']) ){
$userInfoArray['learnStyle_mode'] = null;
}
if( !isset($userInfoArray['material_mode']) ){
$userInfoArray['material_mode'] = null;
}
if( !isset($userInfoArray['nickname']) ){
$userInfoArray['nickname'] = null;
}
if( !isset($userInfoArray['realname']) ){
$userInfoArray['realname'] = null;
}
if( !isset($userInfoArray['email']) ){
$userInfoArray['email'] = null;
}
if( !isset($userInfoArray['memo']) ){
$userInfoArray['memo'] = null;
}
// 進行密碼加密
$passUtil = new Util\Password();
$passwdEncrypted = $passUtil->encrypt( $userInfoArray['password'] );
// 新增一筆使用者資料進資料庫
$db = new Database\DBUser();
$db->insertUser(
$userInfoArray['user_id'],
$passwdEncrypted,
$userInfoArray['group_id'],
$userInfoArray['class_id'],
$userInfoArray['enable'],
$userInfoArray['learnStyle_mode'],
$userInfoArray['material_mode'],
$userInfoArray['nickname'],
$userInfoArray['realname'],
$userInfoArray['email'],
$userInfoArray['memo']
);
}
}
else throw Exception\NoDataException();*/
}
/**
* 是否已有相同名稱的帳號名稱
*
* @param string $group_id 群組ID
* @return bool 已有相同的群組ID
* @since 2.0.0
*/
public function isExist($group_id) {
/*$db = new Database\DBUser();
$info = $db->queryUser($userName);
if( $info != null ) return true;
else return false;*/
}
/**
* 移除此群組
*
* @param string $group_id 群組ID
* @throw UElearning\User\Exception\GroupNoFoundException
* @since 2.0.0
*/
public function remove($group_id) {
/*
// 若有此使用者
if($this->isExist($userName)) {
// TODO: 檢查所有關聯的資料,確認是否可以移除
// 移除資料庫中的使用者
$db = new Database\DBUser();
$db->deleteUser($userName);
}
// 若沒有這位使用者
else {
throw new Exception\UserNoFoundException($userName);
}
*/
}
}

View File

@ -69,6 +69,37 @@ class DBUserTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($info['memo'], $memo); $this->assertEquals($info['memo'], $memo);
} }
/**
* 測試更改使用者資料
*
* @dataProvider userDataProvider
*/
public function testChangeUser($uId, $uPassword, $gId, $cId, $enable,
$l_mode, $m_mode,
$nickName, $realName, $email, $memo){
$afterData = 'sfisjojjoij';
// 記下更改前的資料
$info = $this->db->queryUser($uId);
$beforeData = $info['memo'];
// 更改資料
$this->db->changeUserData($uId, 'memo', $afterData);
// 檢查更改後的結果
$info = $this->db->queryUser($uId);
$this->assertEquals($info['memo'], $afterData);
// 改回來
$this->db->changeUserData($uId, 'memo', $beforeData);
// 檢查有沒有改回來
$info = $this->db->queryUser($uId);
$this->assertEquals($info['memo'], $beforeData);
}
/** /**
* 測試移除使用者 * 測試移除使用者
* *

View File

@ -14,7 +14,7 @@ class UserAdminTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* 測試安裝初始化資料庫 * 測試建立使用者
* *
* @dataProvider userDataProvider * @dataProvider userDataProvider
*/ */

152
tests/User/UserTest.php Normal file
View File

@ -0,0 +1,152 @@
<?php
/**
* UserTest.php
*
* @package UElearning
* @author Yuan Chiu <chyuaner@gmail.com>
*/
namespace UElearning;
require_once UELEARNING_LIB_ROOT.'/User/User.php';
require_once UELEARNING_LIB_ROOT.'/User/UserAdmin.php';
use UElearning\User;
class UserTest extends \PHPUnit_Framework_TestCase
{
/**
* 測試建立使用者
*
* @dataProvider userDataProvider
*/
public function testCreateUser($uId, $uPassword, $gId, $cId, $enable,
$l_mode, $m_mode,
$nickName, $realName, $email, $memo)
{
try {
// 建立資料庫管理物件
$userAdmin = new User\UserAdmin();
// TODO: 建立使用者
$userAdmin->create(
array( 'user_id' => $uId,
'password' => $uPassword,
'group_id' => $gId,
'class_id' => $cId,
'enable' => $enable,
'learnStyle_mode' => $l_mode,
'material_mode' => $m_mode,
'nickname' => $nickName,
'realname' => $realName,
'email' => $email,
'memo' => $memo
));
}
// 若設定的DBMS不被支援 則丟出例外
catch (Database\Exception\DatabaseNoSupportException $e) {
throw $e;
}
}
/**
* 測試取得資料
*
* @dataProvider userDataProvider
*/
public function testGetInfo($uId, $uPassword, $gId, $cId, $enable,
$l_mode, $m_mode,
$nickName, $realName, $email, $memo)
{
try {
$user = new User\User($uId);
// 密碼檢查
$this->assertEquals($user->isPasswordCorrect($uPassword), true);
$this->assertEquals($user->isPasswordCorrect($uPassword.'as1@#'), false);
// 個人資料檢查
$this->assertEquals($user->getNickName(), $nickName);
$this->assertEquals($user->getRealName(), $realName);
$this->assertEquals($user->getEmail(), $email);
$this->assertEquals($user->getMemo(), $memo);
}
catch (User\Exception\UserNoFoundException $e) {
echo 'No Found user: '. $e->getUserId();
}
}
/**
* 測試設定資料
*
* @dataProvider userDataProvider
*/
public function testSetInfo($uId, $uPassword, $gId, $cId, $enable,
$l_mode, $m_mode,
$nickName, $realName, $email, $memo)
{
try {
$user = new User\User($uId);
// 密碼檢查
$user->changePassword('asdw');
$this->assertEquals($user->isPasswordCorrect('asdw'), true);
// 設定啟用檢查
$user->setEnable(false);
$this->assertEquals($user->isEnable(), false);
// 個人資料檢查
$user->setNickName('叉洛伊');
$this->assertEquals($user->getNickName(), '叉洛伊');
$user->setRealName('Eric Chiou');
$this->assertEquals($user->getRealName(), 'Eric Chiou');
$user->setEmail('sdj@example.moe');
$this->assertEquals($user->getEmail(), 'sdj@example.moe');
$user->setMemo('sacmldscmdlsvndlsknvkdsvne;vne;wnvoewzcmlsnwensc');
$this->assertEquals($user->getMemo(),
'sacmldscmdlsvndlsknvkdsvne;vne;wnvoewzcmlsnwensc');
}
catch (User\Exception\UserNoFoundException $e) {
echo 'No Found user: '. $e->getUserId();
}
}
/**
* 刪除建立的帳號(恢復原狀用)
*
* @dataProvider userDataProvider
*/
public function testDeleteUser($uId)
{
// 建立資料庫管理物件
$userAdmin = new User\UserAdmin();
// 移除此使用者
$userAdmin->remove($uId);
// 檢查是否已確實建立
$this->assertEquals($userAdmin->isExist($uId), false);
}
/**
* 測試時要填的資料
*/
public function userDataProvider(){
return array(
array('yuan_unittest', 'pass123', 'admin', null, true,
'harf-line-learn', 1,
'元兒~', 'Yuan Chiu', 'chyuaner@gmail.com', null),
array('eee_unittest', 'qqqssss', 'admin', null, 1,
'harf-line-learn', '1',
'sss', 'Yuan Chiu', 'chyuanesr@gmail.com', null)
);
}
}