PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/models/time/TimeEventTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 424 lines | 293 code | 83 blank | 48 comment | 6 complexity | be8d229808eb1a016e485a1ab4b40c6d 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 TimeEventTest::main() if this source file is executed directly.
  21. if (!defined("PHPUnit_MAIN_METHOD")) {
  22. define("PHPUnit_MAIN_METHOD", "TimeEventTest::main");
  23. }
  24. require_once "PHPUnit/Framework/TestCase.php";
  25. require_once "PHPUnit/Framework/TestSuite.php";
  26. require_once "testConf.php";
  27. require_once 'TimeEvent.php';
  28. require_once ROOT_PATH."/lib/confs/Conf.php";
  29. require_once ROOT_PATH."/lib/common/UniqueIDGenerator.php";
  30. /**
  31. * Test class for TimeEvent.
  32. * Generated by PHPUnit_Util_Skeleton on 2007-03-20 at 11:24:22.
  33. */
  34. class TimeEventTest extends PHPUnit_Framework_TestCase {
  35. public $classTimeEvent = null;
  36. public $connection = null;
  37. /**
  38. * Runs the test methods of this class.
  39. *
  40. * @access public
  41. * @static
  42. */
  43. public static function main() {
  44. require_once "PHPUnit/TextUI/TestRunner.php";
  45. $suite = new PHPUnit_Framework_TestSuite("TimeEventTest");
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. /**
  49. * Sets up the fixture, for example, open a network connection.
  50. * This method is called before a test is executed.
  51. *
  52. * @access protected
  53. */
  54. protected function setUp() {
  55. $this->classTimeEvent = new TimeEvent();
  56. $conf = new Conf();
  57. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  58. $this->assertTrue(mysql_select_db($conf->dbname));
  59. $this->_deleteTestData();
  60. $this->_runQuery("INSERT INTO `hs_hr_employee`(emp_number, emp_lastname, emp_firstname, emp_nick_name, coun_code) VALUES ('010', 'Arnold', 'Subasinghe', 'Arnold', 'AF')");
  61. mysql_query("INSERT INTO `hs_hr_customer` (`customer_id`, `name`, `description`, `deleted`) ".
  62. "VALUES (10, 'OrangeHRM', 'Implement OrangeHRM', 0)");
  63. mysql_query("INSERT INTO `hs_hr_project` (`project_id`, `customer_id`, `name`, `description`, `deleted`) ".
  64. "VALUES (10, 10, 'OrangeHRM', 'Implement OrangeHRM', 0)");
  65. mysql_query("INSERT INTO `hs_hr_project_activity` (`activity_id`, `project_id`, `name`) VALUES (10, 10, 'Test');");
  66. mysql_query("INSERT INTO `hs_hr_timesheet_submission_period` (`timesheet_period_id`, `name`, `frequency`, `period`, `start_day`, `end_day`, `description`) ".
  67. "VALUES (10, 'Permanent', 7, 1, ".date('N').", ".date('N', time()+3600*24*6).", 'Testing')");
  68. mysql_query("INSERT INTO `hs_hr_timesheet` (`timesheet_id`, `employee_id`, `timesheet_period_id`, `start_date`, `end_date`, `status`) ".
  69. "VALUES (10, 10, 10, '".date('Y-m-d')."', '".date('Y-m-d', time()+3600*24*6)." 23:59:59', 0)");
  70. mysql_query("INSERT INTO `hs_hr_timesheet` (`timesheet_id`, `employee_id`, `timesheet_period_id`, `start_date`, `end_date`, `status`) ".
  71. "VALUES (11, 10, 10, '".date('Y-m-d', time()+3600*24*7)."', '".date('Y-m-d', time()+3600*24*13)." 23:59:59', 0)");
  72. mysql_query("INSERT INTO `hs_hr_time_event` (`time_event_id`, `project_id`, `activity_id`, `employee_id`, `timesheet_id`, `start_time`, `end_time`, `reported_date`, `duration`, `description`) ".
  73. "VALUES (10, 10, 10, 10, 10, '".date('Y-m-d 11:30')."', '".date('Y-m-d 11:30', time()+3600)."', '".date('Y-m-d')."', 60, 'Testing')");
  74. mysql_query("INSERT INTO `hs_hr_time_event` (`time_event_id`, `project_id`, `activity_id`, `employee_id`, `timesheet_id`, `start_time`, `end_time`, `reported_date`, `duration`, `description`) ".
  75. "VALUES (11, 10, 10, 10, 10, '".date('Y-m-d 13:00', time()+3600)."', '".date('Y-m-d 13:00', time()+3600*2)."', '".date('Y-m-d')."', 60, 'Testing1')");
  76. mysql_query("INSERT INTO `hs_hr_time_event` (`time_event_id`, `project_id`, `activity_id`, `employee_id`, `timesheet_id`, `start_time`, `end_time`, `reported_date`, `duration`, `description`) ".
  77. "VALUES (12, 10, 10, 10, 10, '".date('Y-m-d 14:30', time()+3600*2)."', NULL, '".date('Y-m-d')."', NULL, 'Testing2')");
  78. mysql_query("INSERT INTO `hs_hr_time_event` (`time_event_id`, `project_id`, `activity_id`, `employee_id`, `timesheet_id`, `start_time`, `end_time`, `reported_date`, `duration`, `description`) ".
  79. "VALUES (13, 10, 10, 10, 11, '".date('Y-m-d H:i:s', time()+3600*3)."', '".date('Y-m-d H:i:s', time()+3600*4)."', '".date('Y-m-d')."', 60, 'Testing unfinished activities')");
  80. $result = mysql_query("SELECT `duration` FROM `hs_hr_time_event` WHERE `timesheet_id` = 10");
  81. UniqueIDGenerator::getInstance()->resetIDs();
  82. }
  83. /**
  84. * Tears down the fixture, for example, close a network connection.
  85. * This method is called after a test is executed.
  86. *
  87. * @access protected
  88. */
  89. protected function tearDown() {
  90. $this->_deleteTestData();
  91. UniqueIDGenerator::getInstance()->resetIDs();
  92. }
  93. /**
  94. * Deletes test data created during test
  95. */
  96. private function _deleteTestData() {
  97. mysql_query("DELETE FROM `hs_hr_time_event` WHERE `time_event_id` IN (10, 11, 12, 13)", $this->connection);
  98. mysql_query("DELETE FROM `hs_hr_timesheet` WHERE `timesheet_id` IN (10, 11, 12)", $this->connection);
  99. mysql_query("DELETE FROM `hs_hr_timesheet_submission_period` WHERE `timesheet_period_id` = 10", $this->connection);
  100. mysql_query("DELETE FROM `hs_hr_project_activity` WHERE `activity_id` = 10", $this->connection);
  101. mysql_query("DELETE FROM `hs_hr_project` WHERE `project_id` = 10", $this->connection);
  102. mysql_query("DELETE FROM `hs_hr_customer` WHERE `customer_id` = 10", $this->connection);
  103. mysql_query("DELETE FROM `hs_hr_employee` WHERE `emp_number` = 10", $this->connection);
  104. }
  105. /**
  106. * Run given sql query
  107. */
  108. private function _runQuery($sql) {
  109. $this->assertTrue(mysql_query($sql), mysql_error());
  110. }
  111. public function testFetchTimeEvents() {
  112. $eventObj = $this->classTimeEvent;
  113. $eventObj->setTimesheetId(50);
  114. $res = $eventObj->fetchTimeEvents();
  115. $this->assertEquals($res, false, "Returned non existing record");
  116. }
  117. public function testFetchTimeEvents2() {
  118. $eventObj = $this->classTimeEvent;
  119. $eventObj->setTimesheetId(10);
  120. $res = $eventObj->fetchTimeEvents();
  121. $expected[0] = array(10, 10, 10, 10, 10, date('Y-m-d 11:30'), date('Y-m-d 11:30', time()+3600), date('Y-m-d'), 60, 'Testing');
  122. $expected[1] = array(11, 10, 10, 10, 10, date('Y-m-d 13:00', time()+3600), date('Y-m-d 13:00', time()+3600*2), date('Y-m-d'), 60, 'Testing1');
  123. $expected[2] = array(12, 10, 10, 10, 10, date('Y-m-d 14:30', time()+3600*2), null, date('Y-m-d'), null, 'Testing2');
  124. $this->assertNotNull($res, "Returned nothing");
  125. $this->assertEquals(count($res), count($expected), "Didn't return the expected number of records");
  126. for ($i=0; $i<count($res); $i++) {
  127. $this->assertEquals($expected[$i][0], $res[$i]->getTimeEventId(), "Invalid time event id");
  128. $this->assertEquals($expected[$i][1], $res[$i]->getProjectId(), "Invalid project id");
  129. $this->assertEquals($expected[$i][2], $res[$i]->getActivityId(), "Invalid activity id");
  130. $this->assertEquals($expected[$i][3], $res[$i]->getEmployeeId(), "Invalid employee id");
  131. $this->assertEquals($expected[$i][4], $res[$i]->getTimesheetId(), "Invalid timesheet id");
  132. $this->assertEquals($expected[$i][5], $res[$i]->getStartTime(), "Invalid start time");
  133. $this->assertEquals($expected[$i][6], $res[$i]->getEndTime(), "Invalid end time");
  134. $this->assertEquals($expected[$i][7], $res[$i]->getReportedDate(), "Invalid reported date");
  135. $this->assertEquals($expected[$i][8], $res[$i]->getDuration(), "Invalid duration");
  136. $this->assertEquals($expected[$i][9], $res[$i]->getDescription(), "Invalid description");
  137. }
  138. }
  139. public function testFetchTimeEvents3() {
  140. $eventObj = $this->classTimeEvent;
  141. $eventObj->setTimesheetId(10);
  142. $res = $eventObj->fetchTimeEvents(true);
  143. $expected[0] = array(12, 10, 10, 10, 10, date('Y-m-d 14:30', time()+3600*2), null, date('Y-m-d'), null, 'Testing2');
  144. $this->assertNotNull($res, "Returned nothing");
  145. $this->assertEquals(count($res), count($expected), "Didn't return the expected number of records");
  146. for ($i=0; $i<count($res); $i++) {
  147. $this->assertEquals($expected[$i][0], $res[$i]->getTimeEventId(), "Invalid time event id");
  148. $this->assertEquals($expected[$i][1], $res[$i]->getProjectId(), "Invalid project id");
  149. $this->assertEquals($expected[$i][2], $res[$i]->getActivityId(), "Invalid activity id");
  150. $this->assertEquals($expected[$i][3], $res[$i]->getEmployeeId(), "Invalid employee id");
  151. $this->assertEquals($expected[$i][4], $res[$i]->getTimesheetId(), "Invalid timesheet id");
  152. $this->assertEquals($expected[$i][5], $res[$i]->getStartTime(), "Invalid start time");
  153. $this->assertEquals($expected[$i][6], $res[$i]->getEndTime(), "Invalid end time");
  154. $this->assertEquals($expected[$i][7], $res[$i]->getReportedDate(), "Invalid reported date");
  155. $this->assertEquals($expected[$i][8], $res[$i]->getDuration(), "Invalid duration");
  156. $this->assertEquals($expected[$i][9], $res[$i]->getDescription(), "Invalid description");
  157. }
  158. }
  159. public function testAddTimeEvent() {
  160. mysql_query("DELETE FROM `hs_hr_time_event` WHERE `time_event_id` IN (12)", $this->connection);
  161. $eventObj = $this->classTimeEvent;
  162. $expected[0] = array(13, 10, 10, 10, 10, date('Y-m-d 01:22'), date('Y-m-d 03:42'), date('Y-m-d'), 90, "Testing2");
  163. $eventObj->setProjectId($expected[0][1]);
  164. $eventObj->setActivityId($expected[0][2]);
  165. $eventObj->setEmployeeId($expected[0][3]);
  166. $eventObj->setTimesheetId($expected[0][4]);
  167. $eventObj->setStartTime($expected[0][5]);
  168. $eventObj->setEndTime($expected[0][6]);
  169. $eventObj->setReportedDate($expected[0][7]);
  170. $eventObj->setDuration($expected[0][8]);
  171. $eventObj->setDescription($expected[0][9]);
  172. $res = $eventObj->addTimeEvent();
  173. $this->assertTrue($res, "Adding failed");
  174. $expected[0][0] = $eventObj->getTimeEventId();
  175. $res = $eventObj->fetchTimeEvents();
  176. $this->assertNotNull($res, "Returned nothing");
  177. $this->assertEquals(count($expected), count($res), "Didn't return the expected number of records");
  178. for ($i=0; $i<count($res); $i++) {
  179. $this->assertEquals($expected[$i][0], $res[$i]->getTimeEventId(), "Invalid time event id");
  180. $this->assertEquals($expected[$i][1], $res[$i]->getProjectId(), "Invalid project id");
  181. $this->assertEquals($expected[$i][2], $res[$i]->getActivityId(), "Invalid activity id");
  182. $this->assertEquals($expected[$i][3], $res[$i]->getEmployeeId(), "Invalid employee id");
  183. $this->assertEquals($expected[$i][4], $res[$i]->getTimesheetId(), "Invalid timesheet id");
  184. $this->assertEquals($expected[$i][5], $res[$i]->getStartTime(), "Invalid start time");
  185. $this->assertEquals($expected[$i][6], $res[$i]->getEndTime(), "Invalid end time");
  186. $this->assertEquals($expected[$i][7], $res[$i]->getReportedDate(), "Invalid reported date");
  187. $this->assertEquals($expected[$i][8], $res[$i]->getDuration(), "Invalid duration");
  188. $this->assertEquals($expected[$i][9], $res[$i]->getDescription(), "Invalid description");
  189. }
  190. }
  191. public function testEditTimeEvent() {
  192. $eventObj = $this->classTimeEvent;
  193. $expected[0] = array(11, 10, 10, 10, 10, date('Y-m-d 04:00', time()-3600), date('Y-m-d 04:00', time()-3600*0.5), date('Y-m-d'), 30, "Testing12");
  194. $eventObj->setTimeEventId($expected[0][0]);
  195. $eventObj->setProjectId($expected[0][1]);
  196. $eventObj->setActivityId($expected[0][2]);
  197. $eventObj->setEmployeeId($expected[0][3]);
  198. $eventObj->setTimesheetId($expected[0][4]);
  199. $eventObj->setStartTime($expected[0][5]);
  200. $eventObj->setEndTime($expected[0][6]);
  201. $eventObj->setReportedDate($expected[0][7]);
  202. $eventObj->setDuration($expected[0][8]);
  203. $eventObj->setDescription($expected[0][9]);
  204. $res = $eventObj->editTimeEvent();
  205. $this->assertTrue($res, "Editing failed");
  206. $res = $eventObj->fetchTimeEvents();
  207. $this->assertNotNull($res, "Returned nothing");
  208. $this->assertEquals(count($res), count($expected), "Didn't return the expected number of records");
  209. for ($i=0; $i<count($res); $i++) {
  210. $this->assertEquals($expected[$i][0], $res[$i]->getTimeEventId(), "Invalid time event id");
  211. $this->assertEquals($expected[$i][1], $res[$i]->getProjectId(), "Invalid project id");
  212. $this->assertEquals($expected[$i][2], $res[$i]->getActivityId(), "Invalid activity id");
  213. $this->assertEquals($expected[$i][3], $res[$i]->getEmployeeId(), "Invalid employee id");
  214. $this->assertEquals($expected[$i][4], $res[$i]->getTimesheetId(), "Invalid timesheet id");
  215. $this->assertEquals($expected[$i][5], $res[$i]->getStartTime(), "Invalid start time");
  216. $this->assertEquals($expected[$i][6], $res[$i]->getEndTime(), "Invalid end time");
  217. $this->assertEquals($expected[$i][7], $res[$i]->getReportedDate(), "Invalid reported date");
  218. $this->assertEquals($expected[$i][8], $res[$i]->getDuration(), "Invalid duration");
  219. $this->assertEquals($expected[$i][9], $res[$i]->getDescription(), "Invalid description");
  220. }
  221. }
  222. public function testPendingTimeEvents() {
  223. $eventObj = $this->classTimeEvent;
  224. $eventObj->setTimeEventId(11);
  225. $res = $eventObj->pendingTimeEvents();
  226. $this->assertNull($res, "Returned completed time event");
  227. }
  228. public function testPendingTimeEvents2() {
  229. $eventObj = $this->classTimeEvent;
  230. $eventObj->setTimeEventId(12);
  231. $res = $eventObj->pendingTimeEvents();
  232. $expected[0] = array(12, 10, 10, 10, 10, date('Y-m-d 14:30', time()+3600*2), null, date('Y-m-d'), null, 'Testing2');
  233. $this->assertNotNull($res, "Returned nothing");
  234. $this->assertEquals(count($res), count($expected), "Didn't return the expected number of records");
  235. for ($i=0; $i<count($res); $i++) {
  236. $this->assertEquals($expected[$i][0], $res[$i]->getTimeEventId(), "Invalid time event id");
  237. $this->assertEquals($expected[$i][1], $res[$i]->getProjectId(), "Invalid project id");
  238. $this->assertEquals($expected[$i][2], $res[$i]->getActivityId(), "Invalid activity id");
  239. $this->assertEquals($expected[$i][3], $res[$i]->getEmployeeId(), "Invalid employee id");
  240. $this->assertEquals($expected[$i][4], $res[$i]->getTimesheetId(), "Invalid timesheet id");
  241. $this->assertEquals($expected[$i][5], $res[$i]->getStartTime(), "Invalid start time");
  242. $this->assertEquals($expected[$i][6], $res[$i]->getEndTime(), "Invalid end time");
  243. $this->assertEquals($expected[$i][7], $res[$i]->getReportedDate(), "Invalid reported date");
  244. $this->assertEquals($expected[$i][8], $res[$i]->getDuration(), "Invalid duration");
  245. $this->assertEquals($expected[$i][9], $res[$i]->getDescription(), "Invalid description");
  246. }
  247. }
  248. public function testPendingTimeEvents3() {
  249. $eventObj = $this->classTimeEvent;
  250. $res = $eventObj->pendingTimeEvents();
  251. $expected[0] = array(12, 10, 10, 10, 10, date('Y-m-d 14:30', time()+3600*2), null, date('Y-m-d'), null, 'Testing2');
  252. $this->assertNotNull($res, "Returned nothing when no id was specified");
  253. $this->assertEquals(count($res), count($expected), "Didn't return the expected number of records");
  254. for ($i=0; $i<count($res); $i++) {
  255. $this->assertEquals($expected[$i][0], $res[$i]->getTimeEventId(), "Invalid time event id");
  256. $this->assertEquals($expected[$i][1], $res[$i]->getProjectId(), "Invalid project id");
  257. $this->assertEquals($expected[$i][2], $res[$i]->getActivityId(), "Invalid activity id");
  258. $this->assertEquals($expected[$i][3], $res[$i]->getEmployeeId(), "Invalid employee id");
  259. $this->assertEquals($expected[$i][4], $res[$i]->getTimesheetId(), "Invalid timesheet id");
  260. $this->assertEquals($expected[$i][5], $res[$i]->getStartTime(), "Invalid start time");
  261. $this->assertEquals($expected[$i][6], $res[$i]->getEndTime(), "Invalid end time");
  262. $this->assertEquals($expected[$i][7], $res[$i]->getReportedDate(), "Invalid reported date");
  263. $this->assertEquals($expected[$i][8], $res[$i]->getDuration(), "Invalid duration");
  264. $this->assertEquals($expected[$i][9], $res[$i]->getDescription(), "Invalid description");
  265. }
  266. }
  267. public function testDeleteTimeEvent() {
  268. $eventObj = $this->classTimeEvent;
  269. $eventObj->setTimeEventId(10);
  270. $res = $eventObj->deleteTimeEvent();
  271. $this->assertTrue($res, "Deletion failed");
  272. $res = $eventObj->fetchTimeEvents();
  273. $this->assertNull($res, "Found deleted records");
  274. }
  275. public function testResolveTimesheet() {
  276. $timesheetObj = new Timesheet();
  277. $eventObj = $this->classTimeEvent;
  278. $expected[0] = array(11, 10, 10, 10, 10, date('Y-m-d 13:00', time()+3600), date('Y-m-d 13:00', time()+3600*1.5), date('Y-m-d'), 30, "Testing12");
  279. $eventObj->setProjectId($expected[0][1]);
  280. $eventObj->setEmployeeId($expected[0][3]);
  281. $eventObj->setStartTime($expected[0][5]);
  282. $eventObj->setEndTime($expected[0][6]);
  283. $eventObj->resolveTimesheet(10);
  284. $this->assertNotNull($eventObj->getTimesheetId(), "Timesheet id was not resolved");
  285. $this->assertEquals($eventObj->getTimesheetId(), $expected[0][4], "Timesheet id is invalid");
  286. }
  287. public function testResolveTimesheet2() {
  288. $eventObj = $this->classTimeEvent;
  289. $expected[0] = array(11, 10, 10, 10, 12, date('Y-m-d 13:00', time()+3600*24*15), date('Y-m-d 13:00', (time()+3600*24*15)+1800)." 23:59:59", date('Y-m-d'), 30, "Testing12");
  290. $eventObj->setProjectId($expected[0][1]);
  291. $eventObj->setEmployeeId($expected[0][3]);
  292. $eventObj->setStartTime($expected[0][5]);
  293. $eventObj->setEndTime($expected[0][6]);
  294. $eventObj->resolveTimesheet(10);
  295. $this->assertNotNull($eventObj->getTimesheetId(), "Timesheet id was not resolved");
  296. $this->assertEquals($eventObj->getTimesheetId(), $expected[0][4], "Timesheet id is invalid");
  297. }
  298. public function testTimeReport() {
  299. $eventObj = $this->classTimeEvent;
  300. $eventObj->setEmployeeId(10);
  301. $res = $eventObj->timeReport(date('Y-m-d', time()+3600*24), date('Y-m-d', time()+3600*47));
  302. $this->assertNull($res, "Empty report received");
  303. }
  304. public function testFetchTimeSheetIds(){
  305. $eventObj = $this->classTimeEvent;
  306. $eventObj->setStartTime(date('Y-m-d'));
  307. $eventObj->setEndTime(date('Y-m-d', time()+3600*2));
  308. $employeeIds = array(10);
  309. $timsheetIds = $eventObj->fetchTimeSheetIds($employeeIds);
  310. $this->assertEquals($timsheetIds[0], 10 , "Employee id is wrong");
  311. $this->assertEquals($timsheetIds[1], 11 , "Employee id is wrong");
  312. }
  313. public function testIsUnfinishedTimesheet() {
  314. $this->assertTrue(TimeEvent::isUnfinishedTimesheet(10));
  315. $this->assertFalse(TimeEvent::isUnfinishedTimesheet(11));
  316. }
  317. }
  318. // Call TimeEventTest::main() if this source file is executed directly.
  319. if (PHPUnit_MAIN_METHOD == "TimeEventTest::main") {
  320. TimeEventTest::main();
  321. }
  322. ?>