diff --git a/htdocs/lib/Database/DBUser.php b/htdocs/lib/Database/DBUser.php index 1cfa80a..afec946 100644 --- a/htdocs/lib/Database/DBUser.php +++ b/htdocs/lib/Database/DBUser.php @@ -23,5 +23,66 @@ require_once UELEARNING_LIB_ROOT.'/Database/Exceptions.php'; */ class DBUser extends Database { + const FORM_USER = 'User'; + + /** + * 新增一個使用者 + * @param string $uId 使用者名稱 + * @param string $password 密碼 + * @param string $gId 群組 + * @param string $cId 班級 + * @param string $enable 啟用此帳號 + * @param string $l_mode 學習模式 + * @param string $m_mode 教材模式 + * @param string $nickName 暱稱 + * @param string $realName 姓名 + * @param string $email 電子郵件地址 + * @param string $memo 備註 + */ + public function insertUser($uId, $password, $gId, $cId, $enable, + $l_mode, $m_mode, + $nickName, $realName, $email, $memo){ + + // 檢查是否有支援所設定的DBMS + if($this->db_type == 'mysql') { + + //紀錄使用者帳號進資料庫 + $sqlString = "INSERT INTO ".$this->table(self::FORM_USER). "(`UID`, `UPassword`, `GID`, `CID`, `UEnabled`, `UBuild_Time`, `LMode`, `MMode`, `UNickname`, `UReal_Name`, `UEmail`, `UMemo`) VALUES ( :id , :passwd, :gid , :cid , :enable , NOW() , :lmode , :mmode , :nickname , :realname , :email , :memo )"; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":id", $uId); + $query->bindParam(":passwd", $uPassword); + $query->bindParam(":gid", $gId); + $query->bindParam(":cid", $cId); + $query->bindParam(":enable", $enable); + $query->bindParam(":lmode", $l_mode); + $query->bindParam(":mmode", $m_mode); + $query->bindParam(":nickname", $nickName); + $query->bindParam(":realname", $realName); + $query->bindParam(":email", $email); + $query->bindParam(":memo", $memo); + $query->execute(); + } + else { + throw new Exception\DatabaseNoSupportException($this->db_type); + } + + } + + /** + * 移除一位使用者 + * @param string $uId 使用者名稱 + */ + public function deleteUser($uId) { + if($this->db_type == 'mysql') { + $sqlString = "DELETE FROM ".$this->table(self::FORM_USER). " WHERE `UID` = :id "; + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":id", $uId); + $query->execute(); + } + else { + throw new Exception\DatabaseNoSupportException($this->db_type); + } + } } \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index 4091f0b..c6cda52 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -4,6 +4,7 @@ tests + tests/InstallTest.php \ No newline at end of file diff --git a/tests/Database/DBUserTest.php b/tests/Database/DBUserTest.php new file mode 100644 index 0000000..de9a4ec --- /dev/null +++ b/tests/Database/DBUserTest.php @@ -0,0 +1,61 @@ + + */ +namespace UElearning; + +require_once UELEARNING_LIB_ROOT.'/Database/DBUser.php'; +require_once UELEARNING_LIB_ROOT.'/Database/Exceptions.php'; +use UElearning\Database\DBUser; +use UElearning\Database\Exception; + +class DBUserTest extends \PHPUnit_Framework_TestCase +{ + + protected $db; + + protected function setUp(){ + try { + // 建立資料庫管理物件 + $this->db = new DBUser(); + + } + // 若設定的DBMS不被支援 則丟出例外 + catch (Database\Exception\DatabaseNoSupportException $e) { + throw $e; + } + } + + /** + * 測試建立使用者 + * + * @dataProvider userDataProvider + */ + public function testCreateUser($uId, $uPassword, $gId, $cId, $enable, + $l_mode, $m_mode, + $nickName, $realName, $email, $memo){ + + $this->db->insertUser($uId, $uPassword, $gId, $cId, $enable, + $l_mode, $m_mode, + $nickName, $realName, $email, $memo); + + } + + /** + * 測試移除使用者 + * + * @dataProvider userDataProvider + */ + public function testDeleteUser($uId) { + $this->db->deleteUser($uId); + } + + public function userDataProvider(){ + return array( + array('yuan', 'pass123', 'admin', null, true, 'harf-line-learn', 1, '元兒~', 'Yuan Chiu', 'chyuaner@gmail.com', null) + ); + } +} \ No newline at end of file diff --git a/tests/InstallTest.php b/tests/InstallTest.php index f0aff88..872d64c 100644 --- a/tests/InstallTest.php +++ b/tests/InstallTest.php @@ -1,33 +1,33 @@ - */ -namespace UElearning; - -require_once UELEARNING_LIB_ROOT.'/Database/DBAdmin.php'; - -class InstallTest extends \PHPUnit_Framework_TestCase -{ - - /** - * 測試安裝初始化資料庫 - */ - public function testInstallDatabase() - { - - try { - // 建立資料庫管理物件 - $dbAdmin = new Database\DBAdmin(); - - // 建立所有所需的資料表 - $dbAdmin->createAllTable(); - } - // 若設定的DBMS不被支援 則丟出例外 - catch (Database\Exception\DatabaseNoSupportException $e) { - throw $e; - } - } -} \ No newline at end of file +///** +// * InstallTest +// * +// * @package UElearning +// * @author Yuan Chiu +// */ +//namespace UElearning; +// +//require_once UELEARNING_LIB_ROOT.'/Database/DBAdmin.php'; +// +//class InstallTest extends \PHPUnit_Framework_TestCase +//{ +// +// /** +// * 測試安裝初始化資料庫 +// */ +// public function testInstallDatabase() +// { +// +// try { +// // 建立資料庫管理物件 +// $dbAdmin = new Database\DBAdmin(); +// +// // 建立所有所需的資料表 +// $dbAdmin->createAllTable(); +// } +// // 若設定的DBMS不被支援 則丟出例外 +// catch (Database\Exception\DatabaseNoSupportException $e) { +// throw $e; +// } +// } +//} \ No newline at end of file diff --git a/tests/User/UserAdminTest.php b/tests/User/UserAdminTest.php index e69de29..1beed5a 100644 --- a/tests/User/UserAdminTest.php +++ b/tests/User/UserAdminTest.php @@ -0,0 +1,33 @@ + + */ +namespace UElearning; + +require_once UELEARNING_LIB_ROOT.'/User/UserAdmin.php'; +use UElearning\User\UserAdmin; + +class UserAdminTest extends \PHPUnit_Framework_TestCase +{ + + /** + * 測試安裝初始化資料庫 + */ + public function testCreateUser() + { + + try { + // 建立資料庫管理物件 + $dbAdmin = new UserAdmin(); + + // TODO: 建立使用者 + } + // 若設定的DBMS不被支援 則丟出例外 + catch (Database\Exception\DatabaseNoSupportException $e) { + throw $e; + } + } +} \ No newline at end of file