これは、cakephp テスト ドキュメントにあります。
http://book.cakephp.org/3.0/en/development/testing.html#testing-actions-that-require-authentication
認証が必要なアクションのテスト AuthComponent を使用している場合は、AuthComponent がユーザーの ID を検証するために使用するセッション データをスタブ化する必要があります。これを行うには、IntegrationTestCase のヘルパー メソッドを使用できます。add メソッドを含む ArticlesController があり、その add メソッドに認証が必要であると仮定すると、次のテストを作成できます。
public function testAddUnauthenticatedFails()
{
// No session data set.
$this->get('/articles/add');
$this->assertRedirect(['controller' => 'Users', 'action' => 'login']);
}
public function testAddAuthenticated()
{
// Set session data
$this->session([
'Auth' => [
'User' => [
'id' => 1,
'username' => 'testing',
// other keys.
]
]
]);
$this->get('/articles/add');
$this->assertResponseOk();
// Other assertions.
}
私はこれを使いました
// Set session data
$this->session(['Auth.User.id' => 1]);
私は実際に役割を持っているので、私のソリューションは次のようになります。
public function testDisplay()
{
$this->session(['Auth.User.id' => 1, 'Auth.User.role' => 'admin']);
$this->get('/pages/home');
$this->assertResponseOk();
$this->assertResponseContains('CakePHP');
$this->assertResponseContains('<html>');
}