PageRenderTime 67ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/models/eimadmin/ProjectActivityTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 610 lines | 447 code | 66 blank | 97 comment | 1 complexity | 57eabab7cb3f4b7cef94b2ce11c00683 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. /**
  3. * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
  4. * all the essential functionalities required for any enterprise.
  5. * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
  6. *
  7. * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
  8. * the GNU General Public License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program;
  16. * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA
  18. *
  19. */
  20. // Call ProjectActivityTest::main() if this source file is executed directly.
  21. if (!defined("PHPUnit_MAIN_METHOD")) {
  22. define("PHPUnit_MAIN_METHOD", "ProjectActivityTest::main");
  23. }
  24. require_once "PHPUnit/Framework/TestCase.php";
  25. require_once "PHPUnit/Framework/TestSuite.php";
  26. require_once "testConf.php";
  27. require_once ROOT_PATH."/lib/confs/Conf.php";
  28. require_once ROOT_PATH."/lib/models/eimadmin/ProjectActivity.php";
  29. require_once ROOT_PATH."/lib/common/UniqueIDGenerator.php";
  30. /**
  31. * Test class for ProjectActivity.
  32. * Generated by PHPUnit_Util_Skeleton on 2007-07-07 at 17:44:48.
  33. */
  34. class ProjectActivityTest extends PHPUnit_Framework_TestCase {
  35. /**
  36. * Runs the test methods of this class.
  37. *
  38. * @access public
  39. * @static
  40. */
  41. public static function main() {
  42. require_once "PHPUnit/TextUI/TestRunner.php";
  43. $suite = new PHPUnit_Framework_TestSuite("ProjectActivityTest");
  44. $result = PHPUnit_TextUI_TestRunner::run($suite);
  45. }
  46. /**
  47. * Sets up the fixture, making sure table is empty and creating database
  48. * entries needed during test.
  49. *
  50. * @access protected
  51. */
  52. protected function setUp() {
  53. $conf = new Conf();
  54. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  55. mysql_select_db($conf->dbname);
  56. // NOTE: TRUNCATE TABLE resets AUTO_INCREMENT values and starts counting from the beginning.
  57. mysql_query("TRUNCATE TABLE `hs_hr_customer`", $this->connection);
  58. mysql_query("TRUNCATE TABLE `hs_hr_project`", $this->connection);
  59. mysql_query("TRUNCATE TABLE `hs_hr_project_activity`", $this->connection);
  60. // Insert a project and customer for use in the test
  61. mysql_query("INSERT INTO hs_hr_customer(customer_id, name, description, deleted) VALUES(1, 'Test customer', 'description', 0)");
  62. mysql_query("INSERT INTO hs_hr_customer(customer_id, name, description, deleted) VALUES(0, 'Internal customer', 'description', 0)");
  63. mysql_query("INSERT INTO hs_hr_project(project_id, customer_id, name, description, deleted) VALUES(0, 0, 'Internal project', 'Internal project', 0)");
  64. mysql_query("INSERT INTO hs_hr_project(project_id, customer_id, name, description, deleted) VALUES(1, 1, 'Test project 1', 'a test proj 1', 0)");
  65. mysql_query("INSERT INTO hs_hr_project(project_id, customer_id, name, description, deleted) VALUES(2, 1, 'Test project 2', 'a test proj 2', 0)");
  66. UniqueIDGenerator::getInstance()->resetIDs();
  67. }
  68. /**
  69. * Tears down the fixture, removed database entries created during test.
  70. *
  71. * @access protected
  72. */
  73. protected function tearDown() {
  74. mysql_query("TRUNCATE TABLE `hs_hr_project`", $this->connection);
  75. mysql_query("TRUNCATE TABLE `hs_hr_project_activity`", $this->connection);
  76. mysql_query("TRUNCATE TABLE `hs_hr_customer`", $this->connection);
  77. UniqueIDGenerator::getInstance()->resetIDs();
  78. }
  79. /**
  80. * Tests the ProjectActivity constructor
  81. */
  82. public function testNew() {
  83. $activity = new ProjectActivity();
  84. $this->assertNull($activity->getId(), "Activity Id should be null");
  85. $this->assertNull($activity->getName(), "Name should be null");
  86. $this->assertNull($activity->getProjectId(), "Project Id should be null");
  87. $this->assertFalse($activity->isDeleted(), "Activity was created in deleted state");
  88. $activity = new ProjectActivity(21);
  89. $this->assertEquals(21, $activity->getId(), "Activity Id not set in constructor");
  90. $this->assertNull($activity->getName(), "Name should be null");
  91. $this->assertNull($activity->getProjectId(), "Project Id should be null");
  92. $this->assertFalse($activity->isDeleted(), "Activity was created in deleted state");
  93. }
  94. /**
  95. * Tests the save() method.
  96. */
  97. public function testSave() {
  98. // Test that saving an activity without a project ID or a name is not allowed.
  99. $activity = new ProjectActivity();
  100. try {
  101. $activity->save();
  102. $this->fail("Exception not thrown");
  103. } catch (ProjectActivityException $e) {
  104. $this->assertEquals(0, $this->_getNumActivities(), "No rows should be inserted");
  105. }
  106. // Test that saving an activity without a project ID is not allowed.
  107. $activity = new ProjectActivity();
  108. $activity->setName("Test Project Activity");
  109. try {
  110. $activity->save();
  111. $this->fail("Exception not thrown");
  112. } catch (ProjectActivityException $e) {
  113. $this->assertEquals(0, $this->_getNumActivities(), "No rows should be inserted");
  114. }
  115. // Test that saving an activity without a name is not allowed.
  116. $activity = new ProjectActivity();
  117. $activity->setProjectId(1);
  118. try {
  119. $activity->save();
  120. $this->fail("Exception not thrown");
  121. } catch (ProjectActivityException $e) {
  122. $this->assertEquals(0, $this->_getNumActivities(), "No rows should be inserted");
  123. }
  124. // Save a valid new activity
  125. $activity1Id = UniqueIDGenerator::getInstance()->getLastId("hs_hr_project_activity", "activity_id") + 1;
  126. $activity1 = new ProjectActivity();
  127. $activity1->setProjectId(1);
  128. $activity1->setName("Development");
  129. $activity1->save();
  130. $this->assertEquals($activity1Id, $activity1->getId(), "activity ID not updated with auto_increment value");
  131. $result = mysql_query("SELECT * FROM hs_hr_project_activity");
  132. $this->assertEquals(1, mysql_num_rows($result), "Only one row should be inserted");
  133. $row = mysql_fetch_assoc($result);
  134. $this->_checkRow($activity1, $row);
  135. // Save a second activity.
  136. $activity2Id = UniqueIDGenerator::getInstance()->getLastId("hs_hr_project_activity", "activity_id") + 1;
  137. $activity2 = new ProjectActivity();
  138. $activity2->setProjectId(1);
  139. $activity2->setName("QA Testing");
  140. $activity2->save();
  141. $this->assertEquals($activity2Id, $activity2->getId(), "activity ID not updated with auto_increment value");
  142. $result = mysql_query("SELECT * FROM hs_hr_project_activity ORDER BY activity_id ASC");
  143. $this->assertEquals(2, mysql_num_rows($result), "Only one row should be inserted");
  144. // check both rows
  145. $this->_checkRow($activity1, mysql_fetch_assoc($result));
  146. $this->_checkRow($activity2, mysql_fetch_assoc($result));
  147. // Change attributes and save activity using existing object
  148. $activity1->setName("Updated activity");
  149. $activity1->setProjectId(2);
  150. $activity1->save();
  151. $this->assertEquals($activity1Id, $activity1->getId(), "activity ID should not change");
  152. $result = mysql_query("SELECT * FROM hs_hr_project_activity WHERE activity_id = $activity1Id");
  153. $this->_checkRow($activity1, mysql_fetch_assoc($result));
  154. // Change attributes and save activity using new object
  155. $activity3 = new ProjectActivity($activity2Id);
  156. $activity3->setProjectId(1);
  157. $activity3->setName("Installing");
  158. $activity3->save();
  159. $result = mysql_query("SELECT * FROM hs_hr_project_activity WHERE activity_id = $activity2Id");
  160. $this->_checkRow($activity3, mysql_fetch_assoc($result));
  161. // Verify that saving an activity without changes does not throw an exception
  162. try {
  163. $activity3->save();
  164. } catch (ProjectActivityException $e) {
  165. $this->fail("Saving without changes should not throw an exception");
  166. }
  167. // Verify that setting name to null and saving throws an exception
  168. $activity1->setName(null);
  169. try {
  170. $activity1->save();
  171. $this->fail("Exception not thrown");
  172. } catch (ProjectActivityException $e) {
  173. // expected
  174. }
  175. // Save an activity for the project 0
  176. $activity3Id = UniqueIDGenerator::getInstance()->getLastId("hs_hr_project_activity", "activity_id") + 1;
  177. $activity3 = new ProjectActivity();
  178. $activity3->setProjectId(0);
  179. $activity3->setName("Test internal");
  180. $activity3->save();
  181. $this->assertEquals($activity3Id, $activity3->getId(), "activity ID not updated with auto_increment value");
  182. $result = mysql_query("SELECT * FROM hs_hr_project_activity WHERE activity_id = $activity3Id");
  183. $this->_checkRow($activity3, mysql_fetch_assoc($result));
  184. }
  185. /**
  186. * Test testGetActivityList() method.
  187. */
  188. public function testGetActivityList() {
  189. // Verify that invalid project ids throw exceptions
  190. try {
  191. ProjectActivity::getActivityList("");
  192. $this->fail("Exception not thrown");
  193. } catch (ProjectActivityException $e) {
  194. // Expected
  195. }
  196. // Verify that invalid project ids throw exceptions
  197. try {
  198. ProjectActivity::getActivityList("xfe");
  199. $this->fail("Exception not thrown");
  200. } catch (ProjectActivityException $e) {
  201. // Expected
  202. }
  203. // Verify that invalid project ids throw exceptions
  204. try {
  205. ProjectActivity::getActivityList(null);
  206. $this->fail("Exception not thrown");
  207. } catch (ProjectActivityException $e) {
  208. // Expected
  209. }
  210. // Test with empty table
  211. $projId = 1;
  212. $list = ProjectActivity::getActivityList($projId);
  213. $this->assertType("array", $list);
  214. $this->assertEquals(0, count($list), "List should be empty");
  215. $list = ProjectActivity::getActivityList($projId, true);
  216. $this->assertType("array", $list);
  217. $this->assertEquals(0, count($list), "List should be empty");
  218. // create some activities
  219. $actList = $this->_getTestActivities();
  220. $this->_createActivites($actList);
  221. // query
  222. $projId = 1;
  223. $list = ProjectActivity::getActivityList($projId);
  224. $this->assertType("array", $list);
  225. $this->assertEquals(2, count($list), "2 activities should be returned.");
  226. foreach ($list as $activity) {
  227. $this->assertTrue($activity instanceof ProjectActivity, "Should return ProjectActivity objects");
  228. $id = $activity->getId();
  229. $this->assertEquals($actList[$id], $activity);
  230. $this->assertFalse($activity->isDeleted(), "Should not be deleted");
  231. $this->assertEquals($projId, $activity->getProjectId(), "Project ID not correct");
  232. }
  233. // query including deleted
  234. $projId = 1;
  235. $list = ProjectActivity::getActivityList($projId, true);
  236. $this->assertType("array", $list);
  237. $this->assertEquals(3, count($list), "3 activities should be returned.");
  238. foreach ($list as $activity) {
  239. $this->assertTrue($activity instanceof ProjectActivity, "Should return ProjectActivity objects");
  240. $id = $activity->getId();
  241. $this->assertEquals($actList[$id], $activity);
  242. $this->assertEquals($projId, $activity->getProjectId(), "Project ID not correct");
  243. }
  244. }
  245. /**
  246. * Tests getActivity() method
  247. */
  248. public function testGetActivity() {
  249. // Verify that invalid project ids throw exceptions
  250. try {
  251. ProjectActivity::getActivity("");
  252. $this->fail("Exception not thrown");
  253. } catch (ProjectActivityException $e) {
  254. // Expected
  255. }
  256. // Verify that invalid project ids throw exceptions
  257. try {
  258. ProjectActivity::getActivity("xfe");
  259. $this->fail("Exception not thrown");
  260. } catch (ProjectActivityException $e) {
  261. // Expected
  262. }
  263. // Verify that invalid project ids throw exceptions
  264. try {
  265. ProjectActivity::getActivity(null);
  266. $this->fail("Exception not thrown");
  267. } catch (ProjectActivityException $e) {
  268. // Expected
  269. }
  270. // non existant activity id.
  271. $obj = ProjectActivity::getActivity(1);
  272. $this->assertNull($obj);
  273. // create some activities
  274. $actList = $this->_getTestActivities();
  275. $this->_createActivites($actList);
  276. $obj = ProjectActivity::getActivity(2);
  277. $this->assertNotNull($obj);
  278. $this->assertTrue($obj instanceof ProjectActivity);
  279. $this->assertEquals($actList[$obj->getId()], $obj);
  280. // verify that deleted activites are returned as well
  281. $obj = ProjectActivity::getActivity(3);
  282. $this->assertNotNull($obj);
  283. $this->assertTrue($obj instanceof ProjectActivity);
  284. $this->assertTrue($obj->isDeleted());
  285. $this->assertEquals($actList[$obj->getId()], $obj);
  286. // non existant activity id (with entries in table)
  287. $obj = ProjectActivity::getActivity(5);
  288. $this->assertNull($obj);
  289. }
  290. /**
  291. * test testgetActivitiesWithName() method.
  292. */
  293. public function testGetActivitiesWithName() {
  294. // Verify that invalid project ids throw exceptions
  295. try {
  296. ProjectActivity::getActivitiesWithName("", "Test");
  297. $this->fail("Exception not thrown");
  298. } catch (ProjectActivityException $e) {
  299. // Expected
  300. }
  301. // Verify that invalid project ids throw exceptions
  302. try {
  303. ProjectActivity::getActivitiesWithName("xafd", "Test");
  304. $this->fail("Exception not thrown");
  305. } catch (ProjectActivityException $e) {
  306. // Expected
  307. }
  308. // Verify that invalid project ids throw exceptions
  309. try {
  310. ProjectActivity::getActivitiesWithName(null, "Test");
  311. $this->fail("Exception not thrown");
  312. } catch (ProjectActivityException $e) {
  313. // Expected
  314. }
  315. // Test that activity name is escaped to avoid sql injection.
  316. // If not, following will throw an error.
  317. ProjectActivity::getActivitiesWithName(1, "' WHERE xkaf in (SELECT * from xaf)");
  318. // non existent name (with empty table)
  319. $list = ProjectActivity::getActivitiesWithName(1, "Test activity");
  320. $this->assertEquals(0, count($list));
  321. // create some activities
  322. $actList = $this->_getTestActivities();
  323. $this->_createActivites($actList);
  324. // non existent name
  325. $list = ProjectActivity::getActivitiesWithName(1, "Test activity 2");
  326. $this->assertEquals(0, count($list));
  327. // valid name
  328. $list = ProjectActivity::getActivitiesWithName(1, "test 1");
  329. $this->assertEquals(1, count($list));
  330. $obj = $list[0];
  331. $this->assertEquals($actList[$obj->getId()], $obj);
  332. // verify that deleted activities are not included by default
  333. $list = ProjectActivity::getActivitiesWithName(1, "test 3");
  334. $this->assertEquals(0, count($list));
  335. // include deleted activities
  336. $list = ProjectActivity::getActivitiesWithName(1, "test 3", true);
  337. $this->assertEquals(1, count($list));
  338. $obj = $list[0];
  339. $this->assertEquals($actList[$obj->getId()], $obj);
  340. // multiple matches
  341. mysql_query("UPDATE hs_hr_project_activity SET name = 'test name' where project_id = 1");
  342. $list = ProjectActivity::getActivitiesWithName(1, "test name");
  343. $this->assertEquals(2, count($list));
  344. $list = ProjectActivity::getActivitiesWithName(1, "test name", true);
  345. $this->assertEquals(3, count($list));
  346. }
  347. /**
  348. * Tests the deleteActivities() method.
  349. */
  350. public function testDeleteActivities() {
  351. $projId = 1;
  352. $ids = array(1, 2, 3, 4);
  353. // Verify that invalid project ids throw exceptions
  354. try {
  355. ProjectActivity::deleteActivities($ids, "Test");
  356. $this->fail("Exception not thrown");
  357. } catch (ProjectActivityException $e) {
  358. // Expected
  359. }
  360. // Verify that invalid project ids throw exceptions
  361. try {
  362. ProjectActivity::deleteActivities($ids, "");
  363. $this->fail("Exception not thrown");
  364. } catch (ProjectActivityException $e) {
  365. // Expected
  366. }
  367. // Verify that invalid activity ids throw exceptions
  368. try {
  369. ProjectActivity::deleteActivities(null, 1);
  370. $this->fail("Exception not thrown");
  371. } catch (ProjectActivityException $e) {
  372. // Expected
  373. }
  374. // Verify that invalid activity ids throw exceptions
  375. try {
  376. ProjectActivity::deleteActivities(array(1, ""), 1);
  377. $this->fail("Exception not thrown");
  378. } catch (ProjectActivityException $e) {
  379. // Expected
  380. }
  381. // Verify that invalid activity ids throw exceptions
  382. try {
  383. ProjectActivity::deleteActivities(array(1, "ew"), 1);
  384. $this->fail("Exception not thrown");
  385. } catch (ProjectActivityException $e) {
  386. // Expected
  387. }
  388. // try deleting unavailable ids.
  389. $numDeleted = ProjectActivity::deleteActivities($ids, $projId);
  390. $this->assertEquals(0, $numDeleted);
  391. $numDeleted = ProjectActivity::deleteActivities($ids);
  392. $this->assertEquals(0, $numDeleted);
  393. // create some activites
  394. $actList = $this->_getTestActivities();
  395. $this->_createActivites($actList);
  396. mysql_query("UPDATE hs_hr_project_activity SET deleted = 0");
  397. // delete one and check
  398. $ids = array(1);
  399. $numDeleted = ProjectActivity::deleteActivities($ids);
  400. $this->assertEquals(1, $numDeleted);
  401. $num = $this->_getNumActivities("activity_id = 1 AND deleted = 1");
  402. $this->assertEquals(1, $num);
  403. $num = $this->_getNumActivities("deleted = 1");
  404. $this->assertEquals(1, $num);
  405. $num = $this->_getNumActivities("deleted = 0");
  406. $this->assertEquals(3, $num);
  407. // delete already deleted activity, verify no change
  408. $numDeleted = ProjectActivity::deleteActivities($ids);
  409. $this->assertEquals(0, $numDeleted);
  410. $num = $this->_getNumActivities("activity_id = 1 AND deleted = 1");
  411. $this->assertEquals(1, $num);
  412. $num = $this->_getNumActivities("deleted = 1");
  413. $this->assertEquals(1, $num);
  414. mysql_query("UPDATE hs_hr_project_activity SET deleted = 0");
  415. // verify that only activies in given project are deleted.
  416. // NOTE: 1,2,3 belong to projId 1, 4 to projId 2
  417. $projId = 2;
  418. $ids = array(1, 2, 3);
  419. $numDeleted = ProjectActivity::deleteActivities($ids, $projId);
  420. $this->assertEquals(0, $numDeleted);
  421. $num = $this->_getNumActivities("deleted = 1");
  422. $this->assertEquals(0, $num);
  423. $ids = array(1, 2, 3, 4);
  424. $numDeleted = ProjectActivity::deleteActivities($ids, $projId);
  425. $this->assertEquals(1, $numDeleted);
  426. $num = $this->_getNumActivities("deleted = 1");
  427. $this->assertEquals(1, $num);
  428. $num = $this->_getNumActivities("activity_id = 4 AND deleted = 1");
  429. $this->assertEquals(1, $num);
  430. // delete multiple activities
  431. $ids = array(1, 2, 3);
  432. $numDeleted = ProjectActivity::deleteActivities($ids);
  433. $this->assertEquals(3, $numDeleted);
  434. $num = $this->_getNumActivities("deleted = 1");
  435. $this->assertEquals(4, $num);
  436. }
  437. /**
  438. * Returns the number of rows in the project_activity table
  439. *
  440. * @param string $where where clause
  441. * @return int number of rows
  442. */
  443. private function _getNumActivities($where = null) {
  444. $sql = "SELECT COUNT(*) FROM hs_hr_project_activity";
  445. if (!empty($where)) {
  446. $sql .= " WHERE " . $where;
  447. }
  448. $result = mysql_query($sql);
  449. $row = mysql_fetch_array($result, MYSQL_NUM);
  450. $count = $row[0];
  451. return $count;
  452. }
  453. /**
  454. * Checks that the attributes of the activity object and the database row match.
  455. *
  456. * @param ProjectActivity $activity
  457. * @param array $row
  458. */
  459. private function _checkRow($activity, $row) {
  460. $this->assertEquals($activity->getName(), $row['name'], "Activity name not correct");
  461. $this->assertEquals($activity->getProjectId(), $row['project_id'], "Project id wrong");
  462. $this->assertEquals($activity->getId(), $row['activity_id'], "Activity id wrong");
  463. $this->assertEquals($activity->isDeleted(), (bool)$row['deleted'], "Deleted value wrong");
  464. }
  465. /**
  466. * Creates some ProjectActivity objects for use in the tests
  467. * @return array Array of ProjectActivity objects
  468. */
  469. private function _getTestActivities() {
  470. $activities['1'] = $this->_getActivityObject(1, 1, "test 1", false);
  471. $activities['2'] = $this->_getActivityObject(2, 1, "test 2", false);
  472. $activities['3'] = $this->_getActivityObject(3, 1, "test 3", true);
  473. $activities['4'] = $this->_getActivityObject(4, 2, "test 4", false);
  474. return $activities;
  475. }
  476. /**
  477. * Create a ProjectActivity object with the passed parameters
  478. */
  479. private function _getActivityObject($activity_id, $project_id, $name, $deleted) {
  480. $activity = new ProjectActivity($activity_id);
  481. $activity->setProjectId($project_id);
  482. $activity->setName($name);
  483. $activity->setDeleted($deleted);
  484. return $activity;
  485. }
  486. /**
  487. * Saves the given Project Activity objects in the databas
  488. *
  489. * @param ProjectActivity $activities ProjectActivity objects to save.
  490. */
  491. private function _createActivites($activities) {
  492. foreach ($activities as $activity) {
  493. $sql = sprintf("INSERT INTO hs_hr_project_activity(activity_id, project_id, name, deleted) " .
  494. "VALUES(%d, %d, '%s', %d)",
  495. $activity->getId(), $activity->getProjectId(), $activity->getName(),
  496. ($activity->isDeleted() ? 1 : 0));
  497. mysql_query($sql);
  498. UniqueIDGenerator::getInstance()->initTable();
  499. }
  500. }
  501. }
  502. // Call ProjectActivityTest::main() if this source file is executed directly.
  503. if (PHPUnit_MAIN_METHOD == "ProjectActivityTest::main") {
  504. ProjectActivityTest::main();
  505. }
  506. ?>