PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/models/eimadmin/export/CustomizableCSVExportTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 236 lines | 147 code | 40 blank | 49 comment | 4 complexity | 81438f0c899711874d442b4a0fc100f7 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 CustomizableCSVExportTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "CustomizableCSVExportTest::main");
  5. }
  6. require_once "PHPUnit/Framework/TestCase.php";
  7. require_once "PHPUnit/Framework/TestSuite.php";
  8. require_once "testConf.php";
  9. require_once 'CustomizableCSVExport.php';
  10. require_once ROOT_PATH . '/lib/common/LocaleUtil.php';
  11. require_once ROOT_PATH . '/lib/models/hrfunct/EmpDirectDebit.php';
  12. require_once ROOT_PATH . '/lib/models/eimadmin/encryption/KeyHandler.php';
  13. /**
  14. * Test class for CustomizableCSVExport.
  15. * Generated by PHPUnit_Util_Skeleton on 2008-01-09 at 11:53:31.
  16. */
  17. class CustomizableCSVExportTest 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("CustomizableCSVExportTest");
  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->_truncateTables();
  40. // insert some test data
  41. $this->_runQuery("INSERT INTO hs_hr_custom_export(export_id, name, fields, headings) VALUES (1, 'Export 1', 'empId,lastName,firstName,middleName,street1,street2,city', '')");
  42. $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')");
  43. $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')");
  44. // insert some employee data
  45. $sql = "INSERT INTO hs_hr_employee" .
  46. "(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, " .
  47. "emp_nick_name, emp_smoker, ethnic_race_code, emp_birthday, nation_code, " .
  48. "emp_gender, emp_marital_status, emp_ssn_num, emp_sin_num, emp_other_id, " .
  49. "emp_dri_lice_num, emp_dri_lice_exp_date, emp_military_service, emp_status, " .
  50. "job_title_code, eeo_cat_code, work_station, " .
  51. "emp_street1, emp_street2, city_code, coun_code, provin_code, emp_zipcode, " .
  52. "emp_hm_telephone, emp_mobile, emp_work_telephone, emp_work_email, " .
  53. "sal_grd_code, joined_date, emp_oth_email, " .
  54. "custom1, custom2, custom3, custom4, custom5, " .
  55. "custom6, custom7, custom8, custom9, custom10) VALUES (" .
  56. "'10', 'E1921A', 'Karunadasa', 'Kamal', 'K', " .
  57. "NULL, NULL, NULL, '1974-11-20', NULL, " .
  58. "1, NULL, '987654320', '', '', " .
  59. "null, NULL, NULL, 'EST001', " .
  60. "NULL, NULL, NULL, " .
  61. "'111 Main Street', 'SUITE A29', 'Houston', 'US', 'TX', '77845', " .
  62. "'', '', '', NULL, " .
  63. "NULL, '1997-12-11', NULL, " .
  64. "'c1', 'c2', 'c3', 'c4', 'c5'," .
  65. "'c6', 'c7', 'c8', 'c9', 'c10'" .
  66. ")";
  67. if (KeyHandler::keyExists()) {
  68. $key = KeyHandler::readKey();
  69. $sql = str_replace("'987654320'", "AES_ENCRYPT('987654320', '$key')", $sql);
  70. }
  71. $this->_runQuery($sql);
  72. $sql = "INSERT into hs_hr_emp_us_tax(emp_number, tax_federal_status, tax_federal_exceptions, " .
  73. "tax_state, tax_state_status, tax_state_exceptions, tax_unemp_state,tax_work_state) VALUES (" .
  74. "10, 'NRA', 2, 'MD', 'NA', 3, 'VA', 'AZ')";
  75. $this->_runQuery($sql);
  76. // Add direct debit information
  77. $dd = new EmpDirectDebit();
  78. $dd->setEmpNumber(10);
  79. $dd->setRoutingNumber(11111);
  80. $dd->setAccount('AC 1');
  81. $dd->setAmount(121);
  82. $dd->setAccountType('CHECKING');
  83. $dd->setTransactionType('BLANK');
  84. $this->assertTrue($dd->add(), mysql_error());
  85. $dd = new EmpDirectDebit();
  86. $dd->setEmpNumber(10);
  87. $dd->setRoutingNumber(22222);
  88. $dd->setAccount('AC #2');
  89. $dd->setAmount(23);
  90. $dd->setAccountType('SAVINGS');
  91. $dd->setTransactionType('FLATMINUS');
  92. $this->assertTrue($dd->add(), mysql_error());
  93. }
  94. /**
  95. * Tears down the fixture, for example, close a network connection.
  96. * This method is called after a test is executed.
  97. *
  98. * @access protected
  99. */
  100. protected function tearDown() {
  101. $this->_truncateTables();
  102. }
  103. /**
  104. * Test the constructor
  105. */
  106. public function testConstructor() {
  107. // check with invalid id
  108. try {
  109. $export = new CustomizableCSVExport(4);
  110. $this->fail("Exception expected");
  111. } catch (Exception $e) {
  112. }
  113. // check with valid id
  114. $export = new CustomizableCSVExport(2);
  115. }
  116. /**
  117. * Test the getName() function
  118. */
  119. public function testGetName() {
  120. $export = new CustomizableCSVExport(2);
  121. $this->assertEquals('Export 2', $export->getName());
  122. $export = new CustomizableCSVExport(1);
  123. $this->assertEquals('Export 1', $export->getName());
  124. }
  125. /**
  126. * Test the getHeader() function
  127. */
  128. public function testGetHeader() {
  129. // Export with header not defined, should default to assigned field names
  130. $export = new CustomizableCSVExport(1);
  131. $this->assertEquals('empId,lastName,firstName,middleName,street1,street2,city', $export->getHeader());
  132. // Export with header defined
  133. $export = new CustomizableCSVExport(2);
  134. $this->assertEquals('Employee Id,Last Name,First Name,City', $export->getHeader());
  135. $export = new CustomizableCSVExport(3);
  136. $this->assertEquals('Employee Id,Address1, Address2, City', $export->getHeader());
  137. }
  138. /**
  139. * Test the getCSVData() method
  140. */
  141. public function testGetCSVData() {
  142. // Export empId,lastName,firstName,middleName,street1,street2,city
  143. $export = new CustomizableCSVExport(1);
  144. $csv = $export->getCSVData();
  145. $expected = 'E1921A,Karunadasa,Kamal,K,111 Main Street,SUITE A29,Houston' . "\n";
  146. $this->assertEquals($expected, $csv);
  147. // In a different order
  148. $this->_runQuery("UPDATE hs_hr_custom_export SET fields = 'street1,lastName,empId,firstName,street2,middleName,city' WHERE export_id = 1");
  149. $export = new CustomizableCSVExport(1);
  150. $csv = $export->getCSVData();
  151. $expected = '111 Main Street,Karunadasa,E1921A,Kamal,SUITE A29,K,Houston' . "\n";
  152. $this->assertEquals($expected, $csv);
  153. $this->_runQuery("UPDATE hs_hr_custom_export SET fields = 'salary,payFrequency,street1,lastName,firstName,street2,middleName,city,location' WHERE export_id = 1");
  154. $export = new CustomizableCSVExport(1);
  155. $csv = $export->getCSVData();
  156. $expected = ',,111 Main Street,Karunadasa,Kamal,SUITE A29,K,Houston,' . "\n";
  157. $this->assertEquals($expected, $csv);
  158. // All available fields
  159. $available = "empId,lastName,firstName,middleName,street1,street2,city," .
  160. "state,zip,gender,birthDate,ssn,empStatus,joinedDate,workStation,location,custom1,custom2," .
  161. "custom3,custom4,custom5,custom6,custom7,custom8,custom9,custom10," .
  162. "workState,salary,payFrequency," .
  163. "FITWStatus,FITWExemptions,SITWState,SITWStatus,SITWExemptions," .
  164. "SUIState,DD1Routing,DD1Account,DD1Amount,".
  165. "DD1AmountCode,DD1Checking,DD2Routing,".
  166. "DD2Account,DD2Amount,DD2AmountCode,DD2Checking";
  167. $this->_runQuery("UPDATE hs_hr_custom_export SET headings = '', fields = '{$available}' WHERE export_id = 2");
  168. $joinedDate = LocaleUtil::getInstance()->formatDate('1997-12-11');
  169. $expected = "E1921A,Karunadasa,Kamal,K,111 Main Street,SUITE A29,Houston," .
  170. "TX,77845,M,1974-11-20,987654320,EST001,{$joinedDate},,,c1,c2," .
  171. "c3,c4,c5,c6,c7,c8,c9,c10," .
  172. "AZ,,," .
  173. "NRA,2,MD,NA,3," .
  174. "VA,11111,AC 1,121.00,".
  175. "Blank,Y,22222,".
  176. "AC #2,23.00,Flat-,\n";
  177. $export = new CustomizableCSVExport(2);
  178. $csv = $export->getCSVData();
  179. $this->assertEquals($expected, $csv);
  180. }
  181. /**
  182. * Run given sql query
  183. */
  184. private function _runQuery($sql) {
  185. $this->assertTrue(mysql_query($sql), mysql_error());
  186. }
  187. private function _truncateTables() {
  188. $this->_runQuery("TRUNCATE TABLE hs_hr_custom_export");
  189. $this->_runQuery("TRUNCATE TABLE `hs_hr_employee`");
  190. }
  191. }
  192. // Call CustomizableCSVExportTest::main() if this source file is executed directly.
  193. if (PHPUnit_MAIN_METHOD == "CustomizableCSVExportTest::main") {
  194. CustomizableCSVExportTest::main();
  195. }
  196. ?>