/** * Check whether the user is a project admin * * @param int $projectId Project for which to check. If not given, all projects are checked. * @return boolean */ private function _checkIsProjectAdmin($projectId = null) { $projectAdmin = false; $id = $this->getEmployeeId(); if (!empty($id)) { $gw = new ProjectAdminGateway(); $projectAdmin = $gw->isAdmin($id, $projectId); } return $projectAdmin; }
/** * Tests the isAdmin() method. */ public function testIsAdmin() { $gw = new ProjectAdminGateway(); // Verify that invalid project id's emp numbers throw exceptions try { $gw->isAdmin($empNumber = 12, $projectId = ""); $this->fail("Exception not thrown"); } catch (ProjectAdminException $e) { // Expected. } try { $gw->isAdmin($empNumber = 12, $projectId = "test"); $this->fail("Exception not thrown"); } catch (ProjectAdminException $e) { // Expected. } try { $gw->isAdmin($empNumber = "", $projectId = 1); $this->fail("Exception not thrown"); } catch (ProjectAdminException $e) { // Expected. } try { $gw->isAdmin($empNumber = "xyz", $projectId = 1); $this->fail("Exception not thrown"); } catch (ProjectAdminException $e) { // Expected. } try { $gw->isAdmin($empNumber = 1, $projectId = null); } catch (ProjectAdminException $e) { $this->fail("null project id should be allowed."); } // valid employee but not admin $this->assertFalse($gw->isAdmin($empNumber = 1, $projectId = 1)); $this->assertFalse($gw->isAdmin($empNumber = 1, $projectId = 11)); $this->assertFalse($gw->isAdmin($empNumber = 1)); // invalid emp number $this->assertFalse($gw->isAdmin($empNumber = 13, $projectId = 1)); $this->assertFalse($gw->isAdmin($empNumber = 188, $projectId = 11)); $this->assertFalse($gw->isAdmin($empNumber = 15)); $this->_insertAdmins(); // valid admin, correct project. $this->assertTrue($gw->isAdmin($empNumber = 1, $projectId = 1)); // valid admin, without giving a project $this->assertTrue($gw->isAdmin($empNumber = 1)); // valid admin, incorrect/invalid project. $this->assertFalse($gw->isAdmin($empNumber = 1, $projectId = 2)); $this->assertFalse($gw->isAdmin($empNumber = 1, $projectId = 12)); // admin with multiple projects $this->assertTrue($gw->isAdmin($empNumber = 2, $projectId = 1)); $this->assertTrue($gw->isAdmin($empNumber = 2, $projectId = 2)); $this->assertTrue($gw->isAdmin($empNumber = 2)); $this->assertFalse($gw->isAdmin($empNumber = 2, $projectId = 21)); // Deleted projects not considered when project Id not given $this->assertTrue(mysql_query("UPDATE hs_hr_project SET deleted = 1 WHERE project_id = 1")); $this->assertEquals(1, mysql_affected_rows()); $this->assertFalse($gw->isAdmin($empNumber = 1)); $this->assertTrue($gw->isAdmin($empNumber = 1, $projectId = 1)); $this->assertTrue(mysql_query("UPDATE hs_hr_project SET deleted = 1 WHERE project_id = 2")); $this->assertEquals(1, mysql_affected_rows()); $this->assertFalse($gw->isAdmin($empNumber = 2)); $this->assertTrue($gw->isAdmin($empNumber = 2, $projectId = 2)); $this->assertTrue($gw->isAdmin($empNumber = 2, $projectId = 1)); }