PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/models/hrfunct/LocationHistoryTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 570 lines | 354 code | 107 blank | 109 comment | 10 complexity | 28a0bd0f4b6496b7ef741e79c369d97e 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 LocationHistoryTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "LocationHistoryTest::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 'LocationHistory.php';
  14. /**
  15. * Test class for LocationHistory.
  16. * Generated by PHPUnit_Util_Skeleton on 2008-03-11 at 20:06:47.
  17. */
  18. class LocationHistoryTest extends PHPUnit_Framework_TestCase {
  19. private $subDivisionHistory;
  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("LocationHistoryTest");
  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 locations
  43. $this->_runQuery("INSERT INTO hs_hr_location(loc_code, loc_name, loc_country, loc_add, loc_zip) " .
  44. "VALUES('LOC001', 'Kandy', 'LK', '111 Main street', '20000')");
  45. $this->_runQuery("INSERT INTO hs_hr_location(loc_code, loc_name, loc_country, loc_add, loc_zip) " .
  46. "VALUES('LOC002', 'Colombo', 'LK', '111 Main street', '20000')");
  47. $this->_runQuery("INSERT INTO hs_hr_location(loc_code, loc_name, loc_country, loc_add, loc_zip) " .
  48. "VALUES('LOC003', 'England', 'LK', '111 Main street', '20000')");
  49. $this->_runQuery("INSERT INTO hs_hr_location(loc_code, loc_name, loc_country, loc_add, loc_zip) " .
  50. "VALUES('LOC004', 'Japan', 'LK', '111 Main street', '20000')");
  51. // Insert employees
  52. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name) " .
  53. "VALUES(11, '0011', 'Rajasinghe', 'Saman', 'Marlon')");
  54. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name) " .
  55. "VALUES(12, '0022', 'Jayasinghe', 'Aruna', 'Shantha')");
  56. // location history for employee 11
  57. $this->subDivisionHistory[1] = $this->_getLocationHistory(11, 'LOC003', 'England', '-4 years', '-2 years');
  58. $this->subDivisionHistory[2] = $this->_getLocationHistory(11, 'LOC004', 'Japan', '-2 years', '-1 years');
  59. $this->subDivisionHistory[3] = $this->_getLocationHistory(11, 'LOC001', 'Kandy', '-1 years', '-1 month');
  60. $this->subDivisionHistory[4] = $this->_getLocationHistory(11, 'LOC002', 'Colombo', '-1 month', null);
  61. // location history for employee 12
  62. $this->subDivisionHistory[5] = $this->_getLocationHistory(12, 'LOC002', 'Colombo', '-5 years', '-2 year');
  63. $this->subDivisionHistory[6] = $this->_getLocationHistory(12, 'LOC003', 'Japan', '-2 years', null);
  64. foreach($this->subDivisionHistory as $his) {
  65. $this->_insertLocationHistory($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_location_history`");
  80. $this->_runQuery("TRUNCATE TABLE `hs_hr_compstructtree`");
  81. $this->_runQuery("TRUNCATE TABLE `hs_hr_employee`");
  82. $this->_runQuery("TRUNCATE TABLE `hs_hr_location`");
  83. }
  84. /**
  85. * Test case for updateHistory().
  86. */
  87. public function testUpdateHistory() {
  88. $history = new LocationHistory();
  89. // invalid emp number
  90. try {
  91. $history->updateHistory('ab1', 'LOC003');
  92. $this->fail("Exception expected");
  93. } catch (EmpHistoryException $e) {
  94. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  95. }
  96. // invalid location code
  97. try {
  98. $history->updateHistory(11, 'JOBA003');
  99. $this->fail("Exception expected");
  100. } catch (EmpHistoryException $e) {
  101. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  102. }
  103. // No change
  104. $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'LOC003' AND end_date IS NULL"));
  105. $before = $this->_getNumRows();
  106. $result = $history->updateHistory(12, 'LOC003');
  107. $this->assertFalse($result);
  108. $this->assertEquals($before, $this->_getNumRows());
  109. $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'LOC003' AND end_date IS NULL"));
  110. // Employee with 2 current items, verify allowed
  111. $this->_runQuery('UPDATE hs_hr_emp_location_history SET end_date = null WHERE id=' . $this->subDivisionHistory[3]->getId());
  112. $this->assertEquals(2, $this->_getNumRows("emp_number = 11 AND end_date IS NULL"));
  113. $before = $this->_getNumRows();
  114. // Update location already one of the current location - no change expected
  115. $result = $history->updateHistory(11, 'LOC001');
  116. $this->assertFalse($result);
  117. $this->assertEquals(2, $this->_getNumRows("emp_number = 11 AND end_date IS NULL"));
  118. $this->assertEquals($before, $this->_getNumRows());
  119. // Update new location, should be added to list of current locations
  120. $before = $this->_getNumRows();
  121. $result = $history->updateHistory(11, 'LOC003');
  122. $this->assertTrue($result);
  123. $this->assertEquals(3, $this->_getNumRows("emp_number = 11 AND end_date IS NULL"));
  124. $this->assertEquals($before + 1, $this->_getNumRows());
  125. // Change location
  126. $before = $this->_getNumRows();
  127. $result = $history->updateHistory(12, 'LOC001');
  128. $this->assertTrue($result);
  129. $this->assertEquals($before + 1, $this->_getNumRows());
  130. // Verify that existing current item's end date is not set (since multiple current items are allowed)
  131. $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'LOC003' AND end_date IS NULL"));
  132. $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'LOC001' AND end_date IS NULL"));
  133. // validate end date of old location not set
  134. $result = $this->_getMatchingRows('id = ' . $this->subDivisionHistory[6]->getId());
  135. $this->assertTrue(is_array($result));
  136. $this->assertEquals(1, count($result));
  137. $this->assertNull($result[0]['end_date']);
  138. // validate start date of new location correctly set
  139. $result = $this->_getMatchingRows("emp_number = 12 AND code = 'LOC001' AND end_date IS NULL");
  140. $this->assertTrue(is_array($result));
  141. $this->assertEquals(1, count($result));
  142. $this->assertNotNull($result[0]['start_date']);
  143. // Verify the start time is correct
  144. $startDate = $result[0]['start_date'];
  145. $this->assertTrue((time() - strtotime($startDate)) < 30);
  146. // Verify name is current
  147. $this->assertEquals('Kandy', $result[0]['name']);
  148. // Update history for employee with no current history items.
  149. $this->_runQuery('DELETE from hs_hr_emp_location_history');
  150. $this->assertEquals(0, $this->_getNumRows());
  151. $result = $history->updateHistory(12, 'LOC003');
  152. $this->assertTrue($result);
  153. $this->assertEquals(1, $this->_getNumRows());
  154. $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND code = 'LOC003' AND end_date IS NULL"));
  155. }
  156. /**
  157. * Test case for getHistory().
  158. */
  159. public function testGetHistory() {
  160. $history = new LocationHistory();
  161. // invalid emp number
  162. try {
  163. $list = $history->getHistory('A22');
  164. $this->fail("Exception expected");
  165. } catch (EmpHistoryException $e) {
  166. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  167. }
  168. // non existent emp number
  169. $list = $history->getHistory(14);
  170. $this->assertTrue(is_array($list));
  171. $this->assertEquals(0, count($list));
  172. // emp with 1 history item and one current items
  173. $list = $history->getHistory(12);
  174. $this->assertTrue(is_array($list));
  175. $this->assertEquals(1, count($list));
  176. $this->_compareHistory(array($this->subDivisionHistory[5]), $list);
  177. // emp with 3 history items and one current items
  178. $list = $history->getHistory(11);
  179. $this->assertTrue(is_array($list));
  180. $this->assertEquals(3, count($list));
  181. $this->_compareHistory(array($this->subDivisionHistory[1], $this->subDivisionHistory[2], $this->subDivisionHistory[3]), $list);
  182. // emp with 2 history items and 2 current items
  183. /*$this->_runQuery('UPDATE hs_hr_emp_location_history SET end_date = null WHERE id=' . $this->subDivisionHistory[3]->getId());
  184. $list = $history->getHistory(11);
  185. $this->assertTrue(is_array($list));
  186. $this->assertEquals(2, count($list));
  187. $this->_compareHistory(array($this->subDivisionHistory[1], $this->subDivisionHistory[2]), $list);*/
  188. // emp with 1 history item only
  189. $this->_runQuery('DELETE from hs_hr_emp_location_history WHERE emp_number = 12 AND end_date is null');
  190. $list = $history->getHistory(12);
  191. $this->assertTrue(is_array($list));
  192. $this->assertEquals(1, count($list));
  193. $this->_compareHistory(array($this->subDivisionHistory[5]), $list);
  194. // emp number with no history
  195. $this->_runQuery('DELETE from hs_hr_emp_location_history WHERE emp_number = 12');
  196. $list = $history->getHistory(14);
  197. $this->assertTrue(is_array($list));
  198. $this->assertEquals(0, count($list));
  199. }
  200. /**
  201. * Test delete() method
  202. */
  203. public function testDelete() {
  204. // find array of id's that are not available in database
  205. foreach ($this->subDivisionHistory as $hist) {
  206. $ids[] = $hist->getId();
  207. }
  208. $notIds = array_values(array_diff(range(1, 14), $ids));
  209. $before = $this->_getNumRows();
  210. $history = new LocationHistory();
  211. // invalid params
  212. try {
  213. $history->delete(34);
  214. $this->fail("Exception not thrown");
  215. } catch (EmpHistoryException $e) {
  216. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  217. }
  218. // invalid params
  219. try {
  220. $history->delete(array(1, 'w', 12));
  221. $this->fail("Exception not thrown");
  222. } catch (EmpHistoryException $e) {
  223. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  224. }
  225. // empty array
  226. $res = $history->delete(array());
  227. $this->assertEquals(0, $res);
  228. $this->assertEquals($before, $this->_getNumRows());
  229. // no matches
  230. $res = $history->delete(array($notIds[1], $notIds[4]));
  231. $this->assertEquals(0, $res);
  232. $this->assertEquals($before, $this->_getNumRows());
  233. // one match
  234. $res = $history->delete(array($ids[0], $notIds[3]));
  235. $this->assertEquals(1, $res);
  236. $this->assertEquals(1, $before - $this->_getNumRows());
  237. $before = $this->_getNumRows();
  238. // one more the rest
  239. $res = $history->delete(array($ids[2]));
  240. $this->assertEquals(1, $res);
  241. $this->assertEquals(1, $before - $this->_getNumRows());
  242. $before = $this->_getNumRows();
  243. // rest
  244. $res = $history->delete(array($ids[1], $ids[3], $ids[4], $ids[5]));
  245. $this->assertEquals(4, $res);
  246. $this->assertEquals(4, $before - $this->_getNumRows());
  247. $this->assertEquals(0, $this->_getNumRows());
  248. }
  249. /**
  250. * Test save() method
  251. */
  252. public function testSave() {
  253. // empNum missing
  254. $before = $this->_getNumRows();
  255. $history = $this->_getLocationHistory(null, 'LOC003', 'England', '-4 years', '-2 years');
  256. try {
  257. $history->save();
  258. $this->fail('Should throw exception');
  259. } catch (EmpHistoryException $e) {
  260. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  261. }
  262. $this->assertEquals($before, $this->_getNumRows());
  263. // code missing
  264. $history = $this->_getLocationHistory(11, null, 'England', '-4 years', '-2 years');
  265. try {
  266. $history->save();
  267. $this->fail('Should throw exception');
  268. } catch (EmpHistoryException $e) {
  269. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  270. }
  271. $this->assertEquals($before, $this->_getNumRows());
  272. // start time missing
  273. $history = $this->_getLocationHistory(11, 'LOC001', 'England', null, '-2 years');
  274. try {
  275. $history->save();
  276. $this->fail('Should throw exception');
  277. } catch (EmpHistoryException $e) {
  278. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  279. }
  280. $this->assertEquals($before, $this->_getNumRows());
  281. // Invalid emp number
  282. $history = $this->_getLocationHistory('X1', 'LOC001', 'England', '-3 years', '-2 years');
  283. try {
  284. $history->save();
  285. $this->fail('Should throw exception');
  286. } catch (EmpHistoryException $e) {
  287. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  288. }
  289. $this->assertEquals($before, $this->_getNumRows());
  290. // Invalid location code
  291. $history = $this->_getLocationHistory(11, 'DIV1', 'England', '-3 years', '-2 years');
  292. try {
  293. $history->save();
  294. $this->fail('Should throw exception');
  295. } catch (EmpHistoryException $e) {
  296. $this->assertEquals(EmpHistoryException::INVALID_PARAMETER, $e->getCode());
  297. }
  298. $this->assertEquals($before, $this->_getNumRows());
  299. // Start date greater than end date
  300. $history = $this->_getLocationHistory(11, 'LOC001', 'England', '-3 years', '-4 years');
  301. try {
  302. $history->save();
  303. $this->fail('Should throw exception');
  304. } catch (EmpHistoryException $e) {
  305. $this->assertEquals(EmpHistoryException::END_BEFORE_START, $e->getCode(), $e->getMessage());
  306. }
  307. $this->assertEquals($before, $this->_getNumRows());
  308. // new
  309. $before = $this->_getNumRows();
  310. $history = $this->_getLocationHistory(11, 'LOC001', 'England', '-4 years', '-3 years');
  311. $id = $history->save();
  312. $this->assertEquals(($before + 1), $this->_getNumRows());
  313. $this->assertEquals(1, $this->_getNumRows("emp_number = 11 AND id = $id AND code = 'LOC001'"));
  314. // update
  315. $before = $this->_getNumRows();
  316. $history = $this->_getLocationHistory(11, 'LOC004', 'England', '-7 years', '-5 years');
  317. $history->setId($id);
  318. $newId = $history->save();
  319. $this->assertEquals($id, $newId);
  320. $this->assertEquals($before, $this->_getNumRows());
  321. $this->assertEquals(1, $this->_getNumRows("emp_number = 11 AND id = $id AND code = 'LOC004'"));
  322. // update without location code
  323. $before = $this->_getNumRows();
  324. $history = $this->_getLocationHistory(11, null, 'England', '-7 years', '-5 years');
  325. $history->setId($id);
  326. try {
  327. $id = $history->save();
  328. $this->fail('Should throw exception');
  329. } catch (EmpHistoryException $e) {
  330. }
  331. $this->assertEquals($before, $this->_getNumRows());
  332. // Add second item of same type for same employee
  333. $this->assertEquals(0, $this->_getNumRows("emp_number = 12 AND code = '4'"));
  334. $before = $this->_getNumRows();
  335. $history = $this->_getLocationHistory(12, 'LOC004', 'England', '-6 years', '-2 years');
  336. $id = $history->save();
  337. $history = $this->_getLocationHistory(12, 'LOC004', 'England', '-4 years', '-2 years');
  338. $id = $history->save();
  339. $this->assertEquals($before + 2, $this->_getNumRows());
  340. $this->assertEquals(2, $this->_getNumRows("emp_number = 12 AND code = 'LOC004'"));
  341. // New item without end date allowed.
  342. $before = $this->_getNumRows();
  343. $history = $this->_getLocationHistory(12, 'LOC003', 'England', '-4 years', null);
  344. $id = $history->save();
  345. $this->assertEquals(($before + 1), $this->_getNumRows());
  346. $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND id = $id AND code = 'LOC003' AND name='England'"));
  347. // New item with name not set allowed. Verify correct name is taken from location table
  348. $before = $this->_getNumRows();
  349. $history = $this->_getLocationHistory(12, 'LOC002', null, '-4 years', '-1 years');
  350. $id = $history->save();
  351. $this->assertEquals(($before + 1), $this->_getNumRows());
  352. $this->assertEquals(1, $this->_getNumRows("emp_number = 12 AND id = $id AND code = 'LOC002' AND name='Colombo'"));
  353. // Update with name null, verify name updated.
  354. $before = $this->_getNumRows();
  355. $history = $this->_getLocationHistory(12, 'LOC002', null, '-4 years', '-1 years');
  356. $history->setId($id);
  357. $newId = $history->save();
  358. $this->assertEquals($id, $newId);
  359. $this->assertEquals($before, $this->_getNumRows());
  360. $result = $this->_getMatchingRows('id = ' . $newId);
  361. $this->assertTrue(is_array($result));
  362. $this->assertEquals(1, count($result));
  363. $this->assertEquals('Colombo', $result[0]['name']);
  364. }
  365. /**
  366. * Compares two arrays of history objects
  367. *
  368. * @param array $expected Expected
  369. * @param array $result Result
  370. */
  371. private function _compareHistory($expected, $result) {
  372. $this->assertEquals(count($expected), count($result));
  373. $i = 0;
  374. foreach ($result as $empLocation) {
  375. $this->assertTrue($empLocation instanceof LocationHistory, "Should return LocationHistory objects");
  376. $this->assertEquals($expected[$i], $empLocation);
  377. $i++;
  378. }
  379. }
  380. /**
  381. * Gets a location history object with the given parameters
  382. */
  383. private function _getLocationHistory($empNum, $subDivisionCode, $locationName, $startDate, $endDate) {
  384. if (!empty($startDate)) {
  385. $startDate = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime($startDate));
  386. }
  387. if (!empty($endDate)) {
  388. $endDate = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime($endDate));
  389. }
  390. $locationHis = new LocationHistory();
  391. $locationHis->setEmpNumber($empNum);
  392. $locationHis->setCode($subDivisionCode);
  393. $locationHis->setName($locationName);
  394. $locationHis->setStartDate($startDate);
  395. $locationHis->setEndDate($endDate);
  396. return $locationHis;
  397. }
  398. /**
  399. * Insert given location history item to the database
  400. */
  401. private function _insertLocationHistory(&$locationHistory) {
  402. $startDate = $locationHistory->getStartDate();
  403. $endDate = $locationHistory->getEndDate();
  404. $startDate = is_null($startDate) ? 'null' : "'{$startDate}'";
  405. $endDate = is_null($endDate) ? 'null' : "'{$endDate}'";
  406. $sql = sprintf("INSERT INTO hs_hr_emp_location_history(emp_number,code, name," .
  407. "start_date, end_date) VALUES (%d, '%s', '%s', %s, %s)",
  408. $locationHistory->getEmpNumber(), $locationHistory->getCode(),
  409. $locationHistory->getName(), $startDate,
  410. $endDate);
  411. $this->_runQuery($sql);
  412. $id = mysql_insert_id();
  413. $locationHistory->setId($id);
  414. }
  415. /**
  416. * Returns the number of rows in the hs_hr_emp_location_history table
  417. *
  418. * @param string $where where clause
  419. * @return int number of rows
  420. */
  421. private function _getNumRows($where = null) {
  422. $sql = "SELECT COUNT(*) FROM hs_hr_emp_location_history";
  423. if (!empty($where)) {
  424. $sql .= " WHERE " . $where;
  425. }
  426. $result = $this->_runQuery($sql);
  427. $row = mysql_fetch_array($result, MYSQL_NUM);
  428. $count = $row[0];
  429. return $count;
  430. }
  431. /**
  432. * Returns rows that match the given query from the database.
  433. *
  434. * @param string $where where clause
  435. * @return Array 2D associative array with results. Null if no matching results found
  436. */
  437. private function _getMatchingRows($where = null) {
  438. $sql = "SELECT * FROM hs_hr_emp_location_history";
  439. if (!empty($where)) {
  440. $sql .= " WHERE " . $where;
  441. }
  442. $list = null;
  443. $result = mysql_query($sql);
  444. while ($result && ($row = mysql_fetch_assoc($result))) {
  445. $list[] = $row;
  446. }
  447. return $list;
  448. }
  449. private function _runQuery($sql) {
  450. $result = mysql_query($sql);
  451. if ($result === false) {
  452. $error = mysql_error();
  453. $error .= 'SQL = ' . $sql;
  454. $this->fail($error);
  455. }
  456. return $result;
  457. }
  458. }
  459. // Call LocationHistoryTest::main() if this source file is executed directly.
  460. if (PHPUnit_MAIN_METHOD == "LocationHistoryTest::main") {
  461. LocationHistoryTest::main();
  462. }
  463. ?>