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

/lib/models/eimadmin/CustomImportTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 529 lines | 336 code | 103 blank | 90 comment | 4 complexity | 1129b71686e0f13b670c8ac5c1a16745 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 CustomImportTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "CustomImportTest::main");
  5. }
  6. require_once "PHPUnit/Framework/TestCase.php";
  7. require_once "PHPUnit/Framework/TestSuite.php";
  8. require_once ROOT_PATH . '/lib/models/eimadmin/CustomImport.php';
  9. require_once "testConf.php";
  10. /**
  11. * Test class for CustomImport.
  12. * Generated by PHPUnit_Util_Skeleton on 2008-01-26 at 19:19:52.
  13. */
  14. class CustomImportTest extends PHPUnit_Framework_TestCase {
  15. /**
  16. * Runs the test methods of this class.
  17. *
  18. * @access public
  19. * @static
  20. */
  21. public static function main() {
  22. require_once "PHPUnit/TextUI/TestRunner.php";
  23. $suite = new PHPUnit_Framework_TestSuite("CustomImportTest");
  24. $result = PHPUnit_TextUI_TestRunner::run($suite);
  25. }
  26. /**
  27. * Sets up the fixture, for example, open a network connection.
  28. * This method is called before a test is executed.
  29. *
  30. * @access protected
  31. */
  32. protected function setUp() {
  33. $conf = new Conf();
  34. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  35. mysql_select_db($conf->dbname);
  36. $this->_runQuery("TRUNCATE TABLE hs_hr_custom_import");
  37. // insert some test data
  38. $this->_runQuery("INSERT INTO hs_hr_custom_import(import_id, name, fields, has_heading) VALUES (1, 'Import 1', 'empId,lastName,firstName,middleName,street1,street2,city', 0)");
  39. $this->_runQuery("INSERT INTO hs_hr_custom_import(import_id, name, fields, has_heading) VALUES (2, 'Import 2', 'empId,lastName,firstName,city', 1)");
  40. $this->_runQuery("INSERT INTO hs_hr_custom_import(import_id, name, fields, has_heading) VALUES (3, 'Import 3', 'empId,firstName,lastName,street1,street2,city', 1)");
  41. $this->_runQuery("TRUNCATE TABLE hs_hr_custom_fields");
  42. $this->_runQuery("INSERT INTO hs_hr_custom_fields(field_num, name, type, extra_data) VALUES ('1', 'Blood Group', '0', '')");
  43. UniqueIDGenerator::getInstance()->resetIDs();
  44. }
  45. /**
  46. * Tears down the fixture, for example, close a network connection.
  47. * This method is called after a test is executed.
  48. *
  49. * @access protected
  50. */
  51. protected function tearDown() {
  52. $this->_runQuery("TRUNCATE TABLE hs_hr_custom_import");
  53. $this->_runQuery("TRUNCATE TABLE hs_hr_custom_fields");
  54. UniqueIDGenerator::getInstance()->resetIDs();
  55. }
  56. /**
  57. * Implement getCustomImport
  58. */
  59. public function testGetCustomImport() {
  60. // non existent id
  61. $this->assertNull(CustomImport::getCustomImport(10));
  62. // invalid id
  63. try {
  64. $import = CustomImport::getCustomImport('X1');
  65. $this->fail("Should throw exception on invalid parameter");
  66. } catch (CustomImportException $e) {
  67. $this->assertEquals(CustomImportException::INVALID_PARAMETERS, $e->getCode());
  68. }
  69. // valid id
  70. $import = CustomImport::getCustomImport(1);
  71. $this->assertEquals(1, $import->getId());
  72. $this->assertEquals('Import 1', $import->getName());
  73. $assignedFields = $import->getAssignedFields();
  74. $expected = array('empId','lastName','firstName','middleName','street1','street2','city');
  75. $this->assertTrue(is_array($assignedFields));
  76. $this->assertEquals(count($expected), count($assignedFields));
  77. $diff = array_diff_assoc($expected, $assignedFields);
  78. $this->assertEquals(0, count($diff), "Assigned fields not correct");
  79. $this->assertFalse($import->getContainsHeader());
  80. $import = CustomImport::getCustomImport(2);
  81. $this->assertEquals(2, $import->getId());
  82. $this->assertEquals('Import 2', $import->getName());
  83. $assignedFields = $import->getAssignedFields();
  84. $expected = array('empId','lastName','firstName','city');
  85. $this->assertTrue(is_array($assignedFields));
  86. $this->assertEquals(count($expected), count($assignedFields));
  87. $diff = array_diff_assoc($expected, $assignedFields);
  88. $this->assertEquals(0, count($diff), "Assigned fields not correct");
  89. $this->assertTrue($import->getContainsHeader());
  90. }
  91. /**
  92. * Test the getAllFields method
  93. */
  94. public function testGetAllFields() {
  95. $allFields = CustomImport::getAllFields();
  96. $this->assertTrue(!empty($allFields));
  97. $this->assertTrue(is_array($allFields));
  98. // compare arrays considering order
  99. $expected = array("empId", "lastName", "firstName", "middleName",
  100. 'HomePhone','MobilePhone', 'WorkPhone', 'WorkEmail','OtherEmail', 'DrivingLic',
  101. "street1", "street2", "city",
  102. "state", "zip", "gender", "birthDate", "ssn", "joinedDate", "workStation", "custom1",
  103. "workState",
  104. "FITWStatus", "FITWExemptions", "SITWState", "SITWStatus", "SITWExemptions",
  105. "SUIState", "DD1Routing", "DD1Account", "DD1Amount",
  106. "DD1AmountCode", "DD1Checking", "DD2Routing",
  107. "DD2Account", "DD2Amount", "DD2AmountCode", "DD2Checking");
  108. $diff = array_diff_assoc($expected, $allFields);
  109. $this->assertEquals(0, count($diff), "Incorrect fields returned");
  110. // verify that there are no duplicates
  111. $unique = array_unique($allFields);
  112. $this->assertEquals(count($unique), count($allFields), "Duplicate field names found!");
  113. // verify that none of the fields have a comma in them
  114. foreach ($allFields as $field) {
  115. $this->assertTrue((strpos($field, ",") === false), "Field name contains comma");
  116. }
  117. }
  118. /**
  119. * Test method for getCustomImportList().
  120. */
  121. public function testGetCustomImportList() {
  122. $list = CustomImport::getCustomImportList();
  123. $this->assertTrue(is_array($list));
  124. $this->assertEquals(3, count($list));
  125. $expected = array(1, 2, 3);
  126. foreach ($list as $import) {
  127. $key = array_search($import->getId(), $expected);
  128. $this->assertTrue($key !== false);
  129. unset($expected[$key]);
  130. }
  131. $this->assertTrue(empty($expected));
  132. $this->_runQuery("DELETE FROM hs_hr_custom_import");
  133. $list = CustomImport::getCustomImportList();
  134. $this->assertTrue(is_array($list));
  135. $this->assertEquals(0, count($list));
  136. }
  137. /**
  138. * Test for getCustomImportListForView().
  139. */
  140. public function testGetCustomImportListForView() {
  141. $recordFound = CustomImport::getCustomImportListForView(1,"Import 2",1);
  142. $this->assertTrue(is_array($recordFound));
  143. $this->assertEquals(1, count($recordFound));
  144. $this->assertEquals($recordFound[0][0],2);
  145. $this->assertEquals($recordFound[0][1],"Import 2");
  146. $this->assertEquals($recordFound[0][2],"empId,lastName,firstName,city");
  147. $recordFound = CustomImport::getCustomImportListForView(1,"Import 3",1);
  148. $this->assertTrue(is_array($recordFound));
  149. $this->assertEquals(1, count($recordFound));
  150. $this->assertEquals($recordFound[0][0],3);
  151. $this->assertEquals($recordFound[0][1],"Import 3");
  152. $this->assertEquals($recordFound[0][2],"empId,firstName,lastName,street1,street2,city");
  153. $recordFound = CustomImport::getCustomImportListForView(1,"Import 1",1);
  154. $this->assertTrue(is_array($recordFound));
  155. $this->assertEquals(1, count($recordFound));
  156. $this->assertEquals($recordFound[0][0],1);
  157. $this->assertEquals($recordFound[0][1],"Import 1");
  158. $this->assertEquals($recordFound[0][2],"empId,lastName,firstName,middleName,street1,street2,city");
  159. $recordFound = CustomImport::getCustomImportListForView(1,"Import New",1);
  160. $this->assertFalse(is_array($recordFound));
  161. // clean up
  162. $this->_runQuery("DELETE FROM hs_hr_custom_import");
  163. }
  164. /**
  165. * Test the getAvailableFields() method
  166. */
  167. public function testGetAvailableFields() {
  168. $allFields = CustomImport::getAllFields();
  169. $allCount = count($allFields);
  170. $import = new CustomImport();
  171. $import->setName("NewImport12");
  172. // Assign everything
  173. $import->setAssignedFields($allFields);
  174. $available = $import->getAvailableFields();
  175. $this->assertTrue(is_array($available));
  176. $this->assertEquals(0, count($available));
  177. // Assign 3 fields
  178. $assign = array("empId", "firstName","gender");
  179. $import->setAssignedFields($assign);
  180. $available = $import->getAvailableFields();
  181. $this->assertTrue(is_array($available));
  182. $this->assertEquals($allCount - 3, count($available));
  183. $expected = $allFields;
  184. unset($expected[array_search("empId", $expected)]);
  185. unset($expected[array_search("firstName", $expected)]);
  186. unset($expected[array_search("gender", $expected)]);
  187. // Verify arrays equal
  188. $diff = array_diff($expected, $available);
  189. $this->assertEquals(0, count($diff), "Arrays should be equal");
  190. }
  191. /**
  192. * Test deleteImports() method
  193. */
  194. public function testDeleteImports() {
  195. $countBefore = $this->_count();
  196. // invalid id parameter
  197. try {
  198. $deleted = CustomImport::deleteImports(1);
  199. $this->fail("Should throw an exception on invalid parameter");
  200. } catch (CustomImportException $e) {
  201. $this->assertEquals(CustomImportException::INVALID_PARAMETERS, $e->getCode());
  202. }
  203. try {
  204. $deleted = CustomImport::deleteImports(array(1, "xyz"));
  205. $this->fail("Should throw an exception on invalid parameter");
  206. } catch (CustomImportException $e) {
  207. $this->assertEquals(CustomImportException::INVALID_PARAMETERS, $e->getCode());
  208. }
  209. // empty array
  210. $ids = array();
  211. $deleted = CustomImport::deleteImports($ids);
  212. $this->assertEquals(0, $deleted);
  213. $count = $this->_count();
  214. $this->assertEquals($countBefore, $count);
  215. // one id
  216. $ids = array(1);
  217. $deleted = CustomImport::deleteImports($ids);
  218. $this->assertEquals(1, $deleted);
  219. $count = $this->_count();
  220. $this->assertEquals($countBefore - 1, $count);
  221. // two id's
  222. $ids = array(2, 3);
  223. $deleted = CustomImport::deleteImports($ids);
  224. $this->assertEquals(2, $deleted);
  225. $count = $this->_count();
  226. $this->assertEquals($countBefore - 3, $count);
  227. }
  228. /**
  229. * Test case for save() method for new custom import definition
  230. */
  231. public function testSaveNew() {
  232. $countBefore = $this->_count();
  233. // save with duplicate name should throw exception
  234. $import = new CustomImport();
  235. $import->setName("Import 1");
  236. $import->setAssignedFields(array("empId", "lastName", "firstName", "street1", "gender"));
  237. try {
  238. $import->save();
  239. $this->fail("Exception should be thrown on duplicate name");
  240. } catch (CustomImportException $e) {
  241. $this->assertEquals(CustomImportException::DUPLICATE_IMPORT_NAME, $e->getCode(), $e->getMessage());
  242. }
  243. // Exception should be thrown on empty name
  244. $import = new CustomImport();
  245. $import->setName("");
  246. $import->setAssignedFields(array("empId", "street1", "gender", "lastName", "firstName"));
  247. try {
  248. $import->save();
  249. $this->fail("Exception should be thrown on empty name");
  250. } catch (CustomImportException $e) {
  251. $this->assertEquals(CustomImportException::EMPTY_IMPORT_NAME, $e->getCode());
  252. }
  253. // save with empty fields should throw exception
  254. $import->setName("New Import 1");
  255. $import->setAssignedFields(array());
  256. try {
  257. $import->save();
  258. $this->fail("Exception should be thrown on empty assigned fields");
  259. } catch (CustomImportException $e) {
  260. $this->assertEquals(CustomImportException::NO_ASSIGNED_FIELDS, $e->getCode());
  261. }
  262. $import->setName("New Import 1");
  263. $import->setAssignedFields(null);
  264. try {
  265. $import->save();
  266. $this->fail("Exception should be thrown on empty assigned fields");
  267. } catch (CustomImportException $e) {
  268. $this->assertEquals(CustomImportException::NO_ASSIGNED_FIELDS, $e->getCode());
  269. }
  270. // save with field not in field list should throw exception
  271. $import->setName("New Import 1");
  272. $import->setAssignedFields(array("firstName", "lastName", "EmployeeId"));
  273. try {
  274. $import->save();
  275. $this->fail("Exception should be thrown on invalid field");
  276. } catch (CustomImportException $e) {
  277. $this->assertEquals(CustomImportException::INVALID_FIELD_NAME, $e->getCode());
  278. }
  279. // save with compulsary field missing should throw exception.
  280. $import->setAssignedFields(array("firstName", "empId"));
  281. try {
  282. $import->save();
  283. $this->fail("Exception should be thrown when compulsary field is missing");
  284. } catch (CustomImportException $e) {
  285. $this->assertEquals(CustomImportException::COMPULSARY_FIELDS_NOT_ASSIGNED, $e->getCode());
  286. }
  287. // valid save, verify data saved
  288. $import->setName("New Import 1");
  289. $import->setAssignedFields(array("empId", "street1", "gender", "lastName", "firstName"));
  290. $import->setContainsHeader(true);
  291. $import->save();
  292. $id = $import->getId();
  293. // verify id set
  294. $this->assertTrue(!empty($id));
  295. $this->assertEquals(4, $id);
  296. // verify saved
  297. $name = $import->getName();
  298. $fields = implode(",", $import->getAssignedFields());
  299. $hasHeader = CustomImport::HAS_HEADING;
  300. $countAfter = $this->_count();
  301. $this->assertEquals(1, $countAfter - $countBefore);
  302. $count = $this->_count("import_id={$id} AND name='{$name}' AND fields='{$fields}' AND has_heading={$hasHeader}");
  303. $this->assertEquals(1, $count, "Not inserted");
  304. // should be able to save without setting containsHeader
  305. $import2 = new CustomImport();
  306. $import2->setName("New Import 2");
  307. $import2->setAssignedFields(array("empId", "street1", "gender", "lastName", "firstName"));
  308. $import2->save();
  309. $id = $import2->getId();
  310. // verify id set
  311. $this->assertTrue(!empty($id));
  312. $this->assertEquals(5, $id);
  313. // verify saved
  314. $name = $import2->getName();
  315. $fields = implode(",", $import2->getAssignedFields());
  316. $hasHeader = CustomImport::NO_HEADING;;
  317. $countAfter = $this->_count();
  318. $this->assertEquals(2, $countAfter - $countBefore);
  319. $count = $this->_count("import_id={$id} AND name='{$name}' AND fields='{$fields}' AND has_heading={$hasHeader}");
  320. $this->assertEquals(1, $count, "Not inserted");
  321. }
  322. /**
  323. * Test case for save() method for existing custom import definition
  324. */
  325. public function testSaveUpdate() {
  326. $countBefore = $this->_count();
  327. // save with duplicate name should throw exception
  328. $import = new CustomImport();
  329. // we set id = 2, so save should update the record with id=2
  330. $import->setId(2);
  331. $import->setName("Import 1");
  332. $import->setAssignedFields(array("empId", "street1", "gender", "firstName", "lastName"));
  333. try {
  334. $import->save();
  335. $this->fail("Exception should be thrown on duplicate name");
  336. } catch (CustomImportException $e) {
  337. $this->assertEquals(CustomImportException::DUPLICATE_IMPORT_NAME, $e->getCode());
  338. }
  339. // save with empty fields should throw exception
  340. $import->setName("New Import 1");
  341. $import->setAssignedFields(array());
  342. try {
  343. $import->save();
  344. $this->fail("Exception should be thrown on empty assigned fields");
  345. } catch (CustomImportException $e) {
  346. $this->assertEquals(CustomImportException::NO_ASSIGNED_FIELDS, $e->getCode());
  347. }
  348. $import->setName("New Import 1");
  349. $import->setAssignedFields(null);
  350. try {
  351. $import->save();
  352. $this->fail("Exception should be thrown on empty assigned fields");
  353. } catch (CustomImportException $e) {
  354. $this->assertEquals(CustomImportException::NO_ASSIGNED_FIELDS, $e->getCode());
  355. }
  356. // save with field not in field list should throw exception
  357. $import->setName("New Import 1");
  358. $import->setAssignedFields(array("firstName", "lastName", "EmployeeId"));
  359. try {
  360. $import->save();
  361. $this->fail("Exception should be thrown on invalid field");
  362. } catch (CustomImportException $e) {
  363. $this->assertEquals(CustomImportException::INVALID_FIELD_NAME, $e->getCode());
  364. }
  365. // save with compulsary field missing should throw exception.
  366. $import->setName("New Import 1");
  367. $import->setAssignedFields(array("firstName", "empId"));
  368. try {
  369. $import->save();
  370. $this->fail("Exception should be thrown when compulsary field is missing");
  371. } catch (CustomImportException $e) {
  372. $this->assertEquals(CustomImportException::COMPULSARY_FIELDS_NOT_ASSIGNED, $e->getCode());
  373. }
  374. // valid save, verify data saved
  375. $import->setName("New Import 1");
  376. $import->setAssignedFields(array("empId", "street1", "firstName", "lastName", "gender"));
  377. $import->setContainsHeader(true);
  378. $import->save();
  379. $id = $import->getId();
  380. // verify id not changed
  381. $this->assertTrue(!empty($id));
  382. $this->assertEquals(2, $id);
  383. // verify saved
  384. $name = $import->getName();
  385. $fields = implode(",", $import->getAssignedFields());
  386. $hasHeader = CustomImport::HAS_HEADING;
  387. $countAfter = $this->_count();
  388. $this->assertEquals($countAfter, $countBefore);
  389. $count = $this->_count("import_id={$id} AND name='{$name}' AND fields='{$fields}' AND has_heading='{$hasHeader}'");
  390. $this->assertEquals(1, $count, "Not Updated");
  391. // Save without changing anything
  392. $import->save();
  393. $id = $import->getId();
  394. // verify id not changed
  395. $this->assertTrue(!empty($id));
  396. $this->assertEquals(2, $id);
  397. $countAfter = $this->_count();
  398. $this->assertEquals($countAfter, $countBefore);
  399. $count = $this->_count("import_id={$id} AND name='{$name}' AND fields='{$fields}' AND has_heading='{$hasHeader}'");
  400. $this->assertEquals(1, $count, "Not Updated");
  401. }
  402. private function _runQuery($sql) {
  403. $this->assertTrue(mysql_query($sql), mysql_error());
  404. }
  405. /**
  406. * Count the number of rows in the database with the give condition
  407. *
  408. * @param string $where Optional where condition
  409. * @return int Number of matching rows in database
  410. */
  411. private function _count($where = null) {
  412. $sql = "SELECT COUNT(*) FROM hs_hr_custom_import";
  413. if (!empty($where)) {
  414. $sql .= " WHERE " . $where;
  415. }
  416. $result = mysql_query($sql);
  417. $row = mysql_fetch_array($result, MYSQL_NUM);
  418. $count = $row[0];
  419. return $count;
  420. }
  421. }
  422. // Call CustomImportTest::main() if this source file is executed directly.
  423. if (PHPUnit_MAIN_METHOD == "CustomImportTest::main") {
  424. CustomImportTest::main();
  425. }
  426. ?>