From c4f22416470815a780d36c68fd9c2e9fae663b84 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Wed, 19 Jul 2017 14:27:30 +0800 Subject: [PATCH] Passed ManageProjectTest suite with database migration and sqlite in memory test --- app/Providers/AuthServiceProvider.php | 15 ++++++++++++--- config/database.php | 2 +- database/factories/ModelFactory.php | 11 ++++++++++- phpunit.xml | 3 ++- tests/TestCase.php | 2 ++ tests/functional/ManageProjectsTest.php | 13 ++++++++++++- 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 16005ad..04ac698 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -28,8 +28,8 @@ class AuthServiceProvider extends ServiceProvider // Dynamically register permissions with Laravel's Gate. foreach ($this->getPermissions() as $permission) { - Gate::define($permission->name, function ($user) use ($permission) { - return $user->hasPermission($permission); + Gate::define($permission, function ($user) { + return $user->hasRole('admin'); }); } @@ -57,6 +57,15 @@ class AuthServiceProvider extends ServiceProvider */ protected function getPermissions() { - return Permission::with('roles')->get(); + return [ + 'manage_users', + 'manage_role_permissions', + 'manage_backups', + 'manage_options', + 'manage_projects', + 'manage_payments', + 'manage_subscriptions', + 'see_reports', + ]; } } diff --git a/config/database.php b/config/database.php index edd6425..b149499 100644 --- a/config/database.php +++ b/config/database.php @@ -48,7 +48,7 @@ return [ 'sqlite' => [ 'driver' => 'sqlite', - 'database' => database_path('database.sqlite'), + 'database' => env('DB_DATABASE', database_path('database.sqlite')), 'prefix' => '', ], diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index f4266c4..30ebbfb 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -6,6 +6,7 @@ use App\Entities\Projects\Project; use App\Entities\Projects\Task; use App\Entities\Subscriptions\Subscription; use App\Entities\Users\Event; +use App\Entities\Users\Role; use App\Entities\Users\User; $factory->define(User::class, function (Faker\Generator $faker) { @@ -15,7 +16,15 @@ $factory->define(User::class, function (Faker\Generator $faker) { 'email' => $email = $faker->unique()->email, 'password' => 'member', 'remember_token' => str_random(10), - 'api_token' => bcrypt($email), + 'api_token' => str_random(40), + ]; +}); + +$factory->define(Role::class, function (Faker\Generator $faker) { + return [ + 'type' => 0, + 'name' => $faker->word, + 'label' => $faker->sentence, ]; }); diff --git a/phpunit.xml b/phpunit.xml index 620fbc2..f996508 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -23,7 +23,8 @@ - + + diff --git a/tests/TestCase.php b/tests/TestCase.php index aa86018..0e46e14 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,5 +1,6 @@ create(['name' => 'admin']); $user = factory(User::class)->create(); $user->assignRole('admin'); $this->actingAs($user); diff --git a/tests/functional/ManageProjectsTest.php b/tests/functional/ManageProjectsTest.php index a3a41d4..b6002f2 100644 --- a/tests/functional/ManageProjectsTest.php +++ b/tests/functional/ManageProjectsTest.php @@ -4,6 +4,7 @@ use App\Entities\Payments\Payment; use App\Entities\Projects\Feature; use App\Entities\Projects\Project; use App\Entities\Projects\Task; +use App\Entities\Users\Role; use App\Entities\Users\User; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; @@ -11,11 +12,14 @@ use Illuminate\Foundation\Testing\WithoutMiddleware; class ManageProjectsTest extends TestCase { - use DatabaseTransactions; + use DatabaseMigrations; /** @test */ public function admin_can_input_new_project_with_existing_customer() { + $adminRole = factory(Role::class)->create(['name' => 'admin']); + $customerRole = factory(Role::class)->create(['name' => 'customer']); + $users = factory(User::class, 2)->create(); $users[0]->assignRole('admin'); $this->actingAs($users[0]); @@ -42,6 +46,7 @@ class ManageProjectsTest extends TestCase public function admin_can_input_new_project_with_new_customer() { $this->adminUserSigningIn(); + $customerRole = factory(Role::class)->create(['name' => 'customer']); $this->visit(route('projects.index')); $this->seePageIs(route('projects.index')); @@ -107,6 +112,9 @@ class ManageProjectsTest extends TestCase /** @test */ public function admin_can_edit_a_project() { + $adminRole = factory(Role::class)->create(['name' => 'admin']); + $customerRole = factory(Role::class)->create(['name' => 'customer']); + $users = factory(User::class, 2)->create(); $users[0]->assignRole('admin'); $this->actingAs($users[0]); @@ -142,6 +150,9 @@ class ManageProjectsTest extends TestCase /** @test */ public function form_is_validated_on_invalid_project_entry() { + $adminRole = factory(Role::class)->create(['name' => 'admin']); + $customerRole = factory(Role::class)->create(['name' => 'customer']); + $users = factory(User::class, 2)->create(); $users[0]->assignRole('admin'); $this->actingAs($users[0]);