diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2019-07-28 17:14:42 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2019-07-29 00:58:06 +0200 |
commit | 219c54411bb765bebd7813ad3e49ab05acf0b150 (patch) | |
tree | 9cf1cea8fccac16030e308df63352c2237311c0e /tests/Unit/Models | |
parent | c1f6374f377d7b6d2aeefa464837226279a521a3 (diff) |
Permissions: Added models and migrations
Diffstat (limited to 'tests/Unit/Models')
-rw-r--r-- | tests/Unit/Models/Auth/AuthModelTest.php | 109 | ||||
-rw-r--r-- | tests/Unit/Models/Auth/PermissionTest.php | 50 | ||||
-rw-r--r-- | tests/Unit/Models/Auth/RoleTest.php | 68 | ||||
-rw-r--r-- | tests/Unit/Models/TeamTest.php | 71 | ||||
-rw-r--r-- | tests/Unit/Models/User/UserTest.php | 72 |
5 files changed, 367 insertions, 3 deletions
diff --git a/tests/Unit/Models/Auth/AuthModelTest.php b/tests/Unit/Models/Auth/AuthModelTest.php new file mode 100644 index 00000000..754e120a --- /dev/null +++ b/tests/Unit/Models/Auth/AuthModelTest.php @@ -0,0 +1,109 @@ +<?php + +namespace Engelsystem\Test\Unit\Models\Auth; + +use Engelsystem\Models\Auth\Permission; +use Engelsystem\Models\Auth\Role; +use Engelsystem\Models\BaseModel; +use Engelsystem\Models\Team; +use Engelsystem\Models\User\User; +use Engelsystem\Test\Unit\Models\ModelTest; + +abstract class AuthModelTest extends ModelTest +{ + /** @var array */ + protected $user = [ + 'name' => 'Test User', + 'password' => 'nope', + 'email' => 'foo@bar.batz', + 'api_key' => 'Test123', + ]; + + /** @var array */ + protected $team = [ + 'name' => 'Test Team', + ]; + + /** @var array */ + protected $role = [ + 'name' => 'Test Role', + ]; + + /** @var array */ + protected $permission = [ + 'name' => 'foo.bar', + ]; + + /** + * @param array $data + * @return Permission + */ + protected function getPermission($data = []) + { + /** @var Permission $model */ + $model = $this->getModel(Permission::class, array_merge($this->permission, $data)); + + return $model; + } + + /** + * @param Permission|Permission[] $permissions + * @param array $data + * @return Role + */ + protected function getRole($permissions = [], $data = []) + { + /** @var Role $model */ + $model = $this->getModel(Role::class, array_merge($this->role, $data), ['permissions' => $permissions]); + + return $model; + } + + /** + * @param Role|Role[] $roles + * @param array $data + * @return Team + */ + protected function getTeam($roles = [], $data = []) + { + /** @var Team $model */ + $model = $this->getModel(Team::class, array_merge($this->team, $data), ['roles' => $roles]); + + return $model; + } + + /** + * @param Team|Team[] $teams + * @param array $data + * @return User + */ + protected function getUser($teams = [], $data = []) + { + /** @var User $model */ + $model = $this->getModel(User::class, array_merge($this->user, $data), ['teams' => $teams]); + + return $model; + } + + /** + * @param string $class BaseModel implementation name + * @param array $data + * @param array $attachments + * @return BaseModel + */ + protected function getModel($class, $data = [], $attachments = []) + { + /** @var BaseModel $instance */ + $instance = new $class($data); + $instance->save(); + + foreach ($attachments as $type => $attachment) { + $objects = is_object($attachment) ? [$attachment] : $attachment; + foreach ($objects as $object) { + $instance->$type()->attach($object); + } + } + + return $instance; + } +} diff --git a/tests/Unit/Models/Auth/PermissionTest.php b/tests/Unit/Models/Auth/PermissionTest.php new file mode 100644 index 00000000..bb5f7605 --- /dev/null +++ b/tests/Unit/Models/Auth/PermissionTest.php @@ -0,0 +1,50 @@ +<?php + +namespace Engelsystem\Test\Unit\Models\Auth; + +use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; +use Engelsystem\Models\Auth\Permission; + +class PermissionTest extends AuthModelTest +{ + use ArraySubsetAsserts; + + /** @var array */ + protected $data = [ + 'name' => 'foo.bar', + 'description' => 'Allows bar in foo', + ]; + + /** + * @covers \Engelsystem\Models\Auth\Permission::roles + */ + public function testPermissions() + { + $role = $this->getRole(); + $role2 = $this->getRole([], ['name' => 'Example Role']); + + $permission = new Permission($this->data); + $permission->save(); + $permission->roles()->attach($role); + $permission->roles()->attach($role2); + + $this->assertCount(2, $permission->roles); + $this->assertEquals('Test Role', $permission->roles()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\Auth\Permission::users + */ + public function testUsers() + { + $role = $this->getRole(); + $team = $this->getTeam($role); + $this->getUser($team); + + $permission = new Permission($this->data); + $permission->save(); + $permission->roles()->attach($role); + + $this->assertEquals('Test User', $permission->users()->get()->first()->name); + } +} diff --git a/tests/Unit/Models/Auth/RoleTest.php b/tests/Unit/Models/Auth/RoleTest.php new file mode 100644 index 00000000..10c22f9c --- /dev/null +++ b/tests/Unit/Models/Auth/RoleTest.php @@ -0,0 +1,68 @@ +<?php + +namespace Engelsystem\Test\Unit\Models\Auth; + +use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; +use Engelsystem\Models\Auth\Role; + +class RoleTest extends AuthModelTest +{ + use ArraySubsetAsserts; + + /** @var array */ + protected $data = [ + 'name' => 'Test Role', + 'description' => 'Lorem Ipsum', + ]; + + /** + * @covers \Engelsystem\Models\Auth\Role::permissions + */ + public function testPermissions() + { + $permission = $this->getPermission(); + $permission2 = $this->getPermission(['name' => 'Another Permission']); + + $role = new Role($this->data); + $role->save(); + $role->permissions()->attach($permission); + $role->permissions()->attach($permission2); + + $this->assertCount(2, $role->permissions); + $this->assertEquals('foo.bar', $role->permissions()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\Auth\Role::teams + */ + public function testTeams() + { + $team = $this->getTeam(); + + $role = new Role($this->data); + $role->save(); + $role->teams()->attach($team); + + $this->assertEquals('Test Team', $role->teams()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\Auth\Role::users + */ + public function testUsers() + { + $team = $this->getTeam(); + $team2 = $this->getTeam([], ['name' => 'Dev']); + $this->getUser($team); + $this->getUser($team, ['name' => 'Foo', 'email' => 'foo@xample.com']); + $this->getUser($team2, ['name' => 'Bar', 'email' => 'bar@xample.com']); + + $role = new Role($this->data); + $role->save(); + $role->teams()->attach($team); + $role->teams()->attach($team2); + + $this->assertCount(3, $role->users()->get()); + $this->assertEquals('Test User', $role->users()->get()->first()->name); + } +} diff --git a/tests/Unit/Models/TeamTest.php b/tests/Unit/Models/TeamTest.php new file mode 100644 index 00000000..fdd5be44 --- /dev/null +++ b/tests/Unit/Models/TeamTest.php @@ -0,0 +1,71 @@ +<?php + +namespace Engelsystem\Test\Unit\Models; + +use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; +use Engelsystem\Models\Team; +use Engelsystem\Test\Unit\Models\Auth\AuthModelTest; + +class TeamTest extends AuthModelTest +{ + use ArraySubsetAsserts; + + /** @var array */ + protected $data = [ + 'name' => 'Example team', + 'description' => 'Lorem Ipsum', + 'contact_name' => 'Con Tact', + 'contact_dect' => '1337', + 'contact_email' => 'foo@bar.batz', + 'restricted' => false, + 'self_signup' => true, + 'requires_drivers_license' => false, + 'show_on_frontend' => true, + 'show_on_dashboard' => true, + ]; + + /** + * @covers \Engelsystem\Models\Team::roles + */ + public function testRoles() + { + $role = $this->getRole(); + $role2 = $this->getRole([], ['name' => 'Another Role']); + + $team = new Team($this->data); + $team->save(); + $team->roles()->attach($role); + $team->roles()->attach($role2); + + $this->assertCount(2, $team->roles); + $this->assertEquals('Test Role', $team->roles()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\Team::supporters + */ + public function testSupporters() + { + $user = $this->getUser(); + + $team = new Team($this->data); + $team->save(); + $team->supporters()->attach($user); + + $this->assertEquals('Test User', $team->supporters()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\Team::users + */ + public function testUsers() + { + $user = $this->getUser(); + + $team = new Team($this->data); + $team->save(); + $team->users()->attach($user); + + $this->assertEquals('Test User', $team->users()->get()->first()->name); + } +} diff --git a/tests/Unit/Models/User/UserTest.php b/tests/Unit/Models/User/UserTest.php index a0d20bc2..4696d56a 100644 --- a/tests/Unit/Models/User/UserTest.php +++ b/tests/Unit/Models/User/UserTest.php @@ -1,6 +1,6 @@ <?php -namespace Engelsystem\Test\Unit\Models; +namespace Engelsystem\Test\Unit\Models\User; use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; use Engelsystem\Models\User\Contact; @@ -9,12 +9,13 @@ use Engelsystem\Models\User\PersonalData; use Engelsystem\Models\User\Settings; use Engelsystem\Models\User\State; use Engelsystem\Models\User\User; -use Engelsystem\Test\Unit\Models\ModelTest; +use Engelsystem\Test\Unit\Models\Auth\AuthModelTest; -class UserTest extends ModelTest +class UserTest extends AuthModelTest { use ArraySubsetAsserts; + /** @var array */ protected $data = [ 'name' => 'lorem', 'password' => '', @@ -87,4 +88,69 @@ class UserTest extends ModelTest $this->assertArraySubset($data, (array)$user->{$name}->attributesToArray()); } + + /** + * @covers \Engelsystem\Models\User\User::permissions + */ + public function testPermissions() + { + $permission = $this->getPermission(); + $role = $this->getRole($permission); + $team = $this->getTeam($role); + + $user = new User($this->data); + $user->save(); + $user->teams()->attach($team); + + $this->assertEquals('foo.bar', $user->permissions()->first()->name); + } + + /** + * @covers \Engelsystem\Models\User\User::roles + */ + public function testRoles() + { + $role = $this->getRole(); + $role2 = $this->getRole([], ['name' => 'Foo Role']); + $team = $this->getTeam([$role, $role2]); + + $user = new User($this->data); + $user->save(); + $user->teams()->attach($team); + + $this->assertCount(2, $user->roles); + $this->assertEquals('Test Role', $user->roles()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\User\User::supports + */ + public function testSupports() + { + $team = $this->getTeam(); + + $user = new User($this->data); + $user->save(); + $user->teams()->attach($team); + $user->supports()->attach($team); + + $this->assertEquals('Test Team', $user->supports()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\User\User::teams + */ + public function testTeams() + { + $team = $this->getTeam(); + $team2 = $this->getTeam([], ['name' => 'Another Team']); + + $user = new User($this->data); + $user->save(); + $user->teams()->attach($team); + $user->teams()->attach($team2); + + $this->assertCount(2, $user->teams); + $this->assertEquals('Test Team', $user->teams()->get()->first()->name); + } } |