PageRenderTime 97ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/models/time/ProjectReportTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 1185 lines | 670 code | 234 blank | 281 comment | 39 complexity | e3d2d4212f2ddd84999ca5d3dd6a3d91 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. // Call ProjectReportTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "ProjectReportTest::main");
  5. }
  6. require_once "PHPUnit/Framework/TestCase.php";
  7. require_once "PHPUnit/Framework/TestSuite.php";
  8. require_once "testConf.php";
  9. require_once ROOT_PATH.'/lib/dao/DMLFunctions.php';
  10. require_once ROOT_PATH.'/lib/dao/SQLQBuilder.php';
  11. require_once ROOT_PATH.'/lib/confs/sysConf.php';
  12. require_once ROOT_PATH.'/lib/models/eimadmin/ProjectActivity.php';
  13. require_once ROOT_PATH.'/lib/models/time/Timesheet.php';
  14. require_once ROOT_PATH.'/lib/models/time/ProjectActivityTime.php';
  15. require_once ROOT_PATH.'/lib/models/time/EmployeeActivityTime.php';
  16. require_once ROOT_PATH.'/lib/models/time/ProjectReport.php';
  17. /**
  18. * Test class for ProjectReport.
  19. * Generated by PHPUnit_Util_Skeleton on 2007-07-16 at 19:32:46.
  20. */
  21. class ProjectReportTest extends PHPUnit_Framework_TestCase {
  22. private $projects;
  23. private $customerId;
  24. private $customerName;
  25. private $activities;
  26. private $connection;
  27. /**
  28. * Runs the test methods of this class.
  29. *
  30. * @access public
  31. * @static
  32. */
  33. public static function main() {
  34. require_once "PHPUnit/TextUI/TestRunner.php";
  35. $suite = new PHPUnit_Framework_TestSuite("ProjectReportTest");
  36. $result = PHPUnit_TextUI_TestRunner::run($suite);
  37. }
  38. /**
  39. * Sets up the fixture, for example, open a network connection.
  40. * This method is called before a test is executed.
  41. *
  42. * @access protected
  43. */
  44. protected function setUp() {
  45. $conf = new Conf();
  46. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  47. mysql_select_db($conf->dbname);
  48. $this->_truncateTables();
  49. // Insert a customer and projects for use in the test
  50. $this->customerId = 1;
  51. $this->customerName = "OrangeHRM";
  52. $this->projects = array( 1 => "Project A", 2 => "Project B");
  53. $this->_addCustomer();
  54. $this->_addProjects();
  55. // Insert activities
  56. $this->_addActivity(1, "Programming");
  57. $this->_addActivity(1, "QA");
  58. //$this->_addActivity(1, "Misc");
  59. $this->_addActivity(1, "Support");
  60. $this->_addActivity(2, "Programming");
  61. $this->_addActivity(2, "QA");
  62. $this->_addActivity(2, "Support");
  63. //$this->_addActivity(2, "Misc");
  64. // Insert deleted activities
  65. $this->_addActivity(1, "Deleted Activity", true);
  66. $this->_addActivity(2, "Deleted Activity", true);
  67. // Add employees
  68. $this->_addEmployee(1, "001", "Rajasinghe", "John");
  69. $this->_addEmployee(2, "002", "Wickramasinghe", "Ravi");
  70. $this->_addEmployee(3, "003", "Kumarasinghe", "Rasanga");
  71. }
  72. /**
  73. * Tears down the fixture, removed database entries created during test.
  74. *
  75. * @access protected
  76. */
  77. protected function tearDown() {
  78. $this->_truncateTables();
  79. }
  80. /**
  81. * test method getProjectActivityTime()
  82. */
  83. public function testGetProjectActivityTimeWithNoEvents() {
  84. $report = new ProjectReport();
  85. // With no events - no days specified
  86. $projectId = 1;
  87. $results = $report->getProjectActivityTime($projectId);
  88. $res = $this->_verifyActivityTime($results, $projectId, array("Programming", "QA", "Support"), array(0, 0, 0));
  89. $this->assertTrue($res);
  90. // With no events - with start date specified
  91. $results = $report->getProjectActivityTime($projectId, "2001-01-01");
  92. $res = $this->_verifyActivityTime($results, $projectId, array("Programming", "QA", "Support"), array(0, 0, 0));
  93. $this->assertTrue($res);
  94. // With no events - with start date and end date specified
  95. $results = $report->getProjectActivityTime($projectId, "2001-01-01", "2007-01-01");
  96. $res = $this->_verifyActivityTime($results, $projectId, array("Programming", "QA", "Support"), array(0, 0, 0));
  97. $this->assertTrue($res);
  98. }
  99. /**
  100. * test method getProjectActivityTime()
  101. */
  102. public function testGetProjectActivityTimeWithOneEvent() {
  103. $report = new ProjectReport();
  104. // Add one event.
  105. $projectId = 1;
  106. $this->_addEvent(1, 1, "Programming", "2007-07-12 10:00", 60);
  107. $results = $report->getProjectActivityTime($projectId, "2007-07-12", "2007-07-12");
  108. $res = $this->_verifyActivityTime($results, $projectId, array("Programming", "QA", "Support"), array(60, 0, 0));
  109. $this->assertTrue($res);
  110. $results = $report->getProjectActivityTime($projectId, "2007-07-1", "2007-07-13");
  111. $res = $this->_verifyActivityTime($results, $projectId, array("Programming", "QA", "Support"), array(60, 0, 0));
  112. $this->assertTrue($res);
  113. }
  114. /**
  115. * test method getProjectActivityTime()
  116. */
  117. public function testGetProjectActivityTimeWithSpanningEvent() {
  118. $report = new ProjectReport();
  119. // Add one event.
  120. $projectId = 1;
  121. $this->_addEvent(1, 1, "QA", "2007-07-12 23:00", 95);
  122. $results = $report->getProjectActivityTime($projectId, "2007-07-12", "2007-07-12");
  123. $res = $this->_verifyActivityTime($results, $projectId, array("Programming", "QA", "Support"), array(0, 60, 0));
  124. $this->assertTrue($res);
  125. $results = $report->getProjectActivityTime($projectId, "2007-07-12", "2007-07-13");
  126. $res = $this->_verifyActivityTime($results, $projectId, array("Programming", "QA", "Support"), array(0, 95, 0));
  127. $this->assertTrue($res);
  128. $results = $report->getProjectActivityTime($projectId, "2007-07-1", "2007-07-13");
  129. $res = $this->_verifyActivityTime($results, $projectId, array("Programming", "QA", "Support"), array(0, 95, 0));
  130. $this->assertTrue($res);
  131. }
  132. /**
  133. * test method getProjectActivityTime()
  134. */
  135. public function testGetProjectActivityTimeWithDeletedActivity() {
  136. $report = new ProjectReport();
  137. // Add one event.
  138. $projectId = 1;
  139. $this->_addEvent(1, 1, "Deleted Activity", "2007-07-12 13:20", 15);
  140. $results = $report->getProjectActivityTime($projectId, "2007-07-12", "2007-07-12");
  141. $res = $this->_verifyActivityTime($results, $projectId,
  142. array("Programming", "QA", "Support", "Deleted Activity"), array(0, 0, 0, 15));
  143. $this->assertTrue($res);
  144. $results = $report->getProjectActivityTime($projectId, "2007-07-1", "2007-07-13");
  145. $res = $this->_verifyActivityTime($results, $projectId,
  146. array("Programming", "QA", "Support", "Deleted Activity"), array(0, 0, 0, 15));
  147. $this->assertTrue($res);
  148. }
  149. /**
  150. * test method getProjectActivityTime()
  151. */
  152. public function testGetProjectActivityTimeWithMultipleActivities() {
  153. $report = new ProjectReport();
  154. // QA - Employee 1
  155. $this->_addEvent(1, 1, "QA", "2007-07-05 09:25", 70);
  156. $this->_addEvent(1, 1, "QA", "2007-07-07 08:12", 33); // 33 min
  157. $this->_addEvent(1, 1, "QA", "2007-07-10 09:25", 8 * 60 + 30); // 510 min
  158. // QA - Employee 2
  159. $this->_addEvent(1, 2, "QA", "2007-07-05 09:25", 70); // 70 min
  160. $this->_addEvent(1, 2, "QA", "2007-07-12 11:25", 4 * 60 + 10); // 250 min
  161. $this->_addEvent(1, 2, "QA", "2007-07-11 06:25", 4 * 60 + 10); // 250 min
  162. // QA Events for different project
  163. $this->_addEvent(2, 1, "QA", "2007-07-05 07:25", 8 * 60 + 10); // 490 min
  164. $this->_addEvent(2, 2, "QA", "2007-07-05 09:25", 5 * 60); // 300 min
  165. // Programming - Employee 1
  166. $this->_addEvent(1, 1, "Programming", "2007-07-05 09:25", 80);
  167. $this->_addEvent(1, 1, "Programming", "2007-07-07 08:12", 70);
  168. $this->_addEvent(1, 1, "Programming", "2007-07-10 09:25", 210);
  169. // Programming - Employee 2
  170. $this->_addEvent(1, 2, "Programming", "2007-07-05 09:25", 230);
  171. $this->_addEvent(1, 2, "Programming", "2007-07-12 11:25", 450);
  172. $this->_addEvent(1, 2, "Programming", "2007-07-11 06:25", 900);
  173. // Programming for different project
  174. $this->_addEvent(2, 1, "Programming", "2007-07-05 05:25", 800);
  175. $this->_addEvent(2, 2, "Programming", "2007-07-12 15:25", 450);
  176. // Event spanning more than one day
  177. $this->_addEvent(1, 1, "Programming", "2007-07-19 18:00", 600);
  178. $this->_addEvent(1, 2, "Programming", "2007-07-18 21:25", 500);
  179. // Deleted activity for project 2
  180. $this->_addEvent(2, 1, "Deleted Activity", "2007-07-10 09:25", 400);
  181. $this->_addEvent(2, 2, "Deleted Activity", "2007-07-11 09:25", 300);
  182. // Check project 2 time
  183. $results = $report->getProjectActivityTime(2, "2007-07-05", "2007-07-25");
  184. $res = $this->_verifyActivityTime($results, 2, array("Programming", "QA", "Support", "Deleted Activity"),
  185. array(1250, 790, 0, 700));
  186. $this->assertTrue($res);
  187. // Check project 1 time
  188. $results = $report->getProjectActivityTime(1, "2007-07-05", "2007-07-25");
  189. $res = $this->_verifyActivityTime($results, 1, array("Programming", "QA", "Support"), array(3040, 1183, 0));
  190. $this->assertTrue($res);
  191. // Project 1 time for day with spanning events.
  192. $results = $report->getProjectActivityTime(1, "2007-07-19", "2007-07-19");
  193. $res = $this->_verifyActivityTime($results, 1, array("Programming", "QA", "Support"), array(360+345, 0, 0));
  194. $this->assertTrue($res);
  195. // Project 1 time from 07-10
  196. $results = $report->getProjectActivityTime(1, "2007-07-10", "2007-07-30");
  197. $res = $this->_verifyActivityTime($results, 1, array("Programming", "QA", "Support"), array(2660, 1010, 0));
  198. $this->assertTrue($res);
  199. }
  200. /**
  201. * test getEmployeeActivityTime() method
  202. */
  203. public function testGetEmployeeActivityTimeWithNoEvents() {
  204. $report = new ProjectReport();
  205. // With no events - no days specified
  206. $projectId = 1;
  207. $activityId = 1;
  208. $results = $report->getEmployeeActivityTime($projectId, $activityId);
  209. $this->assertTrue(is_array($results));
  210. $this->assertEquals(0, count($results));
  211. // With no events - start date specified
  212. $results = $report->getEmployeeActivityTime($projectId, $activityId, "2001-01-01");
  213. $this->assertTrue(is_array($results));
  214. $this->assertEquals(0, count($results));
  215. // With no events - start date and end date specified
  216. $results = $report->getEmployeeActivityTime($projectId, $activityId, "2001-01-01", "2007-01-01");
  217. $this->assertTrue(is_array($results));
  218. $this->assertEquals(0, count($results));
  219. }
  220. /**
  221. * test getEmployeeActivityTime() method
  222. */
  223. public function testGetEmployeeActivityTimeWithOneEvent() {
  224. $report = new ProjectReport();
  225. // Add one event.
  226. $projectId = 1;
  227. $employeeId = 1;
  228. $activityName = "Programming";
  229. $activity = $this->activities[$projectId][$activityName];
  230. $activityId = $activity->getId();
  231. $this->_addEvent($projectId, $employeeId, $activityName, "2007-07-12 10:00", 60);
  232. $results = $report->getEmployeeActivityTime($projectId, $activityId);
  233. $res = $this->_verifyEmployeeTime($results, $projectId, $activityId, array(1), array(60));
  234. $this->assertTrue($res);
  235. // With no events - start date specified
  236. $results = $report->getEmployeeActivityTime($projectId, $activityId, "2001-01-01");
  237. $res = $this->_verifyEmployeeTime($results, $projectId, $activityId, array(1), array(60));
  238. $this->assertTrue($res);
  239. // With no events - start date and end date specified
  240. $results = $report->getEmployeeActivityTime($projectId, $activityId, "2001-01-01", "2007-07-13");
  241. $res = $this->_verifyEmployeeTime($results, $projectId, $activityId, array(1), array(60));
  242. $this->assertTrue($res);
  243. }
  244. /**
  245. * test getEmployeeActivityTime() method
  246. */
  247. public function testGetEmployeeActivityTimeWithSpanningEvent() {
  248. $report = new ProjectReport();
  249. // Add one event.
  250. $projectId = 1;
  251. $employeeId = 1;
  252. $activityName = "QA";
  253. $activity = $this->activities[$projectId][$activityName];
  254. $activityId = $activity->getId();
  255. $this->_addEvent($projectId, $employeeId, $activityName, "2007-07-12 23:00", 95);
  256. $results = $report->getEmployeeActivityTime($projectId, $activityId, "2007-07-12", "2007-07-12");
  257. $res = $this->_verifyEmployeeTime($results, $projectId, $activityId, array(1), array(60));
  258. $this->assertTrue($res);
  259. $results = $report->getEmployeeActivityTime($projectId, $activityId, "2007-07-12", "2007-07-13");
  260. $res = $this->_verifyEmployeeTime($results, $projectId, $activityId, array(1), array(95));
  261. $this->assertTrue($res);
  262. $results = $report->getEmployeeActivityTime($projectId, $activityId, "2007-07-1", "2007-07-15");
  263. $res = $this->_verifyEmployeeTime($results, $projectId, $activityId, array(1), array(95));
  264. $this->assertTrue($res);
  265. // With only start date
  266. $results = $report->getEmployeeActivityTime($projectId, $activityId, "2007-07-1");
  267. $res = $this->_verifyEmployeeTime($results, $projectId, $activityId, array(1), array(95));
  268. $this->assertTrue($res);
  269. // Without range
  270. $results = $report->getEmployeeActivityTime($projectId, $activityId);
  271. $res = $this->_verifyEmployeeTime($results, $projectId, $activityId, array(1), array(95));
  272. $this->assertTrue($res);
  273. }
  274. /**
  275. * test getEmployeeActivityTime() method
  276. */
  277. public function testGetEmployeeActivityTimeWithDeletedActivity() {
  278. $report = new ProjectReport();
  279. // Add one event.
  280. $projectId = 1;
  281. $employeeId = 1;
  282. $activityName = "Deleted Activity";
  283. $activity = $this->activities[$projectId][$activityName];
  284. $activityId = $activity->getId();
  285. $this->_addEvent($projectId, $employeeId, $activityName, "2007-07-12 13:20", 15);
  286. $results = $report->getEmployeeActivityTime($projectId, $activityId, "2007-07-12", "2007-07-12");
  287. $res = $this->_verifyEmployeeTime($results, $projectId, $activityId, array(1), array(15));
  288. $this->assertTrue($res);
  289. $results = $report->getEmployeeActivityTime($projectId, $activityId, "2007-07-01", "2007-07-13");
  290. $res = $this->_verifyEmployeeTime($results, $projectId, $activityId, array(1), array(15));
  291. $this->assertTrue($res);
  292. // With only start date
  293. $results = $report->getEmployeeActivityTime($projectId, $activityId, "2007-07-01");
  294. $res = $this->_verifyEmployeeTime($results, $projectId, $activityId, array(1), array(15));
  295. $this->assertTrue($res);
  296. // Without date range
  297. $results = $report->getEmployeeActivityTime($projectId, $activityId);
  298. $res = $this->_verifyEmployeeTime($results, $projectId, $activityId, array(1), array(15));
  299. $this->assertTrue($res);
  300. }
  301. /**
  302. * test getEmployeeActivityTime() method
  303. */
  304. public function testGetEmployeeActivityTimeWithMultipleActivities() {
  305. $report = new ProjectReport();
  306. // QA - Employee 1
  307. $this->_addEvent(1, 1, "QA", "2007-07-05 09:25", 70);
  308. $this->_addEvent(1, 1, "QA", "2007-07-07 08:12", 33); // 33 min
  309. $this->_addEvent(1, 1, "QA", "2007-07-10 09:25", 8 * 60 + 30); // 510 min
  310. // QA - Employee 2
  311. $this->_addEvent(1, 2, "QA", "2007-07-05 09:25", 70); // 70 min
  312. $this->_addEvent(1, 2, "QA", "2007-07-12 11:25", 4 * 60 + 10); // 250 min
  313. $this->_addEvent(1, 2, "QA", "2007-07-11 06:25", 4 * 60 + 10); // 250 min
  314. // QA Events for different project
  315. $this->_addEvent(2, 1, "QA", "2007-07-05 07:25", 8 * 60 + 10); // 490 min
  316. $this->_addEvent(2, 2, "QA", "2007-07-05 09:25", 5 * 60); // 300 min
  317. // Programming - Employee 1
  318. $this->_addEvent(1, 1, "Programming", "2007-07-05 09:25", 80);
  319. $this->_addEvent(1, 1, "Programming", "2007-07-07 08:12", 70);
  320. $this->_addEvent(1, 1, "Programming", "2007-07-10 09:25", 210);
  321. // Programming - Employee 2
  322. $this->_addEvent(1, 2, "Programming", "2007-07-05 09:25", 230);
  323. $this->_addEvent(1, 2, "Programming", "2007-07-12 11:25", 450);
  324. $this->_addEvent(1, 2, "Programming", "2007-07-11 06:25", 900);
  325. // Programming for different project
  326. $this->_addEvent(2, 1, "Programming", "2007-07-05 05:25", 800);
  327. $this->_addEvent(2, 2, "Programming", "2007-07-12 15:25", 450);
  328. // Event spanning more than one day
  329. $this->_addEvent(1, 1, "Programming", "2007-07-19 18:00", 600);
  330. $this->_addEvent(1, 2, "Programming", "2007-07-18 21:25", 500);
  331. // Deleted activity for project 2
  332. $this->_addEvent(2, 1, "Deleted Activity", "2007-07-10 09:25", 400);
  333. $this->_addEvent(2, 2, "Deleted Activity", "2007-07-11 09:25", 300);
  334. //// Project 1 QA
  335. $activity = $this->activities[1]["QA"];
  336. $activityId = $activity->getId();
  337. $results = $report->getEmployeeActivityTime(1, $activityId, "2007-07-05", "2007-07-25");
  338. $res = $this->_verifyEmployeeTime($results, 1, $activityId, array(1, 2), array(613, 570));
  339. $this->assertTrue($res);
  340. // Without period
  341. $results = $report->getEmployeeActivityTime(1, $activityId);
  342. $res = $this->_verifyEmployeeTime($results, 1, $activityId, array(1, 2), array(613, 570));
  343. $this->assertTrue($res);
  344. // Only from 07-10
  345. $results = $report->getEmployeeActivityTime(1, $activityId, "2007-07-10", "2007-07-25");
  346. $res = $this->_verifyEmployeeTime($results, 1, $activityId, array(1, 2), array(510, 500));
  347. $this->assertTrue($res);
  348. //// Project 1 Programming
  349. $activity = $this->activities[1]["Programming"];
  350. $activityId = $activity->getId();
  351. $results = $report->getEmployeeActivityTime(1, $activityId, "2007-07-05", "2007-07-25");
  352. $res = $this->_verifyEmployeeTime($results, 1, $activityId, array(1, 2), array(960, 2080));
  353. $this->assertTrue($res);
  354. // Without period
  355. $results = $report->getEmployeeActivityTime(1, $activityId);
  356. $res = $this->_verifyEmployeeTime($results, 1, $activityId, array(1, 2), array(960, 2080));
  357. $this->assertTrue($res);
  358. // Only from 07-10
  359. $results = $report->getEmployeeActivityTime(1, $activityId, "2007-07-10", "2007-07-25");
  360. $res = $this->_verifyEmployeeTime($results, 1, $activityId, array(1, 2), array(810, 1850));
  361. $this->assertTrue($res);
  362. // On day with spanning events
  363. $results = $report->getEmployeeActivityTime(1, $activityId, "2007-07-19", "2007-07-19");
  364. $res = $this->_verifyEmployeeTime($results, 1, $activityId, array(1, 2), array(360, 345));
  365. $this->assertTrue($res);
  366. //// Project 1 Support
  367. $activity = $this->activities[1]["Support"];
  368. $activityId = $activity->getId();
  369. $results = $report->getEmployeeActivityTime(1, $activityId, "2007-07-05", "2007-07-25");
  370. $this->assertTrue(is_array($results));
  371. $this->assertEquals(0, count($results));
  372. //// Project 2 Deleted Activity
  373. $activity = $this->activities[2]["Deleted Activity"];
  374. $activityId = $activity->getId();
  375. $results = $report->getEmployeeActivityTime(2, $activityId, "2007-07-10", "2007-07-11");
  376. $res = $this->_verifyEmployeeTime($results, 2, $activityId, array(1, 2), array(400, 300));
  377. $this->assertTrue($res);
  378. //// Project 2 QA
  379. $activity = $this->activities[2]["QA"];
  380. $activityId = $activity->getId();
  381. $results = $report->getEmployeeActivityTime(2, $activityId, "2007-07-05", "2007-07-25");
  382. $res = $this->_verifyEmployeeTime($results, 2, $activityId, array(1, 2), array(490, 300));
  383. $this->assertTrue($res);
  384. // Without period
  385. $results = $report->getEmployeeActivityTime(2, $activityId);
  386. $res = $this->_verifyEmployeeTime($results, 2, $activityId, array(1, 2), array(490, 300));
  387. $this->assertTrue($res);
  388. // Only from 07-10
  389. $results = $report->getEmployeeActivityTime(2, $activityId, "2007-07-10", "2007-07-25");
  390. $this->assertTrue(is_array($results));
  391. $this->assertEquals(0, count($results));
  392. //// Project 2 Programming
  393. $activity = $this->activities[2]["Programming"];
  394. $activityId = $activity->getId();
  395. $results = $report->getEmployeeActivityTime(2, $activityId, "2007-07-05", "2007-07-25");
  396. $res = $this->_verifyEmployeeTime($results, 2, $activityId, array(1, 2), array(800, 450));
  397. $this->assertTrue($res);
  398. // Without period
  399. $results = $report->getEmployeeActivityTime(2, $activityId);
  400. $res = $this->_verifyEmployeeTime($results, 2, $activityId, array(1, 2), array(800, 450));
  401. $this->assertTrue($res);
  402. // Only from 07-12
  403. $results = $report->getEmployeeActivityTime(2, $activityId, "2007-07-12", "2007-07-25");
  404. $res = $this->_verifyEmployeeTime($results, 2, $activityId, array(2), array(450));
  405. $this->assertTrue($res);
  406. }
  407. /**
  408. * test getEmployeeActivityTime() method
  409. */
  410. public function testGetEmployeeActivityTimePaging() {
  411. $report = new ProjectReport();
  412. // Add some extra employees
  413. for ($i=4; $i<30; $i++) {
  414. $empId = str_pad($i, 3, "0", STR_PAD_LEFT);
  415. $this->_addEmployee($i, $empId, "Test Employee $i", "First Name $i");
  416. }
  417. $times = array();
  418. $empIds = array();
  419. // Add events for all employees
  420. for ($i=1; $i<30; $i++) {
  421. $this->_addEvent(1, $i, "Programming", "2007-07-05 05:50", $i * 10);
  422. $this->_addEvent(1, $i, "Programming", "2007-07-12 15:25", $i * 5);
  423. $times[$i] = $i * 10 + $i * 5;
  424. }
  425. $activity = $this->activities[1]["Programming"];
  426. $activityId = $activity->getId();
  427. $sysConf = new sysConf();
  428. $pageSize = $sysConf->itemsPerPage;
  429. $timeValues = array_chunk($times, $pageSize);
  430. $empIds = array_chunk(range(1,29), $pageSize);
  431. for ($page = 0; $page < count($timeValues); $page++) {
  432. $results = $report->getEmployeeActivityTime(1, $activityId, "2007-07-05", "2007-07-12", $page + 1);
  433. $res = $this->_verifyEmployeeTime($results, 1, $activityId, $empIds[$page], $timeValues[$page]);
  434. $this->assertTrue($res);
  435. }
  436. }
  437. /**
  438. * Test for countEmployeesInActivity() method.
  439. */
  440. public function testCountEmployeesInActivity() {
  441. $report = new ProjectReport();
  442. $activity = $this->activities[1]["Programming"];
  443. $activityId = $activity->getId();
  444. $this->assertEquals(0, $report->countEmployeesInActivity(1, $activityId, "2007-07-05", "2007-07-12"));
  445. $this->_addEvent(1, 1, "Programming", "2007-07-05 09:25", 80);
  446. $this->_addEvent(1, 1, "Programming", "2007-07-07 08:12", 70);
  447. $this->_addEvent(1, 1, "Programming", "2007-07-10 09:25", 210);
  448. $this->assertEquals(1, $report->countEmployeesInActivity(1, $activityId, "2007-07-05", "2007-07-06"));
  449. $this->assertEquals(1, $report->countEmployeesInActivity(1, $activityId, "2007-07-05", "2007-07-12"));
  450. $this->_addEvent(1, 2, "Programming", "2007-07-25 09:25", 80);
  451. $this->assertEquals(1, $report->countEmployeesInActivity(1, $activityId, "2007-07-05", "2007-07-20"));
  452. $this->assertEquals(2, $report->countEmployeesInActivity(2, $activityId, "2007-07-05", "2007-07-30"));
  453. // Add some extra employees
  454. for ($i=1; $i<=30; $i++) {
  455. $id = $i + 3;
  456. $empId = str_pad($id, 3, "0", STR_PAD_LEFT);
  457. $this->_addEmployee($id, $empId, "Test Employee $id", "First Name $id");
  458. $this->_addEvent(1, $id, "Programming", "2007-07-05 05:50", $id * 10);
  459. $this->_addEvent(1, $id, "Programming", "2007-07-12 15:25", $id * 5);
  460. }
  461. $this->assertEquals(32, $report->countEmployeesInActivity(2, $activityId, "2007-07-05", "2007-07-30"));
  462. }
  463. //-------------------------------------------------------------------------
  464. // Private methods and their tests
  465. //-------------------------------------------------------------------------
  466. /**
  467. * Tests the _count method
  468. */
  469. public function testCount() {
  470. $this->_runQuery("TRUNCATE table hs_hr_project_activity");
  471. $this->assertEquals(0, $this->_count("hs_hr_project_activity"));
  472. $this->_runQuery("INSERT INTO hs_hr_project_activity(activity_id, project_id, name, deleted) " .
  473. "VALUES(1, 1, 'Programming', 0)");
  474. $this->assertEquals(1, $this->_count("hs_hr_project_activity"));
  475. $this->_runQuery("INSERT INTO hs_hr_project_activity(activity_id, project_id, name, deleted) " .
  476. "VALUES(2, 2, 'QA', 0)");
  477. $this->assertEquals(2, $this->_count("hs_hr_project_activity"));
  478. $this->_runQuery("INSERT INTO hs_hr_project_activity(activity_id, project_id, name, deleted) " .
  479. "VALUES(3, 2, 'Meetings', 0)");
  480. $this->assertEquals(3, $this->_count("hs_hr_project_activity"));
  481. $this->assertEquals(1, $this->_count("hs_hr_project_activity", "activity_id = 1"));
  482. $this->assertEquals(2, $this->_count("hs_hr_project_activity", "project_id = 2"));
  483. $this->assertEquals(1, $this->_count("hs_hr_project_activity", "name = 'Meetings'"));
  484. $this->assertEquals(0, $this->_count("hs_hr_project_activity", "name = 'Test'"));
  485. }
  486. /**
  487. * Tests the _addCustomer private method.
  488. */
  489. public function testAddCustomer() {
  490. $this->assertEquals(1, $this->_count("hs_hr_customer"));
  491. $this->customerId = 2;
  492. $this->customerName = "Qwerty";
  493. $this->_addCustomer();
  494. $this->assertEquals(2, $this->_count("hs_hr_customer"));
  495. $this->assertEquals(1, $this->_count("hs_hr_customer", "name = '{$this->customerName}'"));
  496. }
  497. /**
  498. * Tests the _addProjects method
  499. */
  500. public function testAddProjects() {
  501. $this->assertEquals(2, $this->_count("hs_hr_project"));
  502. $this->projects = array( 3 => "Project X", 4 => "Project Y");
  503. $this->_addProjects();
  504. $this->assertEquals(4, $this->_count("hs_hr_project"));
  505. $this->assertEquals(1, $this->_count("hs_hr_project", "name = 'Project X'"));
  506. $this->assertEquals(1, $this->_count("hs_hr_project", "name = 'Project Y'"));
  507. }
  508. /**
  509. * Tests the _addActivity method
  510. */
  511. public function testAddActivity() {
  512. $this->activities = null;
  513. $count = $this->_count("hs_hr_project_activity");
  514. $this->_addActivity(1, "Test1");
  515. $this->assertEquals(1, $this->_count("hs_hr_project_activity", "name = 'Test1' and deleted = 0"));
  516. $this->assertTrue(isset($this->activities[1]));
  517. $this->assertTrue(isset($this->activities[1]['Test1']));
  518. $this->_addActivity(1, "Test2", true);
  519. $this->assertEquals(1, $this->_count("hs_hr_project_activity", "name = 'Test2' and deleted = 1"));
  520. $this->assertTrue(isset($this->activities[1]['Test2']));
  521. $this->assertEquals($count + 2, $this->_count("hs_hr_project_activity"));
  522. }
  523. /**
  524. * Tests the _addEmployee method
  525. */
  526. public function testAddEmployee() {
  527. $count = $this->_count("hs_hr_employee");
  528. $this->_addEmployee(10, "0010", "Ranasinghe", "Ruwan");
  529. $this->assertEquals($count + 1, $this->_count("hs_hr_employee"));
  530. $this->assertEquals(1, $this->_count("hs_hr_employee", "emp_number = 10"));
  531. }
  532. /**
  533. * Tests the _addEvent method
  534. */
  535. public function testAddEvent() {
  536. $this->assertEquals(0, $this->_count("hs_hr_time_event"));
  537. $this->_addEvent(1, 1, "Programming", "2006-12-02 10:20", 19);
  538. $this->assertEquals(1, $this->_count("hs_hr_time_event"));
  539. $activity = $this->activities[1]['Programming'];
  540. $activityId = $activity->getId();
  541. $this->assertEquals(1, $this->_count("hs_hr_time_event", "activity_id = $activityId"));
  542. $this->assertEquals(1, $this->_count("hs_hr_time_event", "start_time BETWEEN '2006-12-02 10:20' AND '2006-12-02 10:39'"));
  543. }
  544. public function testGetNextId() {
  545. $this->assertEquals(2, $this->_getNextId("hs_hr_customer", "customer_id"));
  546. $this->customerId = 5;
  547. $this->customerName = "Qwerty";
  548. $this->_addCustomer();
  549. $this->assertEquals(6, $this->_getNextId("hs_hr_customer", "customer_id"));
  550. }
  551. /**
  552. * Test the CalculateDuration method
  553. */
  554. public function testCalculateDuration() {
  555. $this->assertEquals(3600, $this->_calculateDuration("2007-01-01 10:45", "2007-01-01 11:45"));
  556. $this->assertEquals(60, $this->_calculateDuration("2007-06-01 10:45", "2007-06-01 10:46"));
  557. }
  558. public function testGetEndTime() {
  559. $this->assertEquals("2007-01-01 11:45", $this->_getEndTime("2007-01-01 10:45", 3600));
  560. $this->assertEquals("2007-06-01 10:46", $this->_getEndTime("2007-06-01 10:45", 60));
  561. }
  562. /**
  563. * Tests the _getTimesheetId method
  564. */
  565. public function testGetTimesheetId() {
  566. // 2007-5-12 -> prev monday = 14, next sunday = 20
  567. $this->assertEquals(0, $this->_count("hs_hr_timesheet"));
  568. $id = $this->_getTimesheetId(1, "2007-5-15 10:10", "2007-5-15 11:20");
  569. $this->assertEquals(1, $this->_count("hs_hr_timesheet"));
  570. $this->assertEquals(1, $this->_count("hs_hr_timesheet", "DATE(start_date) = DATE('2007-05-14') AND DATE(end_date) = DATE('2007-05-20')"));
  571. // Add another timesheet for same period
  572. $id2 = $this->_getTimesheetId(1, "2007-5-16 10:00", "2007-5-16 11:20");
  573. $this->assertEquals(1, $this->_count("hs_hr_timesheet"));
  574. $this->assertEquals($id, $id2);
  575. // Add timesheet for different period
  576. $id3 = $this->_getTimesheetId(1, "2007-5-23 10:00", "2007-5-23 11:20");
  577. $this->assertEquals(2, $this->_count("hs_hr_timesheet"));
  578. $this->assertNotEquals($id, $id3);
  579. }
  580. /**
  581. * Tests the _verifyActivityTime method
  582. */
  583. public function testVerifyActivityTime() {
  584. $results[] = new ProjectActivityTime(1, "Programming", 3600, 1);
  585. $this->assertTrue($this->_verifyActivityTime($results, 1, array("Programming"), array(3600/60)));
  586. // Wrong name
  587. $this->assertFalse($this->_verifyActivityTime($results, 1, array("QA"), array(3600/60)));
  588. // Wrong time
  589. $this->assertFalse($this->_verifyActivityTime($results, 1, array("Programming"), array(120)));
  590. // Wrong project id
  591. $this->assertTrue($this->_verifyActivityTime($results, 2, array("Programming"), array(3600/60)));
  592. $results = array();
  593. $results[] = new ProjectActivityTime(1, "Programming", 4800, 1);
  594. $results[] = new ProjectActivityTime(2, "QA", 2400, 1);
  595. $results[] = new ProjectActivityTime(3, "Support", 1800, 1);
  596. $this->assertTrue($this->_verifyActivityTime($results, 1, array("QA", "Programming", "Support"),
  597. array(2400/60, 4800/60, 1800/60)));
  598. // One time incorrect
  599. $this->assertFalse($this->_verifyActivityTime($results, 1, array("QA", "Programming", "Support"),
  600. array(2400/60, 4800/60, 2400/60)));
  601. // All results not given
  602. $this->assertFalse($this->_verifyActivityTime($results, 1, array("QA", "Programming"),
  603. array(2400/60, 4800/60)));
  604. // Wrong project Id
  605. $this->assertFalse($this->_verifyActivityTime($results, 2, array("QA", "Programming", "Support"),
  606. array(2400/60, 4800/60, 2400/60)));
  607. // One activity wrong
  608. $this->assertFalse($this->_verifyActivityTime($results, 1, array("QA", "Program", "Support"),
  609. array(2400/60, 4800/60, 2400/60)));
  610. $this->assertFalse($this->_verifyActivityTime($results, 1, array("QA", "Misc", "Support"),
  611. array(2400/60, 4800/60, 2400/60)));
  612. // Extra activity given
  613. $this->assertFalse($this->_verifyActivityTime($results, 1, array("QA", "Programming", "Support", "Misc"),
  614. array(2400/60, 4800/60, 1800/60, 1800/60)));
  615. }
  616. /**
  617. * Tests the _verifyEmployeeTime method
  618. */
  619. public function testVerifyEmployeeTime() {
  620. $activity = $this->activities[1]["QA"];
  621. $activityId = $activity->getId();
  622. $results = array(new EmployeeActivityTime(1, "John", "Rajasinghe", $activityId, "QA", 250 * 60));
  623. $this->assertTrue($this->_verifyEmployeeTime($results, 1, $activityId, array(1), array(250)));
  624. // Wrong employee
  625. $this->assertFalse($this->_verifyEmployeeTime($results, 3, $activityId + 1, array(1), array(250)));
  626. // Wrong activity
  627. $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId + 1, array(1), array(250)));
  628. // Wrong time
  629. $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId, array(1), array(252)));
  630. // Activity ID and name not matching in results
  631. $results = array(new EmployeeActivityTime(1, "John", "Rajasinghe", $activityId, "Programming", 250 * 60));
  632. $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId , array(1), array(250)));
  633. // EmployeeId and first name does not match in results
  634. $results = array(new EmployeeActivityTime(1, "Ravi", "Rajasinghe", $activityId, "QA", 250 * 60));
  635. $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId , array(1), array(250)));
  636. // EmployeeId and last name does not match in results
  637. $results = array(new EmployeeActivityTime(1, "John", "Rajapaksha", $activityId, "QA", 250 * 60));
  638. $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId , array(1), array(250)));
  639. $activity = $this->activities[1]["Programming"];
  640. $activityId = $activity->getId();
  641. $results = array();
  642. $results[] = new EmployeeActivityTime(1, "John", "Rajasinghe", $activityId, "Programming", 480 * 60);
  643. $results[] = new EmployeeActivityTime(2, "Ravi", "Wickramasinghe", $activityId, "Programming", 400 * 60);
  644. $results[] = new EmployeeActivityTime(3, "Rasanga", "Kumarasinghe", $activityId, "Programming", 800 * 60);
  645. $this->assertTrue($this->_verifyEmployeeTime($results, 1, $activityId, array(1,2,3), array(480, 400, 800)));
  646. // One time incorrect
  647. $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId, array(1,2,3), array(480, 400, 801)));
  648. // Extra employee in results
  649. $this->_addEmployee(4, "004", "Sam", "Samarasinghe");
  650. $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId, array(1,2,3,4), array(480, 400, 800, 200)));
  651. // Wrong activity id
  652. $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId + 1, array(1,2,3), array(480, 400, 800)));
  653. // One employee missing
  654. $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId, array(1,2), array(480, 400)));
  655. // One activity wrong
  656. $results = array();
  657. $results[] = new EmployeeActivityTime(1, "John", "Rajasinghe", $activityId, "Programming", 480 * 60);
  658. $results[] = new EmployeeActivityTime(2, "Ravi", "Wickramasinghe", $activityId, "QA", 400 * 60);
  659. $results[] = new EmployeeActivityTime(3, "Rasanga", "Kumarasinghe", $activityId, "Programming", 800 * 60);
  660. $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId, array(1,2,3), array(480, 400, 800)));
  661. // Wrong name for one employee in results
  662. $results = array();
  663. $results[] = new EmployeeActivityTime(1, "John", "Rajasinghe", $activityId, "Programming", 480 * 60);
  664. $results[] = new EmployeeActivityTime(2, "Ravi", "Ranasinghe", $activityId, "QA", 400 * 60);
  665. $results[] = new EmployeeActivityTime(3, "Rasanga", "Kumarasinghe", $activityId, "Programming", 800 * 60);
  666. $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId, array(1,2,3), array(480, 400, 800)));
  667. }
  668. /**
  669. * Add customer used by unit test
  670. */
  671. private function _addCustomer() {
  672. $template = "INSERT INTO hs_hr_customer(customer_id, name, description, deleted) VALUES(%d, '%s', '%s', 0)";
  673. $sql = sprintf($template, $this->customerId, $this->customerName, "Desc. " . $this->customerName);
  674. $this->_runQuery($sql);
  675. }
  676. /**
  677. * Add projects used by unit test.
  678. */
  679. private function _addProjects() {
  680. $template = "INSERT INTO hs_hr_project(project_id, customer_id, name, description, deleted) " .
  681. "VALUES(%d, %d, '%s', '%s', 0)";
  682. $id = $this->_count("hs_hr_project") + 1;
  683. foreach ($this->projects as $project) {
  684. $sql = sprintf($template, $id, $this->customerId, $project, "Desc. " . $project);
  685. $this->_runQuery($sql);
  686. $id++;
  687. }
  688. }
  689. /**
  690. * Add an activity to the database and saves it in the $this->activities
  691. * array
  692. *
  693. * @param int $projectId The project Id
  694. * @param string $name The Activity name
  695. * @param bool $deleted Create activity in deleted state
  696. *
  697. * @return ProjectActivity The activity object that was created.
  698. */
  699. private function _addActivity($projectId, $name, $deleted = false) {
  700. $activity = new ProjectActivity();
  701. $activity->setName($name);
  702. $activity->setProjectId($projectId);
  703. $activity->setDeleted($deleted);
  704. $activity->save();
  705. $this->activities[$projectId][$name] = $activity;
  706. return $activity;
  707. }
  708. /**
  709. * Add employee
  710. */
  711. private function _addEmployee($empNumber, $empId, $lastName, $firstName) {
  712. $sql = sprintf("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname) " .
  713. "VALUES(%d, '%s', '%s', '%s')", $empNumber, $empId, $lastName, $firstName);
  714. $this->_runQuery($sql);
  715. }
  716. /**
  717. * Insert event into database.
  718. */
  719. private function _addEvent($projectId, $employeeId, $activityName, $startTime, $duration) {
  720. // Get activity Id
  721. $this->assertTrue(isset($this->activities[$projectId]));
  722. $this->assertTrue(isset($this->activities[$projectId][$activityName]));
  723. $activity = $this->activities[$projectId][$activityName];
  724. $timeEventId = $this->_getNextId("hs_hr_time_event", "time_event_id");
  725. $durationInSec = $duration * 60;
  726. $endTime = $this->_getEndTime($startTime, $durationInSec);
  727. $activityId = $activity->getId();
  728. $timesheetId = $this->_getTimesheetId($employeeId, $startTime, $endTime);
  729. $reportedDate = date("Y-m-d");
  730. $description = "Desc $activityName";
  731. $sql = sprintf("INSERT INTO hs_hr_time_event(time_event_id, project_id, activity_id, employee_id, timesheet_id, " .
  732. "start_time, end_time, reported_date, duration, description) " .
  733. "VALUES(%d, %d, %d, %d, %d, '%s', '%s', '%s', %d, '%s')",
  734. $timeEventId, $projectId, $activityId, $employeeId, $timesheetId, $startTime, $endTime,
  735. $reportedDate, $durationInSec, "activity $activityName");
  736. $this->_runQuery($sql);
  737. }
  738. /**
  739. * Calculate the duration between the given dates in seconds
  740. *
  741. * @param string start date
  742. * @param string end date
  743. *
  744. * @return int duration in seconds
  745. */
  746. private function _calculateDuration($startDate, $endDate) {
  747. $start = strtotime($startDate);
  748. $end = strtotime($endDate);
  749. $duration = $end - $start;
  750. return $duration;
  751. }
  752. /**
  753. * Calculates the end time
  754. * @param string $startTime The start time
  755. * @param int $duration The duration (in seconds)
  756. * @return string The end time.
  757. */
  758. private function _getEndTime($startTime, $duration) {
  759. $start = strtotime($startTime);
  760. $end = $start + $duration;
  761. $endTime = date('Y-m-d H:i', $end);
  762. return $endTime;
  763. }
  764. /**
  765. * Gets the timesheet id for the given employee for the given period.
  766. * If no timesheet is present, creates one
  767. *
  768. * @param int $employeeId Employee id
  769. * @param string $startTime Start time
  770. * @param string $endTime End time
  771. *
  772. * @return int Timesheet id
  773. */
  774. private function _getTimesheetId($employeeId, $startTime, $endTime) {
  775. $start = strtotime($startTime);
  776. $periodStart = strtotime("last Monday", $start);
  777. $periodStartDay = date('Y-m-d', $periodStart);
  778. $id = null;
  779. $sql = "SELECT timesheet_id FROM hs_hr_timesheet WHERE employee_id = $employeeId AND " .
  780. " DATE(start_date) = DATE('$periodStartDay')";
  781. $result = $this->_runQuery($sql);
  782. if (mysql_num_rows($result) > 0) {
  783. $row = mysql_fetch_array($result, MYSQL_NUM);
  784. $id = $row[0];
  785. } else {
  786. $periodEndDay = date('Y-m-d', strtotime("next Sunday", $start));
  787. $id = $this->_getNextId("hs_hr_timesheet", "timesheet_id");
  788. $sql = sprintf("INSERT INTO hs_hr_timesheet(timesheet_id, employee_id, timesheet_period_id, start_date, " .
  789. "end_date, status, comment) VALUES(%d, %d, %d, '%s', '%s', %d, '%s')",
  790. $id, $employeeId, 1, $periodStartDay, $periodEndDay, Timesheet::TIMESHEET_STATUS_NOT_SUBMITTED,
  791. "comment");
  792. $this->_runQuery($sql);
  793. }
  794. return $id;
  795. }
  796. /**
  797. * Verifies that the results contain the given activities and times
  798. *
  799. * @param array $results Array ProjectActivityTime objects
  800. * @param string $projectId project id
  801. * @param array $activities array of activity names expected
  802. * @param array $times array of times values expected (in minutes)
  803. *
  804. * @return bool false if verification failed, true otherwise
  805. */
  806. private function _verifyActivityTime($results, $projectId, $activities, $times) {
  807. // Verify number
  808. if (count($results) != count($activities)) {
  809. return false;
  810. }
  811. foreach ($results as $activityTime) {
  812. // find in activities.
  813. $activityName = $activityTime->getActivityName();
  814. $index = array_search($activityName, $activities);
  815. // if not found, return false
  816. if (($index === false) || ($index === null)) {
  817. return false;
  818. }
  819. // if found, check time
  820. $time = $times[$index];
  821. // if time matches, remove from activities
  822. if ($activityTime->getActivityTime() == ($time * 60)) {
  823. unset($activities[$index]);
  824. unset($times[$index]);
  825. } else {
  826. return false;
  827. }
  828. }
  829. // see if any activities left, if so return false
  830. if ((count($activities) != 0) || (count($times) != 0)) {
  831. return false;
  832. }
  833. return true;
  834. }
  835. /**
  836. * Verifies the results contain the given employees and times
  837. *
  838. * @param array $results The results array
  839. * @param string $projectId The project Id
  840. * @param string $activityId Activity Id
  841. * @param array $empIds Employee Ids
  842. * @param array $times Activity times
  843. *
  844. * @return true if results match or false if not
  845. */
  846. private function _verifyEmployeeTime($results, $projectId, $activityId, $empIds, $times) {
  847. // Verify number
  848. if (count($results) != count($empIds)) {
  849. return false;
  850. }
  851. foreach ($results as $empActivityTime) {
  852. // find emp number in list
  853. $empNumber = $empActivityTime->getEmpNumber();
  854. $index = array_search($empNumber, $empIds);
  855. // if not found, return false
  856. if (($index === false) || ($index === null)) {
  857. return false;
  858. }
  859. // check activity id
  860. if ($activityId != $empActivityTime->getActivityId()) {
  861. return false;
  862. }
  863. // check activity name match
  864. $activityName = $empActivityTime->getActivityName();
  865. if (!isset($this->projects[$projectId][$activityName])) {
  866. return false;
  867. }
  868. $activity = $this->activities[$projectId][$activityName];
  869. if ($activity->getId() != $activityId) {
  870. return false;
  871. }
  872. // Check employee details
  873. $empCountWhere = sprintf("emp_number=%d AND emp_firstname='%s' AND emp_lastname='%s'",
  874. $empActivityTime->getEmpNumber(), $empActivityTime->getFirstName(),
  875. $empActivityTime->getLastName());
  876. if ($this->_count("hs_hr_employee", $empCountWhere) != 1) {
  877. return false;
  878. }
  879. // Check time
  880. $time = $times[$index];
  881. if ($empActivityTime->getActivityTime() == ($time * 60)) {
  882. unset($empIds[$index]);
  883. unset($times[$index]);
  884. } else {
  885. return false;
  886. }
  887. }
  888. // see if any activities left, if so return false
  889. if ((count($empIds) != 0) || (count($times) != 0)) {
  890. return false;
  891. }
  892. return true;
  893. }
  894. /**
  895. * Truncates tables used in test.
  896. */
  897. private function _truncateTables() {
  898. $this->_runQuery("TRUNCATE TABLE `hs_hr_project_activity`");
  899. $this->_runQuery("TRUNCATE TABLE `hs_hr_time_event`");
  900. $this->_runQuery("TRUNCATE TABLE `hs_hr_project`");
  901. $this->_runQuery("TRUNCATE TABLE `hs_hr_customer`");
  902. $this->_runQuery("TRUNCATE TABLE `hs_hr_timesheet`");
  903. $this->_runQuery("TRUNCATE TABLE `hs_hr_employee`");
  904. }
  905. /**
  906. * Counts rows in the given table with optional where clause
  907. *
  908. * @param string $table The table name
  909. * @param string $where The where clause
  910. * @return int number of rows
  911. */
  912. private function _count($table, $where = null) {
  913. $sql = "SELECT COUNT(*) FROM " . $table;
  914. if (!empty($where)) {
  915. $sql .= " WHERE " . $where;
  916. }
  917. $result = $this->_runQuery($sql);
  918. $row = mysql_fetch_array($result, MYSQL_NUM);
  919. $count = $row[0];
  920. return $count;
  921. }
  922. /**
  923. * Return the next Id for the given field in
  924. * the given table
  925. *
  926. * @param string $table Table name
  927. * @param string $field Field name
  928. * @return int Next Id
  929. */
  930. private function _getNextId($table, $field) {
  931. $sql = "SELECT MAX($field) FROM $table";
  932. $result = $this->_runQuery($sql);
  933. $row = mysql_fetch_array($result, MYSQL_NUM);
  934. $nextId = $row[0] + 1;
  935. return $nextId;
  936. }
  937. /**
  938. * Runs the given sql query verifies and returns the
  939. * result.
  940. */
  941. private function _runQuery($sql) {
  942. $result = mysql_query($sql, $this->connection);
  943. $this->assertTrue($result !== false, mysql_error());
  944. return $result;
  945. }
  946. }
  947. // Call ProjectReportTest::main() if this source file is executed directly.
  948. if (PHPUnit_MAIN_METHOD == "ProjectReportTest::main") {
  949. ProjectReportTest::main();
  950. }
  951. ?>