PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/models/recruitment/JobApplicationEventTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 478 lines | 289 code | 73 blank | 116 comment | 4 complexity | 28e6518183d2e5d72f118dfdebc9381c 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 JobApplicationEventTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "JobApplicationEventTest::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/models/recruitment/JobApplication.php";
  12. require_once ROOT_PATH."/lib/models/recruitment/JobVacancy.php";
  13. require_once ROOT_PATH."/lib/common/UniqueIDGenerator.php";
  14. require_once ROOT_PATH."/lib/common/LocaleUtil.php";
  15. require_once 'JobApplicationEvent.php';
  16. /**
  17. * Test class for JobApplicationEvent.
  18. * Generated by PHPUnit_Util_Skeleton on 2008-03-03 at 22:58:56.
  19. */
  20. class JobApplicationEventTest extends PHPUnit_Framework_TestCase {
  21. /**
  22. * Runs the test methods of this class.
  23. *
  24. * @access public
  25. * @static
  26. */
  27. public static function main() {
  28. require_once "PHPUnit/TextUI/TestRunner.php";
  29. $suite = new PHPUnit_Framework_TestSuite("JobApplicationEventTest");
  30. $result = PHPUnit_TextUI_TestRunner::run($suite);
  31. }
  32. /**
  33. * Sets up the fixture, for example, open a network connection.
  34. * This method is called before a test is executed.
  35. *
  36. * @access protected
  37. */
  38. protected function setUp() {
  39. $conf = new Conf();
  40. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  41. mysql_select_db($conf->dbname);
  42. $this->_deleteTables();
  43. // Insert job titles
  44. $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc, jobtit_comm, sal_grd_code) " .
  45. "VALUES('JOB001', 'Manager', 'Manager job title', 'no comments', null)");
  46. $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc, jobtit_comm, sal_grd_code) " .
  47. "VALUES('JOB002', 'Driver', 'Driver job title', 'no comments', null)");
  48. $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc, jobtit_comm, sal_grd_code) " .
  49. "VALUES('JOB003', 'Typist', 'Typist job title', 'no comments', null)");
  50. $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc, jobtit_comm, sal_grd_code) " .
  51. "VALUES('JOB004', 'Programmer', 'Software Programmer', 'no comments', null)");
  52. // Insert employees (managers)
  53. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, job_title_code) " .
  54. "VALUES(11, '0011', 'Rajasinghe', 'Saman', 'Marlon', 'JOB001')");
  55. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, job_title_code, emp_work_email) " .
  56. "VALUES(12, '0022', 'Jayasinghe', 'Aruna', 'Shantha', 'JOB001', 'aruna@example.com')");
  57. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, job_title_code) " .
  58. "VALUES(13, '0042', 'Jayaweera', 'Nimal', 'T', 'JOB001')");
  59. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, job_title_code) " .
  60. "VALUES(14, '0044', 'Karunarathne', 'Jaya', 'S', 'JOB001')");
  61. $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, job_title_code) " .
  62. "VALUES(15, '0054', 'Ranasinghe', 'Kamal', 'Z', 'JOB001')");
  63. // Insert to hs_hr_users table
  64. $this->_runQuery("INSERT INTO `hs_hr_users`(id, user_name, emp_number) VALUES ('USR111','demo', 11)");
  65. $this->_runQuery("INSERT INTO `hs_hr_users`(id, user_name, emp_number) VALUES ('USR112','kamalr', 12)");
  66. $this->_runQuery("INSERT INTO `hs_hr_users`(id, user_name) VALUES ('USR113','adminhr')");
  67. // Insert Job Vacancies
  68. $this->_runQuery("INSERT INTO hs_hr_job_vacancy(vacancy_id, jobtit_code, manager_id, active, description) " .
  69. "VALUES(1, 'JOB001', 11, " . JobVacancy::STATUS_ACTIVE . ", 'Job vacancy 1')");
  70. $this->_runQuery("INSERT INTO hs_hr_job_vacancy(vacancy_id, jobtit_code, manager_id, active, description) " .
  71. "VALUES(2, 'JOB002', 11, " . JobVacancy::STATUS_INACTIVE . ", 'Job vacancy 2')");
  72. $this->_runQuery("INSERT INTO hs_hr_job_vacancy(vacancy_id, jobtit_code, manager_id, active, description) " .
  73. "VALUES(3, 'JOB003', 12, " . JobVacancy::STATUS_INACTIVE . ", 'Job vacancy 3')");
  74. // Insert Job Applications
  75. $application = $this->_getJobApplication(1, 1, 'Janaka', 'T', 'Kulathunga', '111 Main Street', 'Apt X2',
  76. 'Colombo', 'Western', '2222', 'Sri Lanka', '01121111121', '077282828282', 'janaka@example.com',
  77. 'aaa bbb', JobApplication::STATUS_SECOND_INTERVIEW_SCHEDULED);
  78. $application->setHiringManagerName('Saman Rajasinghe');
  79. $application->setJobTitleName('Manager');
  80. $this->jobApplications[1] = $application;
  81. $application = $this->_getJobApplication(2, 2, 'Kamal', 'S', 'Manawarathne', '222 Sea Street', 'Suite B2',
  82. 'Kandy', 'Central', '111111', 'England', '33211121', '079982828282', 'kamal@etst.com',
  83. 'asdfasdf', JobApplication::STATUS_FIRST_INTERVIEW_SCHEDULED);
  84. $application->setHiringManagerName('Saman Rajasinghe');
  85. $application->setJobTitleName('Driver');
  86. $this->jobApplications[2] = $application;
  87. $application = $this->_getJobApplication(3, 3, 'Ruwan', 'S', 'Nawarathne', '393 Hill Street', '#2',
  88. 'Nuwaraeliya', 'Central', '2333', 'Sri Lanka', '05121111121', '072282828282', 'rywab@sfmple.com',
  89. 'aaa sdf bbb', JobApplication::STATUS_SUBMITTED);
  90. $application->setHiringManagerName('Aruna Jayasinghe');
  91. $application->setJobTitleName('Typist');
  92. $this->jobApplications[3] = $application;
  93. $this->_createJobApplications($this->jobApplications);
  94. // Create job application events
  95. $createdTime = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime("-1 hours"));
  96. $eventTime = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime("+5 days"));
  97. // Events for first job application
  98. $this->_createEvent(1, 1, $createdTime, 'USR111', 13, $eventTime,
  99. JobApplicationEvent::EVENT_SCHEDULE_FIRST_INTERVIEW, JobApplicationEvent::STATUS_INTERVIEW_FINISHED,
  100. "1st Interview notes, here");
  101. $createdTime = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime("-0.6 hours"));
  102. $eventTime = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime("+6 days"));
  103. $this->_createEvent(2, 1, $createdTime, 'USR112', 14, $eventTime,
  104. JobApplicationEvent::EVENT_SCHEDULE_SECOND_INTERVIEW, JobApplicationEvent::STATUS_INTERVIEW_SCHEDULED,
  105. "2nd Interview notes, here");
  106. // Events for second job application
  107. $createdTime = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime("-0.3 hours"));
  108. $eventTime = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime("+7 days"));
  109. $this->_createEvent(3, 2, $createdTime, 'USR113', 14, $eventTime,
  110. JobApplicationEvent::EVENT_SCHEDULE_FIRST_INTERVIEW, JobApplicationEvent::STATUS_INTERVIEW_SCHEDULED,
  111. "3rd Interview notes, here");
  112. UniqueIDGenerator::getInstance()->resetIDs();
  113. }
  114. /**
  115. * Tears down the fixture, for example, close a network connection.
  116. * This method is called after a test is executed.
  117. *
  118. * @access protected
  119. */
  120. protected function tearDown() {
  121. $this->_deleteTables();
  122. UniqueIDGenerator::getInstance()->resetIDs();
  123. }
  124. private function _deleteTables() {
  125. $this->_runQuery("DELETE FROM `hs_hr_users` WHERE id in ('USR111', 'USR112', 'USR113')");
  126. $this->_runQuery("TRUNCATE TABLE `hs_hr_job_application_events`");
  127. $this->_runQuery("TRUNCATE TABLE `hs_hr_job_application`");
  128. $this->_runQuery("TRUNCATE TABLE `hs_hr_job_vacancy`");
  129. $this->_runQuery("TRUNCATE TABLE `hs_hr_job_title`");
  130. $this->_runQuery("TRUNCATE TABLE `hs_hr_employee`");
  131. }
  132. /**
  133. * @todo Implement testGetCreatorName().
  134. */
  135. public function testGetCreatorName() {
  136. // Creator with corresponding employee who doesnt have email
  137. $jobApplication = JobApplicationEvent::getJobApplicationEvent(1);
  138. $this->assertEquals('Saman Rajasinghe', $jobApplication->getCreatorName());
  139. // Creator without corresponding user.
  140. $jobApplication = JobApplicationEvent::getJobApplicationEvent(3);
  141. $this->assertEquals('adminhr', $jobApplication->getCreatorName());
  142. }
  143. /**
  144. * @todo Implement testGetCreatorEmail().
  145. */
  146. public function testGetCreatorEmail() {
  147. // Creator with corresponding employee who doesnt have email
  148. $jobApplication = JobApplicationEvent::getJobApplicationEvent(1);
  149. $this->assertEquals('', $jobApplication->getCreatorEmail());
  150. // Creator with corresponding employee who has email
  151. $jobApplication = JobApplicationEvent::getJobApplicationEvent(2);
  152. $this->assertEquals('aruna@example.com', $jobApplication->getCreatorEmail());
  153. // Creator without corresponding user.
  154. $jobApplication = JobApplicationEvent::getJobApplicationEvent(3);
  155. $this->assertEquals('', $jobApplication->getCreatorEmail());
  156. }
  157. /**
  158. * Test save() method
  159. */
  160. public function testSave() {
  161. // New event
  162. $before = $this->_getNumRows();
  163. $createdTime = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime("-1 hours"));
  164. $eventTime = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime("+5 days"));
  165. $event = $this->_getEvent(null, 1, $createdTime, 'USR111', 11,
  166. $eventTime, JobApplicationEvent::EVENT_SCHEDULE_SECOND_INTERVIEW,
  167. JobApplicationEvent::STATUS_INTERVIEW_FINISHED, 'Notes aa');
  168. $id = $event->save();
  169. $after = $this->_getNumRows();
  170. $this->assertEquals(1, $after - $before);
  171. $this->_checkExistsInDb($event);
  172. // Update
  173. $before = $this->_getNumRows();
  174. $event = JobApplicationEvent::getJobApplicationEvent(1);
  175. $event->setNotes('New Notes');
  176. $event->setStatus(JobApplicationEvent::STATUS_INTERVIEW_FINISHED);
  177. $event->setEventType(JobApplicationEvent::EVENT_SCHEDULE_SECOND_INTERVIEW);
  178. $event->save();
  179. $after = $this->_getNumRows();
  180. $this->assertEquals($before, $after);
  181. $this->_checkExistsInDb($event);
  182. // invalid id
  183. $before = $this->_getNumRows();
  184. $event->setId('A1k');
  185. try {
  186. $event->save();
  187. } catch (JobApplicationEventException $e) {
  188. $this->assertEquals(JobApplicationEventException::INVALID_PARAMETER, $e->getCode());
  189. }
  190. $after = $this->_getNumRows();
  191. $this->assertEquals($before, $after);
  192. }
  193. /**
  194. * Test getEvents() method
  195. */
  196. public function testGetEvents() {
  197. $event1 = JobApplicationEvent::getJobApplicationEvent(1);
  198. $event2 = JobApplicationEvent::getJobApplicationEvent(2);
  199. $event3 = JobApplicationEvent::getJobApplicationEvent(3);
  200. // Invalid app id
  201. try {
  202. $events = JobApplicationEvent::getEvents('12A');
  203. } catch (JobApplicationEventException $e) {
  204. $this->assertEquals(JobApplicationEventException::INVALID_PARAMETER, $e->getCode());
  205. }
  206. // app id with no events
  207. $events = JobApplicationEvent::getEvents(3);
  208. $this->assertTrue(is_array($events));
  209. $this->assertEquals(0, count($events));
  210. // app id with events
  211. $events = JobApplicationEvent::getEvents(1);
  212. $this->assertTrue(is_array($events));
  213. $this->assertEquals(2, count($events));
  214. $expected = array($event1, $event2);
  215. $this->_compareEventsWithOrder($expected, $events);
  216. $events = JobApplicationEvent::getEvents(2);
  217. $expected = array($event3);
  218. $this->assertTrue(is_array($events));
  219. $this->assertEquals(1, count($events));
  220. $this->_compareEventsWithOrder($expected, $events);
  221. }
  222. /**
  223. * Test case for getJobApplicationEvent().
  224. */
  225. public function testGetJobApplicationEvent() {
  226. $createdTime = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime("-1 hours"));
  227. $eventTime = date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT, strtotime("+5 days"));
  228. // invalid id
  229. try {
  230. $event = JobApplicationEvent::getJobApplicationEvent('12A');
  231. $this->fail("Exception expected");
  232. } catch (JobApplicationEventException $e) {
  233. $this->assertEquals(JobApplicationEventException::INVALID_PARAMETER, $e->getCode());
  234. }
  235. // valid id but not available
  236. $event = JobApplicationEvent::getJobApplicationEvent(121);
  237. $this->assertNull($event);
  238. // valid available id
  239. $event = JobApplicationEvent::getJobApplicationEvent(1);
  240. $this->assertNotNull($event);
  241. $this->assertEquals(1, $event->getApplicationId());
  242. $this->assertEquals('USR111', $event->getCreatedBy());
  243. $this->assertEquals(13, $event->getOwner());
  244. $this->assertEquals(JobApplicationEvent::EVENT_SCHEDULE_FIRST_INTERVIEW, $event->getEventType());
  245. $this->assertEquals(JobApplicationEvent::STATUS_INTERVIEW_FINISHED, $event->getStatus());
  246. $this->assertEquals("1st Interview notes, here", $event->getNotes());
  247. }
  248. /**
  249. * Create a JobApplication object with the passed parameters
  250. */
  251. private function _getJobApplication($id, $vacancyId, $firstName, $middleName, $lastName, $street1, $street2,
  252. $city, $province, $zip, $country, $mobile, $phone, $email, $qualifications, $status = JobApplication::STATUS_SUBMITTED) {
  253. $application = new JobApplication($id);
  254. $application->setVacancyId($vacancyId);
  255. $application->setFirstName($firstName);
  256. $application->setMiddleName($middleName);
  257. $application->setLastName($lastName);
  258. $application->setStreet1($street1);
  259. $application->setStreet2($street2);
  260. $application->setCity($city);
  261. $application->setProvince($province);
  262. $application->setZip($zip);
  263. $application->setCountry($country);
  264. $application->setMobile($mobile);
  265. $application->setPhone($phone);
  266. $application->setEmail($email);
  267. $application->setQualifications($qualifications);
  268. $application->setStatus($status);
  269. $application->setAppliedDateTime(date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT));
  270. return $application;
  271. }
  272. /**
  273. * Saves the given JobApplication objects in the database
  274. *
  275. * @param array $applications Array of JobApplication objects to save.
  276. */
  277. private function _createJobApplications($applications) {
  278. foreach ($applications as $application) {
  279. $sql = sprintf("INSERT INTO hs_hr_job_application(application_id, vacancy_id, firstname, middlename, ".
  280. "lastname, street1, street2, city, country_code, province, zip, " .
  281. "phone, mobile, email, qualifications, status, applied_datetime) " .
  282. "VALUES(%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s')",
  283. $application->getId(), $application->getVacancyId(), $application->getFirstName(),
  284. $application->getMiddleName(), $application->getLastName(), $application->getStreet1(),
  285. $application->getStreet2(), $application->getCity(), $application->getCountry(),
  286. $application->getProvince(), $application->getZip(), $application->getPhone(),
  287. $application->getMobile(), $application->getEmail(),
  288. $application->getQualifications(), $application->getStatus(),
  289. $application->getAppliedDateTime());
  290. $this->assertTrue(mysql_query($sql), mysql_error());
  291. }
  292. UniqueIDGenerator::getInstance()->initTable();
  293. }
  294. /**
  295. * Create job application event with the passed parameters
  296. *
  297. * @param int $id
  298. * @param int $applicationId
  299. * @param String $createdTime
  300. * @param String $createdBy
  301. * @param int $ownerId
  302. * @param String $eventTime
  303. * @param int $eventType
  304. * @param int $eventStatus
  305. * @param String $notes
  306. */
  307. private function _createEvent($id, $applicationId, $createdTime, $createdBy, $ownerId, $eventTime,
  308. $eventType, $eventStatus, $notes) {
  309. $sql = sprintf("INSERT INTO `hs_hr_job_application_events`(`id`,`application_id`,`created_time`," .
  310. "`created_by`, `owner`, `event_time`, `event_type`, `status`, `notes`) " .
  311. "VALUES (%d, %d, '%s', '%s', %d, '%s', %d, %d, '%s')",
  312. $id, $applicationId, $createdTime, $createdBy, $ownerId, $eventTime,
  313. $eventType, $eventStatus, $notes);
  314. $this->assertTrue(mysql_query($sql), mysql_error());
  315. UniqueIDGenerator::getInstance()->initTable();
  316. }
  317. /**
  318. * Check's that the passed appliation event exists in the database
  319. *
  320. * @param JobApplicationEvent Job Application Event to check
  321. */
  322. private function _checkExistsInDb($application) {
  323. $id = $application->getId();
  324. $appId = $application->getApplicationId();
  325. $createdBy = $application->getCreatedBy();
  326. $owner = $application->getOwner();
  327. $eventType = $application->getEventType();
  328. $status = $application->getStatus();
  329. $status = is_null($status) ? 'null' : $status;
  330. $notes = $application->getNotes();
  331. $notes = is_null($notes) ? 'null' : "'{$notes}'";
  332. $where = "id = {$id} AND application_id = {$appId} AND created_by = '{$createdBy}' AND owner = {$owner}" .
  333. " AND notes = {$notes} AND status = {$status}";
  334. $this->assertEquals(1, $this->_getNumRows($where));
  335. }
  336. /**
  337. * Returns the number of rows in the hs_hr_job_application_events table
  338. *
  339. * @param string $where where clause
  340. * @return int number of rows
  341. */
  342. private function _getNumRows($where = null) {
  343. $sql = "SELECT COUNT(*) FROM hs_hr_job_application_events";
  344. if (!empty($where)) {
  345. $sql .= " WHERE " . $where;
  346. }
  347. $result = mysql_query($sql);
  348. $row = mysql_fetch_array($result, MYSQL_NUM);
  349. $count = $row[0];
  350. return $count;
  351. }
  352. /**
  353. * Get Job Application Event with the passed parameters
  354. *
  355. * @param int $id Job Application Event Id
  356. * @param int $appId Job Application Event Id
  357. * @param string $createdTime
  358. * @param string $createdBy
  359. * @param int $owner
  360. * @param string $eventTime
  361. * @param int $eventType
  362. * @param int $status
  363. * @param string $notes
  364. */
  365. private function _getEvent($id, $appId, $createdTime, $createdBy, $owner,
  366. $eventTime, $eventType, $status, $notes) {
  367. $event = new JobApplicationEvent();
  368. $event->setId($id);
  369. $event->setApplicationId($appId);
  370. $event->setCreatedTime($createdTime);
  371. $event->setCreatedBy($createdBy);
  372. $event->setOwner($owner);
  373. $event->setEventTime($eventTime);
  374. $event->setEventType($eventType);
  375. $event->setStatus($status);
  376. $event->setNotes($notes);
  377. return $event;
  378. }
  379. /**
  380. * Compares two array of JobApplicationEvent objects verifing they contain the same
  381. * objects and considering the order
  382. *
  383. * @param array $expected Expected
  384. * @param array $result Result
  385. */
  386. private function _compareEventsWithOrder($expected, $result) {
  387. $this->assertEquals(count($expected), count($result));
  388. $i = 0;
  389. foreach ($expected as $application) {
  390. $this->assertEquals($application, $result[$i]);
  391. $i++;
  392. }
  393. }
  394. /**
  395. * Run given sql query, checking the return value
  396. */
  397. private function _runQuery($sql) {
  398. $this->assertTrue(mysql_query($sql), mysql_error());
  399. }
  400. }
  401. // Call JobApplicationEventTest::main() if this source file is executed directly.
  402. if (PHPUnit_MAIN_METHOD == "JobApplicationEventTest::main") {
  403. JobApplicationEventTest::main();
  404. }
  405. ?>