diff --git a/.gitignore b/.gitignore index 3b0e2c9..31fa772 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ site/ vendor/ /.phpintel /build +.phpunit.result.cache # hack composer.lock \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index a165347..c08863f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,12 +3,12 @@ language: php dist: trusty php: - - 7.2 + - 8.0 matrix: fast_finish: true allow_failures: - - php: 7.2 + - php: 8.0 before_script: - travis_retry composer self-update diff --git a/composer.json b/composer.json index 14031aa..31936d2 100644 --- a/composer.json +++ b/composer.json @@ -11,18 +11,19 @@ }], "minimum-stability": "dev", "require": { - "php": "^7.2.5", - "illuminate/support": "~7.0", - "illuminate/cache": "~7.0" + "php": "^7.3|^8.0", + "illuminate/cache": "^8.0", + "illuminate/support": "^8.0" }, "require-dev": { - "orchestra/database": "^5.0", - "orchestra/testbench": "^5.0", - "phpunit/phpunit": "~8.0" + "orchestra/database": "^6.0", + "orchestra/testbench": "^6.0", + "phpunit/phpunit": "^9.0" }, "autoload": { "psr-4": { - "HttpOz\\Roles\\": "src/" + "HttpOz\\Roles\\": "src/", + "HttpOz\\Roles\\Database\\Factories\\": "database/factories/" } }, "autoload-dev": { @@ -31,6 +32,8 @@ } }, "config": { + "optimize-autoloader": true, + "preferred-install": "dist", "sort-packages": true }, "extra": { diff --git a/config/roles.php b/config/roles.php index 8f4b24e..2d8e030 100644 --- a/config/roles.php +++ b/config/roles.php @@ -34,7 +34,7 @@ | | Here you can enable cache and change the period for which cache should | remember the roles. Time should be in seconds, refer to official docs - | https://laravel.com/docs/5.8/cache. + | https://laravel.com/docs/8.x/cache. */ 'cache' => [ diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php deleted file mode 100644 index 4593ad0..0000000 --- a/database/factories/ModelFactory.php +++ /dev/null @@ -1,35 +0,0 @@ -define( \HttpOz\Roles\Tests\Stubs\User::class, function ( Faker\Generator $faker ) { - static $password; - - return [ - 'name' => $faker->name, - 'email' => $faker->unique()->safeEmail, - 'password' => $password ?: $password = bcrypt( 'secret' ), - 'remember_token' => Str::random( 10 ), - ]; -} ); - -$factory->define( HttpOz\Roles\Models\Role::class, function ( Faker\Generator $faker ) { - return [ - 'name' => 'Admin', - 'slug' => 'admin', - 'group' => 'system.admin' - ]; -} ); diff --git a/database/factories/RoleFactory.php b/database/factories/RoleFactory.php new file mode 100644 index 0000000..2f4baeb --- /dev/null +++ b/database/factories/RoleFactory.php @@ -0,0 +1,18 @@ + 'Admin', + 'slug' => 'admin', + 'group' => 'system.admin' + ]; + } +} diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php new file mode 100644 index 0000000..d785e02 --- /dev/null +++ b/database/factories/UserFactory.php @@ -0,0 +1,20 @@ + $this->faker->name, + 'email' => $this->faker->unique()->safeEmail, + 'password' => $this->faker->password, + 'remember_token' => $this->faker->uuid, + ]; + } +} \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index 91bfe02..e9f226d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,36 +1,29 @@ - - - - ./tests/Feature - - - - ./tests/Unit - - - - - ./src - - - - - - - - - - - - - + + + + ./src + + + + + + + + + ./tests/Feature + + + ./tests/Unit + + + + + + + + + + + diff --git a/readme.md b/readme.md index 5f7df2d..66b5230 100644 --- a/readme.md +++ b/readme.md @@ -8,19 +8,8 @@ Powerful package for handling roles in Laravel [![Latest Stable Version](https://poser.pugx.org/httpoz/roles/v/stable.svg)](https://packagist.org/packages/httpoz/roles) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/307f89bf-52b1-4d2e-9a62-350d1f5de141/small.png)](https://insight.sensiolabs.com/projects/307f89bf-52b1-4d2e-9a62-350d1f5de141) -|Laravel Version |Roles Version | -|--------|----------| -| 5.3.* | [Roles 1.4.x](https://github.com/httpoz/roles/tree/v1.4.0) | -| 5.4.* | [Roles 2.3.x](https://github.com/httpoz/roles/tree/v2.0.0) | -| 5.5.* | [Roles 3.0.x](https://github.com/httpoz/roles/tree/v3.0.0) | -| 5.6.* | [Roles 3.1.x](https://github.com/httpoz/roles/tree/v3.1.0) | -| 5.7.* | [Roles 3.2.x](https://github.com/httpoz/roles/tree/v3.2.0) | -| 5.8.* | [Roles 3.3.x](https://github.com/httpoz/roles/tree/v3.3.0) | -| 6.x | [Roles 4.x](https://github.com/httpoz/roles/tree/v4.0.0) | -| 7.x | Roles 5.x - -#### History -This project was largely inspired by Roman's [romanbican/roles](https://github.com/romanbican/roles/) Laravel package. However at the time Laravel 5.3 was released his package was not actively maintained. I have ommitted permissions in this package in favour of Laravel's [Authorization](https://laravel.com/docs/6.x/authorization). I intend to keep this package as simple and minimal as is possible. +#### Previous Versions +For Previous versions please see the [releases page](https://github.com/httpoz/roles/releases) - [Installation](#installation) - [Composer](#composer) @@ -45,7 +34,7 @@ This package is very easy to set up. There are only couple of steps. ### Composer Add the package to your project via composer. ```bash -composer require httpoz/roles:^v5.0 +composer require httpoz/roles:^v8.0 ``` ### Config File And Migrations diff --git a/src/Models/Role.php b/src/Models/Role.php index 3bc655b..4dd3a27 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -3,7 +3,9 @@ namespace HttpOz\Roles\Models; +use HttpOz\Roles\Database\Factories\RoleFactory; use HttpOz\Roles\Traits\Sluggable; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use HttpOz\Roles\Traits\RoleHasRelations; @@ -11,7 +13,7 @@ class Role extends Model implements RoleHasRelationsContract { - use Sluggable, RoleHasRelations; + use Sluggable, RoleHasRelations, HasFactory; /** * The attributes that are mass assignable. @@ -42,4 +44,9 @@ public static function findBySlug($slug) { return self::where('slug', $slug)->first(); } + protected static function newFactory() + { + return new RoleFactory(); + } + } diff --git a/src/RolesServiceProvider.php b/src/RolesServiceProvider.php index aa06ff1..b218502 100644 --- a/src/RolesServiceProvider.php +++ b/src/RolesServiceProvider.php @@ -16,21 +16,14 @@ class RolesServiceProvider extends ServiceProvider */ public function boot() { - $this->publishes([ - __DIR__ . '/../config/roles.php' => config_path('roles.php') - ], 'config'); - - $stub = __DIR__ . '/../database/migrations/'; - $target = database_path('migrations').'/'; - - $this->publishes([ - $stub.'create_roles_table.php' => $target . '2016_09_04_000000_create_roles_table.php', - $stub.'create_role_user_table.php' => $target . '2016_09_04_100000_create_role_user_table.php' - ], 'migrations'); + // Load migrations + $this->loadMigrationsFrom(__DIR__ . '/../database/migrations/'); - $this->publishes([ - __DIR__.'/../resources/views' => base_path('resources/views/vendor'), - ], 'views'); + // publish config file + $this->publishes([__DIR__ . '/../config/roles.php' => config_path('roles.php')], 'config'); + + // publish views + $this->loadViewsFrom(__DIR__.'/../resources/views', 'roles'); $this->registerBladeExtensions(); } diff --git a/tests/Feature/CreateRoleTest.php b/tests/Feature/CreateRoleTest.php index 3f5f5c5..9ff6218 100644 --- a/tests/Feature/CreateRoleTest.php +++ b/tests/Feature/CreateRoleTest.php @@ -14,7 +14,7 @@ class CreateRoleTest extends TestCase */ public function testCanCreateRole() { - $adminRole = factory(Role::class)->create([ + $adminRole = Role::factory()->create([ 'name' => 'Admin', 'slug' => 'admin', 'description' => 'Custodians of the system.', diff --git a/tests/Feature/RoleMiddlewareTest.php b/tests/Feature/RoleMiddlewareTest.php index 91a9ba3..eb7cbe0 100644 --- a/tests/Feature/RoleMiddlewareTest.php +++ b/tests/Feature/RoleMiddlewareTest.php @@ -6,13 +6,10 @@ use HttpOz\Roles\Tests\Stubs\User; use HttpOz\Roles\Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; -use Illuminate\Foundation\Testing\DatabaseTransactions; class RoleMiddlewareTest extends TestCase { - use DatabaseMigrations; - /** * A basic test example. * @@ -20,8 +17,8 @@ class RoleMiddlewareTest extends TestCase */ public function testForbiddenRoleRoute() { - $createdUser = factory(User::class)->create(); - $createdRole = factory(Role::class)->create([ + $createdUser = User::factory()->create(); + $createdRole = Role::factory()->create([ 'name' => 'Admin', 'slug' => 'admin', ]); @@ -37,9 +34,5 @@ public function testForbiddenRoleRoute() $this->assertEquals($createdRole->id, $foundRole->id); $this->assertEquals($createdUser->name, $foundUser->name); - - /*$this->get('/admin/users') - ->assertStatus(200) - ->assertSee('I am an admin.');*/ } } diff --git a/tests/Feature/UserRoleTest.php b/tests/Feature/UserRoleTest.php index f39aa07..cadc232 100644 --- a/tests/Feature/UserRoleTest.php +++ b/tests/Feature/UserRoleTest.php @@ -4,18 +4,15 @@ use HttpOz\Roles\Tests\Stubs\User; use \HttpOz\Roles\Models\Role; -use Illuminate\Foundation\Testing\DatabaseMigrations; use HttpOz\Roles\Tests\TestCase; class UserRoleTest extends TestCase { - use DatabaseMigrations; - public function testUserHasRoleOnAttach() { - $admin = factory(User::class)->create(); - $role = factory(Role::class)->create(); + $admin = User::factory()->create(); + $role = Role::factory()->create(); $adminRole = Role::findBySlug('admin'); $user = User::find(1); diff --git a/tests/Stubs/User.php b/tests/Stubs/User.php index bcccf59..01a451c 100644 --- a/tests/Stubs/User.php +++ b/tests/Stubs/User.php @@ -2,22 +2,31 @@ namespace HttpOz\Roles\Tests\Stubs; +use HttpOz\Roles\Database\Factories\UserFactory; use \HttpOz\Roles\Traits\HasRole; use \HttpOz\Roles\Contracts\HasRole as HasRoleContract; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Eloquent; class User extends Eloquent implements HasRoleContract { - use HasRole; + use HasRole, HasFactory; /** * Get the name of the unique identifier for the user. * * @return array */ - public function getAuthIdentifiersName() + public function getAuthIdentifiersName(): array { return ['email', 'username']; } + + + + protected static function newFactory() + { + return new UserFactory(); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 4f94a22..baebbb8 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,7 +2,11 @@ namespace HttpOz\Roles\Tests; +use HttpOz\Roles\Models\Role; +use HttpOz\Roles\RolesServiceProvider; use HttpOz\Roles\Tests\Stubs\User; +use Illuminate\Foundation\Application; +use Orchestra\Database\ConsoleServiceProvider; use Orchestra\Testbench\TestCase as Orchestra; class TestCase extends Orchestra @@ -15,21 +19,20 @@ public function setUp(): void parent::setUp(); $this->loadLaravelMigrations(['--database' => 'testbench']); $this->setUpDatabase($this->app); - - $this->withFactories(__DIR__ . '/../database/factories'); } - protected function getPackageProviders($app) + protected function getPackageProviders($app): array { return [ - \Orchestra\Database\ConsoleServiceProvider::class + RolesServiceProvider::class, + ConsoleServiceProvider::class ]; } /** * Define environment setup. * - * @param \Illuminate\Foundation\Application $app + * @param Application $app * @return void */ public function getEnvironmentSetUp($app) @@ -50,7 +53,7 @@ public function getEnvironmentSetUp($app) 'expiry' => 20160, ], 'models' => [ - 'role' => \HttpOz\Roles\Models\Role::class + 'role' => Role::class ], 'pretend' => [ 'enabled' => false, @@ -64,7 +67,7 @@ public function getEnvironmentSetUp($app) /** * Set up the database. * - * @param \Illuminate\Foundation\Application $app + * @param Application $app */ protected function setUpDatabase($app) { diff --git a/tests/Unit/HasRoleTraitTest.php b/tests/Unit/HasRoleTraitTest.php index 045fd22..329399a 100644 --- a/tests/Unit/HasRoleTraitTest.php +++ b/tests/Unit/HasRoleTraitTest.php @@ -6,125 +6,134 @@ use HttpOz\Roles\Tests\Stubs\User; use HttpOz\Roles\Tests\TestCase; -class HasRoleTraitTest extends TestCase { - public function testCanAttachRole() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); +class HasRoleTraitTest extends TestCase +{ + public function testCanAttachRole() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + + $user->attachRole($adminRole); + + $this->assertEquals(1, $user->roles()->count()); + } - $user->attachRole( $adminRole ); + public function testCanDetachRole() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + + $user->attachRole($adminRole); + $user->attachRole($managerRole); + + $user->detachRole($adminRole); + + $this->assertEquals(1, $user->roles()->count()); + $this->assertEquals('manager', $user->roles()->first()->slug); + } + + public function testCanDetachAllRoles() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + + $user->attachRole($adminRole); + $user->attachRole($managerRole); + + $this->assertEquals(2, $user->roles()->count()); + + $user->detachAllRoles(); + + $this->assertEquals(0, $user->roles()->count()); + } + + public function testHasRole() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + + $user->attachRole($adminRole); + + $this->assertFalse($user->hasRole('manager')); + + config(['roles.cache.enabled' => true]); + + $this->assertTrue($user->hasRole('admin')); + $this->assertFalse($user->hasRole('manager')); + } + + public function testIsRole() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + $user->attachRole($adminRole); + + $this->assertTrue($user->isRole($adminRole->slug)); + $this->assertFalse($user->isRole($managerRole->slug)); + + config(['roles.pretend.enabled' => true]); + + + $this->assertTrue($user->isRole($managerRole->slug)); + } + + public function testIsAll() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + $user->attachRole($adminRole); + $user->attachRole($managerRole); + + $this->assertTrue($user->isAll([$adminRole->id, $managerRole->id])); + } + + public function testSyncRoles() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); - $this->assertEquals( 1, $user->roles()->count()); - } + $user->syncRoles([$managerRole->id, $adminRole->id]); - public function testCanDetachRole() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); + $this->assertTrue($user->isAll([$adminRole->id, $managerRole->id])); - $user->attachRole( $adminRole ); - $user->attachRole( $managerRole ); + config(['roles.pretend.enabled' => true]); - $user->detachRole( $adminRole ); + $this->assertTrue($user->isAll([$adminRole->id, $managerRole->id])); + } - $this->assertEquals( 1, $user->roles()->count() ); - $this->assertEquals( 'manager', $user->roles()->first()->slug ); - } + public function testGroup() + { + $user = USer::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + $user->attachRole($adminRole); - public function testCanDetachAllRoles() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); + $this->assertEquals('system.admin', $user->group()); + $this->assertTrue($user->inGroup('system.admin')); + $this->assertFalse($user->inGroup('default')); - $user->attachRole( $adminRole ); - $user->attachRole( $managerRole ); - - $this->assertEquals( 2, $user->roles()->count() ); - - $user->detachAllRoles(); - - $this->assertEquals( 0, $user->roles()->count() ); - } - - public function testHasRole() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); - - $user->attachRole($adminRole); - - $this->assertFalse($user->hasRole('manager')); - - config(['roles.cache.enabled' => true]); - - $this->assertTrue($user->hasRole('admin')); - $this->assertFalse($user->hasRole('manager')); - } - - public function testIsRole() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); - $user->attachRole($adminRole); - - $this->assertTrue($user->isRole($adminRole->slug)); - $this->assertFalse($user->isRole($managerRole->slug)); - - config(['roles.pretend.enabled' => true]); - - - $this->assertTrue($user->isRole($managerRole->slug)); - } - - public function testIsAll() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); - $user->attachRole($adminRole); - $user->attachRole($managerRole); - - $this->assertTrue($user->isAll([$adminRole->id, $managerRole->id])); - } - - public function testSyncRoles() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); - - $user->syncRoles([$managerRole->id, $adminRole->id]); - - $this->assertTrue($user->isAll([$adminRole->id, $managerRole->id])); - - config(['roles.pretend.enabled' => true]); - - $this->assertTrue($user->isAll([$adminRole->id, $managerRole->id])); - } - - public function testGroup() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); - $user->attachRole($adminRole); - - $this->assertEquals('system.admin', $user->group()); - $this->assertTrue($user->inGroup('system.admin')); - $this->assertFalse($user->inGroup('default')); - - } + } } \ No newline at end of file diff --git a/tests/Unit/RoleHasRelationsTraitTest.php b/tests/Unit/RoleHasRelationsTraitTest.php index 0c9f886..13f1b9f 100644 --- a/tests/Unit/RoleHasRelationsTraitTest.php +++ b/tests/Unit/RoleHasRelationsTraitTest.php @@ -10,8 +10,8 @@ class RoleHasRelationsTraitTest extends TestCase { public function testRoleCanFetchUsers() { - $role = factory(Role::class)->create(); - factory(User::class, 10)->create()->each(function ($u) use ($role) { + $role = Role::factory()->create(); + User::factory(10)->create()->each(function ($u) use ($role) { $u->attachRole($role); }); diff --git a/tests/Unit/RoleTest.php b/tests/Unit/RoleTest.php index f87855a..0573083 100644 --- a/tests/Unit/RoleTest.php +++ b/tests/Unit/RoleTest.php @@ -3,16 +3,13 @@ namespace HttpOz\Roles\Tests\Unit; use HttpOz\Roles\Models\Role; -use Illuminate\Foundation\Testing\DatabaseMigrations; use HttpOz\Roles\Tests\TestCase; class RoleTest extends TestCase { - use DatabaseMigrations; - public function testRoleCanBeFoundBySlug() { - $createdRole = factory(Role::class)->create(['name' => 'Admin', 'slug' => 'admin']); + $createdRole = Role::factory()->create(['name' => 'Admin', 'slug' => 'admin']); $foundRole = Role::findBySlug('admin'); $this->assertEquals($createdRole->id, $foundRole->id);