私が望むシナリオを手動でテストしました。Laravelで承認リダイレクトをテストするにはどうすればよいですか?
管理者は、サイトの/codes
セクションにアクセスできます。 通常のユーザーは/dashboard
にリダイレクトされ(302)、/qr
に行くとメッセージSorry you are not allowed there
が返されます。
手動テストは合格ですが、ラベールテストは失敗します。
私はadminユーザーのlaravel 5.1
テスト使用しています:通常のユーザーのための
public function testAdminViewCodes()
{
//create an admin user
$user = factory(App\User::class, 'admin')->create();
$this->actingAs($user)
->visit('/codes')
->seePageIs('/codes')
->see('Codes');
}
テスト:
public function testNormalViewCodesFail()
{
//create a normal user
$normal_user = factory(App\User::class)->create();
//TODO: Fix this failing test FFS
$this->actingAs($normal_user)
->visit('/qr')
->seePageIs('/dashboard')
->see('Sorry you are not allowed there');
}
テスト結果を。この質問は、しかし、別のpertanent問題があるどのくらいのため
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'email' => $faker->email,
'password' => bcrypt(str_random(10)),
'remember_token' => str_random(10),
'is_admin' => false,
];
});
$factory->defineAs(App\User::class, 'admin', function ($faker) use ($factory) {
$user = $factory->raw(App\User::class);
return array_merge($user, ['is_admin' => true]);
});
私の謝罪:私は工場に問題があるかもしれないと思う
There was 1 failure:
1) AdminTest::testNormalViewQRCodesFail
Did not land on expected page [http://localhost/dashboard].
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/dashboard'
+'http://localhost/codes'
は、常に管理者ユーザーを作成しているようです。ミドルウェアは無視され
Route::group(['middleware' => ['auth', 'isadmin']], function()
{
Route::resource('user', 'UserController');
});
:?
Kernel.php
で
<?php
namespace RMS\Http\Middleware;
use Closure;
class IsAdminMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (app()->env === 'testing') {
return $next($request);
}
if (! $request->user()->isAdmin()) {
return redirect()->route('dashboard')
->with('message', 'Sorry you are not allowed there');
}
return $next($request);
}
}
:
protected $routeMiddleware = [
'auth' => \RMS\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \RMS\Http\Middleware\RedirectIfAuthenticated::class,
'isadmin' => \RMS\Http\Middleware\IsAdminMiddleware::class,
];
とルートに適用され、私は、ユーザーが管理者であるかどうかをテストするためにmiddleware
を使用していますか私はuse WithoutMiddleware;
ステートメントを追加してはいけません。
'手動で検査するためにユーザ生成の後にデバッガで壊れます。'どのようにこれを行うのですか? Pythonには 'pdb()'や 'ipdb()'というクールなツールがあり、laravelに相当するものはありますか? – surfer190
PHP用のデバッガは、[XDebug](https://xdebug.org/)と呼ばれています。いくつかのIDEには、例えば[PHPStorm](https://www.jetbrains.com/help/phpstorm/2016.2/configuring-xdebug.html)などの統合サポートがあります。 –
ええ、それはあなたが解釈の途中で点検することはできません...それとも? – surfer190