From 7d4b1d9249b59676abc9a100a2a11ab46c5b6591 Mon Sep 17 00:00:00 2001 From: AlexandraLivadas Date: Tue, 23 Jun 2020 17:20:03 -0400 Subject: [PATCH 1/3] [Test] unittests for User.class.inc --- test/unittests/UserTest.php | 156 +++++++++++++++++++++++++++++++++--- 1 file changed, 145 insertions(+), 11 deletions(-) diff --git a/test/unittests/UserTest.php b/test/unittests/UserTest.php index c5ac8667085..8f6cbaffd83 100644 --- a/test/unittests/UserTest.php +++ b/test/unittests/UserTest.php @@ -88,10 +88,10 @@ class UserTest extends TestCase * @var array */ private $_projectInfo = array(0 => array('ProjectID' => '1', - 'Name' => 'project_test'), - 1 => array('ProjectID' => '3', - 'Name' => 'project_test2') - ); + 'Name' => 'project_test'), + 1 => array('ProjectID' => '3', + 'Name' => 'project_test2') + ); /** @@ -120,10 +120,10 @@ class UserTest extends TestCase * @var array */ private $_uprojrInfo = array(0 => array('UserID' => '1', - 'ProjectID' => '1'), - 1 => array('UserID' => '1', - 'ProjectID' => '3') - ); + 'ProjectID' => '1'), + 1 => array('UserID' => '1', + 'ProjectID' => '3') + ); /** * Examiners_psc_rel table information * @@ -138,6 +138,7 @@ class UserTest extends TestCase 'active' => 'Y', 'pending_approval' => 'N') ); + /** * User object used for testing * @@ -177,7 +178,7 @@ class UserTest extends TestCase * This can be changed when the rest of the User class updates how it * declares its database. - Alexandra Livadas * - *@var \Database | PHPUnit_Framework_MockObject_MockObject + * @var \Database | PHPUnit_Framework_MockObject_MockObject */ private $_mockDB; /** @@ -370,6 +371,21 @@ public function testGetCenterIDs() ); } + /** + * Test that getProjectIDs returns the correct array of project IDs of the user + * + * @return void + * @covers User::getProjectIDs + */ + public function testGetProjectIDs() + { + $this->_user = \User::factory(self::USERNAME); + $this->assertEquals( + $this->_userInfoComplete['ProjectIDs'], + $this->_user->getProjectIDs() + ); + } + /** * Test that getLanguagePreference returns the correct integer from the user * @@ -489,6 +505,30 @@ public function testHasCenterWhenFalse() $this->assertFalse($this->_user->hasCenter(5)); } + /** + * Test that hasProject returns true when the user has this project ID + * + * @return void + * @covers User::hasProject + */ + public function testHasProjectWhenTrue() + { + $this->_user = \User::factory(self::USERNAME); + $this->assertTrue($this->_user->hasProject(3)); + } + + /** + * Test that hasProject returns false when the user does not have this project ID + * + * @return void + * @covers User::hasProject + */ + public function testHasProjectWhenFalse() + { + $this->_user = \User::factory(self::USERNAME); + $this->assertFalse($this->_user->hasProject(5)); + } + /** * Test that isUserDCC returns true when the user belongs to DCC * @@ -642,7 +682,8 @@ public function testHasLoggedInWhenFalse() * @return void * @covers User::isPasswordDifferent */ - public function testPasswordChangedReturnsTrue() { + public function testPasswordChangedReturnsTrue() + { $this->_user = \User::factory(self::USERNAME); // Should return true (i.e. the password has changed) because random // strings should not generate a match. @@ -660,7 +701,8 @@ public function testPasswordChangedReturnsTrue() { * @return void * @covers User::isPasswordDifferent */ - public function testPasswordChangedReturnsFalse() { + public function testPasswordChangedReturnsFalse() + { // Update the password again to make sure another test hasn't // interfered. $this->_user = \User::factory(self::USERNAME); @@ -675,6 +717,90 @@ public function testPasswordChangedReturnsFalse() { $this->assertFalse($this->_user->isPasswordDifferent($password)); } + /** + * Test that getLastLogin returns a \DateTime object when the query returns + * a timestamp + * + * @return void + * @covers User::getLastLogin + */ + public function testGetLastLoginWhenNotEmpty() + { + $this->_user = \User::factory(self::USERNAME); + $timestamp = '2020-06-15 09:49:23'; + $this->_mockDB->expects($this->any()) + ->method('pselectOne') + ->with( + $this->stringContains("WHERE Login_timestamp <") + ) + ->willReturn($timestamp); + + $this->assertEquals( + new \DateTime('2020-06-15 09:49:23'), + $this->_user->getLastLogin($this->_mockDB) + ); + } + + /** + * Test that getLastLogin returns null when the query returns empty + * + * @return void + * @covers User::getLastLogin + */ + public function testGetLastLoginWhenEmpty() + { + $this->_user = \User::factory(self::USERNAME); + $timestamp = ''; + $this->_mockDB->expects($this->any()) + ->method('pselectOne') + ->with( + $this->stringContains("WHERE Login_timestamp <") + ) + ->willReturn($timestamp); + + $this->assertEquals( + null, + $this->_user->getLastLogin($this->_mockDB) + ); + } + + /** + * Test that isAccessibleBy returns true if the given user + * has a matching center ID and project ID + * + * @return void + * @covers User::isAccessibleBy + */ + public function testIsAccessibleByTrue() + { + $this->_user = \User::factory(self::USERNAME); + $mockUser = $this->getMockBuilder('\User')->getMock(); + $mockUser->expects($this->once())->method("getCenterIDs") + ->willReturn(array(1, 2)); + $mockUser->expects($this->once())->method("getProjectIDs") + ->willReturn(array(1, 3)); + $this->assertTrue($this->_user->isAccessibleBy($mockUser)); + } + + /** + * Test that isAccessibleBy returns false if the given user + * has no matching center IDs or project IDs + * + * @return void + * @covers User::isAccessibleBy + */ + public function testIsAccessibleByFalse() + { + $this->_user = \User::factory(self::USERNAME); + $mockUser = $this->getMockBuilder('\User')->getMock(); + $mockUser->expects($this->once())->method("getCenterIDs") + ->willReturn(array(2, 2)); + $mockUser->expects($this->once())->method("getProjectIDs") + ->willReturn(array(4, 4)); + $this->assertFalse($this->_user->isAccessibleBy($mockUser)); + } + + /** * Set up the fake tables in the database to set up a new user object * @@ -710,6 +836,14 @@ private function _setUpTestDoublesForFactoryUser() "examiners_psc_rel", $this->_eprInfo ); + $this->_dbMock->setFakeTableData( + "permissions", + $this->_permissionsInfo + ); + $this->_dbMock->setFakeTableData( + "user_perm_rel", + $this->_upermInfo + ); } } From 2416b231943387fab8d040938ca725e7023ad00f Mon Sep 17 00:00:00 2001 From: AlexandraLivadas Date: Wed, 24 Jun 2020 10:05:02 -0400 Subject: [PATCH 2/3] Fixing Travis error --- test/unittests/UserTest.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test/unittests/UserTest.php b/test/unittests/UserTest.php index 8f6cbaffd83..3d54fbaf19a 100644 --- a/test/unittests/UserTest.php +++ b/test/unittests/UserTest.php @@ -836,14 +836,6 @@ private function _setUpTestDoublesForFactoryUser() "examiners_psc_rel", $this->_eprInfo ); - $this->_dbMock->setFakeTableData( - "permissions", - $this->_permissionsInfo - ); - $this->_dbMock->setFakeTableData( - "user_perm_rel", - $this->_upermInfo - ); } } From e68f57bad73c5d7ea3b984de16fb7abdce5dfeac Mon Sep 17 00:00:00 2001 From: AlexandraLivadas Date: Tue, 30 Jun 2020 15:18:18 -0400 Subject: [PATCH 3/3] Added tests --- test/unittests/UserTest.php | 360 +++++++++++++++++++++++++++++++++++- 1 file changed, 358 insertions(+), 2 deletions(-) diff --git a/test/unittests/UserTest.php b/test/unittests/UserTest.php index 3d54fbaf19a..0654c1b3ffd 100644 --- a/test/unittests/UserTest.php +++ b/test/unittests/UserTest.php @@ -1,6 +1,6 @@ 'N') ); + private $_permInfo = array(0 => array('permID' => 1, + 'code' => "superuser", + 'description' => "superuser description", + 'categoryID' => 1), + 1 => array('permID' => 2, + 'code' => "test_permission", + 'description' => "description 1", + 'categoryID' => 2), + 2 => array('permID' => 3, + 'code' => "test_permission2", + 'description' => "description 2", + 'categoryID' => 3), + 3 => array('permID' => 4, + 'code' => "test_permission3", + 'description' => "description 3", + 'categoryID' => 4) + ); + private $_userPermInfo = array(0 => array('permID' => 1, + 'userID' => 1), + 1 => array('permID' => 2, + 'userID' => 1), + 2 => array('permID' => 3, + 'userID' => 1) + ); + private $_categoryInfo = array(0 => array('ID' => 1, + 'Description' => "superuser category"), + 1 => array('ID' => 2, + 'Description' => "category 1"), + 2 => array('ID' => 3, + 'Description' => "category 2") + ); /** * User object used for testing * @@ -800,6 +831,331 @@ public function testIsAccessibleByFalse() $this->assertFalse($this->_user->isAccessibleBy($mockUser)); } + /** + * Test that the select function from UserPermissions returns false + * if there is no user for the given username + * + * @covers UserPermissions::select + * @return void + */ + public function testUserPermissionsSelectFalse() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->assertFalse($this->_user->select("111111")); + } + + /** + * Test that the select function from UserPermissions returns true + * if the user exists and correctly sets the permissions of the user. + * Also tests that hasPermission returns true when given a correct + * permission code + * + * @covers UserPermissions::select + * @covers UserPermissions::hasPermission + * @return void + */ + public function testUserPermissionsSelectTrue() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->assertTrue($this->_user->select(self::USERNAME)); + $this->assertTrue($this->_user->hasPermission("test_permission")); + } + + /** + * Test that hasAllPermissions throws an exception if an + * empty array is given + * + * @covers UserPermissions::hasAllPermissions + * @return void + */ + public function testHasAllPermissionsThrowsException() + { + $this->_user = \User::factory(self::USERNAME); + $this->expectException('LorisException'); + $this->_user->hasAllPermissions(array()); + } + + /** + * Test that hasAllPermissions returns true if the user + * has all the permissions in the array + * + * @covers UserPermissions::hasAllPermissions + * @return void + */ + public function testHasAllPermissionsReturnsTrue() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->assertTrue( + $this->_user->hasAllPermissions( + array("superuser", "test_permission", "test_permission2") + ) + ); + } + + /** + * Test that hasAllPermissions returns false if the user does not have + * all of the permissions given in the array + * + * @covers UserPermissions::hasAllPermissions + * @covers UserPermissions::removePermissions + * @return void + */ + public function testHasAllPermissionsReturnsFalse() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->_user->removePermissions(array(3)); + $this->assertFalse( + $this->_user->hasAllPermissions( + array("superuser", "test_permission", "test_permission2") + ) + ); + } + + /** + * Test that hasAnyPermission throws an exception if the array + * given is void + * + * @covers UserPermissions::hasAnyPermission + * @return void + */ + public function testHasAnyPermissionThrowsException() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->expectException('LorisException'); + $this->_user->hasAnyPermission(array()); + } + + /** + * Test that hasAnyPermission returns true if the user has the + * given permissions + * + * @covers UserPermissions::hasAnyPermission + * @return void + */ + public function testHasAnyPermissionReturnsTrue() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->assertTrue( + $this->_user->hasAnyPermission(array("superuser", "test_permission2")) + ); + } + + /** + * Test that getPermissions returns the array of permissions the user has + * + * @covers UserPermissions::getPermissions + * @return void + */ + public function testGetPermissions() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->assertEquals( + $this->_user->getPermissions(), + array('superuser' => true, + 'test_permission' => true, + 'test_permission2' => true, + 'test_permission3' => false) + ); + } + + /** + * Test that addPermissions adds the permission with the given permID + * to the list of user permissions + * + * @covers UserPermissions::addPermissions + * @return void + */ + public function testAddPermissions() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->_user->addPermissions(array(4)); + $this->assertEquals( + $this->_user->getPermissions(), + array('superuser' => true, + 'test_permission' => true, + 'test_permission2' => true, + 'test_permission3' => true) + ); + } + + /** + * Test that removePermissions removes all the user's permissions + * if there is no parameter given + * + * @covers UserPermissions::removePermissions + * @return void + */ + public function testRemoveAllPermissions() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->_user->removePermissions(); + $this->assertEquals( + $this->_user->getPermissions(), + array('superuser' => false, + 'test_permission' => false, + 'test_permission2' => false, + 'test_permission3' => false) + ); + } + + /** + * Test that removePermissions removes the permission with the given + * permID from the user's permissions + * + * @covers UserPermissions::removePermissions + * @return void + */ + public function testRemovePermissions() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->_user->removePermissions(array(3, 4)); + $this->assertEquals( + $this->_user->getPermissions(), + array('superuser' => true, + 'test_permission' => true, + 'test_permission2' => false, + 'test_permission3' => false) + ); + } + + /** + * Test that getPermissionIDs returns the list of permission IDs for the user + * + * @covers UserPermissions::getPermissionIDs + * @return void + */ + public function testGetPermissionIDs() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->assertEquals($this->_user->getPermissionIDs(), array(1, 2, 3)); + } + + /** + * Test that getPermissionsVerbose returns the list of user permissions + * with all the information from the database + * + * @covers UserPermissions::getPermissionsVerbose + * @return void + */ + public function testGetPermissionsVerbose() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->_dbMock->setFakeTableData( + "permissions_category", + $this->_categoryInfo + ); + $this->assertEquals( + $this->_user->getPermissionsVerbose(), + array(0 => array('permID' => '1', + 'code' => "superuser", + 'description' => "superuser description", + 'type' => "superuser category"), + 1 => array('permID' => '2', + 'code' => "test_permission", + 'description' => "description 1", + 'type' => "category 1"), + 2 => array('permID' => '3', + 'code' => "test_permission2", + 'description' => "description 2", + 'type' => "category 2") + ) + ); + } + + /** + * Test that hasCenterPermission returns true when the user + * has superuser permissions + * + * @covers User::hasCenterPermission + * @return void + */ + public function testHasCenterPermissionTrueWithSuperuser() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->assertTrue($this->_user->hasCenterPermission("test", 1)); + } + + /** + * Test that hasCenterPermission returns true if the user does not have + * superuser permissions but has the given permission code and center ID + * + * @covers User::hasCenterPermission + * @covers UserPermissions::removePermissions + * @return void + */ + public function testHasCenterPermissionTrueWithoutSuperuser() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->_user->removePermissions(array(1)); + $this->assertTrue($this->_user->hasCenterPermission("test_permission", 1)); + } + + /** + * Test that hasCenterPermission returns false if the user does not have + * the given permissions code + * + * @covers User::hasCenterPermission + * @covers UserPermissions::removePermissions + * @return void + */ + public function testHasCenterPermissionIncorrectPermission() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->_user->removePermissions(array(1, 3)); + $this->assertFalse($this->_user->hasCenterPermission("test_permission2", 1)); + } + + /** + * Test that hasCenterPermission returns false if the user does not have + * superuser permissions and does not have the given center ID + * + * @covers User::hasCenterPermission + * @covers UserPermissions::removePermissions + * @return void + */ + public function testHasCenterPermissionIncorrectCenter() + { + $this->_user = \User::factory(self::USERNAME); + $this->_setPermissions(); + $this->_user->removePermissions(array(1)); + $this->assertFalse($this->_user->hasCenterPermission("test_permission", 2)); + } + + /** + * Set up user permissions. Used to reset the permissions at the beginning of + * every permissions-related unit test + * + * @return void + */ + private function _setPermissions() + { + $this->_dbMock->run("DROP TEMPORARY TABLE IF EXISTS permissions"); + $this->_dbMock->run("DROP TEMPORARY TABLE IF EXISTS user_perm_rel"); + $this->_dbMock->setFakeTableData( + "permissions", + $this->_permInfo + ); + $this->_dbMock->setFakeTableData( + "user_perm_rel", + $this->_userPermInfo + ); + $this->_user->select(self::USERNAME); + } /** * Set up the fake tables in the database to set up a new user object