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

/lib/models/eimadmin/CustomExportTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 561 lines | 364 code | 105 blank | 92 comment | 4 complexity | 854871cac98763579b95e2533b89c729 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 CustomExportTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "CustomExportTest::main");
  5. }
  6. require_once "PHPUnit/Framework/TestCase.php";
  7. require_once "PHPUnit/Framework/TestSuite.php";
  8. require_once ROOT_PATH . '/lib/models/eimadmin/CustomExport.php';
  9. require_once "testConf.php";
  10. /**
  11. * Test class for CustomExport.
  12. * Generated by PHPUnit_Util_Skeleton on 2008-01-08 at 19:19:52.
  13. */
  14. class CustomExportTest 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("CustomExportTest");
  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_export");
  37. // insert some test data
  38. $this->_runQuery("INSERT INTO hs_hr_custom_export(export_id, name, fields, headings) VALUES (1, 'Export 1', 'empId,lastName,firstName,middleName,street1,street2,city', '')");
  39. $this->_runQuery("INSERT INTO hs_hr_custom_export(export_id, name, fields, headings) VALUES (2, 'Export 2', 'empId,lastName,firstName,city', 'Employee Id,Last Name,First Name,City')");
  40. $this->_runQuery("INSERT INTO hs_hr_custom_export(export_id, name, fields, headings) VALUES (3, 'Export 3', 'empId,street1,street2,city', 'Employee Id,Address1, Address2, City')");
  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_export");
  53. $this->_runQuery("TRUNCATE TABLE hs_hr_custom_fields");
  54. UniqueIDGenerator::getInstance()->resetIDs();
  55. }
  56. /**
  57. * Implement getCustomExport
  58. */
  59. public function testGetCustomExport() {
  60. // non existent id
  61. $this->assertNull(CustomExport::getCustomExport(10));
  62. // invalid id
  63. try {
  64. $export = CustomExport::getCustomExport('X1');
  65. $this->fail("Should throw exception on invalid parameter");
  66. } catch (CustomExportException $e) {
  67. $this->assertEquals(CustomExportException::INVALID_PARAMETERS, $e->getCode());
  68. }
  69. // valid id
  70. $export = CustomExport::getCustomExport(1);
  71. $this->assertEquals(1, $export->getId());
  72. $this->assertEquals('Export 1', $export->getName());
  73. $assignedFields = $export->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. $headers = $export->getHeadings();
  80. $this->assertTrue(is_array($headers));
  81. $this->assertEquals(0, count($headers));
  82. $export = CustomExport::getCustomExport(2);
  83. $this->assertEquals(2, $export->getId());
  84. $this->assertEquals('Export 2', $export->getName());
  85. $assignedFields = $export->getAssignedFields();
  86. $expected = array('empId','lastName','firstName','city');
  87. $this->assertTrue(is_array($assignedFields));
  88. $this->assertEquals(count($expected), count($assignedFields));
  89. $diff = array_diff_assoc($expected, $assignedFields);
  90. $this->assertEquals(0, count($diff), "Assigned fields not correct");
  91. $headers = $export->getHeadings();
  92. $expectedHeader = array('Employee Id','Last Name','First Name','City');
  93. $this->assertTrue(is_array($headers));
  94. $this->assertEquals(count($expectedHeader), count($headers));
  95. $diff = array_diff_assoc($expectedHeader, $headers);
  96. $this->assertEquals(0, count($diff), "Header fields not correct");
  97. }
  98. /**
  99. * Test the getAllFields method
  100. */
  101. public function testGetAllFields() {
  102. $allFields = CustomExport::getAllFields();
  103. $this->assertTrue(!empty($allFields));
  104. $this->assertTrue(is_array($allFields));
  105. // compare arrays considering order
  106. $expected = array("empId", "lastName", "firstName", "middleName", "street1", "street2", "city",
  107. "state", "zip", "gender", "birthDate", "ssn", "empStatus", "joinedDate", "workStation", "location",
  108. "custom1",
  109. "workState", "salary", "payFrequency",
  110. "FITWStatus", "FITWExemptions", "SITWState", "SITWStatus", "SITWExemptions",
  111. "SUIState", "DD1Routing", "DD1Account", "DD1Amount",
  112. "DD1AmountCode", "DD1Checking", "DD2Routing",
  113. "DD2Account", "DD2Amount", "DD2AmountCode", "DD2Checking");
  114. $diff = array_diff_assoc($expected, $allFields);
  115. $this->assertEquals($expected, $allFields);
  116. $this->assertEquals(0, count($diff), "Incorrect fields returned");
  117. // verify that there are no duplicates
  118. $unique = array_unique($allFields);
  119. $this->assertEquals(count($unique), count($allFields), "Duplicate field names found!");
  120. // verify that none of the fields have a comma in them
  121. foreach ($allFields as $field) {
  122. $this->assertTrue((strpos($field, ",") === false), "Field name contains comma");
  123. }
  124. }
  125. /**
  126. * Test method for getCustomExportList().
  127. */
  128. public function testGetCustomExportList() {
  129. $list = CustomExport::getCustomExportList();
  130. $this->assertTrue(is_array($list));
  131. $this->assertEquals(3, count($list));
  132. $expected = array(1, 2, 3);
  133. foreach ($list as $export) {
  134. $key = array_search($export->getId(), $expected);
  135. $this->assertTrue($key !== false);
  136. unset($expected[$key]);
  137. }
  138. $this->assertTrue(empty($expected));
  139. $this->_runQuery("DELETE FROM hs_hr_custom_export");
  140. $list = CustomExport::getCustomExportList();
  141. $this->assertTrue(is_array($list));
  142. $this->assertEquals(0, count($list));
  143. }
  144. /**
  145. * Test for getCustomExportListForView().
  146. */
  147. public function testGetCustomExportListForView() {
  148. $recordFound = CustomExport::getCustomExportListForView(1,"Export 2",1);
  149. $this->assertTrue(is_array($recordFound));
  150. $this->assertEquals(1, count($recordFound));
  151. $this->assertEquals($recordFound[0][0],2);
  152. $this->assertEquals($recordFound[0][1],"Export 2");
  153. $this->assertEquals($recordFound[0][2],"empId,lastName,firstName,city");
  154. $recordFound = CustomExport::getCustomExportListForView(1,"Export 3",1);
  155. $this->assertTrue(is_array($recordFound));
  156. $this->assertEquals(1, count($recordFound));
  157. $this->assertEquals($recordFound[0][0],3);
  158. $this->assertEquals($recordFound[0][1],"Export 3");
  159. $this->assertEquals($recordFound[0][2],"empId,street1,street2,city");
  160. $recordFound = CustomExport::getCustomExportListForView(1,"Export 1",1);
  161. $this->assertTrue(is_array($recordFound));
  162. $this->assertEquals(1, count($recordFound));
  163. $this->assertEquals($recordFound[0][0],1);
  164. $this->assertEquals($recordFound[0][1],"Export 1");
  165. $this->assertEquals($recordFound[0][2],"empId,lastName,firstName,middleName,street1,street2,city");
  166. $recordFound = CustomExport::getCustomExportListForView(1,"Export New",1);
  167. $this->assertFalse(is_array($recordFound));
  168. // clean up
  169. $this->_runQuery("DELETE FROM hs_hr_custom_export");
  170. }
  171. /**
  172. * Test the getAvailableFields() method
  173. */
  174. public function testGetAvailableFields() {
  175. $allFields = CustomExport::getAllFields();
  176. $allCount = count($allFields);
  177. $export = new CustomExport();
  178. $export->setName("NewExport12");
  179. // Assign everything
  180. $export->setAssignedFields($allFields);
  181. $available = $export->getAvailableFields();
  182. $this->assertTrue(is_array($available));
  183. $this->assertEquals(0, count($available));
  184. // Assign 3 fields
  185. $assign = array("empId", "firstName","gender");
  186. $export->setAssignedFields($assign);
  187. $available = $export->getAvailableFields();
  188. $this->assertTrue(is_array($available));
  189. $this->assertEquals($allCount - 3, count($available));
  190. $expected = $allFields;
  191. unset($expected[array_search("empId", $expected)]);
  192. unset($expected[array_search("firstName", $expected)]);
  193. unset($expected[array_search("gender", $expected)]);
  194. // Verify arrays equal
  195. $diff = array_diff($expected, $available);
  196. $this->assertEquals(0, count($diff), "Arrays should be equal");
  197. }
  198. /**
  199. * Test deleteExports() method
  200. */
  201. public function testDeleteExports() {
  202. $countBefore = $this->_count();
  203. // invalid id parameter
  204. try {
  205. $deleted = CustomExport::deleteExports(1);
  206. $this->fail("Should throw an exception on invalid parameter");
  207. } catch (CustomExportException $e) {
  208. $this->assertEquals(CustomExportException::INVALID_PARAMETERS, $e->getCode());
  209. }
  210. try {
  211. $deleted = CustomExport::deleteExports(array(1, "xyz"));
  212. $this->fail("Should throw an exception on invalid parameter");
  213. } catch (CustomExportException $e) {
  214. $this->assertEquals(CustomExportException::INVALID_PARAMETERS, $e->getCode());
  215. }
  216. // empty array
  217. $ids = array();
  218. $deleted = CustomExport::deleteExports($ids);
  219. $this->assertEquals(0, $deleted);
  220. $count = $this->_count();
  221. $this->assertEquals($countBefore, $count);
  222. // one id
  223. $ids = array(1);
  224. $deleted = CustomExport::deleteExports($ids);
  225. $this->assertEquals(1, $deleted);
  226. $count = $this->_count();
  227. $this->assertEquals($countBefore - 1, $count);
  228. // two id's
  229. $ids = array(2, 3);
  230. $deleted = CustomExport::deleteExports($ids);
  231. $this->assertEquals(2, $deleted);
  232. $count = $this->_count();
  233. $this->assertEquals($countBefore - 3, $count);
  234. }
  235. /**
  236. * Test case for save() method for new custom export definition
  237. */
  238. public function testSaveNew() {
  239. $countBefore = $this->_count();
  240. // save with duplicate name should throw exception
  241. $export = new CustomExport();
  242. $export->setName("Export 1");
  243. $export->setAssignedFields(array("empId", "street1", "gender"));
  244. try {
  245. $export->save();
  246. $this->fail("Exception should be thrown on duplicate name");
  247. } catch (CustomExportException $e) {
  248. $this->assertEquals(CustomExportException::DUPLICATE_EXPORT_NAME, $e->getCode(), $e->getMessage());
  249. }
  250. // Exception should be thrown on empty name
  251. $export = new CustomExport();
  252. $export->setName("");
  253. $export->setAssignedFields(array("empId", "street1", "gender"));
  254. try {
  255. $export->save();
  256. $this->fail("Exception should be thrown on empty name");
  257. } catch (CustomExportException $e) {
  258. $this->assertEquals(CustomExportException::EMPTY_EXPORT_NAME, $e->getCode());
  259. }
  260. // save with empty fields should throw exception
  261. $export->setName("New Export 1");
  262. $export->setAssignedFields(array());
  263. try {
  264. $export->save();
  265. $this->fail("Exception should be thrown on empty assigned fields");
  266. } catch (CustomExportException $e) {
  267. $this->assertEquals(CustomExportException::NO_ASSIGNED_FIELDS, $e->getCode());
  268. }
  269. $export->setName("New Export 1");
  270. $export->setAssignedFields(null);
  271. try {
  272. $export->save();
  273. $this->fail("Exception should be thrown on empty assigned fields");
  274. } catch (CustomExportException $e) {
  275. $this->assertEquals(CustomExportException::NO_ASSIGNED_FIELDS, $e->getCode());
  276. }
  277. // save with field not in field list should throw exception
  278. $export->setName("New Export 1");
  279. $export->setAssignedFields(array("firstName", "lastName", "EmployeeId"));
  280. try {
  281. $export->save();
  282. $this->fail("Exception should be thrown on invalid field");
  283. } catch (CustomExportException $e) {
  284. $this->assertEquals(CustomExportException::INVALID_FIELD_NAME, $e->getCode());
  285. }
  286. // save with field count != header count should throw exception
  287. $export->setName("New Export 1");
  288. $export->setAssignedFields(array("empId", "street1", "gender"));
  289. $export->setHeadings(array("Employee Id", "Street 1", "Street 2", "Gender"));
  290. try {
  291. $export->save();
  292. $this->fail("Exception should be thrown on empty assigned fields");
  293. } catch (CustomExportException $e) {
  294. $this->assertEquals(CustomExportException::HEADER_COUNT_DOESNT_MATCH_FIELD_COUNT, $e->getCode());
  295. }
  296. // save with header containing comma should throw exception
  297. $export->setName("New Export 1");
  298. $export->setAssignedFields(array("empId", "street1", "gender"));
  299. $export->setHeadings(array("Employee Id", "Street 1", "Street, 2"));
  300. try {
  301. $export->save();
  302. $this->fail("Exception should be thrown on invalid header names");
  303. } catch (CustomExportException $e) {
  304. $this->assertEquals(CustomExportException::INVALID_HEADER_NAME, $e->getCode());
  305. }
  306. // valid save, verify data saved
  307. $export->setName("New Export 1");
  308. $export->setAssignedFields(array("empId", "street1", "gender"));
  309. $export->setHeadings(array("Employee Id", "Street 1", "Gender"));
  310. $export->save();
  311. $id = $export->getId();
  312. // verify id set
  313. $this->assertTrue(!empty($id));
  314. $this->assertEquals(4, $id);
  315. // verify saved
  316. $name = $export->getName();
  317. $fields = implode(",", $export->getAssignedFields());
  318. $header = implode(",", $export->getHeadings());
  319. $countAfter = $this->_count();
  320. $this->assertEquals(1, $countAfter - $countBefore);
  321. $count = $this->_count("export_id={$id} AND name='{$name}' AND fields='{$fields}' AND headings='{$header}'");
  322. $this->assertEquals(1, $count, "Not inserted");
  323. // should be able to save with empty headings
  324. $export2 = new CustomExport();
  325. $export2->setName("New Export 2");
  326. $export2->setAssignedFields(array("empId", "street1", "gender"));
  327. $export2->save();
  328. $id = $export2->getId();
  329. // verify id set
  330. $this->assertTrue(!empty($id));
  331. $this->assertEquals(5, $id);
  332. // verify saved
  333. $name = $export2->getName();
  334. $fields = implode(",", $export2->getAssignedFields());
  335. $header = "";
  336. $countAfter = $this->_count();
  337. $this->assertEquals(2, $countAfter - $countBefore);
  338. $count = $this->_count("export_id={$id} AND name='{$name}' AND fields='{$fields}' AND headings='{$header}'");
  339. $this->assertEquals(1, $count, "Not inserted");
  340. }
  341. /**
  342. * Test case for save() method for existing custom export definition
  343. */
  344. public function testSaveUpdate() {
  345. $countBefore = $this->_count();
  346. // save with duplicate name should throw exception
  347. $export = new CustomExport();
  348. // we set id = 2, so save should update the record with id=2
  349. $export->setId(2);
  350. $export->setName("Export 1");
  351. $export->setAssignedFields(array("empId", "street1", "gender"));
  352. try {
  353. $export->save();
  354. $this->fail("Exception should be thrown on duplicate name");
  355. } catch (CustomExportException $e) {
  356. $this->assertEquals(CustomExportException::DUPLICATE_EXPORT_NAME, $e->getCode());
  357. }
  358. // save with empty fields should throw exception
  359. $export->setName("New Export 1");
  360. $export->setAssignedFields(array());
  361. try {
  362. $export->save();
  363. $this->fail("Exception should be thrown on empty assigned fields");
  364. } catch (CustomExportException $e) {
  365. $this->assertEquals(CustomExportException::NO_ASSIGNED_FIELDS, $e->getCode());
  366. }
  367. $export->setName("New Export 1");
  368. $export->setAssignedFields(null);
  369. try {
  370. $export->save();
  371. $this->fail("Exception should be thrown on empty assigned fields");
  372. } catch (CustomExportException $e) {
  373. $this->assertEquals(CustomExportException::NO_ASSIGNED_FIELDS, $e->getCode());
  374. }
  375. // save with field not in field list should throw exception
  376. $export->setName("New Export 1");
  377. $export->setAssignedFields(array("firstName", "lastName", "EmployeeId"));
  378. try {
  379. $export->save();
  380. $this->fail("Exception should be thrown on invalid field");
  381. } catch (CustomExportException $e) {
  382. $this->assertEquals(CustomExportException::INVALID_FIELD_NAME, $e->getCode());
  383. }
  384. // save with field count != header count should throw exception
  385. $export->setName("New Export 1");
  386. $export->setAssignedFields(array("empId", "street1", "gender"));
  387. $export->setHeadings(array("Employee Id", "Street 1", "Street 2", "Gender"));
  388. try {
  389. $export->save();
  390. $this->fail("Exception should be thrown on header count mismatch");
  391. } catch (CustomExportException $e) {
  392. $this->assertEquals(CustomExportException::HEADER_COUNT_DOESNT_MATCH_FIELD_COUNT, $e->getCode());
  393. }
  394. // save with header containing comma should throw exception
  395. $export->setName("New Export 1");
  396. $export->setAssignedFields(array("empId", "street1", "gender"));
  397. $export->setHeadings(array("Employee Id", "Street 1", "Street, 2"));
  398. try {
  399. $export->save();
  400. $this->fail("Exception should be thrown on invalid header name");
  401. } catch (CustomExportException $e) {
  402. $this->assertEquals(CustomExportException::INVALID_HEADER_NAME, $e->getCode());
  403. }
  404. // valid save, verify data saved
  405. $export->setName("New Export 1");
  406. $export->setAssignedFields(array("empId", "street1", "gender"));
  407. $export->setHeadings(array("Employee Id", "Street 1", "Gender"));
  408. $export->save();
  409. $id = $export->getId();
  410. // verify id not changed
  411. $this->assertTrue(!empty($id));
  412. $this->assertEquals(2, $id);
  413. // verify saved
  414. $name = $export->getName();
  415. $fields = implode(",", $export->getAssignedFields());
  416. $header = implode(",", $export->getHeadings());
  417. $countAfter = $this->_count();
  418. $this->assertEquals($countAfter, $countBefore);
  419. $count = $this->_count("export_id={$id} AND name='{$name}' AND fields='{$fields}' AND headings='{$header}'");
  420. $this->assertEquals(1, $count, "Not Updated");
  421. // Save without changing anything
  422. $export->save();
  423. $id = $export->getId();
  424. // verify id not changed
  425. $this->assertTrue(!empty($id));
  426. $this->assertEquals(2, $id);
  427. $countAfter = $this->_count();
  428. $this->assertEquals($countAfter, $countBefore);
  429. $count = $this->_count("export_id={$id} AND name='{$name}' AND fields='{$fields}' AND headings='{$header}'");
  430. $this->assertEquals(1, $count, "Not Updated");
  431. }
  432. private function _runQuery($sql) {
  433. $this->assertTrue(mysql_query($sql), mysql_error());
  434. }
  435. /**
  436. * Count the number of rows in the database with the give condition
  437. *
  438. * @param string $where Optional where condition
  439. * @return int Number of matching rows in database
  440. */
  441. private function _count($where = null) {
  442. $sql = "SELECT COUNT(*) FROM hs_hr_custom_export";
  443. if (!empty($where)) {
  444. $sql .= " WHERE " . $where;
  445. }
  446. $result = mysql_query($sql);
  447. $row = mysql_fetch_array($result, MYSQL_NUM);
  448. $count = $row[0];
  449. return $count;
  450. }
  451. }
  452. // Call CustomExportTest::main() if this source file is executed directly.
  453. if (PHPUnit_MAIN_METHOD == "CustomExportTest::main") {
  454. CustomExportTest::main();
  455. }
  456. ?>