BooksControllerのaddメソッドをテストしようとしています。条件は、書籍を追加する前にユーザーがログインする必要があることです。しかし、私はテストメソッドでユーザーログインを行う方法を見つけることはありません。認証が必要なアクションをテストするにはどうすればよいですか?
私のコードは、私は/ユーザー/ログインにリダイレクト取得していますので、アサートに失敗しつつあるthis-
public function testAdd()
{
$user = ['email' => '[email protected]', 'password' => 'abcd'];
$this->post('/users/login', $user);
$book = ['title' => 'Foo', 'writer' => 'writer1', 'edition' => '2nd', 'course' => 'CSE', 'description' => 'abcd', 'price' => '200', 'status' => '0', 'user_id' => '1', 'photo' => 'abcd'];
$this->post('/books/add', $book);
$this->assertRedirect('/books');
}
のようなものです。
私のログイン方法は、この問題を解決する方法はありますthis-
//Login Function
public function login()
{
if($this->request->session()->read('Auth.User'))
{
$this->Flash->error(__('You are already logged in.'));
return $this->redirect(['controller' => 'home','action' => 'index']);
}
if($this->request->is('post'))
{
$user = $this->Auth->identify();
if($user)
{
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
}
//In case of bad login
$this->Flash->error('You must login first.');
}
のようなものですか? ありがとうございます!
ありがとうございます!あなたはそれを釘付け! –