Skip to content
Permalink
Browse files

Episode 30

  • Loading branch information...
JeffreyWay committed Feb 27, 2019
1 parent a9131be commit fdd4a1fab73f57add3105a6c368f32a2eb296e47
@@ -19,6 +19,6 @@ class ProjectPolicy
*/
public function update(User $user, Project $project)
{
return $user->is($project->owner);
return $user->is($project->owner) || $project->members->contains($user);
}
}
@@ -55,4 +55,24 @@ public function addTask($body)
{
return $this->tasks()->create(compact('body'));
}
/**
* Invite a user to the project.
*
* @param \App\User $user
*/
public function invite(User $user)
{
$this->members()->attach($user);
}
/**
* Get all members that are assigned to the team.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function members()
{
return $this->belongsToMany(User::class, 'project_members');
}
}
@@ -0,0 +1,35 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProjectMembersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('project_members', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('project_id');
$table->unsignedInteger('user_id');
$table->timestamps();
$table->index(['project_id', 'user_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('project_members');
}
}
@@ -0,0 +1,27 @@
<?php
namespace Tests\Feature;
use App\User;
use Facades\Tests\Setup\ProjectFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class InvitationsTest extends TestCase
{
use RefreshDatabase;
/** @test */
function a_project_can_invite_a_user()
{
$project = ProjectFactory::create();
$project->invite($newUser = factory(User::class)->create());
$this
->actingAs($newUser)
->post(action('ProjectTasksController@store', $project), $task = ['body' => 'Foo task']);
$this->assertDatabaseHas('tasks', $task);
}
}
@@ -2,6 +2,7 @@
namespace Tests\Unit;
use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -35,4 +36,14 @@ public function it_can_add_a_task()
$this->assertCount(1, $project->tasks);
$this->assertTrue($project->tasks->contains($task));
}
/** @test */
function it_can_invite_a_user()
{
$project = factory('App\Project')->create();
$project->invite($user = factory(User::class)->create());
$this->assertTrue($project->members->contains($user));
}
}

0 comments on commit fdd4a1f

Please sign in to comment.
You can’t perform that action at this time.