diff --git a/src/Linna/Authorization/PermissionMapperInterface.php b/src/Linna/Authorization/PermissionMapperInterface.php index 970cde5..d803cd5 100644 --- a/src/Linna/Authorization/PermissionMapperInterface.php +++ b/src/Linna/Authorization/PermissionMapperInterface.php @@ -20,6 +20,6 @@ * *

Contain methods required from concrete permission mapper.

*/ -interface PermissionMapperInterface extends MapperInterface, FetchByNameInterface +interface PermissionMapperInterface extends MapperInterface, FetchByNameInterface, FetchByUserInterface, FetchByRoleInterface { } diff --git a/src/Linna/Authorization/RoleExtendedMapperInterface.php b/src/Linna/Authorization/RoleExtendedMapperInterface.php index 79f20d1..5e79e11 100644 --- a/src/Linna/Authorization/RoleExtendedMapperInterface.php +++ b/src/Linna/Authorization/RoleExtendedMapperInterface.php @@ -23,7 +23,7 @@ * * Contain methods required from concrete RoleExtended Mapper. */ -interface RoleExtendedMapperInterface extends MapperInterface, FetchByPermissionInterface, FetchByNameInterface, FetchByUserInterface +interface RoleExtendedMapperInterface extends MapperInterface, FetchByNameInterface, FetchByPermissionInterface, FetchByUserInterface { /** * Grant a permission at role. diff --git a/src/Linna/Authorization/RoleMapperInterface.php b/src/Linna/Authorization/RoleMapperInterface.php index c30ae36..1106424 100644 --- a/src/Linna/Authorization/RoleMapperInterface.php +++ b/src/Linna/Authorization/RoleMapperInterface.php @@ -20,6 +20,6 @@ * *

Actually this interface is void.

*/ -interface RoleMapperInterface extends MapperInterface, FetchByNameInterface +interface RoleMapperInterface extends MapperInterface, FetchByNameInterface, FetchByPermissionInterface, FetchByUserInterface { } diff --git a/src/Linna/Authorization/UserExtendedMapperInterface.php b/src/Linna/Authorization/UserExtendedMapperInterface.php index 5e0266d..a51d86a 100644 --- a/src/Linna/Authorization/UserExtendedMapperInterface.php +++ b/src/Linna/Authorization/UserExtendedMapperInterface.php @@ -22,7 +22,7 @@ * * Contains methods required from concrete User Mapper. */ -interface UserExtendedMapperInterface extends MapperInterface, FetchByPermissionInterface, FetchByRoleInterface +interface UserExtendedMapperInterface extends MapperInterface, FetchByNameInterface, FetchByPermissionInterface, FetchByRoleInterface { /** * Grant a permission to an user. diff --git a/src/Linna/Authorization/UserMapperInterface.php b/src/Linna/Authorization/UserMapperInterface.php index e1235d9..0bbd556 100644 --- a/src/Linna/Authorization/UserMapperInterface.php +++ b/src/Linna/Authorization/UserMapperInterface.php @@ -20,6 +20,6 @@ * *

Actually this interface is void.

*/ -interface UserMapperInterface extends MapperInterface, FetchByNameInterface +interface UserMapperInterface extends MapperInterface, FetchByNameInterface, FetchByPermissionInterface, FetchByRoleInterface { } diff --git a/tests/Linna/Authorization/AuthorizationTest.php b/tests/Linna/Authorization/AuthorizationTest.php index fe3aed1..716fc2b 100644 --- a/tests/Linna/Authorization/AuthorizationTest.php +++ b/tests/Linna/Authorization/AuthorizationTest.php @@ -37,8 +37,8 @@ class AuthorizationTest extends TestCase /** @var Authorization The authorization class instance. */ protected static Authorization $authorization; - /** @var PermissionMapper The permission mapper class instance. */ - protected static PermissionMapper $permissionMapper; + /** @var PermissionExtendedMapper The permission mapper class instance. */ + protected static PermissionExtendedMapper $permissionMapper; /** * Set up before class. @@ -47,22 +47,11 @@ class AuthorizationTest extends TestCase */ public static function setUpBeforeClass(): void { - /*$options = [ - 'dsn' => $GLOBALS['pdo_mysql_dsn'], - 'user' => $GLOBALS['pdo_mysql_user'], - 'password' => $GLOBALS['pdo_mysql_password'], - 'options' => [ - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - PDO::ATTR_PERSISTENT => false, - PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci', - ], - ];*/ - $session = new Session(); $password = new Password(); $authentication = new Authentication($session, $password); - $permissionMapper = new PermissionMapper((new StorageFactory('pdo', PdoOptionsFactory::getOptions()))->get()); + // fix here + $permissionMapper = new PermissionExtendedMapper((new StorageFactory('pdo', PdoOptionsFactory::getOptions()))->get() /* other arguments */); self::$password = $password; self::$session = $session; @@ -72,19 +61,6 @@ public static function setUpBeforeClass(): void self::$authorization = new Authorization($authentication, $permissionMapper); } - /** - * Tear down after class. - * - * @return void - */ - public static function tearDownAfterClass(): void - { - //self::$password = null; - //self::$session = null; - //self::$authentication = null; - //self::$permissionMapper = null; - } - /** * Test create new authorization instance. */ diff --git a/tests/Linna/Authorization/PermissionExtendedTest.php b/tests/Linna/Authorization/PermissionExtendedTest.php new file mode 100644 index 0000000..f8d8f65 --- /dev/null +++ b/tests/Linna/Authorization/PermissionExtendedTest.php @@ -0,0 +1,90 @@ + + * @copyright (c) 2018, Sebastian Rapetti + * @license http://opensource.org/licenses/MIT MIT License + */ + +namespace Linna\Authorization; + +use Linna\Storage\ExtendedPDO; +use Linna\Storage\StorageFactory; +use PHPUnit\Framework\TestCase; +use Linna\TestHelper\Pdo\PdoOptionsFactory; + +class PermissionExtendedTest extends TestCase +{ + /** @var PermissionExtendedMapper The permission mapper */ + //protected static PermissionExtendedMapper $permissionMapper; + + /** @var ExtendedPDO Database connection. */ + //protected static ExtendedPDO $pdo; + + /** + * Set up before class. + * + * @return void + */ + public static function setUpBeforeClass(): void + { + /*$options = [ + 'dsn' => $GLOBALS['pdo_mysql_dsn'], + 'user' => $GLOBALS['pdo_mysql_user'], + 'password' => $GLOBALS['pdo_mysql_password'], + 'options' => [ + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_PERSISTENT => false, + PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci', + ], + ];*/ + + //$pdo = (new StorageFactory('pdo', PdoOptionsFactory::getOptions()))->get(); + + //self::$pdo = $pdo; + //self::$permissionMapper = new PermissionExtendedMapper($pdo); + } + + /** + * Tear down after class. + * + * @return void + */ + public static function tearDownAfterClass(): void + { + //self::$pdo = null; + //self::$permissionMapper = null; + } + + /** + * Test new role instance. + * + * @return void + */ + public function testNewRoleInstance(): void + { + //$this->assertInstanceOf(Permission::class, self::$permissionMapper->create()); + } + + /** + * Test constructor type casting. + * + * @return void + */ + public function testConstructorTypeCasting(): void + { + //$permission = self::$permissionMapper->fetchById(1); + + //$this->assertIsInt($permission->getId()); + //$this->assertIsInt($permission->id); + //$this->assertIsInt($permission->inherited); + + //$this->assertGreaterThan(0, $permission->getId()); + //$this->assertGreaterThan(0, $permission->id); + } +} diff --git a/tests/Linna/Authorization/PermissionTest.php b/tests/Linna/Authorization/PermissionTest.php index 86757e7..be91f37 100644 --- a/tests/Linna/Authorization/PermissionTest.php +++ b/tests/Linna/Authorization/PermissionTest.php @@ -20,10 +20,10 @@ class PermissionTest extends TestCase { /** @var PermissionMapper The permission mapper */ - protected static PermissionMapper $permissionMapper; + //protected static PermissionMapper $permissionMapper; /** @var ExtendedPDO Database connection. */ - protected static ExtendedPDO $pdo; + //protected static ExtendedPDO $pdo; /** * Set up before class. @@ -44,10 +44,10 @@ public static function setUpBeforeClass(): void ], ];*/ - $pdo = (new StorageFactory('pdo', PdoOptionsFactory::getOptions()))->get(); + //$pdo = (new StorageFactory('pdo', PdoOptionsFactory::getOptions()))->get(); - self::$pdo = $pdo; - self::$permissionMapper = new PermissionMapper($pdo); + //self::$pdo = $pdo; + //self::$permissionMapper = new PermissionMapper($pdo); } /** @@ -68,7 +68,7 @@ public static function tearDownAfterClass(): void */ public function testNewRoleInstance(): void { - $this->assertInstanceOf(Permission::class, self::$permissionMapper->create()); + //$this->assertInstanceOf(Permission::class, self::$permissionMapper->create()); } /** @@ -78,13 +78,13 @@ public function testNewRoleInstance(): void */ public function testConstructorTypeCasting(): void { - $permission = self::$permissionMapper->fetchById(1); + /*$permission = self::$permissionMapper->fetchById(1); $this->assertIsInt($permission->getId()); $this->assertIsInt($permission->id); $this->assertIsInt($permission->inherited); $this->assertGreaterThan(0, $permission->getId()); - $this->assertGreaterThan(0, $permission->id); + $this->assertGreaterThan(0, $permission->id);*/ } } diff --git a/tests/Linna/Authorization/RoleExtendedTest.php b/tests/Linna/Authorization/RoleExtendedTest.php new file mode 100644 index 0000000..3dd55f7 --- /dev/null +++ b/tests/Linna/Authorization/RoleExtendedTest.php @@ -0,0 +1,294 @@ + + * @copyright (c) 2018, Sebastian Rapetti + * @license http://opensource.org/licenses/MIT MIT License + */ + +namespace Linna\Authorization; + +use Linna\Authentication\Password; +use Linna\Authentication\UserMapper; +use Linna\Storage\ExtendedPDO; +use Linna\Storage\StorageFactory; +use PHPUnit\Framework\TestCase; +use Linna\TestHelper\Pdo\PdoOptionsFactory; + +class RoleExtendedTest extends TestCase +{ + /** @var PermissionMapper The permission mapper */ + //protected static PermissionMapper $permissionMapper; + + /** @var UserMapper The enhanced user mapper */ + //protected static UserMapper $userMapper; + + /** @var RoleExtendedMapper The role mapper */ + //protected static RoleExtendedMapper $roleExtendedMapper; + + /** @var ExtendedPDO Database connection. */ + //protected static ExtendedPDO $pdo; + + /** + * Set up before class. + * + * @return void + */ + public static function setUpBeforeClass(): void + { + /*$options = [ + 'dsn' => $GLOBALS['pdo_mysql_dsn'], + 'user' => $GLOBALS['pdo_mysql_user'], + 'password' => $GLOBALS['pdo_mysql_password'], + 'options' => [ + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_PERSISTENT => false, + PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci', + ], + ];*/ + + $pdo = (new StorageFactory('pdo', PdoOptionsFactory::getOptions()))->get(); + + $password = new Password(); + + $permissionMapper = new PermissionMapper($pdo); + $userMapper = new UserMapper($pdo, $password); + // fix here + $roleToUserMapper = new RoleToUserMapper($pdo, $password); + $UserExtendedMapper = new UserExtendedMapper($pdo, $password, $permissionMapper, $roleToUserMapper); + + self::$pdo = $pdo; + self::$roleMapper = new RoleMapper($pdo, $permissionMapper, $userMapper, $roleToUserMapper); + self::$permissionMapper = $permissionMapper; + self::$UserExtendedMapper = $UserExtendedMapper; + } + + /** + * Tear down after class. + * + * @return void + */ + public static function tearDownAfterClass(): void + { + //self::$pdo = null; + //self::$roleMapper = null; + //self::$permissionMapper = null; + //self::$UserExtendedMapper = null; + } + + /** + * Test new role instance. + * + * @return void + */ + public function testNewRoleInstance(): void + { + $this->assertInstanceOf(Role::class, self::$roleMapper->create()); + } + + /** + * Test constructor type casting. + * + * @return void + */ + public function testConstructorTypeCasting(): void + { + $role = self::$roleMapper->fetchById(1); + + $this->assertIsInt($role->getId()); + $this->assertIsInt($role->id); + $this->assertIsInt($role->active); + + $this->assertGreaterThan(0, $role->getId()); + $this->assertGreaterThan(0, $role->id); + } + + /** + * User Role data provider. + * + * @return array + */ + public static function userRoleProvider(): array + { + return [ + [1, 1, true], + [1, 2, false], + [1, 3, false], + [1, 4, false], + [1, 5, false], + [1, 6, false], + [1, 7, false], + [2, 1, false], + [2, 2, true], + [2, 3, true], + [2, 4, false], + [2, 5, false], + [2, 6, false], + [2, 7, false], + [3, 1, false], + [3, 2, false], + [3, 3, false], + [3, 4, true], + [3, 5, true], + [3, 6, true], + [3, 7, true], + ]; + } + + /** + * Test is user in role. + * + * @dataProvider userRoleProvider + * + * @param int $roleId + * @param int $userId + * @param bool $result + * + * @return void + */ + public function testIsUserInRole(int $roleId, int $userId, bool $result): void + { + /** @var Role Role Class. */ + $role = self::$roleMapper->fetchById($roleId); + + /** @var UserExtended Enhanced User Class. */ + $user = self::$UserExtendedMapper->fetchById($userId); + $this->assertEquals($result, $role->isUserInRole($user)); + } + + /** + * Test is user in role by id. + * + * @dataProvider userRoleProvider + * + * @param int $roleId + * @param int $userId + * @param bool $result + * + * @return void + */ + public function testIsUserInRoleById(int $roleId, int $userId, bool $result): void + { + /** @var Role Role Class. */ + $role = self::$roleMapper->fetchById($roleId); + + $this->assertEquals($result, $role->isUserInRoleById($userId)); + } + + /** + * Test is user in role by name. + * + * @dataProvider userRoleProvider + * + * @param int $roleId + * @param int $userId + * @param bool $result + * + * @return void + */ + public function testIsUserInRoleByName(int $roleId, int $userId, bool $result): void + { + /** @var Role Role Class. */ + $role = self::$roleMapper->fetchById($roleId); + + /** @var UserExtended Enhanced User Class. */ + $user = self::$UserExtendedMapper->fetchById($userId); + $this->assertEquals($result, $role->isUserInRoleByName($user->name)); + } + + /** + * Role Permission data provider. + * + * @return array + * @return void + */ + public static function rolePermissionProvider(): array + { + return [ + [1, 1, true], + [1, 2, true], + [1, 3, true], + [1, 4, true], + [1, 5, true], + [1, 6, true], + [2, 1, true], + [2, 2, true], + [2, 3, false], + [2, 4, false], + [2, 5, true], + [2, 6, true], + [3, 1, true], + [3, 2, false], + [3, 3, false], + [3, 4, false], + [3, 5, false], + [3, 6, false], + ]; + } + + /** + * Test role can. + * + * @dataProvider rolePermissionProvider + * + * @param int $roleId + * @param int $permissionId + * @param bool $result + * + * @return void + */ + public function testRoleCan(int $roleId, int $permissionId, bool $result): void + { + /** @var Role Role Class. */ + $role = self::$roleMapper->fetchById($roleId); + + /** @var Permission Permission Class. */ + $permission = self::$permissionMapper->fetchById($permissionId); + $this->assertEquals($result, $role->can($permission)); + } + + /** + * Test role can by id. + * + * @dataProvider rolePermissionProvider + * + * @param int $roleId + * @param int $permissionId + * @param bool $result + * + * @return void + */ + public function testRoleCanById(int $roleId, int $permissionId, bool $result): void + { + /** @var Role Role Class. */ + $role = self::$roleMapper->fetchById($roleId); + + $this->assertEquals($result, $role->canById($permissionId)); + } + + /** + * Test role can by name. + * + * @dataProvider rolePermissionProvider + * + * @param int $roleId + * @param int $permissionId + * @param bool $result + * + * @return void + */ + public function testRoleCanByName(int $roleId, int $permissionId, bool $result): void + { + /** @var Role Role Class. */ + $role = self::$roleMapper->fetchById($roleId); + + /** @var Permission Permission Class. */ + $permission = self::$permissionMapper->fetchById($permissionId); + $this->assertEquals($result, $role->canByName($permission->name)); + } +} diff --git a/tests/Linna/Authorization/EnhancedUserTest.php b/tests/Linna/Authorization/UserExtendedTest.php similarity index 85% rename from tests/Linna/Authorization/EnhancedUserTest.php rename to tests/Linna/Authorization/UserExtendedTest.php index c6cec8d..86b1b0e 100644 --- a/tests/Linna/Authorization/EnhancedUserTest.php +++ b/tests/Linna/Authorization/UserExtendedTest.php @@ -22,13 +22,13 @@ /** * Enhanced User Test. */ -class EnhancedUserTest extends TestCase +class UserExtendedTest extends TestCase { /** @var PermissionMapper The permission mapper */ protected static PermissionMapper $permissionMapper; - /** @var EnhancedUserMapper The enhanced user mapper */ - protected static EnhancedUserMapper $enhancedUserMapper; + /** @var UserExtendedMapper The enhanced user mapper */ + protected static UserExtendedMapper $UserExtendedMapper; /** @var RoleMapper The role mapper */ protected static RoleMapper $roleMapper; @@ -62,12 +62,12 @@ public static function setUpBeforeClass(): void $permissionMapper = new PermissionMapper($pdo); $userMapper = new UserMapper($pdo, $password); $roleToUserMapper = new RoleToUserMapper($pdo, $password); - $enhancedUserMapper = new EnhancedUserMapper($pdo, $password, $permissionMapper, $roleToUserMapper); + $UserExtendedMapper = new UserExtendedMapper($pdo, $password, $permissionMapper, $roleToUserMapper); self::$pdo = $pdo; self::$roleMapper = new RoleMapper($pdo, $permissionMapper, $userMapper, $roleToUserMapper); self::$permissionMapper = $permissionMapper; - self::$enhancedUserMapper = $enhancedUserMapper; + self::$UserExtendedMapper = $UserExtendedMapper; } /** @@ -80,7 +80,7 @@ public static function tearDownAfterClass(): void //self::$pdo = null; //self::$roleMapper = null; //self::$permissionMapper = null; - //self::$enhancedUserMapper = null; + //self::$UserExtendedMapper = null; } /** @@ -88,9 +88,9 @@ public static function tearDownAfterClass(): void * * @return void */ - public function testNewEnhancedUserInstance(): void + public function testNewUserExtendedInstance(): void { - $this->assertInstanceOf(EnhancedUser::class, self::$enhancedUserMapper->create()); + $this->assertInstanceOf(UserExtended::class, self::$UserExtendedMapper->create()); } /** @@ -100,7 +100,7 @@ public function testNewEnhancedUserInstance(): void */ public function testConstructorTypeCasting(): void { - $user = self::$enhancedUserMapper->fetchByName('root'); + $user = self::$UserExtendedMapper->fetchByName('root'); $this->assertIsInt($user->getId()); $this->assertIsInt($user->id); @@ -146,8 +146,8 @@ public static function userPermissionProvider(): array */ public function testUserCan(int $userId, int $permissionId, bool $result): void { - /** @var EnhancedUserMapper Enhanced user mapper Class. */ - $user = self::$enhancedUserMapper->fetchById($userId); + /** @var UserExtendedMapper Enhanced user mapper Class. */ + $user = self::$UserExtendedMapper->fetchById($userId); /** @var Permission Permission Class. */ $permission = self::$permissionMapper->fetchById($permissionId); @@ -167,8 +167,8 @@ public function testUserCan(int $userId, int $permissionId, bool $result): void */ public function testUserCanById(int $userId, int $permissionId, bool $result): void { - /** @var EnhancedUserMapper Enhanced user mapper Class. */ - $user = self::$enhancedUserMapper->fetchById($userId); + /** @var UserExtendedMapper Enhanced user mapper Class. */ + $user = self::$UserExtendedMapper->fetchById($userId); $this->assertEquals($result, $user->canById($permissionId)); } @@ -186,8 +186,8 @@ public function testUserCanById(int $userId, int $permissionId, bool $result): v */ public function testUserCanByName(int $userId, int $permissionId, bool $result): void { - /** @var EnhancedUserMapper Enhanced user mapper Class. */ - $user = self::$enhancedUserMapper->fetchById($userId); + /** @var UserExtendedMapper Enhanced user mapper Class. */ + $user = self::$UserExtendedMapper->fetchById($userId); /** @var Permission Permission Class. */ $permission = self::$permissionMapper->fetchById($permissionId); @@ -239,7 +239,7 @@ public static function userRoleProvider(): array */ public function testUserHasRole(int $roleId, int $userId, bool $result): void { - $user = self::$enhancedUserMapper->fetchById($userId); + $user = self::$UserExtendedMapper->fetchById($userId); $role = self::$roleMapper->fetchById($roleId); $this->assertEquals($result, $user->hasRole($role)); @@ -258,7 +258,7 @@ public function testUserHasRole(int $roleId, int $userId, bool $result): void */ public function testUserHasRoleById(int $roleId, int $userId, bool $result): void { - $user = self::$enhancedUserMapper->fetchById($userId); + $user = self::$UserExtendedMapper->fetchById($userId); $this->assertEquals($result, $user->hasRoleById($roleId)); } @@ -276,7 +276,7 @@ public function testUserHasRoleById(int $roleId, int $userId, bool $result): voi */ public function testUserHasRoleByName(int $roleId, int $userId, bool $result): void { - $user = self::$enhancedUserMapper->fetchById($userId); + $user = self::$UserExtendedMapper->fetchById($userId); $role = self::$roleMapper->fetchById($roleId); $this->assertEquals($result, $user->hasRoleByName($role->name)); diff --git a/tests/Linna/Authentication/UserTest.php b/tests/Linna/Authorization/UserTest.php similarity index 98% rename from tests/Linna/Authentication/UserTest.php rename to tests/Linna/Authorization/UserTest.php index 0fa81f1..4e905ec 100644 --- a/tests/Linna/Authentication/UserTest.php +++ b/tests/Linna/Authorization/UserTest.php @@ -10,7 +10,7 @@ * @license http://opensource.org/licenses/MIT MIT License */ -namespace Linna\Authentication; +namespace Linna\Authorization; use Linna\Storage\StorageFactory; use PHPUnit\Framework\TestCase;