PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/common/UniqueIDGeneratorTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 470 lines | 284 code | 97 blank | 89 comment | 8 complexity | 0bce6817ceaf3710c941bb8ade50fa93 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 UniqueIDGeneratorTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "UniqueIDGeneratorTest::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 'UniqueIDGenerator.php';
  11. /**
  12. * Test class for UniqueIDGenerator.
  13. * Generated by PHPUnit_Util_Skeleton on 2007-07-20 at 11:23:47.
  14. */
  15. class UniqueIDGeneratorTest extends PHPUnit_Framework_TestCase {
  16. private $oldValues;
  17. private $connection;
  18. private $tableInfo = array(array("hs_hr_compstructtree", "id", null),
  19. array("hs_hr_customer", "customer_id", null),
  20. array("hs_hr_education", "edu_code", "EDU"),
  21. array("hs_hr_eec", "eec_code", "EEC"),
  22. array("hs_hr_employee", "emp_number", null),
  23. array("hs_hr_ethnic_race", "ethnic_race_code", "ETH"),
  24. array("hs_hr_holidays", "holiday_id", null),
  25. array("hs_hr_job_title", "jobtit_code", "JOB"),
  26. array("hs_hr_leave", "leave_id", null),
  27. array("hs_hr_leave_requests", "leave_request_id", null),
  28. array("hs_hr_leavetype", "leave_type_id", null),
  29. array("hs_hr_licenses", "licenses_code", "LIC"),
  30. array("hs_hr_language", "lang_code", "LAN"),
  31. array("hs_hr_location", "loc_code", "LOC"),
  32. array("hs_hr_membership", "membship_code", "MME"),
  33. array("hs_hr_membership_type", "membtype_code", "MEM"),
  34. array("hs_hr_module", "mod_id", "MOD"),
  35. array("hs_hr_nationality", "nat_code", "NAT"),
  36. array("hs_hr_project", "project_id", null),
  37. array("hs_hr_project_activity", "activity_id", null),
  38. array("hs_hr_skill", "skill_code", "SKI"),
  39. array("hs_hr_time_event", "time_event_id", null),
  40. array("hs_hr_timesheet", "timesheet_id", null),
  41. array("hs_pr_salary_grade", "sal_grd_code", "SAL"),
  42. array("hs_hr_users", "id", null),
  43. array("hs_hr_user_group", "userg_id", null),
  44. array("hs_hr_workshift", "workshift_id", null),
  45. array("hs_hr_custom_export", "export_id", null),
  46. array("hs_hr_custom_import", "import_id", null),
  47. array("hs_hr_empstat", "estat_code", "EST"),
  48. array("hs_hr_empreport", "rep_code", "REP"),
  49. array("hs_hr_workshift", "workshift_id", null),
  50. array("hs_hr_job_spec", "jobspec_id", null),
  51. array("hs_hr_job_vacancy", "vacancy_id", null),
  52. array("hs_hr_job_application", "application_id", null),
  53. array("hs_hr_job_application_events", "id", null));
  54. /**
  55. * Runs the test methods of this class.
  56. *
  57. * @access public
  58. * @static
  59. */
  60. public static function main() {
  61. require_once "PHPUnit/TextUI/TestRunner.php";
  62. $suite = new PHPUnit_Framework_TestSuite("UniqueIDGeneratorTest");
  63. $result = PHPUnit_TextUI_TestRunner::run($suite);
  64. }
  65. /**
  66. * Sets up the fixture, for example, open a network connection.
  67. * This method is called before a test is executed.
  68. *
  69. * @access protected
  70. */
  71. protected function setUp() {
  72. $conf = new Conf();
  73. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  74. $this->assertTrue($this->connection !== false);
  75. $this->assertTrue(mysql_select_db($conf->dbname));
  76. $result = mysql_query("SELECT `last_id`, `table_name`, `field_name` FROM `hs_hr_unique_id`;");
  77. while($row = mysql_fetch_array($result, MYSQL_NUM)) {
  78. $this->oldValues['AUTO_INC_PK_TABLE']['hs_hr_unique_id'][] = $row;
  79. }
  80. mysql_free_result($result);
  81. $tableList = array('hs_hr_language', 'hs_hr_project', 'hs_hr_customer');
  82. $this->_backupTables($tableList);
  83. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_unique_id`"));
  84. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_language`"));
  85. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_project`"));
  86. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_customer`"));
  87. }
  88. /**
  89. * Tears down the fixture, for example, close a network connection.
  90. * This method is called after a test is executed.
  91. *
  92. * @access protected
  93. */
  94. protected function tearDown() {
  95. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_unique_id`"));
  96. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_language`"));
  97. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_project`"));
  98. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_customer`"));
  99. foreach($this->oldValues['AUTO_INC_PK_TABLE']['hs_hr_unique_id'] as $row) {
  100. $this->assertTrue(mysql_query("INSERT INTO `hs_hr_unique_id` VALUES (NULL, '" . implode("', '", $row) . "')"), mysql_error());
  101. }
  102. $this->_restoreTables();
  103. /* Restore the unique id table */
  104. UniqueIDGenerator::getInstance()->initTable();
  105. }
  106. /**
  107. * Test the getInstance() method.
  108. */
  109. public function testGetInstance() {
  110. $idGen1 = UniqueIDGenerator::getInstance();
  111. $this->assertTrue($idGen1 instanceof UniqueIDGenerator);
  112. $idGen2 = UniqueIDGenerator::getInstance();
  113. $this->assertTrue($idGen1 === $idGen2, "getInstance() should return same object each time it is called");
  114. }
  115. /**
  116. * Test the getNextID() method
  117. */
  118. public function testGetNextID() {
  119. $table1 = "hs_hr_test_table";
  120. $col1 = "id_col";
  121. $table2 = "hs_hr_test2_table";
  122. $col2 = "id";
  123. // Initialize the id table.
  124. $this->assertTrue(mysql_query('INSERT INTO `hs_hr_unique_id`(table_name, field_name, last_id) VALUES("hs_hr_test_table", "id_col", 0)'));
  125. $this->assertTrue(mysql_query('INSERT INTO `hs_hr_unique_id`(table_name, field_name, last_id) VALUES("hs_hr_test2_table", "id", 1002)'));
  126. $idGen = UniqueIDGenerator::getInstance();
  127. // invalid table, invalid column
  128. try {
  129. $idGen->getNextID("invalid_table", "invalid_column");
  130. } catch (IDGeneratorException $e) {
  131. // expected
  132. }
  133. // valid table, invalid column
  134. try {
  135. $idGen->getNextID($table1, "inv_col");
  136. } catch (IDGeneratorException $e) {
  137. // expected
  138. }
  139. // invalid table, valid column
  140. try {
  141. $idGen->getNextID($table1, $col2);
  142. } catch (IDGeneratorException $e) {
  143. // expected
  144. }
  145. $this->assertEquals(1, $idGen->getNextId($table1, $col1));
  146. $this->assertEquals(1, $this->_getLastId($table1, $col1));
  147. $this->assertEquals(2, $idGen->getNextId($table1, $col1));
  148. $this->assertEquals(2, $this->_getLastId($table1, $col1));
  149. $this->assertEquals("TEMP003", $idGen->getNextId($table1, $col1, "TEMP"));
  150. $this->assertEquals(3, $this->_getLastId($table1, $col1));
  151. $this->assertEquals(1003, $idGen->getNextId($table2, $col2));
  152. $this->assertEquals(1003, $this->_getLastId($table2, $col2));
  153. $this->assertEquals("XYZ1004", $idGen->getNextId($table2, $col2, "XYZ"));
  154. $this->assertEquals(1004, $this->_getLastId($table2, $col2));
  155. // try with different min width
  156. $this->assertEquals("XYZ01005", $idGen->getNextId($table2, $col2, "XYZ", 5));
  157. $this->assertEquals(1005, $this->_getLastId($table2, $col2));
  158. $this->assertEquals("XYZ1006", $idGen->getNextId($table2, $col2, "XYZ", 1));
  159. $this->assertEquals(1006, $this->_getLastId($table2, $col2));
  160. // Verify that table names, column names are case insensitive
  161. try {
  162. $this->assertEquals("XYZ1007", $idGen->getNextId(strtoupper($table2), strtoupper($col2), "XYZ", 1));
  163. } catch (IDGeneratorException $e) {
  164. $this->fail("Should accept table names, columns in any case");
  165. }
  166. $this->assertEquals(1007, $this->_getLastId($table2, $col2));
  167. }
  168. /**
  169. * Test the getLastID() method
  170. */
  171. public function testGetLastID() {
  172. $table1 = "hs_hr_test_table";
  173. $col1 = "id_col";
  174. $table2 = "hs_hr_test2_table";
  175. $col2 = "id";
  176. // Initialize the id table.
  177. $this->assertTrue(mysql_query('INSERT INTO `hs_hr_unique_id`(table_name, field_name, last_id) VALUES("hs_hr_test_table", "id_col", 0)'));
  178. $this->assertTrue(mysql_query('INSERT INTO `hs_hr_unique_id`(table_name, field_name, last_id) VALUES("hs_hr_test2_table", "id", 1002)'));
  179. $idGen = UniqueIDGenerator::getInstance();
  180. // invalid table, invalid column
  181. try {
  182. $idGen->getLastID("invalid_table", "invalid_column");
  183. } catch (IDGeneratorException $e) {
  184. // expected
  185. }
  186. // valid table, invalid column
  187. try {
  188. $idGen->getLastID($table1, "inv_col");
  189. } catch (IDGeneratorException $e) {
  190. // expected
  191. }
  192. // invalid table, valid column
  193. try {
  194. $idGen->getLastID($table1, $col2);
  195. } catch (IDGeneratorException $e) {
  196. // expected
  197. }
  198. $this->assertEquals(0, $idGen->getLastID($table1, $col1));
  199. $this->assertEquals(0, $this->_getLastId($table1, $col1));
  200. $this->assertTrue(mysql_query('UPDATE `hs_hr_unique_id` SET last_id = 3 WHERE table_name = "hs_hr_test_table" AND field_name = "id_col"'));
  201. $this->assertEquals("TEMP003", $idGen->getLastID($table1, $col1, "TEMP"));
  202. $this->assertEquals(3, $this->_getLastId($table1, $col1));
  203. $this->assertEquals(1002, $idGen->getLastID($table2, $col2));
  204. $this->assertEquals(1002, $this->_getLastId($table2, $col2));
  205. $this->assertEquals("XYZ1002", $idGen->getLastID($table2, $col2, "XYZ"));
  206. $this->assertEquals(1002, $this->_getLastId($table2, $col2));
  207. // try with different min width
  208. $this->assertEquals("XYZ01002", $idGen->getLastID($table2, $col2, "XYZ", 5));
  209. $this->assertEquals(1002, $this->_getLastId($table2, $col2));
  210. $this->assertEquals("XYZ1002", $idGen->getLastID($table2, $col2, "XYZ", 1));
  211. $this->assertEquals(1002, $this->_getLastId($table2, $col2));
  212. // Verify that table names, column names are case insensitive
  213. try {
  214. $this->assertEquals("XYZ1002", $idGen->getLastID(strtoupper($table2), strtoupper($col2), "XYZ", 1));
  215. } catch (IDGeneratorException $e) {
  216. $this->fail("Should accept table names, columns in any case");
  217. }
  218. $this->assertEquals(1002, $this->_getLastId($table2, $col2));
  219. }
  220. /**
  221. * Test that all tables that need unique ID's have entries created in hs_hr_unique_id
  222. * after initTable() is run.
  223. */
  224. public function testAllTablesHaveEntries() {
  225. $idGen = UniqueIDGenerator::getInstance();
  226. $idGen->initTable();
  227. foreach ($this->tableInfo as $table) {
  228. $tableName = $table[0];
  229. $fieldName = $table[1];
  230. $prefix = $table[2];
  231. $newId = $idGen->getNextID($tableName, $fieldName, $prefix);
  232. $msg = "Invalid ID for table=$tableName, field=$fieldName. Got: $newId";
  233. if (!empty($prefix)) {
  234. // Check that newId has the correct prefix
  235. $this->assertTrue(strpos($newId, $prefix) === 0, $msg);
  236. $newId = str_replace($prefix, "", $newId);
  237. }
  238. // Check that newId is a valid integer.
  239. $this->assertTrue( ((preg_match('/^[0-9]+$/', $newId)) && (intval($newId) >= 0)), $msg);
  240. }
  241. }
  242. /**
  243. * Test the initTable method
  244. */
  245. public function testInitTable() {
  246. // A table with string ID's
  247. $langTable = "hs_hr_language";
  248. $langId = "lang_code";
  249. // A table with int ID's
  250. $cusTable = "hs_hr_customer";
  251. $cusId = "customer_id";
  252. // Try with empty hs_hr_language and hs_hr_nationality tables
  253. $idGen = UniqueIDGenerator::getInstance();
  254. $idGen->initTable();
  255. $this->assertEquals(0, $this->_getLastId($langTable, $langId));
  256. $this->assertEquals(0, $this->_getLastId($cusTable, $cusId));
  257. $this->assertTrue(mysql_query('INSERT INTO hs_hr_language(lang_code, lang_name) VALUES("LAN019", "Japanese")'));
  258. $this->assertTrue(mysql_query('INSERT INTO hs_hr_customer(customer_id, name, description, deleted) VALUES(29, "Test customer", "desc", 0)'));
  259. $idGen->initTable($this->connection);
  260. $this->assertEquals(19, $this->_getLastId($langTable, $langId));
  261. $this->assertEquals(29, $this->_getLastId($cusTable, $cusId));
  262. // Second init table doesn't change anything
  263. $idGen->initTable();
  264. $this->assertEquals(19, $this->_getLastId($langTable, $langId));
  265. $this->assertEquals(29, $this->_getLastId($cusTable, $cusId));
  266. $this->assertTrue(mysql_query('INSERT INTO hs_hr_language(lang_code, lang_name) VALUES("LAN1119", "Japanese")'));
  267. $idGen->initTable();
  268. $this->assertEquals(1119, $this->_getLastId($langTable, $langId));
  269. // Verify that an expception is thrown if an invalid format ID is found
  270. $this->assertTrue(mysql_query('INSERT INTO hs_hr_language(lang_code, lang_name) VALUES("LAX11", "Japanese")'));
  271. try {
  272. $idGen->initTable();
  273. } catch (IDGeneratorException $e) {
  274. // expected. Verify last id was not incremented.
  275. $this->assertEquals(1119, $this->_getLastId($langTable, $langId));
  276. }
  277. // Delete some entries and verify that last id is not changed.
  278. $this->assertTrue(mysql_query('DELETE FROM hs_hr_language WHERE lang_code = "LAX11"'));
  279. $this->assertEquals(1, mysql_affected_rows());
  280. $this->assertTrue(mysql_query('DELETE FROM hs_hr_language WHERE lang_code = "LAN1119"'));
  281. $this->assertEquals(1, mysql_affected_rows());
  282. $this->assertEquals(1119, $this->_getLastId($langTable, $langId));
  283. $this->assertTrue(mysql_query('DELETE FROM hs_hr_language'));
  284. $this->assertEquals(1, mysql_affected_rows());
  285. $this->assertEquals(1119, $this->_getLastId($langTable, $langId));
  286. // Init Table and verify that last id is still not changed
  287. $idGen->initTable();
  288. $this->assertEquals(1119, $this->_getLastId($langTable, $langId));
  289. }
  290. /**
  291. * Test the resetIDs method
  292. */
  293. public function testResetIDs() {
  294. // A table with string ID's
  295. $langTable = "hs_hr_language";
  296. $langId = "lang_code";
  297. // A table with int ID's
  298. $cusTable = "hs_hr_customer";
  299. $cusId = "customer_id";
  300. // Try with empty hs_hr_language and hs_hr_nationality tables
  301. $idGen = UniqueIDGenerator::getInstance();
  302. $idGen->resetIDs();
  303. $this->assertEquals(0, $this->_getLastId($langTable, $langId));
  304. $this->assertEquals(0, $this->_getLastId($cusTable, $cusId));
  305. $this->assertTrue(mysql_query('INSERT INTO hs_hr_language(lang_code, lang_name) VALUES("LAN019", "Japanese")'));
  306. $this->assertTrue(mysql_query('INSERT INTO hs_hr_customer(customer_id, name, description, deleted) VALUES(29, "Test customer", "desc", 0)'));
  307. $idGen->resetIDs();
  308. $this->assertEquals(19, $this->_getLastId($langTable, $langId));
  309. $this->assertEquals(29, $this->_getLastId($cusTable, $cusId));
  310. // Second reset table doesn't change anything
  311. $idGen->resetIDs();
  312. $this->assertEquals(19, $this->_getLastId($langTable, $langId));
  313. $this->assertEquals(29, $this->_getLastId($cusTable, $cusId));
  314. $this->assertTrue(mysql_query('INSERT INTO hs_hr_language(lang_code, lang_name) VALUES("LAN1119", "Japanese")'));
  315. $idGen->resetIDs();
  316. $this->assertEquals(1119, $this->_getLastId($langTable, $langId));
  317. // Verify that deleting entries and calling resetIDs reduces the last ID
  318. $this->assertTrue(mysql_query('DELETE FROM hs_hr_language WHERE lang_code = "LAN1119"'));
  319. $this->assertEquals(1, mysql_affected_rows());
  320. $idGen->resetIDs();
  321. $this->assertEquals(19, $this->_getLastId($langTable, $langId));
  322. $this->assertTrue(mysql_query('DELETE FROM hs_hr_language'));
  323. $idGen->resetIDs();
  324. $this->assertEquals(0, $this->_getLastId($langTable, $langId));
  325. }
  326. /**
  327. * Get the last ID for the given table and field by directly querying the table.
  328. */
  329. private function _getLastId($table, $field) {
  330. return $this->_getSingleFieldValue("hs_hr_unique_id", "last_id", "table_name = \"$table\" AND field_name = \"$field\"");
  331. }
  332. /**
  333. * Convenience method that gets a single field value with the given conditions
  334. *
  335. * @param string $table Table name
  336. * @param string $field Field name
  337. * @param string $where Where clause
  338. *
  339. * @return mixed value from the database
  340. */
  341. private function _getSingleFieldValue($table, $field, $where) {
  342. $sql = "SELECT $field FROM $table WHERE $where";
  343. $result = mysql_query($sql);
  344. $row = mysql_fetch_assoc($result);
  345. $this->assertTrue(is_array($row));
  346. $this->assertEquals(1, count($row));
  347. $this->assertTrue(isset($row[$field]));
  348. return $row[$field];
  349. }
  350. private function _backupTables($arrTableList) {
  351. foreach ($arrTableList as $table) {
  352. $result = mysql_query("SELECT * FROM `$table`");
  353. while($row = mysql_fetch_array($result, MYSQL_NUM)) {
  354. $this->oldValues["$table"][] = $row;
  355. }
  356. mysql_free_result($result);
  357. }
  358. }
  359. private function _restoreTables() {
  360. $arrTableList = array_keys($this->oldValues);
  361. foreach ($arrTableList as $table) {
  362. if ($table == 'AUTO_INC_PK_TABLE') {
  363. continue;
  364. }
  365. $this->assertTrue(mysql_query("INSERT INTO `$table` VALUES ('" . implode("', '", $this->oldValues["$table"]) . "')"), mysql_error());
  366. }
  367. }
  368. }
  369. // Call UniqueIDGeneratorTest::main() if this source file is executed directly.
  370. if (PHPUnit_MAIN_METHOD == "UniqueIDGeneratorTest::main") {
  371. UniqueIDGeneratorTest::main();
  372. }
  373. ?>