Open takanori-matsushita opened 4 years ago
リスト 10.61: 管理者権限の制御をアクションレベルでテストする green ブラウザテストのため省略
リスト 10.62: 削除リンクとユーザー削除に対する統合テスト green テスト用に一時的にuser.blade.phpファイルを変更する。
<li>
<img src={{gravator_for($user, ['size'=>50])}}>
<a href="{{route('users.show', compact('user'))}}">{{$user->name}}</a>
@auth
@if (auth()->user()->admin === true && Auth::id() !== $user->id) //ログイン中のユーザーのadminカラムがtrueかつ、ログイン中のユーザーIDが$userのIDと一致しない際に表示する。
<form action="{{route('users.destroy', compact('user'))}}" method="post" class="delete" style="display: inline">
@method('DELETE')
@csrf
|<button type="submit" value="delete" onclick="return confirm('You sure?')">delete</button>
</form>
@endif
@endauth
</li>
tests/Browser/UsersIndexTest.php
public function testIndexAsAdminIncludingPaginationAndDeleteLinks()
{
$users = $this->twoUsers();
$admin = $users[0];
eval(\Psy\sh());
$this->browse(function (Browser $browser) use ($admin) {
$browser->visit(route('login'))
->type('email', $admin->email)
->type('password', 'password')
->press('Log in!')
->visit(route('users.index'))
->assertPathIs('/users')
->assertSee('Sterling Archer')
->press('delete')
->assertDialogOpened('You sure?') //指定のテキストを含む確認ダイアログが表示されているか
->acceptDialog() //ダイアログのyesのクリックを許可する
->assertDontSee('Sterling Archer')
->click('@Users')
->press('Logout');
});
}
public function testIndexAsNonAdmin()
{
$users = $this->twoUsers();
$nonAdmin = $users[1];
$this->browse(function (Browser $browser) use ($nonAdmin) {
$browser->visit(route('login'))
->type('email', $nonAdmin->email)
->type('password', 'password')
->press('Log in!')
->visit(route('users.index'))
->assertPathIs('/users')
->assertSee('Sterling Archer')
->assertDontSee('delete')
->click('@Users')
->press('Logout');
});
}
}
リスト 10.63: green
php artisan dusk
認証後にloginページに飛ぼうとしたら、rootページにリダイレクトするように設定する app/Http/Middleware/RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect()->route('root'); //rootに変更
}
return $next($request);
}
branch: updating-users