Skip to content
Permalink
Browse files

Episode 31

  • Loading branch information...
JeffreyWay committed Mar 5, 2019
1 parent fdd4a1f commit 296858114ca6e9e3cb18d98369d0d5d8db9cc5dd
Showing with 47 additions and 2 deletions.
  1. +1 −1 app/Http/Controllers/ProjectsController.php
  2. +1 −1 app/Project.php
  3. +14 −0 app/User.php
  4. +8 −0 tests/Feature/ManageProjectsTest.php
  5. +23 −0 tests/Unit/UserTest.php
@@ -13,7 +13,7 @@ class ProjectsController extends Controller
*/
public function index()
{
$projects = auth()->user()->projects;
$projects = auth()->user()->accessibleProjects();
return view('projects.index', compact('projects'));
}
@@ -73,6 +73,6 @@ public function invite(User $user)
*/
public function members()
{
return $this->belongsToMany(User::class, 'project_members');
return $this->belongsToMany(User::class, 'project_members')->withTimestamps();
}
}
@@ -39,4 +39,18 @@ public function projects()
{
return $this->hasMany(Project::class, 'owner_id')->latest('updated_at');
}
/**
* Get all projects that the user has access to.
*
* @return mixed
*/
public function accessibleProjects()
{
return Project::where('owner_id', $this->id)
->orWhereHas('members', function ($query) {
$query->where('user_id', $this->id);
})
->get();
}
}
@@ -49,6 +49,14 @@ public function a_user_can_create_a_project()
->assertSee($attributes['notes']);
}
/** @test */
function a_user_can_see_all_projects_they_have_been_invited_to_on_their_dashboard()
{
$project = tap(ProjectFactory::create())->invite($this->signIn());
$this->get('/projects')->assertSee($project->title);
}
/** @test */
function unauthorized_users_cannot_delete_projects()
{
@@ -2,6 +2,8 @@
namespace Tests\Unit;
use App\User;
use Facades\Tests\Setup\ProjectFactory;
use Tests\TestCase;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -17,4 +19,25 @@ public function a_user_has_projects()
$this->assertInstanceOf(Collection::class, $user->projects);
}
/** @test */
function a_user_has_accessible_projects()
{
$john = $this->signIn();
ProjectFactory::ownedBy($john)->create();
$this->assertCount(1, $john->accessibleProjects());
$sally = factory(User::class)->create();
$nick = factory(User::class)->create();
$project = tap(ProjectFactory::ownedBy($sally)->create())->invite($nick);
$this->assertCount(1, $john->accessibleProjects());
$project->invite($john);
$this->assertCount(2, $john->accessibleProjects());
}
}

0 comments on commit 2968581

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