PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/models/hrfunct/JobTitleHistoryTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 555 lines | 344 code | 103 blank | 108 comment | 9 complexity | 12911e2bb54dce647bf58166a55502af 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 JobTitleHistoryTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "JobTitleHistoryTest::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/confs/Conf.php";
  10. require_once ROOT_PATH."/lib/confs/sysConf.php";
  11. require_once ROOT_PATH."/lib/common/LocaleUtil.php";
  12. require_once ROOT_PATH."/lib/common/UniqueIDGenerator.php";
  13. require_once 'JobTitleHistory.php';
  14. /**
  15. * Test class for JobTitleHistory.
  16. * Generated by PHPUnit_Util_Skeleton on 2008-03-11 at 20:06:47.
  17. */
  18. class JobTitleHistoryTest extends PHPUnit_Framework_TestCase {
  19. private $jobtitleHistory;
  20. /**
  21. * Runs the test methods of this class.
  22. *
  23. * @access public
  24. * @static
  25. */
  26. public static function main() {
  27. require_once "PHPUnit/TextUI/TestRunner.php";
  28. $suite = new PHPUnit_Framework_TestSuite("JobTitleHistoryTest");
  29. $result = PHPUnit_TextUI_TestRunner::run($suite);
  30. }
  31. /**
  32. * Sets up the fixture, for example, open a network connection.
  33. * This method is called before a test is executed.
  34. *
  35. * @access protected
  36. */
  37. protected function setUp() {
  38. $conf = new Conf();
  39. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  40. mysql_select_db($conf->dbname);
  41. $this->_deleteTables();
  42. // Insert job titles
  43. $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc, jobtit_comm, sal_grd_code) " .
  44. "VALUES('JOB001', 'Manager', 'Manager job title', 'no comments', null)");
  45. $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc, jobtit_comm, sal_grd_code) " .
  46. "VALUES('JOB002', 'Driver', 'Driver job title', 'no comments', null)");
  47. $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc, jobtit_comm, sal_grd_code) " .
  48. "VALUES('JOB003', 'Typist', 'Typist job title', 'no comments', null)");
  49. $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc, jobtit_comm, sal_grd_code) " .
  50. "VALUES('JOB004', 'Programmer', 'Software Programmer', 'no comments', null)");
  51. // Insert employees (managers)
  52. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, job_title_code) " .
  53. "VALUES(11, '0011', 'Rajasinghe', 'Saman', 'Marlon', 'JOB001')");
  54. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, job_title_code) " .
  55. "VALUES(12, '0022', 'Jayasinghe', 'Aruna', 'Shantha', 'JOB001')");
  56. // Job title history for employee 11
  57. $this->jobtitleHistory[1] = $this->_getJobTitleHistory(11, 'JOB003', 'Typist', '-4 years', '-2 years');
  58. $this->jobtitleHistory[2] = $this->_getJobTitleHistory(11, 'JOB004', 'Programmer', '-2 years', '-1 years');
  59. $this->jobtitleHistory[3] = $this->_getJobTitleHistory(11, 'JOB001', 'Manager', '-1 years', '-1 month');
  60. $this->jobtitleHistory[4] = $this->_getJobTitleHistory(11, 'JOB002', 'Driver', '-1 month', null);
  61. // Job title history for employee 12
  62. $this->jobtitleHistory[5] = $this->_getJobTitleHistory(12, 'JOB002', 'Driver', '-5 years', '-2 year');
  63. $this->jobtitleHistory[6] = $this->_getJobTitleHistory(12, 'JOB003', 'Programmer', '-2 years', null);
  64. foreach($this->jobtitleHistory as $his) {
  65. $this->_insertJobTitleHistory($his);
  66. }
  67. UniqueIDGenerator::getInstance()->resetIDs();
  68. }
  69. /**
  70. * Tears down the fixture, removed database entries created during test.
  71. *
  72. * @access protected
  73. */
  74. protected function tearDown() {
  75. $this->_deleteTables();
  76. UniqueIDGenerator::getInstance()->resetIDs();
  77. }
  78. private function _deleteTables() {
  79. $this->_runQuery("TRUNCATE TABLE `hs_hr_emp_jobtitle_history`");
  80. $this->_runQuery("TRUNCATE TABLE `hs_hr_job_title`");
  81. $this->_runQuery("TRUNCATE TABLE `hs_hr_employee`");
  82. }
  83. /**
  84. * Test case for updateHistory().
  85. */
  86. public function testUpdateHistory() {
  87. $history = new JobTitleHistory();
  88. // invalid emp number
  89. try {
  90. $history->updateHistory('ab1', 'JOB003');
  91. $this->fail("Exception expected");
  92. } catch (EmpHistoryException $e) {
  93. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  94. }
  95. // invalid job title code
  96. try {
  97. $history->updateHistory(11, 'JOBA003');
  98. $this->fail("Exception expected");
  99. } catch (EmpHistoryException $e) {
  100. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  101. }
  102. // No change
  103. $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'JOB003' AND end_date IS NULL"));
  104. $before = $this->_getNumRows();
  105. $result = $history->updateHistory(12, 'JOB003');
  106. $this->assertFalse($result);
  107. $this->assertEquals($before, $this->_getNumRows());
  108. $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'JOB003' AND end_date IS NULL"));
  109. // Employee with 2 current items, verify exception thrown
  110. $this->_runQuery('UPDATE hs_hr_emp_jobtitle_history SET end_date = null WHERE id=' . $this->jobtitleHistory[3]->getId());
  111. try {
  112. $result = $history->updateHistory(11, 'JOB001');
  113. $this->fail('Exception expected');
  114. } catch (EmpHistoryException $e) {
  115. $this->assertEquals(EmpHistoryException::MULTIPLE_CURRENT_ITEMS_NOT_ALLOWED, $e->getCode());
  116. }
  117. // Change job title
  118. $result = $history->updateHistory(12, 'JOB001');
  119. $this->assertTrue($result);
  120. $this->assertEquals($before + 1, $this->_getNumRows());
  121. $this->assertEquals(0, $this->_getNumRows("emp_number = 12 AND code = 'JOB002' AND end_date IS NULL"));
  122. $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'JOB001' AND end_date IS NULL"));
  123. // validate end date of old job correctly set
  124. $result = $this->_getMatchingRows('id = ' . $this->jobtitleHistory[6]->getId());
  125. $this->assertTrue(is_array($result));
  126. $this->assertEquals(1, count($result));
  127. $this->assertNotNull($result[0]['end_date']);
  128. // Verify the end time is correct
  129. $endDate = $result[0]['end_date'];
  130. $this->assertTrue((time() - strtotime($endDate)) < 30);
  131. // Verify name is current
  132. $this->assertEquals('Programmer', $result[0]['name']);
  133. // validate start date of new job correctly set
  134. $result = $this->_getMatchingRows("emp_number = 12 AND code = 'JOB001' AND end_date IS NULL");
  135. $this->assertTrue(is_array($result));
  136. $this->assertEquals(1, count($result));
  137. $this->assertNotNull($result[0]['start_date']);
  138. // Verify the start time is correct
  139. $startDate = $result[0]['start_date'];
  140. $this->assertTrue((time() - strtotime($startDate)) < 30);
  141. // Verify name is current
  142. $this->assertEquals('Manager', $result[0]['name']);
  143. // Update history for employee with no current history items.
  144. $this->_runQuery('DELETE from hs_hr_emp_jobtitle_history');
  145. $this->assertEquals(0, $this->_getNumRows());
  146. $result = $history->updateHistory(12, 'JOB003');
  147. $this->assertTrue($result);
  148. $this->assertEquals(1, $this->_getNumRows());
  149. $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'JOB003' AND end_date IS NULL"));
  150. }
  151. /**
  152. * Test case for getHistory().
  153. */
  154. public function testGetHistory() {
  155. $history = new JobTitleHistory();
  156. // invalid emp number
  157. try {
  158. $list = $history->getHistory('A22');
  159. $this->fail("Exception expected");
  160. } catch (EmpHistoryException $e) {
  161. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  162. }
  163. // non existent emp number
  164. $list = $history->getHistory(14);
  165. $this->assertTrue(is_array($list));
  166. $this->assertEquals(0, count($list));
  167. // emp with 1 history item and one current items
  168. $list = $history->getHistory(12);
  169. $this->assertTrue(is_array($list));
  170. $this->assertEquals(1, count($list));
  171. $this->_compareHistory(array($this->jobtitleHistory[5]), $list);
  172. // emp with 3 history items and one current items
  173. $list = $history->getHistory(11);
  174. $this->assertTrue(is_array($list));
  175. $this->assertEquals(3, count($list));
  176. $this->_compareHistory(array($this->jobtitleHistory[1], $this->jobtitleHistory[2], $this->jobtitleHistory[3]), $list);
  177. // emp with 2 history items and 2 current items
  178. /*$this->_runQuery('UPDATE hs_hr_emp_jobtitle_history SET end_date = null WHERE id=' . $this->jobtitleHistory[3]->getId());
  179. $list = $history->getHistory(11);
  180. $this->assertTrue(is_array($list));
  181. $this->assertEquals(2, count($list));
  182. $this->_compareHistory(array($this->jobtitleHistory[1], $this->jobtitleHistory[2]), $list);*/
  183. // emp with 1 history item only
  184. $this->_runQuery('DELETE from hs_hr_emp_jobtitle_history WHERE emp_number = 12 AND end_date is null');
  185. $list = $history->getHistory(12);
  186. $this->assertTrue(is_array($list));
  187. $this->assertEquals(1, count($list));
  188. $this->_compareHistory(array($this->jobtitleHistory[5]), $list);
  189. // emp number with no history
  190. $this->_runQuery('DELETE from hs_hr_emp_jobtitle_history WHERE emp_number = 12');
  191. $list = $history->getHistory(14);
  192. $this->assertTrue(is_array($list));
  193. $this->assertEquals(0, count($list));
  194. }
  195. /**
  196. * Test delete() method
  197. */
  198. public function testDelete() {
  199. // find array of id's that are not available in database
  200. foreach ($this->jobtitleHistory as $hist) {
  201. $ids[] = $hist->getId();
  202. }
  203. $notIds = array_values(array_diff(range(1, 14), $ids));
  204. $before = $this->_getNumRows();
  205. $history = new JobTitleHistory();
  206. // invalid params
  207. try {
  208. $history->delete(34);
  209. $this->fail("Exception not thrown");
  210. } catch (EmpHistoryException $e) {
  211. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  212. }
  213. // invalid params
  214. try {
  215. $history->delete(array(1, 'w', 12));
  216. $this->fail("Exception not thrown");
  217. } catch (EmpHistoryException $e) {
  218. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  219. }
  220. // empty array
  221. $res = $history->delete(array());
  222. $this->assertEquals(0, $res);
  223. $this->assertEquals($before, $this->_getNumRows());
  224. // no matches
  225. $res = $history->delete(array($notIds[1], $notIds[4]));
  226. $this->assertEquals(0, $res);
  227. $this->assertEquals($before, $this->_getNumRows());
  228. // one match
  229. $res = $history->delete(array($ids[0], $notIds[3]));
  230. $this->assertEquals(1, $res);
  231. $this->assertEquals(1, $before - $this->_getNumRows());
  232. $before = $this->_getNumRows();
  233. // one more the rest
  234. $res = $history->delete(array($ids[2]));
  235. $this->assertEquals(1, $res);
  236. $this->assertEquals(1, $before - $this->_getNumRows());
  237. $before = $this->_getNumRows();
  238. // rest
  239. $res = $history->delete(array($ids[1], $ids[3], $ids[4], $ids[5]));
  240. $this->assertEquals(4, $res);
  241. $this->assertEquals(4, $before - $this->_getNumRows());
  242. $this->assertEquals(0, $this->_getNumRows());
  243. }
  244. /**
  245. * Test save() method
  246. */
  247. public function testSave() {
  248. // empNum missing
  249. $before = $this->_getNumRows();
  250. $history = $this->_getJobTitleHistory(null, 'JOB003', 'Typist', '-4 years', '-2 years');
  251. try {
  252. $history->save();
  253. $this->fail('Should throw exception');
  254. } catch (EmpHistoryException $e) {
  255. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  256. }
  257. $this->assertEquals($before, $this->_getNumRows());
  258. // code missing
  259. $history = $this->_getJobTitleHistory(11, null, 'Typist', '-4 years', '-2 years');
  260. try {
  261. $history->save();
  262. $this->fail('Should throw exception');
  263. } catch (EmpHistoryException $e) {
  264. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  265. }
  266. $this->assertEquals($before, $this->_getNumRows());
  267. // start time missing
  268. $history = $this->_getJobTitleHistory(11, 'JOB001', 'Typist', null, '-2 years');
  269. try {
  270. $history->save();
  271. $this->fail('Should throw exception');
  272. } catch (EmpHistoryException $e) {
  273. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  274. }
  275. $this->assertEquals($before, $this->_getNumRows());
  276. // Invalid emp number
  277. $history = $this->_getJobTitleHistory('X1', 'JOB001', 'Typist', '-3 years', '-2 years');
  278. try {
  279. $history->save();
  280. $this->fail('Should throw exception');
  281. } catch (EmpHistoryException $e) {
  282. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  283. }
  284. $this->assertEquals($before, $this->_getNumRows());
  285. // Invalid job title code
  286. $history = $this->_getJobTitleHistory(11, '001', 'Typist', '-3 years', '-2 years');
  287. try {
  288. $history->save();
  289. $this->fail('Should throw exception');
  290. } catch (EmpHistoryException $e) {
  291. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  292. }
  293. $this->assertEquals($before, $this->_getNumRows());
  294. // Start date greater than end date
  295. $history = $this->_getJobTitleHistory(11, 'JOB001', 'Typist', '-3 years', '-4 years');
  296. try {
  297. $history->save();
  298. $this->fail('Should throw exception');
  299. } catch (EmpHistoryException $e) {
  300. $this->assertEquals(EmpHistoryException::END_BEFORE_START, $e->getCode(), $e->getMessage());
  301. }
  302. $this->assertEquals($before, $this->_getNumRows());
  303. // new
  304. $before = $this->_getNumRows();
  305. $history = $this->_getJobTitleHistory(11, 'JOB001', 'Typist', '-4 years', '-3 years');
  306. $id = $history->save();
  307. $this->assertEquals(($before + 1), $this->_getNumRows());
  308. $this->assertEquals(1, $this->_getNumRows("emp_number = 11 AND id = $id AND code = 'JOB001'"));
  309. // update
  310. $before = $this->_getNumRows();
  311. $history = $this->_getJobTitleHistory(11, 'JOB004', 'Typist', '-7 years', '-5 years');
  312. $history->setId($id);
  313. $newId = $history->save();
  314. $this->assertEquals($id, $newId);
  315. $this->assertEquals($before, $this->_getNumRows());
  316. $this->assertEquals(1, $this->_getNumRows("emp_number = 11 AND id = $id AND code = 'JOB004'"));
  317. // update without job title code
  318. $before = $this->_getNumRows();
  319. $history = $this->_getJobTitleHistory(11, null, 'Typist', '-7 years', '-5 years');
  320. $history->setId($id);
  321. try {
  322. $id = $history->save();
  323. $this->fail('Should throw exception');
  324. } catch (EmpHistoryException $e) {
  325. }
  326. $this->assertEquals($before, $this->_getNumRows());
  327. // Add second item of same type for same employee
  328. $this->assertEquals(0, $this->_getNumRows("emp_number = 12 AND code = 'JOB004'"));
  329. $before = $this->_getNumRows();
  330. $history = $this->_getJobTitleHistory(12, 'JOB004', 'Typist', '-6 years', '-2 years');
  331. $id = $history->save();
  332. $history = $this->_getJobTitleHistory(12, 'JOB004', 'Typist', '-4 years', '-2 years');
  333. $id = $history->save();
  334. $this->assertEquals($before + 2, $this->_getNumRows());
  335. $this->assertEquals(2, $this->_getNumRows("emp_number = 12 AND code = 'JOB004'"));
  336. // New item without end date allowed.
  337. $before = $this->_getNumRows();
  338. $history = $this->_getJobTitleHistory(12, 'JOB003', 'Typist', '-4 years', null);
  339. $id = $history->save();
  340. $this->assertEquals(($before + 1), $this->_getNumRows());
  341. $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND id = $id AND code = 'JOB003' AND name='Typist'"));
  342. // New item with name not set allowed. Verify correct name is taken from job title table
  343. $before = $this->_getNumRows();
  344. $history = $this->_getJobTitleHistory(12, 'JOB002', null, '-4 years', '-1 years');
  345. $id = $history->save();
  346. $this->assertEquals(($before + 1), $this->_getNumRows());
  347. $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND id = $id AND code = 'JOB002' AND name='Driver'"));
  348. // Update with name null, verify name updated.
  349. $before = $this->_getNumRows();
  350. $history = $this->_getJobTitleHistory(12, 'JOB002', null, '-4 years', '-1 years');
  351. $history->setId($id);
  352. $newId = $history->save();
  353. $this->assertEquals($id, $newId);
  354. $this->assertEquals($before, $this->_getNumRows());
  355. $result = $this->_getMatchingRows('id = ' . $newId);
  356. $this->assertTrue(is_array($result));
  357. $this->assertEquals(1, count($result));
  358. $this->assertEquals('Driver', $result[0]['name']);
  359. }
  360. /**
  361. * Compares two arrays of history objects
  362. *
  363. * @param array $expected Expected
  364. * @param array $result Result
  365. */
  366. private function _compareHistory($expected, $result) {
  367. $this->assertEquals(count($expected), count($result));
  368. $i = 0;
  369. foreach ($result as $empLocation) {
  370. $this->assertTrue($empLocation instanceof JobTitleHistory, "Should return JobTitleHistory objects");
  371. $this->assertEquals($expected[$i], $empLocation);
  372. $i++;
  373. }
  374. }
  375. /**
  376. * Gets a job title history object with the given parameters
  377. */
  378. private function _getJobTitleHistory($empNum, $jobtitleCode, $jobTitleName, $startDate, $endDate) {
  379. if (!empty($startDate)) {
  380. $startDate = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime($startDate));
  381. }
  382. if (!empty($endDate)) {
  383. $endDate = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime($endDate));
  384. }
  385. $jobHis = new JobTitleHistory();
  386. $jobHis->setEmpNumber($empNum);
  387. $jobHis->setCode($jobtitleCode);
  388. $jobHis->setName($jobTitleName);
  389. $jobHis->setStartDate($startDate);
  390. $jobHis->setEndDate($endDate);
  391. return $jobHis;
  392. }
  393. /**
  394. * Insert given job title history item to the database
  395. */
  396. private function _insertJobTitleHistory(&$jobTitleHistory) {
  397. $startDate = $jobTitleHistory->getStartDate();
  398. $endDate = $jobTitleHistory->getEndDate();
  399. $startDate = is_null($startDate) ? 'null' : "'{$startDate}'";
  400. $endDate = is_null($endDate) ? 'null' : "'{$endDate}'";
  401. $sql = sprintf("INSERT INTO hs_hr_emp_jobtitle_history(emp_number,code, name," .
  402. "start_date, end_date) VALUES (%d, '%s', '%s', %s, %s)",
  403. $jobTitleHistory->getEmpNumber(), $jobTitleHistory->getCode(),
  404. $jobTitleHistory->getName(), $startDate,
  405. $endDate);
  406. $this->_runQuery($sql);
  407. $id = mysql_insert_id();
  408. $jobTitleHistory->setId($id);
  409. }
  410. /**
  411. * Returns the number of rows in the hs_hr_emp_jobtitle_history table
  412. *
  413. * @param string $where where clause
  414. * @return int number of rows
  415. */
  416. private function _getNumRows($where = null) {
  417. $sql = "SELECT COUNT(*) FROM hs_hr_emp_jobtitle_history";
  418. if (!empty($where)) {
  419. $sql .= " WHERE " . $where;
  420. }
  421. $result = mysql_query($sql);
  422. $row = mysql_fetch_array($result, MYSQL_NUM);
  423. $count = $row[0];
  424. return $count;
  425. }
  426. /**
  427. * Returns rows that match the given query from the database.
  428. *
  429. * @param string $where where clause
  430. * @return Array 2D associative array with results. Null if no matching results found
  431. */
  432. private function _getMatchingRows($where = null) {
  433. $sql = "SELECT * FROM hs_hr_emp_jobtitle_history";
  434. if (!empty($where)) {
  435. $sql .= " WHERE " . $where;
  436. }
  437. $list = null;
  438. $result = mysql_query($sql);
  439. while ($result && ($row = mysql_fetch_assoc($result))) {
  440. $list[] = $row;
  441. }
  442. return $list;
  443. }
  444. private function _runQuery($sql) {
  445. $this->assertTrue(mysql_query($sql), mysql_error());
  446. }
  447. }
  448. // Call JobTitleHistoryTest::main() if this source file is executed directly.
  449. if (PHPUnit_MAIN_METHOD == "JobTitleHistoryTest::main") {
  450. JobTitleHistoryTest::main();
  451. }
  452. ?>