PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/common/authorizeTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 338 lines | 214 code | 60 blank | 64 comment | 1 complexity | 7100da26d539d8b0aca7289f52c65f17 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, AGPL-3.0, BSD-3-Clause, AGPL-1.0, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
  4. * all the essential functionalities required for any enterprise.
  5. * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
  6. *
  7. * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
  8. * the GNU General Public License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program;
  16. * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA
  18. *
  19. */
  20. // Call authorizeTest::main() if this source file is executed directly.
  21. if (!defined("PHPUnit_MAIN_METHOD")) {
  22. define("PHPUnit_MAIN_METHOD", "authorizeTest::main");
  23. }
  24. require_once "PHPUnit/Framework/TestCase.php";
  25. require_once "PHPUnit/Framework/TestSuite.php";
  26. require_once "testConf.php";
  27. $_SESSION['WPATH'] = WPATH;
  28. require_once "authorize.php";
  29. require_once ROOT_PATH."/lib/confs/Conf.php";
  30. /**
  31. * Test class for authorize.
  32. * Generated by PHPUnit_Util_Skeleton on 2006-11-02 at 10:06:38.
  33. */
  34. class authorizeTest extends PHPUnit_Framework_TestCase {
  35. public $authorizeObj = null;
  36. public $connection = null;
  37. public $testSubject = array('employeeId' => "012", 'isAdmin' => "Yes");
  38. /**
  39. * Runs the test methods of this class.
  40. *
  41. * @access public
  42. * @static
  43. */
  44. public static function main() {
  45. require_once "PHPUnit/TextUI/TestRunner.php";
  46. $suite = new PHPUnit_Framework_TestSuite("authorizeTest");
  47. $result = PHPUnit_TextUI_TestRunner::run($suite);
  48. }
  49. /**
  50. * Sets up the fixture, for example, open a network connection.
  51. * This method is called before a test is executed.
  52. *
  53. * @access protected
  54. */
  55. protected function setUp() {
  56. $this->authorizeObj = new authorize($this->testSubject['employeeId'], $this->testSubject['isAdmin']);
  57. $conf = new Conf();
  58. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  59. mysql_select_db($conf->dbname);
  60. $this->_deleteTestData();
  61. // Insert job titles
  62. $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc, jobtit_comm, sal_grd_code) " .
  63. "VALUES('JOB001', 'Manager', 'Manager job title', 'no comments', null)");
  64. $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc, jobtit_comm, sal_grd_code) " .
  65. "VALUES('JOB002', 'Driver', 'Driver job title', 'no comments', null)");
  66. $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc, jobtit_comm, sal_grd_code) " .
  67. "VALUES('JOB003', 'Director', 'Director job title', 'no comments', null)");
  68. $this->_runQuery("INSERT INTO `hs_hr_employee`(emp_number, employee_id, emp_lastname, emp_firstname, emp_nick_name, coun_code) " .
  69. "VALUES (11, NULL, 'Arnold', 'Subasinghe', 'Arnold', 'AF')");
  70. $this->_runQuery("INSERT INTO `hs_hr_employee`(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, emp_nick_name) " .
  71. "VALUES (12, NULL, 'Mohanjith', 'Sudirikku', 'Hannadige', 'MOHA')");
  72. // employees with job titles
  73. // Driver
  74. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, job_title_code, emp_work_email) " .
  75. "VALUES(13, '0013', 'Rajasinghe', 'Saman', 'Marlon', 'JOB002', 'aruna@company.com')");
  76. // Manager
  77. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, job_title_code, emp_work_email) " .
  78. "VALUES(14, '0014', 'Jayasinghe', 'Aruna', 'Shantha', 'JOB001', 'arnold@mydomain.com')");
  79. // Insert director
  80. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, job_title_code, emp_work_email) " .
  81. "VALUES(15, '0032', 'Samuel', 'John', 'A', 'JOB003', 'mohanjith@mydomain.com')");
  82. mysql_query("INSERT INTO `hs_hr_emp_reportto` VALUES ('012', '011', 1);");
  83. mysql_query("INSERT INTO hs_hr_customer(customer_id, name, description, deleted) " .
  84. "VALUES(1, 'Test customer', 'description', 0)");
  85. mysql_query("INSERT INTO hs_hr_project(project_id, customer_id, name, description, deleted) " .
  86. "VALUES(1, 1, 'Test project 1', 'a test proj 1', 0)");
  87. mysql_query("INSERT INTO hs_hr_project(project_id, customer_id, name, description, deleted) " .
  88. "VALUES(2, 1, 'Test project 2', 'a test proj 2', 0)");
  89. }
  90. /**
  91. * Tears down the fixture, for example, close a network connection.
  92. * This method is called after a test is executed.
  93. *
  94. * @access protected
  95. */
  96. protected function tearDown() {
  97. $this->_deleteTestData();
  98. }
  99. /**
  100. * Deletes test data created during test
  101. */
  102. private function _deleteTestData() {
  103. mysql_query("TRUNCATE TABLE `hs_hr_project`", $this->connection);
  104. mysql_query("TRUNCATE TABLE `hs_hr_project_admin`", $this->connection);
  105. mysql_query("TRUNCATE TABLE `hs_hr_customer`", $this->connection);
  106. mysql_query("DELETE FROM `hs_hr_employee` WHERE `emp_number` in (11, 12, 13, 14, 15)", $this->connection);
  107. mysql_query("DELETE FROM `hs_hr_emp_reportto` WHERE `erep_sup_emp_number` = '012' AND `erep_sub_emp_number` = '011'", $this->connection);
  108. $this->_runQuery("TRUNCATE TABLE `hs_hr_job_title`");
  109. }
  110. /**
  111. * Run given sql query
  112. */
  113. private function _runQuery($sql) {
  114. $this->assertTrue(mysql_query($sql), mysql_error());
  115. }
  116. public function testIsAdmin() {
  117. $authObj = new authorize($this->testSubject['employeeId'], 'No');
  118. $res = $authObj->isAdmin();
  119. $this->assertEquals($res, false, "Non admin an Admin");
  120. }
  121. public function testIsAdmin2() {
  122. $res = $this->authorizeObj->isAdmin();
  123. $this->assertEquals($res, true, "Admin not an Admin");
  124. }
  125. public function testIsSupervisor() {
  126. $authObj = new authorize("041", 'Yes');
  127. $res = $authObj->isSupervisor();
  128. $this->assertEquals($res, false, "non Supervisor an Supervisor");
  129. }
  130. public function testIsSupervisor2() {
  131. $this->authorizeObj = new authorize($this->testSubject['employeeId'], $this->testSubject['isAdmin']);
  132. $res = $this->authorizeObj->isSupervisor();
  133. $this->assertEquals($res, true, "Supervisor not an Supervisor");
  134. }
  135. /**
  136. * Test case for isManager function
  137. */
  138. public function testIsManager() {
  139. // driver
  140. $authObj = new authorize('013', 'No');
  141. $this->assertFalse($authObj->isManager());
  142. $authObj = new authorize('013', 'Yes');
  143. $this->assertFalse($authObj->isManager());
  144. // manager
  145. $authObj = new authorize('014', 'No');
  146. $this->assertTrue($authObj->isManager());
  147. $authObj = new authorize('014', 'Yes');
  148. $this->assertTrue($authObj->isManager());
  149. // director
  150. $authObj = new authorize('015', 'No');
  151. $this->assertFalse($authObj->isManager());
  152. $authObj = new authorize('015', 'Yes');
  153. $this->assertFalse($authObj->isManager());
  154. }
  155. /**
  156. * Test case for isDirector function
  157. */
  158. public function testIsDirector() {
  159. // driver
  160. $authObj = new authorize('013', 'No');
  161. $this->assertFalse($authObj->isDirector());
  162. $authObj = new authorize('013', 'Yes');
  163. $this->assertFalse($authObj->isDirector());
  164. // manager
  165. $authObj = new authorize('014', 'No');
  166. $this->assertFalse($authObj->isDirector());
  167. $authObj = new authorize('014', 'Yes');
  168. $this->assertFalse($authObj->isDirector());
  169. // director
  170. $authObj = new authorize('015', 'No');
  171. $this->assertTrue($authObj->isDirector());
  172. $authObj = new authorize('015', 'Yes');
  173. $this->assertTrue($authObj->isDirector());
  174. }
  175. public function testIsESS() {
  176. $authObj = new authorize("", 'Yes');
  177. $res = $authObj->isESS();
  178. $this->assertEquals($res, false, "ESS not an ESS");
  179. }
  180. public function testIsESS2() {
  181. $res = $this->authorizeObj->isESS();
  182. $this->assertEquals($res, true, "ESS not an ESS");
  183. }
  184. public function testIsTheSupervisor() {
  185. $res = $this->authorizeObj->isTheSupervisor("051");
  186. $this->assertEquals($res, false, "The supervisor of unknown employee");
  187. }
  188. public function testIsTheSupervisor2() {
  189. $res = $this->authorizeObj->isTheSupervisor("011");
  190. $this->assertEquals($res, true, "The supervisor of unknown emplyee");
  191. }
  192. public function testFirstRole() {
  193. $authObj = new authorize("041", 'No');
  194. $roleArr = array($authObj->roleAdmin, $authObj->roleSupervisor);
  195. $res = $authObj->firstRole($roleArr);
  196. $this->assertEquals($res, false, "Didn't return the first");
  197. }
  198. public function testFirstRole2() {
  199. $authObj = new authorize($this->testSubject['employeeId'], 'No');
  200. $roleArr = array($authObj->roleAdmin, $authObj->roleSupervisor);
  201. $res = $authObj->firstRole($roleArr);
  202. $this->assertEquals($res, $authObj->roleSupervisor, "Didn't return the first");
  203. }
  204. public function testFirstRole3() {
  205. $authObj = $this->authorizeObj;
  206. $roleArr = array($authObj->roleAdmin, $authObj->roleSupervisor);
  207. $res = $authObj->firstRole($roleArr);
  208. $this->assertEquals($res, $roleArr[0], "Didn't return the first");
  209. }
  210. /**
  211. * Test case of isProjectAdmin() method
  212. */
  213. public function testIsProjectAdmin() {
  214. $authObj = new authorize("012", 'No');
  215. $this->assertFalse($authObj->isProjectAdmin(), "Not a project admin");
  216. $this->assertTrue(mysql_query("INSERT INTO hs_hr_project_admin(emp_number, project_id) " .
  217. "VALUES(12, 1)"));
  218. $authObj = new authorize("012", 'No');
  219. $this->assertTrue($authObj->isProjectAdmin(), "Project admin not identified.");
  220. }
  221. /**
  222. * Test case of isProjectAdminOf() method
  223. */
  224. public function testIsProjectAdminOf() {
  225. $authObj = new authorize("012", 'No');
  226. $this->assertFalse($authObj->isProjectAdminOf(1), "Not a project admin");
  227. mysql_query("INSERT INTO hs_hr_project_admin(emp_number, project_id) " .
  228. "VALUES(12, 1)");
  229. $authObj = new authorize("012", 'No');
  230. $this->assertTrue($authObj->isProjectAdminOf(1), "Employee is an admin of project 1");
  231. $this->assertFalse($authObj->isProjectAdminOf(2), "Employee is not an admin of project 2");
  232. }
  233. public function testIsActionPermitted() {
  234. // Admin
  235. $this->assertTrue($this->authorizeObj->isActionPermitted('TCP'), 'Admin should be permitted all actions');
  236. $this->assertTrue($this->authorizeObj->isActionPermitted('TAX'), 'Admin should be permitted all actions');
  237. // Supervisor
  238. $authObj = new authorize('012', 'No');
  239. $this->assertTrue($authObj->isActionPermitted('TCP'), 'Supervisor should be permitted action TCP');
  240. $this->assertTrue($authObj->isActionPermitted('CST'), 'Supervisor should be permitted action CST');
  241. $this->assertFalse($authObj->isActionPermitted('TAX'), 'Supervisor should not be permitted action TAX');
  242. // Project Admin
  243. $query = "INSERT INTO hs_hr_project_admin (project_id, emp_number) VALUES(1, 11)";
  244. $this->assertTrue(mysql_query($query), mysql_error());
  245. $authObj = new authorize('011', 'No');
  246. $this->assertTrue($authObj->isActionPermitted('PAC'), 'Project Admin should be permitted action PAC');
  247. $this->assertFalse($authObj->isActionPermitted('TAX'), 'Project Admin should not be permitted action TAX');
  248. $query = "DELETE FROM hs_hr_project_admin WHERE project_id = 1 AND emp_number = 11";
  249. $this->assertTrue(mysql_query($query), mysql_error());
  250. // ESS User
  251. $authObj = new authorize('011', 'No');
  252. $this->assertFalse($authObj->isActionPermitted('TCP'), 'ESS User should not be permitted action TCP');
  253. $this->assertFalse($authObj->isActionPermitted('TAX'), 'ESS User should not be permitted action TAX');
  254. }
  255. }
  256. // Call authorizeTest::main() if this source file is executed directly.
  257. if (PHPUnit_MAIN_METHOD == "authorizeTest::main") {
  258. authorizeTest::main();
  259. }
  260. ?>