PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/models/time/WorkshiftTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 745 lines | 565 code | 105 blank | 75 comment | 10 complexity | 8b9f355f74a5d46f5194258de0d0dc18 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 WorkshiftTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "WorkshiftTest::main");
  5. }
  6. require_once "PHPUnit/Framework/TestCase.php";
  7. require_once "PHPUnit/Framework/TestSuite.php";
  8. require_once ROOT_PATH."/lib/confs/Conf.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/common/UniqueIDGenerator.php";
  12. require_once ROOT_PATH."/lib/models/time/Workshift.php";
  13. /**
  14. * Test class for Workshift.
  15. * Generated by PHPUnit_Util_Skeleton on 2007-09-03 at 14:23:03.
  16. */
  17. class WorkshiftTest extends PHPUnit_Framework_TestCase {
  18. /**
  19. * Runs the test methods of this class.
  20. *
  21. * @access public
  22. * @static
  23. */
  24. public static function main() {
  25. require_once "PHPUnit/TextUI/TestRunner.php";
  26. $suite = new PHPUnit_Framework_TestSuite("WorkshiftTest");
  27. $result = PHPUnit_TextUI_TestRunner::run($suite);
  28. }
  29. /**
  30. * Sets up the fixture, for example, open a network connection.
  31. * This method is called before a test is executed.
  32. *
  33. * @access protected
  34. */
  35. protected function setUp() {
  36. $conf = new Conf();
  37. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  38. mysql_select_db($conf->dbname);
  39. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee_workshift`", $this->connection));
  40. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_workshift`", $this->connection));
  41. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee`", $this->connection));
  42. // Insert a project and customer and employees for use in the test
  43. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name) " .
  44. "VALUES(1, '0011', 'Rajasinghe', 'Saman', 'Marlon')"));
  45. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name) " .
  46. "VALUES(2, '0022', 'Jayasinghe', 'Aruna', 'Shantha')"));
  47. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name) " .
  48. "VALUES(3, '0034', 'Ranasinghe', 'Nimal', 'Bandara')"));
  49. UniqueIDGenerator::getInstance()->resetIDs();
  50. }
  51. /**
  52. * Tears down the fixture, for example, close a network connection.
  53. * This method is called after a test is executed.
  54. *
  55. * @access protected
  56. */
  57. protected function tearDown() {
  58. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee_workshift`", $this->connection));
  59. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_workshift`", $this->connection));
  60. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee`", $this->connection));
  61. UniqueIDGenerator::getInstance()->resetIDs();
  62. }
  63. /**
  64. * Test case for testSave().
  65. */
  66. public function testSave() {
  67. $workshift = new Workshift();
  68. try {
  69. $workshift->save();
  70. $this->fail("No exception thrown");
  71. } catch (WorkshiftException $e) {
  72. $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode());
  73. }
  74. $workshift = new Workshift();
  75. $workshift->setName("Test shift");
  76. try {
  77. $workshift->save();
  78. $this->fail("No exception thrown");
  79. } catch (WorkshiftException $e) {
  80. $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode());
  81. }
  82. $workshift = new Workshift();
  83. $workshift->setHoursPerDay(12);
  84. try {
  85. $workshift->save();
  86. $this->fail("No exception thrown");
  87. } catch (WorkshiftException $e) {
  88. $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode());
  89. }
  90. $workshift = new Workshift();
  91. $workshift->setName("Test shift");
  92. $workshift->setHoursPerDay(-1);
  93. try {
  94. $workshift->save();
  95. $this->fail("No exception thrown");
  96. } catch (WorkshiftException $e) {
  97. $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode());
  98. }
  99. $workshift = new Workshift();
  100. $workshift->setName("Test shift");
  101. $workshift->setHoursPerDay(12);
  102. $workshift->save();
  103. $id = $workshift->getWorkshiftId();
  104. $this->assertNotNull($id);
  105. $this->assertEquals(1, $id);
  106. $workshift = $this->_getWorkshift($id);
  107. $this->assertTrue($workshift !== false);
  108. $this->assertEquals("Test shift", $workshift['name']);
  109. $this->assertEquals(12, $workshift['hours_per_day']);
  110. $this->assertEquals(1, $workshift['workshift_id']);
  111. // Update
  112. $workshift = new Workshift();
  113. $workshift->setName("Normal Shift");
  114. $workshift->setHoursPerDay(8);
  115. $workshift->setWorkshiftId(3);
  116. // Set invalid id exception should be thrown
  117. $affected = $workshift->save();
  118. $this->assertEquals(0, $affected);
  119. // valid id, verify that values have changed
  120. $workshift->setWorkshiftId(1);
  121. $affected = $workshift->save();
  122. $this->assertEquals(1, $affected);
  123. $updatedRow = $this->_getWorkshift(1);
  124. $this->assertEquals("Normal Shift", $updatedRow['name']);
  125. $this->assertEquals(8, $updatedRow['hours_per_day']);
  126. $workshift = new Workshift();
  127. $workshift->setWorkshiftId(1);
  128. try {
  129. $workshift->save();
  130. $this->fail("Workshift without name and hours per day saved");
  131. } catch (WorkshiftException $e) {
  132. $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode());
  133. }
  134. // Invalid hours per day
  135. $workshift = new Workshift();
  136. $workshift->setName("Invalid workshift");
  137. $workshift->setHoursPerDay("' {}' '");
  138. try {
  139. $workshift->save();
  140. $this->fail("Invalid hours per day allowed");
  141. } catch (WorkshiftException $e) {
  142. $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode());
  143. }
  144. $workshift = new Workshift();
  145. $workshift->setWorkshiftId(1);
  146. $workshift->setName("Invalid workshift");
  147. $workshift->setHoursPerDay("' {}' '");
  148. try {
  149. $workshift->save();
  150. $this->fail("Invalid hours per day allowed");
  151. } catch (WorkshiftException $e) {
  152. $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode());
  153. }
  154. // Test if sql injection works - update
  155. $workshift = new Workshift();
  156. $workshift->setWorkshiftId(1);
  157. $workshift->setName("sfdk'");
  158. $workshift->setHoursPerDay("22");
  159. $workshift->save();
  160. // check that the value is saved
  161. $updatedRow = $this->_getWorkshift(1);
  162. $this->assertEquals("sfdk'", $updatedRow['name']);
  163. $this->assertEquals(22, $updatedRow['hours_per_day']);
  164. // Test if sql injection works - save
  165. $workshift = new Workshift();
  166. $workshift->setName("eeee'");
  167. $workshift->setHoursPerDay("22");
  168. $workshift->save();
  169. $id = $workshift->getWorkshiftId();
  170. // check that the value is saved
  171. $updatedRow = $this->_getWorkshift($id);
  172. $this->assertEquals("eeee'", $updatedRow['name']);
  173. $this->assertEquals(22, $updatedRow['hours_per_day']);
  174. }
  175. /**
  176. * Test method for delete().
  177. */
  178. public function testDelete() {
  179. // Test for id not available
  180. $workshift = new Workshift();
  181. $workshift->setWorkshiftId(15);
  182. try {
  183. $workshift->delete();
  184. $this->fail("Non existing ID was not checked!");
  185. } catch (WorkshiftException $e) {
  186. $this->assertEquals(WorkshiftException::INVALID_ROW_COUNT, $e->getCode());
  187. }
  188. // Test for valid id
  189. $workshift = new Workshift();
  190. $workshift->setName("Delete Shift");
  191. $workshift->setHoursPerDay(7);
  192. $workshift->save();
  193. // Check the saving
  194. $id = $workshift->getWorkshiftId();
  195. $updatedRow = $this->_getWorkshift($id);
  196. $this->assertNotNull($updatedRow);
  197. // check whether the ID exists
  198. $workshift->delete();
  199. $id = $workshift->getWorkshiftId();
  200. $updatedRow = $this->_getWorkshift($id);
  201. $this->assertNull($updatedRow);
  202. // Empty id
  203. $workshift = new Workshift();
  204. try {
  205. $workshift->delete();
  206. $this->fail("Empty ID was not checked!");
  207. } catch (WorkshiftException $e) {
  208. $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());
  209. }
  210. // Invalid id
  211. $workshift = new Workshift();
  212. $workshift->setWorkshiftId("'fgW");
  213. try {
  214. $workshift->delete();
  215. $this->fail("Invalid ID was not checked!");
  216. } catch (WorkshiftException $e) {
  217. $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());
  218. }
  219. }
  220. /**
  221. * @todo Implement testAssignEmployees().
  222. */
  223. public function testAssignEmployees() {
  224. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'New Test Shift', '5')"));
  225. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Workshift 2', '10')"));
  226. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('3' , 'Workshift 3', '11')"));
  227. // Try to assign without valid workshift id, should throw an error
  228. $employees = array(1, 2, 3);
  229. $workshift = new Workshift();
  230. try {
  231. $workshift->assignEmployees($employees);
  232. $this->fail("Trying to assign employees without setting workshift id should throw exception");
  233. } catch (WorkshiftException $e) {
  234. $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());
  235. }
  236. // Assigning to non existing workshift, should not insert any rows
  237. $workshift->setWorkshiftId(4);
  238. $count = $workshift->assignEmployees($employees);
  239. $this->assertEquals(0, $count);
  240. $this->assertEquals(0, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));
  241. // Assign empty list of employees should be allowed
  242. $workshift->setWorkshiftId(1);
  243. $count = $workshift->assignEmployees(array());
  244. $this->assertEquals(0, $count);
  245. $this->assertEquals(0, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));
  246. // Assign valid employee list
  247. $employees = array(1, 3);
  248. $count = $workshift->assignEmployees($employees);
  249. $this->assertEquals(2, $count);
  250. $this->assertEquals(2, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));
  251. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 1)"));
  252. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 3)"));
  253. // reassigning already assigned employees shouldn't assign them again
  254. $employees = array(1, 2, 3);
  255. $count = $workshift->assignEmployees($employees);
  256. $this->assertEquals(1, $count);
  257. $this->assertEquals(3, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));
  258. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 1)"));
  259. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 2)"));
  260. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 3)"));
  261. // Passing same employee several times should not add duplicate entries
  262. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee_workshift`"));
  263. $employees = array(1, 3, 1, 1, 3);
  264. $count = $workshift->assignEmployees($employees);
  265. $this->assertEquals(2, $count);
  266. $this->assertEquals(2, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));
  267. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 1)"));
  268. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 3)"));
  269. // Invalid employee ID's should not be assigned.
  270. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee_workshift`"));
  271. $employees = array(1, -1, "')", 3);
  272. $count = $workshift->assignEmployees($employees);
  273. $this->assertEquals(2, $count);
  274. $this->assertEquals(2, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));
  275. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 1)"));
  276. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 3)"));
  277. // Non existing employee should not be added
  278. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee_workshift`"));
  279. $employees = array(1, 2, 4, 3, 5);
  280. $count = $workshift->assignEmployees($employees);
  281. $this->assertEquals(3, $count);
  282. $this->assertEquals(3, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));
  283. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 1)"));
  284. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 2)"));
  285. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 3)"));
  286. }
  287. /**
  288. * Test method for getWorkshiftForEmployee
  289. */
  290. public function testGetWorkshiftForEmployee() {
  291. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'New Test Shift', '5')"));
  292. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Workshift 2', '10')"));
  293. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('3' , 'Workshift 3', '11')"));
  294. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (1, 1)"));
  295. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (2, 2)"));
  296. // Invalid employee id
  297. try {
  298. Workshift::getWorkshiftForEmployee('sdf');
  299. $this->fail("Invalid employee number should throw exception");
  300. } catch (WorkshiftException $e) {
  301. $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());
  302. }
  303. // Get workshift for non-existant employee
  304. $this->assertNull(Workshift::getWorkshiftForEmployee(4));
  305. // Get workshift for employee without assigned workshift
  306. $this->assertNull(Workshift::getWorkshiftForEmployee(3));
  307. // Get workshift for employee with workshift assigned
  308. $shift = Workshift::getWorkshiftForEmployee(1);
  309. $this->assertNotNull($shift);
  310. $this->assertEquals("New Test Shift", $shift->getName());
  311. $this->assertEquals(5, $shift->getHoursPerDay());
  312. $this->assertEquals(1, $shift->getWorkshiftId());
  313. }
  314. /**
  315. * Test method for removeAssignedEmployees
  316. */
  317. public function testRemoveAssignedEmployees() {
  318. // Callling remove assigned employees without setting valid workshift id
  319. $workshift = new Workshift();
  320. try {
  321. $workshift->removeAssignedEmployees();
  322. $this->fail("Trying to remove assigned employees without setting workshift id should throw exception");
  323. } catch (WorkshiftException $e) {
  324. $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());
  325. }
  326. // remove assigned employees with non-existent workshift_id, shouldn't throw error
  327. $workshift = new Workshift();
  328. $workshift->setWorkshiftId(4);
  329. $count = $workshift->removeAssignedEmployees();
  330. $this->assertEquals(0, $count);
  331. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'New Test Shift', '5')"));
  332. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Workshift 2', '10')"));
  333. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('3' , 'Workshift 3', '11')"));
  334. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (1, 1)"));
  335. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (2, 2)"));
  336. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (2, 3)"));
  337. $count = $workshift->removeAssignedEmployees();
  338. $this->assertEquals(0, $count);
  339. // check no assignments are deleted
  340. $this->assertEquals(3, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));
  341. // Remove assigned employees when no employees are assigned - check no assignments are deleted
  342. $workshift->setWorkshiftId(3);
  343. $count = $workshift->removeAssignedEmployees();
  344. $this->assertEquals(0, $count);
  345. $this->assertEquals(3, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));
  346. // Remove assigned employees
  347. $workshift->setWorkshiftId(2);
  348. $count = $workshift->removeAssignedEmployees();
  349. $this->assertEquals(2, $count);
  350. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));
  351. $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "workshift_id = 1"));
  352. $workshift->setWorkshiftId(1);
  353. $count = $workshift->removeAssignedEmployees();
  354. $this->assertEquals(1, $count);
  355. $this->assertEquals(0, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));
  356. }
  357. /**
  358. * Test method for getEmployeesWithoutWorkshift
  359. */
  360. public function testGetEmployeesWithoutWorkshift() {
  361. $employees = Workshift::getEmployeesWithoutWorkshift();
  362. $expected[1] = array(1, '0011', 'Rajasinghe', 'Saman', 'Marlon');
  363. $expected[2] = array(2, '0022', 'Jayasinghe', 'Aruna', 'Shantha');
  364. $expected[3] = array(3, '0034', 'Ranasinghe', 'Nimal', 'Bandara');
  365. $this->assertEquals(3, count($employees));
  366. $this->_checkEmployeeList($employees, $expected);
  367. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'New Test Shift', '5')"));
  368. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Workshift 2', '10')"));
  369. $employees = Workshift::getEmployeesWithoutWorkshift();
  370. $this->assertEquals(3, count($employees));
  371. $this->_checkEmployeeList($employees, $expected);
  372. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (1, 1)"));
  373. $employees = Workshift::getEmployeesWithoutWorkshift();
  374. $this->assertEquals(2, count($employees));
  375. unset($expected[1]);
  376. $this->_checkEmployeeList($employees, $expected);
  377. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (2, 2)"));
  378. $employees = Workshift::getEmployeesWithoutWorkshift();
  379. $this->assertEquals(1, count($employees));
  380. unset($expected[2]);
  381. $this->_checkEmployeeList($employees, $expected);
  382. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (2, 3)"));
  383. $employees = Workshift::getEmployeesWithoutWorkshift();
  384. $this->assertEquals(0, count($employees));
  385. }
  386. /**
  387. * Test method for getAssignedEmployees
  388. */
  389. public function testGetAssignedEmployees() {
  390. $workshift = new Workshift();
  391. try {
  392. $workshift->getAssignedEmployees();
  393. $this->fail("Trying to fetch assigned employees without setting workshift id should throw exception");
  394. } catch (WorkshiftException $e) {
  395. $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());
  396. }
  397. // Workshift not in system
  398. $workshift->setWorkshiftId(1);
  399. $employees = $workshift->getAssignedEmployees();
  400. $this->assertEquals(0, count($employees));
  401. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'New Test Shift', '5')"));
  402. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Workshift 2', '10')"));
  403. // Workshift with no assigned employees
  404. $workshift->setWorkshiftId(2);
  405. $employees = $workshift->getAssignedEmployees();
  406. $this->assertEquals(0, count($employees));
  407. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (1, 1)"));
  408. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (2, 2)"));
  409. $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (2, 3)"));
  410. $expected[2] = array(2, '0022', 'Jayasinghe', 'Aruna', 'Shantha');
  411. $expected[3] = array(3, '0034', 'Ranasinghe', 'Nimal', 'Bandara');
  412. $employees = $workshift->getAssignedEmployees();
  413. $this->assertEquals(2, count($employees));
  414. $this->_checkEmployeeList($employees, $expected);
  415. $workshift->setWorkshiftId(1);
  416. $employees = $workshift->getAssignedEmployees();
  417. unset($expected);
  418. $expected[1] = array(1, '0011', 'Rajasinghe', 'Saman', 'Marlon');
  419. $this->assertEquals(1, count($employees));
  420. $this->_checkEmployeeList($employees, $expected);
  421. }
  422. /**
  423. * Test case for testGetWorkshifts().
  424. */
  425. public function testGetWorkshifts() {
  426. // No workshifts, should return empty array
  427. $workshifts = Workshift::getWorkshifts();
  428. $this->assertTrue(is_array($workshifts));
  429. $this->assertEquals(0, count($workshifts));
  430. // Only one workshift, should return array with one workshift
  431. $sql = "INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'New Test Shift', '5')";
  432. $this->assertTrue(mysql_query($sql));
  433. $workshifts = Workshift::getWorkshifts();
  434. $this->assertTrue(is_array($workshifts));
  435. $this->assertEquals(1, count($workshifts));
  436. $this->assertEquals('New Test Shift', $workshifts[0]->getName());
  437. $this->assertEquals(1, $workshifts[0]->getWorkshiftId());
  438. $this->assertEquals(5, $workshifts[0]->getHoursPerDay());
  439. // Many workshifts, should return array with all available workshifts
  440. $sql = "INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Workshift 2', '8')";
  441. $this->assertTrue(mysql_query($sql));
  442. $sql = "INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('3' , 'Workshift 3', '6')";
  443. $this->assertTrue(mysql_query($sql));
  444. $workshifts = Workshift::getWorkshifts();
  445. $this->assertTrue(is_array($workshifts));
  446. $this->assertEquals(3, count($workshifts));
  447. $ids = array(1, 2, 3);
  448. $names = array("New Test Shift", "Workshift 2", "Workshift 3");
  449. $hours = array(5, 8, 6);
  450. foreach ($workshifts as $workshift) {
  451. $id = $workshift->getWorkshiftId();
  452. $index = array_search($id, $ids);
  453. $this->assertTrue($index !== false);
  454. $this->assertEquals($names[$index], $workshift->getName());
  455. $this->assertEquals($hours[$index], $workshift->getHoursPerDay());
  456. unset($ids[$index]);
  457. unset($names[$index]);
  458. unset($hours[$index]);
  459. }
  460. }
  461. /**
  462. * Testcase for testGetWorkshift().
  463. */
  464. public function testGetWorkshift() {
  465. // invalid id
  466. try {
  467. $workshift = Workshift::getWorkshift(null);
  468. $this->fail("Exception not thrown");
  469. } catch (WorkshiftException $e) {
  470. $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());
  471. }
  472. // negative id
  473. try {
  474. $workshift = Workshift::getWorkshift(-1);
  475. $this->fail("Negative id!");
  476. } catch (WorkshiftException $e) {
  477. $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());
  478. }
  479. // try sql injection in id
  480. // id not found in database
  481. try {
  482. $workshift = Workshift::getWorkshift("'{}");
  483. $this->fail("Invalid ID!");
  484. } catch (WorkshiftException $e) {
  485. $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());
  486. }
  487. try {
  488. $workshift = Workshift::getWorkshift(16);
  489. } catch (WorkshiftException $e) {
  490. $this->assertEquals(WorkshiftException::WORKSHIFT_NOT_FOUND, $e->getCode());
  491. }
  492. // valid id
  493. $sql = "INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('3' , 'New Test Shift', '5')";
  494. $result = mysql_query($sql);
  495. $this->assertTrue($result);
  496. // Check whether the returned object is Workshift
  497. $workshift = Workshift::getWorkshift(3);
  498. $this->assertTrue($workshift instanceof Workshift);
  499. $this->assertEquals(3, $workshift->getWorkshiftId());
  500. $this->assertEquals("New Test Shift", $workshift->getName());
  501. $this->assertEquals(5, $workshift->getHoursPerDay());
  502. }
  503. /**
  504. * Test method for deleteWorkshifts().
  505. */
  506. public function testDeleteWorkshifts() {
  507. // Parameter is not an array
  508. try {
  509. Workshift::deleteWorkshifts(null);
  510. $this->fail("null parameter allowed");
  511. } catch (WorkshiftException $e) {
  512. $this->assertEquals(WorkshiftException::INVALID_PARAMETER, $e->getCode());
  513. }
  514. try {
  515. Workshift::deleteWorkshifts(2);
  516. $this->fail("integer parameter allowed");
  517. } catch (WorkshiftException $e) {
  518. $this->assertEquals(WorkshiftException::INVALID_PARAMETER, $e->getCode());
  519. }
  520. // Empty array
  521. $idArray = array();
  522. try {
  523. Workshift::deleteWorkshifts($idArray);
  524. $this->fail("Empty array allowed");
  525. } catch (WorkshiftException $e) {
  526. $this->assertEquals(WorkshiftException::INVALID_PARAMETER, $e->getCode());
  527. }
  528. // array contains invalid ids
  529. $idArray = array(1, 2, -1, 4);
  530. try {
  531. Workshift::deleteWorkshifts($idArray);
  532. $this->fail("Invalid id's allowed");
  533. } catch (WorkshiftException $e) {
  534. $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());
  535. }
  536. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'Work shift 1', '5')"));
  537. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Work shift 2', '5')"));
  538. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('3' , 'Work shift 3', '5')"));
  539. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('4' , 'Work shift 4', '5')"));
  540. // array contains id's not in database
  541. $idArray = array(1, 2, 23);
  542. Workshift::deleteWorkshifts($idArray);
  543. $this->assertEquals(2, $this->_countRows(Workshift::WORKSHIFT_TABLE));
  544. $this->assertEquals(2, $this->_countRows(Workshift::WORKSHIFT_TABLE, "workshift_id IN (3, 4)"));
  545. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_workshift`", $this->connection));
  546. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'Work shift 1', '5')"));
  547. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Work shift 2', '5')"));
  548. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('3' , 'Work shift 3', '5')"));
  549. $this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('4' , 'Work shift 4', '5')"));
  550. // array contains valid ids
  551. $idArray = array(1, 2, 3);
  552. Workshift::deleteWorkshifts($idArray);
  553. $this->assertEquals(1, $this->_countRows(Workshift::WORKSHIFT_TABLE));
  554. $row = $this->_getWorkshift(4);
  555. $this->assertNotNull($row);
  556. $this->assertEquals("Work shift 4", $row['name']);
  557. $this->assertEquals(5, $row['hours_per_day']);
  558. }
  559. private function _getWorkshift($id) {
  560. $sql = "SELECT workshift_id, name, hours_per_day FROM hs_hr_workshift WHERE workshift_id = $id ";
  561. $result = mysql_query($sql);
  562. $this->assertTrue($result !== false, mysql_error());
  563. $num = mysql_num_rows($result);
  564. if ($num == 0) {
  565. return null;
  566. } else if ($num == 1) {
  567. return mysql_fetch_array($result);
  568. } else {
  569. $this->fail("Two workshifts with same id");
  570. }
  571. }
  572. private function _countRows($table, $condition = null) {
  573. $sql = "SELECT COUNT(*) FROM $table";
  574. if (!empty($condition)) {
  575. $sql .= " WHERE $condition";
  576. }
  577. $result = mysql_query($sql);
  578. $this->assertTrue($result !== false);
  579. $this->assertEquals(1, mysql_num_rows($result));
  580. $row = mysql_fetch_array($result);
  581. $count = $row[0];
  582. return $count;
  583. }
  584. /**
  585. * Checks that the expected employees and only the expected employees
  586. * are in the given array of employees. Asserts if not
  587. */
  588. private function _checkEmployeeList($employees, $expected) {
  589. foreach($employees as $employee) {
  590. $empNumber = $employee['emp_number'];
  591. $this->assertTrue(array_key_exists($empNumber, $expected));
  592. $expectedVal = $expected[$empNumber];
  593. $this->assertEquals($expectedVal[1], $employee['employee_id']);
  594. $this->assertEquals($expectedVal[2], $employee['emp_lastname']);
  595. $this->assertEquals($expectedVal[3], $employee['emp_firstname']);
  596. $this->assertEquals($expectedVal[4], $employee['emp_middle_name']);
  597. unset($expected[$empNumber]);
  598. }
  599. }
  600. }
  601. // Call WorkshiftTest::main() if this source file is executed directly.
  602. if (PHPUnit_MAIN_METHOD == "WorkshiftTest::main") {
  603. WorkshiftTest::main();
  604. }
  605. ?>